Debian Codegraph @

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

raw
1 // Based on https://www.freetype.org/freetype2/docs/tutorial/example5.cpp
2
3 #include <QDebug>
4
5 #include "freetypefont.h"
6
7 const FT_Fixed MULTIPLIER_FT = 65536L;
8
9 const char* getErrorMessage(FT_Error err)
10 {
11 #undef __FTERRORS_H__
12 #define FT_ERRORDEF( e, v, s ) case e: return s;
13 #define FT_ERROR_START_LIST switch (err) {
14 #define FT_ERROR_END_LIST }
15 #include FT_ERRORS_H
16 return "(Unknown error)";
17 }
18
19 struct Outliner
20 {
21 static int moveToFn(const FT_Vector *to, void *user)
22 {
23 auto self = static_cast<Outliner *>(user);
24 self->path.moveTo(to->x, to->y);
25 return 0;
26 }
27
28 static int lineToFn(const FT_Vector *to, void *user)
29 {
30 auto self = static_cast<Outliner *>(user);
31 self->path.lineTo(to->x, to->y);
32 return 0;
33 }
34
35 static int quadToFn(const FT_Vector *control, const FT_Vector *to, void *user)
36 {
37 auto self = static_cast<Outliner *>(user);
38 self->path.quadTo(control->x, control->y, to->x, to->y);
39 return 0;
40 }
41
42 static int cubicToFn(const FT_Vector *controlOne,
43 const FT_Vector *controlTwo,
44 const FT_Vector *to,
45 void *user)
46 {
47 auto self = static_cast<Outliner *>(user);
48 self->path.cubicTo(controlOne->x, controlOne->y, controlTwo->x, controlTwo->y, to->x, to->y);
49 return 0;
50 }
51
52 QPainterPath path;
53 };
54
55 FreeTypeFont::FreeTypeFont()
56 {
57 const auto error = FT_Init_FreeType(&m_ftLibrary);
58 if (error) {
59 throw tr("Failed to init FreeType.\n%1").arg(getErrorMessage(error));
60 }
61 }
62
63 FreeTypeFont::~FreeTypeFont()
64 {
65 if (m_ftFace) {
66 FT_Done_Face(m_ftFace);
67 }
68
69 FT_Done_FreeType(m_ftLibrary);
70 }
71
72 void FreeTypeFont::open(const QString &path, const quint32 index)
73 {
74 if (isOpen()) {
75 FT_Done_Face(m_ftFace);
76 m_ftFace = nullptr;
77 }
78
79 const auto utf8Path = path.toUtf8();
80 const auto error = FT_New_Face(m_ftLibrary, utf8Path.constData(), index, &m_ftFace);
81 if (error) {
82 throw tr("Failed to open a font.\n%1").arg(getErrorMessage(error));
83 }
84 }
85
86 bool FreeTypeFont::isOpen() const
87 {
88 return m_ftFace != nullptr;
89 }
90
91 FontInfo FreeTypeFont::fontInfo() const
92 {
93 if (!isOpen()) {
94 throw tr("Font is not loaded.");
95 }
96
97 return FontInfo {
98 m_ftFace->ascender,
99 m_ftFace->height,
100 (quint16)m_ftFace->num_glyphs, // TrueType allows only u16.
101 };
102 }
103
104 Glyph FreeTypeFont::outline(const quint16 gid) const
105 {
106 if (!isOpen()) {
107 throw tr("Font is not loaded.");
108 }
109
110 auto error = FT_Load_Glyph(m_ftFace, gid, FT_LOAD_NO_SCALE | FT_LOAD_NO_BITMAP);
111 if (error) {
112 throw tr("Failed to load a glyph.\n%1").arg(getErrorMessage(error));
113 }
114
115 Outliner outliner;
116
117 FT_Outline_Funcs funcs;
118 funcs.move_to = outliner.moveToFn;
119 funcs.line_to = outliner.lineToFn;
120 funcs.conic_to = outliner.quadToFn;
121 funcs.cubic_to = outliner.cubicToFn;
122 funcs.shift = 0;
123 funcs.delta = 0;
124
125 auto slot = m_ftFace->glyph;
126 auto &outline = slot->outline;
127
128 // Flip outline around x-axis.
129 FT_Matrix matrix;
130 matrix.xx = 1L * MULTIPLIER_FT;
131 matrix.xy = 0L * MULTIPLIER_FT;
132 matrix.yx = 0L * MULTIPLIER_FT;
133 matrix.yy = -1L * MULTIPLIER_FT;
134 FT_Outline_Transform(&outline, &matrix);
135
136 FT_BBox bboxFt;
137 FT_Outline_Get_BBox(&outline, &bboxFt);
138
139 const QRect bbox(
140 (int)bboxFt.xMin,
141 (int)bboxFt.yMin,
142 (int)bboxFt.xMax - (int)bboxFt.xMin,
143 (int)bboxFt.yMax - (int)bboxFt.yMin
144 );
145
146 error = FT_Outline_Decompose(&outline, &funcs, &outliner);
147 if (error) {
148 throw tr("Failed to outline a glyph.\n%1").arg(getErrorMessage(error));
149 }
150
151 outliner.path.setFillRule(Qt::WindingFill);
152
153 return Glyph {
154 outliner.path,
155 bbox,
156 };
157 }
158
159 void FreeTypeFont::setVariations(const QVector<Variation> &variations)
160 {
161 if (!isOpen()) {
162 throw tr("Font is not loaded.");
163 }
164
165 QVector<FT_Fixed> ftCoords;
166
167 for (const auto &var : variations) {
168 ftCoords << var.value * MULTIPLIER_FT;
169 }
170
171 const auto error = FT_Set_Var_Design_Coordinates(m_ftFace, ftCoords.size(), ftCoords.data());
172 if (error) {
173 throw tr("Failed to set variation.\n%1").arg(getErrorMessage(error));
174 }
175 }