1 module nanovg.h; 2 3 // 4 // NanoVG-d: 5 // Copyright (c) 2015 S.Percentage 6 // 7 // Original Source(NanoVG): 8 // Copyright (c) 2013 Mikko Mononen memon@inside.org 9 // 10 // This software is provided 'as-is', without any express or implied 11 // warranty. In no event will the authors be held liable for any damages 12 // arising from the use of this software. 13 // Permission is granted to anyone to use this software for any purpose, 14 // including commercial applications, and to alter it and redistribute it 15 // freely, subject to the following restrictions: 16 // 1. The origin of this software must not be misrepresented; you must not 17 // claim that you wrote the original software. If you use this software 18 // in a product, an acknowledgment in the product documentation would be 19 // appreciated but is not required. 20 // 2. Altered source versions must be plainly marked as such, and must not be 21 // misrepresented as being the original software. 22 // 3. This notice may not be removed or altered from any source distribution. 23 // 24 25 // nanovg.h 26 // NanoVG core exports(Canvas/CustomRenderer APIs) 27 28 struct NVGcontext; 29 30 struct NVGcolor 31 { 32 float r, g, b, a; 33 } 34 struct NVGpaint 35 { 36 float[6] xform; 37 float[2] extent; 38 float radius; 39 float feather; 40 NVGcolor innerColor; 41 NVGcolor outerColor; 42 int image; 43 } 44 alias NVGwinding = int; 45 enum : NVGwinding 46 { 47 NVG_CCW = 1, 48 NVG_CW = 2 49 } 50 alias NVGsolidity = int; 51 enum : NVGsolidity 52 { 53 NVG_SOLID = 1, 54 NVG_HOLE = 2 55 } 56 alias NVGlineCap = int; 57 enum : NVGlineCap 58 { 59 NVG_BUTT, 60 NVG_ROUND, 61 NVG_SQUARE, 62 NVG_BEVEL, 63 NVG_MITER 64 } 65 alias NVGalign = int; 66 enum : NVGalign 67 { 68 // Horizontal align 69 NVG_ALIGN_LEFT = 1 << 0, 70 NVG_ALIGN_CENTER = 1 << 1, 71 NVG_ALIGN_RIGHT = 1 << 2, 72 // Vertical align 73 NVG_ALIGN_TOP = 1 << 3, 74 NVG_ALIGN_MIDDLE = 1 << 4, 75 NVG_ALIGN_BOTTOM = 1 << 5, 76 NVG_ALIGN_BASELINE = 1 << 6 77 } 78 79 struct NVGglyphPosition 80 { 81 const(char)* str; 82 float x; 83 float minx, maxx; 84 } 85 struct NVGtextRow 86 { 87 const(char)* start; 88 const(char)* end; 89 const(char)* next; 90 float width; 91 float minx, maxx; 92 } 93 94 alias NVGimageFlags = int; 95 enum : NVGimageFlags 96 { 97 NVG_IMAGE_GENERATE_MIPMAPS = 1 << 0, 98 NVG_IMAGE_REPEATX = 1 << 1, 99 NVG_IMAGE_REPEATY = 1 << 2, 100 NVG_IMAGE_FLIPY = 1 << 3, 101 NVG_IMAGE_PREMULTIPLIED = 1 << 4 102 } 103 104 extern(C) 105 { 106 void nvgBeginFrame(NVGcontext* pContext, int windowWidth, int windowHeight, float devicePixelRatio); 107 void nvgCancelFrame(NVGcontext* pContext); 108 void nvgEndFrame(NVGcontext* pContext); 109 110 NVGcolor nvgRGB(byte r, byte g, byte b); 111 NVGcolor nvgRGBf(float r, float g, float b); 112 NVGcolor nvgRGBA(byte r, byte g, byte b, byte a); 113 NVGcolor nvgRGBAf(float r, float g, float b, float a); 114 NVGcolor nvgLerpRGBA(NVGcolor c0, NVGcolor c1, float u); 115 NVGcolor nvgTransRGBA(NVGcolor c0, byte a); 116 NVGcolor nvgTransRGBAf(NVGcolor c0, float a); 117 NVGcolor nvgHSL(float h, float s, float l); 118 NVGcolor nvgHSLA(float h, float s, float l, byte a); 119 120 void nvgSave(NVGcontext* pContext); 121 void nvgRestore(NVGcontext* pContext); 122 void nvgReset(NVGcontext* pContext); 123 124 void nvgStrokeColor(NVGcontext* pContext, NVGcolor color); 125 void nvgStrokePaint(NVGcontext* pContext, NVGpaint paint); 126 void nvgFillColor(NVGcontext* pContext, NVGcolor color); 127 void nvgFillPaint(NVGcontext* pContext, NVGpaint paint); 128 void nvgMiterLimit(NVGcontext* pContext, float limit); 129 void nvgStrokeWidth(NVGcontext* pContext, float size); 130 void nvgLineCap(NVGcontext* pContext, int cap); 131 void nvgLineJoin(NVGcontext* pContext, int join); 132 void nvgGlobalAlpha(NVGcontext* pContext, float alpha); 133 134 void nvgResetTransform(NVGcontext* pContext); 135 void nvgTransform(NVGcontext* pContext, float a, float b, float c, float d, float e, float f); 136 void nvgTranslate(NVGcontext* pContext, float x, float y); 137 void nvgRotate(NVGcontext* pContext, float angle); 138 void nvgSkewX(NVGcontext* pContext, float angle); 139 void nvgSkewY(NVGcontext* pContext, float angle); 140 void nvgScale(NVGcontext* pContext, float x, float y); 141 void nvgCurrentTransform(NVGcontext* pContext, float* xform); 142 143 void nvgTransformIdentity(float* dst); 144 void nvgTransformTranslate(float* dst, float tx, float ty); 145 void nvgTransformScale(float* dst, float sx, float sy); 146 void nvgTransformRotate(float* dst, float a); 147 void nvgTransformSkewX(float* dst, float a); 148 void nvgTransformSkewY(float* dst, float a); 149 void nvgTransformMultiply(float* dst, const(float)* src); 150 void nvgTransformPremultiply(float* dst, const(float)* src); 151 int nvgTransformInverse(float* dst, const(float)* src); 152 void nvgTransformPoint(float* dstx, float* dsty, const(float)* xform, float srcx, float srcy); 153 float nvgDegToRad(float deg); 154 float nvgRadToDeg(float rad); 155 156 int nvgCreateImage(NVGcontext* pContext, const(char)* filename, int imageFlags); 157 int nvgCreateImageMem(NVGcontext* pContext, int imageFlags, byte* data, int ndata); 158 int nvgCreateImageRGBA(NVGcontext* pContext, int w, int h, int imageFlags, const(byte)* data); 159 int nvgUpdateImage(NVGcontext* pContext, int image, const(byte)* data); 160 int nvgImageSize(NVGcontext* pContext, int image, int* w, int* h); 161 int nvgDeleteImage(NVGcontext* pContext, int image); 162 163 NVGpaint nvgLinearGradient(NVGcontext* pContext, float sx, float sy, float ex, float ey, NVGcolor icol, NVGcolor ocol); 164 NVGpaint nvgBoxGradient(NVGcontext* pContext, float x, float y, float w, float h, float r, float f, NVGcolor icol, NVGcolor ocol); 165 NVGpaint nvgRadialGradient(NVGcontext* pContext, float cx, float cy, float inr, float outr, NVGcolor icol, NVGcolor ocol); 166 NVGpaint nvgImagePattern(NVGcontext* pContext, float ox, float oy, float ex, float ey, float angle, int image, float alpha); 167 168 void nvgScissor(NVGcontext* pContext, float x, float y, float w, float h); 169 void nvgIntersectScissor(NVGcontext* pContext, float x, float y, float w, float h); 170 void nvgResetScissor(NVGcontext* pContext); 171 172 void nvgBeginPath(NVGcontext* pContext); 173 void nvgMoveTo(NVGcontext* pContext, float x, float y); 174 void nvgLineTo(NVGcontext* pContext, float x, float y); 175 void nvgBezierTo(NVGcontext* pContext, float c1x, float c1y, float c2x, float c2y, float x, float y); 176 void nvgQuadTo(NVGcontext* pContext, float cx, float cy, float x, float y); 177 void nvgArcTo(NVGcontext* pContext, float x1, float y1, float x2, float y2, float radius); 178 void nvgClosePath(NVGcontext* pContext); 179 void nvgPathWinding(NVGcontext* pContext, NVGwinding dir); 180 void nvgArc(NVGcontext* pContext, float cx, float cy, float r, float a0, float a1, NVGwinding dir); 181 void nvgRect(NVGcontext* pContext, float x, float y, float w, float h); 182 void nvgRoundedRect(NVGcontext* pContext, float x, float y, float w, float h, float r); 183 void nvgEllipse(NVGcontext* pContext, float cx, float cy, float rx, float ry); 184 void nvgCircle(NVGcontext* pContext, float cx, float cy, float r); 185 void nvgFill(NVGcontext* pContext); 186 void nvgStroke(NVGcontext* pContext); 187 188 int nvgCreateFont(NVGcontext* pContext, const(char)* name, const(char)* filename); 189 int nvgCreateFontMem(NVGcontext* pContext, const(char)* name, byte* data, int ndata, int freeData); 190 int nvgFindFont(NVGcontext* pContext, const(char)* name); 191 int nvgFontSize(NVGcontext* pContext, float size); 192 int nvgFontBlur(NVGcontext* pContext, float blur); 193 int nvgTextLetterSpacing(NVGcontext* pContext, float spacing); 194 int nvgTextLineHeight(NVGcontext* pContext, float lineHeight); 195 int nvgTextAlign(NVGcontext* pContext, NVGalign _align); 196 int nvgFontFaceId(NVGcontext* pContext, int font); 197 int nvgFontFace(NVGcontext* pContext, const(char)* font); 198 float nvgText(NVGcontext* pContext, float x, float y, const(char)* _string, const(char)* end); 199 void nvgTextBox(NVGcontext* pContext, float x, float y, float breakRowWidth, const(char)* _string, const(char)* end); 200 float nvgTextBounds(NVGcontext* pContext, float x, float y, const(char)* _string, const(char)* end, float* bounds); 201 void nvgTextBoxBounds(NVGcontext* pContext, float x, float y, float breakRowWidth, const(char)* _string, const(char)* end, float* bounds); 202 int nvgTextGlyphPositions(NVGcontext* pContext, float x, float y, const(char)* _string, const(char)* end, NVGglyphPosition* positions, int maxPositions); 203 void nvgTextMetrics(NVGcontext* pContext, float* ascender, float* descender, float* lineh); 204 int nvgTextBreakLines(NVGcontext* pContext, const(char)* _string, const(char)* end, float breakRowWidth, NVGtextRow* rows, int maxRows); 205 } 206 207 // InternalRenderAPIs 208 alias NVGtexture = uint; 209 enum : NVGtexture 210 { 211 NVG_TEXTURE_ALPHA = 0x01, 212 NVG_TEXTURE_RGBA = 0x02 213 } 214 215 struct NVGscissor 216 { 217 float[6] xform; 218 float[2] extent; 219 } 220 struct NVGvertex 221 { 222 float x, y, u, v; 223 } 224 struct NVGpath 225 { 226 int first; 227 int count; 228 byte closed; 229 int nbevel; 230 NVGvertex* fill; 231 int nfill; 232 NVGvertex* stroke; 233 int nstroke; 234 int winding; 235 int convex; 236 } 237 struct NVGparams 238 { 239 void* userPtr; 240 int edgeAntiAlias; 241 extern(C) int function(void* uptr) renderCreate; 242 extern(C) int function(void* uptr, int type, int w, int h, int imageFlags, const(byte)* data) renderCreateTexture; 243 extern(C) int function(void* uptr, int image) renderDeleteTexture; 244 extern(C) int function(void* uptr, int image, int x, int y, int w, int h, const(byte)* data) renderUpdateTexture; 245 extern(C) int function(void* uptr, int image, int* w, int* h) renderGetTextureSize; 246 extern(C) void function(void* uptr, int width, int height) renderViewport; 247 extern(C) void function(void* uptr) renderCancel; 248 extern(C) void function(void* uptr) renderFlush; 249 extern(C) void function(void* uptr, NVGpaint* paint, NVGscissor* scissor, float fringe, const(float)* bounds, const(NVGpath)* paths, int npaths) renderFill; 250 extern(C) void function(void* uptr, NVGpaint* paint, NVGscissor* scissor, float fringe, float strokeWidth, const(NVGpath)* paths, int npaths) renderStroke; 251 extern(C) void function(void* uptr, NVGpaint* paint, NVGscissor* scissor, const(NVGvertex)* verts, int nverts) renderTriangles; 252 extern(C) void function(void* uptr) renderDelete; 253 } 254 255 // InternalContextConstructor/Destructor 256 extern(C) 257 { 258 NVGcontext* nvgCreateInternal(NVGparams* params); 259 void nvgDeleteInternal(NVGcontext* pContext); 260 NVGparams* nvgInternalParams(NVGcontext* pContext); 261 262 // Debug function 263 void nvgDebugDumpPathCache(NVGcontext* pContext); 264 }