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.

195 lines
7.0KB

  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 (*renderCreate)(void* uptr);
  82. int (*renderCreateTexture)(void* uptr, int type, int w, int h, const unsigned char* data);
  83. int (*renderDeleteTexture)(void* uptr, int image);
  84. int (*renderUpdateTexture)(void* uptr, int image, int x, int y, int w, int h, const unsigned char* data);
  85. int (*renderGetTextureSize)(void* uptr, int image, int* w, int* h);
  86. void (*renderFill)(void* uptr, struct NVGpaint* paint, struct NVGscissor* scissor, float aasize, const float* bounds, const struct NVGpath* paths, int npaths);
  87. void (*renderStroke)(void* uptr, struct NVGpaint* paint, struct NVGscissor* scissor, float aasize, float strokeWidth, const struct NVGpath* paths, int npaths);
  88. void (*renderTriangles)(void* uptr, struct NVGpaint* paint, struct NVGscissor* scissor, int image, const struct NVGvertex* verts, int nverts);
  89. void (*renderDelete)(void* uptr);
  90. };
  91. // Contructor and destructor.
  92. struct NVGcontext* nvgCreateInternal(struct NVGparams* params);
  93. void nvgDeleteInternal(struct NVGcontext* ctx);
  94. // Color utils
  95. unsigned int nvgRGB(unsigned char r, unsigned char g, unsigned char b);
  96. unsigned int nvgRGBA(unsigned char r, unsigned char g, unsigned char b, unsigned char a);
  97. unsigned int nvgLerpRGBA(unsigned int c0, unsigned int c1, float u);
  98. // State handling
  99. void nvgSave(struct NVGcontext* ctx);
  100. void nvgRestore(struct NVGcontext* ctx);
  101. void nvgReset(struct NVGcontext* ctx);
  102. // State setting
  103. void nvgStrokeColor(struct NVGcontext* ctx, unsigned int color);
  104. void nvgStrokePaint(struct NVGcontext* ctx, struct NVGpaint paint);
  105. void nvgFillColor(struct NVGcontext* ctx, unsigned int color);
  106. void nvgFillPaint(struct NVGcontext* ctx, struct NVGpaint paint);
  107. void nvgMiterLimit(struct NVGcontext* ctx, float limit);
  108. void nvgStrokeWidth(struct NVGcontext* ctx, float size);
  109. void nvgResetTransform(struct NVGcontext* ctx);
  110. void nvgTransform(struct NVGcontext* ctx, float a, float b, float c, float d, float e, float f);
  111. void nvgTranslate(struct NVGcontext* ctx, float x, float y);
  112. void nvgRotate(struct NVGcontext* ctx, float angle);
  113. void nvgScale(struct NVGcontext* ctx, float x, float y);
  114. // Images
  115. int nvgCreateImage(struct NVGcontext* ctx, const char* filename);
  116. int nvgCreateImageMem(struct NVGcontext* ctx, unsigned char* data, int ndata, int freeData);
  117. int nvgCreateImageRGBA(struct NVGcontext* ctx, int w, int h, const unsigned char* data);
  118. void nvgUpdateImage(struct NVGcontext* ctx, int image, const unsigned char* data);
  119. void nvgImageSize(struct NVGcontext* ctx, int image, int* w, int* h);
  120. void nvgDeleteImage(struct NVGcontext* ctx, int image);
  121. // Paints
  122. struct NVGpaint nvgLinearGradient(struct NVGcontext* ctx, float sx, float sy, float ex, float ey, unsigned int icol, unsigned int ocol);
  123. struct NVGpaint nvgBoxGradient(struct NVGcontext* ctx, float x, float y, float w, float h, float r, float f, unsigned int icol, unsigned int ocol);
  124. struct NVGpaint nvgRadialGradient(struct NVGcontext* ctx, float cx, float cy, float inr, float outr, unsigned int icol, unsigned int ocol);
  125. struct NVGpaint nvgImagePattern(struct NVGcontext* ctx, float ox, float oy, float ex, float ey, float angle, int image, int repeat);
  126. // Scissoring
  127. void nvgScissor(struct NVGcontext* ctx, float x, float y, float w, float h);
  128. void nvgResetScissor(struct NVGcontext* ctx);
  129. // Draw
  130. void nvgBeginFrame(struct NVGcontext* ctx);
  131. void nvgBeginPath(struct NVGcontext* ctx);
  132. void nvgMoveTo(struct NVGcontext* ctx, float x, float y);
  133. void nvgLineTo(struct NVGcontext* ctx, float x, float y);
  134. void nvgBezierTo(struct NVGcontext* ctx, float c1x, float c1y, float c2x, float c2y, float x, float y);
  135. void nvgArcTo(struct NVGcontext* ctx, float x1, float y1, float x2, float y2, float radius);
  136. void nvgClosePath(struct NVGcontext* ctx);
  137. void nvgPathWinding(struct NVGcontext* ctx, int dir);
  138. void nvgArc(struct NVGcontext* ctx, float cx, float cy, float r, float a0, float a1, int dir);
  139. void nvgRect(struct NVGcontext* ctx, float x, float y, float w, float h);
  140. void nvgRoundedRect(struct NVGcontext* ctx, float x, float y, float w, float h, float r);
  141. void nvgEllipse(struct NVGcontext* ctx, float cx, float cy, float rx, float ry);
  142. void nvgCircle(struct NVGcontext* ctx, float cx, float cy, float r);
  143. void nvgFill(struct NVGcontext* ctx);
  144. void nvgStroke(struct NVGcontext* ctx);
  145. // Text
  146. // Add fonts
  147. int nvgCreateFont(struct NVGcontext* ctx, const char* name, const char* path);
  148. int nvgCreateFontMem(struct NVGcontext* ctx, const char* name, unsigned char* data, int ndata, int freeData);
  149. // State setting
  150. void nvgFontSize(struct NVGcontext* ctx, float size);
  151. void nvgLetterSpacing(struct NVGcontext* ctx, float spacing);
  152. void nvgFontBlur(struct NVGcontext* ctx, float blur);
  153. void nvgTextAlign(struct NVGcontext* ctx, int align);
  154. void nvgFontFaceId(struct NVGcontext* ctx, int font);
  155. void nvgFontFace(struct NVGcontext* ctx, const char* font);
  156. // Draw text
  157. void nvgText(struct NVGcontext* ctx, float x, float y, const char* string);
  158. // Measure text
  159. void nvgTextBounds(struct NVGcontext* ctx, const char* string, float* width, float* bounds);
  160. void nvgVertMetrics(struct NVGcontext* ctx, float* ascender, float* descender, float* lineh);
  161. #endif // NANOVG_H