Fix uses of qRound on non-floating-point types.
[profile/ivi/qtbase.git] / src / gui / text / qfontengine_qpf.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtGui module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qfontengine_qpf_p.h"
43
44 #include "private/qpaintengine_raster_p.h"
45 #include <QtCore/qlibraryinfo.h>
46 #include <QtCore/qfileinfo.h>
47
48 #include <QtCore/qfile.h>
49 #include <QtCore/qdir.h>
50 #include <QtCore/qbuffer.h>
51 #if !defined(QT_NO_FREETYPE)
52 #include "private/qfontengine_ft_p.h"
53 #endif
54 #include "private/qcore_unix_p.h" // overrides QT_OPEN
55
56 // for mmap
57 #include <stdlib.h>
58 #include <unistd.h>
59 #include <sys/types.h>
60 #include <sys/stat.h>
61 #include <sys/mman.h>
62 #include <fcntl.h>
63 #include <errno.h>
64
65 QT_BEGIN_NAMESPACE
66
67 #ifndef QT_NO_QWS_QPF2
68
69 #include "qpfutil.cpp"
70
71 QT_BEGIN_INCLUDE_NAMESPACE
72
73 #if defined(Q_WS_QWS)
74 #   include "private/qwscommand_qws_p.h"
75 #   include "qwsdisplay_qws.h"
76 #   include "qabstractfontengine_p.h"
77 #endif
78 #include "qplatformdefs.h"
79 QT_END_INCLUDE_NAMESPACE
80
81 //#define DEBUG_HEADER
82 //#define DEBUG_FONTENGINE
83
84 #if defined(DEBUG_HEADER)
85 # define DEBUG_VERIFY qDebug
86 #else
87 # define DEBUG_VERIFY if (0) qDebug
88 #endif
89
90 #define READ_VERIFY(type, variable) \
91     if (tagPtr + sizeof(type) > endPtr) { \
92         DEBUG_VERIFY() << "read verify failed in line" << __LINE__; \
93         return 0; \
94     } \
95     variable = qFromBigEndian<type>(tagPtr); \
96     DEBUG_VERIFY() << "read value" << variable << "of type " #type; \
97     tagPtr += sizeof(type)
98
99 template <typename T>
100 T readValue(const uchar *&data)
101 {
102     T value = qFromBigEndian<T>(data);
103     data += sizeof(T);
104     return value;
105 }
106
107 #define VERIFY(condition) \
108     if (!(condition)) { \
109         DEBUG_VERIFY() << "condition " #condition " failed in line" << __LINE__; \
110         return 0; \
111     }
112
113 #define VERIFY_TAG(condition) \
114     if (!(condition)) { \
115         DEBUG_VERIFY() << "verifying tag condition " #condition " failed in line" << __LINE__ << "with tag" << tag; \
116         return 0; \
117     }
118
119 static inline const uchar *verifyTag(const uchar *tagPtr, const uchar *endPtr)
120 {
121     quint16 tag, length;
122     READ_VERIFY(quint16, tag);
123     READ_VERIFY(quint16, length);
124     if (tag == QFontEngineQPF::Tag_EndOfHeader)
125         return endPtr;
126     if (tag < QFontEngineQPF::NumTags) {
127         switch (tagTypes[tag]) {
128             case QFontEngineQPF::BitFieldType:
129             case QFontEngineQPF::StringType:
130                 // can't do anything...
131                 break;
132             case QFontEngineQPF::UInt32Type:
133                 VERIFY_TAG(length == sizeof(quint32));
134                 break;
135             case QFontEngineQPF::FixedType:
136                 VERIFY_TAG(length == sizeof(quint32));
137                 break;
138             case QFontEngineQPF::UInt8Type:
139                 VERIFY_TAG(length == sizeof(quint8));
140                 break;
141         }
142 #if defined(DEBUG_HEADER)
143         if (length == 1)
144             qDebug() << "tag data" << hex << *tagPtr;
145         else if (length == 4)
146             qDebug() << "tag data" << hex << tagPtr[0] << tagPtr[1] << tagPtr[2] << tagPtr[3];
147 #endif
148     }
149     return tagPtr + length;
150 }
151
152 const QFontEngineQPF::Glyph *QFontEngineQPF::findGlyph(glyph_t g) const
153 {
154     if (!g || g >= glyphMapEntries)
155         return 0;
156     const quint32 *gmapPtr = reinterpret_cast<const quint32 *>(fontData + glyphMapOffset);
157     quint32 glyphPos = qFromBigEndian<quint32>(gmapPtr[g]);
158     if (glyphPos > glyphDataSize) {
159         if (glyphPos == 0xffffffff)
160             return 0;
161 #if defined(DEBUG_FONTENGINE)
162         qDebug() << "glyph" << g << "outside of glyphData, remapping font file";
163 #endif
164 #if !defined(QT_NO_FREETYPE) && !defined(QT_FONTS_ARE_RESOURCES)
165         const_cast<QFontEngineQPF *>(this)->remapFontData();
166 #endif
167         if (glyphPos > glyphDataSize)
168             return 0;
169     }
170     return reinterpret_cast<const Glyph *>(fontData + glyphDataOffset + glyphPos);
171 }
172
173 bool QFontEngineQPF::verifyHeader(const uchar *data, int size)
174 {
175     VERIFY(size >= int(sizeof(Header)));
176     const Header *header = reinterpret_cast<const Header *>(data);
177     if (header->magic[0] != 'Q'
178         || header->magic[1] != 'P'
179         || header->magic[2] != 'F'
180         || header->magic[3] != '2')
181         return false;
182
183     VERIFY(header->majorVersion <= CurrentMajorVersion);
184     const quint16 dataSize = qFromBigEndian<quint16>(header->dataSize);
185     VERIFY(size >= int(sizeof(Header)) + dataSize);
186
187     const uchar *tagPtr = data + sizeof(Header);
188     const uchar *tagEndPtr = tagPtr + dataSize;
189     while (tagPtr < tagEndPtr - 3) {
190         tagPtr = verifyTag(tagPtr, tagEndPtr);
191         VERIFY(tagPtr);
192     }
193
194     VERIFY(tagPtr <= tagEndPtr);
195     return true;
196 }
197
198 QVariant QFontEngineQPF::extractHeaderField(const uchar *data, HeaderTag requestedTag)
199 {
200     const Header *header = reinterpret_cast<const Header *>(data);
201     const uchar *tagPtr = data + sizeof(Header);
202     const uchar *endPtr = tagPtr + qFromBigEndian<quint16>(header->dataSize);
203     while (tagPtr < endPtr - 3) {
204         quint16 tag = readValue<quint16>(tagPtr);
205         quint16 length = readValue<quint16>(tagPtr);
206         if (tag == requestedTag) {
207             switch (tagTypes[requestedTag]) {
208                 case StringType:
209                     return QVariant(QString::fromUtf8(reinterpret_cast<const char *>(tagPtr), length));
210                 case UInt32Type:
211                     return QVariant(readValue<quint32>(tagPtr));
212                 case UInt8Type:
213                     return QVariant(uint(*tagPtr));
214                 case FixedType:
215                     return QVariant(QFixed::fromFixed(readValue<quint32>(tagPtr)).toReal());
216                 case BitFieldType:
217                     return QVariant(QByteArray(reinterpret_cast<const char *>(tagPtr), length));
218             }
219             return QVariant();
220         } else if (tag == Tag_EndOfHeader) {
221             break;
222         }
223         tagPtr += length;
224     }
225
226     return QVariant();
227 }
228
229 #endif // QT_NO_QWS_QPF2
230
231 QString qws_fontCacheDir()
232 {
233     QString dir;
234 #if defined(Q_WS_QWS)
235     extern QString qws_dataDir();
236     dir = qws_dataDir();
237 #else
238     dir = QDir::tempPath();
239 #endif
240     dir.append(QLatin1String("/fonts/"));
241     QDir qd(dir);
242     if (!qd.exists() && !qd.mkpath(dir))
243         dir = QDir::tempPath();
244     return dir;
245 }
246
247 #ifndef QT_NO_QWS_QPF2
248
249 #ifndef QT_FONTS_ARE_RESOURCES
250 QList<QByteArray> QFontEngineQPF::cleanUpAfterClientCrash(const QList<int> &crashedClientIds)
251 {
252     QList<QByteArray> removedFonts;
253     QDir dir(qws_fontCacheDir(), QLatin1String("*.qsf"));
254     for (int i = 0; i < int(dir.count()); ++i) {
255         const QByteArray fileName = QFile::encodeName(dir.absoluteFilePath(dir[i]));
256
257         int fd = QT_OPEN(fileName.constData(), O_RDONLY, 0);
258         if (fd >= 0) {
259             void *header = ::mmap(0, sizeof(QFontEngineQPF::Header), PROT_READ, MAP_SHARED, fd, 0);
260             if (header && header != MAP_FAILED) {
261                 quint32 lockValue = reinterpret_cast<QFontEngineQPF::Header *>(header)->lock;
262
263                 if (lockValue && crashedClientIds.contains(lockValue)) {
264                     removedFonts.append(fileName);
265                     QFile::remove(QFile::decodeName(fileName));
266                 }
267
268                 ::munmap(header, sizeof(QFontEngineQPF::Header));
269             }
270             QT_CLOSE(fd);
271         }
272     }
273     if (!removedFonts.isEmpty())
274         qDebug() << "list of corrupted and removed fonts:" << removedFonts;
275     return removedFonts;
276 }
277 #endif
278
279 static inline unsigned int getChar(const QChar *str, int &i, const int len)
280 {
281     unsigned int uc = str[i].unicode();
282     if (uc >= 0xd800 && uc < 0xdc00 && i < len-1) {
283         uint low = str[i+1].unicode();
284        if (low >= 0xdc00 && low < 0xe000) {
285             uc = (uc - 0xd800)*0x400 + (low - 0xdc00) + 0x10000;
286             ++i;
287         }
288     }
289     return uc;
290 }
291 #ifdef QT_FONTS_ARE_RESOURCES
292 QFontEngineQPF::QFontEngineQPF(const QFontDef &def, const uchar *bytes, int size)
293     : fd(-1), fontData(bytes), dataSize(size), renderingFontEngine(0)
294 #else
295 QFontEngineQPF::QFontEngineQPF(const QFontDef &def, int fileDescriptor, QFontEngine *fontEngine)
296     : fd(fileDescriptor), fontData(0), dataSize(0), renderingFontEngine(fontEngine)
297 #endif
298 {
299     fontDef = def;
300     cache_cost = 100;
301     freetype = 0;
302     externalCMap = 0;
303     cmapOffset = 0;
304     cmapSize = 0;
305     glyphMapOffset = 0;
306     glyphMapEntries = 0;
307     glyphDataOffset = 0;
308     glyphDataSize = 0;
309     if (renderingFontEngine)
310         glyphFormat = renderingFontEngine->glyphFormat;
311     kerning_pairs_loaded = false;
312     readOnly = true;
313
314 #if defined(DEBUG_FONTENGINE)
315     qDebug() << "QFontEngineQPF::QFontEngineQPF( fd =" << fd << ", renderingFontEngine =" << renderingFontEngine << ')';
316 #endif
317
318 #ifndef QT_FONTS_ARE_RESOURCES
319     if (fd < 0) {
320         if (!renderingFontEngine)
321             return;
322
323         fileName = fontDef.family.toLower() + QLatin1Char('_')
324                    + QString::number(fontDef.pixelSize)
325                    + QLatin1Char('_') + QString::number(fontDef.weight)
326                    + (fontDef.style != QFont::StyleNormal ?
327                       QLatin1String("_italic") : QLatin1String(""))
328                    + QLatin1String(".qsf");
329         fileName.replace(QLatin1Char(' '), QLatin1Char('_'));
330         fileName.prepend(qws_fontCacheDir());
331
332         encodedFileName = QFile::encodeName(fileName);
333         if (::access(encodedFileName, F_OK) == 0) {
334 #if defined(DEBUG_FONTENGINE)
335             qDebug() << "found existing qpf:" << fileName;
336 #endif
337             if (::access(encodedFileName, W_OK | R_OK) == 0) {
338                 fd = QT_OPEN(encodedFileName, O_RDWR);
339             }
340             // read-write access failed - try read-only access
341             if (fd == -1 && ::access(encodedFileName, R_OK) == 0) {
342                 fd = QT_OPEN(encodedFileName, O_RDONLY);
343                 if (fd == -1) {
344 #if defined(DEBUG_FONTENGINE)
345                     qErrnoWarning("QFontEngineQPF: unable to open %s", encodedName.constData());
346 #endif
347                     return;
348                 }
349             }
350             if (fd == -1) {
351 #if defined(DEBUG_FONTENGINE)
352                 qWarning("QFontEngineQPF: insufficient access rights to %s", encodedName.constData());
353 #endif
354                 return;
355             }
356         } else {
357 #if defined(DEBUG_FONTENGINE)
358             qDebug() << "creating qpf on the fly:" << fileName;
359 #endif
360             if (::access(QFile::encodeName(qws_fontCacheDir()), W_OK) == 0) {
361                 fd = QT_OPEN(encodedFileName, O_RDWR | O_EXCL | O_CREAT, 0644);
362                 if (fd == -1) {
363 #if defined(DEBUG_FONTENGINE)
364                     qErrnoWarning(errno, "QFontEngineQPF: open() failed for %s", encodedName.constData());
365 #endif
366                     return;
367                 }
368
369                 QBuffer buffer;
370                 buffer.open(QIODevice::ReadWrite);
371                 QPFGenerator generator(&buffer, renderingFontEngine);
372                 generator.generate();
373                 buffer.close();
374                 const QByteArray &data = buffer.data();
375                 if (QT_WRITE(fd, data.constData(), data.size()) == -1) {
376 #if defined(DEBUG_FONTENGINE)
377                     qErrnoWarning(errno, "QFontEngineQPF: write() failed for %s", encodedName.constData());
378 #endif
379                     return;
380                 }
381             } else {
382 #if defined(DEBUG_FONTENGINE)
383                 qErrnoWarning(errno, "QFontEngineQPF: access() failed for %s", qPrintable(qws_fontCacheDir()));
384 #endif
385                 return;
386             }
387         }
388     }
389
390     QT_STATBUF st;
391     if (QT_FSTAT(fd, &st)) {
392 #if defined(DEBUG_FONTENGINE)
393         qErrnoWarning(errno, "QFontEngineQPF: fstat failed!");
394 #endif
395         return;
396     }
397     dataSize = st.st_size;
398
399
400     fontData = (const uchar *)::mmap(0, st.st_size, PROT_READ | (renderingFontEngine ? PROT_WRITE : 0), MAP_SHARED, fd, 0);
401     if (!fontData || fontData == (const uchar *)MAP_FAILED) {
402 #if defined(DEBUG_FONTENGINE)
403         perror("mmap failed");
404 #endif
405         fontData = 0;
406         return;
407     }
408 #endif //QT_FONTS_ARE_RESOURCES
409
410     if (!verifyHeader(fontData, dataSize)) {
411 #if defined(DEBUG_FONTENGINE)
412         qDebug() << "verifyHeader failed!";
413 #endif
414         return;
415     }
416
417     const Header *header = reinterpret_cast<const Header *>(fontData);
418
419     readOnly = (header->lock == 0xffffffff);
420
421     const uchar *data = fontData + sizeof(Header) + qFromBigEndian<quint16>(header->dataSize);
422     const uchar *endPtr = fontData + dataSize;
423     while (data <= endPtr - 8) {
424         quint16 blockTag = readValue<quint16>(data);
425         data += 2; // skip padding
426         quint32 blockSize = readValue<quint32>(data);
427
428         if (blockTag == CMapBlock) {
429             cmapOffset = data - fontData;
430             cmapSize = blockSize;
431         } else if (blockTag == GMapBlock) {
432             glyphMapOffset = data - fontData;
433             glyphMapEntries = blockSize / 4;
434         } else if (blockTag == GlyphBlock) {
435             glyphDataOffset = data - fontData;
436             glyphDataSize = blockSize;
437         }
438
439         data += blockSize;
440     }
441
442     face_id.filename = QFile::encodeName(extractHeaderField(fontData, Tag_FileName).toString());
443     face_id.index = extractHeaderField(fontData, Tag_FileIndex).toInt();
444 #if !defined(QT_NO_FREETYPE) && !defined(QT_FONTS_ARE_RESOURCES)
445     freetype = QFreetypeFace::getFace(face_id);
446     if (!freetype) {
447         QString newPath =
448 #ifndef QT_NO_SETTINGS
449             QLibraryInfo::location(QLibraryInfo::LibrariesPath) +
450 #endif
451                           QLatin1String("/fonts/") +
452                           QFileInfo(QFile::decodeName(face_id.filename)).fileName();
453         face_id.filename = QFile::encodeName(newPath);
454         freetype = QFreetypeFace::getFace(face_id);
455     }
456     if (freetype) {
457         const quint32 qpfTtfRevision = extractHeaderField(fontData, Tag_FontRevision).toUInt();
458         uchar data[4];
459         uint length = 4;
460         bool ok = freetype->getSfntTable(MAKE_TAG('h', 'e', 'a', 'd'), data, &length);
461         if (!ok || length != 4
462             || qFromBigEndian<quint32>(data) != qpfTtfRevision) {
463             freetype->release(face_id);
464             freetype = 0;
465         }
466     }
467     if (!cmapOffset && freetype) {
468         freetypeCMapTable = getSfntTable(MAKE_TAG('c', 'm', 'a', 'p'));
469         externalCMap = reinterpret_cast<const uchar *>(freetypeCMapTable.constData());
470         cmapSize = freetypeCMapTable.size();
471     }
472 #endif
473
474     // get the real cmap
475     if (cmapOffset) {
476         int tableSize = cmapSize;
477         const uchar *cmapPtr = getCMap(fontData + cmapOffset, tableSize, &symbol, &cmapSize);
478         if (cmapPtr)
479             cmapOffset = cmapPtr - fontData;
480         else
481             cmapOffset = 0;
482     } else if (externalCMap) {
483         int tableSize = cmapSize;
484         externalCMap = getCMap(externalCMap, tableSize, &symbol, &cmapSize);
485     }
486
487     // verify all the positions in the glyphMap
488     if (glyphMapOffset) {
489         const quint32 *gmapPtr = reinterpret_cast<const quint32 *>(fontData + glyphMapOffset);
490         for (uint i = 0; i < glyphMapEntries; ++i) {
491             quint32 glyphDataPos = qFromBigEndian<quint32>(gmapPtr[i]);
492             if (glyphDataPos == 0xffffffff)
493                 continue;
494             if (glyphDataPos >= glyphDataSize) {
495                 // error
496                 glyphMapOffset = 0;
497                 glyphMapEntries = 0;
498                 break;
499             }
500         }
501     }
502
503 #if defined(DEBUG_FONTENGINE)
504     if (!isValid())
505         qDebug() << "fontData" <<  fontData << "dataSize" << dataSize
506                  << "externalCMap" << externalCMap << "cmapOffset" << cmapOffset
507                  << "glyphMapOffset" << glyphMapOffset << "glyphDataOffset" << glyphDataOffset
508                  << "fd" << fd << "glyphDataSize" << glyphDataSize;
509 #endif
510 #if defined(Q_WS_QWS)
511     if (isValid() && renderingFontEngine)
512         qt_fbdpy->sendFontCommand(QWSFontCommand::StartedUsingFont, encodedFileName);
513 #endif
514 }
515
516 QFontEngineQPF::~QFontEngineQPF()
517 {
518 #if defined(Q_WS_QWS)
519     if (isValid() && renderingFontEngine) {
520         QT_TRY {
521             qt_fbdpy->sendFontCommand(QWSFontCommand::StoppedUsingFont, encodedFileName);
522         } QT_CATCH(...) {
523             qDebug("QFontEngineQPF::~QFontEngineQPF: Out of memory");
524             // ignore.
525         }
526     }
527 #endif
528     delete renderingFontEngine;
529     if (fontData) {
530         if (munmap((void *)fontData, dataSize) == -1) {
531 #if defined(DEBUG_FONTENGINE)
532             qErrnoWarning(errno, "~QFontEngineQPF: Unable to munmap");
533 #endif
534         }
535     }
536     if (fd != -1)
537         ::close(fd);
538 #if !defined(QT_NO_FREETYPE)
539     if (freetype)
540         freetype->release(face_id);
541 #endif
542 }
543
544 bool QFontEngineQPF::getSfntTableData(uint tag, uchar *buffer, uint *length) const
545 {
546 #if !defined(QT_NO_FREETYPE)
547     if (freetype)
548         return freetype->getSfntTable(tag, buffer, length);
549 #endif
550     Q_UNUSED(tag);
551     Q_UNUSED(buffer);
552     *length = 0;
553     return false;
554 }
555
556 bool QFontEngineQPF::stringToCMap(const QChar *str, int len, QGlyphLayout *glyphs, int *nglyphs, QTextEngine::ShaperFlags flags) const
557 {
558     if (!externalCMap && !cmapOffset && renderingFontEngine) {
559         if (!renderingFontEngine->stringToCMap(str, len, glyphs, nglyphs, flags))
560             return false;
561 #ifndef QT_NO_FREETYPE
562         const_cast<QFontEngineQPF *>(this)->ensureGlyphsLoaded(*glyphs);
563 #endif
564         return true;
565     }
566
567     if (*nglyphs < len) {
568         *nglyphs = len;
569         return false;
570     }
571
572 #if defined(DEBUG_FONTENGINE)
573     QSet<QChar> seenGlyphs;
574 #endif
575
576     const uchar *cmap = externalCMap ? externalCMap : (fontData + cmapOffset);
577
578     bool mirrored = flags & QTextEngine::RightToLeft;
579     int glyph_pos = 0;
580     if (symbol) {
581         for (int i = 0; i < len; ++i) {
582             unsigned int uc = getChar(str, i, len);
583             glyphs->glyphs[glyph_pos] = getTrueTypeGlyphIndex(cmap, uc);
584             if(!glyphs->glyphs[glyph_pos] && uc < 0x100)
585                 glyphs->glyphs[glyph_pos] = getTrueTypeGlyphIndex(cmap, uc + 0xf000);
586             ++glyph_pos;
587         }
588     } else {
589         for (int i = 0; i < len; ++i) {
590             unsigned int uc = getChar(str, i, len);
591             if (mirrored)
592                 uc = QChar::mirroredChar(uc);
593             glyphs->glyphs[glyph_pos] = getTrueTypeGlyphIndex(cmap, uc);
594 #if 0 && defined(DEBUG_FONTENGINE)
595             QChar c(uc);
596             if (!findGlyph(glyphs[glyph_pos].glyph) && !seenGlyphs.contains(c))
597                 qDebug() << "glyph for character" << c << '/' << hex << uc << "is" << dec << glyphs[glyph_pos].glyph;
598
599             seenGlyphs.insert(c);
600 #endif
601             ++glyph_pos;
602         }
603     }
604
605     *nglyphs = glyph_pos;
606     glyphs->numGlyphs = glyph_pos;
607     recalcAdvances(glyphs, flags);
608     return true;
609 }
610
611 void QFontEngineQPF::recalcAdvances(QGlyphLayout *glyphs, QTextEngine::ShaperFlags) const
612 {
613 #ifndef QT_NO_FREETYPE
614     const_cast<QFontEngineQPF *>(this)->ensureGlyphsLoaded(*glyphs);
615 #endif
616     for (int i = 0; i < glyphs->numGlyphs; ++i) {
617         const Glyph *g = findGlyph(glyphs->glyphs[i]);
618         if (!g) {
619             glyphs->glyphs[i] = 0;
620             continue;
621         }
622         glyphs->advances_x[i] = g->advance;
623         glyphs->advances_y[i] = 0;
624     }
625 }
626
627 QImage QFontEngineQPF::alphaMapForGlyph(glyph_t g)
628 {
629     const Glyph *glyph = findGlyph(g);
630     if (!glyph)
631         return QImage();
632
633     const uchar *bits = ((const uchar *) glyph) + sizeof(Glyph);
634
635     QImage image(glyph->width, glyph->height, QImage::Format_Indexed8);
636     for (int j=0; j<256; ++j)
637         image.setColor(j, qRgba(0, 0, 0, j));
638
639     for (int i=0; i<glyph->height; ++i) {
640         memcpy(image.scanLine(i), bits, glyph->bytesPerLine);
641         bits += glyph->bytesPerLine;
642     }
643     return image;
644 }
645
646 void QFontEngineQPF::draw(QPaintEngine *p, qreal _x, qreal _y, const QTextItemInt &si)
647 {
648     QPaintEngineState *pState = p->state;
649     QRasterPaintEngine *paintEngine = static_cast<QRasterPaintEngine*>(p);
650
651     QTransform matrix = pState->transform();
652     matrix.translate(_x, _y);
653     QFixed x = QFixed::fromReal(matrix.dx());
654     QFixed y = QFixed::fromReal(matrix.dy());
655
656     QVarLengthArray<QFixedPoint> positions;
657     QVarLengthArray<glyph_t> glyphs;
658     getGlyphPositions(si.glyphs, matrix, si.flags, glyphs, positions);
659     if (glyphs.size() == 0)
660         return;
661
662     for(int i = 0; i < glyphs.size(); i++) {
663         const Glyph *glyph = findGlyph(glyphs[i]);
664         if (!glyph)
665             continue;
666
667         const int depth = 8; //###
668
669         paintEngine->alphaPenBlt(reinterpret_cast<const uchar *>(glyph) + sizeof(Glyph), glyph->bytesPerLine, depth,
670                                      qRound(positions[i].x) + glyph->x,
671                                      qRound(positions[i].y) + glyph->y,
672                                      glyph->width, glyph->height);
673     }
674 }
675
676 void QFontEngineQPF::addOutlineToPath(qreal x, qreal y, const QGlyphLayout &glyphs, QPainterPath *path, QTextItem::RenderFlags flags)
677 {
678     if (renderingFontEngine &&
679         (renderingFontEngine->type() != QFontEngine::Proxy
680          || static_cast<QProxyFontEngine *>(renderingFontEngine)->capabilities() & QAbstractFontEngine::CanOutlineGlyphs)) {
681         renderingFontEngine->addOutlineToPath(x, y, glyphs, path, flags);
682         return;
683     }
684     addBitmapFontToPath(x, y, glyphs, path, flags);
685 }
686
687 glyph_metrics_t QFontEngineQPF::boundingBox(const QGlyphLayout &glyphs)
688 {
689 #ifndef QT_NO_FREETYPE
690     const_cast<QFontEngineQPF *>(this)->ensureGlyphsLoaded(glyphs);
691 #endif
692
693     glyph_metrics_t overall;
694     // initialize with line height, we get the same behaviour on all platforms
695     overall.y = -ascent();
696     overall.height = ascent() + descent() + 1;
697
698     QFixed ymax = 0;
699     QFixed xmax = 0;
700     for (int i = 0; i < glyphs.numGlyphs; i++) {
701         const Glyph *g = findGlyph(glyphs.glyphs[i]);
702         if (!g)
703             continue;
704
705         QFixed x = overall.xoff + glyphs.offsets[i].x + g->x;
706         QFixed y = overall.yoff + glyphs.offsets[i].y + g->y;
707         overall.x = qMin(overall.x, x);
708         overall.y = qMin(overall.y, y);
709         xmax = qMax(xmax, x + g->width);
710         ymax = qMax(ymax, y + g->height);
711         overall.xoff += g->advance;
712     }
713     overall.height = qMax(overall.height, ymax - overall.y);
714     overall.width = xmax - overall.x;
715
716     return overall;
717 }
718
719 glyph_metrics_t QFontEngineQPF::boundingBox(glyph_t glyph)
720 {
721 #ifndef QT_NO_FREETYPE
722     {
723         QGlyphLayoutArray<1> tmp;
724         tmp.glyphs[0] = glyph;
725         const_cast<QFontEngineQPF *>(this)->ensureGlyphsLoaded(tmp);
726     }
727 #endif
728     glyph_metrics_t overall;
729     const Glyph *g = findGlyph(glyph);
730     if (!g)
731         return overall;
732     overall.x = g->x;
733     overall.y = g->y;
734     overall.width = g->width;
735     overall.height = g->height;
736     overall.xoff = g->advance;
737     return overall;
738 }
739
740 QFixed QFontEngineQPF::ascent() const
741 {
742     return QFixed::fromReal(extractHeaderField(fontData, Tag_Ascent).value<qreal>());
743 }
744
745 QFixed QFontEngineQPF::descent() const
746 {
747     return QFixed::fromReal(extractHeaderField(fontData, Tag_Descent).value<qreal>());
748 }
749
750 QFixed QFontEngineQPF::leading() const
751 {
752     return QFixed::fromReal(extractHeaderField(fontData, Tag_Leading).value<qreal>());
753 }
754
755 qreal QFontEngineQPF::maxCharWidth() const
756 {
757     return extractHeaderField(fontData, Tag_MaxCharWidth).value<qreal>();
758 }
759
760 qreal QFontEngineQPF::minLeftBearing() const
761 {
762     return extractHeaderField(fontData, Tag_MinLeftBearing).value<qreal>();
763 }
764
765 qreal QFontEngineQPF::minRightBearing() const
766 {
767     return extractHeaderField(fontData, Tag_MinRightBearing).value<qreal>();
768 }
769
770 QFixed QFontEngineQPF::underlinePosition() const
771 {
772     return QFixed::fromReal(extractHeaderField(fontData, Tag_UnderlinePosition).value<qreal>());
773 }
774
775 QFixed QFontEngineQPF::lineThickness() const
776 {
777     return QFixed::fromReal(extractHeaderField(fontData, Tag_LineThickness).value<qreal>());
778 }
779
780 QFontEngine::Type QFontEngineQPF::type() const
781 {
782     return QFontEngine::QPF2;
783 }
784
785 bool QFontEngineQPF::canRender(const QChar *string, int len)
786 {
787     const uchar *cmap = externalCMap ? externalCMap : (fontData + cmapOffset);
788
789     if (symbol) {
790         for (int i = 0; i < len; ++i) {
791             unsigned int uc = getChar(string, i, len);
792             glyph_t g = getTrueTypeGlyphIndex(cmap, uc);
793             if(!g && uc < 0x100)
794                 g = getTrueTypeGlyphIndex(cmap, uc + 0xf000);
795             if (!g)
796                 return false;
797         }
798     } else {
799         for (int i = 0; i < len; ++i) {
800             unsigned int uc = getChar(string, i, len);
801             if (!getTrueTypeGlyphIndex(cmap, uc))
802                 return false;
803         }
804     }
805     return true;
806 }
807
808 bool QFontEngineQPF::isValid() const
809 {
810     return fontData && dataSize && (cmapOffset || externalCMap || renderingFontEngine)
811            && glyphMapOffset && glyphDataOffset && (fd >= 0 || glyphDataSize > 0);
812 }
813
814 #if !defined(QT_NO_FREETYPE)
815 FT_Face QFontEngineQPF::lockFace() const
816 {
817     Q_ASSERT(freetype);
818     freetype->lock();
819     FT_Face face = freetype->face;
820
821     // ### not perfect
822     const int ysize = qRound(fontDef.pixelSize * qreal(64));
823     const int xsize = ysize;
824
825     if (freetype->xsize != xsize || freetype->ysize != ysize) {
826         FT_Set_Char_Size(face, xsize, ysize, 0, 0);
827         freetype->xsize = xsize;
828         freetype->ysize = ysize;
829     }
830     FT_Matrix identityMatrix;
831     identityMatrix.xx = 0x10000;
832     identityMatrix.yy = 0x10000;
833     identityMatrix.xy = 0;
834     identityMatrix.yx = 0;
835     if (freetype->matrix.xx != identityMatrix.xx ||
836         freetype->matrix.yy != identityMatrix.yy ||
837         freetype->matrix.xy != identityMatrix.xy ||
838         freetype->matrix.yx != identityMatrix.yx) {
839         freetype->matrix = identityMatrix;
840         FT_Set_Transform(face, &freetype->matrix, 0);
841     }
842     return face;
843 }
844
845 void QFontEngineQPF::unlockFace() const
846 {
847     freetype->unlock();
848 }
849
850 void QFontEngineQPF::doKerning(QGlyphLayout *g, QTextEngine::ShaperFlags flags) const
851 {
852     if (!kerning_pairs_loaded) {
853         kerning_pairs_loaded = true;
854         if (freetype) {
855             lockFace();
856             if (freetype->face->size->metrics.x_ppem != 0) {
857                 QFixed scalingFactor(freetype->face->units_per_EM/freetype->face->size->metrics.x_ppem);
858                 unlockFace();
859                 const_cast<QFontEngineQPF *>(this)->loadKerningPairs(scalingFactor);
860             } else {
861                 unlockFace();
862             }
863         }
864     }
865     QFontEngine::doKerning(g, flags);
866 }
867
868 HB_Error QFontEngineQPF::getPointInOutline(HB_Glyph glyph, int flags, hb_uint32 point, HB_Fixed *xpos, HB_Fixed *ypos, hb_uint32 *nPoints)
869 {
870     if (!freetype)
871         return HB_Err_Not_Covered;
872     lockFace();
873     HB_Error result = freetype->getPointInOutline(glyph, flags, point, xpos, ypos, nPoints);
874     unlockFace();
875     return result;
876 }
877
878 QFixed QFontEngineQPF::emSquareSize() const
879 {
880     if (!freetype)
881         return QFontEngine::emSquareSize();
882     if (FT_IS_SCALABLE(freetype->face))
883         return freetype->face->units_per_EM;
884     else
885         return freetype->face->size->metrics.y_ppem;
886 }
887
888 void QFontEngineQPF::ensureGlyphsLoaded(const QGlyphLayout &glyphs)
889 {
890     if (readOnly)
891         return;
892     bool locked = false;
893     for (int i = 0; i < glyphs.numGlyphs; ++i) {
894         if (!glyphs.glyphs[i])
895             continue;
896         const Glyph *g = findGlyph(glyphs.glyphs[i]);
897         if (g)
898             continue;
899         if (!locked) {
900             if (!lockFile())
901                 return;
902             locked = true;
903             g = findGlyph(glyphs.glyphs[i]);
904             if (g)
905                 continue;
906         }
907         loadGlyph(glyphs.glyphs[i]);
908     }
909     if (locked) {
910         unlockFile();
911 #if defined(DEBUG_FONTENGINE)
912         qDebug() << "Finished rendering glyphs\n";
913 #endif
914     }
915 }
916
917 void QFontEngineQPF::loadGlyph(glyph_t glyph)
918 {
919     quint32 glyphPos = ~0;
920
921     if (!renderingFontEngine)
922         return;
923     QImage img = renderingFontEngine->alphaMapForGlyph(glyph);
924     if (img.format() != QImage::Format_Indexed8) {
925         bool mono = img.depth() == 1;
926         img = img.convertToFormat(QImage::Format_Indexed8);
927         if (mono) {
928             //### we know that 1 is opaque and 0 is transparent
929             uchar *byte = img.bits();
930             int count = img.byteCount();
931             while (count--)
932                 *byte++ *= 0xff;
933         }
934     }
935     glyph_metrics_t metrics = renderingFontEngine->boundingBox(glyph);
936     renderingFontEngine->removeGlyphFromCache(glyph);
937
938     off_t oldSize = ::lseek(fd, 0, SEEK_END);
939     if (oldSize == (off_t)-1)
940         return;
941
942     Glyph g;
943     g.width = img.width();
944     g.height = img.height();
945     g.bytesPerLine = img.bytesPerLine();
946     g.x = qRound(metrics.x);
947     g.y = qRound(metrics.y);
948     g.advance = qRound(metrics.xoff);
949
950     QT_WRITE(fd, &g, sizeof(g));
951     QT_WRITE(fd, img.bits(), img.byteCount());
952
953     glyphPos = oldSize - glyphDataOffset;
954 #if 0 && defined(DEBUG_FONTENGINE)
955     qDebug() << "glyphPos for new glyph" << glyph << "is" << glyphPos << "oldSize" << oldSize << "glyphDataOffset" << glyphDataOffset;
956 #endif
957
958     quint32 *gmap = (quint32 *)(fontData + glyphMapOffset);
959     gmap[glyph] = qToBigEndian(glyphPos);
960
961     glyphDataSize = glyphPos + sizeof(g) + img.byteCount();
962     quint32 *blockSizePtr = (quint32 *)(fontData + glyphDataOffset - 4);
963     *blockSizePtr = qToBigEndian(glyphDataSize);
964 }
965
966 bool QFontEngineQPF::lockFile()
967 {
968     // #### this does not handle the case when the process holding the
969     // lock hangs for some reason
970     struct flock lock;
971     lock.l_type = F_WRLCK;
972     lock.l_whence = SEEK_SET;
973     lock.l_start = 0;
974     lock.l_len = 0; // lock the whole file
975     while (fcntl(fd, F_SETLKW, &lock) != 0) {
976         if (errno == EINTR)
977             continue;
978         perror("locking qpf");
979         return false;
980     }
981     Header *header = (Header *)fontData;
982     if (header->lock) {
983         lock.l_type = F_UNLCK;
984         if (fcntl(fd, F_SETLK, &lock) != 0)
985             perror("unlocking possibly corrupt qpf");
986         return false;
987     }
988 #if defined(Q_WS_QWS)
989     extern int qws_client_id;
990     // qws_client_id == 0 means we're the server. in this case we just
991     // set the id to 1
992     header->lock = qws_client_id ? qws_client_id : 1;
993 #else
994     header->lock = 1;
995 #endif
996     return true;
997 }
998
999 void QFontEngineQPF::unlockFile()
1000 {
1001     ((Header *)fontData)->lock = 0;
1002
1003     struct flock lock;
1004     lock.l_type = F_UNLCK;
1005     lock.l_whence = SEEK_SET;
1006     lock.l_start = 0;
1007     lock.l_len = 0; // lock the whole file
1008     if (fcntl(fd, F_SETLK, &lock) != 0) {
1009         perror("unlocking qpf");
1010     }
1011
1012     remapFontData();
1013 }
1014
1015 void QFontEngineQPF::remapFontData()
1016 {
1017     off_t newFileSize = ::lseek(fd, 0, SEEK_END);
1018     if (newFileSize == (off_t)-1) {
1019 #ifdef DEBUG_FONTENGINE
1020         perror("QFontEngineQPF::remapFontData: lseek failed");
1021 #endif
1022         fontData = 0;
1023         return;
1024     }
1025
1026 #ifndef QT_NO_MREMAP
1027     fontData = static_cast<uchar *>(::mremap(const_cast<uchar *>(fontData), dataSize, newFileSize, MREMAP_MAYMOVE));
1028     if (!fontData || fontData == (const uchar *)MAP_FAILED) {
1029 #  if defined(DEBUG_FONTENGINE)
1030         perror("QFontEngineQPF::remapFontData(): mremap failed");
1031 #  endif
1032         fontData = 0;
1033     }
1034
1035     if (!fontData)
1036 #endif // QT_NO_MREMAP
1037     {
1038         int status = ::munmap((void *)fontData, dataSize);
1039         if (status != 0)
1040             qErrnoWarning(status, "QFontEngineQPF::remapFomrData: munmap failed!");
1041
1042         fontData = (const uchar *)::mmap(0, newFileSize, PROT_READ | (renderingFontEngine ? PROT_WRITE : 0),
1043                                          MAP_SHARED, fd, 0);
1044         if (!fontData || fontData == (const uchar *)MAP_FAILED) {
1045 #  if defined(DEBUG_FONTENGINE)
1046             perror("mmap failed");
1047 #  endif
1048             fontData = 0;
1049             return;
1050         }
1051     }
1052
1053     dataSize = newFileSize;
1054     glyphDataSize = newFileSize - glyphDataOffset;
1055 #if defined(DEBUG_FONTENGINE)
1056     qDebug() << "remapped the font file to" << newFileSize << "bytes";
1057 #endif
1058 }
1059
1060 #endif // QT_NO_FREETYPE
1061
1062 void QPFGenerator::generate()
1063 {
1064     writeHeader();
1065     writeGMap();
1066     writeBlock(QFontEngineQPF::GlyphBlock, QByteArray());
1067
1068     dev->seek(4); // position of header.lock
1069     writeUInt32(0);
1070 }
1071
1072 void QPFGenerator::writeHeader()
1073 {
1074     QFontEngineQPF::Header header;
1075
1076     header.magic[0] = 'Q';
1077     header.magic[1] = 'P';
1078     header.magic[2] = 'F';
1079     header.magic[3] = '2';
1080     header.lock = 1;
1081     header.majorVersion = QFontEngineQPF::CurrentMajorVersion;
1082     header.minorVersion = QFontEngineQPF::CurrentMinorVersion;
1083     header.dataSize = 0;
1084     dev->write((const char *)&header, sizeof(header));
1085
1086     writeTaggedString(QFontEngineQPF::Tag_FontName, fe->fontDef.family.toUtf8());
1087
1088     QFontEngine::FaceId face = fe->faceId();
1089     writeTaggedString(QFontEngineQPF::Tag_FileName, face.filename);
1090     writeTaggedUInt32(QFontEngineQPF::Tag_FileIndex, face.index);
1091
1092     {
1093         uchar data[4];
1094         uint len = 4;
1095         bool ok = fe->getSfntTableData(MAKE_TAG('h', 'e', 'a', 'd'), data, &len);
1096         if (ok) {
1097             const quint32 revision = qFromBigEndian<quint32>(data);
1098             writeTaggedUInt32(QFontEngineQPF::Tag_FontRevision, revision);
1099         }
1100     }
1101
1102     writeTaggedQFixed(QFontEngineQPF::Tag_Ascent, fe->ascent());
1103     writeTaggedQFixed(QFontEngineQPF::Tag_Descent, fe->descent());
1104     writeTaggedQFixed(QFontEngineQPF::Tag_Leading, fe->leading());
1105     writeTaggedQFixed(QFontEngineQPF::Tag_XHeight, fe->xHeight());
1106     writeTaggedQFixed(QFontEngineQPF::Tag_AverageCharWidth, fe->averageCharWidth());
1107     writeTaggedQFixed(QFontEngineQPF::Tag_MaxCharWidth, QFixed::fromReal(fe->maxCharWidth()));
1108     writeTaggedQFixed(QFontEngineQPF::Tag_LineThickness, fe->lineThickness());
1109     writeTaggedQFixed(QFontEngineQPF::Tag_MinLeftBearing, QFixed::fromReal(fe->minLeftBearing()));
1110     writeTaggedQFixed(QFontEngineQPF::Tag_MinRightBearing, QFixed::fromReal(fe->minRightBearing()));
1111     writeTaggedQFixed(QFontEngineQPF::Tag_UnderlinePosition, fe->underlinePosition());
1112     writeTaggedUInt8(QFontEngineQPF::Tag_PixelSize, fe->fontDef.pixelSize);
1113     writeTaggedUInt8(QFontEngineQPF::Tag_Weight, fe->fontDef.weight);
1114     writeTaggedUInt8(QFontEngineQPF::Tag_Style, fe->fontDef.style);
1115
1116     writeTaggedUInt8(QFontEngineQPF::Tag_GlyphFormat, QFontEngineQPF::AlphamapGlyphs);
1117
1118     writeTaggedString(QFontEngineQPF::Tag_EndOfHeader, QByteArray());
1119     align4();
1120
1121     const quint64 size = dev->pos();
1122     header.dataSize = qToBigEndian<quint16>(size - sizeof(header));
1123     dev->seek(0);
1124     dev->write((const char *)&header, sizeof(header));
1125     dev->seek(size);
1126 }
1127
1128 void QPFGenerator::writeGMap()
1129 {
1130     const quint16 glyphCount = fe->glyphCount();
1131
1132     writeUInt16(QFontEngineQPF::GMapBlock);
1133     writeUInt16(0); // padding
1134     writeUInt32(glyphCount * 4);
1135
1136     QByteArray &buffer = dev->buffer();
1137     const int numBytes = glyphCount * sizeof(quint32);
1138     qint64 pos = buffer.size();
1139     buffer.resize(pos + numBytes);
1140     qMemSet(buffer.data() + pos, 0xff, numBytes);
1141     dev->seek(pos + numBytes);
1142 }
1143
1144 void QPFGenerator::writeBlock(QFontEngineQPF::BlockTag tag, const QByteArray &data)
1145 {
1146     writeUInt16(tag);
1147     writeUInt16(0); // padding
1148     const int padSize = ((data.size() + 3) / 4) * 4 - data.size();
1149     writeUInt32(data.size() + padSize);
1150     dev->write(data);
1151     for (int i = 0; i < padSize; ++i)
1152         writeUInt8(0);
1153 }
1154
1155 void QPFGenerator::writeTaggedString(QFontEngineQPF::HeaderTag tag, const QByteArray &string)
1156 {
1157     writeUInt16(tag);
1158     writeUInt16(string.length());
1159     dev->write(string);
1160 }
1161
1162 void QPFGenerator::writeTaggedUInt32(QFontEngineQPF::HeaderTag tag, quint32 value)
1163 {
1164     writeUInt16(tag);
1165     writeUInt16(sizeof(value));
1166     writeUInt32(value);
1167 }
1168
1169 void QPFGenerator::writeTaggedUInt8(QFontEngineQPF::HeaderTag tag, quint8 value)
1170 {
1171     writeUInt16(tag);
1172     writeUInt16(sizeof(value));
1173     writeUInt8(value);
1174 }
1175
1176 void QPFGenerator::writeTaggedQFixed(QFontEngineQPF::HeaderTag tag, QFixed value)
1177 {
1178     writeUInt16(tag);
1179     writeUInt16(sizeof(quint32));
1180     writeUInt32(value.value());
1181 }
1182
1183 #endif // QT_NO_QWS_QPF2
1184
1185 /*
1186     Creates a new multi qws engine.
1187
1188     This function takes ownership of the QFontEngine, increasing it's refcount.
1189 */
1190 QFontEngineMultiQWS::QFontEngineMultiQWS(QFontEngine *fe, int _script, const QStringList &fallbacks)
1191     : QFontEngineMulti(fallbacks.size() + 1),
1192       fallbackFamilies(fallbacks), script(_script)
1193 {
1194     engines[0] = fe;
1195     fe->ref.ref();
1196     fontDef = engines[0]->fontDef;
1197 }
1198
1199 void QFontEngineMultiQWS::loadEngine(int at)
1200 {
1201     Q_ASSERT(at < engines.size());
1202     Q_ASSERT(engines.at(at) == 0);
1203
1204     QFontDef request = fontDef;
1205     request.styleStrategy |= QFont::NoFontMerging;
1206     request.family = fallbackFamilies.at(at-1);
1207     engines[at] = QFontDatabase::findFont(script,
1208                                           /*fontprivate*/0,
1209                                           request);
1210     Q_ASSERT(engines[at]);
1211     engines[at]->ref.ref();
1212     engines[at]->fontDef = request;
1213 }
1214
1215 void QFontEngineMultiQWS::draw(QPaintEngine */*p*/, qreal /*x*/, qreal /*y*/, const QTextItemInt &/*si*/)
1216 {
1217     qFatal("QFontEngineMultiQWS::draw should never be called!");
1218 }
1219
1220 QT_END_NAMESPACE