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