Debian Codegraph @

debian/missing-sources/ttf-parser/testing-tools/font-view/ttfparserfont.cpp

raw
1 #include <QTransform>
2 #include <QFile>
3 #include <QDebug>
4
5 #include "ttfparserfont.h"
6
7 struct Outliner
8 {
9 static void moveToFn(float x, float y, void *user)
10 {
11 auto self = static_cast<Outliner *>(user);
12 self->path.moveTo(double(x), double(y));
13 }
14
15 static void lineToFn(float x, float y, void *user)
16 {
17 auto self = static_cast<Outliner *>(user);
18 self->path.lineTo(double(x), double(y));
19 }
20
21 static void quadToFn(float x1, float y1, float x, float y, void *user)
22 {
23 auto self = static_cast<Outliner *>(user);
24 self->path.quadTo(double(x1), double(y1), double(x), double(y));
25 }
26
27 static void curveToFn(float x1, float y1, float x2, float y2, float x, float y, void *user)
28 {
29 auto self = static_cast<Outliner *>(user);
30 self->path.cubicTo(double(x1), double(y1), double(x2), double(y2), double(x), double(y));
31 }
32
33 static void closePathFn(void *user)
34 {
35 auto self = static_cast<Outliner *>(user);
36 self->path.closeSubpath();
37 }
38
39 QPainterPath path;
40 };
41
42 TtfParserFont::TtfParserFont()
43 {
44 }
45
46 void TtfParserFont::open(const QString &path, const quint32 index)
47 {
48 if (isOpen()) {
49 m_face.reset();
50 }
51
52 QFile file(path);
53 file.open(QFile::ReadOnly);
54 m_fontData = file.readAll();
55
56 m_face.reset((ttfp_face*)malloc(ttfp_face_size_of()));
57 const auto res = ttfp_face_init(m_fontData.constData(), m_fontData.size(), index, m_face.get());
58
59 if (!res) {
60 throw tr("Failed to open a font.");
61 }
62 }
63
64 bool TtfParserFont::isOpen() const
65 {
66 return m_face != nullptr;
67 }
68
69 FontInfo TtfParserFont::fontInfo() const
70 {
71 if (!isOpen()) {
72 throw tr("Font is not loaded.");
73 }
74
75 return FontInfo {
76 ttfp_get_ascender(m_face.get()),
77 ttfp_get_height(m_face.get()),
78 ttfp_get_number_of_glyphs(m_face.get()),
79 };
80 }
81
82 Glyph TtfParserFont::outline(const quint16 gid) const
83 {
84 if (!isOpen()) {
85 throw tr("Font is not loaded.");
86 }
87
88 Outliner outliner;
89 ttfp_outline_builder builder;
90 builder.move_to = outliner.moveToFn;
91 builder.line_to = outliner.lineToFn;
92 builder.quad_to = outliner.quadToFn;
93 builder.curve_to = outliner.curveToFn;
94 builder.close_path = outliner.closePathFn;
95
96 ttfp_rect rawBbox;
97
98 const bool ok = ttfp_outline_glyph(
99 m_face.get(),
100 builder,
101 &outliner,
102 gid,
103 &rawBbox
104 );
105
106 if (!ok) {
107 return Glyph {
108 QPainterPath(),
109 QRect(),
110 };
111 }
112
113 const QRect bbox(
114 rawBbox.x_min,
115 -rawBbox.y_max,
116 rawBbox.x_max - rawBbox.x_min,
117 rawBbox.y_max - rawBbox.y_min
118 );
119
120 // Flip outline around x-axis.
121 QTransform ts(1, 0, 0, -1, 0, 0);
122 outliner.path = ts.map(outliner.path);
123
124 outliner.path.setFillRule(Qt::WindingFill);
125
126 return Glyph {
127 outliner.path,
128 bbox,
129 };
130 }
131
132 QVector<VariationInfo> TtfParserFont::loadVariations()
133 {
134 if (!isOpen()) {
135 throw tr("Font is not loaded.");
136 }
137
138 QVector<VariationInfo> variations;
139
140 for (uint16_t i = 0; i < ttfp_get_variation_axes_count(m_face.get()); ++i) {
141 ttfp_variation_axis axis;
142 ttfp_get_variation_axis(m_face.get(), i, &axis);
143
144 variations.append(VariationInfo {
145 Tag(axis.tag).toString(),
146 { static_cast<quint32>(axis.tag) },
147 static_cast<qint16>(axis.min_value),
148 static_cast<qint16>(axis.def_value),
149 static_cast<qint16>(axis.max_value),
150 });
151 }
152
153 return variations;
154 }
155
156 void TtfParserFont::setVariations(const QVector<Variation> &variations)
157 {
158 if (!isOpen()) {
159 throw tr("Font is not loaded.");
160 }
161
162 for (const auto &variation : variations) {
163 ttfp_set_variation(m_face.get(), variation.tag.value, variation.value);
164 }
165 }