1#include "python.h"
2
3using namespace pybind11::literals;
4
5void register_nanovg(py::module &m) {
6 /* NanoVG */
7
8 /* To be done: docstrings, functions taking memory arguments (fonts, images) */
9
10 py::module nvg = m.def_submodule("nanovg", "NanoVG bindings");
11
12 py::enum_<NVGwinding>(nvg, "NVGwinding")
13 .value("CCW", NVG_CCW)
14 .value("CW", NVG_CW)
15 .export_values();
16
17 py::enum_<NVGsolidity>(nvg, "NVGsolidity")
18 .value("SOLID", NVG_SOLID)
19 .value("HOLE", NVG_HOLE)
20 .export_values();
21
22 py::enum_<NVGlineCap>(nvg, "NVGlineCap")
23 .value("BUTT", NVG_BUTT)
24 .value("ROUND", NVG_ROUND)
25 .value("SQUARE", NVG_SQUARE)
26 .value("BEVEL", NVG_BEVEL)
27 .value("MITER", NVG_MITER)
28 .export_values();
29
30 py::enum_<NVGalign>(nvg, "NVGalign")
31 .value("ALIGN_LEFT", NVG_ALIGN_LEFT)
32 .value("ALIGN_CENTER", NVG_ALIGN_CENTER)
33 .value("ALIGN_RIGHT", NVG_ALIGN_RIGHT)
34 .value("ALIGN_TOP", NVG_ALIGN_TOP)
35 .value("ALIGN_MIDDLE", NVG_ALIGN_MIDDLE)
36 .value("ALIGN_BOTTOM", NVG_ALIGN_BOTTOM)
37 .value("ALIGN_BASELINE", NVG_ALIGN_BASELINE)
38 .export_values();
39
40 py::enum_<NVGblendFactor>(nvg, "NVGblendFactor")
41 .value("ZERO", NVG_ZERO)
42 .value("ONE", NVG_ONE)
43 .value("SRC_COLOR", NVG_SRC_COLOR)
44 .value("ONE_MINUS_SRC_COLOR", NVG_ONE_MINUS_SRC_COLOR)
45 .value("DST_COLOR", NVG_DST_COLOR)
46 .value("ONE_MINUS_DST_COLOR", NVG_ONE_MINUS_DST_COLOR)
47 .value("SRC_ALPHA", NVG_SRC_ALPHA)
48 .value("ONE_MINUS_SRC_ALPHA", NVG_ONE_MINUS_SRC_ALPHA)
49 .value("DST_ALPHA", NVG_DST_ALPHA)
50 .value("ONE_MINUS_DST_ALPHA", NVG_ONE_MINUS_DST_ALPHA)
51 .value("SRC_ALPHA_SATURATE", NVG_SRC_ALPHA_SATURATE)
52 .export_values();
53
54 py::enum_<NVGcompositeOperation>(nvg, "NVGcompositeOperation")
55 .value("SOURCE_OVER", NVG_SOURCE_OVER)
56 .value("SOURCE_IN", NVG_SOURCE_IN)
57 .value("SOURCE_OUT", NVG_SOURCE_OUT)
58 .value("ATOP", NVG_ATOP)
59 .value("DESTINATION_OVER", NVG_DESTINATION_OVER)
60 .value("DESTINATION_IN", NVG_DESTINATION_IN)
61 .value("DESTINATION_OUT", NVG_DESTINATION_OUT)
62 .value("DESTINATION_ATOP", NVG_DESTINATION_ATOP)
63 .value("LIGHTER", NVG_LIGHTER)
64 .value("COPY", NVG_COPY)
65 .value("XOR", NVG_XOR)
66 .export_values();
67
68 py::class_<NVGpaint>(nvg, "NVGpaint");
69
70 py::class_<NVGcolor>(nvg, "NVGcolor")
71 .def("__init__",
72 [](NVGcolor &c, const Color &c2) { new (&c) NVGcolor(c2); }
73 );
74
75 py::implicitly_convertible<Color, NVGcolor>();
76
77 nvg.def("RGB", &nvgRGB);
78 nvg.def("RGBf", &nvgRGBf);
79 nvg.def("RGBA", &nvgRGBA);
80 nvg.def("RGBAf", &nvgRGBAf);
81
82 nvg.def("LerpRGBA", &nvgLerpRGBA);
83 nvg.def("TransRGBA", &nvgTransRGBA);
84 nvg.def("TransRGBAf", &nvgTransRGBAf);
85
86 nvg.def("HSL", &nvgHSL);
87 nvg.def("HSLA", &nvgHSLA);
88
89 //nvg.def("TextGlyphPositions", &nvgTextBounds);
90 //nvg.def("TextGlyphMetrics", &nvgTextBounds);
91 //nvg.def("TextTextBreakLines", &nvgTextBounds);
92
93 py::class_<NVGcontext>(nvg, "NVGcontext")
94 .def("GlobalCompositeOperation", &nvgGlobalCompositeOperation,
95 "factor"_a)
96 .def("GlobalCompositeBlendFunc", &nvgGlobalCompositeBlendFunc,
97 "sfactor"_a, "dfactor"_a)
98 .def("GlobalCompositeBlendFuncSeparate",
99 &nvgGlobalCompositeBlendFuncSeparate, "srcRGB"_a, "dstRGB"_a,
100 "srcAlpha"_a, "dstAlpha"_a)
101 .def("Save", &nvgSave)
102 .def("Restore", &nvgRestore)
103 .def("Reset", &nvgReset)
104 .def("StrokeColor", &nvgStrokeColor, "color"_a)
105 .def("StrokePaint", &nvgStrokePaint, "paint"_a)
106 .def("FillColor", &nvgFillColor, "color"_a)
107 .def("FillPaint", &nvgFillPaint, "paint"_a)
108 .def("MiterLimit", &nvgMiterLimit, "limit"_a)
109 .def("StrokeWidth", &nvgStrokeWidth, "size"_a)
110 .def("LineCap", &nvgLineCap, "cap"_a)
111 .def("LineJoin", &nvgLineJoin, "join"_a)
112 .def("GlobalAlpha", &nvgGlobalAlpha, "alpha"_a)
113 .def("ResetTransform", &nvgResetTransform)
114 .def("Transform", &nvgTransform, "a"_a, "b"_a, "c"_a, "d"_a, "e"_a,
115 "f"_a)
116 .def("Translate", &nvgTranslate, "x"_a, "y"_a)
117 .def("Rotate", &nvgRotate, "angle"_a)
118 .def("SkewX", &nvgSkewX, "angle"_a)
119 .def("SkewY", &nvgSkewY, "angle"_a)
120 .def("Scale", &nvgScale, "x"_a, "y"_a)
121 .def("CreateImage", &nvgCreateImage, "filename"_a, "imageFlags"_a)
122 .def("DeleteImage", &nvgDeleteImage, "image"_a)
123 .def("LinearGradient", &nvgLinearGradient, "sx"_a, "sy"_a, "ex"_a,
124 "ey"_a, "icol"_a, "ocol"_a)
125 .def("BoxGradient", &nvgBoxGradient, "x"_a, "y"_a, "w"_a, "h"_a, "r"_a,
126 "f"_a, "icol"_a, "ocol"_a)
127 .def("RadialGradient", &nvgRadialGradient, "cx"_a, "cy"_a, "inr"_a,
128 "outr"_a, "icol"_a, "ocol"_a)
129 .def("ImagePattern", &nvgImagePattern, "ox"_a, "oy"_a, "ex"_a, "ey"_a,
130 "angle"_a, "image"_a, "alpha"_a)
131 .def("Scissor", &nvgScissor, "x"_a, "y"_a, "w"_a, "h"_a)
132 .def("IntersectScissor", &nvgIntersectScissor, "x"_a, "y"_a, "w"_a,
133 "h"_a)
134 .def("ResetScissor", &nvgResetScissor)
135 .def("BeginPath", &nvgBeginPath)
136 .def("MoveTo", &nvgMoveTo, "x"_a, "y"_a)
137 .def("LineTo", &nvgLineTo, "x"_a, "y"_a)
138 .def("BezierTo", &nvgBezierTo, "c1x"_a, "c1y"_a, "c2x"_a, "c2y"_a,
139 "x"_a, "y"_a)
140 .def("QuadTo", &nvgQuadTo, "cx"_a, "cy"_a, "x"_a, "y"_a)
141 .def("ArcTo", &nvgArcTo, "x1"_a, "y1"_a, "x2"_a, "y2"_a, "radius"_a)
142 .def("ClosePath", &nvgClosePath)
143 .def("PathWinding", &nvgPathWinding, "dir"_a)
144 .def("Arc", &nvgArc, "cx"_a, "cy"_a, "r"_a, "a0"_a, "a1"_a, "dir"_a)
145 .def("Rect", &nvgRect, "x"_a, "y"_a, "w"_a, "h"_a)
146 .def("RoundedRect", &nvgRoundedRect, "x"_a, "y"_a, "w"_a, "h"_a, "r"_a)
147 .def("RoundedRectVarying", &nvgRoundedRectVarying, "x"_a, "y"_a, "w"_a,
148 "h"_a, "radTopLeft"_a, "radTopRight"_a, "radBottomRight"_a,
149 "radBottomLeft"_a)
150 .def("Ellipse", &nvgEllipse, "cx"_a, "cy"_a, "rx"_a, "ry"_a)
151 .def("Circle", &nvgCircle, "cx"_a, "cy"_a, "r"_a)
152 .def("Fill", &nvgFill)
153 .def("Stroke", &nvgStroke)
154 .def("CreateFont", &nvgCreateFont, "name"_a, "filename"_a)
155 .def("FindFont", &nvgFindFont, "name"_a)
156 .def("AddFallbackFontId", &nvgAddFallbackFontId, "baseFont"_a,
157 "fallbackFont"_a)
158 .def("AddFallbackFont", &nvgAddFallbackFont, "baseFont"_a,
159 "fallbackFont"_a)
160 .def("FontSize", &nvgFontSize, "size"_a)
161 .def("FontBlur", &nvgFontBlur, "blur"_a)
162 .def("TextLetterSpacing", &nvgTextLetterSpacing, "spacing"_a)
163 .def("TextLineHeight", &nvgTextLineHeight, "lineHeight"_a)
164 .def("TextAlign", &nvgTextAlign, "align"_a)
165 .def("FontFaceId", &nvgFontFaceId, "font"_a)
166 .def("FontFace", &nvgFontFace, "font"_a)
167 .def("Text", [](NVGcontext *ctx, float x, float y,
168 const char *str) { nvgText(ctx, x, y, str, nullptr); },
169 "x"_a, "y"_a, "string"_a)
170 .def("TextBounds",
171 [](NVGcontext *ctx, float x, float y, const char *str) {
172 float bounds[4];
173 nvgTextBounds(ctx, x, y, str, nullptr, bounds);
174 return std::make_tuple(bounds[0], bounds[1], bounds[2], bounds[3]);
175 }, "x"_a, "y"_a, "string"_a)
176 .def("TextBox",
177 [](NVGcontext *ctx, float x, float y, float breakRowWidth,
178 const char *str) {
179 nvgTextBox(ctx, x, y, breakRowWidth, str, nullptr);
180 },
181 "x"_a, "y"_a, "breakRowWidth"_a, "string"_a)
182 .def("TextBoxBounds",
183 [](NVGcontext *ctx, float x, float y, float breakRowWidth,
184 const char *str) {
185 float bounds[4];
186 nvgTextBoxBounds(ctx, x, y, breakRowWidth, str, nullptr, bounds);
187 return std::make_tuple(bounds[0], bounds[1], bounds[2], bounds[3]);
188 },
189 "x"_a, "y"_a, "breakRowWidth"_a, "string"_a)
190 .def("BeginFrame", &nvgBeginFrame, "windowWidth"_a, "windowHeight"_a,
191 "devicePixelRatio"_a)
192 .def("CancelFrame", &nvgCancelFrame)
193 .def("EndFrame", &nvgEndFrame);
194}
195