You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

nanovg.h 27KB

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