1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtGui module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #include "qfontengine_qpf_p.h"
44 #include "private/qpaintengine_raster_p.h"
45 #include <QtCore/qlibraryinfo.h>
46 #include <QtCore/qfileinfo.h>
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"
54 #include "private/qcore_unix_p.h" // overrides QT_OPEN
59 #include <sys/types.h>
67 #ifndef QT_NO_QWS_QPF2
69 #include "qpfutil.cpp"
71 QT_BEGIN_INCLUDE_NAMESPACE
74 # include "private/qwscommand_qws_p.h"
75 # include "qwsdisplay_qws.h"
76 # include "qabstractfontengine_p.h"
78 #include "qplatformdefs.h"
79 QT_END_INCLUDE_NAMESPACE
81 //#define DEBUG_HEADER
82 //#define DEBUG_FONTENGINE
84 #if defined(DEBUG_HEADER)
85 # define DEBUG_VERIFY qDebug
87 # define DEBUG_VERIFY if (0) qDebug
90 #define READ_VERIFY(type, variable) \
91 if (tagPtr + sizeof(type) > endPtr) { \
92 DEBUG_VERIFY() << "read verify failed in line" << __LINE__; \
95 variable = qFromBigEndian<type>(tagPtr); \
96 DEBUG_VERIFY() << "read value" << variable << "of type " #type; \
97 tagPtr += sizeof(type)
100 T readValue(const uchar *&data)
102 T value = qFromBigEndian<T>(data);
107 #define VERIFY(condition) \
108 if (!(condition)) { \
109 DEBUG_VERIFY() << "condition " #condition " failed in line" << __LINE__; \
113 #define VERIFY_TAG(condition) \
114 if (!(condition)) { \
115 DEBUG_VERIFY() << "verifying tag condition " #condition " failed in line" << __LINE__ << "with tag" << tag; \
119 static inline const uchar *verifyTag(const uchar *tagPtr, const uchar *endPtr)
122 READ_VERIFY(quint16, tag);
123 READ_VERIFY(quint16, length);
124 if (tag == QFontEngineQPF::Tag_EndOfHeader)
126 if (tag < QFontEngineQPF::NumTags) {
127 switch (tagTypes[tag]) {
128 case QFontEngineQPF::BitFieldType:
129 case QFontEngineQPF::StringType:
130 // can't do anything...
132 case QFontEngineQPF::UInt32Type:
133 VERIFY_TAG(length == sizeof(quint32));
135 case QFontEngineQPF::FixedType:
136 VERIFY_TAG(length == sizeof(quint32));
138 case QFontEngineQPF::UInt8Type:
139 VERIFY_TAG(length == sizeof(quint8));
142 #if defined(DEBUG_HEADER)
144 qDebug() << "tag data" << hex << *tagPtr;
145 else if (length == 4)
146 qDebug() << "tag data" << hex << tagPtr[0] << tagPtr[1] << tagPtr[2] << tagPtr[3];
149 return tagPtr + length;
152 const QFontEngineQPF::Glyph *QFontEngineQPF::findGlyph(glyph_t g) const
154 if (!g || g >= glyphMapEntries)
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)
161 #if defined(DEBUG_FONTENGINE)
162 qDebug() << "glyph" << g << "outside of glyphData, remapping font file";
164 #if !defined(QT_NO_FREETYPE) && !defined(QT_FONTS_ARE_RESOURCES)
165 const_cast<QFontEngineQPF *>(this)->remapFontData();
167 if (glyphPos > glyphDataSize)
170 return reinterpret_cast<const Glyph *>(fontData + glyphDataOffset + glyphPos);
173 bool QFontEngineQPF::verifyHeader(const uchar *data, int size)
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')
183 VERIFY(header->majorVersion <= CurrentMajorVersion);
184 const quint16 dataSize = qFromBigEndian<quint16>(header->dataSize);
185 VERIFY(size >= int(sizeof(Header)) + dataSize);
187 const uchar *tagPtr = data + sizeof(Header);
188 const uchar *tagEndPtr = tagPtr + dataSize;
189 while (tagPtr < tagEndPtr - 3) {
190 tagPtr = verifyTag(tagPtr, tagEndPtr);
194 VERIFY(tagPtr <= tagEndPtr);
198 QVariant QFontEngineQPF::extractHeaderField(const uchar *data, HeaderTag requestedTag)
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]) {
209 return QVariant(QString::fromUtf8(reinterpret_cast<const char *>(tagPtr), length));
211 return QVariant(readValue<quint32>(tagPtr));
213 return QVariant(uint(*tagPtr));
215 return QVariant(QFixed::fromFixed(readValue<quint32>(tagPtr)).toReal());
217 return QVariant(QByteArray(reinterpret_cast<const char *>(tagPtr), length));
220 } else if (tag == Tag_EndOfHeader) {
229 #endif // QT_NO_QWS_QPF2
231 QString qws_fontCacheDir()
234 #if defined(Q_WS_QWS)
235 extern QString qws_dataDir();
238 dir = QDir::tempPath();
240 dir.append(QLatin1String("/fonts/"));
242 if (!qd.exists() && !qd.mkpath(dir))
243 dir = QDir::tempPath();
247 #ifndef QT_NO_QWS_QPF2
249 #ifndef QT_FONTS_ARE_RESOURCES
250 QList<QByteArray> QFontEngineQPF::cleanUpAfterClientCrash(const QList<int> &crashedClientIds)
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]));
257 int fd = QT_OPEN(fileName.constData(), O_RDONLY, 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;
263 if (lockValue && crashedClientIds.contains(lockValue)) {
264 removedFonts.append(fileName);
265 QFile::remove(QFile::decodeName(fileName));
268 ::munmap(header, sizeof(QFontEngineQPF::Header));
273 if (!removedFonts.isEmpty())
274 qDebug() << "list of corrupted and removed fonts:" << removedFonts;
279 static inline unsigned int getChar(const QChar *str, int &i, const int len)
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;
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)
295 QFontEngineQPF::QFontEngineQPF(const QFontDef &def, int fileDescriptor, QFontEngine *fontEngine)
296 : fd(fileDescriptor), fontData(0), dataSize(0), renderingFontEngine(fontEngine)
309 if (renderingFontEngine)
310 glyphFormat = renderingFontEngine->glyphFormat;
311 kerning_pairs_loaded = false;
314 #if defined(DEBUG_FONTENGINE)
315 qDebug() << "QFontEngineQPF::QFontEngineQPF( fd =" << fd << ", renderingFontEngine =" << renderingFontEngine << ')';
318 #ifndef QT_FONTS_ARE_RESOURCES
320 if (!renderingFontEngine)
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());
332 encodedFileName = QFile::encodeName(fileName);
333 if (::access(encodedFileName, F_OK) == 0) {
334 #if defined(DEBUG_FONTENGINE)
335 qDebug() << "found existing qpf:" << fileName;
337 if (::access(encodedFileName, W_OK | R_OK) == 0) {
338 fd = QT_OPEN(encodedFileName, O_RDWR);
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);
344 #if defined(DEBUG_FONTENGINE)
345 qErrnoWarning("QFontEngineQPF: unable to open %s", encodedName.constData());
351 #if defined(DEBUG_FONTENGINE)
352 qWarning("QFontEngineQPF: insufficient access rights to %s", encodedName.constData());
357 #if defined(DEBUG_FONTENGINE)
358 qDebug() << "creating qpf on the fly:" << fileName;
360 if (::access(QFile::encodeName(qws_fontCacheDir()), W_OK) == 0) {
361 fd = QT_OPEN(encodedFileName, O_RDWR | O_EXCL | O_CREAT, 0644);
363 #if defined(DEBUG_FONTENGINE)
364 qErrnoWarning(errno, "QFontEngineQPF: open() failed for %s", encodedName.constData());
370 buffer.open(QIODevice::ReadWrite);
371 QPFGenerator generator(&buffer, renderingFontEngine);
372 generator.generate();
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());
382 #if defined(DEBUG_FONTENGINE)
383 qErrnoWarning(errno, "QFontEngineQPF: access() failed for %s", qPrintable(qws_fontCacheDir()));
391 if (QT_FSTAT(fd, &st)) {
392 #if defined(DEBUG_FONTENGINE)
393 qErrnoWarning(errno, "QFontEngineQPF: fstat failed!");
397 dataSize = st.st_size;
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");
408 #endif //QT_FONTS_ARE_RESOURCES
410 if (!verifyHeader(fontData, dataSize)) {
411 #if defined(DEBUG_FONTENGINE)
412 qDebug() << "verifyHeader failed!";
417 const Header *header = reinterpret_cast<const Header *>(fontData);
419 readOnly = (header->lock == 0xffffffff);
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);
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;
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);
448 #ifndef QT_NO_SETTINGS
449 QLibraryInfo::location(QLibraryInfo::LibrariesPath) +
451 QLatin1String("/fonts/") +
452 QFileInfo(QFile::decodeName(face_id.filename)).fileName();
453 face_id.filename = QFile::encodeName(newPath);
454 freetype = QFreetypeFace::getFace(face_id);
457 const quint32 qpfTtfRevision = extractHeaderField(fontData, Tag_FontRevision).toUInt();
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);
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();
476 int tableSize = cmapSize;
477 const uchar *cmapPtr = getCMap(fontData + cmapOffset, tableSize, &symbol, &cmapSize);
479 cmapOffset = cmapPtr - fontData;
482 } else if (externalCMap) {
483 int tableSize = cmapSize;
484 externalCMap = getCMap(externalCMap, tableSize, &symbol, &cmapSize);
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)
494 if (glyphDataPos >= glyphDataSize) {
503 #if defined(DEBUG_FONTENGINE)
505 qDebug() << "fontData" << fontData << "dataSize" << dataSize
506 << "externalCMap" << externalCMap << "cmapOffset" << cmapOffset
507 << "glyphMapOffset" << glyphMapOffset << "glyphDataOffset" << glyphDataOffset
508 << "fd" << fd << "glyphDataSize" << glyphDataSize;
510 #if defined(Q_WS_QWS)
511 if (isValid() && renderingFontEngine)
512 qt_fbdpy->sendFontCommand(QWSFontCommand::StartedUsingFont, encodedFileName);
516 QFontEngineQPF::~QFontEngineQPF()
518 #if defined(Q_WS_QWS)
519 if (isValid() && renderingFontEngine) {
521 qt_fbdpy->sendFontCommand(QWSFontCommand::StoppedUsingFont, encodedFileName);
523 qDebug("QFontEngineQPF::~QFontEngineQPF: Out of memory");
528 delete renderingFontEngine;
530 if (munmap((void *)fontData, dataSize) == -1) {
531 #if defined(DEBUG_FONTENGINE)
532 qErrnoWarning(errno, "~QFontEngineQPF: Unable to munmap");
538 #if !defined(QT_NO_FREETYPE)
540 freetype->release(face_id);
544 bool QFontEngineQPF::getSfntTableData(uint tag, uchar *buffer, uint *length) const
546 #if !defined(QT_NO_FREETYPE)
548 return freetype->getSfntTable(tag, buffer, length);
556 bool QFontEngineQPF::stringToCMap(const QChar *str, int len, QGlyphLayout *glyphs, int *nglyphs, QTextEngine::ShaperFlags flags) const
558 if (!externalCMap && !cmapOffset && renderingFontEngine) {
559 if (!renderingFontEngine->stringToCMap(str, len, glyphs, nglyphs, flags))
561 #ifndef QT_NO_FREETYPE
562 const_cast<QFontEngineQPF *>(this)->ensureGlyphsLoaded(*glyphs);
567 if (*nglyphs < len) {
572 #if defined(DEBUG_FONTENGINE)
573 QSet<QChar> seenGlyphs;
576 const uchar *cmap = externalCMap ? externalCMap : (fontData + cmapOffset);
578 bool mirrored = flags & QTextEngine::RightToLeft;
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);
589 for (int i = 0; i < len; ++i) {
590 unsigned int uc = getChar(str, i, len);
592 uc = QChar::mirroredChar(uc);
593 glyphs->glyphs[glyph_pos] = getTrueTypeGlyphIndex(cmap, uc);
594 #if 0 && defined(DEBUG_FONTENGINE)
596 if (!findGlyph(glyphs[glyph_pos].glyph) && !seenGlyphs.contains(c))
597 qDebug() << "glyph for character" << c << '/' << hex << uc << "is" << dec << glyphs[glyph_pos].glyph;
599 seenGlyphs.insert(c);
605 *nglyphs = glyph_pos;
606 glyphs->numGlyphs = glyph_pos;
607 recalcAdvances(glyphs, flags);
611 void QFontEngineQPF::recalcAdvances(QGlyphLayout *glyphs, QTextEngine::ShaperFlags) const
613 #ifndef QT_NO_FREETYPE
614 const_cast<QFontEngineQPF *>(this)->ensureGlyphsLoaded(*glyphs);
616 for (int i = 0; i < glyphs->numGlyphs; ++i) {
617 const Glyph *g = findGlyph(glyphs->glyphs[i]);
619 glyphs->glyphs[i] = 0;
622 glyphs->advances_x[i] = g->advance;
623 glyphs->advances_y[i] = 0;
627 QImage QFontEngineQPF::alphaMapForGlyph(glyph_t g)
629 const Glyph *glyph = findGlyph(g);
633 const uchar *bits = ((const uchar *) glyph) + sizeof(Glyph);
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));
639 for (int i=0; i<glyph->height; ++i) {
640 memcpy(image.scanLine(i), bits, glyph->bytesPerLine);
641 bits += glyph->bytesPerLine;
646 void QFontEngineQPF::draw(QPaintEngine *p, qreal _x, qreal _y, const QTextItemInt &si)
648 QPaintEngineState *pState = p->state;
649 QRasterPaintEngine *paintEngine = static_cast<QRasterPaintEngine*>(p);
651 QTransform matrix = pState->transform();
652 matrix.translate(_x, _y);
653 QFixed x = QFixed::fromReal(matrix.dx());
654 QFixed y = QFixed::fromReal(matrix.dy());
656 QVarLengthArray<QFixedPoint> positions;
657 QVarLengthArray<glyph_t> glyphs;
658 getGlyphPositions(si.glyphs, matrix, si.flags, glyphs, positions);
659 if (glyphs.size() == 0)
662 for(int i = 0; i < glyphs.size(); i++) {
663 const Glyph *glyph = findGlyph(glyphs[i]);
667 const int depth = 8; //###
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);
676 void QFontEngineQPF::addOutlineToPath(qreal x, qreal y, const QGlyphLayout &glyphs, QPainterPath *path, QTextItem::RenderFlags flags)
678 if (renderingFontEngine &&
679 (renderingFontEngine->type() != QFontEngine::Proxy
680 || static_cast<QProxyFontEngine *>(renderingFontEngine)->capabilities() & QAbstractFontEngine::CanOutlineGlyphs)) {
681 renderingFontEngine->addOutlineToPath(x, y, glyphs, path, flags);
684 addBitmapFontToPath(x, y, glyphs, path, flags);
687 glyph_metrics_t QFontEngineQPF::boundingBox(const QGlyphLayout &glyphs)
689 #ifndef QT_NO_FREETYPE
690 const_cast<QFontEngineQPF *>(this)->ensureGlyphsLoaded(glyphs);
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;
700 for (int i = 0; i < glyphs.numGlyphs; i++) {
701 const Glyph *g = findGlyph(glyphs.glyphs[i]);
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;
713 overall.height = qMax(overall.height, ymax - overall.y);
714 overall.width = xmax - overall.x;
719 glyph_metrics_t QFontEngineQPF::boundingBox(glyph_t glyph)
721 #ifndef QT_NO_FREETYPE
723 QGlyphLayoutArray<1> tmp;
724 tmp.glyphs[0] = glyph;
725 const_cast<QFontEngineQPF *>(this)->ensureGlyphsLoaded(tmp);
728 glyph_metrics_t overall;
729 const Glyph *g = findGlyph(glyph);
734 overall.width = g->width;
735 overall.height = g->height;
736 overall.xoff = g->advance;
740 QFixed QFontEngineQPF::ascent() const
742 return QFixed::fromReal(extractHeaderField(fontData, Tag_Ascent).value<qreal>());
745 QFixed QFontEngineQPF::descent() const
747 return QFixed::fromReal(extractHeaderField(fontData, Tag_Descent).value<qreal>());
750 QFixed QFontEngineQPF::leading() const
752 return QFixed::fromReal(extractHeaderField(fontData, Tag_Leading).value<qreal>());
755 qreal QFontEngineQPF::maxCharWidth() const
757 return extractHeaderField(fontData, Tag_MaxCharWidth).value<qreal>();
760 qreal QFontEngineQPF::minLeftBearing() const
762 return extractHeaderField(fontData, Tag_MinLeftBearing).value<qreal>();
765 qreal QFontEngineQPF::minRightBearing() const
767 return extractHeaderField(fontData, Tag_MinRightBearing).value<qreal>();
770 QFixed QFontEngineQPF::underlinePosition() const
772 return QFixed::fromReal(extractHeaderField(fontData, Tag_UnderlinePosition).value<qreal>());
775 QFixed QFontEngineQPF::lineThickness() const
777 return QFixed::fromReal(extractHeaderField(fontData, Tag_LineThickness).value<qreal>());
780 QFontEngine::Type QFontEngineQPF::type() const
782 return QFontEngine::QPF2;
785 bool QFontEngineQPF::canRender(const QChar *string, int len)
787 const uchar *cmap = externalCMap ? externalCMap : (fontData + cmapOffset);
790 for (int i = 0; i < len; ++i) {
791 unsigned int uc = getChar(string, i, len);
792 glyph_t g = getTrueTypeGlyphIndex(cmap, uc);
794 g = getTrueTypeGlyphIndex(cmap, uc + 0xf000);
799 for (int i = 0; i < len; ++i) {
800 unsigned int uc = getChar(string, i, len);
801 if (!getTrueTypeGlyphIndex(cmap, uc))
808 bool QFontEngineQPF::isValid() const
810 return fontData && dataSize && (cmapOffset || externalCMap || renderingFontEngine)
811 && glyphMapOffset && glyphDataOffset && (fd >= 0 || glyphDataSize > 0);
814 #if !defined(QT_NO_FREETYPE)
815 FT_Face QFontEngineQPF::lockFace() const
819 FT_Face face = freetype->face;
822 const int ysize = qRound(fontDef.pixelSize * qreal(64));
823 const int xsize = ysize;
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;
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);
845 void QFontEngineQPF::unlockFace() const
850 void QFontEngineQPF::doKerning(QGlyphLayout *g, QTextEngine::ShaperFlags flags) const
852 if (!kerning_pairs_loaded) {
853 kerning_pairs_loaded = true;
856 if (freetype->face->size->metrics.x_ppem != 0) {
857 QFixed scalingFactor(freetype->face->units_per_EM/freetype->face->size->metrics.x_ppem);
859 const_cast<QFontEngineQPF *>(this)->loadKerningPairs(scalingFactor);
865 QFontEngine::doKerning(g, flags);
868 HB_Error QFontEngineQPF::getPointInOutline(HB_Glyph glyph, int flags, hb_uint32 point, HB_Fixed *xpos, HB_Fixed *ypos, hb_uint32 *nPoints)
871 return HB_Err_Not_Covered;
873 HB_Error result = freetype->getPointInOutline(glyph, flags, point, xpos, ypos, nPoints);
878 QFixed QFontEngineQPF::emSquareSize() const
881 return QFontEngine::emSquareSize();
882 if (FT_IS_SCALABLE(freetype->face))
883 return freetype->face->units_per_EM;
885 return freetype->face->size->metrics.y_ppem;
888 void QFontEngineQPF::ensureGlyphsLoaded(const QGlyphLayout &glyphs)
893 for (int i = 0; i < glyphs.numGlyphs; ++i) {
894 if (!glyphs.glyphs[i])
896 const Glyph *g = findGlyph(glyphs.glyphs[i]);
903 g = findGlyph(glyphs.glyphs[i]);
907 loadGlyph(glyphs.glyphs[i]);
911 #if defined(DEBUG_FONTENGINE)
912 qDebug() << "Finished rendering glyphs\n";
917 void QFontEngineQPF::loadGlyph(glyph_t glyph)
919 quint32 glyphPos = ~0;
921 if (!renderingFontEngine)
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);
928 //### we know that 1 is opaque and 0 is transparent
929 uchar *byte = img.bits();
930 int count = img.byteCount();
935 glyph_metrics_t metrics = renderingFontEngine->boundingBox(glyph);
936 renderingFontEngine->removeGlyphFromCache(glyph);
938 off_t oldSize = ::lseek(fd, 0, SEEK_END);
939 if (oldSize == (off_t)-1)
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);
950 QT_WRITE(fd, &g, sizeof(g));
951 QT_WRITE(fd, img.bits(), img.byteCount());
953 glyphPos = oldSize - glyphDataOffset;
954 #if 0 && defined(DEBUG_FONTENGINE)
955 qDebug() << "glyphPos for new glyph" << glyph << "is" << glyphPos << "oldSize" << oldSize << "glyphDataOffset" << glyphDataOffset;
958 quint32 *gmap = (quint32 *)(fontData + glyphMapOffset);
959 gmap[glyph] = qToBigEndian(glyphPos);
961 glyphDataSize = glyphPos + sizeof(g) + img.byteCount();
962 quint32 *blockSizePtr = (quint32 *)(fontData + glyphDataOffset - 4);
963 *blockSizePtr = qToBigEndian(glyphDataSize);
966 bool QFontEngineQPF::lockFile()
968 // #### this does not handle the case when the process holding the
969 // lock hangs for some reason
971 lock.l_type = F_WRLCK;
972 lock.l_whence = SEEK_SET;
974 lock.l_len = 0; // lock the whole file
975 while (fcntl(fd, F_SETLKW, &lock) != 0) {
978 perror("locking qpf");
981 Header *header = (Header *)fontData;
983 lock.l_type = F_UNLCK;
984 if (fcntl(fd, F_SETLK, &lock) != 0)
985 perror("unlocking possibly corrupt qpf");
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
992 header->lock = qws_client_id ? qws_client_id : 1;
999 void QFontEngineQPF::unlockFile()
1001 ((Header *)fontData)->lock = 0;
1004 lock.l_type = F_UNLCK;
1005 lock.l_whence = SEEK_SET;
1007 lock.l_len = 0; // lock the whole file
1008 if (fcntl(fd, F_SETLK, &lock) != 0) {
1009 perror("unlocking qpf");
1015 void QFontEngineQPF::remapFontData()
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");
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");
1036 #endif // QT_NO_MREMAP
1038 int status = ::munmap((void *)fontData, dataSize);
1040 qErrnoWarning(status, "QFontEngineQPF::remapFomrData: munmap failed!");
1042 fontData = (const uchar *)::mmap(0, newFileSize, PROT_READ | (renderingFontEngine ? PROT_WRITE : 0),
1044 if (!fontData || fontData == (const uchar *)MAP_FAILED) {
1045 # if defined(DEBUG_FONTENGINE)
1046 perror("mmap failed");
1053 dataSize = newFileSize;
1054 glyphDataSize = newFileSize - glyphDataOffset;
1055 #if defined(DEBUG_FONTENGINE)
1056 qDebug() << "remapped the font file to" << newFileSize << "bytes";
1060 #endif // QT_NO_FREETYPE
1062 void QPFGenerator::generate()
1066 writeBlock(QFontEngineQPF::GlyphBlock, QByteArray());
1068 dev->seek(4); // position of header.lock
1072 void QPFGenerator::writeHeader()
1074 QFontEngineQPF::Header header;
1076 header.magic[0] = 'Q';
1077 header.magic[1] = 'P';
1078 header.magic[2] = 'F';
1079 header.magic[3] = '2';
1081 header.majorVersion = QFontEngineQPF::CurrentMajorVersion;
1082 header.minorVersion = QFontEngineQPF::CurrentMinorVersion;
1083 header.dataSize = 0;
1084 dev->write((const char *)&header, sizeof(header));
1086 writeTaggedString(QFontEngineQPF::Tag_FontName, fe->fontDef.family.toUtf8());
1088 QFontEngine::FaceId face = fe->faceId();
1089 writeTaggedString(QFontEngineQPF::Tag_FileName, face.filename);
1090 writeTaggedUInt32(QFontEngineQPF::Tag_FileIndex, face.index);
1095 bool ok = fe->getSfntTableData(MAKE_TAG('h', 'e', 'a', 'd'), data, &len);
1097 const quint32 revision = qFromBigEndian<quint32>(data);
1098 writeTaggedUInt32(QFontEngineQPF::Tag_FontRevision, revision);
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);
1116 writeTaggedUInt8(QFontEngineQPF::Tag_GlyphFormat, QFontEngineQPF::AlphamapGlyphs);
1118 writeTaggedString(QFontEngineQPF::Tag_EndOfHeader, QByteArray());
1121 const quint64 size = dev->pos();
1122 header.dataSize = qToBigEndian<quint16>(size - sizeof(header));
1124 dev->write((const char *)&header, sizeof(header));
1128 void QPFGenerator::writeGMap()
1130 const quint16 glyphCount = fe->glyphCount();
1132 writeUInt16(QFontEngineQPF::GMapBlock);
1133 writeUInt16(0); // padding
1134 writeUInt32(glyphCount * 4);
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);
1144 void QPFGenerator::writeBlock(QFontEngineQPF::BlockTag tag, const QByteArray &data)
1147 writeUInt16(0); // padding
1148 const int padSize = ((data.size() + 3) / 4) * 4 - data.size();
1149 writeUInt32(data.size() + padSize);
1151 for (int i = 0; i < padSize; ++i)
1155 void QPFGenerator::writeTaggedString(QFontEngineQPF::HeaderTag tag, const QByteArray &string)
1158 writeUInt16(string.length());
1162 void QPFGenerator::writeTaggedUInt32(QFontEngineQPF::HeaderTag tag, quint32 value)
1165 writeUInt16(sizeof(value));
1169 void QPFGenerator::writeTaggedUInt8(QFontEngineQPF::HeaderTag tag, quint8 value)
1172 writeUInt16(sizeof(value));
1176 void QPFGenerator::writeTaggedQFixed(QFontEngineQPF::HeaderTag tag, QFixed value)
1179 writeUInt16(sizeof(quint32));
1180 writeUInt32(value.value());
1183 #endif // QT_NO_QWS_QPF2
1186 Creates a new multi qws engine.
1188 This function takes ownership of the QFontEngine, increasing it's refcount.
1190 QFontEngineMultiQWS::QFontEngineMultiQWS(QFontEngine *fe, int _script, const QStringList &fallbacks)
1191 : QFontEngineMulti(fallbacks.size() + 1),
1192 fallbackFamilies(fallbacks), script(_script)
1196 fontDef = engines[0]->fontDef;
1199 void QFontEngineMultiQWS::loadEngine(int at)
1201 Q_ASSERT(at < engines.size());
1202 Q_ASSERT(engines.at(at) == 0);
1204 QFontDef request = fontDef;
1205 request.styleStrategy |= QFont::NoFontMerging;
1206 request.family = fallbackFamilies.at(at-1);
1207 engines[at] = QFontDatabase::findFont(script,
1210 Q_ASSERT(engines[at]);
1211 engines[at]->ref.ref();
1212 engines[at]->fontDef = request;
1215 void QFontEngineMultiQWS::draw(QPaintEngine */*p*/, qreal /*x*/, qreal /*y*/, const QTextItemInt &/*si*/)
1217 qFatal("QFontEngineMultiQWS::draw should never be called!");