Coverage Report

Created: 2025-11-16 07:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qtbase/src/gui/text/qfontengine_qpf2.cpp
Line
Count
Source
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 "qfontengine_qpf2_p.h"
41
42
#include <QtCore/QFile>
43
#include <QtCore/QFileInfo>
44
#include <QtCore/QDir>
45
#include <QtCore/QBuffer>
46
#include <QtCore/private/qstringiterator_p.h>
47
48
#include <QtGui/private/qpaintengine_raster_p.h>
49
#include <QtGui/private/qguiapplication_p.h>
50
#include <qpa/qplatformfontdatabase.h>
51
#include <qpa/qplatformintegration.h>
52
53
QT_BEGIN_NAMESPACE
54
55
//#define DEBUG_HEADER
56
//#define DEBUG_FONTENGINE
57
58
static const QFontEngineQPF2::TagType tagTypes[QFontEngineQPF2::NumTags] = {
59
    QFontEngineQPF2::StringType, // FontName
60
    QFontEngineQPF2::StringType, // FileName
61
    QFontEngineQPF2::UInt32Type, // FileIndex
62
    QFontEngineQPF2::UInt32Type, // FontRevision
63
    QFontEngineQPF2::StringType, // FreeText
64
    QFontEngineQPF2::FixedType,  // Ascent
65
    QFontEngineQPF2::FixedType,  // Descent
66
    QFontEngineQPF2::FixedType,  // Leading
67
    QFontEngineQPF2::FixedType,  // XHeight
68
    QFontEngineQPF2::FixedType,  // AverageCharWidth
69
    QFontEngineQPF2::FixedType,  // MaxCharWidth
70
    QFontEngineQPF2::FixedType,  // LineThickness
71
    QFontEngineQPF2::FixedType,  // MinLeftBearing
72
    QFontEngineQPF2::FixedType,  // MinRightBearing
73
    QFontEngineQPF2::FixedType,  // UnderlinePosition
74
    QFontEngineQPF2::UInt8Type,  // GlyphFormat
75
    QFontEngineQPF2::UInt8Type,  // PixelSize
76
    QFontEngineQPF2::UInt8Type,  // Weight
77
    QFontEngineQPF2::UInt8Type,  // Style
78
    QFontEngineQPF2::StringType, // EndOfHeader
79
    QFontEngineQPF2::BitFieldType// WritingSystems
80
};
81
82
83
#if defined(DEBUG_HEADER)
84
# define DEBUG_VERIFY qDebug
85
#else
86
0
# define DEBUG_VERIFY if (0) qDebug
87
#endif
88
89
#define READ_VERIFY(type, variable) \
90
0
    if (tagPtr + sizeof(type) > endPtr) { \
91
0
        DEBUG_VERIFY() << "read verify failed in line" << __LINE__; \
92
0
        return 0; \
93
0
    } \
94
0
    variable = qFromBigEndian<type>(tagPtr); \
95
0
    DEBUG_VERIFY() << "read value" << variable << "of type " #type; \
96
0
    tagPtr += sizeof(type)
97
98
template <typename T>
99
T readValue(const uchar *&data)
100
0
{
101
0
    T value = qFromBigEndian<T>(data);
102
0
    data += sizeof(T);
103
0
    return value;
104
0
}
Unexecuted instantiation: unsigned short readValue<unsigned short>(unsigned char const*&)
Unexecuted instantiation: unsigned int readValue<unsigned int>(unsigned char const*&)
105
106
#define VERIFY(condition) \
107
0
    if (!(condition)) { \
108
0
        DEBUG_VERIFY() << "condition " #condition " failed in line" << __LINE__; \
109
0
        return 0; \
110
0
    }
111
112
#define VERIFY_TAG(condition) \
113
0
    if (!(condition)) { \
114
0
        DEBUG_VERIFY() << "verifying tag condition " #condition " failed in line" << __LINE__ << "with tag" << tag; \
115
0
        return 0; \
116
0
    }
117
118
static inline const uchar *verifyTag(const uchar *tagPtr, const uchar *endPtr)
119
0
{
120
0
    quint16 tag, length;
121
0
    READ_VERIFY(quint16, tag);
122
0
    READ_VERIFY(quint16, length);
123
0
    if (tag == QFontEngineQPF2::Tag_EndOfHeader)
124
0
        return endPtr;
125
0
    if (tag < QFontEngineQPF2::NumTags) {
126
0
        switch (tagTypes[tag]) {
127
0
            case QFontEngineQPF2::BitFieldType:
128
0
            case QFontEngineQPF2::StringType:
129
                // can't do anything...
130
0
                break;
131
0
            case QFontEngineQPF2::UInt32Type:
132
0
                VERIFY_TAG(length == sizeof(quint32));
133
0
                break;
134
0
            case QFontEngineQPF2::FixedType:
135
0
                VERIFY_TAG(length == sizeof(quint32));
136
0
                break;
137
0
            case QFontEngineQPF2::UInt8Type:
138
0
                VERIFY_TAG(length == sizeof(quint8));
139
0
                break;
140
0
        }
141
#if defined(DEBUG_HEADER)
142
        if (length == 1)
143
            qDebug() << "tag data" << Qt::hex << *tagPtr;
144
        else if (length == 4)
145
            qDebug() << "tag data" << Qt::hex << tagPtr[0] << tagPtr[1] << tagPtr[2] << tagPtr[3];
146
#endif
147
0
    }
148
0
    return tagPtr + length;
149
0
}
150
151
const QFontEngineQPF2::Glyph *QFontEngineQPF2::findGlyph(glyph_t g) const
152
0
{
153
0
    if (!g || g >= glyphMapEntries)
154
0
        return nullptr;
155
0
    const quint32 *gmapPtr = reinterpret_cast<const quint32 *>(fontData + glyphMapOffset);
156
0
    quint32 glyphPos = qFromBigEndian<quint32>(gmapPtr[g]);
157
0
    if (glyphPos > glyphDataSize) {
158
0
        if (glyphPos == 0xffffffff)
159
0
            return nullptr;
160
#if defined(DEBUG_FONTENGINE)
161
        qDebug() << "glyph" << g << "outside of glyphData, remapping font file";
162
#endif
163
0
        if (glyphPos > glyphDataSize)
164
0
            return nullptr;
165
0
    }
166
0
    return reinterpret_cast<const Glyph *>(fontData + glyphDataOffset + glyphPos);
167
0
}
168
169
bool QFontEngineQPF2::verifyHeader(const uchar *data, int size)
170
0
{
171
0
    VERIFY(quintptr(data) % Q_ALIGNOF(Header) == 0);
172
0
    VERIFY(size >= int(sizeof(Header)));
173
0
    const Header *header = reinterpret_cast<const Header *>(data);
174
0
    if (header->magic[0] != 'Q'
175
0
        || header->magic[1] != 'P'
176
0
        || header->magic[2] != 'F'
177
0
        || header->magic[3] != '2')
178
0
        return false;
179
180
0
    VERIFY(header->majorVersion <= CurrentMajorVersion);
181
0
    const quint16 dataSize = qFromBigEndian<quint16>(header->dataSize);
182
0
    VERIFY(size >= int(sizeof(Header)) + dataSize);
183
184
0
    const uchar *tagPtr = data + sizeof(Header);
185
0
    const uchar *tagEndPtr = tagPtr + dataSize;
186
0
    while (tagPtr < tagEndPtr - 3) {
187
0
        tagPtr = verifyTag(tagPtr, tagEndPtr);
188
0
        VERIFY(tagPtr);
189
0
    }
190
191
0
    VERIFY(tagPtr <= tagEndPtr);
192
0
    return true;
193
0
}
194
195
QVariant QFontEngineQPF2::extractHeaderField(const uchar *data, HeaderTag requestedTag)
196
0
{
197
0
    const Header *header = reinterpret_cast<const Header *>(data);
198
0
    const uchar *tagPtr = data + sizeof(Header);
199
0
    const uchar *endPtr = tagPtr + qFromBigEndian<quint16>(header->dataSize);
200
0
    while (tagPtr < endPtr - 3) {
201
0
        quint16 tag = readValue<quint16>(tagPtr);
202
0
        quint16 length = readValue<quint16>(tagPtr);
203
0
        if (tag == requestedTag) {
204
0
            switch (tagTypes[requestedTag]) {
205
0
                case StringType:
206
0
                    return QVariant(QString::fromUtf8(reinterpret_cast<const char *>(tagPtr), length));
207
0
                case UInt32Type:
208
0
                    return QVariant(readValue<quint32>(tagPtr));
209
0
                case UInt8Type:
210
0
                    return QVariant(uint(*tagPtr));
211
0
                case FixedType:
212
0
                    return QVariant(QFixed::fromFixed(readValue<quint32>(tagPtr)).toReal());
213
0
                case BitFieldType:
214
0
                    return QVariant(QByteArray(reinterpret_cast<const char *>(tagPtr), length));
215
0
            }
216
0
            return QVariant();
217
0
        } else if (tag == Tag_EndOfHeader) {
218
0
            break;
219
0
        }
220
0
        tagPtr += length;
221
0
    }
222
223
0
    return QVariant();
224
0
}
225
226
227
QFontEngineQPF2::QFontEngineQPF2(const QFontDef &def, const QByteArray &data)
228
0
    : QFontEngine(QPF2),
229
0
      fontData(reinterpret_cast<const uchar *>(data.constData())), dataSize(data.size())
230
0
{
231
0
    fontDef = def;
232
0
    cache_cost = 100;
233
0
    cmap = nullptr;
234
0
    cmapOffset = 0;
235
0
    cmapSize = 0;
236
0
    glyphMapOffset = 0;
237
0
    glyphMapEntries = 0;
238
0
    glyphDataOffset = 0;
239
0
    glyphDataSize = 0;
240
0
    kerning_pairs_loaded = false;
241
0
    readOnly = true;
242
243
#if defined(DEBUG_FONTENGINE)
244
    qDebug() << "QFontEngineQPF2::QFontEngineQPF2( fd =" << fd << ", renderingFontEngine =" << renderingFontEngine << ')';
245
#endif
246
247
0
    if (!verifyHeader(fontData, dataSize)) {
248
#if defined(DEBUG_FONTENGINE)
249
        qDebug("verifyHeader failed!");
250
#endif
251
0
        return;
252
0
    }
253
254
0
    const Header *header = reinterpret_cast<const Header *>(fontData);
255
256
0
    readOnly = (header->lock == 0xffffffff);
257
258
0
    const uchar *imgData = fontData + sizeof(Header) + qFromBigEndian<quint16>(header->dataSize);
259
0
    const uchar *endPtr = fontData + dataSize;
260
0
    while (imgData <= endPtr - 8) {
261
0
        quint16 blockTag = readValue<quint16>(imgData);
262
0
        imgData += 2; // skip padding
263
0
        quint32 blockSize = readValue<quint32>(imgData);
264
265
0
        if (blockTag == CMapBlock) {
266
0
            cmapOffset = imgData - fontData;
267
0
            cmapSize = blockSize;
268
0
        } else if (blockTag == GMapBlock) {
269
0
            glyphMapOffset = imgData - fontData;
270
0
            glyphMapEntries = blockSize / 4;
271
0
        } else if (blockTag == GlyphBlock) {
272
0
            glyphDataOffset = imgData - fontData;
273
0
            glyphDataSize = blockSize;
274
0
        }
275
276
0
        imgData += blockSize;
277
0
    }
278
279
0
    face_id.filename = QFile::encodeName(extractHeaderField(fontData, Tag_FileName).toString());
280
0
    face_id.index = extractHeaderField(fontData, Tag_FileIndex).toInt();
281
282
    // get the real cmap
283
0
    if (cmapOffset) {
284
0
        cmap = QFontEngine::getCMap(fontData + cmapOffset, cmapSize, &symbol, &cmapSize);
285
0
        cmapOffset = cmap ? cmap - fontData : 0;
286
0
    }
287
288
    // verify all the positions in the glyphMap
289
0
    if (glyphMapOffset) {
290
0
        const quint32 *gmapPtr = reinterpret_cast<const quint32 *>(fontData + glyphMapOffset);
291
0
        for (uint i = 0; i < glyphMapEntries; ++i) {
292
0
            quint32 glyphDataPos = qFromBigEndian<quint32>(gmapPtr[i]);
293
0
            if (glyphDataPos == 0xffffffff)
294
0
                continue;
295
0
            if (glyphDataPos >= glyphDataSize) {
296
                // error
297
0
                glyphMapOffset = 0;
298
0
                glyphMapEntries = 0;
299
0
                break;
300
0
            }
301
0
        }
302
0
    }
303
304
#if defined(DEBUG_FONTENGINE)
305
    if (!isValid())
306
        qDebug() << "fontData" <<  fontData << "dataSize" << dataSize
307
                 << "cmap" << cmap << "cmapOffset" << cmapOffset
308
                 << "glyphMapOffset" << glyphMapOffset << "glyphDataOffset" << glyphDataOffset
309
                 << "fd" << fd << "glyphDataSize" << glyphDataSize;
310
#endif
311
0
}
312
313
QFontEngineQPF2::~QFontEngineQPF2()
314
0
{
315
0
}
316
317
bool QFontEngineQPF2::getSfntTableData(uint tag, uchar *buffer, uint *length) const
318
0
{
319
0
    if (tag != MAKE_TAG('c', 'm', 'a', 'p') || !cmap)
320
0
        return false;
321
322
0
    if (buffer && int(*length) >= cmapSize)
323
0
        memcpy(buffer, cmap, cmapSize);
324
0
    *length = cmapSize;
325
0
    Q_ASSERT(int(*length) > 0);
326
0
    return true;
327
0
}
328
329
glyph_t QFontEngineQPF2::glyphIndex(uint ucs4) const
330
0
{
331
0
    glyph_t glyph = getTrueTypeGlyphIndex(cmap, cmapSize, ucs4);
332
0
    if (glyph == 0 && symbol && ucs4 < 0x100)
333
0
        glyph = getTrueTypeGlyphIndex(cmap, cmapSize, ucs4 + 0xf000);
334
0
    if (!findGlyph(glyph))
335
0
        glyph = 0;
336
337
0
    return glyph;
338
0
}
339
340
bool QFontEngineQPF2::stringToCMap(const QChar *str, int len, QGlyphLayout *glyphs, int *nglyphs, QFontEngine::ShaperFlags flags) const
341
0
{
342
0
    Q_ASSERT(glyphs->numGlyphs >= *nglyphs);
343
0
    if (*nglyphs < len) {
344
0
        *nglyphs = len;
345
0
        return false;
346
0
    }
347
348
#if defined(DEBUG_FONTENGINE)
349
    QSet<QChar> seenGlyphs;
350
#endif
351
352
0
    int glyph_pos = 0;
353
0
    if (symbol) {
354
0
        QStringIterator it(str, str + len);
355
0
        while (it.hasNext()) {
356
0
            const uint uc = it.next();
357
0
            glyphs->glyphs[glyph_pos] = getTrueTypeGlyphIndex(cmap, cmapSize, uc);
358
0
            if(!glyphs->glyphs[glyph_pos] && uc < 0x100)
359
0
                glyphs->glyphs[glyph_pos] = getTrueTypeGlyphIndex(cmap, cmapSize, uc + 0xf000);
360
0
            ++glyph_pos;
361
0
        }
362
0
    } else {
363
0
        QStringIterator it(str, str + len);
364
0
        while (it.hasNext()) {
365
0
            const uint uc = it.next();
366
0
            glyphs->glyphs[glyph_pos] = getTrueTypeGlyphIndex(cmap, cmapSize, uc);
367
#if 0 && defined(DEBUG_FONTENGINE)
368
            QChar c(uc);
369
            if (!findGlyph(glyphs[glyph_pos].glyph) && !seenGlyphs.contains(c))
370
                qDebug() << "glyph for character" << c << '/' << Qt::hex << uc << "is" << Qt::dec << glyphs[glyph_pos].glyph;
371
372
            seenGlyphs.insert(c);
373
#endif
374
0
            ++glyph_pos;
375
0
        }
376
0
    }
377
378
0
    *nglyphs = glyph_pos;
379
0
    glyphs->numGlyphs = glyph_pos;
380
381
0
    if (!(flags & GlyphIndicesOnly))
382
0
        recalcAdvances(glyphs, flags);
383
384
0
    return true;
385
0
}
386
387
void QFontEngineQPF2::recalcAdvances(QGlyphLayout *glyphs, QFontEngine::ShaperFlags) const
388
0
{
389
0
    for (int i = 0; i < glyphs->numGlyphs; ++i) {
390
0
        const Glyph *g = findGlyph(glyphs->glyphs[i]);
391
0
        if (!g)
392
0
            continue;
393
0
        glyphs->advances[i] = g->advance;
394
0
    }
395
0
}
396
397
QImage QFontEngineQPF2::alphaMapForGlyph(glyph_t g)
398
0
{
399
0
    const Glyph *glyph = findGlyph(g);
400
0
    if (!glyph)
401
0
        return QImage();
402
403
0
    const uchar *bits = ((const uchar *) glyph) + sizeof(Glyph);
404
405
0
    QImage image(bits,glyph->width, glyph->height, glyph->bytesPerLine, QImage::Format_Alpha8);
406
407
0
    return image;
408
0
}
409
410
void QFontEngineQPF2::addOutlineToPath(qreal x, qreal y, const QGlyphLayout &glyphs, QPainterPath *path, QTextItem::RenderFlags flags)
411
0
{
412
0
    addBitmapFontToPath(x, y, glyphs, path, flags);
413
0
}
414
415
glyph_metrics_t QFontEngineQPF2::boundingBox(const QGlyphLayout &glyphs)
416
0
{
417
0
    glyph_metrics_t overall;
418
    // initialize with line height, we get the same behaviour on all platforms
419
0
    overall.y = -ascent();
420
0
    overall.height = ascent() + descent() + 1;
421
422
0
    QFixed ymax = 0;
423
0
    QFixed xmax = 0;
424
0
    for (int i = 0; i < glyphs.numGlyphs; i++) {
425
0
        const Glyph *g = findGlyph(glyphs.glyphs[i]);
426
0
        if (!g)
427
0
            continue;
428
429
0
        QFixed x = overall.xoff + glyphs.offsets[i].x + g->x;
430
0
        QFixed y = overall.yoff + glyphs.offsets[i].y + g->y;
431
0
        overall.x = qMin(overall.x, x);
432
0
        overall.y = qMin(overall.y, y);
433
0
        xmax = qMax(xmax, x + g->width);
434
0
        ymax = qMax(ymax, y + g->height);
435
0
        overall.xoff += g->advance;
436
0
    }
437
0
    overall.height = qMax(overall.height, ymax - overall.y);
438
0
    overall.width = xmax - overall.x;
439
440
0
    return overall;
441
0
}
442
443
glyph_metrics_t QFontEngineQPF2::boundingBox(glyph_t glyph)
444
0
{
445
0
    glyph_metrics_t overall;
446
0
    const Glyph *g = findGlyph(glyph);
447
0
    if (!g)
448
0
        return overall;
449
0
    overall.x = g->x;
450
0
    overall.y = g->y;
451
0
    overall.width = g->width;
452
0
    overall.height = g->height;
453
0
    overall.xoff = g->advance;
454
0
    return overall;
455
0
}
456
457
QFixed QFontEngineQPF2::ascent() const
458
0
{
459
0
    return QFixed::fromReal(qvariant_cast<qreal>(extractHeaderField(fontData, Tag_Ascent)));
460
0
}
461
462
QFixed QFontEngineQPF2::capHeight() const
463
0
{
464
0
    return calculatedCapHeight();
465
0
}
466
467
QFixed QFontEngineQPF2::descent() const
468
0
{
469
0
    return QFixed::fromReal(qvariant_cast<qreal>(extractHeaderField(fontData, Tag_Descent)));
470
0
}
471
472
QFixed QFontEngineQPF2::leading() const
473
0
{
474
0
    return QFixed::fromReal(qvariant_cast<qreal>(extractHeaderField(fontData, Tag_Leading)));
475
0
}
476
477
qreal QFontEngineQPF2::maxCharWidth() const
478
0
{
479
0
    return qvariant_cast<qreal>(extractHeaderField(fontData, Tag_MaxCharWidth));
480
0
}
481
482
qreal QFontEngineQPF2::minLeftBearing() const
483
0
{
484
0
    return qvariant_cast<qreal>(extractHeaderField(fontData, Tag_MinLeftBearing));
485
0
}
486
487
qreal QFontEngineQPF2::minRightBearing() const
488
0
{
489
0
    return qvariant_cast<qreal>(extractHeaderField(fontData, Tag_MinRightBearing));
490
0
}
491
492
QFixed QFontEngineQPF2::underlinePosition() const
493
0
{
494
0
    return QFixed::fromReal(qvariant_cast<qreal>(extractHeaderField(fontData, Tag_UnderlinePosition)));
495
0
}
496
497
QFixed QFontEngineQPF2::lineThickness() const
498
0
{
499
0
    return QFixed::fromReal(qvariant_cast<qreal>(extractHeaderField(fontData, Tag_LineThickness)));
500
0
}
501
502
bool QFontEngineQPF2::isValid() const
503
0
{
504
0
    return fontData && dataSize && cmapOffset
505
0
           && glyphMapOffset && glyphDataOffset && glyphDataSize > 0;
506
0
}
507
508
void QPF2Generator::generate()
509
0
{
510
0
    writeHeader();
511
0
    writeGMap();
512
0
    writeBlock(QFontEngineQPF2::GlyphBlock, QByteArray());
513
514
0
    dev->seek(4); // position of header.lock
515
0
    writeUInt32(0);
516
0
}
517
518
void QPF2Generator::writeHeader()
519
0
{
520
0
    QFontEngineQPF2::Header header;
521
522
0
    header.magic[0] = 'Q';
523
0
    header.magic[1] = 'P';
524
0
    header.magic[2] = 'F';
525
0
    header.magic[3] = '2';
526
0
    header.lock = 1;
527
0
    header.majorVersion = QFontEngineQPF2::CurrentMajorVersion;
528
0
    header.minorVersion = QFontEngineQPF2::CurrentMinorVersion;
529
0
    header.dataSize = 0;
530
0
    dev->write((const char *)&header, sizeof(header));
531
532
0
    writeTaggedString(QFontEngineQPF2::Tag_FontName, fe->fontDef.family.toUtf8());
533
534
0
    QFontEngine::FaceId face = fe->faceId();
535
0
    writeTaggedString(QFontEngineQPF2::Tag_FileName, face.filename);
536
0
    writeTaggedUInt32(QFontEngineQPF2::Tag_FileIndex, face.index);
537
538
0
    {
539
0
        const QByteArray head = fe->getSfntTable(MAKE_TAG('h', 'e', 'a', 'd'));
540
0
        if (head.size() >= 4) {
541
0
            const quint32 revision = qFromBigEndian<quint32>(head.constData());
542
0
            writeTaggedUInt32(QFontEngineQPF2::Tag_FontRevision, revision);
543
0
        }
544
0
    }
545
546
0
    writeTaggedQFixed(QFontEngineQPF2::Tag_Ascent, fe->ascent());
547
0
    writeTaggedQFixed(QFontEngineQPF2::Tag_Descent, fe->descent());
548
0
    writeTaggedQFixed(QFontEngineQPF2::Tag_Leading, fe->leading());
549
0
    writeTaggedQFixed(QFontEngineQPF2::Tag_XHeight, fe->xHeight());
550
0
    writeTaggedQFixed(QFontEngineQPF2::Tag_AverageCharWidth, fe->averageCharWidth());
551
0
    writeTaggedQFixed(QFontEngineQPF2::Tag_MaxCharWidth, QFixed::fromReal(fe->maxCharWidth()));
552
0
    writeTaggedQFixed(QFontEngineQPF2::Tag_LineThickness, fe->lineThickness());
553
0
    writeTaggedQFixed(QFontEngineQPF2::Tag_MinLeftBearing, QFixed::fromReal(fe->minLeftBearing()));
554
0
    writeTaggedQFixed(QFontEngineQPF2::Tag_MinRightBearing, QFixed::fromReal(fe->minRightBearing()));
555
0
    writeTaggedQFixed(QFontEngineQPF2::Tag_UnderlinePosition, fe->underlinePosition());
556
0
    writeTaggedUInt8(QFontEngineQPF2::Tag_PixelSize, fe->fontDef.pixelSize);
557
0
    writeTaggedUInt8(QFontEngineQPF2::Tag_Weight, fe->fontDef.weight);
558
0
    writeTaggedUInt8(QFontEngineQPF2::Tag_Style, fe->fontDef.style);
559
560
0
    writeTaggedUInt8(QFontEngineQPF2::Tag_GlyphFormat, QFontEngineQPF2::AlphamapGlyphs);
561
562
0
    writeTaggedString(QFontEngineQPF2::Tag_EndOfHeader, QByteArray());
563
0
    align4();
564
565
0
    const quint64 size = dev->pos();
566
0
    header.dataSize = qToBigEndian<quint16>(size - sizeof(header));
567
0
    dev->seek(0);
568
0
    dev->write((const char *)&header, sizeof(header));
569
0
    dev->seek(size);
570
0
}
571
572
void QPF2Generator::writeGMap()
573
0
{
574
0
    const quint16 glyphCount = fe->glyphCount();
575
576
0
    writeUInt16(QFontEngineQPF2::GMapBlock);
577
0
    writeUInt16(0); // padding
578
0
    writeUInt32(glyphCount * 4);
579
580
0
    QByteArray &buffer = dev->buffer();
581
0
    const int numBytes = glyphCount * sizeof(quint32);
582
0
    qint64 pos = buffer.size();
583
0
    buffer.resize(pos + numBytes);
584
0
    memset(buffer.data() + pos, 0xff, numBytes);
585
0
    dev->seek(pos + numBytes);
586
0
}
587
588
void QPF2Generator::writeBlock(QFontEngineQPF2::BlockTag tag, const QByteArray &data)
589
0
{
590
0
    writeUInt16(tag);
591
0
    writeUInt16(0); // padding
592
0
    const int padSize = ((data.size() + 3) / 4) * 4 - data.size();
593
0
    writeUInt32(data.size() + padSize);
594
0
    dev->write(data);
595
0
    for (int i = 0; i < padSize; ++i)
596
0
        writeUInt8(0);
597
0
}
598
599
void QPF2Generator::writeTaggedString(QFontEngineQPF2::HeaderTag tag, const QByteArray &string)
600
0
{
601
0
    writeUInt16(tag);
602
0
    writeUInt16(string.length());
603
0
    dev->write(string);
604
0
}
605
606
void QPF2Generator::writeTaggedUInt32(QFontEngineQPF2::HeaderTag tag, quint32 value)
607
0
{
608
0
    writeUInt16(tag);
609
0
    writeUInt16(sizeof(value));
610
0
    writeUInt32(value);
611
0
}
612
613
void QPF2Generator::writeTaggedUInt8(QFontEngineQPF2::HeaderTag tag, quint8 value)
614
0
{
615
0
    writeUInt16(tag);
616
0
    writeUInt16(sizeof(value));
617
0
    writeUInt8(value);
618
0
}
619
620
void QPF2Generator::writeTaggedQFixed(QFontEngineQPF2::HeaderTag tag, QFixed value)
621
0
{
622
0
    writeUInt16(tag);
623
0
    writeUInt16(sizeof(quint32));
624
0
    writeUInt32(value.value());
625
0
}
626
627
QT_END_NAMESPACE