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

Last change on this file since 605 was 561, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.1 sources.

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