Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/gui/text/qfontsubset.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||
2 | ** | - | ||||||||||||
3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||||||||
4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||
5 | ** | - | ||||||||||||
6 | ** This file is part of the QtGui module of the Qt Toolkit. | - | ||||||||||||
7 | ** | - | ||||||||||||
8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||
9 | ** Commercial License Usage | - | ||||||||||||
10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||
11 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||
12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||
13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||
14 | ** and conditions see http://www.qt.io/terms-conditions. For further | - | ||||||||||||
15 | ** information use the contact form at http://www.qt.io/contact-us. | - | ||||||||||||
16 | ** | - | ||||||||||||
17 | ** GNU Lesser General Public License Usage | - | ||||||||||||
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||
19 | ** General Public License version 2.1 or version 3 as published by the Free | - | ||||||||||||
20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||
21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||
22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||
23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||
24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||
25 | ** | - | ||||||||||||
26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||
27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||
28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||
29 | ** | - | ||||||||||||
30 | ** $QT_END_LICENSE$ | - | ||||||||||||
31 | ** | - | ||||||||||||
32 | ****************************************************************************/ | - | ||||||||||||
33 | - | |||||||||||||
34 | #include "qfontsubset_p.h" | - | ||||||||||||
35 | #include <qdebug.h> | - | ||||||||||||
36 | #include <qendian.h> | - | ||||||||||||
37 | #include <qpainterpath.h> | - | ||||||||||||
38 | #include "private/qpdf_p.h" | - | ||||||||||||
39 | - | |||||||||||||
40 | #include "qfontsubset_agl.cpp" | - | ||||||||||||
41 | - | |||||||||||||
42 | #include <algorithm> | - | ||||||||||||
43 | - | |||||||||||||
44 | QT_BEGIN_NAMESPACE | - | ||||||||||||
45 | - | |||||||||||||
46 | // This map is used for symbol fonts to get the correct glyph names for the latin range | - | ||||||||||||
47 | static const unsigned short symbol_map[0x100] = { | - | ||||||||||||
48 | 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, | - | ||||||||||||
49 | 0x0008, 0x0009, 0x000a, 0x000b, 0x000c, 0x000d, 0x000e, 0x000f, | - | ||||||||||||
50 | 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, | - | ||||||||||||
51 | 0x0018, 0x0019, 0x001a, 0x001b, 0x001c, 0x001d, 0x001e, 0x001f, | - | ||||||||||||
52 | 0x0020, 0x0021, 0x2200, 0x0023, 0x2203, 0x0025, 0x0026, 0x220b, | - | ||||||||||||
53 | 0x0028, 0x0029, 0x2217, 0x002b, 0x002c, 0x2212, 0x002e, 0x002f, | - | ||||||||||||
54 | 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, | - | ||||||||||||
55 | 0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d, 0x003e, 0x003f, | - | ||||||||||||
56 | - | |||||||||||||
57 | 0x2245, 0x0391, 0x0392, 0x03a7, 0x0394, 0x0395, 0x03a6, 0x0393, | - | ||||||||||||
58 | 0x0397, 0x0399, 0x03d1, 0x039a, 0x039b, 0x039c, 0x039d, 0x039f, | - | ||||||||||||
59 | 0x03a0, 0x0398, 0x03a1, 0x03a3, 0x03a4, 0x03a5, 0x03c2, 0x03a9, | - | ||||||||||||
60 | 0x039e, 0x03a8, 0x0396, 0x005b, 0x2234, 0x005d, 0x22a5, 0x005f, | - | ||||||||||||
61 | 0xf8e5, 0x03b1, 0x03b2, 0x03c7, 0x03b4, 0x03b5, 0x03c6, 0x03b3, | - | ||||||||||||
62 | 0x03b7, 0x03b9, 0x03d5, 0x03ba, 0x03bb, 0x03bc, 0x03bd, 0x03bf, | - | ||||||||||||
63 | 0x03c0, 0x03b8, 0x03c1, 0x03c3, 0x03c4, 0x03c5, 0x03d6, 0x03c9, | - | ||||||||||||
64 | 0x03be, 0x03c8, 0x03b6, 0x007b, 0x007c, 0x007d, 0x223c, 0x007f, | - | ||||||||||||
65 | - | |||||||||||||
66 | 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087, | - | ||||||||||||
67 | 0x0088, 0x0089, 0x008a, 0x008b, 0x008c, 0x008d, 0x008e, 0x008f, | - | ||||||||||||
68 | 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097, | - | ||||||||||||
69 | 0x0098, 0x0099, 0x009a, 0x009b, 0x009c, 0x009d, 0x009e, 0x009f, | - | ||||||||||||
70 | 0x20ac, 0x03d2, 0x2023, 0x2264, 0x2044, 0x221e, 0x0192, 0x2263, | - | ||||||||||||
71 | 0x2666, 0x2665, 0x2660, 0x2194, 0x2190, 0x2191, 0x2192, 0x2193, | - | ||||||||||||
72 | 0x00b0, 0x00b1, 0x2033, 0x2265, 0x00d7, 0x221d, 0x2202, 0x2022, | - | ||||||||||||
73 | 0x00f7, 0x2260, 0x2261, 0x2248, 0x2026, 0xf8e6, 0xf8e7, 0x21b5, | - | ||||||||||||
74 | - | |||||||||||||
75 | 0x2135, 0x2111, 0x211c, 0x2118, 0x2297, 0x2295, 0x2205, 0x2229, | - | ||||||||||||
76 | 0x222a, 0x2283, 0x2287, 0x2284, 0x2282, 0x2286, 0x2208, 0x2209, | - | ||||||||||||
77 | 0x2220, 0x2207, 0xf6da, 0xf6d9, 0xf6db, 0x220f, 0x221a, 0x22c5, | - | ||||||||||||
78 | 0x00ac, 0x2227, 0x2228, 0x21d4, 0x21d0, 0x21d1, 0x21d2, 0x21d3, | - | ||||||||||||
79 | 0x25ca, 0x2329, 0xf8e8, 0xf8e9, 0xf8ea, 0x2211, 0xf8eb, 0xf8ec, | - | ||||||||||||
80 | 0xf8ed, 0xf8ee, 0xf8ef, 0xf8f0, 0xf8f1, 0xf8f2, 0xf8f3, 0xf8f4, | - | ||||||||||||
81 | 0x0000, 0x232a, 0x222b, 0x2320, 0xf8f5, 0x2321, 0xf8f6, 0xf8f7, | - | ||||||||||||
82 | 0xf8f8, 0xf8f9, 0xf8fa, 0xf8fb, 0xf8fc, 0xf8fd, 0xf8fe, 0x0000 | - | ||||||||||||
83 | }; | - | ||||||||||||
84 | - | |||||||||||||
85 | // ---------------------------- PS/PDF helper methods ----------------------------------- | - | ||||||||||||
86 | - | |||||||||||||
87 | #ifndef QT_NO_PDF | - | ||||||||||||
88 | - | |||||||||||||
89 | QByteArray QFontSubset::glyphName(unsigned short unicode, bool symbol) | - | ||||||||||||
90 | { | - | ||||||||||||
91 | if (symbol && unicode < 0x100)
| 0 | ||||||||||||
92 | // map from latin1 to symbol | - | ||||||||||||
93 | unicode = symbol_map[unicode]; never executed: unicode = symbol_map[unicode]; | 0 | ||||||||||||
94 | - | |||||||||||||
95 | const AGLEntry *r = std::lower_bound(unicode_to_agl_map, unicode_to_agl_map + unicode_to_agl_map_size, unicode); | - | ||||||||||||
96 | if ((r != unicode_to_agl_map + unicode_to_agl_map_size) && !(unicode < *r))
| 0 | ||||||||||||
97 | return glyph_names + r->index; never executed: return glyph_names + r->index; | 0 | ||||||||||||
98 | - | |||||||||||||
99 | char buffer[8]; | - | ||||||||||||
100 | buffer[0] = 'u'; | - | ||||||||||||
101 | buffer[1] = 'n'; | - | ||||||||||||
102 | buffer[2] = 'i'; | - | ||||||||||||
103 | QPdf::toHex(unicode, buffer+3); | - | ||||||||||||
104 | return buffer; never executed: return buffer; | 0 | ||||||||||||
105 | } | - | ||||||||||||
106 | - | |||||||||||||
107 | QByteArray QFontSubset::glyphName(unsigned int glyph, const QVector<int> &reverseMap) const | - | ||||||||||||
108 | { | - | ||||||||||||
109 | uint glyphIndex = glyph_indices[glyph]; | - | ||||||||||||
110 | - | |||||||||||||
111 | if (glyphIndex == 0)
| 0 | ||||||||||||
112 | return "/.notdef"; never executed: return "/.notdef"; | 0 | ||||||||||||
113 | - | |||||||||||||
114 | QByteArray ba; | - | ||||||||||||
115 | QPdf::ByteStream s(&ba); | - | ||||||||||||
116 | if (reverseMap[glyphIndex] && reverseMap[glyphIndex] < 0x10000) {
| 0 | ||||||||||||
117 | s << '/' << glyphName(reverseMap[glyphIndex], false); | - | ||||||||||||
118 | } else { never executed: end of block | 0 | ||||||||||||
119 | s << "/gl" << (int)glyphIndex; | - | ||||||||||||
120 | } never executed: end of block | 0 | ||||||||||||
121 | return ba; never executed: return ba; | 0 | ||||||||||||
122 | } | - | ||||||||||||
123 | - | |||||||||||||
124 | - | |||||||||||||
125 | QByteArray QFontSubset::widthArray() const | - | ||||||||||||
126 | { | - | ||||||||||||
127 | Q_ASSERT(!widths.isEmpty()); | - | ||||||||||||
128 | - | |||||||||||||
129 | QFontEngine::Properties properties = fontEngine->properties(); | - | ||||||||||||
130 | - | |||||||||||||
131 | QByteArray width; | - | ||||||||||||
132 | QPdf::ByteStream s(&width); | - | ||||||||||||
133 | QFixed scale = QFixed(1000)/emSquare; | - | ||||||||||||
134 | - | |||||||||||||
135 | QFixed defWidth = widths[0]; | - | ||||||||||||
136 | //qDebug("defWidth=%d, scale=%f", defWidth.toInt(), scale.toReal()); | - | ||||||||||||
137 | for (int i = 0; i < nGlyphs(); ++i) {
| 0 | ||||||||||||
138 | if (defWidth != widths[i])
| 0 | ||||||||||||
139 | defWidth = 0; never executed: defWidth = 0; | 0 | ||||||||||||
140 | } never executed: end of block | 0 | ||||||||||||
141 | if (defWidth > 0) {
| 0 | ||||||||||||
142 | s << "/DW " << (defWidth*scale).toInt(); | - | ||||||||||||
143 | } else { never executed: end of block | 0 | ||||||||||||
144 | s << "/W ["; | - | ||||||||||||
145 | for (int g = 0; g < nGlyphs();) {
| 0 | ||||||||||||
146 | QFixed w = widths[g]; | - | ||||||||||||
147 | int start = g; | - | ||||||||||||
148 | int startLinear = 0; | - | ||||||||||||
149 | ++g; | - | ||||||||||||
150 | while (g < nGlyphs()) {
| 0 | ||||||||||||
151 | QFixed nw = widths[g]; | - | ||||||||||||
152 | if (nw == w) {
| 0 | ||||||||||||
153 | if (!startLinear)
| 0 | ||||||||||||
154 | startLinear = g - 1; never executed: startLinear = g - 1; | 0 | ||||||||||||
155 | } else { never executed: end of block | 0 | ||||||||||||
156 | if (startLinear > 0 && g - startLinear >= 10)
| 0 | ||||||||||||
157 | break; never executed: break; | 0 | ||||||||||||
158 | startLinear = 0; | - | ||||||||||||
159 | } never executed: end of block | 0 | ||||||||||||
160 | w = nw; | - | ||||||||||||
161 | ++g; | - | ||||||||||||
162 | } never executed: end of block | 0 | ||||||||||||
163 | // qDebug("start=%x startLinear=%x g-1=%x",start,startLinear,g-1); | - | ||||||||||||
164 | if (g - startLinear < 10)
| 0 | ||||||||||||
165 | startLinear = 0; never executed: startLinear = 0; | 0 | ||||||||||||
166 | int endnonlinear = startLinear ? startLinear : g;
| 0 | ||||||||||||
167 | // qDebug(" startLinear=%x endnonlinear=%x", startLinear,endnonlinear); | - | ||||||||||||
168 | if (endnonlinear > start) {
| 0 | ||||||||||||
169 | s << start << '['; | - | ||||||||||||
170 | for (int i = start; i < endnonlinear; ++i)
| 0 | ||||||||||||
171 | s << (widths[i]*scale).toInt(); never executed: s << (widths[i]*scale).toInt(); | 0 | ||||||||||||
172 | s << "]\n"; | - | ||||||||||||
173 | } never executed: end of block | 0 | ||||||||||||
174 | if (startLinear)
| 0 | ||||||||||||
175 | s << startLinear << g - 1 << (widths[startLinear]*scale).toInt() << '\n'; never executed: s << startLinear << g - 1 << (widths[startLinear]*scale).toInt() << '\n'; | 0 | ||||||||||||
176 | } never executed: end of block | 0 | ||||||||||||
177 | s << "]\n"; | - | ||||||||||||
178 | } never executed: end of block | 0 | ||||||||||||
179 | return width; never executed: return width; | 0 | ||||||||||||
180 | } | - | ||||||||||||
181 | - | |||||||||||||
182 | static void checkRanges(QPdf::ByteStream &ts, QByteArray &ranges, int &nranges) | - | ||||||||||||
183 | { | - | ||||||||||||
184 | if (++nranges > 100) {
| 0 | ||||||||||||
185 | ts << nranges << "beginbfrange\n" | - | ||||||||||||
186 | << ranges << "endbfrange\n"; | - | ||||||||||||
187 | ranges = QByteArray(); | - | ||||||||||||
188 | nranges = 0; | - | ||||||||||||
189 | } never executed: end of block | 0 | ||||||||||||
190 | } never executed: end of block | 0 | ||||||||||||
191 | - | |||||||||||||
192 | QVector<int> QFontSubset::getReverseMap() const | - | ||||||||||||
193 | { | - | ||||||||||||
194 | QVector<int> reverseMap(0x10000, 0); | - | ||||||||||||
195 | for (uint uc = 0; uc < 0x10000; ++uc) {
| 0 | ||||||||||||
196 | int idx = glyph_indices.indexOf(fontEngine->glyphIndex(uc)); | - | ||||||||||||
197 | if (idx >= 0 && !reverseMap.at(idx))
| 0 | ||||||||||||
198 | reverseMap[idx] = uc; never executed: reverseMap[idx] = uc; | 0 | ||||||||||||
199 | } never executed: end of block | 0 | ||||||||||||
200 | return reverseMap; never executed: return reverseMap; | 0 | ||||||||||||
201 | } | - | ||||||||||||
202 | - | |||||||||||||
203 | QByteArray QFontSubset::createToUnicodeMap() const | - | ||||||||||||
204 | { | - | ||||||||||||
205 | QVector<int> reverseMap = getReverseMap(); | - | ||||||||||||
206 | - | |||||||||||||
207 | QByteArray touc; | - | ||||||||||||
208 | QPdf::ByteStream ts(&touc); | - | ||||||||||||
209 | ts << "/CIDInit /ProcSet findresource begin\n" | - | ||||||||||||
210 | "12 dict begin\n" | - | ||||||||||||
211 | "begincmap\n" | - | ||||||||||||
212 | "/CIDSystemInfo << /Registry (Adobe) /Ordering (UCS) /Supplement 0 >> def\n" | - | ||||||||||||
213 | "/CMapName /Adobe-Identity-UCS def\n" | - | ||||||||||||
214 | "/CMapType 2 def\n" | - | ||||||||||||
215 | "1 begincodespacerange\n" | - | ||||||||||||
216 | "<0000> <FFFF>\n" | - | ||||||||||||
217 | "endcodespacerange\n"; | - | ||||||||||||
218 | - | |||||||||||||
219 | int nranges = 1; | - | ||||||||||||
220 | QByteArray ranges = "<0000> <0000> <0000>\n"; | - | ||||||||||||
221 | QPdf::ByteStream s(&ranges); | - | ||||||||||||
222 | - | |||||||||||||
223 | char buf[5]; | - | ||||||||||||
224 | for (int g = 1; g < nGlyphs(); ) {
| 0 | ||||||||||||
225 | int uc0 = reverseMap.at(g); | - | ||||||||||||
226 | if (!uc0) {
| 0 | ||||||||||||
227 | ++g; | - | ||||||||||||
228 | continue; never executed: continue; | 0 | ||||||||||||
229 | } | - | ||||||||||||
230 | int start = g; | - | ||||||||||||
231 | int startLinear = 0; | - | ||||||||||||
232 | ++g; | - | ||||||||||||
233 | while (g < nGlyphs()) {
| 0 | ||||||||||||
234 | int uc = reverseMap[g]; | - | ||||||||||||
235 | // cmaps can't have the high byte changing within one range, so we need to break on that as well | - | ||||||||||||
236 | if (!uc || (g>>8) != (start >> 8))
| 0 | ||||||||||||
237 | break; never executed: break; | 0 | ||||||||||||
238 | if (uc == uc0 + 1) {
| 0 | ||||||||||||
239 | if (!startLinear)
| 0 | ||||||||||||
240 | startLinear = g - 1; never executed: startLinear = g - 1; | 0 | ||||||||||||
241 | } else { never executed: end of block | 0 | ||||||||||||
242 | if (startLinear > 0 && g - startLinear >= 10)
| 0 | ||||||||||||
243 | break; never executed: break; | 0 | ||||||||||||
244 | startLinear = 0; | - | ||||||||||||
245 | } never executed: end of block | 0 | ||||||||||||
246 | uc0 = uc; | - | ||||||||||||
247 | ++g; | - | ||||||||||||
248 | } never executed: end of block | 0 | ||||||||||||
249 | // qDebug("start=%x startLinear=%x g-1=%x",start,startLinear,g-1); | - | ||||||||||||
250 | if (g - startLinear < 10)
| 0 | ||||||||||||
251 | startLinear = 0; never executed: startLinear = 0; | 0 | ||||||||||||
252 | int endnonlinear = startLinear ? startLinear : g;
| 0 | ||||||||||||
253 | // qDebug(" startLinear=%x endnonlinear=%x", startLinear,endnonlinear); | - | ||||||||||||
254 | if (endnonlinear > start) {
| 0 | ||||||||||||
255 | s << '<' << QPdf::toHex((ushort)start, buf) << "> <"; | - | ||||||||||||
256 | s << QPdf::toHex((ushort)(endnonlinear - 1), buf) << "> "; | - | ||||||||||||
257 | if (endnonlinear == start + 1) {
| 0 | ||||||||||||
258 | s << '<' << QPdf::toHex((ushort)reverseMap[start], buf) << ">\n"; | - | ||||||||||||
259 | } else { never executed: end of block | 0 | ||||||||||||
260 | s << '['; | - | ||||||||||||
261 | for (int i = start; i < endnonlinear; ++i) {
| 0 | ||||||||||||
262 | s << '<' << QPdf::toHex((ushort)reverseMap[i], buf) << "> "; | - | ||||||||||||
263 | } never executed: end of block | 0 | ||||||||||||
264 | s << "]\n"; | - | ||||||||||||
265 | } never executed: end of block | 0 | ||||||||||||
266 | checkRanges(ts, ranges, nranges); | - | ||||||||||||
267 | } never executed: end of block | 0 | ||||||||||||
268 | if (startLinear) {
| 0 | ||||||||||||
269 | while (startLinear < g) {
| 0 | ||||||||||||
270 | int len = g - startLinear; | - | ||||||||||||
271 | int uc_start = reverseMap[startLinear]; | - | ||||||||||||
272 | int uc_end = uc_start + len - 1; | - | ||||||||||||
273 | if ((uc_end >> 8) != (uc_start >> 8))
| 0 | ||||||||||||
274 | len = 256 - (uc_start & 0xff); never executed: len = 256 - (uc_start & 0xff); | 0 | ||||||||||||
275 | s << '<' << QPdf::toHex((ushort)startLinear, buf) << "> <"; | - | ||||||||||||
276 | s << QPdf::toHex((ushort)(startLinear + len - 1), buf) << "> "; | - | ||||||||||||
277 | s << '<' << QPdf::toHex((ushort)reverseMap[startLinear], buf) << ">\n"; | - | ||||||||||||
278 | checkRanges(ts, ranges, nranges); | - | ||||||||||||
279 | startLinear += len; | - | ||||||||||||
280 | } never executed: end of block | 0 | ||||||||||||
281 | } never executed: end of block | 0 | ||||||||||||
282 | } never executed: end of block | 0 | ||||||||||||
283 | if (nranges) {
| 0 | ||||||||||||
284 | ts << nranges << "beginbfrange\n" | - | ||||||||||||
285 | << ranges << "endbfrange\n"; | - | ||||||||||||
286 | } never executed: end of block | 0 | ||||||||||||
287 | ts << "endcmap\n" | - | ||||||||||||
288 | "CMapName currentdict /CMap defineresource pop\n" | - | ||||||||||||
289 | "end\n" | - | ||||||||||||
290 | "end\n"; | - | ||||||||||||
291 | - | |||||||||||||
292 | return touc; never executed: return touc; | 0 | ||||||||||||
293 | } | - | ||||||||||||
294 | - | |||||||||||||
295 | int QFontSubset::addGlyph(int index) | - | ||||||||||||
296 | { | - | ||||||||||||
297 | int idx = glyph_indices.indexOf(index); | - | ||||||||||||
298 | if (idx < 0) {
| 0 | ||||||||||||
299 | idx = glyph_indices.size(); | - | ||||||||||||
300 | glyph_indices.append(index); | - | ||||||||||||
301 | } never executed: end of block | 0 | ||||||||||||
302 | return idx; never executed: return idx; | 0 | ||||||||||||
303 | } | - | ||||||||||||
304 | - | |||||||||||||
305 | #endif // QT_NO_PDF | - | ||||||||||||
306 | - | |||||||||||||
307 | // ------------------------------ Truetype generation ---------------------------------------------- | - | ||||||||||||
308 | - | |||||||||||||
309 | typedef qint16 F2DOT14; | - | ||||||||||||
310 | typedef quint32 Tag; | - | ||||||||||||
311 | typedef quint16 GlyphID; | - | ||||||||||||
312 | typedef quint16 Offset; | - | ||||||||||||
313 | - | |||||||||||||
314 | - | |||||||||||||
315 | class QTtfStream { | - | ||||||||||||
316 | public: | - | ||||||||||||
317 | QTtfStream(QByteArray &ba) : data((uchar *)ba.data()) { start = data; } never executed: end of block | 0 | ||||||||||||
318 | QTtfStream &operator <<(quint8 v) { *data = v; ++data; return *this; } never executed: return *this; | 0 | ||||||||||||
319 | QTtfStream &operator <<(quint16 v) { qToBigEndian(v, data); data += sizeof(v); return *this; } never executed: return *this; | 0 | ||||||||||||
320 | QTtfStream &operator <<(quint32 v) { qToBigEndian(v, data); data += sizeof(v); return *this; } never executed: return *this; | 0 | ||||||||||||
321 | QTtfStream &operator <<(qint8 v) { *data = quint8(v); ++data; return *this; } never executed: return *this; | 0 | ||||||||||||
322 | QTtfStream &operator <<(qint16 v) { qToBigEndian(v, data); data += sizeof(v); return *this; } never executed: return *this; | 0 | ||||||||||||
323 | QTtfStream &operator <<(qint32 v) { qToBigEndian(v, data); data += sizeof(v); return *this; } never executed: return *this; | 0 | ||||||||||||
324 | QTtfStream &operator <<(qint64 v) { qToBigEndian(v, data); data += sizeof(v); return *this; } never executed: return *this; | 0 | ||||||||||||
325 | - | |||||||||||||
326 | int offset() const { return data - start; } never executed: return data - start; | 0 | ||||||||||||
327 | void setOffset(int o) { data = start + o; } never executed: end of block | 0 | ||||||||||||
328 | void align4() { while (offset() & 3) { *data = '\0'; ++data; } } never executed: end of block never executed: end of block
| 0 | ||||||||||||
329 | private: | - | ||||||||||||
330 | uchar *data; | - | ||||||||||||
331 | uchar *start; | - | ||||||||||||
332 | }; | - | ||||||||||||
333 | - | |||||||||||||
334 | struct QTtfTable { | - | ||||||||||||
335 | Tag tag; | - | ||||||||||||
336 | QByteArray data; | - | ||||||||||||
337 | }; | - | ||||||||||||
338 | Q_DECLARE_TYPEINFO(QTtfTable, Q_MOVABLE_TYPE); | - | ||||||||||||
339 | - | |||||||||||||
340 | - | |||||||||||||
341 | struct qttf_head_table { | - | ||||||||||||
342 | qint32 font_revision; | - | ||||||||||||
343 | quint16 flags; | - | ||||||||||||
344 | qint64 created; | - | ||||||||||||
345 | qint64 modified; | - | ||||||||||||
346 | qint16 xMin; | - | ||||||||||||
347 | qint16 yMin; | - | ||||||||||||
348 | qint16 xMax; | - | ||||||||||||
349 | qint16 yMax; | - | ||||||||||||
350 | quint16 macStyle; | - | ||||||||||||
351 | qint16 indexToLocFormat; | - | ||||||||||||
352 | }; | - | ||||||||||||
353 | Q_DECLARE_TYPEINFO(qttf_head_table, Q_PRIMITIVE_TYPE); | - | ||||||||||||
354 | - | |||||||||||||
355 | - | |||||||||||||
356 | struct qttf_hhea_table { | - | ||||||||||||
357 | qint16 ascender; | - | ||||||||||||
358 | qint16 descender; | - | ||||||||||||
359 | qint16 lineGap; | - | ||||||||||||
360 | quint16 maxAdvanceWidth; | - | ||||||||||||
361 | qint16 minLeftSideBearing; | - | ||||||||||||
362 | qint16 minRightSideBearing; | - | ||||||||||||
363 | qint16 xMaxExtent; | - | ||||||||||||
364 | quint16 numberOfHMetrics; | - | ||||||||||||
365 | }; | - | ||||||||||||
366 | Q_DECLARE_TYPEINFO(qttf_hhea_table, Q_PRIMITIVE_TYPE); | - | ||||||||||||
367 | - | |||||||||||||
368 | - | |||||||||||||
369 | struct qttf_maxp_table { | - | ||||||||||||
370 | quint16 numGlyphs; | - | ||||||||||||
371 | quint16 maxPoints; | - | ||||||||||||
372 | quint16 maxContours; | - | ||||||||||||
373 | quint16 maxCompositePoints; | - | ||||||||||||
374 | quint16 maxCompositeContours; | - | ||||||||||||
375 | quint16 maxComponentElements; | - | ||||||||||||
376 | quint16 maxComponentDepth; | - | ||||||||||||
377 | }; | - | ||||||||||||
378 | Q_DECLARE_TYPEINFO(qttf_maxp_table, Q_PRIMITIVE_TYPE); | - | ||||||||||||
379 | - | |||||||||||||
380 | struct qttf_name_table { | - | ||||||||||||
381 | QString copyright; | - | ||||||||||||
382 | QString family; | - | ||||||||||||
383 | QString subfamily; | - | ||||||||||||
384 | QString postscript_name; | - | ||||||||||||
385 | }; | - | ||||||||||||
386 | Q_DECLARE_TYPEINFO(qttf_name_table, Q_MOVABLE_TYPE); | - | ||||||||||||
387 | - | |||||||||||||
388 | - | |||||||||||||
389 | static QTtfTable generateHead(const qttf_head_table &head); | - | ||||||||||||
390 | static QTtfTable generateHhea(const qttf_hhea_table &hhea); | - | ||||||||||||
391 | static QTtfTable generateMaxp(const qttf_maxp_table &maxp); | - | ||||||||||||
392 | static QTtfTable generateName(const qttf_name_table &name); | - | ||||||||||||
393 | - | |||||||||||||
394 | struct qttf_font_tables | - | ||||||||||||
395 | { | - | ||||||||||||
396 | qttf_head_table head; | - | ||||||||||||
397 | qttf_hhea_table hhea; | - | ||||||||||||
398 | qttf_maxp_table maxp; | - | ||||||||||||
399 | }; | - | ||||||||||||
400 | - | |||||||||||||
401 | - | |||||||||||||
402 | struct QTtfGlyph { | - | ||||||||||||
403 | quint16 index; | - | ||||||||||||
404 | qint16 xMin; | - | ||||||||||||
405 | qint16 xMax; | - | ||||||||||||
406 | qint16 yMin; | - | ||||||||||||
407 | qint16 yMax; | - | ||||||||||||
408 | quint16 advanceWidth; | - | ||||||||||||
409 | qint16 lsb; | - | ||||||||||||
410 | quint16 numContours; | - | ||||||||||||
411 | quint16 numPoints; | - | ||||||||||||
412 | QByteArray data; | - | ||||||||||||
413 | }; | - | ||||||||||||
414 | Q_DECLARE_TYPEINFO(QTtfGlyph, Q_MOVABLE_TYPE); | - | ||||||||||||
415 | - | |||||||||||||
416 | static QTtfGlyph generateGlyph(int index, const QPainterPath &path, qreal advance, qreal lsb, qreal ppem); | - | ||||||||||||
417 | // generates glyf, loca and hmtx | - | ||||||||||||
418 | static QVector<QTtfTable> generateGlyphTables(qttf_font_tables &tables, const QVector<QTtfGlyph> &_glyphs); | - | ||||||||||||
419 | - | |||||||||||||
420 | static QByteArray bindFont(const QVector<QTtfTable>& _tables); | - | ||||||||||||
421 | - | |||||||||||||
422 | - | |||||||||||||
423 | static quint32 checksum(const QByteArray &table) | - | ||||||||||||
424 | { | - | ||||||||||||
425 | quint32 sum = 0; | - | ||||||||||||
426 | int offset = 0; | - | ||||||||||||
427 | const uchar *d = (const uchar *)table.constData(); | - | ||||||||||||
428 | while (offset <= table.size()-3) {
| 0 | ||||||||||||
429 | sum += qFromBigEndian<quint32>(d + offset); | - | ||||||||||||
430 | offset += 4; | - | ||||||||||||
431 | } never executed: end of block | 0 | ||||||||||||
432 | int shift = 24; | - | ||||||||||||
433 | quint32 x = 0; | - | ||||||||||||
434 | while (offset < table.size()) {
| 0 | ||||||||||||
435 | x |= ((quint32)d[offset]) << shift; | - | ||||||||||||
436 | ++offset; | - | ||||||||||||
437 | shift -= 8; | - | ||||||||||||
438 | } never executed: end of block | 0 | ||||||||||||
439 | sum += x; | - | ||||||||||||
440 | - | |||||||||||||
441 | return sum; never executed: return sum; | 0 | ||||||||||||
442 | } | - | ||||||||||||
443 | - | |||||||||||||
444 | static QTtfTable generateHead(const qttf_head_table &head) | - | ||||||||||||
445 | { | - | ||||||||||||
446 | const int head_size = 54; | - | ||||||||||||
447 | QTtfTable t; | - | ||||||||||||
448 | t.tag = MAKE_TAG('h', 'e', 'a', 'd'); | - | ||||||||||||
449 | t.data.resize(head_size); | - | ||||||||||||
450 | - | |||||||||||||
451 | QTtfStream s(t.data); | - | ||||||||||||
452 | - | |||||||||||||
453 | // qint32 Table version number 0x00010000 for version 1.0. | - | ||||||||||||
454 | // qint32 fontRevision Set by font manufacturer. | - | ||||||||||||
455 | s << qint32(0x00010000) | - | ||||||||||||
456 | << head.font_revision | - | ||||||||||||
457 | // quint32 checkSumAdjustment To compute: set it to 0, sum the entire font as quint32, then store 0xB1B0AFBA - sum. | - | ||||||||||||
458 | << quint32(0) | - | ||||||||||||
459 | // quint32 magicNumber Set to 0x5F0F3CF5. | - | ||||||||||||
460 | << quint32(0x5F0F3CF5) | - | ||||||||||||
461 | // quint16 flags Bit 0: Baseline for font at y=0; | - | ||||||||||||
462 | // Bit 1: Left sidebearing point at x=0; | - | ||||||||||||
463 | // Bit 2: Instructions may depend on point size; | - | ||||||||||||
464 | // Bit 3: Force ppem to integer values for all internal scaler math; may use fractional ppem sizes if this bit is clear; | - | ||||||||||||
465 | // Bit 4: Instructions may alter advance width (the advance widths might not scale linearly); | - | ||||||||||||
466 | // Bits 5-10: These should be set according to Apple's specification . However, they are not implemented in OpenType. | - | ||||||||||||
467 | // Bit 11: Font data is 'lossless,' as a result of having been compressed and decompressed with the Agfa MicroType Express engine. | - | ||||||||||||
468 | // Bit 12: Font converted (produce compatible metrics) | - | ||||||||||||
469 | // Bit 13: Font optimized for ClearType | - | ||||||||||||
470 | // Bit 14: Reserved, set to 0 | - | ||||||||||||
471 | // Bit 15: Reserved, set to 0 | - | ||||||||||||
472 | << quint16(0) | - | ||||||||||||
473 | - | |||||||||||||
474 | // quint16 unitsPerEm Valid range is from 16 to 16384. This value should be a power of 2 for fonts that have TrueType outlines. | - | ||||||||||||
475 | << quint16(2048) | - | ||||||||||||
476 | // qint64 created Number of seconds since 12:00 midnight, January 1, 1904. 64-bit integer | - | ||||||||||||
477 | << head.created | - | ||||||||||||
478 | // qint64 modified Number of seconds since 12:00 midnight, January 1, 1904. 64-bit integer | - | ||||||||||||
479 | << head.modified | - | ||||||||||||
480 | // qint16 xMin For all glyph bounding boxes. | - | ||||||||||||
481 | // qint16 yMin For all glyph bounding boxes. | - | ||||||||||||
482 | // qint16 xMax For all glyph bounding boxes. | - | ||||||||||||
483 | // qint16 yMax For all glyph bounding boxes. | - | ||||||||||||
484 | << head.xMin | - | ||||||||||||
485 | << head.yMin | - | ||||||||||||
486 | << head.xMax | - | ||||||||||||
487 | << head.yMax | - | ||||||||||||
488 | // quint16 macStyle Bit 0: Bold (if set to 1); | - | ||||||||||||
489 | // Bit 1: Italic (if set to 1) | - | ||||||||||||
490 | // Bit 2: Underline (if set to 1) | - | ||||||||||||
491 | // Bit 3: Outline (if set to 1) | - | ||||||||||||
492 | // Bit 4: Shadow (if set to 1) | - | ||||||||||||
493 | // Bit 5: Condensed (if set to 1) | - | ||||||||||||
494 | // Bit 6: Extended (if set to 1) | - | ||||||||||||
495 | // Bits 7-15: Reserved (set to 0). | - | ||||||||||||
496 | << head.macStyle | - | ||||||||||||
497 | // quint16 lowestRecPPEM Smallest readable size in pixels. | - | ||||||||||||
498 | << quint16(6) // just a wild guess | - | ||||||||||||
499 | // qint16 fontDirectionHint 0: Fully mixed directional glyphs; | - | ||||||||||||
500 | << qint16(0) | - | ||||||||||||
501 | // 1: Only strongly left to right; | - | ||||||||||||
502 | // 2: Like 1 but also contains neutrals; | - | ||||||||||||
503 | // -1: Only strongly right to left; | - | ||||||||||||
504 | // -2: Like -1 but also contains neutrals. 1 | - | ||||||||||||
505 | // qint16 indexToLocFormat 0 for short offsets, 1 for long. | - | ||||||||||||
506 | << head.indexToLocFormat | - | ||||||||||||
507 | // qint16 glyphDataFormat 0 for current format. | - | ||||||||||||
508 | << qint16(0); | - | ||||||||||||
509 | - | |||||||||||||
510 | Q_ASSERT(s.offset() == head_size); | - | ||||||||||||
511 | return t; never executed: return t; | 0 | ||||||||||||
512 | } | - | ||||||||||||
513 | - | |||||||||||||
514 | - | |||||||||||||
515 | static QTtfTable generateHhea(const qttf_hhea_table &hhea) | - | ||||||||||||
516 | { | - | ||||||||||||
517 | const int hhea_size = 36; | - | ||||||||||||
518 | QTtfTable t; | - | ||||||||||||
519 | t.tag = MAKE_TAG('h', 'h', 'e', 'a'); | - | ||||||||||||
520 | t.data.resize(hhea_size); | - | ||||||||||||
521 | - | |||||||||||||
522 | QTtfStream s(t.data); | - | ||||||||||||
523 | // qint32 Table version number 0x00010000 for version 1.0. | - | ||||||||||||
524 | s << qint32(0x00010000) | - | ||||||||||||
525 | // qint16 Ascender Typographic ascent. (Distance from baseline of highest ascender) | - | ||||||||||||
526 | << hhea.ascender | - | ||||||||||||
527 | // qint16 Descender Typographic descent. (Distance from baseline of lowest descender) | - | ||||||||||||
528 | << hhea.descender | - | ||||||||||||
529 | // qint16 LineGap Typographic line gap. | - | ||||||||||||
530 | // Negative LineGap values are treated as zero | - | ||||||||||||
531 | // in Windows 3.1, System 6, and | - | ||||||||||||
532 | // System 7. | - | ||||||||||||
533 | << hhea.lineGap | - | ||||||||||||
534 | // quint16 advanceWidthMax Maximum advance width value in 'hmtx' table. | - | ||||||||||||
535 | << hhea.maxAdvanceWidth | - | ||||||||||||
536 | // qint16 minLeftSideBearing Minimum left sidebearing value in 'hmtx' table. | - | ||||||||||||
537 | << hhea.minLeftSideBearing | - | ||||||||||||
538 | // qint16 minRightSideBearing Minimum right sidebearing value; calculated as Min(aw - lsb - (xMax - xMin)). | - | ||||||||||||
539 | << hhea.minRightSideBearing | - | ||||||||||||
540 | // qint16 xMaxExtent Max(lsb + (xMax - xMin)). | - | ||||||||||||
541 | << hhea.xMaxExtent | - | ||||||||||||
542 | // qint16 caretSlopeRise Used to calculate the slope of the cursor (rise/run); 1 for vertical. | - | ||||||||||||
543 | << qint16(1) | - | ||||||||||||
544 | // qint16 caretSlopeRun 0 for vertical. | - | ||||||||||||
545 | << qint16(0) | - | ||||||||||||
546 | // qint16 caretOffset The amount by which a slanted highlight on a glyph needs to be shifted to produce the best appearance. Set to 0 for non-slanted fonts | - | ||||||||||||
547 | << qint16(0) | - | ||||||||||||
548 | // qint16 (reserved) set to 0 | - | ||||||||||||
549 | << qint16(0) | - | ||||||||||||
550 | // qint16 (reserved) set to 0 | - | ||||||||||||
551 | << qint16(0) | - | ||||||||||||
552 | // qint16 (reserved) set to 0 | - | ||||||||||||
553 | << qint16(0) | - | ||||||||||||
554 | // qint16 (reserved) set to 0 | - | ||||||||||||
555 | << qint16(0) | - | ||||||||||||
556 | // qint16 metricDataFormat 0 for current format. | - | ||||||||||||
557 | << qint16(0) | - | ||||||||||||
558 | // quint16 numberOfHMetrics Number of hMetric entries in 'hmtx' table | - | ||||||||||||
559 | << hhea.numberOfHMetrics; | - | ||||||||||||
560 | - | |||||||||||||
561 | Q_ASSERT(s.offset() == hhea_size); | - | ||||||||||||
562 | return t; never executed: return t; | 0 | ||||||||||||
563 | } | - | ||||||||||||
564 | - | |||||||||||||
565 | - | |||||||||||||
566 | static QTtfTable generateMaxp(const qttf_maxp_table &maxp) | - | ||||||||||||
567 | { | - | ||||||||||||
568 | const int maxp_size = 32; | - | ||||||||||||
569 | QTtfTable t; | - | ||||||||||||
570 | t.tag = MAKE_TAG('m', 'a', 'x', 'p'); | - | ||||||||||||
571 | t.data.resize(maxp_size); | - | ||||||||||||
572 | - | |||||||||||||
573 | QTtfStream s(t.data); | - | ||||||||||||
574 | - | |||||||||||||
575 | // qint32 Table version number 0x00010000 for version 1.0. | - | ||||||||||||
576 | s << qint32(0x00010000) | - | ||||||||||||
577 | // quint16 numGlyphs The number of glyphs in the font. | - | ||||||||||||
578 | << maxp.numGlyphs | - | ||||||||||||
579 | // quint16 maxPoints Maximum points in a non-composite glyph. | - | ||||||||||||
580 | << maxp.maxPoints | - | ||||||||||||
581 | // quint16 maxContours Maximum contours in a non-composite glyph. | - | ||||||||||||
582 | << maxp.maxContours | - | ||||||||||||
583 | // quint16 maxCompositePoints Maximum points in a composite glyph. | - | ||||||||||||
584 | << maxp.maxCompositePoints | - | ||||||||||||
585 | // quint16 maxCompositeContours Maximum contours in a composite glyph. | - | ||||||||||||
586 | << maxp.maxCompositeContours | - | ||||||||||||
587 | // quint16 maxZones 1 if instructions do not use the twilight zone (Z0), or 2 if instructions do use Z0; should be set to 2 in most cases. | - | ||||||||||||
588 | << quint16(1) // we do not embed instructions | - | ||||||||||||
589 | // quint16 maxTwilightPoints Maximum points used in Z0. | - | ||||||||||||
590 | << quint16(0) | - | ||||||||||||
591 | // quint16 maxStorage Number of Storage Area locations. | - | ||||||||||||
592 | << quint16(0) | - | ||||||||||||
593 | // quint16 maxFunctionDefs Number of FDEFs. | - | ||||||||||||
594 | << quint16(0) | - | ||||||||||||
595 | // quint16 maxInstructionDefs Number of IDEFs. | - | ||||||||||||
596 | << quint16(0) | - | ||||||||||||
597 | // quint16 maxStackElements Maximum stack depth2. | - | ||||||||||||
598 | << quint16(0) | - | ||||||||||||
599 | // quint16 maxSizeOfInstructions Maximum byte count for glyph instructions. | - | ||||||||||||
600 | << quint16(0) | - | ||||||||||||
601 | // quint16 maxComponentElements Maximum number of components referenced at "top level" for any composite glyph. | - | ||||||||||||
602 | << maxp.maxComponentElements | - | ||||||||||||
603 | // quint16 maxComponentDepth Maximum levels of recursion; 1 for simple components. | - | ||||||||||||
604 | << maxp.maxComponentDepth; | - | ||||||||||||
605 | - | |||||||||||||
606 | Q_ASSERT(s.offset() == maxp_size); | - | ||||||||||||
607 | return t; never executed: return t; | 0 | ||||||||||||
608 | } | - | ||||||||||||
609 | - | |||||||||||||
610 | struct QTtfNameRecord { | - | ||||||||||||
611 | quint16 nameId; | - | ||||||||||||
612 | QString value; | - | ||||||||||||
613 | }; | - | ||||||||||||
614 | Q_DECLARE_TYPEINFO(QTtfNameRecord, Q_MOVABLE_TYPE); | - | ||||||||||||
615 | - | |||||||||||||
616 | static QTtfTable generateName(const QVector<QTtfNameRecord> &name); | - | ||||||||||||
617 | - | |||||||||||||
618 | static QTtfTable generateName(const qttf_name_table &name) | - | ||||||||||||
619 | { | - | ||||||||||||
620 | QVector<QTtfNameRecord> list; | - | ||||||||||||
621 | QTtfNameRecord rec; | - | ||||||||||||
622 | rec.nameId = 0; | - | ||||||||||||
623 | rec.value = name.copyright; | - | ||||||||||||
624 | list.append(rec); | - | ||||||||||||
625 | rec.nameId = 1; | - | ||||||||||||
626 | rec.value = name.family; | - | ||||||||||||
627 | list.append(rec); | - | ||||||||||||
628 | rec.nameId = 2; | - | ||||||||||||
629 | rec.value = name.subfamily; | - | ||||||||||||
630 | list.append(rec); | - | ||||||||||||
631 | rec.nameId = 4; | - | ||||||||||||
632 | rec.value = name.family; | - | ||||||||||||
633 | if (name.subfamily != QLatin1String("Regular"))
| 0 | ||||||||||||
634 | rec.value += QLatin1Char(' ') + name.subfamily; never executed: rec.value += QLatin1Char(' ') + name.subfamily; | 0 | ||||||||||||
635 | list.append(rec); | - | ||||||||||||
636 | rec.nameId = 6; | - | ||||||||||||
637 | rec.value = name.postscript_name; | - | ||||||||||||
638 | list.append(rec); | - | ||||||||||||
639 | - | |||||||||||||
640 | return generateName(list); never executed: return generateName(list); | 0 | ||||||||||||
641 | } | - | ||||||||||||
642 | - | |||||||||||||
643 | // ####### should probably generate Macintosh/Roman name entries as well | - | ||||||||||||
644 | static QTtfTable generateName(const QVector<QTtfNameRecord> &name) | - | ||||||||||||
645 | { | - | ||||||||||||
646 | const int char_size = 2; | - | ||||||||||||
647 | - | |||||||||||||
648 | QTtfTable t; | - | ||||||||||||
649 | t.tag = MAKE_TAG('n', 'a', 'm', 'e'); | - | ||||||||||||
650 | - | |||||||||||||
651 | const int name_size = 6 + 12*name.size(); | - | ||||||||||||
652 | int string_size = 0; | - | ||||||||||||
653 | for (int i = 0; i < name.size(); ++i) {
| 0 | ||||||||||||
654 | string_size += name.at(i).value.length()*char_size; | - | ||||||||||||
655 | } never executed: end of block | 0 | ||||||||||||
656 | t.data.resize(name_size + string_size); | - | ||||||||||||
657 | - | |||||||||||||
658 | QTtfStream s(t.data); | - | ||||||||||||
659 | // quint16 format Format selector (=0). | - | ||||||||||||
660 | s << quint16(0) | - | ||||||||||||
661 | // quint16 count Number of name records. | - | ||||||||||||
662 | << quint16(name.size()) | - | ||||||||||||
663 | // quint16 stringOffset Offset to start of string storage (from start of table). | - | ||||||||||||
664 | << quint16(name_size); | - | ||||||||||||
665 | // NameRecord nameRecord[count] The name records where count is the number of records. | - | ||||||||||||
666 | // (Variable) | - | ||||||||||||
667 | - | |||||||||||||
668 | int off = 0; | - | ||||||||||||
669 | for (int i = 0; i < name.size(); ++i) {
| 0 | ||||||||||||
670 | int len = name.at(i).value.length()*char_size; | - | ||||||||||||
671 | // quint16 platformID Platform ID. | - | ||||||||||||
672 | // quint16 encodingID Platform-specific encoding ID. | - | ||||||||||||
673 | // quint16 languageID Language ID. | - | ||||||||||||
674 | s << quint16(3) | - | ||||||||||||
675 | << quint16(1) | - | ||||||||||||
676 | << quint16(0x0409) // en_US | - | ||||||||||||
677 | // quint16 nameId Name ID. | - | ||||||||||||
678 | << name.at(i).nameId | - | ||||||||||||
679 | // quint16 length String length (in bytes). | - | ||||||||||||
680 | << quint16(len) | - | ||||||||||||
681 | // quint16 offset String offset from start of storage area (in bytes). | - | ||||||||||||
682 | << quint16(off); | - | ||||||||||||
683 | off += len; | - | ||||||||||||
684 | } never executed: end of block | 0 | ||||||||||||
685 | for (int i = 0; i < name.size(); ++i) {
| 0 | ||||||||||||
686 | const QString &n = name.at(i).value; | - | ||||||||||||
687 | const ushort *uc = n.utf16(); | - | ||||||||||||
688 | for (int i = 0; i < n.length(); ++i) {
| 0 | ||||||||||||
689 | s << quint16(*uc); | - | ||||||||||||
690 | ++uc; | - | ||||||||||||
691 | } never executed: end of block | 0 | ||||||||||||
692 | } never executed: end of block | 0 | ||||||||||||
693 | return t; never executed: return t; | 0 | ||||||||||||
694 | } | - | ||||||||||||
695 | - | |||||||||||||
696 | - | |||||||||||||
697 | enum Flags { | - | ||||||||||||
698 | OffCurve = 0, | - | ||||||||||||
699 | OnCurve = (1 << 0), | - | ||||||||||||
700 | XShortVector = (1 << 1), | - | ||||||||||||
701 | YShortVector = (1 << 2), | - | ||||||||||||
702 | Repeat = (1 << 3), | - | ||||||||||||
703 | XSame = (1 << 4), | - | ||||||||||||
704 | XShortPositive = (1 << 4), | - | ||||||||||||
705 | YSame = (1 << 5), | - | ||||||||||||
706 | YShortPositive = (1 << 5) | - | ||||||||||||
707 | }; | - | ||||||||||||
708 | struct TTF_POINT { | - | ||||||||||||
709 | qint16 x; | - | ||||||||||||
710 | qint16 y; | - | ||||||||||||
711 | quint8 flags; | - | ||||||||||||
712 | }; | - | ||||||||||||
713 | Q_DECLARE_TYPEINFO(TTF_POINT, Q_PRIMITIVE_TYPE); | - | ||||||||||||
714 | - | |||||||||||||
715 | static void convertPath(const QPainterPath &path, QVector<TTF_POINT> *points, QVector<int> *endPoints, qreal ppem) | - | ||||||||||||
716 | { | - | ||||||||||||
717 | int numElements = path.elementCount(); | - | ||||||||||||
718 | for (int i = 0; i < numElements - 1; ++i) {
| 0 | ||||||||||||
719 | const QPainterPath::Element &e = path.elementAt(i); | - | ||||||||||||
720 | TTF_POINT p; | - | ||||||||||||
721 | p.x = qRound(e.x * 2048. / ppem); | - | ||||||||||||
722 | p.y = qRound(-e.y * 2048. / ppem); | - | ||||||||||||
723 | p.flags = 0; | - | ||||||||||||
724 | - | |||||||||||||
725 | switch(e.type) { | - | ||||||||||||
726 | case QPainterPath::MoveToElement: never executed: case QPainterPath::MoveToElement: | 0 | ||||||||||||
727 | if (i != 0) {
| 0 | ||||||||||||
728 | // see if start and end points of the last contour agree | - | ||||||||||||
729 | int start = endPoints->size() ? endPoints->at(endPoints->size()-1) - 1 : 0;
| 0 | ||||||||||||
730 | int end = points->size() - 1; | - | ||||||||||||
731 | if (points->at(end).x == points->at(start).x
| 0 | ||||||||||||
732 | && points->at(end).y == points->at(start).y)
| 0 | ||||||||||||
733 | points->takeLast(); never executed: points->takeLast(); | 0 | ||||||||||||
734 | endPoints->append(points->size() - 1); | - | ||||||||||||
735 | } never executed: end of block | 0 | ||||||||||||
736 | // fall through | - | ||||||||||||
737 | case QPainterPath::LineToElement: code before this statement never executed: case QPainterPath::LineToElement: never executed: case QPainterPath::LineToElement: | 0 | ||||||||||||
738 | p.flags = OnCurve; | - | ||||||||||||
739 | break; never executed: break; | 0 | ||||||||||||
740 | case QPainterPath::CurveToElement: { never executed: case QPainterPath::CurveToElement: | 0 | ||||||||||||
741 | // cubic bezier curve, we need to reduce to a list of quadratic curves | - | ||||||||||||
742 | TTF_POINT list[3*16 + 4]; // we need max 16 subdivisions | - | ||||||||||||
743 | list[3] = points->at(points->size() - 1); | - | ||||||||||||
744 | list[2] = p; | - | ||||||||||||
745 | const QPainterPath::Element &e2 = path.elementAt(++i); | - | ||||||||||||
746 | list[1].x = qRound(e2.x * 2048. / ppem); | - | ||||||||||||
747 | list[1].y = qRound(-e2.y * 2048. / ppem); | - | ||||||||||||
748 | const QPainterPath::Element &e3 = path.elementAt(++i); | - | ||||||||||||
749 | list[0].x = qRound(e3.x * 2048. / ppem); | - | ||||||||||||
750 | list[0].y = qRound(-e3.y * 2048. / ppem); | - | ||||||||||||
751 | - | |||||||||||||
752 | TTF_POINT *base = list; | - | ||||||||||||
753 | - | |||||||||||||
754 | bool try_reduce = points->size() > 1
| 0 | ||||||||||||
755 | && points->at(points->size() - 1).flags == OnCurve
| 0 | ||||||||||||
756 | && points->at(points->size() - 2).flags == OffCurve;
| 0 | ||||||||||||
757 | // qDebug("generating beziers:"); | - | ||||||||||||
758 | while (base >= list) {
| 0 | ||||||||||||
759 | const int split_limit = 3; | - | ||||||||||||
760 | // { | - | ||||||||||||
761 | // qDebug("iteration:"); | - | ||||||||||||
762 | // TTF_POINT *x = list; | - | ||||||||||||
763 | // while (x <= base + 3) { | - | ||||||||||||
764 | // qDebug() << " " << QPoint(x->x, x->y); | - | ||||||||||||
765 | // ++x; | - | ||||||||||||
766 | // } | - | ||||||||||||
767 | // } | - | ||||||||||||
768 | Q_ASSERT(base - list < 3*16 + 1); | - | ||||||||||||
769 | // first see if we can easily reduce the cubic to a quadratic bezier curve | - | ||||||||||||
770 | int i1_x = base[1].x + ((base[1].x - base[0].x) >> 1); | - | ||||||||||||
771 | int i1_y = base[1].y + ((base[1].y - base[0].y) >> 1); | - | ||||||||||||
772 | int i2_x = base[2].x + ((base[2].x - base[3].x) >> 1); | - | ||||||||||||
773 | int i2_y = base[2].y + ((base[2].y - base[3].y) >> 1); | - | ||||||||||||
774 | // qDebug() << "checking: i1=" << QPoint(i1_x, i1_y) << " i2=" << QPoint(i2_x, i2_y); | - | ||||||||||||
775 | if (qAbs(i1_x - i2_x) <= split_limit && qAbs(i1_y - i2_y) <= split_limit) {
| 0 | ||||||||||||
776 | // got a quadratic bezier curve | - | ||||||||||||
777 | TTF_POINT np; | - | ||||||||||||
778 | np.x = (i1_x + i2_x) >> 1; | - | ||||||||||||
779 | np.y = (i1_y + i2_y) >> 1; | - | ||||||||||||
780 | if (try_reduce) {
| 0 | ||||||||||||
781 | // see if we can optimize out the last onCurve point | - | ||||||||||||
782 | int mx = (points->at(points->size() - 2).x + base[2].x) >> 1; | - | ||||||||||||
783 | int my = (points->at(points->size() - 2).y + base[2].y) >> 1; | - | ||||||||||||
784 | if (qAbs(mx - base[3].x) <= split_limit && qAbs(my = base[3].y) <= split_limit)
| 0 | ||||||||||||
785 | points->takeLast(); never executed: points->takeLast(); | 0 | ||||||||||||
786 | try_reduce = false; | - | ||||||||||||
787 | } never executed: end of block | 0 | ||||||||||||
788 | np.flags = OffCurve; | - | ||||||||||||
789 | points->append(np); | - | ||||||||||||
790 | // qDebug() << " appending offcurve point " << QPoint(np.x, np.y); | - | ||||||||||||
791 | base -= 3; | - | ||||||||||||
792 | } else { never executed: end of block | 0 | ||||||||||||
793 | // need to split | - | ||||||||||||
794 | // qDebug() << " -> splitting"; | - | ||||||||||||
795 | qint16 a, b, c, d; | - | ||||||||||||
796 | base[6].x = base[3].x; | - | ||||||||||||
797 | c = base[1].x; | - | ||||||||||||
798 | d = base[2].x; | - | ||||||||||||
799 | base[1].x = a = ( base[0].x + c ) >> 1; | - | ||||||||||||
800 | base[5].x = b = ( base[3].x + d ) >> 1; | - | ||||||||||||
801 | c = ( c + d ) >> 1; | - | ||||||||||||
802 | base[2].x = a = ( a + c ) >> 1; | - | ||||||||||||
803 | base[4].x = b = ( b + c ) >> 1; | - | ||||||||||||
804 | base[3].x = ( a + b ) >> 1; | - | ||||||||||||
805 | - | |||||||||||||
806 | base[6].y = base[3].y; | - | ||||||||||||
807 | c = base[1].y; | - | ||||||||||||
808 | d = base[2].y; | - | ||||||||||||
809 | base[1].y = a = ( base[0].y + c ) >> 1; | - | ||||||||||||
810 | base[5].y = b = ( base[3].y + d ) >> 1; | - | ||||||||||||
811 | c = ( c + d ) >> 1; | - | ||||||||||||
812 | base[2].y = a = ( a + c ) >> 1; | - | ||||||||||||
813 | base[4].y = b = ( b + c ) >> 1; | - | ||||||||||||
814 | base[3].y = ( a + b ) >> 1; | - | ||||||||||||
815 | base += 3; | - | ||||||||||||
816 | } never executed: end of block | 0 | ||||||||||||
817 | } | - | ||||||||||||
818 | p = list[0]; | - | ||||||||||||
819 | p.flags = OnCurve; | - | ||||||||||||
820 | break; never executed: break; | 0 | ||||||||||||
821 | } | - | ||||||||||||
822 | case QPainterPath::CurveToDataElement: never executed: case QPainterPath::CurveToDataElement: | 0 | ||||||||||||
823 | Q_ASSERT(false); | - | ||||||||||||
824 | break; never executed: break; | 0 | ||||||||||||
825 | } | - | ||||||||||||
826 | // qDebug() << " appending oncurve point " << QPoint(p.x, p.y); | - | ||||||||||||
827 | points->append(p); | - | ||||||||||||
828 | } never executed: end of block | 0 | ||||||||||||
829 | int start = endPoints->size() ? endPoints->at(endPoints->size()-1) + 1 : 0;
| 0 | ||||||||||||
830 | int end = points->size() - 1; | - | ||||||||||||
831 | if (points->at(end).x == points->at(start).x
| 0 | ||||||||||||
832 | && points->at(end).y == points->at(start).y)
| 0 | ||||||||||||
833 | points->takeLast(); never executed: points->takeLast(); | 0 | ||||||||||||
834 | endPoints->append(points->size() - 1); | - | ||||||||||||
835 | } never executed: end of block | 0 | ||||||||||||
836 | - | |||||||||||||
837 | static void getBounds(const QVector<TTF_POINT> &points, qint16 *xmin, qint16 *xmax, qint16 *ymin, qint16 *ymax) | - | ||||||||||||
838 | { | - | ||||||||||||
839 | *xmin = points.at(0).x; | - | ||||||||||||
840 | *xmax = *xmin; | - | ||||||||||||
841 | *ymin = points.at(0).y; | - | ||||||||||||
842 | *ymax = *ymin; | - | ||||||||||||
843 | - | |||||||||||||
844 | for (int i = 1; i < points.size(); ++i) {
| 0 | ||||||||||||
845 | *xmin = qMin(*xmin, points.at(i).x); | - | ||||||||||||
846 | *xmax = qMax(*xmax, points.at(i).x); | - | ||||||||||||
847 | *ymin = qMin(*ymin, points.at(i).y); | - | ||||||||||||
848 | *ymax = qMax(*ymax, points.at(i).y); | - | ||||||||||||
849 | } never executed: end of block | 0 | ||||||||||||
850 | } never executed: end of block | 0 | ||||||||||||
851 | - | |||||||||||||
852 | static int convertToRelative(QVector<TTF_POINT> *points) | - | ||||||||||||
853 | { | - | ||||||||||||
854 | // convert points to relative and setup flags | - | ||||||||||||
855 | // qDebug() << "relative points:"; | - | ||||||||||||
856 | qint16 prev_x = 0; | - | ||||||||||||
857 | qint16 prev_y = 0; | - | ||||||||||||
858 | int point_array_size = 0; | - | ||||||||||||
859 | for (int i = 0; i < points->size(); ++i) {
| 0 | ||||||||||||
860 | const int x = points->at(i).x; | - | ||||||||||||
861 | const int y = points->at(i).y; | - | ||||||||||||
862 | TTF_POINT rel; | - | ||||||||||||
863 | rel.x = x - prev_x; | - | ||||||||||||
864 | rel.y = y - prev_y; | - | ||||||||||||
865 | rel.flags = points->at(i).flags; | - | ||||||||||||
866 | Q_ASSERT(rel.flags < 2); | - | ||||||||||||
867 | if (!rel.x) {
| 0 | ||||||||||||
868 | rel.flags |= XSame; | - | ||||||||||||
869 | } else if (rel.x > 0 && rel.x < 256) { never executed: end of block
| 0 | ||||||||||||
870 | rel.flags |= XShortVector|XShortPositive; | - | ||||||||||||
871 | point_array_size++; | - | ||||||||||||
872 | } else if (rel.x < 0 && rel.x > -256) { never executed: end of block
| 0 | ||||||||||||
873 | rel.flags |= XShortVector; | - | ||||||||||||
874 | rel.x = -rel.x; | - | ||||||||||||
875 | point_array_size++; | - | ||||||||||||
876 | } else { never executed: end of block | 0 | ||||||||||||
877 | point_array_size += 2; | - | ||||||||||||
878 | } never executed: end of block | 0 | ||||||||||||
879 | if (!rel.y) {
| 0 | ||||||||||||
880 | rel.flags |= YSame; | - | ||||||||||||
881 | } else if (rel.y > 0 && rel.y < 256) { never executed: end of block
| 0 | ||||||||||||
882 | rel.flags |= YShortVector|YShortPositive; | - | ||||||||||||
883 | point_array_size++; | - | ||||||||||||
884 | } else if (rel.y < 0 && rel.y > -256) { never executed: end of block
| 0 | ||||||||||||
885 | rel.flags |= YShortVector; | - | ||||||||||||
886 | rel.y = -rel.y; | - | ||||||||||||
887 | point_array_size++; | - | ||||||||||||
888 | } else { never executed: end of block | 0 | ||||||||||||
889 | point_array_size += 2; | - | ||||||||||||
890 | } never executed: end of block | 0 | ||||||||||||
891 | (*points)[i] = rel; | - | ||||||||||||
892 | // #define toString(x) ((rel.flags & x) ? #x : "") | - | ||||||||||||
893 | // qDebug() << " " << QPoint(rel.x, rel.y) << "flags=" | - | ||||||||||||
894 | // << toString(OnCurve) << toString(XShortVector) | - | ||||||||||||
895 | // << (rel.flags & XShortVector ? toString(XShortPositive) : toString(XSame)) | - | ||||||||||||
896 | // << toString(YShortVector) | - | ||||||||||||
897 | // << (rel.flags & YShortVector ? toString(YShortPositive) : toString(YSame)); | - | ||||||||||||
898 | - | |||||||||||||
899 | prev_x = x; | - | ||||||||||||
900 | prev_y = y; | - | ||||||||||||
901 | } never executed: end of block | 0 | ||||||||||||
902 | return point_array_size; never executed: return point_array_size; | 0 | ||||||||||||
903 | } | - | ||||||||||||
904 | - | |||||||||||||
905 | static void getGlyphData(QTtfGlyph *glyph, const QVector<TTF_POINT> &points, const QVector<int> &endPoints, int point_array_size) | - | ||||||||||||
906 | { | - | ||||||||||||
907 | const int max_size = 5*sizeof(qint16) // header | - | ||||||||||||
908 | + endPoints.size()*sizeof(quint16) // end points of contours | - | ||||||||||||
909 | + sizeof(quint16) // instruction length == 0 | - | ||||||||||||
910 | + points.size()*(1) // flags | - | ||||||||||||
911 | + point_array_size; // coordinates | - | ||||||||||||
912 | - | |||||||||||||
913 | glyph->data.resize(max_size); | - | ||||||||||||
914 | - | |||||||||||||
915 | QTtfStream s(glyph->data); | - | ||||||||||||
916 | s << qint16(endPoints.size()) | - | ||||||||||||
917 | << glyph->xMin << glyph->yMin << glyph->xMax << glyph->yMax; | - | ||||||||||||
918 | - | |||||||||||||
919 | for (int i = 0; i < endPoints.size(); ++i)
| 0 | ||||||||||||
920 | s << quint16(endPoints.at(i)); never executed: s << quint16(endPoints.at(i)); | 0 | ||||||||||||
921 | s << quint16(0); // instruction length | - | ||||||||||||
922 | - | |||||||||||||
923 | // emit flags | - | ||||||||||||
924 | for (int i = 0; i < points.size(); ++i)
| 0 | ||||||||||||
925 | s << quint8(points.at(i).flags); never executed: s << quint8(points.at(i).flags); | 0 | ||||||||||||
926 | // emit points | - | ||||||||||||
927 | for (int i = 0; i < points.size(); ++i) {
| 0 | ||||||||||||
928 | quint8 flags = points.at(i).flags; | - | ||||||||||||
929 | qint16 x = points.at(i).x; | - | ||||||||||||
930 | - | |||||||||||||
931 | if (flags & XShortVector)
| 0 | ||||||||||||
932 | s << quint8(x); never executed: s << quint8(x); | 0 | ||||||||||||
933 | else if (!(flags & XSame))
| 0 | ||||||||||||
934 | s << qint16(x); never executed: s << qint16(x); | 0 | ||||||||||||
935 | } never executed: end of block | 0 | ||||||||||||
936 | for (int i = 0; i < points.size(); ++i) {
| 0 | ||||||||||||
937 | quint8 flags = points.at(i).flags; | - | ||||||||||||
938 | qint16 y = points.at(i).y; | - | ||||||||||||
939 | - | |||||||||||||
940 | if (flags & YShortVector)
| 0 | ||||||||||||
941 | s << quint8(y); never executed: s << quint8(y); | 0 | ||||||||||||
942 | else if (!(flags & YSame))
| 0 | ||||||||||||
943 | s << qint16(y); never executed: s << qint16(y); | 0 | ||||||||||||
944 | } never executed: end of block | 0 | ||||||||||||
945 | - | |||||||||||||
946 | // qDebug() << "offset=" << s.offset() << "max_size=" << max_size << "point_array_size=" << point_array_size; | - | ||||||||||||
947 | Q_ASSERT(s.offset() == max_size); | - | ||||||||||||
948 | - | |||||||||||||
949 | glyph->numContours = endPoints.size(); | - | ||||||||||||
950 | glyph->numPoints = points.size(); | - | ||||||||||||
951 | } never executed: end of block | 0 | ||||||||||||
952 | - | |||||||||||||
953 | static QTtfGlyph generateGlyph(int index, const QPainterPath &path, qreal advance, qreal lsb, qreal ppem) | - | ||||||||||||
954 | { | - | ||||||||||||
955 | QVector<TTF_POINT> points; | - | ||||||||||||
956 | QVector<int> endPoints; | - | ||||||||||||
957 | QTtfGlyph glyph; | - | ||||||||||||
958 | glyph.index = index; | - | ||||||||||||
959 | glyph.advanceWidth = qRound(advance * 2048. / ppem); | - | ||||||||||||
960 | glyph.lsb = qRound(lsb * 2048. / ppem); | - | ||||||||||||
961 | - | |||||||||||||
962 | if (!path.elementCount()) {
| 0 | ||||||||||||
963 | //qDebug("glyph %d is empty", index); | - | ||||||||||||
964 | lsb = 0; | - | ||||||||||||
965 | glyph.xMin = glyph.xMax = glyph.yMin = glyph.yMax = 0; | - | ||||||||||||
966 | glyph.numContours = 0; | - | ||||||||||||
967 | glyph.numPoints = 0; | - | ||||||||||||
968 | return glyph; never executed: return glyph; | 0 | ||||||||||||
969 | } | - | ||||||||||||
970 | - | |||||||||||||
971 | convertPath(path, &points, &endPoints, ppem); | - | ||||||||||||
972 | - | |||||||||||||
973 | // qDebug() << "number of contours=" << endPoints.size(); | - | ||||||||||||
974 | // for (int i = 0; i < points.size(); ++i) | - | ||||||||||||
975 | // qDebug() << " point[" << i << "] = " << QPoint(points.at(i).x, points.at(i).y) << " flags=" << points.at(i).flags; | - | ||||||||||||
976 | // qDebug() << "endPoints:"; | - | ||||||||||||
977 | // for (int i = 0; i < endPoints.size(); ++i) | - | ||||||||||||
978 | // qDebug() << endPoints.at(i); | - | ||||||||||||
979 | - | |||||||||||||
980 | getBounds(points, &glyph.xMin, &glyph.xMax, &glyph.yMin, &glyph.yMax); | - | ||||||||||||
981 | int point_array_size = convertToRelative(&points); | - | ||||||||||||
982 | getGlyphData(&glyph, points, endPoints, point_array_size); | - | ||||||||||||
983 | return glyph; never executed: return glyph; | 0 | ||||||||||||
984 | } | - | ||||||||||||
985 | - | |||||||||||||
986 | static bool operator <(const QTtfGlyph &g1, const QTtfGlyph &g2) | - | ||||||||||||
987 | { | - | ||||||||||||
988 | return g1.index < g2.index; never executed: return g1.index < g2.index; | 0 | ||||||||||||
989 | } | - | ||||||||||||
990 | - | |||||||||||||
991 | static QVector<QTtfTable> generateGlyphTables(qttf_font_tables &tables, const QVector<QTtfGlyph> &_glyphs) | - | ||||||||||||
992 | { | - | ||||||||||||
993 | const int max_size_small = 65536*2; | - | ||||||||||||
994 | QVector<QTtfGlyph> glyphs = _glyphs; | - | ||||||||||||
995 | std::sort(glyphs.begin(), glyphs.end()); | - | ||||||||||||
996 | - | |||||||||||||
997 | Q_ASSERT(tables.maxp.numGlyphs == glyphs.at(glyphs.size()-1).index + 1); | - | ||||||||||||
998 | int nGlyphs = tables.maxp.numGlyphs; | - | ||||||||||||
999 | - | |||||||||||||
1000 | int glyf_size = 0; | - | ||||||||||||
1001 | for (int i = 0; i < glyphs.size(); ++i)
| 0 | ||||||||||||
1002 | glyf_size += (glyphs.at(i).data.size() + 3) & ~3; never executed: glyf_size += (glyphs.at(i).data.size() + 3) & ~3; | 0 | ||||||||||||
1003 | - | |||||||||||||
1004 | tables.head.indexToLocFormat = glyf_size < max_size_small ? 0 : 1;
| 0 | ||||||||||||
1005 | tables.hhea.numberOfHMetrics = nGlyphs; | - | ||||||||||||
1006 | - | |||||||||||||
1007 | QTtfTable glyf; | - | ||||||||||||
1008 | glyf.tag = MAKE_TAG('g', 'l', 'y', 'f'); | - | ||||||||||||
1009 | - | |||||||||||||
1010 | QTtfTable loca; | - | ||||||||||||
1011 | loca.tag = MAKE_TAG('l', 'o', 'c', 'a'); | - | ||||||||||||
1012 | loca.data.resize(glyf_size < max_size_small ? (nGlyphs+1)*sizeof(quint16) : (nGlyphs+1)*sizeof(quint32)); | - | ||||||||||||
1013 | QTtfStream ls(loca.data); | - | ||||||||||||
1014 | - | |||||||||||||
1015 | QTtfTable hmtx; | - | ||||||||||||
1016 | hmtx.tag = MAKE_TAG('h', 'm', 't', 'x'); | - | ||||||||||||
1017 | hmtx.data.resize(nGlyphs*4); | - | ||||||||||||
1018 | QTtfStream hs(hmtx.data); | - | ||||||||||||
1019 | - | |||||||||||||
1020 | int pos = 0; | - | ||||||||||||
1021 | for (int i = 0; i < nGlyphs; ++i) {
| 0 | ||||||||||||
1022 | int gpos = glyf.data.size(); | - | ||||||||||||
1023 | quint16 advance = 0; | - | ||||||||||||
1024 | qint16 lsb = 0; | - | ||||||||||||
1025 | - | |||||||||||||
1026 | if (glyphs[pos].index == i) {
| 0 | ||||||||||||
1027 | // emit glyph | - | ||||||||||||
1028 | // qDebug("emitting glyph %d: size=%d", i, glyphs.at(i).data.size()); | - | ||||||||||||
1029 | glyf.data += glyphs.at(pos).data; | - | ||||||||||||
1030 | while (glyf.data.size() & 1)
| 0 | ||||||||||||
1031 | glyf.data.append('\0'); never executed: glyf.data.append('\0'); | 0 | ||||||||||||
1032 | advance = glyphs.at(pos).advanceWidth; | - | ||||||||||||
1033 | lsb = glyphs.at(pos).lsb; | - | ||||||||||||
1034 | ++pos; | - | ||||||||||||
1035 | } never executed: end of block | 0 | ||||||||||||
1036 | if (glyf_size < max_size_small) {
| 0 | ||||||||||||
1037 | // use short loca format | - | ||||||||||||
1038 | ls << quint16(gpos>>1); | - | ||||||||||||
1039 | } else { never executed: end of block | 0 | ||||||||||||
1040 | // use long loca format | - | ||||||||||||
1041 | ls << quint32(gpos); | - | ||||||||||||
1042 | } never executed: end of block | 0 | ||||||||||||
1043 | hs << advance | - | ||||||||||||
1044 | << lsb; | - | ||||||||||||
1045 | } never executed: end of block | 0 | ||||||||||||
1046 | if (glyf_size < max_size_small) {
| 0 | ||||||||||||
1047 | // use short loca format | - | ||||||||||||
1048 | ls << quint16(glyf.data.size()>>1); | - | ||||||||||||
1049 | } else { never executed: end of block | 0 | ||||||||||||
1050 | // use long loca format | - | ||||||||||||
1051 | ls << quint32(glyf.data.size()); | - | ||||||||||||
1052 | } never executed: end of block | 0 | ||||||||||||
1053 | - | |||||||||||||
1054 | Q_ASSERT(loca.data.size() == ls.offset()); | - | ||||||||||||
1055 | Q_ASSERT(hmtx.data.size() == hs.offset()); | - | ||||||||||||
1056 | - | |||||||||||||
1057 | QVector<QTtfTable> list; | - | ||||||||||||
1058 | list.append(glyf); | - | ||||||||||||
1059 | list.append(loca); | - | ||||||||||||
1060 | list.append(hmtx); | - | ||||||||||||
1061 | return list; never executed: return list; | 0 | ||||||||||||
1062 | } | - | ||||||||||||
1063 | - | |||||||||||||
1064 | static bool operator <(const QTtfTable &t1, const QTtfTable &t2) | - | ||||||||||||
1065 | { | - | ||||||||||||
1066 | return t1.tag < t2.tag; never executed: return t1.tag < t2.tag; | 0 | ||||||||||||
1067 | } | - | ||||||||||||
1068 | - | |||||||||||||
1069 | static QByteArray bindFont(const QVector<QTtfTable>& _tables) | - | ||||||||||||
1070 | { | - | ||||||||||||
1071 | QVector<QTtfTable> tables = _tables; | - | ||||||||||||
1072 | - | |||||||||||||
1073 | std::sort(tables.begin(), tables.end()); | - | ||||||||||||
1074 | - | |||||||||||||
1075 | QByteArray font; | - | ||||||||||||
1076 | const int header_size = sizeof(qint32) + 4*sizeof(quint16); | - | ||||||||||||
1077 | const int directory_size = 4*sizeof(quint32)*tables.size(); | - | ||||||||||||
1078 | font.resize(header_size + directory_size); | - | ||||||||||||
1079 | - | |||||||||||||
1080 | int log2 = 0; | - | ||||||||||||
1081 | int pow = 1; | - | ||||||||||||
1082 | int n = tables.size() >> 1; | - | ||||||||||||
1083 | while (n) {
| 0 | ||||||||||||
1084 | ++log2; | - | ||||||||||||
1085 | pow <<= 1; | - | ||||||||||||
1086 | n >>= 1; | - | ||||||||||||
1087 | } never executed: end of block | 0 | ||||||||||||
1088 | - | |||||||||||||
1089 | quint32 head_offset = 0; | - | ||||||||||||
1090 | { | - | ||||||||||||
1091 | QTtfStream f(font); | - | ||||||||||||
1092 | // Offset Table | - | ||||||||||||
1093 | // Type Name Description | - | ||||||||||||
1094 | // qint32 sfnt version 0x00010000 for version 1.0. | - | ||||||||||||
1095 | // quint16 numTables Number of tables. | - | ||||||||||||
1096 | // quint16 searchRange (Maximum power of 2 <= numTables) x 16. | - | ||||||||||||
1097 | // quint16 entrySelector Log2(maximum power of 2 <= numTables). | - | ||||||||||||
1098 | // quint16 rangeShift NumTables x 16-searchRange. | - | ||||||||||||
1099 | f << qint32(0x00010000) | - | ||||||||||||
1100 | << quint16(tables.size()) | - | ||||||||||||
1101 | << quint16(16*pow) | - | ||||||||||||
1102 | << quint16(log2) | - | ||||||||||||
1103 | << quint16(16*(tables.size() - pow)); | - | ||||||||||||
1104 | - | |||||||||||||
1105 | // Table Directory | - | ||||||||||||
1106 | // Type Name Description | - | ||||||||||||
1107 | // quint32 tag 4 -byte identifier. | - | ||||||||||||
1108 | // quint32 checkSum CheckSum for this table. | - | ||||||||||||
1109 | // quint32 offset Offset from beginning of TrueType font file. | - | ||||||||||||
1110 | // quint32 length Length of this table. | - | ||||||||||||
1111 | quint32 table_offset = header_size + directory_size; | - | ||||||||||||
1112 | for (int i = 0; i < tables.size(); ++i) {
| 0 | ||||||||||||
1113 | const QTtfTable &t = tables.at(i); | - | ||||||||||||
1114 | const quint32 size = (t.data.size() + 3) & ~3; | - | ||||||||||||
1115 | if (t.tag == MAKE_TAG('h', 'e', 'a', 'd'))
| 0 | ||||||||||||
1116 | head_offset = table_offset; never executed: head_offset = table_offset; | 0 | ||||||||||||
1117 | f << t.tag | - | ||||||||||||
1118 | << checksum(t.data) | - | ||||||||||||
1119 | << table_offset | - | ||||||||||||
1120 | << t.data.size(); | - | ||||||||||||
1121 | table_offset += size; | - | ||||||||||||
1122 | #define TAG(x) char(t.tag >> 24) << char((t.tag >> 16) & 0xff) << char((t.tag >> 8) & 0xff) << char(t.tag & 0xff) | - | ||||||||||||
1123 | //qDebug() << "table " << TAG(t.tag) << "has size " << t.data.size() << "stream at " << f.offset(); | - | ||||||||||||
1124 | } never executed: end of block | 0 | ||||||||||||
1125 | } | - | ||||||||||||
1126 | for (int i = 0; i < tables.size(); ++i) {
| 0 | ||||||||||||
1127 | const QByteArray &t = tables.at(i).data; | - | ||||||||||||
1128 | font += t; | - | ||||||||||||
1129 | int s = t.size(); | - | ||||||||||||
1130 | while (s & 3) { font += '\0'; ++s; } never executed: end of block
| 0 | ||||||||||||
1131 | } never executed: end of block | 0 | ||||||||||||
1132 | - | |||||||||||||
1133 | if (!head_offset) {
| 0 | ||||||||||||
1134 | qWarning("QFontSubset: Font misses 'head' table"); | - | ||||||||||||
1135 | return QByteArray(); never executed: return QByteArray(); | 0 | ||||||||||||
1136 | } | - | ||||||||||||
1137 | - | |||||||||||||
1138 | // calculate the fonts checksum and qToBigEndian into 'head's checksum_adjust | - | ||||||||||||
1139 | quint32 checksum_adjust = 0xB1B0AFBA - checksum(font); | - | ||||||||||||
1140 | qToBigEndian(checksum_adjust, (uchar *)font.data() + head_offset + 8); | - | ||||||||||||
1141 | - | |||||||||||||
1142 | return font; never executed: return font; | 0 | ||||||||||||
1143 | } | - | ||||||||||||
1144 | - | |||||||||||||
1145 | - | |||||||||||||
1146 | /* | - | ||||||||||||
1147 | PDF requires the following tables: | - | ||||||||||||
1148 | - | |||||||||||||
1149 | head, hhea, loca, maxp, cvt , prep, glyf, hmtx, fpgm | - | ||||||||||||
1150 | - | |||||||||||||
1151 | This means we don't have to add a os/2, post or name table. cvt , prep and fpgm could be empty | - | ||||||||||||
1152 | if really required. | - | ||||||||||||
1153 | */ | - | ||||||||||||
1154 | - | |||||||||||||
1155 | QByteArray QFontSubset::toTruetype() const | - | ||||||||||||
1156 | { | - | ||||||||||||
1157 | qttf_font_tables font; | - | ||||||||||||
1158 | memset(&font, 0, sizeof(qttf_font_tables)); | - | ||||||||||||
1159 | - | |||||||||||||
1160 | qreal ppem = fontEngine->fontDef.pixelSize; | - | ||||||||||||
1161 | #define TO_TTF(x) qRound(x * 2048. / ppem) | - | ||||||||||||
1162 | - | |||||||||||||
1163 | QFontEngine::Properties properties = fontEngine->properties(); | - | ||||||||||||
1164 | // initialize some stuff needed in createWidthArray | - | ||||||||||||
1165 | emSquare = 2048; | - | ||||||||||||
1166 | widths.resize(nGlyphs()); | - | ||||||||||||
1167 | - | |||||||||||||
1168 | // head table | - | ||||||||||||
1169 | font.head.font_revision = 0x00010000; | - | ||||||||||||
1170 | font.head.flags = (1 << 2) | (1 << 4); | - | ||||||||||||
1171 | font.head.created = 0; // ### | - | ||||||||||||
1172 | font.head.modified = 0; // ### | - | ||||||||||||
1173 | font.head.xMin = SHRT_MAX; | - | ||||||||||||
1174 | font.head.xMax = SHRT_MIN; | - | ||||||||||||
1175 | font.head.yMin = SHRT_MAX; | - | ||||||||||||
1176 | font.head.yMax = SHRT_MIN; | - | ||||||||||||
1177 | font.head.macStyle = (fontEngine->fontDef.weight > QFont::Normal) ? 1 : 0;
| 0 | ||||||||||||
1178 | font.head.macStyle |= (fontEngine->fontDef.styleHint != QFont::StyleNormal) ? 1 : 0;
| 0 | ||||||||||||
1179 | - | |||||||||||||
1180 | // hhea table | - | ||||||||||||
1181 | font.hhea.ascender = qRound(properties.ascent); | - | ||||||||||||
1182 | font.hhea.descender = -qRound(properties.descent); | - | ||||||||||||
1183 | font.hhea.lineGap = qRound(properties.leading); | - | ||||||||||||
1184 | font.hhea.maxAdvanceWidth = TO_TTF(fontEngine->maxCharWidth()); | - | ||||||||||||
1185 | font.hhea.minLeftSideBearing = TO_TTF(fontEngine->minLeftBearing()); | - | ||||||||||||
1186 | font.hhea.minRightSideBearing = TO_TTF(fontEngine->minRightBearing()); | - | ||||||||||||
1187 | font.hhea.xMaxExtent = SHRT_MIN; | - | ||||||||||||
1188 | - | |||||||||||||
1189 | font.maxp.numGlyphs = 0; | - | ||||||||||||
1190 | font.maxp.maxPoints = 0; | - | ||||||||||||
1191 | font.maxp.maxContours = 0; | - | ||||||||||||
1192 | font.maxp.maxCompositePoints = 0; | - | ||||||||||||
1193 | font.maxp.maxCompositeContours = 0; | - | ||||||||||||
1194 | font.maxp.maxComponentElements = 0; | - | ||||||||||||
1195 | font.maxp.maxComponentDepth = 0; | - | ||||||||||||
1196 | const int numGlyphs = nGlyphs(); | - | ||||||||||||
1197 | font.maxp.numGlyphs = numGlyphs; | - | ||||||||||||
1198 | QVector<QTtfGlyph> glyphs; | - | ||||||||||||
1199 | glyphs.reserve(numGlyphs); | - | ||||||||||||
1200 | - | |||||||||||||
1201 | uint sumAdvances = 0; | - | ||||||||||||
1202 | for (int i = 0; i < numGlyphs; ++i) {
| 0 | ||||||||||||
1203 | glyph_t g = glyph_indices.at(i); | - | ||||||||||||
1204 | QPainterPath path; | - | ||||||||||||
1205 | glyph_metrics_t metric; | - | ||||||||||||
1206 | fontEngine->getUnscaledGlyph(g, &path, &metric); | - | ||||||||||||
1207 | if (noEmbed) {
| 0 | ||||||||||||
1208 | path = QPainterPath(); | - | ||||||||||||
1209 | if (g == 0)
| 0 | ||||||||||||
1210 | path.addRect(QRectF(0, 0, 1000, 1000)); never executed: path.addRect(QRectF(0, 0, 1000, 1000)); | 0 | ||||||||||||
1211 | } never executed: end of block | 0 | ||||||||||||
1212 | QTtfGlyph glyph = generateGlyph(i, path, metric.xoff.toReal(), metric.x.toReal(), properties.emSquare.toReal()); | - | ||||||||||||
1213 | - | |||||||||||||
1214 | font.head.xMin = qMin(font.head.xMin, glyph.xMin); | - | ||||||||||||
1215 | font.head.xMax = qMax(font.head.xMax, glyph.xMax); | - | ||||||||||||
1216 | font.head.yMin = qMin(font.head.yMin, glyph.yMin); | - | ||||||||||||
1217 | font.head.yMax = qMax(font.head.yMax, glyph.yMax); | - | ||||||||||||
1218 | - | |||||||||||||
1219 | font.hhea.xMaxExtent = qMax(font.hhea.xMaxExtent, (qint16)(glyph.lsb + glyph.xMax - glyph.xMin)); | - | ||||||||||||
1220 | - | |||||||||||||
1221 | font.maxp.maxPoints = qMax(font.maxp.maxPoints, glyph.numPoints); | - | ||||||||||||
1222 | font.maxp.maxContours = qMax(font.maxp.maxContours, glyph.numContours); | - | ||||||||||||
1223 | - | |||||||||||||
1224 | if (glyph.xMax > glyph.xMin)
| 0 | ||||||||||||
1225 | sumAdvances += glyph.xMax - glyph.xMin; never executed: sumAdvances += glyph.xMax - glyph.xMin; | 0 | ||||||||||||
1226 | - | |||||||||||||
1227 | // qDebug("adding glyph %d size=%d", glyph.index, glyph.data.size()); | - | ||||||||||||
1228 | glyphs.append(glyph); | - | ||||||||||||
1229 | widths[i] = glyph.advanceWidth; | - | ||||||||||||
1230 | } never executed: end of block | 0 | ||||||||||||
1231 | - | |||||||||||||
1232 | - | |||||||||||||
1233 | QVector<QTtfTable> tables = generateGlyphTables(font, glyphs); | - | ||||||||||||
1234 | tables.append(generateHead(font.head)); | - | ||||||||||||
1235 | tables.append(generateHhea(font.hhea)); | - | ||||||||||||
1236 | tables.append(generateMaxp(font.maxp)); | - | ||||||||||||
1237 | // name | - | ||||||||||||
1238 | QTtfTable name_table; | - | ||||||||||||
1239 | name_table.tag = MAKE_TAG('n', 'a', 'm', 'e'); | - | ||||||||||||
1240 | if (!noEmbed)
| 0 | ||||||||||||
1241 | name_table.data = fontEngine->getSfntTable(name_table.tag); never executed: name_table.data = fontEngine->getSfntTable(name_table.tag); | 0 | ||||||||||||
1242 | if (name_table.data.isEmpty()) {
| 0 | ||||||||||||
1243 | qttf_name_table name; | - | ||||||||||||
1244 | if (noEmbed)
| 0 | ||||||||||||
1245 | name.copyright = QLatin1String("Fake font"); never executed: name.copyright = QLatin1String("Fake font"); | 0 | ||||||||||||
1246 | else | - | ||||||||||||
1247 | name.copyright = QLatin1String(properties.copyright); never executed: name.copyright = QLatin1String(properties.copyright); | 0 | ||||||||||||
1248 | name.family = fontEngine->fontDef.family; | - | ||||||||||||
1249 | name.subfamily = QLatin1String("Regular"); // ###### | - | ||||||||||||
1250 | name.postscript_name = QLatin1String(properties.postscriptName); | - | ||||||||||||
1251 | name_table = generateName(name); | - | ||||||||||||
1252 | } never executed: end of block | 0 | ||||||||||||
1253 | tables.append(name_table); | - | ||||||||||||
1254 | - | |||||||||||||
1255 | if (!noEmbed) {
| 0 | ||||||||||||
1256 | QTtfTable os2; | - | ||||||||||||
1257 | os2.tag = MAKE_TAG('O', 'S', '/', '2'); | - | ||||||||||||
1258 | os2.data = fontEngine->getSfntTable(os2.tag); | - | ||||||||||||
1259 | if (!os2.data.isEmpty())
| 0 | ||||||||||||
1260 | tables.append(os2); never executed: tables.append(os2); | 0 | ||||||||||||
1261 | } never executed: end of block | 0 | ||||||||||||
1262 | - | |||||||||||||
1263 | return bindFont(tables); never executed: return bindFont(tables); | 0 | ||||||||||||
1264 | } | - | ||||||||||||
1265 | - | |||||||||||||
1266 | QT_END_NAMESPACE | - | ||||||||||||
Source code | Switch to Preprocessed file |