1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtGui module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
43 #include <private/qfontengine_p.h>
47 #include "qpainterpath.h"
48 #include "qvarlengtharray.h"
51 #include <private/qharfbuzz_p.h>
55 static inline bool qtransform_equals_no_translate(const QTransform &a, const QTransform &b)
57 if (a.type() <= QTransform::TxTranslate && b.type() <= QTransform::TxTranslate) {
60 // We always use paths for perspective text anyway, so no
61 // point in checking the full matrix...
62 Q_ASSERT(a.type() < QTransform::TxProject);
63 Q_ASSERT(b.type() < QTransform::TxProject);
65 return a.m11() == b.m11()
68 && a.m22() == b.m22();
72 // Harfbuzz helper functions
74 static HB_Bool hb_stringToGlyphs(HB_Font font, const HB_UChar16 *string, hb_uint32 length, HB_Glyph *glyphs, hb_uint32 *numGlyphs, HB_Bool rightToLeft)
76 QFontEngine *fe = (QFontEngine *)font->userData;
78 QVarLengthGlyphLayoutArray qglyphs(*numGlyphs);
80 QTextEngine::ShaperFlags shaperFlags(QTextEngine::GlyphIndicesOnly);
82 shaperFlags |= QTextEngine::RightToLeft;
84 int nGlyphs = *numGlyphs;
85 bool result = fe->stringToCMap(reinterpret_cast<const QChar *>(string), length, &qglyphs, &nGlyphs, shaperFlags);
90 for (hb_uint32 i = 0; i < *numGlyphs; ++i)
91 glyphs[i] = qglyphs.glyphs[i];
96 static void hb_getAdvances(HB_Font font, const HB_Glyph *glyphs, hb_uint32 numGlyphs, HB_Fixed *advances, int flags)
98 QFontEngine *fe = (QFontEngine *)font->userData;
100 QVarLengthGlyphLayoutArray qglyphs(numGlyphs);
102 for (hb_uint32 i = 0; i < numGlyphs; ++i)
103 qglyphs.glyphs[i] = glyphs[i];
105 fe->recalcAdvances(&qglyphs, flags & HB_ShaperFlag_UseDesignMetrics ? QFlags<QTextEngine::ShaperFlag>(QTextEngine::DesignMetrics) : QFlags<QTextEngine::ShaperFlag>(0));
107 for (hb_uint32 i = 0; i < numGlyphs; ++i)
108 advances[i] = qglyphs.advances_x[i].value();
111 static HB_Bool hb_canRender(HB_Font font, const HB_UChar16 *string, hb_uint32 length)
113 QFontEngine *fe = (QFontEngine *)font->userData;
114 return fe->canRender(reinterpret_cast<const QChar *>(string), length);
117 static void hb_getGlyphMetrics(HB_Font font, HB_Glyph glyph, HB_GlyphMetrics *metrics)
119 QFontEngine *fe = (QFontEngine *)font->userData;
120 glyph_metrics_t m = fe->boundingBox(glyph);
121 metrics->x = m.x.value();
122 metrics->y = m.y.value();
123 metrics->width = m.width.value();
124 metrics->height = m.height.value();
125 metrics->xOffset = m.xoff.value();
126 metrics->yOffset = m.yoff.value();
129 static HB_Fixed hb_getFontMetric(HB_Font font, HB_FontMetric metric)
131 if (metric == HB_FontAscent) {
132 QFontEngine *fe = (QFontEngine *)font->userData;
133 return fe->ascent().value();
138 HB_Error QFontEngine::getPointInOutline(HB_Glyph glyph, int flags, hb_uint32 point, HB_Fixed *xpos, HB_Fixed *ypos, hb_uint32 *nPoints)
146 return HB_Err_Not_Covered;
149 static 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 QFontEngine *fe = (QFontEngine *)font->userData;
152 return fe->getPointInOutline(glyph, flags, point, xpos, ypos, nPoints);
155 static const HB_FontClass hb_fontClass = {
156 hb_stringToGlyphs, hb_getAdvances, hb_canRender, hb_getPointInOutline,
157 hb_getGlyphMetrics, hb_getFontMetric
160 static HB_Error hb_getSFntTable(void *font, HB_Tag tableTag, HB_Byte *buffer, HB_UInt *length)
162 QFontEngine *fe = (QFontEngine *)font;
163 if (!fe->getSfntTableData(tableTag, buffer, length))
164 return HB_Err_Invalid_Argument;
170 QFontEngine::QFontEngine()
177 memset(&hbFont, 0, sizeof(hbFont));
178 hbFont.klass = &hb_fontClass;
179 hbFont.userData = this;
185 QFontEngine::~QFontEngine()
187 m_glyphCaches.clear();
191 QFixed QFontEngine::lineThickness() const
194 int score = fontDef.weight * fontDef.pixelSize;
195 int lw = score / 700;
197 // looks better with thicker line for small pointsizes
198 if (lw < 2 && score >= 1050) lw = 2;
204 QFixed QFontEngine::underlinePosition() const
206 return ((lineThickness() * 2) + 3) / 6;
209 HB_Font QFontEngine::harfbuzzFont() const
211 if (!hbFont.x_ppem) {
212 QFixed emSquare = emSquareSize();
213 hbFont.x_ppem = fontDef.pixelSize;
214 hbFont.y_ppem = fontDef.pixelSize * fontDef.stretch / 100;
215 hbFont.x_scale = (QFixed(hbFont.x_ppem * (1 << 16)) / emSquare).value();
216 hbFont.y_scale = (QFixed(hbFont.y_ppem * (1 << 16)) / emSquare).value();
221 HB_Face QFontEngine::harfbuzzFace() const
224 hbFace = qHBNewFace(const_cast<QFontEngine *>(this), hb_getSFntTable);
230 glyph_metrics_t QFontEngine::boundingBox(glyph_t glyph, const QTransform &matrix)
232 glyph_metrics_t metrics = boundingBox(glyph);
234 if (matrix.type() > QTransform::TxTranslate) {
235 return metrics.transformed(matrix);
240 QFixed QFontEngine::xHeight() const
242 QGlyphLayoutArray<8> glyphs;
244 QChar x((ushort)'x');
245 stringToCMap(&x, 1, &glyphs, &nglyphs, QTextEngine::GlyphIndicesOnly);
247 glyph_metrics_t bb = const_cast<QFontEngine *>(this)->boundingBox(glyphs.glyphs[0]);
251 QFixed QFontEngine::averageCharWidth() const
253 QGlyphLayoutArray<8> glyphs;
255 QChar x((ushort)'x');
256 stringToCMap(&x, 1, &glyphs, &nglyphs, QTextEngine::GlyphIndicesOnly);
258 glyph_metrics_t bb = const_cast<QFontEngine *>(this)->boundingBox(glyphs.glyphs[0]);
263 void QFontEngine::getGlyphPositions(const QGlyphLayout &glyphs, const QTransform &matrix, QTextItem::RenderFlags flags,
264 QVarLengthArray<glyph_t> &glyphs_out, QVarLengthArray<QFixedPoint> &positions)
269 const bool transform = matrix.m11() != 1.
270 || matrix.m12() != 0.
271 || matrix.m21() != 0.
272 || matrix.m22() != 1.;
274 xpos = QFixed::fromReal(matrix.dx());
275 ypos = QFixed::fromReal(matrix.dy());
279 if (flags & QTextItem::RightToLeft) {
280 int i = glyphs.numGlyphs;
281 int totalKashidas = 0;
283 if (glyphs.attributes[i].dontPrint)
285 xpos += glyphs.advances_x[i] + QFixed::fromFixed(glyphs.justifications[i].space_18d6);
286 ypos += glyphs.advances_y[i];
287 totalKashidas += glyphs.justifications[i].nKashidas;
289 positions.resize(glyphs.numGlyphs+totalKashidas);
290 glyphs_out.resize(glyphs.numGlyphs+totalKashidas);
293 while(i < glyphs.numGlyphs) {
294 if (glyphs.attributes[i].dontPrint) {
298 xpos -= glyphs.advances_x[i];
299 ypos -= glyphs.advances_y[i];
301 QFixed gpos_x = xpos + glyphs.offsets[i].x;
302 QFixed gpos_y = ypos + glyphs.offsets[i].y;
304 QPointF gpos(gpos_x.toReal(), gpos_y.toReal());
305 gpos = gpos * matrix;
306 gpos_x = QFixed::fromReal(gpos.x());
307 gpos_y = QFixed::fromReal(gpos.y());
309 positions[current].x = gpos_x;
310 positions[current].y = gpos_y;
311 glyphs_out[current] = glyphs.glyphs[i];
313 if (glyphs.justifications[i].nKashidas) {
314 QChar ch(0x640); // Kashida character
315 QGlyphLayoutArray<8> g;
317 stringToCMap(&ch, 1, &g, &nglyphs, 0);
318 for (uint k = 0; k < glyphs.justifications[i].nKashidas; ++k) {
319 xpos -= g.advances_x[0];
320 ypos -= g.advances_y[0];
322 QFixed gpos_x = xpos + glyphs.offsets[i].x;
323 QFixed gpos_y = ypos + glyphs.offsets[i].y;
325 QPointF gpos(gpos_x.toReal(), gpos_y.toReal());
326 gpos = gpos * matrix;
327 gpos_x = QFixed::fromReal(gpos.x());
328 gpos_y = QFixed::fromReal(gpos.y());
330 positions[current].x = gpos_x;
331 positions[current].y = gpos_y;
332 glyphs_out[current] = g.glyphs[0];
336 xpos -= QFixed::fromFixed(glyphs.justifications[i].space_18d6);
341 positions.resize(glyphs.numGlyphs);
342 glyphs_out.resize(glyphs.numGlyphs);
345 while (i < glyphs.numGlyphs) {
346 if (!glyphs.attributes[i].dontPrint) {
347 positions[current].x = xpos + glyphs.offsets[i].x;
348 positions[current].y = ypos + glyphs.offsets[i].y;
349 glyphs_out[current] = glyphs.glyphs[i];
350 xpos += glyphs.advances_x[i] + QFixed::fromFixed(glyphs.justifications[i].space_18d6);
351 ypos += glyphs.advances_y[i];
357 while (i < glyphs.numGlyphs) {
358 if (!glyphs.attributes[i].dontPrint) {
359 QFixed gpos_x = xpos + glyphs.offsets[i].x;
360 QFixed gpos_y = ypos + glyphs.offsets[i].y;
361 QPointF gpos(gpos_x.toReal(), gpos_y.toReal());
362 gpos = gpos * matrix;
363 positions[current].x = QFixed::fromReal(gpos.x());
364 positions[current].y = QFixed::fromReal(gpos.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];
374 positions.resize(current);
375 glyphs_out.resize(current);
376 Q_ASSERT(positions.size() == glyphs_out.size());
379 void QFontEngine::getGlyphBearings(glyph_t glyph, qreal *leftBearing, qreal *rightBearing)
381 glyph_metrics_t gi = boundingBox(glyph);
382 bool isValid = gi.isValid();
383 if (leftBearing != 0)
384 *leftBearing = isValid ? gi.x.toReal() : 0.0;
385 if (rightBearing != 0)
386 *rightBearing = isValid ? (gi.xoff - gi.x - gi.width).toReal() : 0.0;
389 glyph_metrics_t QFontEngine::tightBoundingBox(const QGlyphLayout &glyphs)
391 glyph_metrics_t overall;
395 for (int i = 0; i < glyphs.numGlyphs; i++) {
396 glyph_metrics_t bb = boundingBox(glyphs.glyphs[i]);
397 QFixed x = overall.xoff + glyphs.offsets[i].x + bb.x;
398 QFixed y = overall.yoff + glyphs.offsets[i].y + bb.y;
399 overall.x = qMin(overall.x, x);
400 overall.y = qMin(overall.y, y);
401 xmax = qMax(xmax, x + bb.width);
402 ymax = qMax(ymax, y + bb.height);
403 overall.xoff += bb.xoff;
404 overall.yoff += bb.yoff;
406 overall.height = qMax(overall.height, ymax - overall.y);
407 overall.width = xmax - overall.x;
413 void QFontEngine::addOutlineToPath(qreal x, qreal y, const QGlyphLayout &glyphs, QPainterPath *path,
414 QTextItem::RenderFlags flags)
416 if (!glyphs.numGlyphs)
419 QVarLengthArray<QFixedPoint> positions;
420 QVarLengthArray<glyph_t> positioned_glyphs;
421 QTransform matrix = QTransform::fromTranslate(x, y);
422 getGlyphPositions(glyphs, matrix, flags, positioned_glyphs, positions);
423 addGlyphsToPath(positioned_glyphs.data(), positions.data(), positioned_glyphs.size(), path, flags);
426 #define GRID(x, y) grid[(y)*(w+1) + (x)]
427 #define SET(x, y) (*(image_data + (y)*bpl + ((x) >> 3)) & (0x80 >> ((x) & 7)))
429 enum { EdgeRight = 0x1,
435 static void collectSingleContour(qreal x0, qreal y0, uint *grid, int x, int y, int w, int h, QPainterPath *path)
439 path->moveTo(x + x0, y + y0);
441 if (GRID(x, y) & EdgeRight) {
442 while (GRID(x, y) & EdgeRight) {
443 GRID(x, y) &= ~EdgeRight;
447 path->lineTo(x + x0, y + y0);
450 if (GRID(x, y) & EdgeDown) {
451 while (GRID(x, y) & EdgeDown) {
452 GRID(x, y) &= ~EdgeDown;
456 path->lineTo(x + x0, y + y0);
459 if (GRID(x, y) & EdgeLeft) {
460 while (GRID(x, y) & EdgeLeft) {
461 GRID(x, y) &= ~EdgeLeft;
465 path->lineTo(x + x0, y + y0);
468 if (GRID(x, y) & EdgeUp) {
469 while (GRID(x, y) & EdgeUp) {
470 GRID(x, y) &= ~EdgeUp;
474 path->lineTo(x + x0, y + y0);
478 path->closeSubpath();
481 Q_GUI_EXPORT void qt_addBitmapToPath(qreal x0, qreal y0, const uchar *image_data, int bpl, int w, int h, QPainterPath *path)
483 uint *grid = new uint[(w+1)*(h+1)];
485 for (int y = 0; y <= h; ++y) {
486 for (int x = 0; x <= w; ++x) {
487 bool topLeft = (x == 0)|(y == 0) ? false : SET(x - 1, y - 1);
488 bool topRight = (x == w)|(y == 0) ? false : SET(x, y - 1);
489 bool bottomLeft = (x == 0)|(y == h) ? false : SET(x - 1, y);
490 bool bottomRight = (x == w)|(y == h) ? false : SET(x, y);
493 if ((!topRight) & bottomRight)
494 GRID(x, y) |= EdgeRight;
495 if ((!bottomRight) & bottomLeft)
496 GRID(x, y) |= EdgeDown;
497 if ((!bottomLeft) & topLeft)
498 GRID(x, y) |= EdgeLeft;
499 if ((!topLeft) & topRight)
500 GRID(x, y) |= EdgeUp;
505 for (int y = 0; y < h; ++y) {
506 for (int x = 0; x < w; ++x) {
509 // found start of a contour, follow it
510 collectSingleContour(x0, y0, grid, x, y, w, h, path);
520 void QFontEngine::addBitmapFontToPath(qreal x, qreal y, const QGlyphLayout &glyphs,
521 QPainterPath *path, QTextItem::RenderFlags flags)
523 // TODO what to do with 'flags' ??
525 QFixed advanceX = QFixed::fromReal(x);
526 QFixed advanceY = QFixed::fromReal(y);
527 for (int i=0; i < glyphs.numGlyphs; ++i) {
528 glyph_metrics_t metrics = boundingBox(glyphs.glyphs[i]);
529 if (metrics.width.value() == 0 || metrics.height.value() == 0) {
530 advanceX += glyphs.advances_x[i];
531 advanceY += glyphs.advances_y[i];
534 const QImage alphaMask = alphaMapForGlyph(glyphs.glyphs[i]);
536 const int w = alphaMask.width();
537 const int h = alphaMask.height();
538 const int srcBpl = alphaMask.bytesPerLine();
540 if (alphaMask.depth() == 1) {
543 bitmap = QImage(w, h, QImage::Format_Mono);
544 const uchar *imageData = alphaMask.bits();
545 const int destBpl = bitmap.bytesPerLine();
546 uchar *bitmapData = bitmap.bits();
548 for (int yi = 0; yi < h; ++yi) {
549 const uchar *src = imageData + yi*srcBpl;
550 uchar *dst = bitmapData + yi*destBpl;
551 for (int xi = 0; xi < w; ++xi) {
552 const int byte = xi / 8;
553 const int bit = xi % 8;
557 dst[byte] |= 128 >> bit;
561 const uchar *bitmap_data = bitmap.bits();
562 QFixedPoint offset = glyphs.offsets[i];
563 advanceX += offset.x;
564 advanceY += offset.y;
565 qt_addBitmapToPath((advanceX + metrics.x).toReal(), (advanceY + metrics.y).toReal(), bitmap_data, bitmap.bytesPerLine(), w, h, path);
566 advanceX += glyphs.advances_x[i];
567 advanceY += glyphs.advances_y[i];
571 void QFontEngine::addGlyphsToPath(glyph_t *glyphs, QFixedPoint *positions, int nGlyphs,
572 QPainterPath *path, QTextItem::RenderFlags flags)
574 qreal x = positions[0].x.toReal();
575 qreal y = positions[0].y.toReal();
576 QVarLengthGlyphLayoutArray g(nGlyphs);
578 for (int i = 0; i < nGlyphs; ++i) {
579 g.glyphs[i] = glyphs[i];
580 if (i < nGlyphs - 1) {
581 g.advances_x[i] = positions[i+1].x - positions[i].x;
582 g.advances_y[i] = positions[i+1].y - positions[i].y;
584 g.advances_x[i] = QFixed::fromReal(maxCharWidth());
589 addBitmapFontToPath(x, y, g, path, flags);
592 QImage QFontEngine::alphaMapForGlyph(glyph_t glyph, QFixed /*subPixelPosition*/)
594 // For font engines don't support subpixel positioning
595 return alphaMapForGlyph(glyph);
598 QImage QFontEngine::alphaMapForGlyph(glyph_t glyph, const QTransform &t)
600 QImage i = alphaMapForGlyph(glyph);
601 if (t.type() > QTransform::TxTranslate)
602 i = i.transformed(t).convertToFormat(QImage::Format_Indexed8);
603 Q_ASSERT(i.depth() <= 8); // To verify that transformed didn't change the format...
608 QImage QFontEngine::alphaMapForGlyph(glyph_t glyph, QFixed subPixelPosition, const QTransform &t)
610 if (! supportsSubPixelPositions())
611 return alphaMapForGlyph(glyph, t);
613 QImage i = alphaMapForGlyph(glyph, subPixelPosition);
614 if (t.type() > QTransform::TxTranslate)
615 i = i.transformed(t).convertToFormat(QImage::Format_Indexed8);
616 Q_ASSERT(i.depth() <= 8); // To verify that transformed didn't change the format...
621 QImage QFontEngine::alphaRGBMapForGlyph(glyph_t glyph, QFixed /*subPixelPosition*/, int /* margin */, const QTransform &t)
623 QImage alphaMask = alphaMapForGlyph(glyph, t);
624 QImage rgbMask(alphaMask.width(), alphaMask.height(), QImage::Format_RGB32);
626 QVector<QRgb> colorTable = alphaMask.colorTable();
627 for (int y=0; y<alphaMask.height(); ++y) {
628 uint *dst = (uint *) rgbMask.scanLine(y);
629 uchar *src = (uchar *) alphaMask.scanLine(y);
630 for (int x=0; x<alphaMask.width(); ++x) {
631 int val = qAlpha(colorTable.at(src[x]));
632 dst[x] = qRgb(val, val, val);
639 QFixed QFontEngine::subPixelPositionForX(QFixed x)
641 int m_subPixelPositionCount = 4;
642 if (!supportsSubPixelPositions())
645 QFixed subPixelPosition;
647 subPixelPosition = x - x.floor();
648 QFixed fraction = (subPixelPosition / QFixed::fromReal(1.0 / m_subPixelPositionCount)).floor();
649 subPixelPosition = fraction / QFixed(m_subPixelPositionCount);
651 return subPixelPosition;
654 QImage *QFontEngine::lockedAlphaMapForGlyph(glyph_t glyph, QFixed subPixelPosition,
655 QFontEngine::GlyphFormat neededFormat,
656 const QTransform &t, QPoint *offset)
658 Q_ASSERT(currentlyLockedAlphaMap.isNull());
659 if (neededFormat == Format_None)
660 neededFormat = Format_A32;
662 if (neededFormat != Format_A32)
663 currentlyLockedAlphaMap = alphaMapForGlyph(glyph, subPixelPosition, t);
665 currentlyLockedAlphaMap = alphaRGBMapForGlyph(glyph, subPixelPosition, 0, t);
668 *offset = QPoint(0, 0);
670 return ¤tlyLockedAlphaMap;
673 void QFontEngine::unlockAlphaMapForGlyph()
675 Q_ASSERT(!currentlyLockedAlphaMap.isNull());
676 currentlyLockedAlphaMap = QImage();
679 QImage QFontEngine::alphaMapForGlyph(glyph_t glyph)
681 glyph_metrics_t gm = boundingBox(glyph);
682 int glyph_x = qFloor(gm.x.toReal());
683 int glyph_y = qFloor(gm.y.toReal());
684 int glyph_width = qCeil((gm.x + gm.width).toReal()) - glyph_x;
685 int glyph_height = qCeil((gm.y + gm.height).toReal()) - glyph_y;
687 if (glyph_width <= 0 || glyph_height <= 0)
691 pt.y = -glyph_y; // the baseline
693 QImage im(glyph_width + 4, glyph_height, QImage::Format_ARGB32_Premultiplied);
694 im.fill(Qt::transparent);
696 p.setRenderHint(QPainter::Antialiasing);
697 addGlyphsToPath(&glyph, &pt, 1, &path, 0);
699 p.setBrush(Qt::black);
703 QImage indexed(im.width(), im.height(), QImage::Format_Indexed8);
704 QVector<QRgb> colors(256);
705 for (int i=0; i<256; ++i)
706 colors[i] = qRgba(0, 0, 0, i);
707 indexed.setColorTable(colors);
709 for (int y=0; y<im.height(); ++y) {
710 uchar *dst = (uchar *) indexed.scanLine(y);
711 uint *src = (uint *) im.scanLine(y);
712 for (int x=0; x<im.width(); ++x)
713 dst[x] = qAlpha(src[x]);
719 void QFontEngine::removeGlyphFromCache(glyph_t)
723 QFontEngine::Properties QFontEngine::properties() const
726 QByteArray psname = QFontEngine::convertToPostscriptFontFamilyName(fontDef.family.toUtf8());
728 psname += QByteArray::number(fontDef.style);
730 psname += QByteArray::number(fontDef.weight);
732 p.postscriptName = psname;
734 p.descent = descent();
735 p.leading = leading();
736 p.emSquare = p.ascent;
737 p.boundingBox = QRectF(0, -p.ascent.toReal(), maxCharWidth(), (p.ascent + p.descent).toReal());
739 p.capHeight = p.ascent;
740 p.lineWidth = lineThickness();
744 void QFontEngine::getUnscaledGlyph(glyph_t glyph, QPainterPath *path, glyph_metrics_t *metrics)
746 *metrics = boundingBox(glyph);
750 addGlyphsToPath(&glyph, &p, 1, path, QFlag(0));
753 QByteArray QFontEngine::getSfntTable(uint tag) const
757 if (!getSfntTableData(tag, 0, &len))
762 if (!getSfntTableData(tag, reinterpret_cast<uchar *>(table.data()), &len))
767 void QFontEngine::setGlyphCache(void *key, QFontEngineGlyphCache *data)
771 GlyphCacheEntry entry;
774 if (m_glyphCaches.contains(entry))
777 // Limit the glyph caches to 4. This covers all 90 degree rotations and limits
778 // memory use when there is continuous or random rotation
779 if (m_glyphCaches.size() == 4)
780 m_glyphCaches.removeLast();
782 m_glyphCaches.push_front(entry);
786 QFontEngineGlyphCache *QFontEngine::glyphCache(void *key, QFontEngineGlyphCache::Type type, const QTransform &transform) const
788 for (QLinkedList<GlyphCacheEntry>::const_iterator it = m_glyphCaches.constBegin(), end = m_glyphCaches.constEnd(); it != end; ++it) {
789 QFontEngineGlyphCache *c = it->cache.data();
790 if (key == it->context
791 && type == c->cacheType()
792 && qtransform_equals_no_translate(c->m_transform, transform)) {
799 #if defined(Q_WS_WIN) || defined(Q_WS_X11) || defined(Q_WS_QWS) || defined(Q_WS_QPA) || defined(Q_OS_SYMBIAN)
800 static inline QFixed kerning(int left, int right, const QFontEngine::KernPair *pairs, int numPairs)
802 uint left_right = (left << 16) + right;
804 left = 0, right = numPairs - 1;
805 while (left <= right) {
806 int middle = left + ( ( right - left ) >> 1 );
808 if(pairs[middle].left_right == left_right)
809 return pairs[middle].adjust;
811 if (pairs[middle].left_right < left_right)
819 void QFontEngine::doKerning(QGlyphLayout *glyphs, QTextEngine::ShaperFlags flags) const
821 int numPairs = kerning_pairs.size();
825 const KernPair *pairs = kerning_pairs.constData();
827 if(flags & QTextEngine::DesignMetrics) {
828 for(int i = 0; i < glyphs->numGlyphs - 1; ++i)
829 glyphs->advances_x[i] += kerning(glyphs->glyphs[i], glyphs->glyphs[i+1] , pairs, numPairs);
831 for(int i = 0; i < glyphs->numGlyphs - 1; ++i)
832 glyphs->advances_x[i] += qRound(kerning(glyphs->glyphs[i], glyphs->glyphs[i+1] , pairs, numPairs));
836 void QFontEngine::loadKerningPairs(QFixed scalingFactor)
838 kerning_pairs.clear();
840 QByteArray tab = getSfntTable(MAKE_TAG('k', 'e', 'r', 'n'));
844 const uchar *table = reinterpret_cast<const uchar *>(tab.constData());
846 unsigned short version = qFromBigEndian<quint16>(table);
848 // qDebug("wrong version");
852 unsigned short numTables = qFromBigEndian<quint16>(table + 2);
855 for(int i = 0; i < numTables; ++i) {
856 if (offset + 6 > tab.size()) {
857 // qDebug("offset out of bounds");
860 const uchar *header = table + offset;
862 ushort version = qFromBigEndian<quint16>(header);
863 ushort length = qFromBigEndian<quint16>(header+2);
864 ushort coverage = qFromBigEndian<quint16>(header+4);
865 // qDebug("subtable: version=%d, coverage=%x",version, coverage);
866 if(version == 0 && coverage == 0x0001) {
867 if (offset + length > tab.size()) {
868 // qDebug("length ouf ot bounds");
871 const uchar *data = table + offset + 6;
873 ushort nPairs = qFromBigEndian<quint16>(data);
874 if(nPairs * 6 + 8 > length - 6) {
875 // qDebug("corrupt table!");
881 for(int i = 0; i < nPairs; ++i) {
882 QFontEngine::KernPair p;
883 p.left_right = (((uint)qFromBigEndian<quint16>(data+off)) << 16) + qFromBigEndian<quint16>(data+off+2);
884 p.adjust = QFixed(((int)(short)qFromBigEndian<quint16>(data+off+4))) / scalingFactor;
885 kerning_pairs.append(p);
893 qSort(kerning_pairs);
894 // for (int i = 0; i < kerning_pairs.count(); ++i)
895 // qDebug() << 'i' << i << "left_right" << hex << kerning_pairs.at(i).left_right;
899 void QFontEngine::doKerning(QGlyphLayout *, QTextEngine::ShaperFlags) const
904 int QFontEngine::glyphCount() const
906 QByteArray maxpTable = getSfntTable(MAKE_TAG('m', 'a', 'x', 'p'));
907 if (maxpTable.size() < 6)
909 return qFromBigEndian<quint16>(reinterpret_cast<const uchar *>(maxpTable.constData() + 4));
912 const uchar *QFontEngine::getCMap(const uchar *table, uint tableSize, bool *isSymbolFont, int *cmapSize)
914 const uchar *header = table;
918 const uchar *endPtr = table + tableSize;
921 if (qFromBigEndian<quint16>(header) != 0)
924 unsigned short numTables = qFromBigEndian<quint16>(header + 2);
925 const uchar *maps = table + 4;
926 if (maps + 8 * numTables > endPtr)
936 MicrosoftUnicodeExtended
939 int symbolTable = -1;
942 for (int n = 0; n < numTables; ++n) {
943 const quint16 platformId = qFromBigEndian<quint16>(maps + 8 * n);
944 const quint16 platformSpecificId = qFromBigEndian<quint16>(maps + 8 * n + 2);
945 switch (platformId) {
947 if (score < Unicode &&
948 (platformSpecificId == 0 ||
949 platformSpecificId == 2 ||
950 platformSpecificId == 3)) {
953 } else if (score < Unicode11 && platformSpecificId == 1) {
959 if (score < AppleRoman && platformSpecificId == 0) { // Apple Roman
965 switch (platformSpecificId) {
968 if (score < Symbol) {
974 if (score < MicrosoftUnicode) {
976 score = MicrosoftUnicode;
980 if (score < MicrosoftUnicodeExtended) {
982 score = MicrosoftUnicodeExtended;
996 *isSymbolFont = (symbolTable > -1);
998 unsigned int unicode_table = qFromBigEndian<quint32>(maps + 8*tableToUse + 4);
1000 if (!unicode_table || unicode_table + 8 > tableSize)
1003 // get the header of the unicode table
1004 header = table + unicode_table;
1006 unsigned short format = qFromBigEndian<quint16>(header);
1007 unsigned int length;
1009 length = qFromBigEndian<quint16>(header + 2);
1011 length = qFromBigEndian<quint32>(header + 4);
1013 if (table + unicode_table + length > endPtr)
1017 // To support symbol fonts that contain a unicode table for the symbol area
1018 // we check the cmap tables and fall back to symbol font unless that would
1019 // involve losing information from the unicode table
1020 if (symbolTable > -1 && ((score == Unicode) || (score == Unicode11))) {
1021 const uchar *selectedTable = table + unicode_table;
1023 // Check that none of the latin1 range are in the unicode table
1024 bool unicodeTableHasLatin1 = false;
1025 for (int uc=0x00; uc<0x100; ++uc) {
1026 if (getTrueTypeGlyphIndex(selectedTable, uc) != 0) {
1027 unicodeTableHasLatin1 = true;
1032 // Check that at least one symbol char is in the unicode table
1033 bool unicodeTableHasSymbols = false;
1034 if (!unicodeTableHasLatin1) {
1035 for (int uc=0xf000; uc<0xf100; ++uc) {
1036 if (getTrueTypeGlyphIndex(selectedTable, uc) != 0) {
1037 unicodeTableHasSymbols = true;
1043 // Fall back to symbol table
1044 if (!unicodeTableHasLatin1 && unicodeTableHasSymbols) {
1045 tableToUse = symbolTable;
1051 return table + unicode_table;
1054 quint32 QFontEngine::getTrueTypeGlyphIndex(const uchar *cmap, uint unicode)
1056 unsigned short format = qFromBigEndian<quint16>(cmap);
1059 return (int) *(cmap+6+unicode);
1060 } else if (format == 4) {
1061 /* some fonts come with invalid cmap tables, where the last segment
1062 specified end = start = rangeoffset = 0xffff, delta = 0x0001
1063 Since 0xffff is never a valid Unicode char anyway, we just get rid of the issue
1064 by returning 0 for 0xffff
1066 if(unicode >= 0xffff)
1068 quint16 segCountX2 = qFromBigEndian<quint16>(cmap + 6);
1069 const unsigned char *ends = cmap + 14;
1071 for (; i < segCountX2/2 && qFromBigEndian<quint16>(ends + 2*i) < unicode; i++) {}
1073 const unsigned char *idx = ends + segCountX2 + 2 + 2*i;
1074 quint16 startIndex = qFromBigEndian<quint16>(idx);
1076 if (startIndex > unicode)
1080 qint16 idDelta = (qint16)qFromBigEndian<quint16>(idx);
1082 quint16 idRangeoffset_t = (quint16)qFromBigEndian<quint16>(idx);
1085 if (idRangeoffset_t) {
1086 quint16 id = qFromBigEndian<quint16>(idRangeoffset_t + 2*(unicode - startIndex) + idx);
1088 glyphIndex = (idDelta + id) % 0x10000;
1092 glyphIndex = (idDelta + unicode) % 0x10000;
1095 } else if (format == 6) {
1096 quint16 tableSize = qFromBigEndian<quint16>(cmap + 2);
1098 quint16 firstCode6 = qFromBigEndian<quint16>(cmap + 6);
1099 if (unicode < firstCode6)
1102 quint16 entryCount6 = qFromBigEndian<quint16>(cmap + 8);
1103 if (entryCount6 * 2 + 10 > tableSize)
1106 quint16 sentinel6 = firstCode6 + entryCount6;
1107 if (unicode >= sentinel6)
1110 quint16 entryIndex6 = unicode - firstCode6;
1111 return qFromBigEndian<quint16>(cmap + 10 + (entryIndex6 * 2));
1112 } else if (format == 12) {
1113 quint32 nGroups = qFromBigEndian<quint32>(cmap + 12);
1115 cmap += 16; // move to start of groups
1117 int left = 0, right = nGroups - 1;
1118 while (left <= right) {
1119 int middle = left + ( ( right - left ) >> 1 );
1121 quint32 startCharCode = qFromBigEndian<quint32>(cmap + 12*middle);
1122 if(unicode < startCharCode)
1125 quint32 endCharCode = qFromBigEndian<quint32>(cmap + 12*middle + 4);
1126 if(unicode <= endCharCode)
1127 return qFromBigEndian<quint32>(cmap + 12*middle + 8) + unicode - startCharCode;
1132 qDebug("cmap table of format %d not implemented", format);
1138 QByteArray QFontEngine::convertToPostscriptFontFamilyName(const QByteArray &family)
1140 QByteArray f = family;
1155 class QRgbGreyPalette: public QVector<QRgb>
1162 for (int i = 0; i < size(); ++i, ++it)
1163 *it = 0xff000000 | i | (i<<8) | (i<<16);
1167 Q_GLOBAL_STATIC(QVector<QRgb>, qt_grayPalette)
1169 const QVector<QRgb> &QFontEngine::grayPalette()
1171 return *qt_grayPalette();
1174 QFixed QFontEngine::lastRightBearing(const QGlyphLayout &glyphs, bool round)
1176 if (glyphs.numGlyphs >= 1) {
1177 glyph_t glyph = glyphs.glyphs[glyphs.numGlyphs - 1];
1178 glyph_metrics_t gi = boundingBox(glyph);
1180 return round ? QFixed(qRound(gi.xoff - gi.x - gi.width))
1181 : QFixed(gi.xoff - gi.x - gi.width);
1186 // ------------------------------------------------------------------
1187 // The box font engine
1188 // ------------------------------------------------------------------
1190 QFontEngineBox::QFontEngineBox(int size)
1193 cache_cost = sizeof(QFontEngineBox);
1196 QFontEngineBox::~QFontEngineBox()
1200 bool QFontEngineBox::stringToCMap(const QChar *, int len, QGlyphLayout *glyphs, int *nglyphs, QTextEngine::ShaperFlags) const
1202 if (*nglyphs < len) {
1207 for (int i = 0; i < len; i++) {
1208 glyphs->glyphs[i] = 0;
1209 glyphs->advances_x[i] = _size;
1210 glyphs->advances_y[i] = 0;
1214 glyphs->numGlyphs = len;
1218 void QFontEngineBox::recalcAdvances(QGlyphLayout *glyphs, QTextEngine::ShaperFlags) const
1220 for (int i = 0; i < glyphs->numGlyphs; i++) {
1221 glyphs->advances_x[i] = _size;
1222 glyphs->advances_y[i] = 0;
1226 void QFontEngineBox::addOutlineToPath(qreal x, qreal y, const QGlyphLayout &glyphs, QPainterPath *path, QTextItem::RenderFlags flags)
1228 if (!glyphs.numGlyphs)
1231 QVarLengthArray<QFixedPoint> positions;
1232 QVarLengthArray<glyph_t> positioned_glyphs;
1233 QTransform matrix = QTransform::fromTranslate(x, y - _size);
1234 getGlyphPositions(glyphs, matrix, flags, positioned_glyphs, positions);
1236 QSize s(_size - 3, _size - 3);
1237 for (int k = 0; k < positions.size(); k++)
1238 path->addRect(QRectF(positions[k].toPointF(), s));
1241 glyph_metrics_t QFontEngineBox::boundingBox(const QGlyphLayout &glyphs)
1243 glyph_metrics_t overall;
1244 overall.width = _size*glyphs.numGlyphs;
1245 overall.height = _size;
1246 overall.xoff = overall.width;
1250 #if defined(Q_WS_QWS) || defined(Q_WS_QPA)
1251 void QFontEngineBox::draw(QPaintEngine *p, qreal x, qreal y, const QTextItemInt &ti)
1253 if (!ti.glyphs.numGlyphs)
1256 // any fixes here should probably also be done in QPaintEnginePrivate::drawBoxTextItem
1257 QSize s(_size - 3, _size - 3);
1259 QVarLengthArray<QFixedPoint> positions;
1260 QVarLengthArray<glyph_t> glyphs;
1261 QTransform matrix = QTransform::fromTranslate(x, y - _size);
1262 ti.fontEngine->getGlyphPositions(ti.glyphs, matrix, ti.flags, glyphs, positions);
1263 if (glyphs.size() == 0)
1267 QPainter *painter = p->painter();
1269 painter->setBrush(Qt::NoBrush);
1270 QPen pen = painter->pen();
1271 pen.setWidthF(lineThickness().toReal());
1272 painter->setPen(pen);
1273 for (int k = 0; k < positions.size(); k++)
1274 painter->drawRect(QRectF(positions[k].toPointF(), s));
1279 glyph_metrics_t QFontEngineBox::boundingBox(glyph_t)
1281 return glyph_metrics_t(0, -_size, _size, _size, _size, 0);
1286 QFixed QFontEngineBox::ascent() const
1291 QFixed QFontEngineBox::descent() const
1296 QFixed QFontEngineBox::leading() const
1298 QFixed l = _size * QFixed::fromReal(qreal(0.15));
1302 qreal QFontEngineBox::maxCharWidth() const
1308 int QFontEngineBox::cmap() const
1314 const char *QFontEngineBox::name() const
1319 bool QFontEngineBox::canRender(const QChar *, int)
1324 QFontEngine::Type QFontEngineBox::type() const
1329 QImage QFontEngineBox::alphaMapForGlyph(glyph_t)
1331 QImage image(_size, _size, QImage::Format_Indexed8);
1332 QVector<QRgb> colors(256);
1333 for (int i=0; i<256; ++i)
1334 colors[i] = qRgba(0, 0, 0, i);
1335 image.setColorTable(colors);
1338 // can't use qpainter for index8; so use setPixel to draw our rectangle.
1339 for (int i=2; i <= _size-3; ++i) {
1340 image.setPixel(i, 2, 255);
1341 image.setPixel(i, _size-3, 255);
1342 image.setPixel(2, i, 255);
1343 image.setPixel(_size-3, i, 255);
1348 // ------------------------------------------------------------------
1350 // ------------------------------------------------------------------
1352 static inline uchar highByte(glyph_t glyph)
1353 { return glyph >> 24; }
1355 // strip high byte from glyph
1356 static inline glyph_t stripped(glyph_t glyph)
1357 { return glyph & 0x00ffffff; }
1359 QFontEngineMulti::QFontEngineMulti(int engineCount)
1361 engines.fill(0, engineCount);
1365 QFontEngineMulti::~QFontEngineMulti()
1367 for (int i = 0; i < engines.size(); ++i) {
1368 QFontEngine *fontEngine = engines.at(i);
1370 fontEngine->ref.deref();
1371 if (fontEngine->cache_count == 0 && fontEngine->ref == 0)
1377 bool QFontEngineMulti::stringToCMap(const QChar *str, int len,
1378 QGlyphLayout *glyphs, int *nglyphs,
1379 QTextEngine::ShaperFlags flags) const
1382 if (!engine(0)->stringToCMap(str, len, glyphs, &ng, flags))
1386 for (int i = 0; i < len; ++i) {
1387 bool surrogate = (str[i].isHighSurrogate() && i < len-1 && str[i+1].isLowSurrogate());
1389 if (glyphs->glyphs[glyph_pos] == 0 && str[i].category() != QChar::Separator_Line) {
1390 QGlyphLayoutInstance tmp = glyphs->instance(glyph_pos);
1391 for (int x = 1; x < engines.size(); ++x) {
1392 QFontEngine *engine = engines.at(x);
1394 const_cast<QFontEngineMulti *>(this)->loadEngine(x);
1395 engine = engines.at(x);
1397 Q_ASSERT(engine != 0);
1398 if (engine->type() == Box)
1400 glyphs->advances_x[glyph_pos] = glyphs->advances_y[glyph_pos] = 0;
1401 glyphs->offsets[glyph_pos] = QFixedPoint();
1403 QGlyphLayout offs = glyphs->mid(glyph_pos, num);
1404 engine->stringToCMap(str + i, surrogate ? 2 : 1, &offs, &num, flags);
1405 Q_ASSERT(num == 1); // surrogates only give 1 glyph
1406 if (glyphs->glyphs[glyph_pos]) {
1407 // set the high byte to indicate which engine the glyph came from
1408 glyphs->glyphs[glyph_pos] |= (x << 24);
1412 // ensure we use metrics from the 1st font when we use the fallback image.
1413 if (!glyphs->glyphs[glyph_pos]) {
1414 glyphs->setInstance(glyph_pos, tmp);
1423 glyphs->numGlyphs = ng;
1427 glyph_metrics_t QFontEngineMulti::boundingBox(const QGlyphLayout &glyphs)
1429 if (glyphs.numGlyphs <= 0)
1430 return glyph_metrics_t();
1432 glyph_metrics_t overall;
1434 int which = highByte(glyphs.glyphs[0]);
1437 for (end = 0; end < glyphs.numGlyphs; ++end) {
1438 const int e = highByte(glyphs.glyphs[end]);
1442 // set the high byte to zero
1443 for (i = start; i < end; ++i)
1444 glyphs.glyphs[i] = stripped(glyphs.glyphs[i]);
1446 // merge the bounding box for this run
1447 const glyph_metrics_t gm = engine(which)->boundingBox(glyphs.mid(start, end - start));
1449 overall.x = qMin(overall.x, gm.x);
1450 overall.y = qMin(overall.y, gm.y);
1451 overall.width = overall.xoff + gm.width;
1452 overall.height = qMax(overall.height + overall.y, gm.height + gm.y) -
1453 qMin(overall.y, gm.y);
1454 overall.xoff += gm.xoff;
1455 overall.yoff += gm.yoff;
1457 // reset the high byte for all glyphs
1458 const int hi = which << 24;
1459 for (i = start; i < end; ++i)
1460 glyphs.glyphs[i] = hi | glyphs.glyphs[i];
1467 // set the high byte to zero
1468 for (i = start; i < end; ++i)
1469 glyphs.glyphs[i] = stripped(glyphs.glyphs[i]);
1471 // merge the bounding box for this run
1472 const glyph_metrics_t gm = engine(which)->boundingBox(glyphs.mid(start, end - start));
1474 overall.x = qMin(overall.x, gm.x);
1475 overall.y = qMin(overall.y, gm.y);
1476 overall.width = overall.xoff + gm.width;
1477 overall.height = qMax(overall.height + overall.y, gm.height + gm.y) -
1478 qMin(overall.y, gm.y);
1479 overall.xoff += gm.xoff;
1480 overall.yoff += gm.yoff;
1482 // reset the high byte for all glyphs
1483 const int hi = which << 24;
1484 for (i = start; i < end; ++i)
1485 glyphs.glyphs[i] = hi | glyphs.glyphs[i];
1490 void QFontEngineMulti::getGlyphBearings(glyph_t glyph, qreal *leftBearing, qreal *rightBearing)
1492 int which = highByte(glyph);
1493 engine(which)->getGlyphBearings(stripped(glyph), leftBearing, rightBearing);
1496 void QFontEngineMulti::addOutlineToPath(qreal x, qreal y, const QGlyphLayout &glyphs,
1497 QPainterPath *path, QTextItem::RenderFlags flags)
1499 if (glyphs.numGlyphs <= 0)
1502 int which = highByte(glyphs.glyphs[0]);
1505 if (flags & QTextItem::RightToLeft) {
1506 for (int gl = 0; gl < glyphs.numGlyphs; gl++) {
1507 x += glyphs.advances_x[gl].toReal();
1508 y += glyphs.advances_y[gl].toReal();
1511 for (end = 0; end < glyphs.numGlyphs; ++end) {
1512 const int e = highByte(glyphs.glyphs[end]);
1516 if (flags & QTextItem::RightToLeft) {
1517 for (i = start; i < end; ++i) {
1518 x -= glyphs.advances_x[i].toReal();
1519 y -= glyphs.advances_y[i].toReal();
1523 // set the high byte to zero
1524 for (i = start; i < end; ++i)
1525 glyphs.glyphs[i] = stripped(glyphs.glyphs[i]);
1526 engine(which)->addOutlineToPath(x, y, glyphs.mid(start, end - start), path, flags);
1527 // reset the high byte for all glyphs and update x and y
1528 const int hi = which << 24;
1529 for (i = start; i < end; ++i)
1530 glyphs.glyphs[i] = hi | glyphs.glyphs[i];
1532 if (!(flags & QTextItem::RightToLeft)) {
1533 for (i = start; i < end; ++i) {
1534 x += glyphs.advances_x[i].toReal();
1535 y += glyphs.advances_y[i].toReal();
1544 if (flags & QTextItem::RightToLeft) {
1545 for (i = start; i < end; ++i) {
1546 x -= glyphs.advances_x[i].toReal();
1547 y -= glyphs.advances_y[i].toReal();
1551 // set the high byte to zero
1552 for (i = start; i < end; ++i)
1553 glyphs.glyphs[i] = stripped(glyphs.glyphs[i]);
1555 engine(which)->addOutlineToPath(x, y, glyphs.mid(start, end - start), path, flags);
1557 // reset the high byte for all glyphs
1558 const int hi = which << 24;
1559 for (i = start; i < end; ++i)
1560 glyphs.glyphs[i] = hi | glyphs.glyphs[i];
1563 void QFontEngineMulti::recalcAdvances(QGlyphLayout *glyphs, QTextEngine::ShaperFlags flags) const
1565 if (glyphs->numGlyphs <= 0)
1568 int which = highByte(glyphs->glyphs[0]);
1571 for (end = 0; end < glyphs->numGlyphs; ++end) {
1572 const int e = highByte(glyphs->glyphs[end]);
1576 // set the high byte to zero
1577 for (i = start; i < end; ++i)
1578 glyphs->glyphs[i] = stripped(glyphs->glyphs[i]);
1580 QGlyphLayout offs = glyphs->mid(start, end - start);
1581 engine(which)->recalcAdvances(&offs, flags);
1583 // reset the high byte for all glyphs and update x and y
1584 const int hi = which << 24;
1585 for (i = start; i < end; ++i)
1586 glyphs->glyphs[i] = hi | glyphs->glyphs[i];
1593 // set the high byte to zero
1594 for (i = start; i < end; ++i)
1595 glyphs->glyphs[i] = stripped(glyphs->glyphs[i]);
1597 QGlyphLayout offs = glyphs->mid(start, end - start);
1598 engine(which)->recalcAdvances(&offs, flags);
1600 // reset the high byte for all glyphs
1601 const int hi = which << 24;
1602 for (i = start; i < end; ++i)
1603 glyphs->glyphs[i] = hi | glyphs->glyphs[i];
1606 void QFontEngineMulti::doKerning(QGlyphLayout *glyphs, QTextEngine::ShaperFlags flags) const
1608 if (glyphs->numGlyphs <= 0)
1611 int which = highByte(glyphs->glyphs[0]);
1614 for (end = 0; end < glyphs->numGlyphs; ++end) {
1615 const int e = highByte(glyphs->glyphs[end]);
1619 // set the high byte to zero
1620 for (i = start; i < end; ++i)
1621 glyphs->glyphs[i] = stripped(glyphs->glyphs[i]);
1623 QGlyphLayout offs = glyphs->mid(start, end - start);
1624 engine(which)->doKerning(&offs, flags);
1626 // reset the high byte for all glyphs and update x and y
1627 const int hi = which << 24;
1628 for (i = start; i < end; ++i)
1629 glyphs->glyphs[i] = hi | glyphs->glyphs[i];
1636 // set the high byte to zero
1637 for (i = start; i < end; ++i)
1638 glyphs->glyphs[i] = stripped(glyphs->glyphs[i]);
1640 QGlyphLayout offs = glyphs->mid(start, end - start);
1641 engine(which)->doKerning(&offs, flags);
1643 // reset the high byte for all glyphs
1644 const int hi = which << 24;
1645 for (i = start; i < end; ++i)
1646 glyphs->glyphs[i] = hi | glyphs->glyphs[i];
1649 glyph_metrics_t QFontEngineMulti::boundingBox(glyph_t glyph)
1651 const int which = highByte(glyph);
1652 Q_ASSERT(which < engines.size());
1653 return engine(which)->boundingBox(stripped(glyph));
1656 QFixed QFontEngineMulti::ascent() const
1657 { return engine(0)->ascent(); }
1659 QFixed QFontEngineMulti::descent() const
1660 { return engine(0)->descent(); }
1662 QFixed QFontEngineMulti::leading() const
1664 return engine(0)->leading();
1667 QFixed QFontEngineMulti::xHeight() const
1669 return engine(0)->xHeight();
1672 QFixed QFontEngineMulti::averageCharWidth() const
1674 return engine(0)->averageCharWidth();
1677 QFixed QFontEngineMulti::lineThickness() const
1679 return engine(0)->lineThickness();
1682 QFixed QFontEngineMulti::underlinePosition() const
1684 return engine(0)->underlinePosition();
1687 qreal QFontEngineMulti::maxCharWidth() const
1689 return engine(0)->maxCharWidth();
1692 qreal QFontEngineMulti::minLeftBearing() const
1694 return engine(0)->minLeftBearing();
1697 qreal QFontEngineMulti::minRightBearing() const
1699 return engine(0)->minRightBearing();
1702 bool QFontEngineMulti::canRender(const QChar *string, int len)
1704 if (engine(0)->canRender(string, len))
1707 QVarLengthGlyphLayoutArray glyphs(len);
1709 if (stringToCMap(string, len, &glyphs, &nglyphs, QTextEngine::GlyphIndicesOnly) == false) {
1710 glyphs.resize(nglyphs);
1711 stringToCMap(string, len, &glyphs, &nglyphs, QTextEngine::GlyphIndicesOnly);
1714 bool allExist = true;
1715 for (int i = 0; i < nglyphs; i++) {
1716 if (!glyphs.glyphs[i]) {
1725 /* Implement alphaMapForGlyph() which is called by Lighthouse/Windows code.
1726 * Ideally, that code should be fixed to correctly handle QFontEngineMulti. */
1728 QImage QFontEngineMulti::alphaMapForGlyph(glyph_t glyph)
1730 const int which = highByte(glyph);
1731 Q_ASSERT(which < engines.size());
1732 return engine(which)->alphaMapForGlyph(stripped(glyph));
1735 QImage QFontEngineMulti::alphaMapForGlyph(glyph_t glyph, QFixed subPixelPosition)
1737 const int which = highByte(glyph);
1738 Q_ASSERT(which < engines.size());
1739 return engine(which)->alphaMapForGlyph(stripped(glyph), subPixelPosition);
1742 QImage QFontEngineMulti::alphaMapForGlyph(glyph_t glyph, const QTransform &t)
1744 const int which = highByte(glyph);
1745 Q_ASSERT(which < engines.size());
1746 return engine(which)->alphaMapForGlyph(stripped(glyph), t);
1749 QImage QFontEngineMulti::alphaMapForGlyph(glyph_t glyph, QFixed subPixelPosition, const QTransform &t)
1751 const int which = highByte(glyph);
1752 Q_ASSERT(which < engines.size());
1753 return engine(which)->alphaMapForGlyph(stripped(glyph), subPixelPosition, t);
1756 QImage QFontEngineMulti::alphaRGBMapForGlyph(glyph_t glyph, QFixed subPixelPosition, int margin, const QTransform &t)
1758 const int which = highByte(glyph);
1759 Q_ASSERT(which < engines.size());
1760 return engine(which)->alphaRGBMapForGlyph(stripped(glyph), subPixelPosition, margin, t);