1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the QtGui module of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Lesser General Public License Usage
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
18 | ** General Public License version 2.1 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
20 | ** packaging of this file. Please review the following information to
|
---|
21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
23 | **
|
---|
24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at [email protected].
|
---|
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 | kerning_pairs_loaded = false;
|
---|
310 | readOnly = true;
|
---|
311 |
|
---|
312 | #if defined(DEBUG_FONTENGINE)
|
---|
313 | qDebug() << "QFontEngineQPF::QFontEngineQPF( fd =" << fd << ", renderingFontEngine =" << renderingFontEngine << ')';
|
---|
314 | #endif
|
---|
315 |
|
---|
316 | #ifndef QT_FONTS_ARE_RESOURCES
|
---|
317 | if (fd < 0) {
|
---|
318 | if (!renderingFontEngine)
|
---|
319 | return;
|
---|
320 |
|
---|
321 | fileName = fontDef.family.toLower() + QLatin1Char('_')
|
---|
322 | + QString::number(fontDef.pixelSize)
|
---|
323 | + QLatin1Char('_') + QString::number(fontDef.weight)
|
---|
324 | + (fontDef.style != QFont::StyleNormal ?
|
---|
325 | QLatin1String("_italic") : QLatin1String(""))
|
---|
326 | + QLatin1String(".qsf");
|
---|
327 | fileName.replace(QLatin1Char(' '), QLatin1Char('_'));
|
---|
328 | fileName.prepend(qws_fontCacheDir());
|
---|
329 |
|
---|
330 | encodedFileName = QFile::encodeName(fileName);
|
---|
331 | if (::access(encodedFileName, F_OK) == 0) {
|
---|
332 | #if defined(DEBUG_FONTENGINE)
|
---|
333 | qDebug() << "found existing qpf:" << fileName;
|
---|
334 | #endif
|
---|
335 | if (::access(encodedFileName, W_OK | R_OK) == 0) {
|
---|
336 | fd = QT_OPEN(encodedFileName, O_RDWR);
|
---|
337 | }
|
---|
338 | // read-write access failed - try read-only access
|
---|
339 | if (fd == -1 && ::access(encodedFileName, R_OK) == 0) {
|
---|
340 | fd = QT_OPEN(encodedFileName, O_RDONLY);
|
---|
341 | if (fd == -1) {
|
---|
342 | #if defined(DEBUG_FONTENGINE)
|
---|
343 | qErrnoWarning("QFontEngineQPF: unable to open %s", encodedName.constData());
|
---|
344 | #endif
|
---|
345 | return;
|
---|
346 | }
|
---|
347 | }
|
---|
348 | if (fd == -1) {
|
---|
349 | #if defined(DEBUG_FONTENGINE)
|
---|
350 | qWarning("QFontEngineQPF: insufficient access rights to %s", encodedName.constData());
|
---|
351 | #endif
|
---|
352 | return;
|
---|
353 | }
|
---|
354 | } else {
|
---|
355 | #if defined(DEBUG_FONTENGINE)
|
---|
356 | qDebug() << "creating qpf on the fly:" << fileName;
|
---|
357 | #endif
|
---|
358 | if (::access(QFile::encodeName(qws_fontCacheDir()), W_OK) == 0) {
|
---|
359 | fd = QT_OPEN(encodedFileName, O_RDWR | O_EXCL | O_CREAT, 0644);
|
---|
360 | if (fd == -1) {
|
---|
361 | #if defined(DEBUG_FONTENGINE)
|
---|
362 | qErrnoWarning(errno, "QFontEngineQPF: open() failed for %s", encodedName.constData());
|
---|
363 | #endif
|
---|
364 | return;
|
---|
365 | }
|
---|
366 |
|
---|
367 | QBuffer buffer;
|
---|
368 | buffer.open(QIODevice::ReadWrite);
|
---|
369 | QPFGenerator generator(&buffer, renderingFontEngine);
|
---|
370 | generator.generate();
|
---|
371 | buffer.close();
|
---|
372 | const QByteArray &data = buffer.data();
|
---|
373 | if (QT_WRITE(fd, data.constData(), data.size()) == -1) {
|
---|
374 | #if defined(DEBUG_FONTENGINE)
|
---|
375 | qErrnoWarning(errno, "QFontEngineQPF: write() failed for %s", encodedName.constData());
|
---|
376 | #endif
|
---|
377 | return;
|
---|
378 | }
|
---|
379 | } else {
|
---|
380 | #if defined(DEBUG_FONTENGINE)
|
---|
381 | qErrnoWarning(errno, "QFontEngineQPF: access() failed for %s", qPrintable(qws_fontCacheDir()));
|
---|
382 | #endif
|
---|
383 | return;
|
---|
384 | }
|
---|
385 | }
|
---|
386 | }
|
---|
387 |
|
---|
388 | QT_STATBUF st;
|
---|
389 | if (QT_FSTAT(fd, &st)) {
|
---|
390 | #if defined(DEBUG_FONTENGINE)
|
---|
391 | qErrnoWarning(errno, "QFontEngineQPF: fstat failed!");
|
---|
392 | #endif
|
---|
393 | return;
|
---|
394 | }
|
---|
395 | dataSize = st.st_size;
|
---|
396 |
|
---|
397 |
|
---|
398 | fontData = (const uchar *)::mmap(0, st.st_size, PROT_READ | (renderingFontEngine ? PROT_WRITE : 0), MAP_SHARED, fd, 0);
|
---|
399 | if (!fontData || fontData == (const uchar *)MAP_FAILED) {
|
---|
400 | #if defined(DEBUG_FONTENGINE)
|
---|
401 | perror("mmap failed");
|
---|
402 | #endif
|
---|
403 | fontData = 0;
|
---|
404 | return;
|
---|
405 | }
|
---|
406 | #endif //QT_FONTS_ARE_RESOURCES
|
---|
407 |
|
---|
408 | if (!verifyHeader(fontData, dataSize)) {
|
---|
409 | #if defined(DEBUG_FONTENGINE)
|
---|
410 | qDebug() << "verifyHeader failed!";
|
---|
411 | #endif
|
---|
412 | return;
|
---|
413 | }
|
---|
414 |
|
---|
415 | const Header *header = reinterpret_cast<const Header *>(fontData);
|
---|
416 |
|
---|
417 | readOnly = (header->lock == 0xffffffff);
|
---|
418 |
|
---|
419 | const uchar *data = fontData + sizeof(Header) + qFromBigEndian<quint16>(header->dataSize);
|
---|
420 | const uchar *endPtr = fontData + dataSize;
|
---|
421 | while (data <= endPtr - 8) {
|
---|
422 | quint16 blockTag = readValue<quint16>(data);
|
---|
423 | data += 2; // skip padding
|
---|
424 | quint32 blockSize = readValue<quint32>(data);
|
---|
425 |
|
---|
426 | if (blockTag == CMapBlock) {
|
---|
427 | cmapOffset = data - fontData;
|
---|
428 | cmapSize = blockSize;
|
---|
429 | } else if (blockTag == GMapBlock) {
|
---|
430 | glyphMapOffset = data - fontData;
|
---|
431 | glyphMapEntries = blockSize / 4;
|
---|
432 | } else if (blockTag == GlyphBlock) {
|
---|
433 | glyphDataOffset = data - fontData;
|
---|
434 | glyphDataSize = blockSize;
|
---|
435 | }
|
---|
436 |
|
---|
437 | data += blockSize;
|
---|
438 | }
|
---|
439 |
|
---|
440 | face_id.filename = QFile::encodeName(extractHeaderField(fontData, Tag_FileName).toString());
|
---|
441 | face_id.index = extractHeaderField(fontData, Tag_FileIndex).toInt();
|
---|
442 | #if !defined(QT_NO_FREETYPE) && !defined(QT_FONTS_ARE_RESOURCES)
|
---|
443 | freetype = QFreetypeFace::getFace(face_id);
|
---|
444 | if (!freetype) {
|
---|
445 | QString newPath =
|
---|
446 | #ifndef QT_NO_SETTINGS
|
---|
447 | QLibraryInfo::location(QLibraryInfo::LibrariesPath) +
|
---|
448 | #endif
|
---|
449 | QLatin1String("/fonts/") +
|
---|
450 | QFileInfo(QFile::decodeName(face_id.filename)).fileName();
|
---|
451 | face_id.filename = QFile::encodeName(newPath);
|
---|
452 | freetype = QFreetypeFace::getFace(face_id);
|
---|
453 | }
|
---|
454 | if (freetype) {
|
---|
455 | const quint32 qpfTtfRevision = extractHeaderField(fontData, Tag_FontRevision).toUInt();
|
---|
456 | uchar data[4];
|
---|
457 | uint length = 4;
|
---|
458 | bool ok = freetype->getSfntTable(MAKE_TAG('h', 'e', 'a', 'd'), data, &length);
|
---|
459 | if (!ok || length != 4
|
---|
460 | || qFromBigEndian<quint32>(data) != qpfTtfRevision) {
|
---|
461 | freetype->release(face_id);
|
---|
462 | freetype = 0;
|
---|
463 | }
|
---|
464 | }
|
---|
465 | if (!cmapOffset && freetype) {
|
---|
466 | freetypeCMapTable = getSfntTable(MAKE_TAG('c', 'm', 'a', 'p'));
|
---|
467 | externalCMap = reinterpret_cast<const uchar *>(freetypeCMapTable.constData());
|
---|
468 | cmapSize = freetypeCMapTable.size();
|
---|
469 | }
|
---|
470 | #endif
|
---|
471 |
|
---|
472 | // get the real cmap
|
---|
473 | if (cmapOffset) {
|
---|
474 | int tableSize = cmapSize;
|
---|
475 | const uchar *cmapPtr = getCMap(fontData + cmapOffset, tableSize, &symbol, &cmapSize);
|
---|
476 | if (cmapPtr)
|
---|
477 | cmapOffset = cmapPtr - fontData;
|
---|
478 | else
|
---|
479 | cmapOffset = 0;
|
---|
480 | } else if (externalCMap) {
|
---|
481 | int tableSize = cmapSize;
|
---|
482 | externalCMap = getCMap(externalCMap, tableSize, &symbol, &cmapSize);
|
---|
483 | }
|
---|
484 |
|
---|
485 | // verify all the positions in the glyphMap
|
---|
486 | if (glyphMapOffset) {
|
---|
487 | const quint32 *gmapPtr = reinterpret_cast<const quint32 *>(fontData + glyphMapOffset);
|
---|
488 | for (uint i = 0; i < glyphMapEntries; ++i) {
|
---|
489 | quint32 glyphDataPos = qFromBigEndian<quint32>(gmapPtr[i]);
|
---|
490 | if (glyphDataPos == 0xffffffff)
|
---|
491 | continue;
|
---|
492 | if (glyphDataPos >= glyphDataSize) {
|
---|
493 | // error
|
---|
494 | glyphMapOffset = 0;
|
---|
495 | glyphMapEntries = 0;
|
---|
496 | break;
|
---|
497 | }
|
---|
498 | }
|
---|
499 | }
|
---|
500 |
|
---|
501 | #if defined(DEBUG_FONTENGINE)
|
---|
502 | if (!isValid())
|
---|
503 | qDebug() << "fontData" << fontData << "dataSize" << dataSize
|
---|
504 | << "externalCMap" << externalCMap << "cmapOffset" << cmapOffset
|
---|
505 | << "glyphMapOffset" << glyphMapOffset << "glyphDataOffset" << glyphDataOffset
|
---|
506 | << "fd" << fd << "glyphDataSize" << glyphDataSize;
|
---|
507 | #endif
|
---|
508 | #if defined(Q_WS_QWS)
|
---|
509 | if (isValid() && renderingFontEngine)
|
---|
510 | qt_fbdpy->sendFontCommand(QWSFontCommand::StartedUsingFont, encodedFileName);
|
---|
511 | #endif
|
---|
512 | }
|
---|
513 |
|
---|
514 | QFontEngineQPF::~QFontEngineQPF()
|
---|
515 | {
|
---|
516 | #if defined(Q_WS_QWS)
|
---|
517 | if (isValid() && renderingFontEngine) {
|
---|
518 | QT_TRY {
|
---|
519 | qt_fbdpy->sendFontCommand(QWSFontCommand::StoppedUsingFont, encodedFileName);
|
---|
520 | } QT_CATCH(...) {
|
---|
521 | qDebug("QFontEngineQPF::~QFontEngineQPF: Out of memory");
|
---|
522 | // ignore.
|
---|
523 | }
|
---|
524 | }
|
---|
525 | #endif
|
---|
526 | delete renderingFontEngine;
|
---|
527 | if (fontData) {
|
---|
528 | if (munmap((void *)fontData, dataSize) == -1) {
|
---|
529 | #if defined(DEBUG_FONTENGINE)
|
---|
530 | qErrnoWarning(errno, "~QFontEngineQPF: Unable to munmap");
|
---|
531 | #endif
|
---|
532 | }
|
---|
533 | }
|
---|
534 | if (fd != -1)
|
---|
535 | ::close(fd);
|
---|
536 | #if !defined(QT_NO_FREETYPE)
|
---|
537 | if (freetype)
|
---|
538 | freetype->release(face_id);
|
---|
539 | #endif
|
---|
540 | }
|
---|
541 |
|
---|
542 | bool QFontEngineQPF::getSfntTableData(uint tag, uchar *buffer, uint *length) const
|
---|
543 | {
|
---|
544 | #if !defined(QT_NO_FREETYPE)
|
---|
545 | if (freetype)
|
---|
546 | return freetype->getSfntTable(tag, buffer, length);
|
---|
547 | #endif
|
---|
548 | Q_UNUSED(tag);
|
---|
549 | Q_UNUSED(buffer);
|
---|
550 | *length = 0;
|
---|
551 | return false;
|
---|
552 | }
|
---|
553 |
|
---|
554 | bool QFontEngineQPF::stringToCMap(const QChar *str, int len, QGlyphLayout *glyphs, int *nglyphs, QTextEngine::ShaperFlags flags) const
|
---|
555 | {
|
---|
556 | if (!externalCMap && !cmapOffset && renderingFontEngine) {
|
---|
557 | if (!renderingFontEngine->stringToCMap(str, len, glyphs, nglyphs, flags))
|
---|
558 | return false;
|
---|
559 | #ifndef QT_NO_FREETYPE
|
---|
560 | const_cast<QFontEngineQPF *>(this)->ensureGlyphsLoaded(*glyphs);
|
---|
561 | #endif
|
---|
562 | return true;
|
---|
563 | }
|
---|
564 |
|
---|
565 | if (*nglyphs < len) {
|
---|
566 | *nglyphs = len;
|
---|
567 | return false;
|
---|
568 | }
|
---|
569 |
|
---|
570 | #if defined(DEBUG_FONTENGINE)
|
---|
571 | QSet<QChar> seenGlyphs;
|
---|
572 | #endif
|
---|
573 |
|
---|
574 | const uchar *cmap = externalCMap ? externalCMap : (fontData + cmapOffset);
|
---|
575 |
|
---|
576 | bool mirrored = flags & QTextEngine::RightToLeft;
|
---|
577 | int glyph_pos = 0;
|
---|
578 | if (symbol) {
|
---|
579 | for (int i = 0; i < len; ++i) {
|
---|
580 | unsigned int uc = getChar(str, i, len);
|
---|
581 | glyphs->glyphs[glyph_pos] = getTrueTypeGlyphIndex(cmap, uc);
|
---|
582 | if(!glyphs->glyphs[glyph_pos] && uc < 0x100)
|
---|
583 | glyphs->glyphs[glyph_pos] = getTrueTypeGlyphIndex(cmap, uc + 0xf000);
|
---|
584 | ++glyph_pos;
|
---|
585 | }
|
---|
586 | } else {
|
---|
587 | for (int i = 0; i < len; ++i) {
|
---|
588 | unsigned int uc = getChar(str, i, len);
|
---|
589 | if (mirrored)
|
---|
590 | uc = QChar::mirroredChar(uc);
|
---|
591 | glyphs->glyphs[glyph_pos] = getTrueTypeGlyphIndex(cmap, uc);
|
---|
592 | #if 0 && defined(DEBUG_FONTENGINE)
|
---|
593 | QChar c(uc);
|
---|
594 | if (!findGlyph(glyphs[glyph_pos].glyph) && !seenGlyphs.contains(c))
|
---|
595 | qDebug() << "glyph for character" << c << '/' << hex << uc << "is" << dec << glyphs[glyph_pos].glyph;
|
---|
596 |
|
---|
597 | seenGlyphs.insert(c);
|
---|
598 | #endif
|
---|
599 | ++glyph_pos;
|
---|
600 | }
|
---|
601 | }
|
---|
602 |
|
---|
603 | *nglyphs = glyph_pos;
|
---|
604 | glyphs->numGlyphs = glyph_pos;
|
---|
605 | recalcAdvances(glyphs, flags);
|
---|
606 | return true;
|
---|
607 | }
|
---|
608 |
|
---|
609 | void QFontEngineQPF::recalcAdvances(QGlyphLayout *glyphs, QTextEngine::ShaperFlags) const
|
---|
610 | {
|
---|
611 | #ifndef QT_NO_FREETYPE
|
---|
612 | const_cast<QFontEngineQPF *>(this)->ensureGlyphsLoaded(*glyphs);
|
---|
613 | #endif
|
---|
614 | for (int i = 0; i < glyphs->numGlyphs; ++i) {
|
---|
615 | const Glyph *g = findGlyph(glyphs->glyphs[i]);
|
---|
616 | if (!g) {
|
---|
617 | glyphs->glyphs[i] = 0;
|
---|
618 | continue;
|
---|
619 | }
|
---|
620 | glyphs->advances_x[i] = g->advance;
|
---|
621 | glyphs->advances_y[i] = 0;
|
---|
622 | }
|
---|
623 | }
|
---|
624 |
|
---|
625 | QImage QFontEngineQPF::alphaMapForGlyph(glyph_t g)
|
---|
626 | {
|
---|
627 | const Glyph *glyph = findGlyph(g);
|
---|
628 | if (!glyph)
|
---|
629 | return QImage();
|
---|
630 |
|
---|
631 | const uchar *bits = ((const uchar *) glyph) + sizeof(Glyph);
|
---|
632 |
|
---|
633 | QImage image(glyph->width, glyph->height, QImage::Format_Indexed8);
|
---|
634 | for (int j=0; j<256; ++j)
|
---|
635 | image.setColor(j, qRgba(0, 0, 0, j));
|
---|
636 |
|
---|
637 | for (int i=0; i<glyph->height; ++i) {
|
---|
638 | memcpy(image.scanLine(i), bits, glyph->bytesPerLine);
|
---|
639 | bits += glyph->bytesPerLine;
|
---|
640 | }
|
---|
641 | return image;
|
---|
642 | }
|
---|
643 |
|
---|
644 | void QFontEngineQPF::draw(QPaintEngine *p, qreal _x, qreal _y, const QTextItemInt &si)
|
---|
645 | {
|
---|
646 | QPaintEngineState *pState = p->state;
|
---|
647 | QRasterPaintEngine *paintEngine = static_cast<QRasterPaintEngine*>(p);
|
---|
648 |
|
---|
649 | QTransform matrix = pState->transform();
|
---|
650 | matrix.translate(_x, _y);
|
---|
651 | QFixed x = QFixed::fromReal(matrix.dx());
|
---|
652 | QFixed y = QFixed::fromReal(matrix.dy());
|
---|
653 |
|
---|
654 | QVarLengthArray<QFixedPoint> positions;
|
---|
655 | QVarLengthArray<glyph_t> glyphs;
|
---|
656 | getGlyphPositions(si.glyphs, matrix, si.flags, glyphs, positions);
|
---|
657 | if (glyphs.size() == 0)
|
---|
658 | return;
|
---|
659 |
|
---|
660 | for(int i = 0; i < glyphs.size(); i++) {
|
---|
661 | const Glyph *glyph = findGlyph(glyphs[i]);
|
---|
662 | if (!glyph)
|
---|
663 | continue;
|
---|
664 |
|
---|
665 | const int depth = 8; //###
|
---|
666 |
|
---|
667 | paintEngine->alphaPenBlt(reinterpret_cast<const uchar *>(glyph) + sizeof(Glyph), glyph->bytesPerLine, depth,
|
---|
668 | qRound(positions[i].x) + glyph->x,
|
---|
669 | qRound(positions[i].y) + glyph->y,
|
---|
670 | glyph->width, glyph->height);
|
---|
671 | }
|
---|
672 | }
|
---|
673 |
|
---|
674 | void QFontEngineQPF::addOutlineToPath(qreal x, qreal y, const QGlyphLayout &glyphs, QPainterPath *path, QTextItem::RenderFlags flags)
|
---|
675 | {
|
---|
676 | if (renderingFontEngine &&
|
---|
677 | (renderingFontEngine->type() != QFontEngine::Proxy
|
---|
678 | || static_cast<QProxyFontEngine *>(renderingFontEngine)->capabilities() & QAbstractFontEngine::CanOutlineGlyphs)) {
|
---|
679 | renderingFontEngine->addOutlineToPath(x, y, glyphs, path, flags);
|
---|
680 | return;
|
---|
681 | }
|
---|
682 | addBitmapFontToPath(x, y, glyphs, path, flags);
|
---|
683 | }
|
---|
684 |
|
---|
685 | glyph_metrics_t QFontEngineQPF::boundingBox(const QGlyphLayout &glyphs)
|
---|
686 | {
|
---|
687 | #ifndef QT_NO_FREETYPE
|
---|
688 | const_cast<QFontEngineQPF *>(this)->ensureGlyphsLoaded(glyphs);
|
---|
689 | #endif
|
---|
690 |
|
---|
691 | glyph_metrics_t overall;
|
---|
692 | // initialize with line height, we get the same behaviour on all platforms
|
---|
693 | overall.y = -ascent();
|
---|
694 | overall.height = ascent() + descent() + 1;
|
---|
695 |
|
---|
696 | QFixed ymax = 0;
|
---|
697 | QFixed xmax = 0;
|
---|
698 | for (int i = 0; i < glyphs.numGlyphs; i++) {
|
---|
699 | const Glyph *g = findGlyph(glyphs.glyphs[i]);
|
---|
700 | if (!g)
|
---|
701 | continue;
|
---|
702 |
|
---|
703 | QFixed x = overall.xoff + glyphs.offsets[i].x + g->x;
|
---|
704 | QFixed y = overall.yoff + glyphs.offsets[i].y + g->y;
|
---|
705 | overall.x = qMin(overall.x, x);
|
---|
706 | overall.y = qMin(overall.y, y);
|
---|
707 | xmax = qMax(xmax, x + g->width);
|
---|
708 | ymax = qMax(ymax, y + g->height);
|
---|
709 | overall.xoff += g->advance;
|
---|
710 | }
|
---|
711 | overall.height = qMax(overall.height, ymax - overall.y);
|
---|
712 | overall.width = xmax - overall.x;
|
---|
713 |
|
---|
714 | return overall;
|
---|
715 | }
|
---|
716 |
|
---|
717 | glyph_metrics_t QFontEngineQPF::boundingBox(glyph_t glyph)
|
---|
718 | {
|
---|
719 | #ifndef QT_NO_FREETYPE
|
---|
720 | {
|
---|
721 | QGlyphLayoutArray<1> tmp;
|
---|
722 | tmp.glyphs[0] = glyph;
|
---|
723 | const_cast<QFontEngineQPF *>(this)->ensureGlyphsLoaded(tmp);
|
---|
724 | }
|
---|
725 | #endif
|
---|
726 | glyph_metrics_t overall;
|
---|
727 | const Glyph *g = findGlyph(glyph);
|
---|
728 | if (!g)
|
---|
729 | return overall;
|
---|
730 | overall.x = g->x;
|
---|
731 | overall.y = g->y;
|
---|
732 | overall.width = g->width;
|
---|
733 | overall.height = g->height;
|
---|
734 | overall.xoff = g->advance;
|
---|
735 | return overall;
|
---|
736 | }
|
---|
737 |
|
---|
738 | QFixed QFontEngineQPF::ascent() const
|
---|
739 | {
|
---|
740 | return QFixed::fromReal(extractHeaderField(fontData, Tag_Ascent).value<qreal>());
|
---|
741 | }
|
---|
742 |
|
---|
743 | QFixed QFontEngineQPF::descent() const
|
---|
744 | {
|
---|
745 | return QFixed::fromReal(extractHeaderField(fontData, Tag_Descent).value<qreal>());
|
---|
746 | }
|
---|
747 |
|
---|
748 | QFixed QFontEngineQPF::leading() const
|
---|
749 | {
|
---|
750 | return QFixed::fromReal(extractHeaderField(fontData, Tag_Leading).value<qreal>());
|
---|
751 | }
|
---|
752 |
|
---|
753 | qreal QFontEngineQPF::maxCharWidth() const
|
---|
754 | {
|
---|
755 | return extractHeaderField(fontData, Tag_MaxCharWidth).value<qreal>();
|
---|
756 | }
|
---|
757 |
|
---|
758 | qreal QFontEngineQPF::minLeftBearing() const
|
---|
759 | {
|
---|
760 | return extractHeaderField(fontData, Tag_MinLeftBearing).value<qreal>();
|
---|
761 | }
|
---|
762 |
|
---|
763 | qreal QFontEngineQPF::minRightBearing() const
|
---|
764 | {
|
---|
765 | return extractHeaderField(fontData, Tag_MinRightBearing).value<qreal>();
|
---|
766 | }
|
---|
767 |
|
---|
768 | QFixed QFontEngineQPF::underlinePosition() const
|
---|
769 | {
|
---|
770 | return QFixed::fromReal(extractHeaderField(fontData, Tag_UnderlinePosition).value<qreal>());
|
---|
771 | }
|
---|
772 |
|
---|
773 | QFixed QFontEngineQPF::lineThickness() const
|
---|
774 | {
|
---|
775 | return QFixed::fromReal(extractHeaderField(fontData, Tag_LineThickness).value<qreal>());
|
---|
776 | }
|
---|
777 |
|
---|
778 | QFontEngine::Type QFontEngineQPF::type() const
|
---|
779 | {
|
---|
780 | return QFontEngine::QPF2;
|
---|
781 | }
|
---|
782 |
|
---|
783 | bool QFontEngineQPF::canRender(const QChar *string, int len)
|
---|
784 | {
|
---|
785 | const uchar *cmap = externalCMap ? externalCMap : (fontData + cmapOffset);
|
---|
786 |
|
---|
787 | if (symbol) {
|
---|
788 | for (int i = 0; i < len; ++i) {
|
---|
789 | unsigned int uc = getChar(string, i, len);
|
---|
790 | glyph_t g = getTrueTypeGlyphIndex(cmap, uc);
|
---|
791 | if(!g && uc < 0x100)
|
---|
792 | g = getTrueTypeGlyphIndex(cmap, uc + 0xf000);
|
---|
793 | if (!g)
|
---|
794 | return false;
|
---|
795 | }
|
---|
796 | } else {
|
---|
797 | for (int i = 0; i < len; ++i) {
|
---|
798 | unsigned int uc = getChar(string, i, len);
|
---|
799 | if (!getTrueTypeGlyphIndex(cmap, uc))
|
---|
800 | return false;
|
---|
801 | }
|
---|
802 | }
|
---|
803 | return true;
|
---|
804 | }
|
---|
805 |
|
---|
806 | bool QFontEngineQPF::isValid() const
|
---|
807 | {
|
---|
808 | return fontData && dataSize && (cmapOffset || externalCMap || renderingFontEngine)
|
---|
809 | && glyphMapOffset && glyphDataOffset && (fd >= 0 || glyphDataSize > 0);
|
---|
810 | }
|
---|
811 |
|
---|
812 | #if !defined(QT_NO_FREETYPE)
|
---|
813 | FT_Face QFontEngineQPF::lockFace() const
|
---|
814 | {
|
---|
815 | Q_ASSERT(freetype);
|
---|
816 | freetype->lock();
|
---|
817 | FT_Face face = freetype->face;
|
---|
818 |
|
---|
819 | // ### not perfect
|
---|
820 | const int ysize = qRound(fontDef.pixelSize * qreal(64));
|
---|
821 | const int xsize = ysize;
|
---|
822 |
|
---|
823 | if (freetype->xsize != xsize || freetype->ysize != ysize) {
|
---|
824 | FT_Set_Char_Size(face, xsize, ysize, 0, 0);
|
---|
825 | freetype->xsize = xsize;
|
---|
826 | freetype->ysize = ysize;
|
---|
827 | }
|
---|
828 | FT_Matrix identityMatrix;
|
---|
829 | identityMatrix.xx = 0x10000;
|
---|
830 | identityMatrix.yy = 0x10000;
|
---|
831 | identityMatrix.xy = 0;
|
---|
832 | identityMatrix.yx = 0;
|
---|
833 | if (freetype->matrix.xx != identityMatrix.xx ||
|
---|
834 | freetype->matrix.yy != identityMatrix.yy ||
|
---|
835 | freetype->matrix.xy != identityMatrix.xy ||
|
---|
836 | freetype->matrix.yx != identityMatrix.yx) {
|
---|
837 | freetype->matrix = identityMatrix;
|
---|
838 | FT_Set_Transform(face, &freetype->matrix, 0);
|
---|
839 | }
|
---|
840 | return face;
|
---|
841 | }
|
---|
842 |
|
---|
843 | void QFontEngineQPF::unlockFace() const
|
---|
844 | {
|
---|
845 | freetype->unlock();
|
---|
846 | }
|
---|
847 |
|
---|
848 | void QFontEngineQPF::doKerning(QGlyphLayout *g, QTextEngine::ShaperFlags flags) const
|
---|
849 | {
|
---|
850 | if (!kerning_pairs_loaded) {
|
---|
851 | kerning_pairs_loaded = true;
|
---|
852 | if (freetype) {
|
---|
853 | lockFace();
|
---|
854 | if (freetype->face->size->metrics.x_ppem != 0) {
|
---|
855 | QFixed scalingFactor(freetype->face->units_per_EM/freetype->face->size->metrics.x_ppem);
|
---|
856 | unlockFace();
|
---|
857 | const_cast<QFontEngineQPF *>(this)->loadKerningPairs(scalingFactor);
|
---|
858 | } else {
|
---|
859 | unlockFace();
|
---|
860 | }
|
---|
861 | }
|
---|
862 | }
|
---|
863 | QFontEngine::doKerning(g, flags);
|
---|
864 | }
|
---|
865 |
|
---|
866 | HB_Error QFontEngineQPF::getPointInOutline(HB_Glyph glyph, int flags, hb_uint32 point, HB_Fixed *xpos, HB_Fixed *ypos, hb_uint32 *nPoints)
|
---|
867 | {
|
---|
868 | if (!freetype)
|
---|
869 | return HB_Err_Not_Covered;
|
---|
870 | lockFace();
|
---|
871 | HB_Error result = freetype->getPointInOutline(glyph, flags, point, xpos, ypos, nPoints);
|
---|
872 | unlockFace();
|
---|
873 | return result;
|
---|
874 | }
|
---|
875 |
|
---|
876 | QFixed QFontEngineQPF::emSquareSize() const
|
---|
877 | {
|
---|
878 | if (!freetype)
|
---|
879 | return QFontEngine::emSquareSize();
|
---|
880 | if (FT_IS_SCALABLE(freetype->face))
|
---|
881 | return freetype->face->units_per_EM;
|
---|
882 | else
|
---|
883 | return freetype->face->size->metrics.y_ppem;
|
---|
884 | }
|
---|
885 |
|
---|
886 | void QFontEngineQPF::ensureGlyphsLoaded(const QGlyphLayout &glyphs)
|
---|
887 | {
|
---|
888 | if (readOnly)
|
---|
889 | return;
|
---|
890 | bool locked = false;
|
---|
891 | for (int i = 0; i < glyphs.numGlyphs; ++i) {
|
---|
892 | if (!glyphs.glyphs[i])
|
---|
893 | continue;
|
---|
894 | const Glyph *g = findGlyph(glyphs.glyphs[i]);
|
---|
895 | if (g)
|
---|
896 | continue;
|
---|
897 | if (!locked) {
|
---|
898 | if (!lockFile())
|
---|
899 | return;
|
---|
900 | locked = true;
|
---|
901 | g = findGlyph(glyphs.glyphs[i]);
|
---|
902 | if (g)
|
---|
903 | continue;
|
---|
904 | }
|
---|
905 | loadGlyph(glyphs.glyphs[i]);
|
---|
906 | }
|
---|
907 | if (locked) {
|
---|
908 | unlockFile();
|
---|
909 | #if defined(DEBUG_FONTENGINE)
|
---|
910 | qDebug() << "Finished rendering glyphs\n";
|
---|
911 | #endif
|
---|
912 | }
|
---|
913 | }
|
---|
914 |
|
---|
915 | void QFontEngineQPF::loadGlyph(glyph_t glyph)
|
---|
916 | {
|
---|
917 | quint32 glyphPos = ~0;
|
---|
918 |
|
---|
919 | if (!renderingFontEngine)
|
---|
920 | return;
|
---|
921 | QImage img = renderingFontEngine->alphaMapForGlyph(glyph);
|
---|
922 | if (img.format() != QImage::Format_Indexed8) {
|
---|
923 | bool mono = img.depth() == 1;
|
---|
924 | img = img.convertToFormat(QImage::Format_Indexed8);
|
---|
925 | if (mono) {
|
---|
926 | //### we know that 1 is opaque and 0 is transparent
|
---|
927 | uchar *byte = img.bits();
|
---|
928 | int count = img.byteCount();
|
---|
929 | while (count--)
|
---|
930 | *byte++ *= 0xff;
|
---|
931 | }
|
---|
932 | }
|
---|
933 | glyph_metrics_t metrics = renderingFontEngine->boundingBox(glyph);
|
---|
934 | renderingFontEngine->removeGlyphFromCache(glyph);
|
---|
935 |
|
---|
936 | off_t oldSize = ::lseek(fd, 0, SEEK_END);
|
---|
937 | if (oldSize == (off_t)-1)
|
---|
938 | return;
|
---|
939 |
|
---|
940 | Glyph g;
|
---|
941 | g.width = img.width();
|
---|
942 | g.height = img.height();
|
---|
943 | g.bytesPerLine = img.bytesPerLine();
|
---|
944 | g.x = qRound(metrics.x);
|
---|
945 | g.y = qRound(metrics.y);
|
---|
946 | g.advance = qRound(metrics.xoff);
|
---|
947 |
|
---|
948 | QT_WRITE(fd, &g, sizeof(g));
|
---|
949 | QT_WRITE(fd, img.bits(), img.byteCount());
|
---|
950 |
|
---|
951 | glyphPos = oldSize - glyphDataOffset;
|
---|
952 | #if 0 && defined(DEBUG_FONTENGINE)
|
---|
953 | qDebug() << "glyphPos for new glyph" << glyph << "is" << glyphPos << "oldSize" << oldSize << "glyphDataOffset" << glyphDataOffset;
|
---|
954 | #endif
|
---|
955 |
|
---|
956 | quint32 *gmap = (quint32 *)(fontData + glyphMapOffset);
|
---|
957 | gmap[glyph] = qToBigEndian(glyphPos);
|
---|
958 |
|
---|
959 | glyphDataSize = glyphPos + sizeof(g) + img.byteCount();
|
---|
960 | quint32 *blockSizePtr = (quint32 *)(fontData + glyphDataOffset - 4);
|
---|
961 | *blockSizePtr = qToBigEndian(glyphDataSize);
|
---|
962 | }
|
---|
963 |
|
---|
964 | bool QFontEngineQPF::lockFile()
|
---|
965 | {
|
---|
966 | // #### this does not handle the case when the process holding the
|
---|
967 | // lock hangs for some reason
|
---|
968 | struct flock lock;
|
---|
969 | lock.l_type = F_WRLCK;
|
---|
970 | lock.l_whence = SEEK_SET;
|
---|
971 | lock.l_start = 0;
|
---|
972 | lock.l_len = 0; // lock the whole file
|
---|
973 | while (fcntl(fd, F_SETLKW, &lock) != 0) {
|
---|
974 | if (errno == EINTR)
|
---|
975 | continue;
|
---|
976 | perror("locking qpf");
|
---|
977 | return false;
|
---|
978 | }
|
---|
979 | Header *header = (Header *)fontData;
|
---|
980 | if (header->lock) {
|
---|
981 | lock.l_type = F_UNLCK;
|
---|
982 | if (fcntl(fd, F_SETLK, &lock) != 0)
|
---|
983 | perror("unlocking possibly corrupt qpf");
|
---|
984 | return false;
|
---|
985 | }
|
---|
986 | #if defined(Q_WS_QWS)
|
---|
987 | extern int qws_client_id;
|
---|
988 | // qws_client_id == 0 means we're the server. in this case we just
|
---|
989 | // set the id to 1
|
---|
990 | header->lock = qws_client_id ? qws_client_id : 1;
|
---|
991 | #else
|
---|
992 | header->lock = 1;
|
---|
993 | #endif
|
---|
994 | return true;
|
---|
995 | }
|
---|
996 |
|
---|
997 | void QFontEngineQPF::unlockFile()
|
---|
998 | {
|
---|
999 | ((Header *)fontData)->lock = 0;
|
---|
1000 |
|
---|
1001 | struct flock lock;
|
---|
1002 | lock.l_type = F_UNLCK;
|
---|
1003 | lock.l_whence = SEEK_SET;
|
---|
1004 | lock.l_start = 0;
|
---|
1005 | lock.l_len = 0; // lock the whole file
|
---|
1006 | if (fcntl(fd, F_SETLK, &lock) != 0) {
|
---|
1007 | perror("unlocking qpf");
|
---|
1008 | }
|
---|
1009 |
|
---|
1010 | remapFontData();
|
---|
1011 | }
|
---|
1012 |
|
---|
1013 | void QFontEngineQPF::remapFontData()
|
---|
1014 | {
|
---|
1015 | off_t newFileSize = ::lseek(fd, 0, SEEK_END);
|
---|
1016 | if (newFileSize == (off_t)-1) {
|
---|
1017 | #ifdef DEBUG_FONTENGINE
|
---|
1018 | perror("QFontEngineQPF::remapFontData: lseek failed");
|
---|
1019 | #endif
|
---|
1020 | fontData = 0;
|
---|
1021 | return;
|
---|
1022 | }
|
---|
1023 |
|
---|
1024 | #ifndef QT_NO_MREMAP
|
---|
1025 | fontData = static_cast<uchar *>(::mremap(const_cast<uchar *>(fontData), dataSize, newFileSize, MREMAP_MAYMOVE));
|
---|
1026 | if (!fontData || fontData == (const uchar *)MAP_FAILED) {
|
---|
1027 | # if defined(DEBUG_FONTENGINE)
|
---|
1028 | perror("QFontEngineQPF::remapFontData(): mremap failed");
|
---|
1029 | # endif
|
---|
1030 | fontData = 0;
|
---|
1031 | }
|
---|
1032 |
|
---|
1033 | if (!fontData)
|
---|
1034 | #endif // QT_NO_MREMAP
|
---|
1035 | {
|
---|
1036 | int status = ::munmap((void *)fontData, dataSize);
|
---|
1037 | if (status != 0)
|
---|
1038 | qErrnoWarning(status, "QFontEngineQPF::remapFomrData: munmap failed!");
|
---|
1039 |
|
---|
1040 | fontData = (const uchar *)::mmap(0, newFileSize, PROT_READ | (renderingFontEngine ? PROT_WRITE : 0),
|
---|
1041 | MAP_SHARED, fd, 0);
|
---|
1042 | if (!fontData || fontData == (const uchar *)MAP_FAILED) {
|
---|
1043 | # if defined(DEBUG_FONTENGINE)
|
---|
1044 | perror("mmap failed");
|
---|
1045 | # endif
|
---|
1046 | fontData = 0;
|
---|
1047 | return;
|
---|
1048 | }
|
---|
1049 | }
|
---|
1050 |
|
---|
1051 | dataSize = newFileSize;
|
---|
1052 | glyphDataSize = newFileSize - glyphDataOffset;
|
---|
1053 | #if defined(DEBUG_FONTENGINE)
|
---|
1054 | qDebug() << "remapped the font file to" << newFileSize << "bytes";
|
---|
1055 | #endif
|
---|
1056 | }
|
---|
1057 |
|
---|
1058 | #endif // QT_NO_FREETYPE
|
---|
1059 |
|
---|
1060 | void QPFGenerator::generate()
|
---|
1061 | {
|
---|
1062 | writeHeader();
|
---|
1063 | writeGMap();
|
---|
1064 | writeBlock(QFontEngineQPF::GlyphBlock, QByteArray());
|
---|
1065 |
|
---|
1066 | dev->seek(4); // position of header.lock
|
---|
1067 | writeUInt32(0);
|
---|
1068 | }
|
---|
1069 |
|
---|
1070 | void QPFGenerator::writeHeader()
|
---|
1071 | {
|
---|
1072 | QFontEngineQPF::Header header;
|
---|
1073 |
|
---|
1074 | header.magic[0] = 'Q';
|
---|
1075 | header.magic[1] = 'P';
|
---|
1076 | header.magic[2] = 'F';
|
---|
1077 | header.magic[3] = '2';
|
---|
1078 | header.lock = 1;
|
---|
1079 | header.majorVersion = QFontEngineQPF::CurrentMajorVersion;
|
---|
1080 | header.minorVersion = QFontEngineQPF::CurrentMinorVersion;
|
---|
1081 | header.dataSize = 0;
|
---|
1082 | dev->write((const char *)&header, sizeof(header));
|
---|
1083 |
|
---|
1084 | writeTaggedString(QFontEngineQPF::Tag_FontName, fe->fontDef.family.toUtf8());
|
---|
1085 |
|
---|
1086 | QFontEngine::FaceId face = fe->faceId();
|
---|
1087 | writeTaggedString(QFontEngineQPF::Tag_FileName, face.filename);
|
---|
1088 | writeTaggedUInt32(QFontEngineQPF::Tag_FileIndex, face.index);
|
---|
1089 |
|
---|
1090 | {
|
---|
1091 | uchar data[4];
|
---|
1092 | uint len = 4;
|
---|
1093 | bool ok = fe->getSfntTableData(MAKE_TAG('h', 'e', 'a', 'd'), data, &len);
|
---|
1094 | if (ok) {
|
---|
1095 | const quint32 revision = qFromBigEndian<quint32>(data);
|
---|
1096 | writeTaggedUInt32(QFontEngineQPF::Tag_FontRevision, revision);
|
---|
1097 | }
|
---|
1098 | }
|
---|
1099 |
|
---|
1100 | writeTaggedQFixed(QFontEngineQPF::Tag_Ascent, fe->ascent());
|
---|
1101 | writeTaggedQFixed(QFontEngineQPF::Tag_Descent, fe->descent());
|
---|
1102 | writeTaggedQFixed(QFontEngineQPF::Tag_Leading, fe->leading());
|
---|
1103 | writeTaggedQFixed(QFontEngineQPF::Tag_XHeight, fe->xHeight());
|
---|
1104 | writeTaggedQFixed(QFontEngineQPF::Tag_AverageCharWidth, fe->averageCharWidth());
|
---|
1105 | writeTaggedQFixed(QFontEngineQPF::Tag_MaxCharWidth, QFixed::fromReal(fe->maxCharWidth()));
|
---|
1106 | writeTaggedQFixed(QFontEngineQPF::Tag_LineThickness, fe->lineThickness());
|
---|
1107 | writeTaggedQFixed(QFontEngineQPF::Tag_MinLeftBearing, QFixed::fromReal(fe->minLeftBearing()));
|
---|
1108 | writeTaggedQFixed(QFontEngineQPF::Tag_MinRightBearing, QFixed::fromReal(fe->minRightBearing()));
|
---|
1109 | writeTaggedQFixed(QFontEngineQPF::Tag_UnderlinePosition, fe->underlinePosition());
|
---|
1110 | writeTaggedUInt8(QFontEngineQPF::Tag_PixelSize, fe->fontDef.pixelSize);
|
---|
1111 | writeTaggedUInt8(QFontEngineQPF::Tag_Weight, fe->fontDef.weight);
|
---|
1112 | writeTaggedUInt8(QFontEngineQPF::Tag_Style, fe->fontDef.style);
|
---|
1113 |
|
---|
1114 | writeTaggedUInt8(QFontEngineQPF::Tag_GlyphFormat, QFontEngineQPF::AlphamapGlyphs);
|
---|
1115 |
|
---|
1116 | writeTaggedString(QFontEngineQPF::Tag_EndOfHeader, QByteArray());
|
---|
1117 | align4();
|
---|
1118 |
|
---|
1119 | const quint64 size = dev->pos();
|
---|
1120 | header.dataSize = qToBigEndian<quint16>(size - sizeof(header));
|
---|
1121 | dev->seek(0);
|
---|
1122 | dev->write((const char *)&header, sizeof(header));
|
---|
1123 | dev->seek(size);
|
---|
1124 | }
|
---|
1125 |
|
---|
1126 | void QPFGenerator::writeGMap()
|
---|
1127 | {
|
---|
1128 | const quint16 glyphCount = fe->glyphCount();
|
---|
1129 |
|
---|
1130 | writeUInt16(QFontEngineQPF::GMapBlock);
|
---|
1131 | writeUInt16(0); // padding
|
---|
1132 | writeUInt32(glyphCount * 4);
|
---|
1133 |
|
---|
1134 | QByteArray &buffer = dev->buffer();
|
---|
1135 | const int numBytes = glyphCount * sizeof(quint32);
|
---|
1136 | qint64 pos = buffer.size();
|
---|
1137 | buffer.resize(pos + numBytes);
|
---|
1138 | qMemSet(buffer.data() + pos, 0xff, numBytes);
|
---|
1139 | dev->seek(pos + numBytes);
|
---|
1140 | }
|
---|
1141 |
|
---|
1142 | void QPFGenerator::writeBlock(QFontEngineQPF::BlockTag tag, const QByteArray &data)
|
---|
1143 | {
|
---|
1144 | writeUInt16(tag);
|
---|
1145 | writeUInt16(0); // padding
|
---|
1146 | const int padSize = ((data.size() + 3) / 4) * 4 - data.size();
|
---|
1147 | writeUInt32(data.size() + padSize);
|
---|
1148 | dev->write(data);
|
---|
1149 | for (int i = 0; i < padSize; ++i)
|
---|
1150 | writeUInt8(0);
|
---|
1151 | }
|
---|
1152 |
|
---|
1153 | void QPFGenerator::writeTaggedString(QFontEngineQPF::HeaderTag tag, const QByteArray &string)
|
---|
1154 | {
|
---|
1155 | writeUInt16(tag);
|
---|
1156 | writeUInt16(string.length());
|
---|
1157 | dev->write(string);
|
---|
1158 | }
|
---|
1159 |
|
---|
1160 | void QPFGenerator::writeTaggedUInt32(QFontEngineQPF::HeaderTag tag, quint32 value)
|
---|
1161 | {
|
---|
1162 | writeUInt16(tag);
|
---|
1163 | writeUInt16(sizeof(value));
|
---|
1164 | writeUInt32(value);
|
---|
1165 | }
|
---|
1166 |
|
---|
1167 | void QPFGenerator::writeTaggedUInt8(QFontEngineQPF::HeaderTag tag, quint8 value)
|
---|
1168 | {
|
---|
1169 | writeUInt16(tag);
|
---|
1170 | writeUInt16(sizeof(value));
|
---|
1171 | writeUInt8(value);
|
---|
1172 | }
|
---|
1173 |
|
---|
1174 | void QPFGenerator::writeTaggedQFixed(QFontEngineQPF::HeaderTag tag, QFixed value)
|
---|
1175 | {
|
---|
1176 | writeUInt16(tag);
|
---|
1177 | writeUInt16(sizeof(quint32));
|
---|
1178 | writeUInt32(value.value());
|
---|
1179 | }
|
---|
1180 |
|
---|
1181 | #endif // QT_NO_QWS_QPF2
|
---|
1182 |
|
---|
1183 | /*
|
---|
1184 | Creates a new multi qws engine.
|
---|
1185 |
|
---|
1186 | This function takes ownership of the QFontEngine, increasing it's refcount.
|
---|
1187 | */
|
---|
1188 | QFontEngineMultiQWS::QFontEngineMultiQWS(QFontEngine *fe, int _script, const QStringList &fallbacks)
|
---|
1189 | : QFontEngineMulti(fallbacks.size() + 1),
|
---|
1190 | fallbackFamilies(fallbacks), script(_script)
|
---|
1191 | {
|
---|
1192 | engines[0] = fe;
|
---|
1193 | fe->ref.ref();
|
---|
1194 | fontDef = engines[0]->fontDef;
|
---|
1195 | }
|
---|
1196 |
|
---|
1197 | void QFontEngineMultiQWS::loadEngine(int at)
|
---|
1198 | {
|
---|
1199 | Q_ASSERT(at < engines.size());
|
---|
1200 | Q_ASSERT(engines.at(at) == 0);
|
---|
1201 |
|
---|
1202 | QFontDef request = fontDef;
|
---|
1203 | request.styleStrategy |= QFont::NoFontMerging;
|
---|
1204 | request.family = fallbackFamilies.at(at-1);
|
---|
1205 | engines[at] = QFontDatabase::findFont(script,
|
---|
1206 | /*fontprivate*/0,
|
---|
1207 | request);
|
---|
1208 | Q_ASSERT(engines[at]);
|
---|
1209 | engines[at]->ref.ref();
|
---|
1210 | engines[at]->fontDef = request;
|
---|
1211 | }
|
---|
1212 |
|
---|
1213 | void QFontEngineMultiQWS::draw(QPaintEngine */*p*/, qreal /*x*/, qreal /*y*/, const QTextItemInt &/*si*/)
|
---|
1214 | {
|
---|
1215 | qFatal("QFontEngineMultiQWS::draw should never be called!");
|
---|
1216 | }
|
---|
1217 |
|
---|
1218 | QT_END_NAMESPACE
|
---|