source: trunk/src/gui/text/qfontengine.cpp@ 219

Last change on this file since 219 was 125, checked in by Dmitry A. Kuminov, 16 years ago

gui: Added QFontEnginePMFT that makes use of FreeType to render fonts and enabled by default (no proper font matching yet, HELV.PFB is always used so far).

File size: 49.8 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** 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 are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <qdebug.h>
43#include <private/qfontengine_p.h>
44
45#include "qbitmap.h"
46#include "qpainter.h"
47#include "qpainterpath.h"
48#include "qvarlengtharray.h"
49#include <private/qpdf_p.h>
50#include <qmath.h>
51#include <qendian.h>
52#include <private/qharfbuzz_p.h>
53
54QT_BEGIN_NAMESPACE
55
56static inline bool qtransform_equals_no_translate(const QTransform &a, const QTransform &b)
57{
58 if (a.type() <= QTransform::TxTranslate && b.type() <= QTransform::TxTranslate) {
59 return true;
60 } else {
61 // We always use paths for perspective text anyway, so no
62 // point in checking the full matrix...
63 Q_ASSERT(a.type() < QTransform::TxProject);
64 Q_ASSERT(b.type() < QTransform::TxProject);
65
66 return a.m11() == b.m11()
67 && a.m12() == b.m12()
68 && a.m21() == b.m21()
69 && a.m22() == b.m22();
70 }
71}
72
73
74
75QFontEngineGlyphCache::~QFontEngineGlyphCache()
76{
77}
78
79// Harfbuzz helper functions
80
81static HB_Bool hb_stringToGlyphs(HB_Font font, const HB_UChar16 *string, hb_uint32 length, HB_Glyph *glyphs, hb_uint32 *numGlyphs, HB_Bool rightToLeft)
82{
83 QFontEngine *fe = (QFontEngine *)font->userData;
84
85 QVarLengthGlyphLayoutArray qglyphs(*numGlyphs);
86
87 QTextEngine::ShaperFlags shaperFlags(QTextEngine::GlyphIndicesOnly);
88 if (rightToLeft)
89 shaperFlags |= QTextEngine::RightToLeft;
90
91 int nGlyphs = *numGlyphs;
92 bool result = fe->stringToCMap(reinterpret_cast<const QChar *>(string), length, &qglyphs, &nGlyphs, shaperFlags);
93 *numGlyphs = nGlyphs;
94 if (!result)
95 return false;
96
97 for (hb_uint32 i = 0; i < *numGlyphs; ++i)
98 glyphs[i] = qglyphs.glyphs[i];
99
100 return true;
101}
102
103static void hb_getAdvances(HB_Font font, const HB_Glyph *glyphs, hb_uint32 numGlyphs, HB_Fixed *advances, int flags)
104{
105 QFontEngine *fe = (QFontEngine *)font->userData;
106
107 QVarLengthGlyphLayoutArray qglyphs(numGlyphs);
108
109 for (hb_uint32 i = 0; i < numGlyphs; ++i)
110 qglyphs.glyphs[i] = glyphs[i];
111
112 fe->recalcAdvances(&qglyphs, flags & HB_ShaperFlag_UseDesignMetrics ? QFlags<QTextEngine::ShaperFlag>(QTextEngine::DesignMetrics) : QFlags<QTextEngine::ShaperFlag>(0));
113
114 for (hb_uint32 i = 0; i < numGlyphs; ++i)
115 advances[i] = qglyphs.advances_x[i].value();
116}
117
118static HB_Bool hb_canRender(HB_Font font, const HB_UChar16 *string, hb_uint32 length)
119{
120 QFontEngine *fe = (QFontEngine *)font->userData;
121 return fe->canRender(reinterpret_cast<const QChar *>(string), length);
122}
123
124static void hb_getGlyphMetrics(HB_Font font, HB_Glyph glyph, HB_GlyphMetrics *metrics)
125{
126 QFontEngine *fe = (QFontEngine *)font->userData;
127 glyph_metrics_t m = fe->boundingBox(glyph);
128 metrics->x = m.x.value();
129 metrics->y = m.y.value();
130 metrics->width = m.width.value();
131 metrics->height = m.height.value();
132 metrics->xOffset = m.xoff.value();
133 metrics->yOffset = m.yoff.value();
134}
135
136static HB_Fixed hb_getFontMetric(HB_Font font, HB_FontMetric metric)
137{
138 if (metric == HB_FontAscent) {
139 QFontEngine *fe = (QFontEngine *)font->userData;
140 return fe->ascent().value();
141 }
142 return 0;
143}
144
145HB_Error QFontEngine::getPointInOutline(HB_Glyph glyph, int flags, hb_uint32 point, HB_Fixed *xpos, HB_Fixed *ypos, hb_uint32 *nPoints)
146{
147 Q_UNUSED(glyph)
148 Q_UNUSED(flags)
149 Q_UNUSED(point)
150 Q_UNUSED(xpos)
151 Q_UNUSED(ypos)
152 Q_UNUSED(nPoints)
153 return HB_Err_Not_Covered;
154}
155
156static HB_Error hb_getPointInOutline(HB_Font font, HB_Glyph glyph, int flags, hb_uint32 point, HB_Fixed *xpos, HB_Fixed *ypos, hb_uint32 *nPoints)
157{
158 QFontEngine *fe = (QFontEngine *)font->userData;
159 return fe->getPointInOutline(glyph, flags, point, xpos, ypos, nPoints);
160}
161
162static const HB_FontClass hb_fontClass = {
163 hb_stringToGlyphs, hb_getAdvances, hb_canRender, hb_getPointInOutline,
164 hb_getGlyphMetrics, hb_getFontMetric
165};
166
167static HB_Error hb_getSFntTable(void *font, HB_Tag tableTag, HB_Byte *buffer, HB_UInt *length)
168{
169 QFontEngine *fe = (QFontEngine *)font;
170 if (!fe->getSfntTableData(tableTag, buffer, length))
171 return HB_Err_Invalid_Argument;
172 return HB_Err_Ok;
173}
174
175// QFontEngine
176
177QFontEngine::QFontEngine()
178 : QObject()
179{
180 ref = 0;
181 cache_count = 0;
182 fsType = 0;
183 symbol = false;
184 memset(&hbFont, 0, sizeof(hbFont));
185 hbFont.klass = &hb_fontClass;
186 hbFont.userData = this;
187
188 hbFace = 0;
189 glyphFormat = -1;
190}
191
192QFontEngine::~QFontEngine()
193{
194 for (GlyphPointerHash::iterator it = m_glyphPointerHash.begin(), end = m_glyphPointerHash.end();
195 it != end; ++it) {
196 for (QList<QFontEngineGlyphCache*>::iterator it2 = it.value().begin(), end2 = it.value().end();
197 it2 != end2; ++it2)
198 delete *it2;
199 }
200 m_glyphPointerHash.clear();
201 for (GlyphIntHash::iterator it = m_glyphIntHash.begin(), end = m_glyphIntHash.end();
202 it != end; ++it) {
203 for (QList<QFontEngineGlyphCache*>::iterator it2 = it.value().begin(), end2 = it.value().end();
204 it2 != end2; ++it2)
205 delete *it2;
206 }
207 m_glyphIntHash.clear();
208 qHBFreeFace(hbFace);
209}
210
211QFixed QFontEngine::lineThickness() const
212{
213 // ad hoc algorithm
214 int score = fontDef.weight * fontDef.pixelSize;
215 int lw = score / 700;
216
217 // looks better with thicker line for small pointsizes
218 if (lw < 2 && score >= 1050) lw = 2;
219 if (lw == 0) lw = 1;
220
221 return lw;
222}
223
224QFixed QFontEngine::underlinePosition() const
225{
226 return ((lineThickness() * 2) + 3) / 6;
227}
228
229HB_Font QFontEngine::harfbuzzFont() const
230{
231 if (!hbFont.x_ppem) {
232 QFixed emSquare = emSquareSize();
233 hbFont.x_ppem = fontDef.pixelSize;
234 hbFont.y_ppem = fontDef.pixelSize * fontDef.stretch / 100;
235 hbFont.x_scale = (QFixed(hbFont.x_ppem * (1 << 16)) / emSquare).value();
236 hbFont.y_scale = (QFixed(hbFont.y_ppem * (1 << 16)) / emSquare).value();
237 }
238 return &hbFont;
239}
240
241HB_Face QFontEngine::harfbuzzFace() const
242{
243 if (!hbFace)
244 hbFace = qHBNewFace(const_cast<QFontEngine *>(this), hb_getSFntTable);
245 return hbFace;
246}
247
248glyph_metrics_t QFontEngine::boundingBox(glyph_t glyph, const QTransform &matrix)
249{
250 glyph_metrics_t metrics = boundingBox(glyph);
251
252 if (matrix.type() > QTransform::TxTranslate) {
253 return metrics.transformed(matrix);
254 }
255 return metrics;
256}
257
258QFixed QFontEngine::xHeight() const
259{
260 QGlyphLayoutArray<8> glyphs;
261 int nglyphs = 7;
262 QChar x((ushort)'x');
263 stringToCMap(&x, 1, &glyphs, &nglyphs, QTextEngine::GlyphIndicesOnly);
264
265 glyph_metrics_t bb = const_cast<QFontEngine *>(this)->boundingBox(glyphs.glyphs[0]);
266 return bb.height;
267}
268
269QFixed QFontEngine::averageCharWidth() const
270{
271 QGlyphLayoutArray<8> glyphs;
272 int nglyphs = 7;
273 QChar x((ushort)'x');
274 stringToCMap(&x, 1, &glyphs, &nglyphs, QTextEngine::GlyphIndicesOnly);
275
276 glyph_metrics_t bb = const_cast<QFontEngine *>(this)->boundingBox(glyphs.glyphs[0]);
277 return bb.xoff;
278}
279
280
281void QFontEngine::getGlyphPositions(const QGlyphLayout &glyphs, const QTransform &matrix, QTextItem::RenderFlags flags,
282 QVarLengthArray<glyph_t> &glyphs_out, QVarLengthArray<QFixedPoint> &positions)
283{
284 QFixed xpos;
285 QFixed ypos;
286
287 const bool transform = matrix.m11() != 1.
288 || matrix.m12() != 0.
289 || matrix.m21() != 0.
290 || matrix.m22() != 1.;
291 if (!transform) {
292 xpos = QFixed::fromReal(matrix.dx());
293 ypos = QFixed::fromReal(matrix.dy());
294 }
295
296 int current = 0;
297 if (flags & QTextItem::RightToLeft) {
298 int i = glyphs.numGlyphs;
299 int totalKashidas = 0;
300 while(i--) {
301 xpos += glyphs.advances_x[i] + QFixed::fromFixed(glyphs.justifications[i].space_18d6);
302 ypos += glyphs.advances_y[i];
303 totalKashidas += glyphs.justifications[i].nKashidas;
304 }
305 positions.resize(glyphs.numGlyphs+totalKashidas);
306 glyphs_out.resize(glyphs.numGlyphs+totalKashidas);
307
308 i = 0;
309 while(i < glyphs.numGlyphs) {
310 if (glyphs.attributes[i].dontPrint) {
311 ++i;
312 continue;
313 }
314 xpos -= glyphs.advances_x[i];
315 ypos -= glyphs.advances_y[i];
316
317 QFixed gpos_x = xpos + glyphs.offsets[i].x;
318 QFixed gpos_y = ypos + glyphs.offsets[i].y;
319 if (transform) {
320 QPointF gpos(gpos_x.toReal(), gpos_y.toReal());
321 gpos = gpos * matrix;
322 gpos_x = QFixed::fromReal(gpos.x());
323 gpos_y = QFixed::fromReal(gpos.y());
324 }
325 positions[current].x = gpos_x;
326 positions[current].y = gpos_y;
327 glyphs_out[current] = glyphs.glyphs[i];
328 ++current;
329 if (glyphs.justifications[i].nKashidas) {
330 QChar ch(0x640); // Kashida character
331 QGlyphLayoutArray<8> g;
332 int nglyphs = 7;
333 stringToCMap(&ch, 1, &g, &nglyphs, 0);
334 for (uint k = 0; k < glyphs.justifications[i].nKashidas; ++k) {
335 xpos -= g.advances_x[0];
336 ypos -= g.advances_y[0];
337
338 QFixed gpos_x = xpos + glyphs.offsets[i].x;
339 QFixed gpos_y = ypos + glyphs.offsets[i].y;
340 if (transform) {
341 QPointF gpos(gpos_x.toReal(), gpos_y.toReal());
342 gpos = gpos * matrix;
343 gpos_x = QFixed::fromReal(gpos.x());
344 gpos_y = QFixed::fromReal(gpos.y());
345 }
346 positions[current].x = gpos_x;
347 positions[current].y = gpos_y;
348 glyphs_out[current] = g.glyphs[0];
349 ++current;
350 }
351 } else {
352 xpos -= QFixed::fromFixed(glyphs.justifications[i].space_18d6);
353 }
354 ++i;
355 }
356 } else {
357 positions.resize(glyphs.numGlyphs);
358 glyphs_out.resize(glyphs.numGlyphs);
359 int i = 0;
360 if (!transform) {
361 while (i < glyphs.numGlyphs) {
362 if (!glyphs.attributes[i].dontPrint) {
363 positions[current].x = xpos + glyphs.offsets[i].x;
364 positions[current].y = ypos + glyphs.offsets[i].y;
365 glyphs_out[current] = glyphs.glyphs[i];
366 xpos += glyphs.advances_x[i] + QFixed::fromFixed(glyphs.justifications[i].space_18d6);
367 ypos += glyphs.advances_y[i];
368 ++current;
369 }
370 ++i;
371 }
372 } else {
373 positions.resize(glyphs.numGlyphs);
374 glyphs_out.resize(glyphs.numGlyphs);
375 int i = 0;
376 while (i < glyphs.numGlyphs) {
377 if (!glyphs.attributes[i].dontPrint) {
378 QFixed gpos_x = xpos + glyphs.offsets[i].x;
379 QFixed gpos_y = ypos + glyphs.offsets[i].y;
380 QPointF gpos(gpos_x.toReal(), gpos_y.toReal());
381 gpos = gpos * matrix;
382 positions[current].x = QFixed::fromReal(gpos.x());
383 positions[current].y = QFixed::fromReal(gpos.y());
384 glyphs_out[current] = glyphs.glyphs[i];
385 xpos += glyphs.advances_x[i] + QFixed::fromFixed(glyphs.justifications[i].space_18d6);
386 ypos += glyphs.advances_y[i];
387 ++current;
388 }
389 ++i;
390 }
391 }
392 }
393 positions.resize(current);
394 glyphs_out.resize(current);
395 Q_ASSERT(positions.size() == glyphs_out.size());
396}
397
398
399glyph_metrics_t QFontEngine::tightBoundingBox(const QGlyphLayout &glyphs)
400{
401 glyph_metrics_t overall;
402
403 QFixed ymax = 0;
404 QFixed xmax = 0;
405 for (int i = 0; i < glyphs.numGlyphs; i++) {
406 glyph_metrics_t bb = boundingBox(glyphs.glyphs[i]);
407 QFixed x = overall.xoff + glyphs.offsets[i].x + bb.x;
408 QFixed y = overall.yoff + glyphs.offsets[i].y + bb.y;
409 overall.x = qMin(overall.x, x);
410 overall.y = qMin(overall.y, y);
411 xmax = qMax(xmax, x + bb.width);
412 ymax = qMax(ymax, y + bb.height);
413 overall.xoff += bb.xoff;
414 overall.yoff += bb.yoff;
415 }
416 overall.height = qMax(overall.height, ymax - overall.y);
417 overall.width = xmax - overall.x;
418
419 return overall;
420}
421
422
423void QFontEngine::addOutlineToPath(qreal x, qreal y, const QGlyphLayout &glyphs, QPainterPath *path,
424 QTextItem::RenderFlags flags)
425{
426 if (!glyphs.numGlyphs)
427 return;
428
429 QVarLengthArray<QFixedPoint> positions;
430 QVarLengthArray<glyph_t> positioned_glyphs;
431 QTransform matrix;
432 matrix.translate(x, y);
433 getGlyphPositions(glyphs, matrix, flags, positioned_glyphs, positions);
434 addGlyphsToPath(positioned_glyphs.data(), positions.data(), positioned_glyphs.size(), path, flags);
435}
436
437#define GRID(x, y) grid[(y)*(w+1) + (x)]
438#define SET(x, y) (*(image_data + (y)*bpl + ((x) >> 3)) & (0x80 >> ((x) & 7)))
439
440enum { EdgeRight = 0x1,
441 EdgeDown = 0x2,
442 EdgeLeft = 0x4,
443 EdgeUp = 0x8
444};
445
446static void collectSingleContour(qreal x0, qreal y0, uint *grid, int x, int y, int w, int h, QPainterPath *path)
447{
448 Q_UNUSED(h);
449
450 path->moveTo(x + x0, y + y0);
451 while (GRID(x, y)) {
452 if (GRID(x, y) & EdgeRight) {
453 while (GRID(x, y) & EdgeRight) {
454 GRID(x, y) &= ~EdgeRight;
455 ++x;
456 }
457 Q_ASSERT(x <= w);
458 path->lineTo(x + x0, y + y0);
459 continue;
460 }
461 if (GRID(x, y) & EdgeDown) {
462 while (GRID(x, y) & EdgeDown) {
463 GRID(x, y) &= ~EdgeDown;
464 ++y;
465 }
466 Q_ASSERT(y <= h);
467 path->lineTo(x + x0, y + y0);
468 continue;
469 }
470 if (GRID(x, y) & EdgeLeft) {
471 while (GRID(x, y) & EdgeLeft) {
472 GRID(x, y) &= ~EdgeLeft;
473 --x;
474 }
475 Q_ASSERT(x >= 0);
476 path->lineTo(x + x0, y + y0);
477 continue;
478 }
479 if (GRID(x, y) & EdgeUp) {
480 while (GRID(x, y) & EdgeUp) {
481 GRID(x, y) &= ~EdgeUp;
482 --y;
483 }
484 Q_ASSERT(y >= 0);
485 path->lineTo(x + x0, y + y0);
486 continue;
487 }
488 }
489 path->closeSubpath();
490}
491
492void qt_addBitmapToPath(qreal x0, qreal y0, const uchar *image_data, int bpl, int w, int h, QPainterPath *path)
493{
494 uint *grid = new uint[(w+1)*(h+1)];
495 // set up edges
496 for (int y = 0; y <= h; ++y) {
497 for (int x = 0; x <= w; ++x) {
498 bool topLeft = (x == 0)|(y == 0) ? false : SET(x - 1, y - 1);
499 bool topRight = (x == w)|(y == 0) ? false : SET(x, y - 1);
500 bool bottomLeft = (x == 0)|(y == h) ? false : SET(x - 1, y);
501 bool bottomRight = (x == w)|(y == h) ? false : SET(x, y);
502
503 GRID(x, y) = 0;
504 if ((!topRight) & bottomRight)
505 GRID(x, y) |= EdgeRight;
506 if ((!bottomRight) & bottomLeft)
507 GRID(x, y) |= EdgeDown;
508 if ((!bottomLeft) & topLeft)
509 GRID(x, y) |= EdgeLeft;
510 if ((!topLeft) & topRight)
511 GRID(x, y) |= EdgeUp;
512 }
513 }
514
515 // collect edges
516 for (int y = 0; y < h; ++y) {
517 for (int x = 0; x < w; ++x) {
518 if (!GRID(x, y))
519 continue;
520 // found start of a contour, follow it
521 collectSingleContour(x0, y0, grid, x, y, w, h, path);
522 }
523 }
524 delete [] grid;
525}
526
527#undef GRID
528#undef SET
529
530
531void QFontEngine::addBitmapFontToPath(qreal x, qreal y, const QGlyphLayout &glyphs,
532 QPainterPath *path, QTextItem::RenderFlags flags)
533{
534// TODO what to do with 'flags' ??
535 Q_UNUSED(flags);
536 QFixed advanceX = QFixed::fromReal(x);
537 QFixed advanceY = QFixed::fromReal(y);
538 for (int i=0; i < glyphs.numGlyphs; ++i) {
539 glyph_metrics_t metrics = boundingBox(glyphs.glyphs[i]);
540 if (metrics.width.value() == 0 || metrics.height.value() == 0) {
541 advanceX += glyphs.advances_x[i];
542 advanceY += glyphs.advances_y[i];
543 continue;
544 }
545 const QImage alphaMask = alphaMapForGlyph(glyphs.glyphs[i]);
546
547 const int w = alphaMask.width();
548 const int h = alphaMask.height();
549 const int srcBpl = alphaMask.bytesPerLine();
550 QImage bitmap;
551 if (alphaMask.depth() == 1) {
552 bitmap = alphaMask;
553 } else {
554 bitmap = QImage(w, h, QImage::Format_Mono);
555 const uchar *imageData = alphaMask.bits();
556 const int destBpl = bitmap.bytesPerLine();
557 uchar *bitmapData = bitmap.bits();
558
559 for (int yi = 0; yi < h; ++yi) {
560 const uchar *src = imageData + yi*srcBpl;
561 uchar *dst = bitmapData + yi*destBpl;
562 for (int xi = 0; xi < w; ++xi) {
563 const int byte = xi / 8;
564 const int bit = xi % 8;
565 if (bit == 0)
566 dst[byte] = 0;
567 if (src[xi])
568 dst[byte] |= 128 >> bit;
569 }
570 }
571 }
572 const uchar *bitmap_data = bitmap.bits();
573 QFixedPoint offset = glyphs.offsets[i];
574 advanceX += offset.x;
575 advanceY += offset.y;
576 qt_addBitmapToPath((advanceX + metrics.x).toReal(), (advanceY + metrics.y).toReal(), bitmap_data, bitmap.bytesPerLine(), w, h, path);
577 advanceX += glyphs.advances_x[i];
578 advanceY += glyphs.advances_y[i];
579 }
580}
581
582void QFontEngine::addGlyphsToPath(glyph_t *glyphs, QFixedPoint *positions, int nGlyphs,
583 QPainterPath *path, QTextItem::RenderFlags flags)
584{
585 qreal x = positions[0].x.toReal();
586 qreal y = positions[0].y.toReal();
587 QVarLengthGlyphLayoutArray g(nGlyphs);
588
589 for (int i = 0; i < nGlyphs; ++i) {
590 g.glyphs[i] = glyphs[i];
591 if (i < nGlyphs - 1) {
592 g.advances_x[i] = positions[i+1].x - positions[i].x;
593 g.advances_y[i] = positions[i+1].y - positions[i].y;
594 } else {
595 g.advances_x[i] = QFixed::fromReal(maxCharWidth());
596 g.advances_y[i] = 0;
597 }
598 }
599
600 addBitmapFontToPath(x, y, g, path, flags);
601}
602
603QImage QFontEngine::alphaMapForGlyph(glyph_t glyph, const QTransform &t)
604{
605 QImage i = alphaMapForGlyph(glyph);
606 if (t.type() > QTransform::TxTranslate)
607 i = i.transformed(t);
608 Q_ASSERT(i.depth() <= 8); // To verify that transformed didn't change the format...
609 return i;
610}
611
612QImage QFontEngine::alphaRGBMapForGlyph(glyph_t glyph, int /* margin */, const QTransform &t)
613{
614 QImage alphaMask = alphaMapForGlyph(glyph, t);
615 QImage rgbMask(alphaMask.width(), alphaMask.height(), QImage::Format_RGB32);
616
617 for (int y=0; y<alphaMask.height(); ++y) {
618 uint *dst = (uint *) rgbMask.scanLine(y);
619 uchar *src = (uchar *) alphaMask.scanLine(y);
620 for (int x=0; x<alphaMask.width(); ++x)
621 dst[x] = qRgb(src[x], src[x], src[x]);
622 }
623
624 return rgbMask;
625}
626
627
628void QFontEngine::removeGlyphFromCache(glyph_t)
629{
630}
631
632QFontEngine::Properties QFontEngine::properties() const
633{
634 Properties p;
635#ifndef QT_NO_PRINTER
636 QByteArray psname = QPdf::stripSpecialCharacters(fontDef.family.toUtf8());
637#else
638 QByteArray psname = fontDef.family.toUtf8();
639#endif
640 psname += '-';
641 psname += QByteArray::number(fontDef.style);
642 psname += '-';
643 psname += QByteArray::number(fontDef.weight);
644
645 p.postscriptName = psname;
646 p.ascent = ascent();
647 p.descent = descent();
648 p.leading = leading();
649 p.emSquare = p.ascent;
650 p.boundingBox = QRectF(0, -p.ascent.toReal(), maxCharWidth(), (p.ascent + p.descent).toReal());
651 p.italicAngle = 0;
652 p.capHeight = p.ascent;
653 p.lineWidth = lineThickness();
654 return p;
655}
656
657void QFontEngine::getUnscaledGlyph(glyph_t glyph, QPainterPath *path, glyph_metrics_t *metrics)
658{
659 *metrics = boundingBox(glyph);
660 QFixedPoint p;
661 p.x = 0;
662 p.y = 0;
663 addGlyphsToPath(&glyph, &p, 1, path, QFlag(0));
664}
665
666QByteArray QFontEngine::getSfntTable(uint tag) const
667{
668 QByteArray table;
669 uint len = 0;
670 if (!getSfntTableData(tag, 0, &len))
671 return table;
672 if (!len)
673 return table;
674 table.resize(len);
675 if (!getSfntTableData(tag, reinterpret_cast<uchar *>(table.data()), &len))
676 return QByteArray();
677 return table;
678}
679
680void QFontEngine::expireGlyphCache()
681{
682 if (m_glyphCacheQueue.count() > 10) { // hold only 10 caches in memory.
683 QFontEngineGlyphCache *old = m_glyphCacheQueue.takeFirst();
684 // remove the value from either of our hashes
685 for (GlyphPointerHash::iterator i = m_glyphPointerHash.begin(); i != m_glyphPointerHash.end(); ++i) {
686 QList<QFontEngineGlyphCache *> list = i.value();
687 if (list.removeAll(old)) {
688 if (list.isEmpty())
689 m_glyphPointerHash.remove(i.key());
690 else
691 m_glyphPointerHash.insert(i.key(), list);
692 break;
693 }
694 }
695 for (GlyphIntHash::iterator i = m_glyphIntHash.begin(); i != m_glyphIntHash.end(); ++i) {
696 QList<QFontEngineGlyphCache *> list = i.value();
697 if (list.removeAll(old)) {
698 if (list.isEmpty())
699 m_glyphIntHash.remove(i.key());
700 else
701 m_glyphIntHash.insert(i.key(), list);
702 break;
703 }
704 }
705 delete old;
706 }
707}
708
709void QFontEngine::setGlyphCache(void *key, QFontEngineGlyphCache *data)
710{
711 Q_ASSERT(data);
712 QList<QFontEngineGlyphCache*> items = m_glyphPointerHash.value(key);
713
714 for (QList<QFontEngineGlyphCache*>::iterator it = items.begin(), end = items.end(); it != end; ++it) {
715 QFontEngineGlyphCache *c = *it;
716 if (qtransform_equals_no_translate(c->m_transform, data->m_transform)) {
717 if (c == data)
718 return;
719 items.removeAll(c);
720 delete c;
721 break;
722 }
723 }
724 items.append(data);
725 m_glyphPointerHash.insert(key, items);
726
727 m_glyphCacheQueue.append(data);
728 expireGlyphCache();
729}
730
731void QFontEngine::setGlyphCache(QFontEngineGlyphCache::Type key, QFontEngineGlyphCache *data)
732{
733 Q_ASSERT(data);
734 QList<QFontEngineGlyphCache*> items = m_glyphIntHash.value(key);
735
736 for (QList<QFontEngineGlyphCache*>::iterator it = items.begin(), end = items.end(); it != end; ++it) {
737 QFontEngineGlyphCache *c = *it;
738 if (qtransform_equals_no_translate(c->m_transform, data->m_transform)) {
739 if (c == data)
740 return;
741 items.removeAll(c);
742 delete c;
743 break;
744 }
745 }
746 items.append(data);
747 m_glyphIntHash.insert(key, items);
748
749 m_glyphCacheQueue.append(data);
750 expireGlyphCache();
751}
752
753QFontEngineGlyphCache *QFontEngine::glyphCache(void *key, const QTransform &transform) const
754{
755 QList<QFontEngineGlyphCache*> items = m_glyphPointerHash.value(key);
756
757 for (QList<QFontEngineGlyphCache*>::iterator it = items.begin(), end = items.end(); it != end; ++it) {
758 QFontEngineGlyphCache *c = *it;
759 if (qtransform_equals_no_translate(c->m_transform, transform)) {
760 m_glyphCacheQueue.removeAll(c); // last used, move it up
761 m_glyphCacheQueue.append(c);
762 return c;
763 }
764 }
765 return 0;
766}
767
768QFontEngineGlyphCache *QFontEngine::glyphCache(QFontEngineGlyphCache::Type key, const QTransform &transform) const
769{
770 QList<QFontEngineGlyphCache*> items = m_glyphIntHash.value(key);
771
772 for (QList<QFontEngineGlyphCache*>::iterator it = items.begin(), end = items.end(); it != end; ++it) {
773 QFontEngineGlyphCache *c = *it;
774 if (qtransform_equals_no_translate(c->m_transform, transform)) {
775 m_glyphCacheQueue.removeAll(c); // last used, move it up
776 m_glyphCacheQueue.append(c);
777 return c;
778 }
779 }
780 return 0;
781}
782
783#if defined(Q_WS_WIN) || defined(Q_WS_PM) || defined(Q_WS_X11) || defined(Q_WS_QWS)
784static inline QFixed kerning(int left, int right, const QFontEngine::KernPair *pairs, int numPairs)
785{
786 uint left_right = (left << 16) + right;
787
788 left = 0, right = numPairs - 1;
789 while (left <= right) {
790 int middle = left + ( ( right - left ) >> 1 );
791
792 if(pairs[middle].left_right == left_right)
793 return pairs[middle].adjust;
794
795 if (pairs[middle].left_right < left_right)
796 left = middle + 1;
797 else
798 right = middle - 1;
799 }
800 return 0;
801}
802
803void QFontEngine::doKerning(QGlyphLayout *glyphs, QTextEngine::ShaperFlags flags) const
804{
805 int numPairs = kerning_pairs.size();
806 if(!numPairs)
807 return;
808
809 const KernPair *pairs = kerning_pairs.constData();
810
811 if(flags & QTextEngine::DesignMetrics) {
812 for(int i = 0; i < glyphs->numGlyphs - 1; ++i)
813 glyphs->advances_x[i] += kerning(glyphs->glyphs[i], glyphs->glyphs[i+1] , pairs, numPairs);
814 } else {
815 for(int i = 0; i < glyphs->numGlyphs - 1; ++i)
816 glyphs->advances_x[i] += qRound(kerning(glyphs->glyphs[i], glyphs->glyphs[i+1] , pairs, numPairs));
817 }
818}
819
820void QFontEngine::loadKerningPairs(QFixed scalingFactor)
821{
822 kerning_pairs.clear();
823
824 QByteArray tab = getSfntTable(MAKE_TAG('k', 'e', 'r', 'n'));
825 if (tab.isEmpty())
826 return;
827
828 const uchar *table = reinterpret_cast<const uchar *>(tab.constData());
829
830 unsigned short version = qFromBigEndian<quint16>(table);
831 if (version != 0) {
832// qDebug("wrong version");
833 return;
834 }
835
836 unsigned short numTables = qFromBigEndian<quint16>(table + 2);
837 {
838 int offset = 4;
839 for(int i = 0; i < numTables; ++i) {
840 if (offset + 6 > tab.size()) {
841// qDebug("offset out of bounds");
842 goto end;
843 }
844 const uchar *header = table + offset;
845
846 ushort version = qFromBigEndian<quint16>(header);
847 ushort length = qFromBigEndian<quint16>(header+2);
848 ushort coverage = qFromBigEndian<quint16>(header+4);
849// qDebug("subtable: version=%d, coverage=%x",version, coverage);
850 if(version == 0 && coverage == 0x0001) {
851 if (offset + length > tab.size()) {
852// qDebug("length ouf ot bounds");
853 goto end;
854 }
855 const uchar *data = table + offset + 6;
856
857 ushort nPairs = qFromBigEndian<quint16>(data);
858 if(nPairs * 6 + 8 > length - 6) {
859// qDebug("corrupt table!");
860 // corrupt table
861 goto end;
862 }
863
864 int off = 8;
865 for(int i = 0; i < nPairs; ++i) {
866 QFontEngine::KernPair p;
867 p.left_right = (((uint)qFromBigEndian<quint16>(data+off)) << 16) + qFromBigEndian<quint16>(data+off+2);
868 p.adjust = QFixed(((int)(short)qFromBigEndian<quint16>(data+off+4))) / scalingFactor;
869 kerning_pairs.append(p);
870 off += 6;
871 }
872 }
873 offset += length;
874 }
875 }
876end:
877 qSort(kerning_pairs);
878// for (int i = 0; i < kerning_pairs.count(); ++i)
879// qDebug() << "i" << i << "left_right" << hex << kerning_pairs.at(i).left_right;
880}
881
882#else
883void QFontEngine::doKerning(QGlyphLayout *, QTextEngine::ShaperFlags) const
884{
885}
886#endif
887
888int QFontEngine::glyphCount() const
889{
890 QByteArray maxpTable = getSfntTable(MAKE_TAG('m', 'a', 'x', 'p'));
891 if (maxpTable.size() < 6)
892 return 0;
893 return qFromBigEndian<quint16>(reinterpret_cast<const uchar *>(maxpTable.constData() + 4));
894}
895
896const uchar *QFontEngine::getCMap(const uchar *table, uint tableSize, bool *isSymbolFont, int *cmapSize)
897{
898 const uchar *header = table;
899 if (tableSize < 4)
900 return 0;
901
902 const uchar *endPtr = table + tableSize;
903
904 // version check
905 if (qFromBigEndian<quint16>(header) != 0)
906 return 0;
907
908 unsigned short numTables = qFromBigEndian<quint16>(header + 2);
909 const uchar *maps = table + 4;
910 if (maps + 8 * numTables > endPtr)
911 return 0;
912
913 int tableToUse = -1;
914 int score = 0;
915 for (int n = 0; n < numTables; ++n) {
916 const quint16 platformId = qFromBigEndian<quint16>(maps + 8 * n);
917 const quint16 platformSpecificId = qFromBigEndian<quint16>(maps + 8 * n + 2);
918 switch (platformId) {
919 case 0: // Unicode
920 if (score < 4 &&
921 (platformSpecificId == 0 ||
922 platformSpecificId == 2 ||
923 platformSpecificId == 3)) {
924 tableToUse = n;
925 score = 4;
926 } else if (score < 3 && platformSpecificId == 1) {
927 tableToUse = n;
928 score = 3;
929 }
930 break;
931 case 1: // Apple
932 if (score < 2 && platformSpecificId == 0) { // Apple Roman
933 tableToUse = n;
934 score = 2;
935 }
936 break;
937 case 3: // Microsoft
938 switch (platformSpecificId) {
939 case 0:
940 if (score < 1) {
941 tableToUse = n;
942 score = 1;
943 }
944 break;
945 case 1:
946 if (score < 5) {
947 tableToUse = n;
948 score = 5;
949 }
950 break;
951 case 0xa:
952 if (score < 6) {
953 tableToUse = n;
954 score = 6;
955 }
956 break;
957 default:
958 break;
959 }
960 default:
961 break;
962 }
963 }
964 if(tableToUse < 0)
965 return 0;
966 *isSymbolFont = (score == 1);
967
968 unsigned int unicode_table = qFromBigEndian<quint32>(maps + 8*tableToUse + 4);
969
970 if (!unicode_table || unicode_table + 8 > tableSize)
971 return 0;
972
973 // get the header of the unicode table
974 header = table + unicode_table;
975
976 unsigned short format = qFromBigEndian<quint16>(header);
977 unsigned int length;
978 if(format < 8)
979 length = qFromBigEndian<quint16>(header + 2);
980 else
981 length = qFromBigEndian<quint32>(header + 4);
982
983 if (table + unicode_table + length > endPtr)
984 return 0;
985 *cmapSize = length;
986 return table + unicode_table;
987}
988
989quint32 QFontEngine::getTrueTypeGlyphIndex(const uchar *cmap, uint unicode)
990{
991 unsigned short format = qFromBigEndian<quint16>(cmap);
992 if (format == 0) {
993 if (unicode < 256)
994 return (int) *(cmap+6+unicode);
995 } else if (format == 4) {
996 /* some fonts come with invalid cmap tables, where the last segment
997 specified end = start = rangeoffset = 0xffff, delta = 0x0001
998 Since 0xffff is never a valid Unicode char anyway, we just get rid of the issue
999 by returning 0 for 0xffff
1000 */
1001 if(unicode >= 0xffff)
1002 return 0;
1003 quint16 segCountX2 = qFromBigEndian<quint16>(cmap + 6);
1004 const unsigned char *ends = cmap + 14;
1005 quint16 endIndex = 0;
1006 int i = 0;
1007 for (; i < segCountX2/2 && (endIndex = qFromBigEndian<quint16>(ends + 2*i)) < unicode; i++) {}
1008
1009 const unsigned char *idx = ends + segCountX2 + 2 + 2*i;
1010 quint16 startIndex = qFromBigEndian<quint16>(idx);
1011
1012 if (startIndex > unicode)
1013 return 0;
1014
1015 idx += segCountX2;
1016 qint16 idDelta = (qint16)qFromBigEndian<quint16>(idx);
1017 idx += segCountX2;
1018 quint16 idRangeoffset_t = (quint16)qFromBigEndian<quint16>(idx);
1019
1020 quint16 glyphIndex;
1021 if (idRangeoffset_t) {
1022 quint16 id = qFromBigEndian<quint16>(idRangeoffset_t + 2*(unicode - startIndex) + idx);
1023 if (id)
1024 glyphIndex = (idDelta + id) % 0x10000;
1025 else
1026 glyphIndex = 0;
1027 } else {
1028 glyphIndex = (idDelta + unicode) % 0x10000;
1029 }
1030 return glyphIndex;
1031 } else if (format == 6) {
1032 quint16 tableSize = qFromBigEndian<quint16>(cmap + 2);
1033
1034 quint16 firstCode6 = qFromBigEndian<quint16>(cmap + 6);
1035 if (unicode < firstCode6)
1036 return 0;
1037
1038 quint16 entryCount6 = qFromBigEndian<quint16>(cmap + 8);
1039 if (entryCount6 * 2 + 10 > tableSize)
1040 return 0;
1041
1042 quint16 sentinel6 = firstCode6 + entryCount6;
1043 if (unicode >= sentinel6)
1044 return 0;
1045
1046 quint16 entryIndex6 = unicode - firstCode6;
1047 return qFromBigEndian<quint16>(cmap + 10 + (entryIndex6 * 2));
1048 } else if (format == 12) {
1049 quint32 nGroups = qFromBigEndian<quint32>(cmap + 12);
1050
1051 cmap += 16; // move to start of groups
1052
1053 int left = 0, right = nGroups - 1;
1054 while (left <= right) {
1055 int middle = left + ( ( right - left ) >> 1 );
1056
1057 quint32 startCharCode = qFromBigEndian<quint32>(cmap + 12*middle);
1058 if(unicode < startCharCode)
1059 right = middle - 1;
1060 else {
1061 quint32 endCharCode = qFromBigEndian<quint32>(cmap + 12*middle + 4);
1062 if(unicode <= endCharCode)
1063 return qFromBigEndian<quint32>(cmap + 12*middle + 8) + unicode - startCharCode;
1064 left = middle + 1;
1065 }
1066 }
1067 } else {
1068 qDebug("cmap table of format %d not implemented", format);
1069 }
1070
1071 return 0;
1072}
1073
1074// ------------------------------------------------------------------
1075// The box font engine
1076// ------------------------------------------------------------------
1077
1078QFontEngineBox::QFontEngineBox(int size)
1079 : _size(size)
1080{
1081 cache_cost = sizeof(QFontEngineBox);
1082}
1083
1084QFontEngineBox::~QFontEngineBox()
1085{
1086}
1087
1088bool QFontEngineBox::stringToCMap(const QChar *, int len, QGlyphLayout *glyphs, int *nglyphs, QTextEngine::ShaperFlags) const
1089{
1090 if (*nglyphs < len) {
1091 *nglyphs = len;
1092 return false;
1093 }
1094
1095 for (int i = 0; i < len; i++) {
1096 glyphs->glyphs[i] = 0;
1097 glyphs->advances_x[i] = _size;
1098 glyphs->advances_y[i] = 0;
1099 }
1100
1101 *nglyphs = len;
1102 glyphs->numGlyphs = len;
1103 return true;
1104}
1105
1106void QFontEngineBox::recalcAdvances(QGlyphLayout *glyphs, QTextEngine::ShaperFlags) const
1107{
1108 for (int i = 0; i < glyphs->numGlyphs; i++) {
1109 glyphs->advances_x[i] = _size;
1110 glyphs->advances_y[i] = 0;
1111 }
1112}
1113
1114void QFontEngineBox::addOutlineToPath(qreal x, qreal y, const QGlyphLayout &glyphs, QPainterPath *path, QTextItem::RenderFlags flags)
1115{
1116 if (!glyphs.numGlyphs)
1117 return;
1118
1119 QVarLengthArray<QFixedPoint> positions;
1120 QVarLengthArray<glyph_t> positioned_glyphs;
1121 QTransform matrix;
1122 matrix.translate(x, y - _size);
1123 getGlyphPositions(glyphs, matrix, flags, positioned_glyphs, positions);
1124
1125 QSize s(_size - 3, _size - 3);
1126 for (int k = 0; k < positions.size(); k++)
1127 path->addRect(QRectF(positions[k].toPointF(), s));
1128}
1129
1130glyph_metrics_t QFontEngineBox::boundingBox(const QGlyphLayout &glyphs)
1131{
1132 glyph_metrics_t overall;
1133 overall.width = _size*glyphs.numGlyphs;
1134 overall.height = _size;
1135 overall.xoff = overall.width;
1136 return overall;
1137}
1138
1139#if defined(Q_WS_QWS)
1140void QFontEngineBox::draw(QPaintEngine *p, qreal x, qreal y, const QTextItemInt &ti)
1141{
1142 if (!ti.glyphs.numGlyphs)
1143 return;
1144
1145 // any fixes here should probably also be done in QPaintEnginePrivate::drawBoxTextItem
1146 QSize s(_size - 3, _size - 3);
1147
1148 QVarLengthArray<QFixedPoint> positions;
1149 QVarLengthArray<glyph_t> glyphs;
1150 QTransform matrix;
1151 matrix.translate(x, y - _size);
1152 ti.fontEngine->getGlyphPositions(ti.glyphs, matrix, ti.flags, glyphs, positions);
1153 if (glyphs.size() == 0)
1154 return;
1155
1156
1157 QPainter *painter = p->painter();
1158 painter->save();
1159 painter->setBrush(Qt::NoBrush);
1160 QPen pen = painter->pen();
1161 pen.setWidthF(lineThickness().toReal());
1162 painter->setPen(pen);
1163 for (int k = 0; k < positions.size(); k++)
1164 painter->drawRect(QRectF(positions[k].toPointF(), s));
1165 painter->restore();
1166}
1167#endif
1168
1169glyph_metrics_t QFontEngineBox::boundingBox(glyph_t)
1170{
1171 return glyph_metrics_t(0, -_size, _size, _size, _size, 0);
1172}
1173
1174
1175
1176QFixed QFontEngineBox::ascent() const
1177{
1178 return _size;
1179}
1180
1181QFixed QFontEngineBox::descent() const
1182{
1183 return 0;
1184}
1185
1186QFixed QFontEngineBox::leading() const
1187{
1188 QFixed l = _size * QFixed::fromReal(qreal(0.15));
1189 return l.ceil();
1190}
1191
1192qreal QFontEngineBox::maxCharWidth() const
1193{
1194 return _size;
1195}
1196
1197#ifdef Q_WS_X11
1198int QFontEngineBox::cmap() const
1199{
1200 return -1;
1201}
1202#endif
1203
1204const char *QFontEngineBox::name() const
1205{
1206 return "null";
1207}
1208
1209bool QFontEngineBox::canRender(const QChar *, int)
1210{
1211 return true;
1212}
1213
1214QFontEngine::Type QFontEngineBox::type() const
1215{
1216 return Box;
1217}
1218
1219QImage QFontEngineBox::alphaMapForGlyph(glyph_t)
1220{
1221 QImage image(_size, _size, QImage::Format_Indexed8);
1222 QVector<QRgb> colors(256);
1223 for (int i=0; i<256; ++i)
1224 colors[i] = qRgba(0, 0, 0, i);
1225 image.setColorTable(colors);
1226 image.fill(0);
1227
1228 // can't use qpainter for index8; so use setPixel to draw our rectangle.
1229 for (int i=2; i <= _size-3; ++i) {
1230 image.setPixel(i, 2, 255);
1231 image.setPixel(i, _size-3, 255);
1232 image.setPixel(2, i, 255);
1233 image.setPixel(_size-3, i, 255);
1234 }
1235 return image;
1236}
1237
1238// ------------------------------------------------------------------
1239// Multi engine
1240// ------------------------------------------------------------------
1241
1242static inline uchar highByte(glyph_t glyph)
1243{ return glyph >> 24; }
1244
1245// strip high byte from glyph
1246static inline glyph_t stripped(glyph_t glyph)
1247{ return glyph & 0x00ffffff; }
1248
1249QFontEngineMulti::QFontEngineMulti(int engineCount)
1250{
1251 engines.fill(0, engineCount);
1252 cache_cost = 0;
1253}
1254
1255QFontEngineMulti::~QFontEngineMulti()
1256{
1257 for (int i = 0; i < engines.size(); ++i) {
1258 QFontEngine *fontEngine = engines.at(i);
1259 if (fontEngine) {
1260 fontEngine->ref.deref();
1261 if (fontEngine->cache_count == 0 && fontEngine->ref == 0)
1262 delete fontEngine;
1263 }
1264 }
1265}
1266
1267bool QFontEngineMulti::stringToCMap(const QChar *str, int len,
1268 QGlyphLayout *glyphs, int *nglyphs,
1269 QTextEngine::ShaperFlags flags) const
1270{
1271 int ng = *nglyphs;
1272 if (!engine(0)->stringToCMap(str, len, glyphs, &ng, flags))
1273 return false;
1274
1275 int glyph_pos = 0;
1276 for (int i = 0; i < len; ++i) {
1277 bool surrogate = (str[i].unicode() >= 0xd800 && str[i].unicode() < 0xdc00 && i < len-1
1278 && str[i+1].unicode() >= 0xdc00 && str[i+1].unicode() < 0xe000);
1279 if (glyphs->glyphs[glyph_pos] == 0) {
1280
1281 QGlyphLayoutInstance tmp = glyphs->instance(glyph_pos);
1282 for (int x = 1; x < engines.size(); ++x) {
1283 QFontEngine *engine = engines.at(x);
1284 if (!engine) {
1285 const_cast<QFontEngineMulti *>(this)->loadEngine(x);
1286 engine = engines.at(x);
1287 }
1288 Q_ASSERT(engine != 0);
1289 if (engine->type() == Box)
1290 continue;
1291 glyphs->advances_x[glyph_pos] = glyphs->advances_y[glyph_pos] = 0;
1292 glyphs->offsets[glyph_pos] = QFixedPoint();
1293 int num = 2;
1294 QGlyphLayout offs = glyphs->mid(glyph_pos, num);
1295 engine->stringToCMap(str + i, surrogate ? 2 : 1, &offs, &num, flags);
1296 Q_ASSERT(num == 1); // surrogates only give 1 glyph
1297 if (glyphs->glyphs[glyph_pos]) {
1298 // set the high byte to indicate which engine the glyph came from
1299 glyphs->glyphs[glyph_pos] |= (x << 24);
1300 break;
1301 }
1302 }
1303 // ensure we use metrics from the 1st font when we use the fallback image.
1304 if (!glyphs->glyphs[glyph_pos]) {
1305 glyphs->setInstance(glyph_pos, tmp);
1306 }
1307 }
1308 if (surrogate)
1309 ++i;
1310 ++glyph_pos;
1311 }
1312
1313 *nglyphs = ng;
1314 glyphs->numGlyphs = ng;
1315 return true;
1316}
1317
1318glyph_metrics_t QFontEngineMulti::boundingBox(const QGlyphLayout &glyphs)
1319{
1320 if (glyphs.numGlyphs <= 0)
1321 return glyph_metrics_t();
1322
1323 glyph_metrics_t overall;
1324
1325 int which = highByte(glyphs.glyphs[0]);
1326 int start = 0;
1327 int end, i;
1328 for (end = 0; end < glyphs.numGlyphs; ++end) {
1329 const int e = highByte(glyphs.glyphs[end]);
1330 if (e == which)
1331 continue;
1332
1333 // set the high byte to zero
1334 for (i = start; i < end; ++i)
1335 glyphs.glyphs[i] = stripped(glyphs.glyphs[i]);
1336
1337 // merge the bounding box for this run
1338 const glyph_metrics_t gm = engine(which)->boundingBox(glyphs.mid(start, end - start));
1339
1340 overall.x = qMin(overall.x, gm.x);
1341 overall.y = qMin(overall.y, gm.y);
1342 overall.width = overall.xoff + gm.width;
1343 overall.height = qMax(overall.height + overall.y, gm.height + gm.y) -
1344 qMin(overall.y, gm.y);
1345 overall.xoff += gm.xoff;
1346 overall.yoff += gm.yoff;
1347
1348 // reset the high byte for all glyphs
1349 const int hi = which << 24;
1350 for (i = start; i < end; ++i)
1351 glyphs.glyphs[i] = hi | glyphs.glyphs[i];
1352
1353 // change engine
1354 start = end;
1355 which = e;
1356 }
1357
1358 // set the high byte to zero
1359 for (i = start; i < end; ++i)
1360 glyphs.glyphs[i] = stripped(glyphs.glyphs[i]);
1361
1362 // merge the bounding box for this run
1363 const glyph_metrics_t gm = engine(which)->boundingBox(glyphs.mid(start, end - start));
1364
1365 overall.x = qMin(overall.x, gm.x);
1366 overall.y = qMin(overall.y, gm.y);
1367 overall.width = overall.xoff + gm.width;
1368 overall.height = qMax(overall.height + overall.y, gm.height + gm.y) -
1369 qMin(overall.y, gm.y);
1370 overall.xoff += gm.xoff;
1371 overall.yoff += gm.yoff;
1372
1373 // reset the high byte for all glyphs
1374 const int hi = which << 24;
1375 for (i = start; i < end; ++i)
1376 glyphs.glyphs[i] = hi | glyphs.glyphs[i];
1377
1378 return overall;
1379}
1380
1381void QFontEngineMulti::addOutlineToPath(qreal x, qreal y, const QGlyphLayout &glyphs,
1382 QPainterPath *path, QTextItem::RenderFlags flags)
1383{
1384 if (glyphs.numGlyphs <= 0)
1385 return;
1386
1387 int which = highByte(glyphs.glyphs[0]);
1388 int start = 0;
1389 int end, i;
1390 if (flags & QTextItem::RightToLeft) {
1391 for (int gl = 0; gl < glyphs.numGlyphs; gl++) {
1392 x += glyphs.advances_x[gl].toReal();
1393 y += glyphs.advances_y[gl].toReal();
1394 }
1395 }
1396 for (end = 0; end < glyphs.numGlyphs; ++end) {
1397 const int e = highByte(glyphs.glyphs[end]);
1398 if (e == which)
1399 continue;
1400
1401 if (flags & QTextItem::RightToLeft) {
1402 for (i = start; i < end; ++i) {
1403 x -= glyphs.advances_x[i].toReal();
1404 y -= glyphs.advances_y[i].toReal();
1405 }
1406 }
1407
1408 // set the high byte to zero
1409 for (i = start; i < end; ++i)
1410 glyphs.glyphs[i] = stripped(glyphs.glyphs[i]);
1411 engine(which)->addOutlineToPath(x, y, glyphs.mid(start, end - start), path, flags);
1412 // reset the high byte for all glyphs and update x and y
1413 const int hi = which << 24;
1414 for (i = start; i < end; ++i)
1415 glyphs.glyphs[i] = hi | glyphs.glyphs[i];
1416
1417 if (!(flags & QTextItem::RightToLeft)) {
1418 for (i = start; i < end; ++i) {
1419 x += glyphs.advances_x[i].toReal();
1420 y += glyphs.advances_y[i].toReal();
1421 }
1422 }
1423
1424 // change engine
1425 start = end;
1426 which = e;
1427 }
1428
1429 if (flags & QTextItem::RightToLeft) {
1430 for (i = start; i < end; ++i) {
1431 x -= glyphs.advances_x[i].toReal();
1432 y -= glyphs.advances_y[i].toReal();
1433 }
1434 }
1435
1436 // set the high byte to zero
1437 for (i = start; i < end; ++i)
1438 glyphs.glyphs[i] = stripped(glyphs.glyphs[i]);
1439
1440 engine(which)->addOutlineToPath(x, y, glyphs.mid(start, end - start), path, flags);
1441
1442 // reset the high byte for all glyphs
1443 const int hi = which << 24;
1444 for (i = start; i < end; ++i)
1445 glyphs.glyphs[i] = hi | glyphs.glyphs[i];
1446}
1447
1448void QFontEngineMulti::recalcAdvances(QGlyphLayout *glyphs, QTextEngine::ShaperFlags flags) const
1449{
1450 if (glyphs->numGlyphs <= 0)
1451 return;
1452
1453 int which = highByte(glyphs->glyphs[0]);
1454 int start = 0;
1455 int end, i;
1456 for (end = 0; end < glyphs->numGlyphs; ++end) {
1457 const int e = highByte(glyphs->glyphs[end]);
1458 if (e == which)
1459 continue;
1460
1461 // set the high byte to zero
1462 for (i = start; i < end; ++i)
1463 glyphs->glyphs[i] = stripped(glyphs->glyphs[i]);
1464
1465 QGlyphLayout offs = glyphs->mid(start, end - start);
1466 engine(which)->recalcAdvances(&offs, flags);
1467
1468 // reset the high byte for all glyphs and update x and y
1469 const int hi = which << 24;
1470 for (i = start; i < end; ++i)
1471 glyphs->glyphs[i] = hi | glyphs->glyphs[i];
1472
1473 // change engine
1474 start = end;
1475 which = e;
1476 }
1477
1478 // set the high byte to zero
1479 for (i = start; i < end; ++i)
1480 glyphs->glyphs[i] = stripped(glyphs->glyphs[i]);
1481
1482 QGlyphLayout offs = glyphs->mid(start, end - start);
1483 engine(which)->recalcAdvances(&offs, flags);
1484
1485 // reset the high byte for all glyphs
1486 const int hi = which << 24;
1487 for (i = start; i < end; ++i)
1488 glyphs->glyphs[i] = hi | glyphs->glyphs[i];
1489}
1490
1491void QFontEngineMulti::doKerning(QGlyphLayout *glyphs, QTextEngine::ShaperFlags flags) const
1492{
1493 if (glyphs->numGlyphs <= 0)
1494 return;
1495
1496 int which = highByte(glyphs->glyphs[0]);
1497 int start = 0;
1498 int end, i;
1499 for (end = 0; end < glyphs->numGlyphs; ++end) {
1500 const int e = highByte(glyphs->glyphs[end]);
1501 if (e == which)
1502 continue;
1503
1504 // set the high byte to zero
1505 for (i = start; i < end; ++i)
1506 glyphs->glyphs[i] = stripped(glyphs->glyphs[i]);
1507
1508 QGlyphLayout offs = glyphs->mid(start, end - start);
1509 engine(which)->doKerning(&offs, flags);
1510
1511 // reset the high byte for all glyphs and update x and y
1512 const int hi = which << 24;
1513 for (i = start; i < end; ++i)
1514 glyphs->glyphs[i] = hi | glyphs->glyphs[i];
1515
1516 // change engine
1517 start = end;
1518 which = e;
1519 }
1520
1521 // set the high byte to zero
1522 for (i = start; i < end; ++i)
1523 glyphs->glyphs[i] = stripped(glyphs->glyphs[i]);
1524
1525 QGlyphLayout offs = glyphs->mid(start, end - start);
1526 engine(which)->doKerning(&offs, flags);
1527
1528 // reset the high byte for all glyphs
1529 const int hi = which << 24;
1530 for (i = start; i < end; ++i)
1531 glyphs->glyphs[i] = hi | glyphs->glyphs[i];
1532}
1533
1534glyph_metrics_t QFontEngineMulti::boundingBox(glyph_t glyph)
1535{
1536 const int which = highByte(glyph);
1537 Q_ASSERT(which < engines.size());
1538 return engine(which)->boundingBox(stripped(glyph));
1539}
1540
1541QFixed QFontEngineMulti::ascent() const
1542{ return engine(0)->ascent(); }
1543
1544QFixed QFontEngineMulti::descent() const
1545{ return engine(0)->descent(); }
1546
1547QFixed QFontEngineMulti::leading() const
1548{
1549 return engine(0)->leading();
1550}
1551
1552QFixed QFontEngineMulti::xHeight() const
1553{
1554 return engine(0)->xHeight();
1555}
1556
1557QFixed QFontEngineMulti::averageCharWidth() const
1558{
1559 return engine(0)->averageCharWidth();
1560}
1561
1562QFixed QFontEngineMulti::lineThickness() const
1563{
1564 return engine(0)->lineThickness();
1565}
1566
1567QFixed QFontEngineMulti::underlinePosition() const
1568{
1569 return engine(0)->underlinePosition();
1570}
1571
1572qreal QFontEngineMulti::maxCharWidth() const
1573{
1574 return engine(0)->maxCharWidth();
1575}
1576
1577qreal QFontEngineMulti::minLeftBearing() const
1578{
1579 return engine(0)->minLeftBearing();
1580}
1581
1582qreal QFontEngineMulti::minRightBearing() const
1583{
1584 return engine(0)->minRightBearing();
1585}
1586
1587bool QFontEngineMulti::canRender(const QChar *string, int len)
1588{
1589 if (engine(0)->canRender(string, len))