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