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.

200 lines
7.3KB

  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. #define NVG_PI 3.14159265358979323846264338327f
  21. enum NVGdir {
  22. NVG_CCW = 1,
  23. NVG_CW = 2,
  24. };
  25. enum NVGdir2 {
  26. NVG_SOLID = 1, // ccw
  27. NVG_HOLE = 2, // cw
  28. };
  29. enum NVGpatternRepeat {
  30. NVG_REPEATX = 0x01,
  31. NVG_REPEATY = 0x02,
  32. };
  33. enum NVGaling {
  34. // Horizontal align
  35. NVG_ALIGN_LEFT = 1<<0, // Default
  36. NVG_ALIGN_CENTER = 1<<1,
  37. NVG_ALIGN_RIGHT = 1<<2,
  38. // Vertical align
  39. NVG_ALIGN_TOP = 1<<3,
  40. NVG_ALIGN_MIDDLE = 1<<4,
  41. NVG_ALIGN_BOTTOM = 1<<5,
  42. NVG_ALIGN_BASELINE = 1<<6, // Default
  43. };
  44. // Used by the rendering API
  45. enum NVGtexture {
  46. NVG_TEXTURE_ALPHA = 0x01,
  47. NVG_TEXTURE_RGBA = 0x02,
  48. };
  49. struct NVGpaint
  50. {
  51. float xform[6];
  52. float extent[2];
  53. float radius;
  54. float feather;
  55. unsigned int innerColor;
  56. unsigned int outerColor;
  57. int image;
  58. int repeat;
  59. };
  60. struct NVGscissor
  61. {
  62. float xform[6];
  63. float extent[2];
  64. };
  65. struct NVGvertex {
  66. float x,y,u,v;
  67. };
  68. struct NVGpath {
  69. int first;
  70. int count;
  71. unsigned char closed;
  72. int nbevel;
  73. struct NVGvertex* fill;
  74. int nfill;
  75. struct NVGvertex* stroke;
  76. int nstroke;
  77. int winding;
  78. };
  79. struct NVGparams {
  80. void* userPtr;
  81. int atlasWidth, atlasHeight;
  82. int (*renderCreate)(void* uptr);
  83. int (*renderCreateTexture)(void* uptr, int type, int w, int h, const unsigned char* data);
  84. int (*renderDeleteTexture)(void* uptr, int image);
  85. int (*renderUpdateTexture)(void* uptr, int image, int x, int y, int w, int h, const unsigned char* data);
  86. int (*renderGetTextureSize)(void* uptr, int image, int* w, int* h);
  87. void (*renderFill)(void* uptr, struct NVGpaint* paint, struct NVGscissor* scissor, float aasize, const float* bounds, const struct NVGpath* paths, int npaths);
  88. void (*renderStroke)(void* uptr, struct NVGpaint* paint, struct NVGscissor* scissor, float aasize, float strokeWidth, const struct NVGpath* paths, int npaths);
  89. void (*renderTriangles)(void* uptr, struct NVGpaint* paint, struct NVGscissor* scissor, int image, const struct NVGvertex* verts, int nverts);
  90. void (*renderDelete)(void* uptr);
  91. };
  92. // Contructor and destructor.
  93. struct NVGcontext* nvgCreateInternal(struct NVGparams* params);
  94. void nvgDeleteInternal(struct NVGcontext* ctx);
  95. // Color utils
  96. unsigned int nvgRGB(unsigned char r, unsigned char g, unsigned char b);
  97. unsigned int nvgRGBA(unsigned char r, unsigned char g, unsigned char b, unsigned char a);
  98. unsigned int nvgLerpRGBA(unsigned int c0, unsigned int c1, float u);
  99. unsigned int nvgTransRGBA(unsigned int c0, unsigned char a);
  100. unsigned int nvgHSL(float h, float s, float l);
  101. unsigned int nvgHSLA(float h, float s, float l, unsigned char a);
  102. // State handling
  103. void nvgSave(struct NVGcontext* ctx);
  104. void nvgRestore(struct NVGcontext* ctx);
  105. void nvgReset(struct NVGcontext* ctx);
  106. // State setting
  107. void nvgStrokeColor(struct NVGcontext* ctx, unsigned int color);
  108. void nvgStrokePaint(struct NVGcontext* ctx, struct NVGpaint paint);
  109. void nvgFillColor(struct NVGcontext* ctx, unsigned int color);
  110. void nvgFillPaint(struct NVGcontext* ctx, struct NVGpaint paint);
  111. void nvgMiterLimit(struct NVGcontext* ctx, float limit);
  112. void nvgStrokeWidth(struct NVGcontext* ctx, float size);
  113. void nvgResetTransform(struct NVGcontext* ctx);
  114. void nvgTransform(struct NVGcontext* ctx, float a, float b, float c, float d, float e, float f);
  115. void nvgTranslate(struct NVGcontext* ctx, float x, float y);
  116. void nvgRotate(struct NVGcontext* ctx, float angle);
  117. void nvgScale(struct NVGcontext* ctx, float x, float y);
  118. // Images
  119. int nvgCreateImage(struct NVGcontext* ctx, const char* filename);
  120. int nvgCreateImageMem(struct NVGcontext* ctx, unsigned char* data, int ndata, int freeData);
  121. int nvgCreateImageRGBA(struct NVGcontext* ctx, int w, int h, const unsigned char* data);
  122. void nvgUpdateImage(struct NVGcontext* ctx, int image, const unsigned char* data);
  123. void nvgImageSize(struct NVGcontext* ctx, int image, int* w, int* h);
  124. void nvgDeleteImage(struct NVGcontext* ctx, int image);
  125. // Paints
  126. struct NVGpaint nvgLinearGradient(struct NVGcontext* ctx, float sx, float sy, float ex, float ey, unsigned int icol, unsigned int ocol);
  127. struct NVGpaint nvgBoxGradient(struct NVGcontext* ctx, float x, float y, float w, float h, float r, float f, unsigned int icol, unsigned int ocol);
  128. struct NVGpaint nvgRadialGradient(struct NVGcontext* ctx, float cx, float cy, float inr, float outr, unsigned int icol, unsigned int ocol);
  129. struct NVGpaint nvgImagePattern(struct NVGcontext* ctx, float ox, float oy, float ex, float ey, float angle, int image, int repeat);
  130. // Scissoring
  131. void nvgScissor(struct NVGcontext* ctx, float x, float y, float w, float h);
  132. void nvgResetScissor(struct NVGcontext* ctx);
  133. // Draw
  134. void nvgBeginFrame(struct NVGcontext* ctx);
  135. void nvgBeginPath(struct NVGcontext* ctx);
  136. void nvgMoveTo(struct NVGcontext* ctx, float x, float y);
  137. void nvgLineTo(struct NVGcontext* ctx, float x, float y);
  138. void nvgBezierTo(struct NVGcontext* ctx, float c1x, float c1y, float c2x, float c2y, float x, float y);
  139. void nvgArcTo(struct NVGcontext* ctx, float x1, float y1, float x2, float y2, float radius);
  140. void nvgClosePath(struct NVGcontext* ctx);
  141. void nvgPathWinding(struct NVGcontext* ctx, int dir);
  142. void nvgArc(struct NVGcontext* ctx, float cx, float cy, float r, float a0, float a1, int dir);
  143. void nvgRect(struct NVGcontext* ctx, float x, float y, float w, float h);
  144. void nvgRoundedRect(struct NVGcontext* ctx, float x, float y, float w, float h, float r);
  145. void nvgEllipse(struct NVGcontext* ctx, float cx, float cy, float rx, float ry);
  146. void nvgCircle(struct NVGcontext* ctx, float cx, float cy, float r);
  147. void nvgFill(struct NVGcontext* ctx);
  148. void nvgStroke(struct NVGcontext* ctx);
  149. // Text
  150. // Add fonts
  151. int nvgCreateFont(struct NVGcontext* ctx, const char* name, const char* path);
  152. int nvgCreateFontMem(struct NVGcontext* ctx, const char* name, unsigned char* data, int ndata, int freeData);
  153. int nvgFindFont(struct NVGcontext* ctx, const char* name);
  154. // State setting
  155. void nvgFontSize(struct NVGcontext* ctx, float size);
  156. void nvgLetterSpacing(struct NVGcontext* ctx, float spacing);
  157. void nvgFontBlur(struct NVGcontext* ctx, float blur);
  158. void nvgTextAlign(struct NVGcontext* ctx, int align);
  159. void nvgFontFaceId(struct NVGcontext* ctx, int font);
  160. void nvgFontFace(struct NVGcontext* ctx, const char* font);
  161. // Draw text
  162. float nvgText(struct NVGcontext* ctx, float x, float y, const char* string, const char* end);
  163. // Measure text
  164. float nvgTextBounds(struct NVGcontext* ctx, const char* string, const char* end, float* bounds);
  165. void nvgVertMetrics(struct NVGcontext* ctx, float* ascender, float* descender, float* lineh);
  166. #endif // NANOVG_H