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