bgfx/examples/common/nanovg/nanovg.h

627 lines
25 KiB
C
Raw Normal View History

2014-04-27 10:50:11 +04:00
//
// Copyright (c) 2013 Mikko Mononen memon@inside.org
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
// 3. This notice may not be removed or altered from any source distribution.
//
#ifndef NANOVG_H
#define NANOVG_H
#ifdef __cplusplus
extern "C" {
#endif
#define NVG_PI 3.14159265358979323846264338327f
2014-11-17 05:59:17 +03:00
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4201) // nonstandard extension used : nameless struct/union
#endif
typedef struct NVGcontext NVGcontext;
2014-04-27 10:50:11 +04:00
struct NVGcolor {
union {
float rgba[4];
struct {
float r,g,b,a;
};
};
};
2014-11-17 05:59:17 +03:00
typedef struct NVGcolor NVGcolor;
2014-04-27 10:50:11 +04:00
struct NVGpaint {
float xform[6];
float extent[2];
float radius;
float feather;
2014-11-17 05:59:17 +03:00
NVGcolor innerColor;
NVGcolor outerColor;
2014-04-27 10:50:11 +04:00
int image;
};
2014-11-17 05:59:17 +03:00
typedef struct NVGpaint NVGpaint;
2014-04-27 10:50:11 +04:00
enum NVGwinding {
NVG_CCW = 1, // Winding for solid shapes
NVG_CW = 2, // Winding for holes
};
enum NVGsolidity {
NVG_SOLID = 1, // CCW
NVG_HOLE = 2, // CW
};
enum NVGlineCap {
NVG_BUTT,
NVG_ROUND,
NVG_SQUARE,
NVG_BEVEL,
NVG_MITER,
};
enum NVGalign {
// Horizontal align
NVG_ALIGN_LEFT = 1<<0, // Default, align text horizontally to left.
NVG_ALIGN_CENTER = 1<<1, // Align text horizontally to center.
NVG_ALIGN_RIGHT = 1<<2, // Align text horizontally to right.
// Vertical align
NVG_ALIGN_TOP = 1<<3, // Align text vertically to top.
NVG_ALIGN_MIDDLE = 1<<4, // Align text vertically to middle.
NVG_ALIGN_BOTTOM = 1<<5, // Align text vertically to bottom.
NVG_ALIGN_BASELINE = 1<<6, // Default, align text vertically to baseline.
};
struct NVGglyphPosition {
const char* str; // Position of the glyph in the input string.
2014-11-17 05:59:17 +03:00
float x; // The x-coordinate of the logical glyph position.
float minx, maxx; // The bounds of the glyph shape.
2014-04-27 10:50:11 +04:00
};
2014-11-17 05:59:17 +03:00
typedef struct NVGglyphPosition NVGglyphPosition;
2014-04-27 10:50:11 +04:00
struct NVGtextRow {
const char* start; // Pointer to the input text where the row starts.
const char* end; // Pointer to the input text where the row ends (one past the last character).
const char* next; // Pointer to the beginning of the next row.
float width; // Logical width of the row.
float minx, maxx; // Actual bounds of the row. Logical with and bounds can differ because of kerning and some parts over extending.
};
2014-11-17 05:59:17 +03:00
typedef struct NVGtextRow NVGtextRow;
enum NVGimageFlags {
NVG_IMAGE_GENERATE_MIPMAPS = 1<<0, // Generate mipmaps during creation of the image.
NVG_IMAGE_REPEATX = 1<<1, // Repeat image in X direction.
NVG_IMAGE_REPEATY = 1<<2, // Repeat image in Y direction.
NVG_IMAGE_FLIPY = 1<<3, // Flips (inverses) image in Y direction when rendered.
NVG_IMAGE_PREMULTIPLIED = 1<<4, // Image data has premultiplied alpha.
};
2014-04-27 10:50:11 +04:00
// Begin drawing a new frame
// Calls to nanovg drawing API should be wrapped in nvgBeginFrame() & nvgEndFrame()
// nvgBeginFrame() defines the size of the window to render to in relation currently
// set viewport (i.e. glViewport on GL backends). Device pixel ration allows to
// control the rendering on Hi-DPI devices.
// For example, GLFW returns two dimension for an opened window: window size and
// frame buffer size. In that case you would set windowWidth/Height to the window size
// devicePixelRatio to: frameBufferWidth / windowWidth.
2014-11-17 05:59:17 +03:00
void nvgBeginFrame(NVGcontext* ctx, int windowWidth, int windowHeight, float devicePixelRatio);
// Cancels drawing the current frame.
void nvgCancelFrame(NVGcontext* ctx);
2014-04-27 10:50:11 +04:00
// Ends drawing flushing remaining render state.
2014-11-17 05:59:17 +03:00
void nvgEndFrame(NVGcontext* ctx);
2014-04-27 10:50:11 +04:00
//
// Color utils
//
// Colors in NanoVG are stored as unsigned ints in ABGR format.
// Returns a color value from red, green, blue values. Alpha will be set to 255 (1.0f).
2014-11-17 05:59:17 +03:00
NVGcolor nvgRGB(unsigned char r, unsigned char g, unsigned char b);
2014-04-27 10:50:11 +04:00
// Returns a color value from red, green, blue values. Alpha will be set to 1.0f.
2014-11-17 05:59:17 +03:00
NVGcolor nvgRGBf(float r, float g, float b);
2014-04-27 10:50:11 +04:00
// Returns a color value from red, green, blue and alpha values.
2014-11-17 05:59:17 +03:00
NVGcolor nvgRGBA(unsigned char r, unsigned char g, unsigned char b, unsigned char a);
2014-06-30 08:53:23 +04:00
2014-04-27 10:50:11 +04:00
// Returns a color value from red, green, blue and alpha values.
2014-11-17 05:59:17 +03:00
NVGcolor nvgRGBAf(float r, float g, float b, float a);
2014-04-27 10:50:11 +04:00
2015-12-05 22:00:11 +03:00
// Linearly interpolates from color c0 to c1, and returns resulting color value.
2014-11-17 05:59:17 +03:00
NVGcolor nvgLerpRGBA(NVGcolor c0, NVGcolor c1, float u);
2014-04-27 10:50:11 +04:00
// Sets transparency of a color value.
2014-11-17 05:59:17 +03:00
NVGcolor nvgTransRGBA(NVGcolor c0, unsigned char a);
2014-04-27 10:50:11 +04:00
// Sets transparency of a color value.
2014-11-17 05:59:17 +03:00
NVGcolor nvgTransRGBAf(NVGcolor c0, float a);
2014-04-27 10:50:11 +04:00
// Returns color value specified by hue, saturation and lightness.
// HSL values are all in range [0..1], alpha will be set to 255.
2014-11-17 05:59:17 +03:00
NVGcolor nvgHSL(float h, float s, float l);
2014-04-27 10:50:11 +04:00
// Returns color value specified by hue, saturation and lightness and alpha.
// HSL values are all in range [0..1], alpha in range [0..255]
2014-11-17 05:59:17 +03:00
NVGcolor nvgHSLA(float h, float s, float l, unsigned char a);
2014-04-27 10:50:11 +04:00
//
// State Handling
//
// NanoVG contains state which represents how paths will be rendered.
// The state contains transform, fill and stroke styles, text and font styles,
// and scissor clipping.
// Pushes and saves the current render state into a state stack.
// A matching nvgRestore() must be used to restore the state.
2014-11-17 05:59:17 +03:00
void nvgSave(NVGcontext* ctx);
2014-04-27 10:50:11 +04:00
// Pops and restores current render state.
2014-11-17 05:59:17 +03:00
void nvgRestore(NVGcontext* ctx);
2014-04-27 10:50:11 +04:00
// Resets current render state to default values. Does not affect the render state stack.
2014-11-17 05:59:17 +03:00
void nvgReset(NVGcontext* ctx);
2014-04-27 10:50:11 +04:00
//
// Render styles
//
// Fill and stroke render style can be either a solid color or a paint which is a gradient or a pattern.
// Solid color is simply defined as a color value, different kinds of paints can be created
// using nvgLinearGradient(), nvgBoxGradient(), nvgRadialGradient() and nvgImagePattern().
//
// Current render style can be saved and restored using nvgSave() and nvgRestore().
// Sets current stroke style to a solid color.
2014-11-17 05:59:17 +03:00
void nvgStrokeColor(NVGcontext* ctx, NVGcolor color);
2014-04-27 10:50:11 +04:00
// Sets current stroke style to a paint, which can be a one of the gradients or a pattern.
2014-11-17 05:59:17 +03:00
void nvgStrokePaint(NVGcontext* ctx, NVGpaint paint);
2014-04-27 10:50:11 +04:00
2015-12-05 22:00:11 +03:00
// Sets current fill style to a solid color.
2014-11-17 05:59:17 +03:00
void nvgFillColor(NVGcontext* ctx, NVGcolor color);
2014-04-27 10:50:11 +04:00
// Sets current fill style to a paint, which can be a one of the gradients or a pattern.
2014-11-17 05:59:17 +03:00
void nvgFillPaint(NVGcontext* ctx, NVGpaint paint);
2014-04-27 10:50:11 +04:00
// Sets the miter limit of the stroke style.
// Miter limit controls when a sharp corner is beveled.
2014-11-17 05:59:17 +03:00
void nvgMiterLimit(NVGcontext* ctx, float limit);
2014-04-27 10:50:11 +04:00
2015-12-05 22:00:11 +03:00
// Sets the stroke width of the stroke style.
2014-11-17 05:59:17 +03:00
void nvgStrokeWidth(NVGcontext* ctx, float size);
2014-04-27 10:50:11 +04:00
// Sets how the end of the line (cap) is drawn,
// Can be one of: NVG_BUTT (default), NVG_ROUND, NVG_SQUARE.
2014-11-17 05:59:17 +03:00
void nvgLineCap(NVGcontext* ctx, int cap);
2014-04-27 10:50:11 +04:00
// Sets how sharp path corners are drawn.
// Can be one of NVG_MITER (default), NVG_ROUND, NVG_BEVEL.
2014-11-17 05:59:17 +03:00
void nvgLineJoin(NVGcontext* ctx, int join);
// Sets the transparency applied to all rendered shapes.
// Alreade transparent paths will get proportionally more transparent as well.
void nvgGlobalAlpha(NVGcontext* ctx, float alpha);
2014-04-27 10:50:11 +04:00
//
// Transforms
//
// The paths, gradients, patterns and scissor region are transformed by an transformation
// matrix at the time when they are passed to the API.
// The current transformation matrix is a affine matrix:
// [sx kx tx]
// [ky sy ty]
// [ 0 0 1]
// Where: sx,sy define scaling, kx,ky skewing, and tx,ty translation.
// The last row is assumed to be 0,0,1 and is not stored.
//
// Apart from nvgResetTransform(), each transformation function first creates
// specific transformation matrix and pre-multiplies the current transformation by it.
//
// Current coordinate system (transformation) can be saved and restored using nvgSave() and nvgRestore().
// Resets current transform to a identity matrix.
2014-11-17 05:59:17 +03:00
void nvgResetTransform(NVGcontext* ctx);
2014-04-27 10:50:11 +04:00
// Premultiplies current coordinate system by specified matrix.
// The parameters are interpreted as matrix as follows:
// [a c e]
// [b d f]
// [0 0 1]
2014-11-17 05:59:17 +03:00
void nvgTransform(NVGcontext* ctx, float a, float b, float c, float d, float e, float f);
2014-04-27 10:50:11 +04:00
// Translates current coordinate system.
2014-11-17 05:59:17 +03:00
void nvgTranslate(NVGcontext* ctx, float x, float y);
2015-12-05 22:00:11 +03:00
// Rotates current coordinate system. Angle is specified in radians.
2014-11-17 05:59:17 +03:00
void nvgRotate(NVGcontext* ctx, float angle);
2015-12-05 22:00:11 +03:00
// Skews the current coordinate system along X axis. Angle is specified in radians.
2014-11-17 05:59:17 +03:00
void nvgSkewX(NVGcontext* ctx, float angle);
2014-04-27 10:50:11 +04:00
2015-12-05 22:00:11 +03:00
// Skews the current coordinate system along Y axis. Angle is specified in radians.
2014-11-17 05:59:17 +03:00
void nvgSkewY(NVGcontext* ctx, float angle);
2014-04-27 10:50:11 +04:00
2015-12-05 22:00:11 +03:00
// Scales the current coordinate system.
2014-11-17 05:59:17 +03:00
void nvgScale(NVGcontext* ctx, float x, float y);
// Stores the top part (a-f) of the current transformation matrix in to the specified buffer.
// [a c e]
// [b d f]
// [0 0 1]
// There should be space for 6 floats in the return buffer for the values a-f.
void nvgCurrentTransform(NVGcontext* ctx, float* xform);
// The following functions can be used to make calculations on 2x3 transformation matrices.
2015-12-05 22:00:11 +03:00
// A 2x3 matrix is represented as float[6].
2014-11-17 05:59:17 +03:00
// Sets the transform to identity matrix.
void nvgTransformIdentity(float* dst);
// Sets the transform to translation matrix matrix.
void nvgTransformTranslate(float* dst, float tx, float ty);
// Sets the transform to scale matrix.
void nvgTransformScale(float* dst, float sx, float sy);
2015-12-05 22:00:11 +03:00
// Sets the transform to rotate matrix. Angle is specified in radians.
2014-11-17 05:59:17 +03:00
void nvgTransformRotate(float* dst, float a);
2015-12-05 22:00:11 +03:00
// Sets the transform to skew-x matrix. Angle is specified in radians.
2014-11-17 05:59:17 +03:00
void nvgTransformSkewX(float* dst, float a);
2015-12-05 22:00:11 +03:00
// Sets the transform to skew-y matrix. Angle is specified in radians.
2014-11-17 05:59:17 +03:00
void nvgTransformSkewY(float* dst, float a);
// Sets the transform to the result of multiplication of two transforms, of A = A*B.
void nvgTransformMultiply(float* dst, const float* src);
// Sets the transform to the result of multiplication of two transforms, of A = B*A.
void nvgTransformPremultiply(float* dst, const float* src);
// Sets the destination to inverse of specified transform.
// Returns 1 if the inverse could be calculated, else 0.
int nvgTransformInverse(float* dst, const float* src);
// Transform a point by given transform.
void nvgTransformPoint(float* dstx, float* dsty, const float* xform, float srcx, float srcy);
2015-12-05 22:00:11 +03:00
// Converts degrees to radians and vice versa.
2014-11-17 05:59:17 +03:00
float nvgDegToRad(float deg);
float nvgRadToDeg(float rad);
2014-04-27 10:50:11 +04:00
//
// Images
//
// NanoVG allows you to load jpg, png, psd, tga, pic and gif files to be used for rendering.
// In addition you can upload your own image. The image loading is provided by stb_image.
2014-11-17 05:59:17 +03:00
// The parameter imageFlags is combination of flags defined in NVGimageFlags.
2014-04-27 10:50:11 +04:00
// Creates image by loading it from the disk from specified file name.
// Returns handle to the image.
2014-11-17 05:59:17 +03:00
int nvgCreateImage(NVGcontext* ctx, const char* filename, int imageFlags);
2014-04-27 10:50:11 +04:00
// Creates image by loading it from the specified chunk of memory.
// Returns handle to the image.
2014-11-17 05:59:17 +03:00
int nvgCreateImageMem(NVGcontext* ctx, int imageFlags, unsigned char* data, int ndata);
2014-04-27 10:50:11 +04:00
// Creates image from specified image data.
// Returns handle to the image.
2014-11-17 05:59:17 +03:00
int nvgCreateImageRGBA(NVGcontext* ctx, int w, int h, int imageFlags, const unsigned char* data);
2014-04-27 10:50:11 +04:00
// Updates image data specified by image handle.
2014-11-17 05:59:17 +03:00
void nvgUpdateImage(NVGcontext* ctx, int image, const unsigned char* data);
2014-04-27 10:50:11 +04:00
2015-12-05 22:00:11 +03:00
// Returns the dimensions of a created image.
2014-11-17 05:59:17 +03:00
void nvgImageSize(NVGcontext* ctx, int image, int* w, int* h);
2014-04-27 10:50:11 +04:00
// Deletes created image.
2014-11-17 05:59:17 +03:00
void nvgDeleteImage(NVGcontext* ctx, int image);
2014-04-27 10:50:11 +04:00
//
// Paints
//
// NanoVG supports four types of paints: linear gradient, box gradient, radial gradient and image pattern.
// These can be used as paints for strokes and fills.
// Creates and returns a linear gradient. Parameters (sx,sy)-(ex,ey) specify the start and end coordinates
// of the linear gradient, icol specifies the start color and ocol the end color.
// The gradient is transformed by the current transform when it is passed to nvgFillPaint() or nvgStrokePaint().
2014-11-17 05:59:17 +03:00
NVGpaint nvgLinearGradient(NVGcontext* ctx, float sx, float sy, float ex, float ey,
NVGcolor icol, NVGcolor ocol);
2014-04-27 10:50:11 +04:00
// Creates and returns a box gradient. Box gradient is a feathered rounded rectangle, it is useful for rendering
// drop shadows or hilights for boxes. Parameters (x,y) define the top-left corner of the rectangle,
// (w,h) define the size of the rectangle, r defines the corner radius, and f feather. Feather defines how blurry
// the border of the rectangle is. Parameter icol specifies the inner color and ocol the outer color of the gradient.
// The gradient is transformed by the current transform when it is passed to nvgFillPaint() or nvgStrokePaint().
2014-11-17 05:59:17 +03:00
NVGpaint nvgBoxGradient(NVGcontext* ctx, float x, float y, float w, float h,
float r, float f, NVGcolor icol, NVGcolor ocol);
2014-04-27 10:50:11 +04:00
// Creates and returns a radial gradient. Parameters (cx,cy) specify the center, inr and outr specify
// the inner and outer radius of the gradient, icol specifies the start color and ocol the end color.
// The gradient is transformed by the current transform when it is passed to nvgFillPaint() or nvgStrokePaint().
2014-11-17 05:59:17 +03:00
NVGpaint nvgRadialGradient(NVGcontext* ctx, float cx, float cy, float inr, float outr,
NVGcolor icol, NVGcolor ocol);
2014-04-27 10:50:11 +04:00
// Creates and returns an image patter. Parameters (ox,oy) specify the left-top location of the image pattern,
2014-11-17 05:59:17 +03:00
// (ex,ey) the size of one image, angle rotation around the top-left corner, image is handle to the image to render.
2014-04-27 10:50:11 +04:00
// The gradient is transformed by the current transform when it is passed to nvgFillPaint() or nvgStrokePaint().
2014-11-17 05:59:17 +03:00
NVGpaint nvgImagePattern(NVGcontext* ctx, float ox, float oy, float ex, float ey,
float angle, int image, float alpha);
2014-04-27 10:50:11 +04:00
//
// Scissoring
//
// Scissoring allows you to clip the rendering into a rectangle. This is useful for varius
// user interface cases like rendering a text edit or a timeline.
2014-11-17 05:59:17 +03:00
// Sets the current scissor rectangle.
2014-04-27 10:50:11 +04:00
// The scissor rectangle is transformed by the current transform.
2014-11-17 05:59:17 +03:00
void nvgScissor(NVGcontext* ctx, float x, float y, float w, float h);
// Intersects current scissor rectangle with the specified rectangle.
// The scissor rectangle is transformed by the current transform.
// Note: in case the rotation of previous scissor rect differs from
// the current one, the intersection will be done between the specified
// rectangle and the previous scissor rectangle transformed in the current
// transform space. The resulting shape is always rectangle.
void nvgIntersectScissor(NVGcontext* ctx, float x, float y, float w, float h);
2014-04-27 10:50:11 +04:00
// Reset and disables scissoring.
2014-11-17 05:59:17 +03:00
void nvgResetScissor(NVGcontext* ctx);
2014-04-27 10:50:11 +04:00
//
// Paths
//
// Drawing a new shape starts with nvgBeginPath(), it clears all the currently defined paths.
// Then you define one or more paths and sub-paths which describe the shape. The are functions
// to draw common shapes like rectangles and circles, and lower level step-by-step functions,
// which allow to define a path curve by curve.
//
// NanoVG uses even-odd fill rule to draw the shapes. Solid shapes should have counter clockwise
// winding and holes should have counter clockwise order. To specify winding of a path you can
// call nvgPathWinding(). This is useful especially for the common shapes, which are drawn CCW.
//
// Finally you can fill the path using current fill style by calling nvgFill(), and stroke it
// with current stroke style by calling nvgStroke().
//
// The curve segments and sub-paths are transformed by the current transform.
// Clears the current path and sub-paths.
2014-11-17 05:59:17 +03:00
void nvgBeginPath(NVGcontext* ctx);
2014-04-27 10:50:11 +04:00
// Starts new sub-path with specified point as first point.
2014-11-17 05:59:17 +03:00
void nvgMoveTo(NVGcontext* ctx, float x, float y);
2014-04-27 10:50:11 +04:00
// Adds line segment from the last point in the path to the specified point.
2014-11-17 05:59:17 +03:00
void nvgLineTo(NVGcontext* ctx, float x, float y);
// Adds cubic bezier segment from last point in the path via two control points to the specified point.
void nvgBezierTo(NVGcontext* ctx, float c1x, float c1y, float c2x, float c2y, float x, float y);
2014-04-27 10:50:11 +04:00
2014-11-17 05:59:17 +03:00
// Adds quadratic bezier segment from last point in the path via a control point to the specified point.
void nvgQuadTo(NVGcontext* ctx, float cx, float cy, float x, float y);
2014-04-27 10:50:11 +04:00
// Adds an arc segment at the corner defined by the last path point, and two specified points.
2014-11-17 05:59:17 +03:00
void nvgArcTo(NVGcontext* ctx, float x1, float y1, float x2, float y2, float radius);
2014-04-27 10:50:11 +04:00
// Closes current sub-path with a line segment.
2014-11-17 05:59:17 +03:00
void nvgClosePath(NVGcontext* ctx);
2014-04-27 10:50:11 +04:00
// Sets the current sub-path winding, see NVGwinding and NVGsolidity.
2014-11-17 05:59:17 +03:00
void nvgPathWinding(NVGcontext* ctx, int dir);
2014-04-27 10:50:11 +04:00
2014-11-17 05:59:17 +03:00
// Creates new circle arc shaped sub-path. The arc center is at cx,cy, the arc radius is r,
// and the arc is drawn from angle a0 to a1, and swept in direction dir (NVG_CCW, or NVG_CW).
// Angles are specified in radians.
void nvgArc(NVGcontext* ctx, float cx, float cy, float r, float a0, float a1, int dir);
2014-04-27 10:50:11 +04:00
// Creates new rectangle shaped sub-path.
2014-11-17 05:59:17 +03:00
void nvgRect(NVGcontext* ctx, float x, float y, float w, float h);
2014-04-27 10:50:11 +04:00
// Creates new rounded rectangle shaped sub-path.
2014-11-17 05:59:17 +03:00
void nvgRoundedRect(NVGcontext* ctx, float x, float y, float w, float h, float r);
2014-04-27 10:50:11 +04:00
// Creates new ellipse shaped sub-path.
2014-11-17 05:59:17 +03:00
void nvgEllipse(NVGcontext* ctx, float cx, float cy, float rx, float ry);
2014-04-27 10:50:11 +04:00
// Creates new circle shaped sub-path.
2014-11-17 05:59:17 +03:00
void nvgCircle(NVGcontext* ctx, float cx, float cy, float r);
2014-04-27 10:50:11 +04:00
// Fills the current path with current fill style.
2014-11-17 05:59:17 +03:00
void nvgFill(NVGcontext* ctx);
2014-04-27 10:50:11 +04:00
// Fills the current path with current stroke style.
2014-11-17 05:59:17 +03:00
void nvgStroke(NVGcontext* ctx);
2014-04-27 10:50:11 +04:00
//
// Text
//
// NanoVG allows you to load .ttf files and use the font to render text.
//
// The appearance of the text can be defined by setting the current text style
// and by specifying the fill color. Common text and font settings such as
// font size, letter spacing and text align are supported. Font blur allows you
// to create simple text effects such as drop shadows.
//
// At render time the font face can be set based on the font handles or name.
//
// Font measure functions return values in local space, the calculations are
// carried in the same resolution as the final rendering. This is done because
// the text glyph positions are snapped to the nearest pixels sharp rendering.
//
// The local space means that values are not rotated or scale as per the current
// transformation. For example if you set font size to 12, which would mean that
// line height is 16, then regardless of the current scaling and rotation, the
// returned line height is always 16. Some measures may vary because of the scaling
// since aforementioned pixel snapping.
//
// While this may sound a little odd, the setup allows you to always render the
// same way regardless of scaling. I.e. following works regardless of scaling:
//
// const char* txt = "Text me up.";
// nvgTextBounds(vg, x,y, txt, NULL, bounds);
// nvgBeginPath(vg);
// nvgRoundedRect(vg, bounds[0],bounds[1], bounds[2]-bounds[0], bounds[3]-bounds[1]);
// nvgFill(vg);
//
// Note: currently only solid color fill is supported for text.
// Creates font by loading it from the disk from specified file name.
// Returns handle to the font.
2014-11-17 05:59:17 +03:00
int nvgCreateFont(NVGcontext* ctx, const char* name, const char* filename);
2014-04-27 10:50:11 +04:00
// Creates image by loading it from the specified memory chunk.
// Returns handle to the font.
2014-11-17 05:59:17 +03:00
int nvgCreateFontMem(NVGcontext* ctx, const char* name, unsigned char* data, int ndata, int freeData);
2014-04-27 10:50:11 +04:00
// Finds a loaded font of specified name, and returns handle to it, or -1 if the font is not found.
2014-11-17 05:59:17 +03:00
int nvgFindFont(NVGcontext* ctx, const char* name);
2014-04-27 10:50:11 +04:00
// Sets the font size of current text style.
2014-11-17 05:59:17 +03:00
void nvgFontSize(NVGcontext* ctx, float size);
2014-04-27 10:50:11 +04:00
// Sets the blur of current text style.
2014-11-17 05:59:17 +03:00
void nvgFontBlur(NVGcontext* ctx, float blur);
2014-04-27 10:50:11 +04:00
// Sets the letter spacing of current text style.
2014-11-17 05:59:17 +03:00
void nvgTextLetterSpacing(NVGcontext* ctx, float spacing);
2014-04-27 10:50:11 +04:00
// Sets the proportional line height of current text style. The line height is specified as multiple of font size.
2014-11-17 05:59:17 +03:00
void nvgTextLineHeight(NVGcontext* ctx, float lineHeight);
2014-04-27 10:50:11 +04:00
2015-12-05 22:00:11 +03:00
// Sets the text align of current text style, see NVGalign for options.
2014-11-17 05:59:17 +03:00
void nvgTextAlign(NVGcontext* ctx, int align);
2014-04-27 10:50:11 +04:00
// Sets the font face based on specified id of current text style.
2014-11-17 05:59:17 +03:00
void nvgFontFaceId(NVGcontext* ctx, int font);
2014-04-27 10:50:11 +04:00
// Sets the font face based on specified name of current text style.
2014-11-17 05:59:17 +03:00
void nvgFontFace(NVGcontext* ctx, const char* font);
2014-04-27 10:50:11 +04:00
// Draws text string at specified location. If end is specified only the sub-string up to the end is drawn.
2014-11-17 05:59:17 +03:00
float nvgText(NVGcontext* ctx, float x, float y, const char* string, const char* end);
2014-04-27 10:50:11 +04:00
// 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.
// White space is stripped at the beginning of the rows, the text is split at word boundaries or when new-line characters are encountered.
// Words longer than the max width are slit at nearest character (i.e. no hyphenation).
2014-11-17 05:59:17 +03:00
void nvgTextBox(NVGcontext* ctx, float x, float y, float breakRowWidth, const char* string, const char* end);
2014-04-27 10:50:11 +04:00
// Measures the specified text string. Parameter bounds should be a pointer to float[4],
// if the bounding box of the text should be returned. The bounds value are [xmin,ymin, xmax,ymax]
// Returns the horizontal advance of the measured text (i.e. where the next character should drawn).
// Measured values are returned in local coordinate space.
2014-11-17 05:59:17 +03:00
float nvgTextBounds(NVGcontext* ctx, float x, float y, const char* string, const char* end, float* bounds);
2014-04-27 10:50:11 +04:00
// Measures the specified multi-text string. Parameter bounds should be a pointer to float[4],
// if the bounding box of the text should be returned. The bounds value are [xmin,ymin, xmax,ymax]
// Measured values are returned in local coordinate space.
2014-11-17 05:59:17 +03:00
void nvgTextBoxBounds(NVGcontext* ctx, float x, float y, float breakRowWidth, const char* string, const char* end, float* bounds);
2014-04-27 10:50:11 +04:00
// Calculates the glyph x positions of the specified text. If end is specified only the sub-string will be used.
// Measured values are returned in local coordinate space.
2014-11-17 05:59:17 +03:00
int nvgTextGlyphPositions(NVGcontext* ctx, float x, float y, const char* string, const char* end, NVGglyphPosition* positions, int maxPositions);
2014-04-27 10:50:11 +04:00
// Returns the vertical metrics based on the current text style.
// Measured values are returned in local coordinate space.
2014-11-17 05:59:17 +03:00
void nvgTextMetrics(NVGcontext* ctx, float* ascender, float* descender, float* lineh);
2014-04-27 10:50:11 +04:00
// Breaks the specified text into lines. If end is specified only the sub-string will be used.
// White space is stripped at the beginning of the rows, the text is split at word boundaries or when new-line characters are encountered.
// Words longer than the max width are slit at nearest character (i.e. no hyphenation).
2014-11-17 05:59:17 +03:00
int nvgTextBreakLines(NVGcontext* ctx, const char* string, const char* end, float breakRowWidth, NVGtextRow* rows, int maxRows);
2014-04-27 10:50:11 +04:00
//
// Internal Render API
//
enum NVGtexture {
NVG_TEXTURE_ALPHA = 0x01,
NVG_TEXTURE_RGBA = 0x02,
};
2014-11-17 05:59:17 +03:00
struct NVGscissor {
2014-04-27 10:50:11 +04:00
float xform[6];
float extent[2];
};
2014-11-17 05:59:17 +03:00
typedef struct NVGscissor NVGscissor;
2014-04-27 10:50:11 +04:00
struct NVGvertex {
float x,y,u,v;
};
2014-11-17 05:59:17 +03:00
typedef struct NVGvertex NVGvertex;
2014-04-27 10:50:11 +04:00
struct NVGpath {
int first;
int count;
unsigned char closed;
int nbevel;
2014-11-17 05:59:17 +03:00
NVGvertex* fill;
2014-04-27 10:50:11 +04:00
int nfill;
2014-11-17 05:59:17 +03:00
NVGvertex* stroke;
2014-04-27 10:50:11 +04:00
int nstroke;
int winding;
int convex;
};
2014-11-17 05:59:17 +03:00
typedef struct NVGpath NVGpath;
2014-04-27 10:50:11 +04:00
struct NVGparams {
void* userPtr;
int edgeAntiAlias;
int (*renderCreate)(void* uptr);
2014-11-17 05:59:17 +03:00
int (*renderCreateTexture)(void* uptr, int type, int w, int h, int imageFlags, const unsigned char* data);
2014-04-27 10:50:11 +04:00
int (*renderDeleteTexture)(void* uptr, int image);
int (*renderUpdateTexture)(void* uptr, int image, int x, int y, int w, int h, const unsigned char* data);
int (*renderGetTextureSize)(void* uptr, int image, int* w, int* h);
2015-12-05 22:00:11 +03:00
void (*renderViewport)(void* uptr, int width, int height);
2014-11-17 05:59:17 +03:00
void (*renderCancel)(void* uptr);
void (*renderFlush)(void* uptr);
void (*renderFill)(void* uptr, NVGpaint* paint, NVGscissor* scissor, float fringe, const float* bounds, const NVGpath* paths, int npaths);
void (*renderStroke)(void* uptr, NVGpaint* paint, NVGscissor* scissor, float fringe, float strokeWidth, const NVGpath* paths, int npaths);
void (*renderTriangles)(void* uptr, NVGpaint* paint, NVGscissor* scissor, const NVGvertex* verts, int nverts);
2014-04-27 10:50:11 +04:00
void (*renderDelete)(void* uptr);
};
2014-11-17 05:59:17 +03:00
typedef struct NVGparams NVGparams;
namespace bx { struct AllocatorI; }
NVGcontext* nvgCreate(int edgeaa, unsigned char _viewId, bx::AllocatorI* _allocator = NULL);
void nvgViewId(struct NVGcontext* ctx, unsigned char _viewId);
2014-11-17 05:59:17 +03:00
void nvgDelete(struct NVGcontext* ctx);
2014-04-27 10:50:11 +04:00
2015-12-05 22:00:11 +03:00
// Constructor and destructor, called by the render back-end.
2014-11-17 05:59:17 +03:00
NVGcontext* nvgCreateInternal(NVGparams* params);
void nvgDeleteInternal(NVGcontext* ctx);
2014-04-27 10:50:11 +04:00
2014-11-17 05:59:17 +03:00
NVGparams* nvgInternalParams(NVGcontext* ctx);
2014-04-27 10:50:11 +04:00
2014-11-17 05:59:17 +03:00
// Debug function to dump cached path data.
void nvgDebugDumpPathCache(NVGcontext* ctx);
2014-04-27 10:50:11 +04:00
2014-11-17 05:59:17 +03:00
#ifdef _MSC_VER
#pragma warning(pop)
#endif
2014-04-27 10:50:11 +04:00
2014-11-17 05:59:17 +03:00
#define NVG_NOTUSED(v) for (;;) { (void)(1 ? (void)0 : ( (void)(v) ) ); break; }
2014-04-27 10:50:11 +04:00
#ifdef __cplusplus
}
#endif
#endif // NANOVG_H