1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtGui module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** 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()
176 memset(&hbFont, 0, sizeof(hbFont));
177 hbFont.klass = &hb_fontClass;
178 hbFont.userData = this;
182 m_subPixelPositionCount = 0;
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*/, 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) const
641 if (m_subPixelPositionCount <= 1 || !supportsSubPixelPositions())
644 QFixed subPixelPosition;
646 subPixelPosition = x - x.floor();
647 QFixed fraction = (subPixelPosition / QFixed::fromReal(1.0 / m_subPixelPositionCount)).floor();
649 // Compensate for precision loss in fixed point to make sure we are always drawing at a subpixel position over
650 // the lower boundary for the selected rasterization by adding 1/64.
651 subPixelPosition = fraction / QFixed(m_subPixelPositionCount) + QFixed::fromReal(0.015625);
653 return subPixelPosition;
656 QImage *QFontEngine::lockedAlphaMapForGlyph(glyph_t glyph, QFixed subPixelPosition,
657 QFontEngine::GlyphFormat neededFormat,
658 const QTransform &t, QPoint *offset)
660 Q_ASSERT(currentlyLockedAlphaMap.isNull());
661 if (neededFormat == Format_None)
662 neededFormat = Format_A32;
664 if (neededFormat != Format_A32)
665 currentlyLockedAlphaMap = alphaMapForGlyph(glyph, subPixelPosition, t);
667 currentlyLockedAlphaMap = alphaRGBMapForGlyph(glyph, subPixelPosition, t);
670 *offset = QPoint(0, 0);
672 return ¤tlyLockedAlphaMap;
675 void QFontEngine::unlockAlphaMapForGlyph()
677 Q_ASSERT(!currentlyLockedAlphaMap.isNull());
678 currentlyLockedAlphaMap = QImage();
681 QImage QFontEngine::alphaMapForGlyph(glyph_t glyph)
683 glyph_metrics_t gm = boundingBox(glyph);
684 int glyph_x = qFloor(gm.x.toReal());
685 int glyph_y = qFloor(gm.y.toReal());
686 int glyph_width = qCeil((gm.x + gm.width).toReal()) - glyph_x;
687 int glyph_height = qCeil((gm.y + gm.height).toReal()) - glyph_y;
689 if (glyph_width <= 0 || glyph_height <= 0)
693 pt.y = -glyph_y; // the baseline
695 QImage im(glyph_width + 4, glyph_height, QImage::Format_ARGB32_Premultiplied);
696 im.fill(Qt::transparent);
698 p.setRenderHint(QPainter::Antialiasing);
699 addGlyphsToPath(&glyph, &pt, 1, &path, 0);
701 p.setBrush(Qt::black);
705 QImage indexed(im.width(), im.height(), QImage::Format_Indexed8);
706 QVector<QRgb> colors(256);
707 for (int i=0; i<256; ++i)
708 colors[i] = qRgba(0, 0, 0, i);
709 indexed.setColorTable(colors);
711 for (int y=0; y<im.height(); ++y) {
712 uchar *dst = (uchar *) indexed.scanLine(y);
713 uint *src = (uint *) im.scanLine(y);
714 for (int x=0; x<im.width(); ++x)
715 dst[x] = qAlpha(src[x]);
721 void QFontEngine::removeGlyphFromCache(glyph_t)
725 QFontEngine::Properties QFontEngine::properties() const
728 QByteArray psname = QFontEngine::convertToPostscriptFontFamilyName(fontDef.family.toUtf8());
730 psname += QByteArray::number(fontDef.style);
732 psname += QByteArray::number(fontDef.weight);
734 p.postscriptName = psname;
736 p.descent = descent();
737 p.leading = leading();
738 p.emSquare = p.ascent;
739 p.boundingBox = QRectF(0, -p.ascent.toReal(), maxCharWidth(), (p.ascent + p.descent).toReal());
741 p.capHeight = p.ascent;
742 p.lineWidth = lineThickness();
746 void QFontEngine::getUnscaledGlyph(glyph_t glyph, QPainterPath *path, glyph_metrics_t *metrics)
748 *metrics = boundingBox(glyph);
752 addGlyphsToPath(&glyph, &p, 1, path, QFlag(0));
755 QByteArray QFontEngine::getSfntTable(uint tag) const
759 if (!getSfntTableData(tag, 0, &len))
764 if (!getSfntTableData(tag, reinterpret_cast<uchar *>(table.data()), &len))
769 void QFontEngine::setGlyphCache(void *key, QFontEngineGlyphCache *data)
773 GlyphCacheEntry entry;
776 if (m_glyphCaches.contains(entry))
779 // Limit the glyph caches to 4. This covers all 90 degree rotations and limits
780 // memory use when there is continuous or random rotation
781 if (m_glyphCaches.size() == 4)
782 m_glyphCaches.removeLast();
784 m_glyphCaches.push_front(entry);
788 QFontEngineGlyphCache *QFontEngine::glyphCache(void *key, QFontEngineGlyphCache::Type type, const QTransform &transform) const
790 for (QLinkedList<GlyphCacheEntry>::const_iterator it = m_glyphCaches.constBegin(), end = m_glyphCaches.constEnd(); it != end; ++it) {
791 QFontEngineGlyphCache *c = it->cache.data();
792 if (key == it->context
793 && type == c->cacheType()
794 && qtransform_equals_no_translate(c->m_transform, transform)) {
801 static inline QFixed kerning(int left, int right, const QFontEngine::KernPair *pairs, int numPairs)
803 uint left_right = (left << 16) + right;
805 left = 0, right = numPairs - 1;
806 while (left <= right) {
807 int middle = left + ( ( right - left ) >> 1 );
809 if(pairs[middle].left_right == left_right)
810 return pairs[middle].adjust;
812 if (pairs[middle].left_right < left_right)
820 void QFontEngine::doKerning(QGlyphLayout *glyphs, QTextEngine::ShaperFlags flags) const
822 int numPairs = kerning_pairs.size();
826 const KernPair *pairs = kerning_pairs.constData();
828 if(flags & QTextEngine::DesignMetrics) {
829 for(int i = 0; i < glyphs->numGlyphs - 1; ++i)
830 glyphs->advances_x[i] += kerning(glyphs->glyphs[i], glyphs->glyphs[i+1] , pairs, numPairs);
832 for(int i = 0; i < glyphs->numGlyphs - 1; ++i)
833 glyphs->advances_x[i] += qRound(kerning(glyphs->glyphs[i], glyphs->glyphs[i+1] , pairs, numPairs));
837 void QFontEngine::loadKerningPairs(QFixed scalingFactor)
839 kerning_pairs.clear();
841 QByteArray tab = getSfntTable(MAKE_TAG('k', 'e', 'r', 'n'));
845 const uchar *table = reinterpret_cast<const uchar *>(tab.constData());
847 unsigned short version = qFromBigEndian<quint16>(table);
849 // qDebug("wrong version");
853 unsigned short numTables = qFromBigEndian<quint16>(table + 2);
856 for(int i = 0; i < numTables; ++i) {
857 if (offset + 6 > tab.size()) {
858 // qDebug("offset out of bounds");
861 const uchar *header = table + offset;
863 ushort version = qFromBigEndian<quint16>(header);
864 ushort length = qFromBigEndian<quint16>(header+2);
865 ushort coverage = qFromBigEndian<quint16>(header+4);
866 // qDebug("subtable: version=%d, coverage=%x",version, coverage);
867 if(version == 0 && coverage == 0x0001) {
868 if (offset + length > tab.size()) {
869 // qDebug("length ouf ot bounds");
872 const uchar *data = table + offset + 6;
874 ushort nPairs = qFromBigEndian<quint16>(data);
875 if(nPairs * 6 + 8 > length - 6) {
876 // qDebug("corrupt table!");
882 for(int i = 0; i < nPairs; ++i) {
883 QFontEngine::KernPair p;
884 p.left_right = (((uint)qFromBigEndian<quint16>(data+off)) << 16) + qFromBigEndian<quint16>(data+off+2);
885 p.adjust = QFixed(((int)(short)qFromBigEndian<quint16>(data+off+4))) / scalingFactor;
886 kerning_pairs.append(p);
894 qSort(kerning_pairs);
895 // for (int i = 0; i < kerning_pairs.count(); ++i)
896 // qDebug() << 'i' << i << "left_right" << hex << kerning_pairs.at(i).left_right;
900 int QFontEngine::glyphCount() const
902 QByteArray maxpTable = getSfntTable(MAKE_TAG('m', 'a', 'x', 'p'));
903 if (maxpTable.size() < 6)
905 return qFromBigEndian<quint16>(reinterpret_cast<const uchar *>(maxpTable.constData() + 4));
908 const uchar *QFontEngine::getCMap(const uchar *table, uint tableSize, bool *isSymbolFont, int *cmapSize)
910 const uchar *header = table;
914 const uchar *endPtr = table + tableSize;
917 if (qFromBigEndian<quint16>(header) != 0)
920 unsigned short numTables = qFromBigEndian<quint16>(header + 2);
921 const uchar *maps = table + 4;
922 if (maps + 8 * numTables > endPtr)
932 MicrosoftUnicodeExtended
935 int symbolTable = -1;
938 for (int n = 0; n < numTables; ++n) {
939 const quint16 platformId = qFromBigEndian<quint16>(maps + 8 * n);
940 const quint16 platformSpecificId = qFromBigEndian<quint16>(maps + 8 * n + 2);
941 switch (platformId) {
943 if (score < Unicode &&
944 (platformSpecificId == 0 ||
945 platformSpecificId == 2 ||
946 platformSpecificId == 3)) {
949 } else if (score < Unicode11 && platformSpecificId == 1) {
955 if (score < AppleRoman && platformSpecificId == 0) { // Apple Roman
961 switch (platformSpecificId) {
964 if (score < Symbol) {
970 if (score < MicrosoftUnicode) {
972 score = MicrosoftUnicode;
976 if (score < MicrosoftUnicodeExtended) {
978 score = MicrosoftUnicodeExtended;
992 *isSymbolFont = (symbolTable > -1);
994 unsigned int unicode_table = qFromBigEndian<quint32>(maps + 8*tableToUse + 4);
996 if (!unicode_table || unicode_table + 8 > tableSize)
999 // get the header of the unicode table
1000 header = table + unicode_table;
1002 unsigned short format = qFromBigEndian<quint16>(header);
1003 unsigned int length;
1005 length = qFromBigEndian<quint16>(header + 2);
1007 length = qFromBigEndian<quint32>(header + 4);
1009 if (table + unicode_table + length > endPtr)
1013 // To support symbol fonts that contain a unicode table for the symbol area
1014 // we check the cmap tables and fall back to symbol font unless that would
1015 // involve losing information from the unicode table
1016 if (symbolTable > -1 && ((score == Unicode) || (score == Unicode11))) {
1017 const uchar *selectedTable = table + unicode_table;
1019 // Check that none of the latin1 range are in the unicode table
1020 bool unicodeTableHasLatin1 = false;
1021 for (int uc=0x00; uc<0x100; ++uc) {
1022 if (getTrueTypeGlyphIndex(selectedTable, uc) != 0) {
1023 unicodeTableHasLatin1 = true;
1028 // Check that at least one symbol char is in the unicode table
1029 bool unicodeTableHasSymbols = false;
1030 if (!unicodeTableHasLatin1) {
1031 for (int uc=0xf000; uc<0xf100; ++uc) {
1032 if (getTrueTypeGlyphIndex(selectedTable, uc) != 0) {
1033 unicodeTableHasSymbols = true;
1039 // Fall back to symbol table
1040 if (!unicodeTableHasLatin1 && unicodeTableHasSymbols) {
1041 tableToUse = symbolTable;
1047 return table + unicode_table;
1050 quint32 QFontEngine::getTrueTypeGlyphIndex(const uchar *cmap, uint unicode)
1052 unsigned short format = qFromBigEndian<quint16>(cmap);
1055 return (int) *(cmap+6+unicode);
1056 } else if (format == 4) {
1057 /* some fonts come with invalid cmap tables, where the last segment
1058 specified end = start = rangeoffset = 0xffff, delta = 0x0001
1059 Since 0xffff is never a valid Unicode char anyway, we just get rid of the issue
1060 by returning 0 for 0xffff
1062 if(unicode >= 0xffff)
1064 quint16 segCountX2 = qFromBigEndian<quint16>(cmap + 6);
1065 const unsigned char *ends = cmap + 14;
1067 for (; i < segCountX2/2 && qFromBigEndian<quint16>(ends + 2*i) < unicode; i++) {}
1069 const unsigned char *idx = ends + segCountX2 + 2 + 2*i;
1070 quint16 startIndex = qFromBigEndian<quint16>(idx);
1072 if (startIndex > unicode)
1076 qint16 idDelta = (qint16)qFromBigEndian<quint16>(idx);
1078 quint16 idRangeoffset_t = (quint16)qFromBigEndian<quint16>(idx);
1081 if (idRangeoffset_t) {
1082 quint16 id = qFromBigEndian<quint16>(idRangeoffset_t + 2*(unicode - startIndex) + idx);
1084 glyphIndex = (idDelta + id) % 0x10000;
1088 glyphIndex = (idDelta + unicode) % 0x10000;
1091 } else if (format == 6) {
1092 quint16 tableSize = qFromBigEndian<quint16>(cmap + 2);
1094 quint16 firstCode6 = qFromBigEndian<quint16>(cmap + 6);
1095 if (unicode < firstCode6)
1098 quint16 entryCount6 = qFromBigEndian<quint16>(cmap + 8);
1099 if (entryCount6 * 2 + 10 > tableSize)
1102 quint16 sentinel6 = firstCode6 + entryCount6;
1103 if (unicode >= sentinel6)
1106 quint16 entryIndex6 = unicode - firstCode6;
1107 return qFromBigEndian<quint16>(cmap + 10 + (entryIndex6 * 2));
1108 } else if (format == 12) {
1109 quint32 nGroups = qFromBigEndian<quint32>(cmap + 12);
1111 cmap += 16; // move to start of groups
1113 int left = 0, right = nGroups - 1;
1114 while (left <= right) {
1115 int middle = left + ( ( right - left ) >> 1 );
1117 quint32 startCharCode = qFromBigEndian<quint32>(cmap + 12*middle);
1118 if(unicode < startCharCode)
1121 quint32 endCharCode = qFromBigEndian<quint32>(cmap + 12*middle + 4);
1122 if(unicode <= endCharCode)
1123 return qFromBigEndian<quint32>(cmap + 12*middle + 8) + unicode - startCharCode;
1128 qDebug("cmap table of format %d not implemented", format);
1134 QByteArray QFontEngine::convertToPostscriptFontFamilyName(const QByteArray &family)
1136 QByteArray f = family;
1151 QFixed QFontEngine::lastRightBearing(const QGlyphLayout &glyphs, bool round)
1153 if (glyphs.numGlyphs >= 1) {
1154 glyph_t glyph = glyphs.glyphs[glyphs.numGlyphs - 1];
1155 glyph_metrics_t gi = boundingBox(glyph);
1157 return round ? QFixed(qRound(gi.xoff - gi.x - gi.width))
1158 : QFixed(gi.xoff - gi.x - gi.width);
1163 // ------------------------------------------------------------------
1164 // The box font engine
1165 // ------------------------------------------------------------------
1167 QFontEngineBox::QFontEngineBox(int size)
1170 cache_cost = sizeof(QFontEngineBox);
1173 QFontEngineBox::~QFontEngineBox()
1177 bool QFontEngineBox::stringToCMap(const QChar *, int len, QGlyphLayout *glyphs, int *nglyphs, QTextEngine::ShaperFlags flags) const
1179 if (*nglyphs < len) {
1184 for (int i = 0; i < len; i++) {
1185 glyphs->glyphs[i] = 0;
1186 if (!(flags & QTextEngine::GlyphIndicesOnly)) {
1187 glyphs->advances_x[i] = _size;
1188 glyphs->advances_y[i] = 0;
1193 glyphs->numGlyphs = len;
1197 void QFontEngineBox::recalcAdvances(QGlyphLayout *glyphs, QTextEngine::ShaperFlags) const
1199 for (int i = 0; i < glyphs->numGlyphs; i++) {
1200 glyphs->advances_x[i] = _size;
1201 glyphs->advances_y[i] = 0;
1205 void QFontEngineBox::addOutlineToPath(qreal x, qreal y, const QGlyphLayout &glyphs, QPainterPath *path, QTextItem::RenderFlags flags)
1207 if (!glyphs.numGlyphs)
1210 QVarLengthArray<QFixedPoint> positions;
1211 QVarLengthArray<glyph_t> positioned_glyphs;
1212 QTransform matrix = QTransform::fromTranslate(x, y - _size);
1213 getGlyphPositions(glyphs, matrix, flags, positioned_glyphs, positions);
1215 QSize s(_size - 3, _size - 3);
1216 for (int k = 0; k < positions.size(); k++)
1217 path->addRect(QRectF(positions[k].toPointF(), s));
1220 glyph_metrics_t QFontEngineBox::boundingBox(const QGlyphLayout &glyphs)
1222 glyph_metrics_t overall;
1223 overall.width = _size*glyphs.numGlyphs;
1224 overall.height = _size;
1225 overall.xoff = overall.width;
1229 void QFontEngineBox::draw(QPaintEngine *p, qreal x, qreal y, const QTextItemInt &ti)
1231 if (!ti.glyphs.numGlyphs)
1234 // any fixes here should probably also be done in QPaintEnginePrivate::drawBoxTextItem
1235 QSize s(_size - 3, _size - 3);
1237 QVarLengthArray<QFixedPoint> positions;
1238 QVarLengthArray<glyph_t> glyphs;
1239 QTransform matrix = QTransform::fromTranslate(x, y - _size);
1240 ti.fontEngine->getGlyphPositions(ti.glyphs, matrix, ti.flags, glyphs, positions);
1241 if (glyphs.size() == 0)
1245 QPainter *painter = p->painter();
1247 painter->setBrush(Qt::NoBrush);
1248 QPen pen = painter->pen();
1249 pen.setWidthF(lineThickness().toReal());
1250 painter->setPen(pen);
1251 for (int k = 0; k < positions.size(); k++)
1252 painter->drawRect(QRectF(positions[k].toPointF(), s));
1256 glyph_metrics_t QFontEngineBox::boundingBox(glyph_t)
1258 return glyph_metrics_t(0, -_size, _size, _size, _size, 0);
1261 QFontEngine *QFontEngineBox::cloneWithSize(qreal pixelSize) const
1263 QFontEngineBox *fe = new QFontEngineBox(pixelSize);
1267 QFixed QFontEngineBox::ascent() const
1272 QFixed QFontEngineBox::descent() const
1277 QFixed QFontEngineBox::leading() const
1279 QFixed l = _size * QFixed::fromReal(qreal(0.15));
1283 qreal QFontEngineBox::maxCharWidth() const
1289 const char *QFontEngineBox::name() const
1294 bool QFontEngineBox::canRender(const QChar *, int)
1299 QFontEngine::Type QFontEngineBox::type() const
1304 QImage QFontEngineBox::alphaMapForGlyph(glyph_t)
1306 QImage image(_size, _size, QImage::Format_Indexed8);
1307 QVector<QRgb> colors(256);
1308 for (int i=0; i<256; ++i)
1309 colors[i] = qRgba(0, 0, 0, i);
1310 image.setColorTable(colors);
1313 // can't use qpainter for index8; so use setPixel to draw our rectangle.
1314 for (int i=2; i <= _size-3; ++i) {
1315 image.setPixel(i, 2, 255);
1316 image.setPixel(i, _size-3, 255);
1317 image.setPixel(2, i, 255);
1318 image.setPixel(_size-3, i, 255);
1323 // ------------------------------------------------------------------
1325 // ------------------------------------------------------------------
1327 static inline uchar highByte(glyph_t glyph)
1328 { return glyph >> 24; }
1330 // strip high byte from glyph
1331 static inline glyph_t stripped(glyph_t glyph)
1332 { return glyph & 0x00ffffff; }
1334 QFontEngineMulti::QFontEngineMulti(int engineCount)
1336 engines.fill(0, engineCount);
1340 QFontEngineMulti::~QFontEngineMulti()
1342 for (int i = 0; i < engines.size(); ++i) {
1343 QFontEngine *fontEngine = engines.at(i);
1345 fontEngine->ref.deref();
1346 if (fontEngine->cache_count == 0 && fontEngine->ref.load() == 0)
1352 bool QFontEngineMulti::stringToCMap(const QChar *str, int len,
1353 QGlyphLayout *glyphs, int *nglyphs,
1354 QTextEngine::ShaperFlags flags) const
1357 if (!engine(0)->stringToCMap(str, len, glyphs, &ng, flags))
1361 for (int i = 0; i < len; ++i) {
1362 bool surrogate = (str[i].isHighSurrogate() && i < len-1 && str[i+1].isLowSurrogate());
1363 uint ucs4 = surrogate ? QChar::surrogateToUcs4(str[i], str[i+1]) : str[i].unicode();
1364 if (glyphs->glyphs[glyph_pos] == 0 && str[i].category() != QChar::Separator_Line) {
1365 QGlyphLayoutInstance tmp = glyphs->instance(glyph_pos);
1366 for (int x=1; x < engines.size(); ++x) {
1367 if (!shouldLoadFontEngineForCharacter(x, ucs4))
1370 QFontEngine *engine = engines.at(x);
1371 bool deleteThisEngine = false;
1373 const_cast<QFontEngineMulti *>(this)->loadEngine(x);
1374 engine = engines.at(x);
1375 deleteThisEngine = true;
1377 Q_ASSERT(engine != 0);
1378 if (engine->type() == Box)
1381 glyphs->advances_x[glyph_pos] = glyphs->advances_y[glyph_pos] = 0;
1382 glyphs->offsets[glyph_pos] = QFixedPoint();
1384 QGlyphLayout offs = glyphs->mid(glyph_pos, num);
1385 engine->stringToCMap(str + i, surrogate ? 2 : 1, &offs, &num, flags);
1386 Q_ASSERT(num == 1); // surrogates only give 1 glyph
1387 if (glyphs->glyphs[glyph_pos]) {
1388 // set the high byte to indicate which engine the glyph came from
1389 glyphs->glyphs[glyph_pos] |= (x << 24);
1391 } else if (deleteThisEngine) {
1392 const_cast<QFontEngineMulti *>(this)->unloadEngine(x);
1396 // ensure we use metrics from the 1st font when we use the fallback image.
1397 if (!glyphs->glyphs[glyph_pos]) {
1398 glyphs->setInstance(glyph_pos, tmp);
1408 glyphs->numGlyphs = ng;
1412 bool QFontEngineMulti::shouldLoadFontEngineForCharacter(int at, uint ucs4) const
1419 void QFontEngineMulti::unloadEngine(int at)
1421 QFontEngine *fontEngine = engines.at(at);
1422 if (fontEngine == 0)
1425 // If there are other references to the engine, keep it around and keep the reference
1426 if (fontEngine->ref.load() == 1) {
1427 QFontCache::instance()->removeEngine(fontEngine);
1428 if (fontEngine->cache_count == 0) {
1435 glyph_metrics_t QFontEngineMulti::boundingBox(const QGlyphLayout &glyphs)
1437 if (glyphs.numGlyphs <= 0)
1438 return glyph_metrics_t();
1440 glyph_metrics_t overall;
1442 int which = highByte(glyphs.glyphs[0]);
1445 for (end = 0; end < glyphs.numGlyphs; ++end) {
1446 const int e = highByte(glyphs.glyphs[end]);
1450 // set the high byte to zero
1451 for (i = start; i < end; ++i)
1452 glyphs.glyphs[i] = stripped(glyphs.glyphs[i]);
1454 // merge the bounding box for this run
1455 const glyph_metrics_t gm = engine(which)->boundingBox(glyphs.mid(start, end - start));
1457 overall.x = qMin(overall.x, gm.x);
1458 overall.y = qMin(overall.y, gm.y);
1459 overall.width = overall.xoff + gm.width;
1460 overall.height = qMax(overall.height + overall.y, gm.height + gm.y) -
1461 qMin(overall.y, gm.y);
1462 overall.xoff += gm.xoff;
1463 overall.yoff += gm.yoff;
1465 // reset the high byte for all glyphs
1466 const int hi = which << 24;
1467 for (i = start; i < end; ++i)
1468 glyphs.glyphs[i] = hi | glyphs.glyphs[i];
1475 // set the high byte to zero
1476 for (i = start; i < end; ++i)
1477 glyphs.glyphs[i] = stripped(glyphs.glyphs[i]);
1479 // merge the bounding box for this run
1480 const glyph_metrics_t gm = engine(which)->boundingBox(glyphs.mid(start, end - start));
1482 overall.x = qMin(overall.x, gm.x);
1483 overall.y = qMin(overall.y, gm.y);
1484 overall.width = overall.xoff + gm.width;
1485 overall.height = qMax(overall.height + overall.y, gm.height + gm.y) -
1486 qMin(overall.y, gm.y);
1487 overall.xoff += gm.xoff;
1488 overall.yoff += gm.yoff;
1490 // reset the high byte for all glyphs
1491 const int hi = which << 24;
1492 for (i = start; i < end; ++i)
1493 glyphs.glyphs[i] = hi | glyphs.glyphs[i];
1498 void QFontEngineMulti::getGlyphBearings(glyph_t glyph, qreal *leftBearing, qreal *rightBearing)
1500 int which = highByte(glyph);
1501 engine(which)->getGlyphBearings(stripped(glyph), leftBearing, rightBearing);
1504 void QFontEngineMulti::addOutlineToPath(qreal x, qreal y, const QGlyphLayout &glyphs,
1505 QPainterPath *path, QTextItem::RenderFlags flags)
1507 if (glyphs.numGlyphs <= 0)
1510 int which = highByte(glyphs.glyphs[0]);
1513 if (flags & QTextItem::RightToLeft) {
1514 for (int gl = 0; gl < glyphs.numGlyphs; gl++) {
1515 x += glyphs.advances_x[gl].toReal();
1516 y += glyphs.advances_y[gl].toReal();
1519 for (end = 0; end < glyphs.numGlyphs; ++end) {
1520 const int e = highByte(glyphs.glyphs[end]);
1524 if (flags & QTextItem::RightToLeft) {
1525 for (i = start; i < end; ++i) {
1526 x -= glyphs.advances_x[i].toReal();
1527 y -= glyphs.advances_y[i].toReal();
1531 // set the high byte to zero
1532 for (i = start; i < end; ++i)
1533 glyphs.glyphs[i] = stripped(glyphs.glyphs[i]);
1534 engine(which)->addOutlineToPath(x, y, glyphs.mid(start, end - start), path, flags);
1535 // reset the high byte for all glyphs and update x and y
1536 const int hi = which << 24;
1537 for (i = start; i < end; ++i)
1538 glyphs.glyphs[i] = hi | glyphs.glyphs[i];
1540 if (!(flags & QTextItem::RightToLeft)) {
1541 for (i = start; i < end; ++i) {
1542 x += glyphs.advances_x[i].toReal();
1543 y += glyphs.advances_y[i].toReal();
1552 if (flags & QTextItem::RightToLeft) {
1553 for (i = start; i < end; ++i) {
1554 x -= glyphs.advances_x[i].toReal();
1555 y -= glyphs.advances_y[i].toReal();
1559 // set the high byte to zero
1560 for (i = start; i < end; ++i)
1561 glyphs.glyphs[i] = stripped(glyphs.glyphs[i]);
1563 engine(which)->addOutlineToPath(x, y, glyphs.mid(start, end - start), path, flags);
1565 // reset the high byte for all glyphs
1566 const int hi = which << 24;
1567 for (i = start; i < end; ++i)
1568 glyphs.glyphs[i] = hi | glyphs.glyphs[i];
1571 void QFontEngineMulti::recalcAdvances(QGlyphLayout *glyphs, QTextEngine::ShaperFlags flags) const
1573 if (glyphs->numGlyphs <= 0)
1576 int which = highByte(glyphs->glyphs[0]);
1579 for (end = 0; end < glyphs->numGlyphs; ++end) {
1580 const int e = highByte(glyphs->glyphs[end]);
1584 // set the high byte to zero
1585 for (i = start; i < end; ++i)
1586 glyphs->glyphs[i] = stripped(glyphs->glyphs[i]);
1588 QGlyphLayout offs = glyphs->mid(start, end - start);
1589 engine(which)->recalcAdvances(&offs, flags);
1591 // reset the high byte for all glyphs and update x and y
1592 const int hi = which << 24;
1593 for (i = start; i < end; ++i)
1594 glyphs->glyphs[i] = hi | glyphs->glyphs[i];
1601 // set the high byte to zero
1602 for (i = start; i < end; ++i)
1603 glyphs->glyphs[i] = stripped(glyphs->glyphs[i]);
1605 QGlyphLayout offs = glyphs->mid(start, end - start);
1606 engine(which)->recalcAdvances(&offs, flags);
1608 // reset the high byte for all glyphs
1609 const int hi = which << 24;
1610 for (i = start; i < end; ++i)
1611 glyphs->glyphs[i] = hi | glyphs->glyphs[i];
1614 void QFontEngineMulti::doKerning(QGlyphLayout *glyphs, QTextEngine::ShaperFlags flags) const
1616 if (glyphs->numGlyphs <= 0)
1619 int which = highByte(glyphs->glyphs[0]);
1622 for (end = 0; end < glyphs->numGlyphs; ++end) {
1623 const int e = highByte(glyphs->glyphs[end]);
1627 // set the high byte to zero
1628 for (i = start; i < end; ++i)
1629 glyphs->glyphs[i] = stripped(glyphs->glyphs[i]);
1631 QGlyphLayout offs = glyphs->mid(start, end - start);
1632 engine(which)->doKerning(&offs, flags);
1634 // reset the high byte for all glyphs and update x and y
1635 const int hi = which << 24;
1636 for (i = start; i < end; ++i)
1637 glyphs->glyphs[i] = hi | glyphs->glyphs[i];
1644 // set the high byte to zero
1645 for (i = start; i < end; ++i)
1646 glyphs->glyphs[i] = stripped(glyphs->glyphs[i]);
1648 QGlyphLayout offs = glyphs->mid(start, end - start);
1649 engine(which)->doKerning(&offs, flags);
1651 // reset the high byte for all glyphs
1652 const int hi = which << 24;
1653 for (i = start; i < end; ++i)
1654 glyphs->glyphs[i] = hi | glyphs->glyphs[i];
1657 glyph_metrics_t QFontEngineMulti::boundingBox(glyph_t glyph)
1659 const int which = highByte(glyph);
1660 Q_ASSERT(which < engines.size());
1661 return engine(which)->boundingBox(stripped(glyph));
1664 QFixed QFontEngineMulti::ascent() const
1665 { return engine(0)->ascent(); }
1667 QFixed QFontEngineMulti::descent() const
1668 { return engine(0)->descent(); }
1670 QFixed QFontEngineMulti::leading() const
1672 return engine(0)->leading();
1675 QFixed QFontEngineMulti::xHeight() const
1677 return engine(0)->xHeight();
1680 QFixed QFontEngineMulti::averageCharWidth() const
1682 return engine(0)->averageCharWidth();
1685 QFixed QFontEngineMulti::lineThickness() const
1687 return engine(0)->lineThickness();
1690 QFixed QFontEngineMulti::underlinePosition() const
1692 return engine(0)->underlinePosition();
1695 qreal QFontEngineMulti::maxCharWidth() const
1697 return engine(0)->maxCharWidth();
1700 qreal QFontEngineMulti::minLeftBearing() const
1702 return engine(0)->minLeftBearing();
1705 qreal QFontEngineMulti::minRightBearing() const
1707 return engine(0)->minRightBearing();
1710 bool QFontEngineMulti::canRender(const QChar *string, int len)
1712 if (engine(0)->canRender(string, len))
1715 QVarLengthGlyphLayoutArray glyphs(len);
1717 if (stringToCMap(string, len, &glyphs, &nglyphs, QTextEngine::GlyphIndicesOnly) == false) {
1718 glyphs.resize(nglyphs);
1719 stringToCMap(string, len, &glyphs, &nglyphs, QTextEngine::GlyphIndicesOnly);
1722 bool allExist = true;
1723 for (int i = 0; i < nglyphs; i++) {
1724 if (!glyphs.glyphs[i]) {
1733 /* Implement alphaMapForGlyph() which is called by Lighthouse/Windows code.
1734 * Ideally, that code should be fixed to correctly handle QFontEngineMulti. */
1736 QImage QFontEngineMulti::alphaMapForGlyph(glyph_t glyph)
1738 const int which = highByte(glyph);
1739 Q_ASSERT(which < engines.size());
1740 return engine(which)->alphaMapForGlyph(stripped(glyph));
1743 QImage QFontEngineMulti::alphaMapForGlyph(glyph_t glyph, QFixed subPixelPosition)
1745 const int which = highByte(glyph);
1746 Q_ASSERT(which < engines.size());
1747 return engine(which)->alphaMapForGlyph(stripped(glyph), subPixelPosition);
1750 QImage QFontEngineMulti::alphaMapForGlyph(glyph_t glyph, const QTransform &t)
1752 const int which = highByte(glyph);
1753 Q_ASSERT(which < engines.size());
1754 return engine(which)->alphaMapForGlyph(stripped(glyph), t);
1757 QImage QFontEngineMulti::alphaMapForGlyph(glyph_t glyph, QFixed subPixelPosition, const QTransform &t)
1759 const int which = highByte(glyph);
1760 Q_ASSERT(which < engines.size());
1761 return engine(which)->alphaMapForGlyph(stripped(glyph), subPixelPosition, t);
1764 QImage QFontEngineMulti::alphaRGBMapForGlyph(glyph_t glyph, QFixed subPixelPosition, const QTransform &t)
1766 const int which = highByte(glyph);
1767 Q_ASSERT(which < engines.size());
1768 return engine(which)->alphaRGBMapForGlyph(stripped(glyph), subPixelPosition, t);