Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/msdfgen/core/export-svg.cpp
9902 views
1
2
#include "export-svg.h"
3
4
#include <cstdio>
5
#include "edge-segments.h"
6
7
namespace msdfgen {
8
9
static void writeSvgCoord(FILE *f, Point2 coord) {
10
fprintf(f, "%.17g %.17g", coord.x, coord.y);
11
}
12
13
static void writeSvgPathDef(FILE *f, const Shape &shape) {
14
bool beginning = true;
15
for (const Contour &c : shape.contours) {
16
if (c.edges.empty())
17
continue;
18
if (beginning)
19
beginning = false;
20
else
21
fputc(' ', f);
22
fputs("M ", f);
23
writeSvgCoord(f, c.edges[0]->controlPoints()[0]);
24
for (const EdgeHolder &e : c.edges) {
25
const Point2 *cp = e->controlPoints();
26
switch (e->type()) {
27
case (int) LinearSegment::EDGE_TYPE:
28
fputs(" L ", f);
29
writeSvgCoord(f, cp[1]);
30
break;
31
case (int) QuadraticSegment::EDGE_TYPE:
32
fputs(" Q ", f);
33
writeSvgCoord(f, cp[1]);
34
fputc(' ', f);
35
writeSvgCoord(f, cp[2]);
36
break;
37
case (int) CubicSegment::EDGE_TYPE:
38
fputs(" C ", f);
39
writeSvgCoord(f, cp[1]);
40
fputc(' ', f);
41
writeSvgCoord(f, cp[2]);
42
fputc(' ', f);
43
writeSvgCoord(f, cp[3]);
44
break;
45
}
46
}
47
fputs(" Z", f);
48
}
49
}
50
51
bool saveSvgShape(const Shape &shape, const char *filename) {
52
if (FILE *f = fopen(filename, "w")) {
53
fputs("<svg xmlns=\"http://www.w3.org/2000/svg\"><path", f);
54
if (!shape.inverseYAxis)
55
fputs(" transform=\"scale(1 -1)\"", f);
56
fputs(" d=\"", f);
57
writeSvgPathDef(f, shape);
58
fputs("\"/></svg>\n", f);
59
fclose(f);
60
return true;
61
}
62
return false;
63
}
64
65
bool saveSvgShape(const Shape &shape, const Shape::Bounds &bounds, const char *filename) {
66
if (FILE *f = fopen(filename, "w")) {
67
fprintf(f, "<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"%.17g %.17g %.17g %.17g\"><path", bounds.l, bounds.b, bounds.r-bounds.l, bounds.t-bounds.b);
68
if (!shape.inverseYAxis)
69
fprintf(f, " transform=\"translate(0 %.17g) scale(1 -1)\"", bounds.b+bounds.t);
70
fputs(" d=\"", f);
71
writeSvgPathDef(f, shape);
72
fputs("\"/></svg>\n", f);
73
fclose(f);
74
return true;
75
}
76
return false;
77
}
78
79
}
80
81