nanovg.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. //
  2. // Copyright (c) 2013 Mikko Mononen [email protected]
  3. //
  4. // This software is provided 'as-is', without any express or implied
  5. // warranty. In no event will the authors be held liable for any damages
  6. // arising from the use of this software.
  7. // Permission is granted to anyone to use this software for any purpose,
  8. // including commercial applications, and to alter it and redistribute it
  9. // freely, subject to the following restrictions:
  10. // 1. The origin of this software must not be misrepresented; you must not
  11. // claim that you wrote the original software. If you use this software
  12. // in a product, an acknowledgment in the product documentation would be
  13. // appreciated but is not required.
  14. // 2. Altered source versions must be plainly marked as such, and must not be
  15. // misrepresented as being the original software.
  16. // 3. This notice may not be removed or altered from any source distribution.
  17. //
  18. #ifndef NANOVG_H
  19. #define NANOVG_H
  20. #include "nanovg_bgfx.h"
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. #define NVG_PI 3.14159265358979323846264338327f
  25. #ifdef _MSC_VER
  26. #pragma warning(push)
  27. #pragma warning(disable: 4201) // nonstandard extension used : nameless struct/union
  28. #endif
  29. typedef struct NVGcontext NVGcontext;
  30. struct NVGcolor {
  31. union {
  32. float rgba[4];
  33. struct {
  34. float r,g,b,a;
  35. };
  36. };
  37. };
  38. typedef struct NVGcolor NVGcolor;
  39. struct NVGpaint {
  40. float xform[6];
  41. float extent[2];
  42. float radius;
  43. float feather;
  44. NVGcolor innerColor;
  45. NVGcolor outerColor;
  46. int image;
  47. };
  48. typedef struct NVGpaint NVGpaint;
  49. enum NVGwinding {
  50. NVG_CCW = 1, // Winding for solid shapes
  51. NVG_CW = 2, // Winding for holes
  52. };
  53. enum NVGsolidity {
  54. NVG_SOLID = 1, // CCW
  55. NVG_HOLE = 2, // CW
  56. };
  57. enum NVGlineCap {
  58. NVG_BUTT,
  59. NVG_ROUND,
  60. NVG_SQUARE,
  61. NVG_BEVEL,
  62. NVG_MITER,
  63. };
  64. enum NVGalign {
  65. // Horizontal align
  66. NVG_ALIGN_LEFT = 1<<0, // Default, align text horizontally to left.
  67. NVG_ALIGN_CENTER = 1<<1, // Align text horizontally to center.
  68. NVG_ALIGN_RIGHT = 1<<2, // Align text horizontally to right.
  69. // Vertical align
  70. NVG_ALIGN_TOP = 1<<3, // Align text vertically to top.
  71. NVG_ALIGN_MIDDLE = 1<<4, // Align text vertically to middle.
  72. NVG_ALIGN_BOTTOM = 1<<5, // Align text vertically to bottom.
  73. NVG_ALIGN_BASELINE = 1<<6, // Default, align text vertically to baseline.
  74. };
  75. enum NVGblendFactor {
  76. NVG_ZERO = 1<<0,
  77. NVG_ONE = 1<<1,
  78. NVG_SRC_COLOR = 1<<2,
  79. NVG_ONE_MINUS_SRC_COLOR = 1<<3,
  80. NVG_DST_COLOR = 1<<4,
  81. NVG_ONE_MINUS_DST_COLOR = 1<<5,
  82. NVG_SRC_ALPHA = 1<<6,
  83. NVG_ONE_MINUS_SRC_ALPHA = 1<<7,
  84. NVG_DST_ALPHA = 1<<8,
  85. NVG_ONE_MINUS_DST_ALPHA = 1<<9,
  86. NVG_SRC_ALPHA_SATURATE = 1<<10,
  87. };
  88. enum NVGcompositeOperation {
  89. NVG_SOURCE_OVER,
  90. NVG_SOURCE_IN,
  91. NVG_SOURCE_OUT,
  92. NVG_ATOP,
  93. NVG_DESTINATION_OVER,
  94. NVG_DESTINATION_IN,
  95. NVG_DESTINATION_OUT,
  96. NVG_DESTINATION_ATOP,
  97. NVG_LIGHTER,
  98. NVG_COPY,
  99. NVG_XOR,
  100. };
  101. struct NVGcompositeOperationState {
  102. int srcRGB;
  103. int dstRGB;
  104. int srcAlpha;
  105. int dstAlpha;
  106. };
  107. typedef struct NVGcompositeOperationState NVGcompositeOperationState;
  108. struct NVGglyphPosition {
  109. const char* str; // Position of the glyph in the input string.
  110. float x; // The x-coordinate of the logical glyph position.
  111. float minx, maxx; // The bounds of the glyph shape.
  112. };
  113. typedef struct NVGglyphPosition NVGglyphPosition;
  114. struct NVGtextRow {
  115. const char* start; // Pointer to the input text where the row starts.
  116. const char* end; // Pointer to the input text where the row ends (one past the last character).
  117. const char* next; // Pointer to the beginning of the next row.
  118. float width; // Logical width of the row.
  119. float minx, maxx; // Actual bounds of the row. Logical with and bounds can differ because of kerning and some parts over extending.
  120. };
  121. typedef struct NVGtextRow NVGtextRow;
  122. enum NVGimageFlags {
  123. NVG_IMAGE_GENERATE_MIPMAPS = 1<<0, // Generate mipmaps during creation of the image.
  124. NVG_IMAGE_REPEATX = 1<<1, // Repeat image in X direction.
  125. NVG_IMAGE_REPEATY = 1<<2, // Repeat image in Y direction.
  126. NVG_IMAGE_FLIPY = 1<<3, // Flips (inverses) image in Y direction when rendered.
  127. NVG_IMAGE_PREMULTIPLIED = 1<<4, // Image data has premultiplied alpha.
  128. NVG_IMAGE_NEAREST = 1<<5, // Image interpolation is Nearest instead Linear
  129. };
  130. // Begin drawing a new frame
  131. // Calls to nanovg drawing API should be wrapped in nvgBeginFrame() & nvgEndFrame()
  132. // nvgBeginFrame() defines the size of the window to render to in relation currently
  133. // set viewport (i.e. glViewport on GL backends). Device pixel ration allows to
  134. // control the rendering on Hi-DPI devices.
  135. // For example, GLFW returns two dimension for an opened window: window size and
  136. // frame buffer size. In that case you would set windowWidth/Height to the window size
  137. // devicePixelRatio to: frameBufferWidth / windowWidth.
  138. void nvgBeginFrame(NVGcontext* ctx, float windowWidth, float windowHeight, float devicePixelRatio);
  139. // Cancels drawing the current frame.
  140. void nvgCancelFrame(NVGcontext* ctx);
  141. // Ends drawing flushing remaining render state.
  142. void nvgEndFrame(NVGcontext* ctx);
  143. //
  144. // Composite operation
  145. //
  146. // The composite operations in NanoVG are modeled after HTML Canvas API, and
  147. // the blend func is based on OpenGL (see corresponding manuals for more info).
  148. // The colors in the blending state have premultiplied alpha.
  149. // Sets the composite operation. The op parameter should be one of NVGcompositeOperation.
  150. void nvgGlobalCompositeOperation(NVGcontext* ctx, int op);
  151. // Sets the composite operation with custom pixel arithmetic. The parameters should be one of NVGblendFactor.
  152. void nvgGlobalCompositeBlendFunc(NVGcontext* ctx, int sfactor, int dfactor);
  153. // Sets the composite operation with custom pixel arithmetic for RGB and alpha components separately. The parameters should be one of NVGblendFactor.
  154. void nvgGlobalCompositeBlendFuncSeparate(NVGcontext* ctx, int srcRGB, int dstRGB, int srcAlpha, int dstAlpha);
  155. //
  156. // Color utils
  157. //
  158. // Colors in NanoVG are stored as unsigned ints in ABGR format.
  159. // Returns a color value from red, green, blue values. Alpha will be set to 255 (1.0f).
  160. NVGcolor nvgRGB(unsigned char r, unsigned char g, unsigned char b);
  161. // Returns a color value from red, green, blue values. Alpha will be set to 1.0f.
  162. NVGcolor nvgRGBf(float r, float g, float b);
  163. // Returns a color value from red, green, blue and alpha values.
  164. NVGcolor nvgRGBA(unsigned char r, unsigned char g, unsigned char b, unsigned char a);
  165. // Returns a color value from red, green, blue and alpha values.
  166. NVGcolor nvgRGBAf(float r, float g, float b, float a);
  167. // Linearly interpolates from color c0 to c1, and returns resulting color value.
  168. NVGcolor nvgLerpRGBA(NVGcolor c0, NVGcolor c1, float u);
  169. // Sets transparency of a color value.
  170. NVGcolor nvgTransRGBA(NVGcolor c0, unsigned char a);
  171. // Sets transparency of a color value.
  172. NVGcolor nvgTransRGBAf(NVGcolor c0, float a);
  173. // Returns color value specified by hue, saturation and lightness.
  174. // HSL values are all in range [0..1], alpha will be set to 255.
  175. NVGcolor nvgHSL(float h, float s, float l);
  176. // Returns color value specified by hue, saturation and lightness and alpha.
  177. // HSL values are all in range [0..1], alpha in range [0..255]
  178. NVGcolor nvgHSLA(float h, float s, float l, unsigned char a);
  179. //
  180. // State Handling
  181. //
  182. // NanoVG contains state which represents how paths will be rendered.
  183. // The state contains transform, fill and stroke styles, text and font styles,
  184. // and scissor clipping.
  185. // Pushes and saves the current render state into a state stack.
  186. // A matching nvgRestore() must be used to restore the state.
  187. void nvgSave(NVGcontext* ctx);
  188. // Pops and restores current render state.
  189. void nvgRestore(NVGcontext* ctx);
  190. // Resets current render state to default values. Does not affect the render state stack.
  191. void nvgReset(NVGcontext* ctx);
  192. //
  193. // Render styles
  194. //
  195. // Fill and stroke render style can be either a solid color or a paint which is a gradient or a pattern.
  196. // Solid color is simply defined as a color value, different kinds of paints can be created
  197. // using nvgLinearGradient(), nvgBoxGradient(), nvgRadialGradient() and nvgImagePattern().
  198. //
  199. // Current render style can be saved and restored using nvgSave() and nvgRestore().
  200. // Sets whether to draw antialias for nvgStroke() and nvgFill(). It's enabled by default.
  201. void nvgShapeAntiAlias(NVGcontext* ctx, int enabled);
  202. // Sets current stroke style to a solid color.
  203. void nvgStrokeColor(NVGcontext* ctx, NVGcolor color);
  204. // Sets current stroke style to a paint, which can be a one of the gradients or a pattern.
  205. void nvgStrokePaint(NVGcontext* ctx, NVGpaint paint);
  206. // Sets current fill style to a solid color.
  207. void nvgFillColor(NVGcontext* ctx, NVGcolor color);
  208. // Sets current fill style to a paint, which can be a one of the gradients or a pattern.
  209. void nvgFillPaint(NVGcontext* ctx, NVGpaint paint);
  210. // Sets the miter limit of the stroke style.
  211. // Miter limit controls when a sharp corner is beveled.
  212. void nvgMiterLimit(NVGcontext* ctx, float limit);
  213. // Sets the stroke width of the stroke style.
  214. void nvgStrokeWidth(NVGcontext* ctx, float size);
  215. // Sets how the end of the line (cap) is drawn,
  216. // Can be one of: NVG_BUTT (default), NVG_ROUND, NVG_SQUARE.
  217. void nvgLineCap(NVGcontext* ctx, int cap);
  218. // Sets how sharp path corners are drawn.
  219. // Can be one of NVG_MITER (default), NVG_ROUND, NVG_BEVEL.
  220. void nvgLineJoin(NVGcontext* ctx, int join);
  221. // Sets the transparency applied to all rendered shapes.
  222. // Already transparent paths will get proportionally more transparent as well.
  223. void nvgGlobalAlpha(NVGcontext* ctx, float alpha);
  224. //
  225. // Transforms
  226. //
  227. // The paths, gradients, patterns and scissor region are transformed by an transformation
  228. // matrix at the time when they are passed to the API.
  229. // The current transformation matrix is a affine matrix:
  230. // [sx kx tx]
  231. // [ky sy ty]
  232. // [ 0 0 1]
  233. // Where: sx,sy define scaling, kx,ky skewing, and tx,ty translation.
  234. // The last row is assumed to be 0,0,1 and is not stored.
  235. //
  236. // Apart from nvgResetTransform(), each transformation function first creates
  237. // specific transformation matrix and pre-multiplies the current transformation by it.
  238. //
  239. // Current coordinate system (transformation) can be saved and restored using nvgSave() and nvgRestore().
  240. // Resets current transform to a identity matrix.
  241. void nvgResetTransform(NVGcontext* ctx);
  242. // Premultiplies current coordinate system by specified matrix.
  243. // The parameters are interpreted as matrix as follows:
  244. // [a c e]
  245. // [b d f]
  246. // [0 0 1]
  247. void nvgTransform(NVGcontext* ctx, float a, float b, float c, float d, float e, float f);
  248. // Translates current coordinate system.
  249. void nvgTranslate(NVGcontext* ctx, float x, float y);
  250. // Rotates current coordinate system. Angle is specified in radians.
  251. void nvgRotate(NVGcontext* ctx, float angle);
  252. // Skews the current coordinate system along X axis. Angle is specified in radians.
  253. void nvgSkewX(NVGcontext* ctx, float angle);
  254. // Skews the current coordinate system along Y axis. Angle is specified in radians.
  255. void nvgSkewY(NVGcontext* ctx, float angle);
  256. // Scales the current coordinate system.
  257. void nvgScale(NVGcontext* ctx, float x, float y);
  258. // Stores the top part (a-f) of the current transformation matrix in to the specified buffer.
  259. // [a c e]
  260. // [b d f]
  261. // [0 0 1]
  262. // There should be space for 6 floats in the return buffer for the values a-f.
  263. void nvgCurrentTransform(NVGcontext* ctx, float* xform);
  264. // The following functions can be used to make calculations on 2x3 transformation matrices.
  265. // A 2x3 matrix is represented as float[6].
  266. // Sets the transform to identity matrix.
  267. void nvgTransformIdentity(float* dst);
  268. // Sets the transform to translation matrix matrix.
  269. void nvgTransformTranslate(float* dst, float tx, float ty);
  270. // Sets the transform to scale matrix.
  271. void nvgTransformScale(float* dst, float sx, float sy);
  272. // Sets the transform to rotate matrix. Angle is specified in radians.
  273. void nvgTransformRotate(float* dst, float a);
  274. // Sets the transform to skew-x matrix. Angle is specified in radians.
  275. void nvgTransformSkewX(float* dst, float a);
  276. // Sets the transform to skew-y matrix. Angle is specified in radians.
  277. void nvgTransformSkewY(float* dst, float a);
  278. // Sets the transform to the result of multiplication of two transforms, of A = A*B.
  279. void nvgTransformMultiply(float* dst, const float* src);
  280. // Sets the transform to the result of multiplication of two transforms, of A = B*A.
  281. void nvgTransformPremultiply(float* dst, const float* src);
  282. // Sets the destination to inverse of specified transform.
  283. // Returns 1 if the inverse could be calculated, else 0.
  284. int nvgTransformInverse(float* dst, const float* src);
  285. // Transform a point by given transform.
  286. void nvgTransformPoint(float* dstx, float* dsty, const float* xform, float srcx, float srcy);
  287. // Converts degrees to radians and vice versa.
  288. float nvgDegToRad(float deg);
  289. float nvgRadToDeg(float rad);
  290. //
  291. // Images
  292. //
  293. // NanoVG allows you to load jpg, png, psd, tga, pic and gif files to be used for rendering.
  294. // In addition you can upload your own image. The image loading is provided by stb_image.
  295. // The parameter imageFlags is combination of flags defined in NVGimageFlags.
  296. // Creates image from specified image data.
  297. // Returns handle to the image.
  298. int nvgCreateImageRGBA(NVGcontext* ctx, int w, int h, int imageFlags, const unsigned char* data);
  299. // Updates image data specified by image handle.
  300. void nvgUpdateImage(NVGcontext* ctx, int image, const unsigned char* data);
  301. // Returns the dimensions of a created image.
  302. void nvgImageSize(NVGcontext* ctx, int image, int* w, int* h);
  303. // Deletes created image.
  304. void nvgDeleteImage(NVGcontext* ctx, int image);
  305. //
  306. // Paints
  307. //
  308. // NanoVG supports four types of paints: linear gradient, box gradient, radial gradient and image pattern.
  309. // These can be used as paints for strokes and fills.
  310. // Creates and returns a linear gradient. Parameters (sx,sy)-(ex,ey) specify the start and end coordinates
  311. // of the linear gradient, icol specifies the start color and ocol the end color.
  312. // The gradient is transformed by the current transform when it is passed to nvgFillPaint() or nvgStrokePaint().
  313. NVGpaint nvgLinearGradient(NVGcontext* ctx, float sx, float sy, float ex, float ey,
  314. NVGcolor icol, NVGcolor ocol);
  315. // Creates and returns a box gradient. Box gradient is a feathered rounded rectangle, it is useful for rendering
  316. // drop shadows or highlights for boxes. Parameters (x,y) define the top-left corner of the rectangle,
  317. // (w,h) define the size of the rectangle, r defines the corner radius, and f feather. Feather defines how blurry
  318. // the border of the rectangle is. Parameter icol specifies the inner color and ocol the outer color of the gradient.
  319. // The gradient is transformed by the current transform when it is passed to nvgFillPaint() or nvgStrokePaint().
  320. NVGpaint nvgBoxGradient(NVGcontext* ctx, float x, float y, float w, float h,
  321. float r, float f, NVGcolor icol, NVGcolor ocol);
  322. // Creates and returns a radial gradient. Parameters (cx,cy) specify the center, inr and outr specify
  323. // the inner and outer radius of the gradient, icol specifies the start color and ocol the end color.
  324. // The gradient is transformed by the current transform when it is passed to nvgFillPaint() or nvgStrokePaint().
  325. NVGpaint nvgRadialGradient(NVGcontext* ctx, float cx, float cy, float inr, float outr,
  326. NVGcolor icol, NVGcolor ocol);
  327. // Creates and returns an image pattern. Parameters (ox,oy) specify the left-top location of the image pattern,
  328. // (ex,ey) the size of one image, angle rotation around the top-left corner, image is handle to the image to render.
  329. // The gradient is transformed by the current transform when it is passed to nvgFillPaint() or nvgStrokePaint().
  330. NVGpaint nvgImagePattern(NVGcontext* ctx, float ox, float oy, float ex, float ey,
  331. float angle, int image, float alpha);
  332. //
  333. // Scissoring
  334. //
  335. // Scissoring allows you to clip the rendering into a rectangle. This is useful for various
  336. // user interface cases like rendering a text edit or a timeline.
  337. // Sets the current scissor rectangle.
  338. // The scissor rectangle is transformed by the current transform.
  339. void nvgScissor(NVGcontext* ctx, float x, float y, float w, float h);
  340. // Intersects current scissor rectangle with the specified rectangle.
  341. // The scissor rectangle is transformed by the current transform.
  342. // Note: in case the rotation of previous scissor rect differs from
  343. // the current one, the intersection will be done between the specified
  344. // rectangle and the previous scissor rectangle transformed in the current
  345. // transform space. The resulting shape is always rectangle.
  346. void nvgIntersectScissor(NVGcontext* ctx, float x, float y, float w, float h);
  347. // Reset and disables scissoring.
  348. void nvgResetScissor(NVGcontext* ctx);
  349. //
  350. // Paths
  351. //
  352. // Drawing a new shape starts with nvgBeginPath(), it clears all the currently defined paths.
  353. // Then you define one or more paths and sub-paths which describe the shape. The are functions
  354. // to draw common shapes like rectangles and circles, and lower level step-by-step functions,
  355. // which allow to define a path curve by curve.
  356. //
  357. // NanoVG uses even-odd fill rule to draw the shapes. Solid shapes should have counter clockwise
  358. // winding and holes should have counter clockwise order. To specify winding of a path you can
  359. // call nvgPathWinding(). This is useful especially for the common shapes, which are drawn CCW.
  360. //
  361. // Finally you can fill the path using current fill style by calling nvgFill(), and stroke it
  362. // with current stroke style by calling nvgStroke().
  363. //
  364. // The curve segments and sub-paths are transformed by the current transform.
  365. // Clears the current path and sub-paths.
  366. void nvgBeginPath(NVGcontext* ctx);
  367. // Starts new sub-path with specified point as first point.
  368. void nvgMoveTo(NVGcontext* ctx, float x, float y);
  369. // Adds line segment from the last point in the path to the specified point.
  370. void nvgLineTo(NVGcontext* ctx, float x, float y);
  371. // Adds cubic bezier segment from last point in the path via two control points to the specified point.
  372. void nvgBezierTo(NVGcontext* ctx, float c1x, float c1y, float c2x, float c2y, float x, float y);
  373. // Adds quadratic bezier segment from last point in the path via a control point to the specified point.
  374. void nvgQuadTo(NVGcontext* ctx, float cx, float cy, float x, float y);
  375. // Adds an arc segment at the corner defined by the last path point, and two specified points.
  376. void nvgArcTo(NVGcontext* ctx, float x1, float y1, float x2, float y2, float radius);
  377. // Closes current sub-path with a line segment.
  378. void nvgClosePath(NVGcontext* ctx);
  379. // Sets the current sub-path winding, see NVGwinding and NVGsolidity.
  380. void nvgPathWinding(NVGcontext* ctx, int dir);
  381. // Creates new circle arc shaped sub-path. The arc center is at cx,cy, the arc radius is r,
  382. // and the arc is drawn from angle a0 to a1, and swept in direction dir (NVG_CCW, or NVG_CW).
  383. // Angles are specified in radians.
  384. void nvgArc(NVGcontext* ctx, float cx, float cy, float r, float a0, float a1, int dir);
  385. // Creates new rectangle shaped sub-path.
  386. void nvgRect(NVGcontext* ctx, float x, float y, float w, float h);
  387. // Creates new rounded rectangle shaped sub-path.
  388. void nvgRoundedRect(NVGcontext* ctx, float x, float y, float w, float h, float r);
  389. // Creates new rounded rectangle shaped sub-path with varying radii for each corner.
  390. void nvgRoundedRectVarying(NVGcontext* ctx, float x, float y, float w, float h, float radTopLeft, float radTopRight, float radBottomRight, float radBottomLeft);
  391. // Creates new ellipse shaped sub-path.
  392. void nvgEllipse(NVGcontext* ctx, float cx, float cy, float rx, float ry);
  393. // Creates new circle shaped sub-path.
  394. void nvgCircle(NVGcontext* ctx, float cx, float cy, float r);
  395. // Fills the current path with current fill style.
  396. void nvgFill(NVGcontext* ctx);
  397. // Fills the current path with current stroke style.
  398. void nvgStroke(NVGcontext* ctx);
  399. //
  400. // Text
  401. //
  402. // NanoVG allows you to load .ttf files and use the font to render text.
  403. //
  404. // The appearance of the text can be defined by setting the current text style
  405. // and by specifying the fill color. Common text and font settings such as
  406. // font size, letter spacing and text align are supported. Font blur allows you
  407. // to create simple text effects such as drop shadows.
  408. //
  409. // At render time the font face can be set based on the font handles or name.
  410. //
  411. // Font measure functions return values in local space, the calculations are
  412. // carried in the same resolution as the final rendering. This is done because
  413. // the text glyph positions are snapped to the nearest pixels sharp rendering.
  414. //
  415. // The local space means that values are not rotated or scale as per the current
  416. // transformation. For example if you set font size to 12, which would mean that
  417. // line height is 16, then regardless of the current scaling and rotation, the
  418. // returned line height is always 16. Some measures may vary because of the scaling
  419. // since aforementioned pixel snapping.
  420. //
  421. // While this may sound a little odd, the setup allows you to always render the
  422. // same way regardless of scaling. I.e. following works regardless of scaling:
  423. //
  424. // const char* txt = "Text me up.";
  425. // nvgTextBounds(vg, x,y, txt, NULL, bounds);
  426. // nvgBeginPath(vg);
  427. // nvgRoundedRect(vg, bounds[0],bounds[1], bounds[2]-bounds[0], bounds[3]-bounds[1]);
  428. // nvgFill(vg);
  429. //
  430. // Note: currently only solid color fill is supported for text.
  431. // Creates font by loading it from the disk from specified file name.
  432. // Returns handle to the font.
  433. int nvgCreateFont(NVGcontext* ctx, const char* name, const char* filename);
  434. // Creates font by loading it from the specified memory chunk.
  435. // Returns handle to the font.
  436. int nvgCreateFontMem(NVGcontext* ctx, const char* name, unsigned char* data, int ndata, int freeData);
  437. // Finds a loaded font of specified name, and returns handle to it, or -1 if the font is not found.
  438. int nvgFindFont(NVGcontext* ctx, const char* name);
  439. // Adds a fallback font by handle.
  440. int nvgAddFallbackFontId(NVGcontext* ctx, int baseFont, int fallbackFont);
  441. // Adds a fallback font by name.
  442. int nvgAddFallbackFont(NVGcontext* ctx, const char* baseFont, const char* fallbackFont);
  443. // Sets the font size of current text style.
  444. void nvgFontSize(NVGcontext* ctx, float size);
  445. // Sets the blur of current text style.
  446. void nvgFontBlur(NVGcontext* ctx, float blur);
  447. // Sets the letter spacing of current text style.
  448. void nvgTextLetterSpacing(NVGcontext* ctx, float spacing);
  449. // Sets the proportional line height of current text style. The line height is specified as multiple of font size.
  450. void nvgTextLineHeight(NVGcontext* ctx, float lineHeight);
  451. // Sets the text align of current text style, see NVGalign for options.
  452. void nvgTextAlign(NVGcontext* ctx, int align);
  453. // Sets the font face based on specified id of current text style.
  454. void nvgFontFaceId(NVGcontext* ctx, int font);
  455. // Sets the font face based on specified name of current text style.
  456. void nvgFontFace(NVGcontext* ctx, const char* font);
  457. // Draws text string at specified location. If end is specified only the sub-string up to the end is drawn.
  458. float nvgText(NVGcontext* ctx, float x, float y, const char* string, const char* end);
  459. // Draws multi-line text string at specified location wrapped at the specified width. If end is specified only the sub-string up to the end is drawn.
  460. // White space is stripped at the beginning of the rows, the text is split at word boundaries or when new-line characters are encountered.
  461. // Words longer than the max width are slit at nearest character (i.e. no hyphenation).
  462. void nvgTextBox(NVGcontext* ctx, float x, float y, float breakRowWidth, const char* string, const char* end);
  463. // Measures the specified text string. Parameter bounds should be a pointer to float[4],
  464. // if the bounding box of the text should be returned. The bounds value are [xmin,ymin, xmax,ymax]
  465. // Returns the horizontal advance of the measured text (i.e. where the next character should drawn).
  466. // Measured values are returned in local coordinate space.
  467. float nvgTextBounds(NVGcontext* ctx, float x, float y, const char* string, const char* end, float* bounds);
  468. // Measures the specified multi-text string. Parameter bounds should be a pointer to float[4],
  469. // if the bounding box of the text should be returned. The bounds value are [xmin,ymin, xmax,ymax]
  470. // Measured values are returned in local coordinate space.
  471. void nvgTextBoxBounds(NVGcontext* ctx, float x, float y, float breakRowWidth, const char* string, const char* end, float* bounds);
  472. // Calculates the glyph x positions of the specified text. If end is specified only the sub-string will be used.
  473. // Measured values are returned in local coordinate space.
  474. int nvgTextGlyphPositions(NVGcontext* ctx, float x, float y, const char* string, const char* end, NVGglyphPosition* positions, int maxPositions);
  475. // Returns the vertical metrics based on the current text style.
  476. // Measured values are returned in local coordinate space.
  477. void nvgTextMetrics(NVGcontext* ctx, float* ascender, float* descender, float* lineh);
  478. // Breaks the specified text into lines. If end is specified only the sub-string will be used.
  479. // White space is stripped at the beginning of the rows, the text is split at word boundaries or when new-line characters are encountered.
  480. // Words longer than the max width are slit at nearest character (i.e. no hyphenation).
  481. int nvgTextBreakLines(NVGcontext* ctx, const char* string, const char* end, float breakRowWidth, NVGtextRow* rows, int maxRows);
  482. //
  483. // Internal Render API
  484. //
  485. enum NVGtexture {
  486. NVG_TEXTURE_ALPHA = 0x01,
  487. NVG_TEXTURE_RGBA = 0x02,
  488. };
  489. struct NVGscissor {
  490. float xform[6];
  491. float extent[2];
  492. };
  493. typedef struct NVGscissor NVGscissor;
  494. struct NVGvertex {
  495. float x,y,u,v;
  496. };
  497. typedef struct NVGvertex NVGvertex;
  498. struct NVGpath {
  499. int first;
  500. int count;
  501. unsigned char closed;
  502. int nbevel;
  503. NVGvertex* fill;
  504. int nfill;
  505. NVGvertex* stroke;
  506. int nstroke;
  507. int winding;
  508. int convex;
  509. };
  510. typedef struct NVGpath NVGpath;
  511. struct NVGparams {
  512. void* userPtr;
  513. int edgeAntiAlias;
  514. int (*renderCreate)(void* uptr);
  515. int (*renderCreateTexture)(void* uptr, int type, int w, int h, int imageFlags, const unsigned char* data);
  516. int (*renderDeleteTexture)(void* uptr, int image);
  517. int (*renderUpdateTexture)(void* uptr, int image, int x, int y, int w, int h, const unsigned char* data);
  518. int (*renderGetTextureSize)(void* uptr, int image, int* w, int* h);
  519. void (*renderViewport)(void* uptr, float width, float height, float devicePixelRatio);
  520. void (*renderCancel)(void* uptr);
  521. void (*renderFlush)(void* uptr);
  522. void (*renderFill)(void* uptr, NVGpaint* paint, NVGcompositeOperationState compositeOperation, NVGscissor* scissor, float fringe, const float* bounds, const NVGpath* paths, int npaths);
  523. void (*renderStroke)(void* uptr, NVGpaint* paint, NVGcompositeOperationState compositeOperation, NVGscissor* scissor, float fringe, float strokeWidth, const NVGpath* paths, int npaths);
  524. void (*renderTriangles)(void* uptr, NVGpaint* paint, NVGcompositeOperationState compositeOperation, NVGscissor* scissor, const NVGvertex* verts, int nverts);
  525. void (*renderDelete)(void* uptr);
  526. };
  527. typedef struct NVGparams NVGparams;
  528. // Constructor and destructor, called by the render back-end.
  529. NVGcontext* nvgCreateInternal(NVGparams* params);
  530. void nvgDeleteInternal(NVGcontext* ctx);
  531. NVGparams* nvgInternalParams(NVGcontext* ctx);
  532. // Debug function to dump cached path data.
  533. void nvgDebugDumpPathCache(NVGcontext* ctx);
  534. #ifdef _MSC_VER
  535. #pragma warning(pop)
  536. #endif
  537. #define NVG_NOTUSED(v) for (;;) { (void)(1 ? (void)0 : ( (void)(v) ) ); break; }
  538. #ifdef __cplusplus
  539. }
  540. #endif
  541. #endif // NANOVG_H