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.

482 lines
19KB

  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. struct NVGcontext;
  25. struct NVGcolor
  26. {
  27. union
  28. {
  29. float rgba[4];
  30. struct
  31. {
  32. float r,g,b,a;
  33. };
  34. };
  35. };
  36. struct NVGpaint
  37. {
  38. float xform[6];
  39. float extent[2];
  40. float radius;
  41. float feather;
  42. struct NVGcolor innerColor;
  43. struct NVGcolor outerColor;
  44. int image;
  45. int repeat;
  46. };
  47. enum NVGwinding {
  48. NVG_CCW = 1, // Winding for solid shapes
  49. NVG_CW = 2, // Winding for holes
  50. };
  51. enum NVGsolidity {
  52. NVG_SOLID = 1, // CCW
  53. NVG_HOLE = 2, // CW
  54. };
  55. enum NVGlineCap {
  56. NVG_BUTT,
  57. NVG_ROUND,
  58. NVG_SQUARE,
  59. NVG_BEVEL,
  60. NVG_MITER,
  61. };
  62. enum NVGpatternRepeat {
  63. NVG_REPEATX = 0x01, // Repeat image pattern in X direction
  64. NVG_REPEATY = 0x02, // Repeat image pattern in Y direction
  65. };
  66. enum NVGalign {
  67. // Horizontal align
  68. NVG_ALIGN_LEFT = 1<<0, // Default, align text horizontally to left.
  69. NVG_ALIGN_CENTER = 1<<1, // Align text horizontally to center.
  70. NVG_ALIGN_RIGHT = 1<<2, // Align text horizontally to right.
  71. // Vertical align
  72. NVG_ALIGN_TOP = 1<<3, // Align text vertically to top.
  73. NVG_ALIGN_MIDDLE = 1<<4, // Align text vertically to middle.
  74. NVG_ALIGN_BOTTOM = 1<<5, // Align text vertically to bottom.
  75. NVG_ALIGN_BASELINE = 1<<6, // Default, align text vertically to baseline.
  76. };
  77. enum NVGalpha {
  78. NVG_STRAIGHT_ALPHA,
  79. NVG_PREMULTIPLIED_ALPHA,
  80. };
  81. // Begin drawing a new frame
  82. // Calls to nanovg drawing API should be wrapped in nvgBeginFrame() & nvgEndFrame()
  83. // nvgBeginFrame() defines the size of the window to render to in relation currently
  84. // set viewport (i.e. glViewport on GL backends). Device pixel ration allows to
  85. // control the rendering on Hi-DPI devices.
  86. // For example, GLFW returns two dimension for an opened window: window size and
  87. // frame buffer size. In that case you would set windowWidth/Height to the window size
  88. // devicePixelRatio to: frameBufferWidth / windowWidth.
  89. // AlphaBlend controls if drawing the shapes to the render target should be done using straight or
  90. // premultiplied alpha. If rendering directly to framebuffer you probably want to use NVG_STRAIGHT_ALPHA,
  91. // if rendering to texture which should contain transparent regions NVG_PREMULTIPLIED_ALPHA is the
  92. // right choice.
  93. void nvgBeginFrame(struct NVGcontext* ctx, int windowWidth, int windowHeight, float devicePixelRatio, int alphaBlend);
  94. // Ends drawing flushing remaining render state.
  95. void nvgEndFrame(struct NVGcontext* ctx);
  96. //
  97. // Color utils
  98. //
  99. // Colors in NanoVG are stored as unsigned ints in ABGR format.
  100. // Returns a color value from red, green, blue values. Alpha will be set to 255.
  101. struct NVGcolor nvgRGB(unsigned char r, unsigned char g, unsigned char b);
  102. // Returns a color value from red, green, blue and alpha values.
  103. struct NVGcolor nvgRGBA(unsigned char r, unsigned char g, unsigned char b, unsigned char a);
  104. // Linearly interpoaltes from color c0 to c1, and returns resulting color value.
  105. struct NVGcolor nvgLerpRGBA(struct NVGcolor c0, struct NVGcolor c1, float u);
  106. // Sets transparency of a color value.
  107. struct NVGcolor nvgTransRGBA(struct NVGcolor c0, unsigned char a);
  108. // Returns color value specified by hue, saturation and lightness.
  109. // HSL values are all in range [0..1], alpha will be set to 255.
  110. struct NVGcolor nvgHSL(float h, float s, float l);
  111. // Returns color value specified by hue, saturation and lightness and alpha.
  112. // HSL values are all in range [0..1], alpha in range [0..255]
  113. struct NVGcolor nvgHSLA(float h, float s, float l, unsigned char a);
  114. // Returns 1 if col.rgba is 0.0f,0.0f,0.0f,0.0f, 0 otherwise
  115. int nvgIsBlack( struct NVGcolor col );
  116. //
  117. // State Handling
  118. //
  119. // NanoVG contains state which represents how paths will be rendered.
  120. // The state contains transform, fill and stroke styles, text and font styles,
  121. // and scissor clipping.
  122. // Pushes and saves the current render state into a state stack.
  123. // A matching nvgRestore() must be used to restore the state.
  124. void nvgSave(struct NVGcontext* ctx);
  125. // Pops and restores current render state.
  126. void nvgRestore(struct NVGcontext* ctx);
  127. // Resets current render state to default values. Does not affect the render state stack.
  128. void nvgReset(struct NVGcontext* ctx);
  129. //
  130. // Render styles
  131. //
  132. // Fill and stroke render style can be either a solid color or a paint which is a gradient or a pattern.
  133. // Solid color is simply defined as a color value, different kinds of paints can be created
  134. // using nvgLinearGradient(), nvgBoxGradient(), nvgRadialGradient() and nvgImagePattern().
  135. //
  136. // Current render style can be saved and restored using nvgSave() and nvgRestore().
  137. // Sets current stroke style to a solid color.
  138. void nvgStrokeColor(struct NVGcontext* ctx, struct NVGcolor color);
  139. // Sets current stroke style to a paint, which can be a one of the gradients or a pattern.
  140. void nvgStrokePaint(struct NVGcontext* ctx, struct NVGpaint paint);
  141. // Sets current fill cstyle to a solid color.
  142. void nvgFillColor(struct NVGcontext* ctx, struct NVGcolor color);
  143. // Sets current fill style to a paint, which can be a one of the gradients or a pattern.
  144. void nvgFillPaint(struct NVGcontext* ctx, struct NVGpaint paint);
  145. // Sets the miter limit of the stroke style.
  146. // Miter limit controls when a sharp corner is beveled.
  147. void nvgMiterLimit(struct NVGcontext* ctx, float limit);
  148. // Sets the stroke witdth of the stroke style.
  149. void nvgStrokeWidth(struct NVGcontext* ctx, float size);
  150. // Sets how the end of the line (cap) is drawn,
  151. // Can be one of: NVG_BUTT (default), NVG_ROUND, NVG_SQUARE.
  152. void nvgLineCap(struct NVGcontext* ctx, int cap);
  153. // Sets how sharp path corners are drawn.
  154. // Can be one of NVG_MITER (default), NVG_ROUND, NVG_BEVEL.
  155. void nvgLineJoin(struct NVGcontext* ctx, int join);
  156. //
  157. // Transforms
  158. //
  159. // The paths, gradients, patterns and scissor region are transformed by an transformation
  160. // matrix at the time when they are passed to the API.
  161. // The current transformation matrix is a affine matrix:
  162. // [sx kx tx]
  163. // [ky sy ty]
  164. // [ 0 0 1]
  165. // Where: sx,sy define scaling, kx,ky skewing, and tx,ty translation.
  166. // The last row is assumed to be 0,0,1 and is not stored.
  167. //
  168. // Apart from nvgResetTransform(), each transformation function first creates
  169. // specific transformation matrix and pre-multiplies the current transformation by it.
  170. //
  171. // Current coordinate system (transformation) can be saved and restored using nvgSave() and nvgRestore().
  172. // Resets current transform to a identity matrix.
  173. void nvgResetTransform(struct NVGcontext* ctx);
  174. // Premultiplies current coordinate system by specified matrix.
  175. // The parameters are interpreted as matrix as follows:
  176. // [a c e]
  177. // [b d f]
  178. // [0 0 1]
  179. void nvgTransform(struct NVGcontext* ctx, float a, float b, float c, float d, float e, float f);
  180. // Translates current coordinate system.
  181. void nvgTranslate(struct NVGcontext* ctx, float x, float y);
  182. // Rotates current coordinate system.
  183. void nvgRotate(struct NVGcontext* ctx, float angle);
  184. // Scales the current coordinat system.
  185. void nvgScale(struct NVGcontext* ctx, float x, float y);
  186. //
  187. // Images
  188. //
  189. // NanoVG allows you to load jpg, png, psd, tga, pic and gif files to be used for rendering.
  190. // In addition you can upload your own image. The image loading is provided by stb_image.
  191. // Creates image by loading it from the disk from specified file name.
  192. // Returns handle to the image.
  193. int nvgCreateImage(struct NVGcontext* ctx, const char* filename);
  194. // Creates image by loading it from the specified memory chunk.
  195. // Returns handle to the image.
  196. int nvgCreateImageMem(struct NVGcontext* ctx, unsigned char* data, int ndata, int freeData);
  197. // Creates image from specified image data.
  198. // Returns handle to the image.
  199. int nvgCreateImageRGBA(struct NVGcontext* ctx, int w, int h, const unsigned char* data);
  200. // Updates image data specified by image handle.
  201. void nvgUpdateImage(struct NVGcontext* ctx, int image, const unsigned char* data);
  202. // Returns the domensions of a created image.
  203. void nvgImageSize(struct NVGcontext* ctx, int image, int* w, int* h);
  204. // Deletes created image.
  205. void nvgDeleteImage(struct NVGcontext* ctx, int image);
  206. //
  207. // Paints
  208. //
  209. // NanoVG supports four types of paints: linear gradient, box gradient, radial gradient and image pattern.
  210. // These can be used as paints for strokes and fills.
  211. // Creates and returns a linear gradient. Parameters (sx,sy)-(ex,ey) specify the start and end coordinates
  212. // of the linear gradient, icol specifies the start color and ocol the end color.
  213. // The gradient is transformed by the current transform when it is passed to nvgFillPaint() or nvgStrokePaint().
  214. struct NVGpaint nvgLinearGradient(struct NVGcontext* ctx, float sx, float sy, float ex, float ey,
  215. struct NVGcolor icol, struct NVGcolor ocol);
  216. // Creates and returns a box gradient. Box gradient is a feathered rounded rectangle, it is useful for rendering
  217. // drop shadows or hilights for boxes. Parameters (x,y) define the top-left corner of the rectangle,
  218. // (w,h) define the size of the rectangle, r defines the corner radius, and f feather. Feather defines how blurry
  219. // the border of the rectangle is. Parameter icol specifies the inner color and ocol the outer color of the gradient.
  220. // The gradient is transformed by the current transform when it is passed to nvgFillPaint() or nvgStrokePaint().
  221. struct NVGpaint nvgBoxGradient(struct NVGcontext* ctx, float x, float y, float w, float h,
  222. float r, float f, struct NVGcolor icol, struct NVGcolor ocol);
  223. // Creates and returns a radial gradient. Parameters (cx,cy) specify the center, inr and outr specify
  224. // the inner and outer radius of the gradient, icol specifies the start color and ocol the end color.
  225. // The gradient is transformed by the current transform when it is passed to nvgFillPaint() or nvgStrokePaint().
  226. struct NVGpaint nvgRadialGradient(struct NVGcontext* ctx, float cx, float cy, float inr, float outr,
  227. struct NVGcolor icol, struct NVGcolor ocol);
  228. // Creates and returns an image patter. Parameters (ox,oy) specify the left-top location of the image pattern,
  229. // (ex,ey) the size of one image, angle rotation around the top-left corner, image is handle to the image to render,
  230. // and repeat is combination of NVG_REPEATX and NVG_REPEATY which tells if the image should be repeated across x or y.
  231. // The gradient is transformed by the current transform when it is passed to nvgFillPaint() or nvgStrokePaint().
  232. struct NVGpaint nvgImagePattern(struct NVGcontext* ctx, float ox, float oy, float ex, float ey,
  233. float angle, int image, int repeat);
  234. //
  235. // Scissoring
  236. //
  237. // Scissoring allows you to clip the rendering into a rectangle. This is useful for varius
  238. // user interface cases like rendering a text edit or a timeline.
  239. // Sets the current
  240. // The scissor rectangle is transformed by the current transform.
  241. void nvgScissor(struct NVGcontext* ctx, float x, float y, float w, float h);
  242. // Reset and disables scissoring.
  243. void nvgResetScissor(struct NVGcontext* ctx);
  244. //
  245. // Paths
  246. //
  247. // Drawing a new shape starts with nvgBeginPath(), it clears all the currently defined paths.
  248. // Then you define one or more paths and sub-paths which describe the shape. The are functions
  249. // to draw common shapes like rectangles and circles, and lower level step-by-step functions,
  250. // which allow to define a path curve by curve.
  251. //
  252. // NanoVG uses even-odd fill rule to draw the shapes. Solid shapes should have counter clockwise
  253. // winding and holes should have counter clockwise order. To specify winding of a path you can
  254. // call nvgPathWinding(). This is useful especially for the common shapes, which are drawn CCW.
  255. //
  256. // Finally you can fill the path using current fill style by calling nvgFill(), and stroke it
  257. // with current stroke style by calling nvgStroke().
  258. //
  259. // The curve segments and sub-paths are transformed by the current transform.
  260. // Clears the current path and sub-paths.
  261. void nvgBeginPath(struct NVGcontext* ctx);
  262. // Starts new sub-path with specified point as first point.
  263. void nvgMoveTo(struct NVGcontext* ctx, float x, float y);
  264. // Adds line segment from the last point in the path to the specified point.
  265. void nvgLineTo(struct NVGcontext* ctx, float x, float y);
  266. // Adds bezier segment from last point in the path via two control points to the specified point.
  267. void nvgBezierTo(struct NVGcontext* ctx, float c1x, float c1y, float c2x, float c2y, float x, float y);
  268. // Adds an arc segment at the corner defined by the last path point, and two specified points.
  269. void nvgArcTo(struct NVGcontext* ctx, float x1, float y1, float x2, float y2, float radius);
  270. // Closes current sub-path with a line segment.
  271. void nvgClosePath(struct NVGcontext* ctx);
  272. // Sets the current sub-path winding, see NVGwinding and NVGsolidity.
  273. void nvgPathWinding(struct NVGcontext* ctx, int dir);
  274. // Creates new arc shaped sub-path.
  275. void nvgArc(struct NVGcontext* ctx, float cx, float cy, float r, float a0, float a1, int dir);
  276. // Creates new rectangle shaped sub-path.
  277. void nvgRect(struct NVGcontext* ctx, float x, float y, float w, float h);
  278. // Creates new rounded rectangle shaped sub-path.
  279. void nvgRoundedRect(struct NVGcontext* ctx, float x, float y, float w, float h, float r);
  280. // Creates new ellipse shaped sub-path.
  281. void nvgEllipse(struct NVGcontext* ctx, float cx, float cy, float rx, float ry);
  282. // Creates new circle shaped sub-path.
  283. void nvgCircle(struct NVGcontext* ctx, float cx, float cy, float r);
  284. // Fills the current path with current fill style.
  285. void nvgFill(struct NVGcontext* ctx);
  286. // Fills the current path with current stroke style.
  287. void nvgStroke(struct NVGcontext* ctx);
  288. //
  289. // Text
  290. //
  291. // NanoVG allows you to load .ttf files and use the font to render text.
  292. //
  293. // The appearance of the text can be defined by setting the current text style
  294. // and by specifying the fill color. Common text and font settings such as
  295. // font size, letter spacing and text align are supported. Font blur allows you
  296. // to create simple text effects such as drop shadows.
  297. //
  298. // At render time the tont face can be set based on the font handles or name.
  299. //
  300. // Note: currently only solid color fill is supported for text.
  301. // Creates font by loading it from the disk from specified file name.
  302. // Returns handle to the font.
  303. int nvgCreateFont(struct NVGcontext* ctx, const char* name, const char* filename);
  304. // Creates image by loading it from the specified memory chunk.
  305. // Returns handle to the font.
  306. int nvgCreateFontMem(struct NVGcontext* ctx, const char* name, unsigned char* data, int ndata, int freeData);
  307. // Finds a loaded font of specified name, and returns handle to it, or -1 if the font is not found.
  308. int nvgFindFont(struct NVGcontext* ctx, const char* name);
  309. // Sets the font size of current text style.
  310. void nvgFontSize(struct NVGcontext* ctx, float size);
  311. // Sets the letter spacing of current text style.
  312. void nvgLetterSpacing(struct NVGcontext* ctx, float spacing);
  313. // Sets the blur of current text style.
  314. void nvgFontBlur(struct NVGcontext* ctx, float blur);
  315. // Sets the text align of current text style, see NVGaling for options.
  316. void nvgTextAlign(struct NVGcontext* ctx, int align);
  317. // Sets the font face based on specified id of current text style.
  318. void nvgFontFaceId(struct NVGcontext* ctx, int font);
  319. // Sets the font face based on specified name of current text style.
  320. void nvgFontFace(struct NVGcontext* ctx, const char* font);
  321. // Draws text string at specified location. If end is specified only the sub-string up to the end is drawn.
  322. float nvgText(struct NVGcontext* ctx, float x, float y, const char* string, const char* end);
  323. // Measures the specified text string. Parameter bounds should be a pointer to float[4] if
  324. // the bounding box of the text should be returned. Returns the width of the measured text.
  325. // Current transform does not affect the measured values.
  326. float nvgTextBounds(struct NVGcontext* ctx, const char* string, const char* end, float* bounds);
  327. // Returns the vertical metrics based on the current text style.
  328. // Current transform does not affect the measured values.
  329. void nvgVertMetrics(struct NVGcontext* ctx, float* ascender, float* descender, float* lineh);
  330. //
  331. // Internal Render API
  332. //
  333. enum NVGtexture {
  334. NVG_TEXTURE_ALPHA = 0x01,
  335. NVG_TEXTURE_RGBA = 0x02,
  336. };
  337. struct NVGscissor
  338. {
  339. float xform[6];
  340. float extent[2];
  341. };
  342. struct NVGvertex {
  343. float x,y,u,v;
  344. };
  345. struct NVGpath {
  346. int first;
  347. int count;
  348. unsigned char closed;
  349. int nbevel;
  350. struct NVGvertex* fill;
  351. int nfill;
  352. struct NVGvertex* stroke;
  353. int nstroke;
  354. int winding;
  355. int convex;
  356. };
  357. struct NVGparams {
  358. void* userPtr;
  359. int atlasWidth, atlasHeight;
  360. int edgeAntiAlias;
  361. int (*renderCreate)(void* uptr);
  362. int (*renderCreateTexture)(void* uptr, int type, int w, int h, const unsigned char* data);
  363. int (*renderDeleteTexture)(void* uptr, int image);
  364. int (*renderUpdateTexture)(void* uptr, int image, int x, int y, int w, int h, const unsigned char* data);
  365. int (*renderGetTextureSize)(void* uptr, int image, int* w, int* h);
  366. void (*renderViewport)(void* uptr, int width, int height, int alphaBlend);
  367. void (*renderFlush)(void* uptr, int alphaBlend);
  368. void (*renderFill)(void* uptr, struct NVGpaint* paint, struct NVGscissor* scissor, float fringe, const float* bounds, const struct NVGpath* paths, int npaths);
  369. void (*renderStroke)(void* uptr, struct NVGpaint* paint, struct NVGscissor* scissor, float fringe, float strokeWidth, const struct NVGpath* paths, int npaths);
  370. void (*renderTriangles)(void* uptr, struct NVGpaint* paint, struct NVGscissor* scissor, const struct NVGvertex* verts, int nverts);
  371. void (*renderDelete)(void* uptr);
  372. };
  373. // Contructor and destructor, called by the render back-end.
  374. struct NVGcontext* nvgCreateInternal(struct NVGparams* params);
  375. void nvgDeleteInternal(struct NVGcontext* ctx);
  376. // Compiler references
  377. // http://sourceforge.net/p/predef/wiki/Compilers/
  378. #if _MSC_VER >= 1800
  379. // VS 2013 seems to be too smart for school, it will still list the variable as unused if passed into sizeof().
  380. #define NVG_NOTUSED(v) do { (void)(v); } while(0)
  381. #else
  382. #define NVG_NOTUSED(v) (void)sizeof(v)
  383. #endif
  384. #ifdef __cplusplus
  385. }
  386. #endif
  387. #endif // NANOVG_H