summaryrefslogtreecommitdiffstats
path: root/src/gui/text/qrawfont_win.cpp
blob: 6c62673b54991c5ebf2cec7392261436388b913a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qrawfont_p.h"
#include <private/qsystemlibrary_p.h>

#if !defined(QT_NO_DIRECTWRITE)
#  include "qfontenginedirectwrite_p.h"
#  include <dwrite.h>
#endif

#if !defined(QT_NO_RAWFONT)

QT_BEGIN_NAMESPACE

namespace {

    template<typename T>
    struct BigEndian
    {
        quint8 data[sizeof(T)];

        operator T() const
        {
            T littleEndian = 0;
            for (int i=0; i<sizeof(T); ++i) {
                littleEndian |= data[i] << ((sizeof(T) - i - 1) * 8);
            }

            return littleEndian;
        }

        BigEndian<T> &operator=(const T &t)
        {
            for (int i=0; i<sizeof(T); ++i) {
                data[i] = ((t >> (sizeof(T) - i - 1) * 8) & 0xff);
            }

            return *this;
        }
    };

#   pragma pack(1)

    // Common structure for all formats of the "name" table
    struct NameTable
    {
        BigEndian<quint16> format;
        BigEndian<quint16> count;
        BigEndian<quint16> stringOffset;
    };

    struct NameRecord
    {
        BigEndian<quint16> platformID;
        BigEndian<quint16> encodingID;
        BigEndian<quint16> languageID;
        BigEndian<quint16> nameID;
        BigEndian<quint16> length;
        BigEndian<quint16> offset;
    };

    struct OffsetSubTable
    {
        BigEndian<quint32> scalerType;
        BigEndian<quint16> numTables;
        BigEndian<quint16> searchRange;
        BigEndian<quint16> entrySelector;
        BigEndian<quint16> rangeShift;
    };

    struct TableDirectory
    {
        BigEndian<quint32> identifier;
        BigEndian<quint32> checkSum;
        BigEndian<quint32> offset;
        BigEndian<quint32> length;
    };

    struct OS2Table
    {
        BigEndian<quint16> version;
        BigEndian<qint16>  avgCharWidth;
        BigEndian<quint16> weightClass;
        BigEndian<quint16> widthClass;
        BigEndian<quint16> type;
        BigEndian<qint16>  subscriptXSize;
        BigEndian<qint16>  subscriptYSize;
        BigEndian<qint16>  subscriptXOffset;
        BigEndian<qint16>  subscriptYOffset;
        BigEndian<qint16>  superscriptXSize;
        BigEndian<qint16>  superscriptYSize;
        BigEndian<qint16>  superscriptXOffset;
        BigEndian<qint16>  superscriptYOffset;
        BigEndian<qint16>  strikeOutSize;
        BigEndian<qint16>  strikeOutPosition;
        BigEndian<qint16>  familyClass;
        quint8             panose[10];
        BigEndian<quint32> unicodeRanges[4];
        quint8             vendorID[4];
        BigEndian<quint16> selection;
        BigEndian<quint16> firstCharIndex;
        BigEndian<quint16> lastCharIndex;
        BigEndian<qint16>  typoAscender;
        BigEndian<qint16>  typoDescender;
        BigEndian<qint16>  typoLineGap;
        BigEndian<quint16> winAscent;
        BigEndian<quint16> winDescent;
        BigEndian<quint32> codepageRanges[2];
        BigEndian<qint16>  height;
        BigEndian<qint16>  capHeight;
        BigEndian<quint16> defaultChar;
        BigEndian<quint16> breakChar;
        BigEndian<quint16> maxContext;
    };

#   pragma pack()

    class EmbeddedFont
    {
    public:
        EmbeddedFont(const QByteArray &fontData);

        QString changeFamilyName(const QString &newFamilyName);
        QByteArray data() const { return m_fontData; }
        TableDirectory *tableDirectoryEntry(const QByteArray &tagName);
        QString familyName(TableDirectory *nameTableDirectory = 0);

    private:
        QByteArray m_fontData;
    };

    EmbeddedFont::EmbeddedFont(const QByteArray &fontData) : m_fontData(fontData)
    {
    }

    TableDirectory *EmbeddedFont::tableDirectoryEntry(const QByteArray &tagName)
    {
        Q_ASSERT(tagName.size() == 4);

        const BigEndian<quint32> *tagIdPtr =
                reinterpret_cast<const BigEndian<quint32> *>(tagName.constData());
        quint32 tagId = *tagIdPtr;

        OffsetSubTable *offsetSubTable = reinterpret_cast<OffsetSubTable *>(m_fontData.data());
        TableDirectory *tableDirectory = reinterpret_cast<TableDirectory *>(offsetSubTable + 1);

        TableDirectory *nameTableDirectoryEntry = 0;
        for (int i=0; i<offsetSubTable->numTables; ++i, ++tableDirectory) {
            if (tableDirectory->identifier == tagId) {
                nameTableDirectoryEntry = tableDirectory;
                break;
            }
        }

        return nameTableDirectoryEntry;
    }

    QString EmbeddedFont::familyName(TableDirectory *nameTableDirectoryEntry)
    {
        QString name;

        if (nameTableDirectoryEntry == 0)
            nameTableDirectoryEntry = tableDirectoryEntry("name");

        if (nameTableDirectoryEntry != 0) {
            NameTable *nameTable = reinterpret_cast<NameTable *>(m_fontData.data()
                                                                 + nameTableDirectoryEntry->offset);
            NameRecord *nameRecord = reinterpret_cast<NameRecord *>(nameTable + 1);
            for (int i=0; i<nameTable->count; ++i, ++nameRecord) {
                if (nameRecord->nameID == 1
                 && nameRecord->platformID == 3 // Windows
                 && nameRecord->languageID == 0x0409) { // US English
                    const void *ptr = reinterpret_cast<const quint8 *>(nameTable)
                                                        + nameTable->stringOffset
                                                        + nameRecord->offset;

                    const BigEndian<quint16> *s = reinterpret_cast<const BigEndian<quint16> *>(ptr);
                    for (int j=0; j<nameRecord->length / sizeof(quint16); ++j)
                        name += QChar(s[j]);

                    break;
                }
            }
        }

        return name;
    }

    QString EmbeddedFont::changeFamilyName(const QString &newFamilyName)
    {
        TableDirectory *nameTableDirectoryEntry = tableDirectoryEntry("name");
        if (nameTableDirectoryEntry == 0)
            return QString();

        QString oldFamilyName = familyName(nameTableDirectoryEntry);

        // Reserve size for name table header, five required name records and string
        const int requiredRecordCount = 5;
        quint16 nameIds[requiredRecordCount] = { 1, 2, 3, 4, 6 };

        int sizeOfHeader = sizeof(NameTable) + sizeof(NameRecord) * requiredRecordCount;
        int newFamilyNameSize = newFamilyName.size() * sizeof(quint16);

        const QString regularString = QString::fromLatin1("Regular");
        int regularStringSize = regularString.size() * sizeof(quint16);

        // Align table size of table to 32 bits (pad with 0)
        int fullSize = ((sizeOfHeader + newFamilyNameSize + regularStringSize) & ~3) + 4;

        QByteArray newNameTable(fullSize, char(0));

        {
            NameTable *nameTable = reinterpret_cast<NameTable *>(newNameTable.data());
            nameTable->count = requiredRecordCount;
            nameTable->stringOffset = sizeOfHeader;

            NameRecord *nameRecord = reinterpret_cast<NameRecord *>(nameTable + 1);
            for (int i=0; i<requiredRecordCount; ++i, nameRecord++) {
                nameRecord->nameID = nameIds[i];
                nameRecord->encodingID = 1;
                nameRecord->languageID = 0x0409;
                nameRecord->platformID = 3;
                nameRecord->length = newFamilyNameSize;

                // Special case for sub-family
                if (nameIds[i] == 4) {
                    nameRecord->offset = newFamilyNameSize;
                    nameRecord->length = regularStringSize;
                }
            }

            // nameRecord now points to string data
            BigEndian<quint16> *stringStorage = reinterpret_cast<BigEndian<quint16> *>(nameRecord);
            const quint16 *sourceString = newFamilyName.utf16();
            for (int i=0; i<newFamilyName.size(); ++i)
                stringStorage[i] = sourceString[i];
            stringStorage += newFamilyName.size();

            sourceString = regularString.utf16();
            for (int i=0; i<regularString.size(); ++i)
                stringStorage[i] = sourceString[i];
        }

        quint32 *p = reinterpret_cast<quint32 *>(newNameTable.data());
        quint32 *tableEnd = reinterpret_cast<quint32 *>(newNameTable.data() + fullSize);

        quint32 checkSum = 0;
        while (p < tableEnd)
            checkSum += *(p++);

        nameTableDirectoryEntry->checkSum = checkSum;
        nameTableDirectoryEntry->offset = m_fontData.size();
        nameTableDirectoryEntry->length = fullSize;

        m_fontData.append(newNameTable);

        return oldFamilyName;
    }

#if !defined(QT_NO_DIRECTWRITE)

    class DirectWriteFontFileStream: public IDWriteFontFileStream
    {
    public:
        DirectWriteFontFileStream(const QByteArray &fontData)
            : m_fontData(fontData)
            , m_referenceCount(0)
        {
        }

        ~DirectWriteFontFileStream()
        {
        }

        HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void **object);
        ULONG STDMETHODCALLTYPE AddRef();
        ULONG STDMETHODCALLTYPE Release();

        HRESULT STDMETHODCALLTYPE ReadFileFragment(const void **fragmentStart, UINT64 fileOffset,
                                                   UINT64 fragmentSize, OUT void **fragmentContext);
        void STDMETHODCALLTYPE ReleaseFileFragment(void *fragmentContext);
        HRESULT STDMETHODCALLTYPE GetFileSize(OUT UINT64 *fileSize);
        HRESULT STDMETHODCALLTYPE GetLastWriteTime(OUT UINT64 *lastWriteTime);

    private:
        QByteArray m_fontData;
        ULONG m_referenceCount;
    };

    HRESULT STDMETHODCALLTYPE DirectWriteFontFileStream::QueryInterface(REFIID iid, void **object)
    {
        if (iid == IID_IUnknown || iid == __uuidof(IDWriteFontFileStream)) {
            *object = this;
            AddRef();
            return S_OK;
        } else {
            *object = NULL;
            return E_NOINTERFACE;
        }
    }

    ULONG STDMETHODCALLTYPE DirectWriteFontFileStream::AddRef()
    {
        return InterlockedIncrement(&m_referenceCount);
    }

    ULONG STDMETHODCALLTYPE DirectWriteFontFileStream::Release()
    {
        ULONG newCount = InterlockedDecrement(&m_referenceCount);
        if (newCount == 0)
            delete this;
        return newCount;
    }

    HRESULT STDMETHODCALLTYPE DirectWriteFontFileStream::ReadFileFragment(
        const void **fragmentStart,
        UINT64 fileOffset,
        UINT64 fragmentSize,
        OUT void **fragmentContext)
    {
        *fragmentContext = NULL;
        if (fragmentSize + fileOffset <= m_fontData.size()) {
            *fragmentStart = m_fontData.data() + fileOffset;
            return S_OK;
        } else {
            *fragmentStart = NULL;
            return E_FAIL;
        }
    }

    void STDMETHODCALLTYPE DirectWriteFontFileStream::ReleaseFileFragment(void *)
    {
    }

    HRESULT STDMETHODCALLTYPE DirectWriteFontFileStream::GetFileSize(UINT64 *fileSize)
    {
        *fileSize = m_fontData.size();
        return S_OK;
    }

    HRESULT STDMETHODCALLTYPE DirectWriteFontFileStream::GetLastWriteTime(UINT64 *lastWriteTime)
    {
        *lastWriteTime = 0;
        return E_NOTIMPL;
    }

    class DirectWriteFontFileLoader: public IDWriteFontFileLoader
    {
    public:
        DirectWriteFontFileLoader() : m_referenceCount(0) {}

        ~DirectWriteFontFileLoader()
        {
        }

        inline void addKey(const void *key, const QByteArray &fontData)
        {
            Q_ASSERT(!m_fontDatas.contains(key));
            m_fontDatas.insert(key, fontData);
        }

        inline void removeKey(const void *key)
        {
            m_fontDatas.remove(key);
        }

        HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void **object);
        ULONG STDMETHODCALLTYPE AddRef();
        ULONG STDMETHODCALLTYPE Release();

        HRESULT STDMETHODCALLTYPE CreateStreamFromKey(void const *fontFileReferenceKey,
                                                      UINT32 fontFileReferenceKeySize,
                                                      OUT IDWriteFontFileStream **fontFileStream);

    private:
        ULONG m_referenceCount;
        QHash<const void *, QByteArray> m_fontDatas;
    };

    HRESULT STDMETHODCALLTYPE DirectWriteFontFileLoader::QueryInterface(const IID &iid,
                                                                        void **object)
    {
        if (iid == IID_IUnknown || iid == __uuidof(IDWriteFontFileLoader)) {
            *object = this;
            AddRef();
            return S_OK;
        } else {
            *object = NULL;
            return E_NOINTERFACE;
        }
    }

    ULONG STDMETHODCALLTYPE DirectWriteFontFileLoader::AddRef()
    {
        return InterlockedIncrement(&m_referenceCount);
    }

    ULONG STDMETHODCALLTYPE DirectWriteFontFileLoader::Release()
    {
        ULONG newCount = InterlockedDecrement(&m_referenceCount);
        if (newCount == 0)
            delete this;
        return newCount;
    }

    HRESULT STDMETHODCALLTYPE DirectWriteFontFileLoader::CreateStreamFromKey(
        void const *fontFileReferenceKey,
        UINT32 fontFileReferenceKeySize,
        IDWriteFontFileStream **fontFileStream)
    {
        Q_UNUSED(fontFileReferenceKeySize);

        if (fontFileReferenceKeySize != sizeof(const void *)) {
            qWarning("DirectWriteFontFileLoader::CreateStreamFromKey: Wrong key size");
            return E_FAIL;
        }

        const void *key = *reinterpret_cast<void * const *>(fontFileReferenceKey);
        *fontFileStream = NULL;
        if (!m_fontDatas.contains(key))
            return E_FAIL;

        QByteArray fontData = m_fontDatas.value(key);
        DirectWriteFontFileStream *stream = new DirectWriteFontFileStream(fontData);
        stream->AddRef();
        *fontFileStream = stream;

        return S_OK;
    }

    class CustomFontFileLoader
    {
    public:
        CustomFontFileLoader() : m_directWriteFactory(0), m_directWriteFontFileLoader(0)
        {
            HRESULT hres = DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED,
                                               __uuidof(IDWriteFactory),
                                               reinterpret_cast<IUnknown **>(&m_directWriteFactory));
            if (FAILED(hres)) {
                qErrnoWarning(hres, "CustomFontFileLoader::CustomFontFileLoader: "
                                    "DWriteCreateFactory failed.");
            } else {
                m_directWriteFontFileLoader = new DirectWriteFontFileLoader();
                m_directWriteFactory->RegisterFontFileLoader(m_directWriteFontFileLoader);
            }
        }

        ~CustomFontFileLoader()
        {
            if (m_directWriteFactory != 0 && m_directWriteFontFileLoader != 0)
                m_directWriteFactory->UnregisterFontFileLoader(m_directWriteFontFileLoader);

            if (m_directWriteFactory != 0)
                m_directWriteFactory->Release();
        }

        void addKey(const void *key, const QByteArray &fontData)
        {
            if (m_directWriteFontFileLoader != 0)
                m_directWriteFontFileLoader->addKey(key, fontData);
        }

        void removeKey(const void *key)
        {
            if (m_directWriteFontFileLoader != 0)
                m_directWriteFontFileLoader->removeKey(key);
        }

        IDWriteFontFileLoader *loader() const
        {
            return m_directWriteFontFileLoader;
        }

    private:
        IDWriteFactory *m_directWriteFactory;
        DirectWriteFontFileLoader *m_directWriteFontFileLoader;
    };

#endif

} // Anonymous namespace


// From qfontdatabase_win.cpp
extern QFontEngine *qt_load_font_engine_win(const QFontDef &request);
// From qfontdatabase.cpp
extern QFont::Weight weightFromInteger(int weight);

void QRawFontPrivate::platformCleanUp()
{
    if (fontHandle != NULL) {
        if (ptrRemoveFontMemResourceEx == NULL) {
            void *func = QSystemLibrary::resolve(QLatin1String("gdi32"), "RemoveFontMemResourceEx");
            ptrRemoveFontMemResourceEx =
                    reinterpret_cast<QRawFontPrivate::PtrRemoveFontMemResourceEx>(func);
        }

        if (ptrRemoveFontMemResourceEx == NULL) {
            qWarning("QRawFont::platformCleanUp: Can't find RemoveFontMemResourceEx in gdi32");
            fontHandle = NULL;
        } else {
            ptrRemoveFontMemResourceEx(fontHandle);
            fontHandle = NULL;
        }
    }
}

void QRawFontPrivate::platformLoadFromData(const QByteArray &_fontData,
                                           int pixelSize,
                                           QFont::HintingPreference hintingPreference)
{
    QByteArray fontData(_fontData);
    EmbeddedFont font(fontData);

#if !defined(QT_NO_DIRECTWRITE)
    if (hintingPreference == QFont::PreferDefaultHinting
     || hintingPreference == QFont::PreferFullHinting)
#endif
    {
        GUID guid;
        CoCreateGuid(&guid);

        QString uniqueFamilyName = QString::fromLatin1("f")
                + QString::number(guid.Data1, 36) + QLatin1Char('-')
                + QString::number(guid.Data2, 36) + QLatin1Char('-')
                + QString::number(guid.Data3, 36) + QLatin1Char('-')
                + QString::number(*reinterpret_cast<quint64 *>(guid.Data4), 36);

        QString actualFontName = font.changeFamilyName(uniqueFamilyName);
        if (actualFontName.isEmpty()) {
            qWarning("QRawFont::platformLoadFromData: Can't change family name of font");
            return;
        }

        if (ptrAddFontMemResourceEx == NULL || ptrRemoveFontMemResourceEx == NULL) {
            void *func = QSystemLibrary::resolve(QLatin1String("gdi32"), "RemoveFontMemResourceEx");
            ptrRemoveFontMemResourceEx =
                    reinterpret_cast<QRawFontPrivate::PtrRemoveFontMemResourceEx>(func);

            func = QSystemLibrary::resolve(QLatin1String("gdi32"), "AddFontMemResourceEx");
            ptrAddFontMemResourceEx =
                    reinterpret_cast<QRawFontPrivate::PtrAddFontMemResourceEx>(func);
        }

        Q_ASSERT(fontHandle == NULL);
        if (ptrAddFontMemResourceEx != NULL && ptrRemoveFontMemResourceEx != NULL) {
            DWORD count = 0;
            fontData = font.data();
            fontHandle = ptrAddFontMemResourceEx(fontData.data(), fontData.size(), 0, &count);

            if (count == 0 && fontHandle != NULL) {
                ptrRemoveFontMemResourceEx(fontHandle);
                fontHandle = NULL;
            }
        }

        if (fontHandle == NULL) {
            qWarning("QRawFont::platformLoadFromData: AddFontMemResourceEx failed");
        } else {
            QFontDef request;
            request.family = uniqueFamilyName;
            request.pixelSize = pixelSize;
            request.styleStrategy = QFont::NoFontMerging | QFont::PreferMatch;
            request.hintingPreference = hintingPreference;

            fontEngine = qt_load_font_engine_win(request);
            if (request.family != fontEngine->fontDef.family) {
                qWarning("QRawFont::platformLoadFromData: Failed to load font. "
                         "Got fallback instead: %s", qPrintable(fontEngine->fontDef.family));
                if (fontEngine->cache_count == 0 && fontEngine->ref == 0)
                    delete fontEngine;
                fontEngine = 0;
            } else {
                Q_ASSERT(fontEngine->cache_count == 0 && fontEngine->ref == 0);

                // Override the generated font name
                static_cast<QFontEngineWin *>(fontEngine)->uniqueFamilyName = uniqueFamilyName;
                fontEngine->fontDef.family = actualFontName;
                fontEngine->ref.ref();
            }
        }
    }
#if !defined(QT_NO_DIRECTWRITE)
    else {
        CustomFontFileLoader fontFileLoader;
        fontFileLoader.addKey(this, fontData);

        IDWriteFactory *factory = NULL;
        HRESULT hres = DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED,
                                           __uuidof(IDWriteFactory),
                                           reinterpret_cast<IUnknown **>(&factory));
        if (FAILED(hres)) {
            qErrnoWarning(hres, "QRawFont::platformLoadFromData: DWriteCreateFactory failed");
            return;
        }

        IDWriteFontFile *fontFile = NULL;
        void *key = this;

        hres = factory->CreateCustomFontFileReference(&key, sizeof(void *),
                                                      fontFileLoader.loader(), &fontFile);
        if (FAILED(hres)) {
            qErrnoWarning(hres, "QRawFont::platformLoadFromData: "
                                "CreateCustomFontFileReference failed");
            factory->Release();
            return;
        }

        BOOL isSupportedFontType;
        DWRITE_FONT_FILE_TYPE fontFileType;
        DWRITE_FONT_FACE_TYPE fontFaceType;
        UINT32 numberOfFaces;
        fontFile->Analyze(&isSupportedFontType, &fontFileType, &fontFaceType, &numberOfFaces);
        if (!isSupportedFontType) {
            fontFile->Release();
            factory->Release();
            return;
        }

        IDWriteFontFace *directWriteFontFace = NULL;
        hres = factory->CreateFontFace(fontFaceType, 1, &fontFile, 0, DWRITE_FONT_SIMULATIONS_NONE,
                                       &directWriteFontFace);
        if (FAILED(hres)) {
            qErrnoWarning(hres, "QRawFont::platformLoadFromData: CreateFontFace failed");
            fontFile->Release();
            factory->Release();
            return;
        }

        fontFile->Release();

        fontEngine = new QFontEngineDirectWrite(factory, directWriteFontFace, pixelSize);

        // Get font family from font data
        fontEngine->fontDef.family = font.familyName();
        fontEngine->ref.ref();

        directWriteFontFace->Release();
        factory->Release();
    }
#endif

    // Get style and weight info
    if (fontEngine != 0) {
        TableDirectory *os2TableEntry = font.tableDirectoryEntry("OS/2");
        if (os2TableEntry != 0) {
            const OS2Table *os2Table =
                    reinterpret_cast<const OS2Table *>(fontData.constData()
                                                       + os2TableEntry->offset);

            bool italic = os2Table->selection & 1;
            bool oblique = os2Table->selection & 128;

            if (italic)
                fontEngine->fontDef.style = QFont::StyleItalic;
            else if (oblique)
                fontEngine->fontDef.style = QFont::StyleOblique;
            else
                fontEngine->fontDef.style = QFont::StyleNormal;

            fontEngine->fontDef.weight = weightFromInteger(os2Table->weightClass);
        }
    }
}

QT_END_NAMESPACE

#endif // QT_NO_RAWFONT