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 HB_Face QFontEngine::initializedHarfbuzzFace() const
232 HB_Face face = harfbuzzFace();
233 if (face != 0 && face->font_for_init != 0)
234 face = qHBLoadFace(face);
239 glyph_metrics_t QFontEngine::boundingBox(glyph_t glyph, const QTransform &matrix)
241 glyph_metrics_t metrics = boundingBox(glyph);
243 if (matrix.type() > QTransform::TxTranslate) {
244 return metrics.transformed(matrix);
249 QFixed QFontEngine::xHeight() const
251 QGlyphLayoutArray<8> glyphs;
253 QChar x((ushort)'x');
254 stringToCMap(&x, 1, &glyphs, &nglyphs, QTextEngine::GlyphIndicesOnly);
256 glyph_metrics_t bb = const_cast<QFontEngine *>(this)->boundingBox(glyphs.glyphs[0]);
260 QFixed QFontEngine::averageCharWidth() const
262 QGlyphLayoutArray<8> glyphs;
264 QChar x((ushort)'x');
265 stringToCMap(&x, 1, &glyphs, &nglyphs, QTextEngine::GlyphIndicesOnly);
267 glyph_metrics_t bb = const_cast<QFontEngine *>(this)->boundingBox(glyphs.glyphs[0]);
272 void QFontEngine::getGlyphPositions(const QGlyphLayout &glyphs, const QTransform &matrix, QTextItem::RenderFlags flags,
273 QVarLengthArray<glyph_t> &glyphs_out, QVarLengthArray<QFixedPoint> &positions)
278 const bool transform = matrix.m11() != 1.
279 || matrix.m12() != 0.
280 || matrix.m21() != 0.
281 || matrix.m22() != 1.;
283 xpos = QFixed::fromReal(matrix.dx());
284 ypos = QFixed::fromReal(matrix.dy());
288 if (flags & QTextItem::RightToLeft) {
289 int i = glyphs.numGlyphs;
290 int totalKashidas = 0;
292 if (glyphs.attributes[i].dontPrint)
294 xpos += glyphs.advances_x[i] + QFixed::fromFixed(glyphs.justifications[i].space_18d6);
295 ypos += glyphs.advances_y[i];
296 totalKashidas += glyphs.justifications[i].nKashidas;
298 positions.resize(glyphs.numGlyphs+totalKashidas);
299 glyphs_out.resize(glyphs.numGlyphs+totalKashidas);
302 while(i < glyphs.numGlyphs) {
303 if (glyphs.attributes[i].dontPrint) {
307 xpos -= glyphs.advances_x[i];
308 ypos -= glyphs.advances_y[i];
310 QFixed gpos_x = xpos + glyphs.offsets[i].x;
311 QFixed gpos_y = ypos + glyphs.offsets[i].y;
313 QPointF gpos(gpos_x.toReal(), gpos_y.toReal());
314 gpos = gpos * matrix;
315 gpos_x = QFixed::fromReal(gpos.x());
316 gpos_y = QFixed::fromReal(gpos.y());
318 positions[current].x = gpos_x;
319 positions[current].y = gpos_y;
320 glyphs_out[current] = glyphs.glyphs[i];
322 if (glyphs.justifications[i].nKashidas) {
323 QChar ch(0x640); // Kashida character
324 QGlyphLayoutArray<8> g;
326 stringToCMap(&ch, 1, &g, &nglyphs, 0);
327 for (uint k = 0; k < glyphs.justifications[i].nKashidas; ++k) {
328 xpos -= g.advances_x[0];
329 ypos -= g.advances_y[0];
331 QFixed gpos_x = xpos + glyphs.offsets[i].x;
332 QFixed gpos_y = ypos + glyphs.offsets[i].y;
334 QPointF gpos(gpos_x.toReal(), gpos_y.toReal());
335 gpos = gpos * matrix;
336 gpos_x = QFixed::fromReal(gpos.x());
337 gpos_y = QFixed::fromReal(gpos.y());
339 positions[current].x = gpos_x;
340 positions[current].y = gpos_y;
341 glyphs_out[current] = g.glyphs[0];
345 xpos -= QFixed::fromFixed(glyphs.justifications[i].space_18d6);
350 positions.resize(glyphs.numGlyphs);
351 glyphs_out.resize(glyphs.numGlyphs);
354 while (i < glyphs.numGlyphs) {
355 if (!glyphs.attributes[i].dontPrint) {
356 positions[current].x = xpos + glyphs.offsets[i].x;
357 positions[current].y = ypos + glyphs.offsets[i].y;
358 glyphs_out[current] = glyphs.glyphs[i];
359 xpos += glyphs.advances_x[i] + QFixed::fromFixed(glyphs.justifications[i].space_18d6);
360 ypos += glyphs.advances_y[i];
366 while (i < glyphs.numGlyphs) {
367 if (!glyphs.attributes[i].dontPrint) {
368 QFixed gpos_x = xpos + glyphs.offsets[i].x;
369 QFixed gpos_y = ypos + glyphs.offsets[i].y;
370 QPointF gpos(gpos_x.toReal(), gpos_y.toReal());
371 gpos = gpos * matrix;
372 positions[current].x = QFixed::fromReal(gpos.x());
373 positions[current].y = QFixed::fromReal(gpos.y());
374 glyphs_out[current] = glyphs.glyphs[i];
375 xpos += glyphs.advances_x[i] + QFixed::fromFixed(glyphs.justifications[i].space_18d6);
376 ypos += glyphs.advances_y[i];
383 positions.resize(current);
384 glyphs_out.resize(current);
385 Q_ASSERT(positions.size() == glyphs_out.size());
388 void QFontEngine::getGlyphBearings(glyph_t glyph, qreal *leftBearing, qreal *rightBearing)
390 glyph_metrics_t gi = boundingBox(glyph);
391 bool isValid = gi.isValid();
392 if (leftBearing != 0)
393 *leftBearing = isValid ? gi.x.toReal() : 0.0;
394 if (rightBearing != 0)
395 *rightBearing = isValid ? (gi.xoff - gi.x - gi.width).toReal() : 0.0;
398 glyph_metrics_t QFontEngine::tightBoundingBox(const QGlyphLayout &glyphs)
400 glyph_metrics_t overall;
404 for (int i = 0; i < glyphs.numGlyphs; i++) {
405 glyph_metrics_t bb = boundingBox(glyphs.glyphs[i]);
406 QFixed x = overall.xoff + glyphs.offsets[i].x + bb.x;
407 QFixed y = overall.yoff + glyphs.offsets[i].y + bb.y;
408 overall.x = qMin(overall.x, x);
409 overall.y = qMin(overall.y, y);
410 xmax = qMax(xmax, x + bb.width);
411 ymax = qMax(ymax, y + bb.height);
412 overall.xoff += bb.xoff;
413 overall.yoff += bb.yoff;
415 overall.height = qMax(overall.height, ymax - overall.y);
416 overall.width = xmax - overall.x;
422 void QFontEngine::addOutlineToPath(qreal x, qreal y, const QGlyphLayout &glyphs, QPainterPath *path,
423 QTextItem::RenderFlags flags)
425 if (!glyphs.numGlyphs)
428 QVarLengthArray<QFixedPoint> positions;
429 QVarLengthArray<glyph_t> positioned_glyphs;
430 QTransform matrix = QTransform::fromTranslate(x, y);
431 getGlyphPositions(glyphs, matrix, flags, positioned_glyphs, positions);
432 addGlyphsToPath(positioned_glyphs.data(), positions.data(), positioned_glyphs.size(), path, flags);
435 #define GRID(x, y) grid[(y)*(w+1) + (x)]
436 #define SET(x, y) (*(image_data + (y)*bpl + ((x) >> 3)) & (0x80 >> ((x) & 7)))
438 enum { EdgeRight = 0x1,
444 static void collectSingleContour(qreal x0, qreal y0, uint *grid, int x, int y, int w, int h, QPainterPath *path)
448 path->moveTo(x + x0, y + y0);
450 if (GRID(x, y) & EdgeRight) {
451 while (GRID(x, y) & EdgeRight) {
452 GRID(x, y) &= ~EdgeRight;
456 path->lineTo(x + x0, y + y0);
459 if (GRID(x, y) & EdgeDown) {
460 while (GRID(x, y) & EdgeDown) {
461 GRID(x, y) &= ~EdgeDown;
465 path->lineTo(x + x0, y + y0);
468 if (GRID(x, y) & EdgeLeft) {
469 while (GRID(x, y) & EdgeLeft) {
470 GRID(x, y) &= ~EdgeLeft;
474 path->lineTo(x + x0, y + y0);
477 if (GRID(x, y) & EdgeUp) {
478 while (GRID(x, y) & EdgeUp) {
479 GRID(x, y) &= ~EdgeUp;
483 path->lineTo(x + x0, y + y0);
487 path->closeSubpath();
490 Q_GUI_EXPORT void qt_addBitmapToPath(qreal x0, qreal y0, const uchar *image_data, int bpl, int w, int h, QPainterPath *path)
492 uint *grid = new uint[(w+1)*(h+1)];
494 for (int y = 0; y <= h; ++y) {
495 for (int x = 0; x <= w; ++x) {
496 bool topLeft = (x == 0)|(y == 0) ? false : SET(x - 1, y - 1);
497 bool topRight = (x == w)|(y == 0) ? false : SET(x, y - 1);
498 bool bottomLeft = (x == 0)|(y == h) ? false : SET(x - 1, y);
499 bool bottomRight = (x == w)|(y == h) ? false : SET(x, y);
502 if ((!topRight) & bottomRight)
503 GRID(x, y) |= EdgeRight;
504 if ((!bottomRight) & bottomLeft)
505 GRID(x, y) |= EdgeDown;
506 if ((!bottomLeft) & topLeft)
507 GRID(x, y) |= EdgeLeft;
508 if ((!topLeft) & topRight)
509 GRID(x, y) |= EdgeUp;
514 for (int y = 0; y < h; ++y) {
515 for (int x = 0; x < w; ++x) {
518 // found start of a contour, follow it
519 collectSingleContour(x0, y0, grid, x, y, w, h, path);
529 void QFontEngine::addBitmapFontToPath(qreal x, qreal y, const QGlyphLayout &glyphs,
530 QPainterPath *path, QTextItem::RenderFlags flags)
532 // TODO what to do with 'flags' ??
534 QFixed advanceX = QFixed::fromReal(x);
535 QFixed advanceY = QFixed::fromReal(y);
536 for (int i=0; i < glyphs.numGlyphs; ++i) {
537 glyph_metrics_t metrics = boundingBox(glyphs.glyphs[i]);
538 if (metrics.width.value() == 0 || metrics.height.value() == 0) {
539 advanceX += glyphs.advances_x[i];
540 advanceY += glyphs.advances_y[i];
543 const QImage alphaMask = alphaMapForGlyph(glyphs.glyphs[i]);
545 const int w = alphaMask.width();
546 const int h = alphaMask.height();
547 const int srcBpl = alphaMask.bytesPerLine();
549 if (alphaMask.depth() == 1) {
552 bitmap = QImage(w, h, QImage::Format_Mono);
553 const uchar *imageData = alphaMask.bits();
554 const int destBpl = bitmap.bytesPerLine();
555 uchar *bitmapData = bitmap.bits();
557 for (int yi = 0; yi < h; ++yi) {
558 const uchar *src = imageData + yi*srcBpl;
559 uchar *dst = bitmapData + yi*destBpl;
560 for (int xi = 0; xi < w; ++xi) {
561 const int byte = xi / 8;
562 const int bit = xi % 8;
566 dst[byte] |= 128 >> bit;
570 const uchar *bitmap_data = bitmap.bits();
571 QFixedPoint offset = glyphs.offsets[i];
572 advanceX += offset.x;
573 advanceY += offset.y;
574 qt_addBitmapToPath((advanceX + metrics.x).toReal(), (advanceY + metrics.y).toReal(), bitmap_data, bitmap.bytesPerLine(), w, h, path);
575 advanceX += glyphs.advances_x[i];
576 advanceY += glyphs.advances_y[i];
580 void QFontEngine::addGlyphsToPath(glyph_t *glyphs, QFixedPoint *positions, int nGlyphs,
581 QPainterPath *path, QTextItem::RenderFlags flags)
583 qreal x = positions[0].x.toReal();
584 qreal y = positions[0].y.toReal();
585 QVarLengthGlyphLayoutArray g(nGlyphs);
587 for (int i = 0; i < nGlyphs; ++i) {
588 g.glyphs[i] = glyphs[i];
589 if (i < nGlyphs - 1) {
590 g.advances_x[i] = positions[i+1].x - positions[i].x;
591 g.advances_y[i] = positions[i+1].y - positions[i].y;
593 g.advances_x[i] = QFixed::fromReal(maxCharWidth());
598 addBitmapFontToPath(x, y, g, path, flags);
601 QImage QFontEngine::alphaMapForGlyph(glyph_t glyph, QFixed /*subPixelPosition*/)
603 // For font engines don't support subpixel positioning
604 return alphaMapForGlyph(glyph);
607 QImage QFontEngine::alphaMapForGlyph(glyph_t glyph, const QTransform &t)
609 QImage i = alphaMapForGlyph(glyph);
610 if (t.type() > QTransform::TxTranslate)
611 i = i.transformed(t).convertToFormat(QImage::Format_Indexed8);
612 Q_ASSERT(i.depth() <= 8); // To verify that transformed didn't change the format...
617 QImage QFontEngine::alphaMapForGlyph(glyph_t glyph, QFixed subPixelPosition, const QTransform &t)
619 if (! supportsSubPixelPositions())
620 return alphaMapForGlyph(glyph, t);
622 QImage i = alphaMapForGlyph(glyph, subPixelPosition);
623 if (t.type() > QTransform::TxTranslate)
624 i = i.transformed(t).convertToFormat(QImage::Format_Indexed8);
625 Q_ASSERT(i.depth() <= 8); // To verify that transformed didn't change the format...
630 QImage QFontEngine::alphaRGBMapForGlyph(glyph_t glyph, QFixed /*subPixelPosition*/, const QTransform &t)
632 QImage alphaMask = alphaMapForGlyph(glyph, t);
633 QImage rgbMask(alphaMask.width(), alphaMask.height(), QImage::Format_RGB32);
635 QVector<QRgb> colorTable = alphaMask.colorTable();
636 for (int y=0; y<alphaMask.height(); ++y) {
637 uint *dst = (uint *) rgbMask.scanLine(y);
638 uchar *src = (uchar *) alphaMask.scanLine(y);
639 for (int x=0; x<alphaMask.width(); ++x) {
640 int val = qAlpha(colorTable.at(src[x]));
641 dst[x] = qRgb(val, val, val);
648 QFixed QFontEngine::subPixelPositionForX(QFixed x) const
650 if (m_subPixelPositionCount <= 1 || !supportsSubPixelPositions())
653 QFixed subPixelPosition;
655 subPixelPosition = x - x.floor();
656 QFixed fraction = (subPixelPosition / QFixed::fromReal(1.0 / m_subPixelPositionCount)).floor();
658 // Compensate for precision loss in fixed point to make sure we are always drawing at a subpixel position over
659 // the lower boundary for the selected rasterization by adding 1/64.
660 subPixelPosition = fraction / QFixed(m_subPixelPositionCount) + QFixed::fromReal(0.015625);
662 return subPixelPosition;
665 QImage *QFontEngine::lockedAlphaMapForGlyph(glyph_t glyph, QFixed subPixelPosition,
666 QFontEngine::GlyphFormat neededFormat,
667 const QTransform &t, QPoint *offset)
669 Q_ASSERT(currentlyLockedAlphaMap.isNull());
670 if (neededFormat == Format_None)
671 neededFormat = Format_A32;
673 if (neededFormat != Format_A32)
674 currentlyLockedAlphaMap = alphaMapForGlyph(glyph, subPixelPosition, t);
676 currentlyLockedAlphaMap = alphaRGBMapForGlyph(glyph, subPixelPosition, t);
679 *offset = QPoint(0, 0);
681 return ¤tlyLockedAlphaMap;
684 void QFontEngine::unlockAlphaMapForGlyph()
686 Q_ASSERT(!currentlyLockedAlphaMap.isNull());
687 currentlyLockedAlphaMap = QImage();
690 QImage QFontEngine::alphaMapForGlyph(glyph_t glyph)
692 glyph_metrics_t gm = boundingBox(glyph);
693 int glyph_x = qFloor(gm.x.toReal());
694 int glyph_y = qFloor(gm.y.toReal());
695 int glyph_width = qCeil((gm.x + gm.width).toReal()) - glyph_x;
696 int glyph_height = qCeil((gm.y + gm.height).toReal()) - glyph_y;
698 if (glyph_width <= 0 || glyph_height <= 0)
702 pt.y = -glyph_y; // the baseline
704 QImage im(glyph_width + 4, glyph_height, QImage::Format_ARGB32_Premultiplied);
705 im.fill(Qt::transparent);
707 p.setRenderHint(QPainter::Antialiasing);
708 addGlyphsToPath(&glyph, &pt, 1, &path, 0);
710 p.setBrush(Qt::black);
714 QImage indexed(im.width(), im.height(), QImage::Format_Indexed8);
715 QVector<QRgb> colors(256);
716 for (int i=0; i<256; ++i)
717 colors[i] = qRgba(0, 0, 0, i);
718 indexed.setColorTable(colors);
720 for (int y=0; y<im.height(); ++y) {
721 uchar *dst = (uchar *) indexed.scanLine(y);
722 uint *src = (uint *) im.scanLine(y);
723 for (int x=0; x<im.width(); ++x)
724 dst[x] = qAlpha(src[x]);
730 void QFontEngine::removeGlyphFromCache(glyph_t)
734 QFontEngine::Properties QFontEngine::properties() const
737 QByteArray psname = QFontEngine::convertToPostscriptFontFamilyName(fontDef.family.toUtf8());
739 psname += QByteArray::number(fontDef.style);
741 psname += QByteArray::number(fontDef.weight);
743 p.postscriptName = psname;
745 p.descent = descent();
746 p.leading = leading();
747 p.emSquare = p.ascent;
748 p.boundingBox = QRectF(0, -p.ascent.toReal(), maxCharWidth(), (p.ascent + p.descent).toReal());
750 p.capHeight = p.ascent;
751 p.lineWidth = lineThickness();
755 void QFontEngine::getUnscaledGlyph(glyph_t glyph, QPainterPath *path, glyph_metrics_t *metrics)
757 *metrics = boundingBox(glyph);
761 addGlyphsToPath(&glyph, &p, 1, path, QFlag(0));
764 QByteArray QFontEngine::getSfntTable(uint tag) const
768 if (!getSfntTableData(tag, 0, &len))
773 if (!getSfntTableData(tag, reinterpret_cast<uchar *>(table.data()), &len))
778 void QFontEngine::setGlyphCache(const void *key, QFontEngineGlyphCache *data)
782 GlyphCacheEntry entry;
785 if (m_glyphCaches.contains(entry))
788 // Limit the glyph caches to 4. This covers all 90 degree rotations and limits
789 // memory use when there is continuous or random rotation
790 if (m_glyphCaches.size() == 4)
791 m_glyphCaches.removeLast();
793 m_glyphCaches.push_front(entry);
797 QFontEngineGlyphCache *QFontEngine::glyphCache(const void *key, QFontEngineGlyphCache::Type type, const QTransform &transform) const
799 for (QLinkedList<GlyphCacheEntry>::const_iterator it = m_glyphCaches.constBegin(), end = m_glyphCaches.constEnd(); it != end; ++it) {
800 QFontEngineGlyphCache *c = it->cache.data();
801 if (key == it->context
802 && type == c->cacheType()
803 && qtransform_equals_no_translate(c->m_transform, transform)) {
810 static inline QFixed kerning(int left, int right, const QFontEngine::KernPair *pairs, int numPairs)
812 uint left_right = (left << 16) + right;
814 left = 0, right = numPairs - 1;
815 while (left <= right) {
816 int middle = left + ( ( right - left ) >> 1 );
818 if(pairs[middle].left_right == left_right)
819 return pairs[middle].adjust;
821 if (pairs[middle].left_right < left_right)
829 void QFontEngine::doKerning(QGlyphLayout *glyphs, QTextEngine::ShaperFlags flags) const
831 int numPairs = kerning_pairs.size();
835 const KernPair *pairs = kerning_pairs.constData();
837 if(flags & QTextEngine::DesignMetrics) {
838 for(int i = 0; i < glyphs->numGlyphs - 1; ++i)
839 glyphs->advances_x[i] += kerning(glyphs->glyphs[i], glyphs->glyphs[i+1] , pairs, numPairs);
841 for(int i = 0; i < glyphs->numGlyphs - 1; ++i)
842 glyphs->advances_x[i] += qRound(kerning(glyphs->glyphs[i], glyphs->glyphs[i+1] , pairs, numPairs));
846 void QFontEngine::loadKerningPairs(QFixed scalingFactor)
848 kerning_pairs.clear();
850 QByteArray tab = getSfntTable(MAKE_TAG('k', 'e', 'r', 'n'));
854 const uchar *table = reinterpret_cast<const uchar *>(tab.constData());
856 unsigned short version = qFromBigEndian<quint16>(table);
858 // qDebug("wrong version");
862 unsigned short numTables = qFromBigEndian<quint16>(table + 2);
865 for(int i = 0; i < numTables; ++i) {
866 if (offset + 6 > tab.size()) {
867 // qDebug("offset out of bounds");
870 const uchar *header = table + offset;
872 ushort version = qFromBigEndian<quint16>(header);
873 ushort length = qFromBigEndian<quint16>(header+2);
874 ushort coverage = qFromBigEndian<quint16>(header+4);
875 // qDebug("subtable: version=%d, coverage=%x",version, coverage);
876 if(version == 0 && coverage == 0x0001) {
877 if (offset + length > tab.size()) {
878 // qDebug("length ouf ot bounds");
881 const uchar *data = table + offset + 6;
883 ushort nPairs = qFromBigEndian<quint16>(data);
884 if(nPairs * 6 + 8 > length - 6) {
885 // qDebug("corrupt table!");
891 for(int i = 0; i < nPairs; ++i) {
892 QFontEngine::KernPair p;
893 p.left_right = (((uint)qFromBigEndian<quint16>(data+off)) << 16) + qFromBigEndian<quint16>(data+off+2);
894 p.adjust = QFixed(((int)(short)qFromBigEndian<quint16>(data+off+4))) / scalingFactor;
895 kerning_pairs.append(p);
903 qSort(kerning_pairs);
904 // for (int i = 0; i < kerning_pairs.count(); ++i)
905 // qDebug() << 'i' << i << "left_right" << hex << kerning_pairs.at(i).left_right;
909 int QFontEngine::glyphCount() const
911 QByteArray maxpTable = getSfntTable(MAKE_TAG('m', 'a', 'x', 'p'));
912 if (maxpTable.size() < 6)
914 return qFromBigEndian<quint16>(reinterpret_cast<const uchar *>(maxpTable.constData() + 4));
917 const uchar *QFontEngine::getCMap(const uchar *table, uint tableSize, bool *isSymbolFont, int *cmapSize)
919 const uchar *header = table;
923 const uchar *endPtr = table + tableSize;
926 if (qFromBigEndian<quint16>(header) != 0)
929 unsigned short numTables = qFromBigEndian<quint16>(header + 2);
930 const uchar *maps = table + 4;
931 if (maps + 8 * numTables > endPtr)
941 MicrosoftUnicodeExtended
944 int symbolTable = -1;
947 for (int n = 0; n < numTables; ++n) {
948 const quint16 platformId = qFromBigEndian<quint16>(maps + 8 * n);
949 const quint16 platformSpecificId = qFromBigEndian<quint16>(maps + 8 * n + 2);
950 switch (platformId) {
952 if (score < Unicode &&
953 (platformSpecificId == 0 ||
954 platformSpecificId == 2 ||
955 platformSpecificId == 3)) {
958 } else if (score < Unicode11 && platformSpecificId == 1) {
964 if (score < AppleRoman && platformSpecificId == 0) { // Apple Roman
970 switch (platformSpecificId) {
973 if (score < Symbol) {
979 if (score < MicrosoftUnicode) {
981 score = MicrosoftUnicode;
985 if (score < MicrosoftUnicodeExtended) {
987 score = MicrosoftUnicodeExtended;
1001 *isSymbolFont = (symbolTable > -1);
1003 unsigned int unicode_table = qFromBigEndian<quint32>(maps + 8*tableToUse + 4);
1005 if (!unicode_table || unicode_table + 8 > tableSize)
1008 // get the header of the unicode table
1009 header = table + unicode_table;
1011 unsigned short format = qFromBigEndian<quint16>(header);
1012 unsigned int length;
1014 length = qFromBigEndian<quint16>(header + 2);
1016 length = qFromBigEndian<quint32>(header + 4);
1018 if (table + unicode_table + length > endPtr)
1022 // To support symbol fonts that contain a unicode table for the symbol area
1023 // we check the cmap tables and fall back to symbol font unless that would
1024 // involve losing information from the unicode table
1025 if (symbolTable > -1 && ((score == Unicode) || (score == Unicode11))) {
1026 const uchar *selectedTable = table + unicode_table;
1028 // Check that none of the latin1 range are in the unicode table
1029 bool unicodeTableHasLatin1 = false;
1030 for (int uc=0x00; uc<0x100; ++uc) {
1031 if (getTrueTypeGlyphIndex(selectedTable, uc) != 0) {
1032 unicodeTableHasLatin1 = true;
1037 // Check that at least one symbol char is in the unicode table
1038 bool unicodeTableHasSymbols = false;
1039 if (!unicodeTableHasLatin1) {
1040 for (int uc=0xf000; uc<0xf100; ++uc) {
1041 if (getTrueTypeGlyphIndex(selectedTable, uc) != 0) {
1042 unicodeTableHasSymbols = true;
1048 // Fall back to symbol table
1049 if (!unicodeTableHasLatin1 && unicodeTableHasSymbols) {
1050 tableToUse = symbolTable;
1056 return table + unicode_table;
1059 quint32 QFontEngine::getTrueTypeGlyphIndex(const uchar *cmap, uint unicode)
1061 unsigned short format = qFromBigEndian<quint16>(cmap);
1064 return (int) *(cmap+6+unicode);
1065 } else if (format == 4) {
1066 /* some fonts come with invalid cmap tables, where the last segment
1067 specified end = start = rangeoffset = 0xffff, delta = 0x0001
1068 Since 0xffff is never a valid Unicode char anyway, we just get rid of the issue
1069 by returning 0 for 0xffff
1071 if(unicode >= 0xffff)
1073 quint16 segCountX2 = qFromBigEndian<quint16>(cmap + 6);
1074 const unsigned char *ends = cmap + 14;
1076 for (; i < segCountX2/2 && qFromBigEndian<quint16>(ends + 2*i) < unicode; i++) {}
1078 const unsigned char *idx = ends + segCountX2 + 2 + 2*i;
1079 quint16 startIndex = qFromBigEndian<quint16>(idx);
1081 if (startIndex > unicode)
1085 qint16 idDelta = (qint16)qFromBigEndian<quint16>(idx);
1087 quint16 idRangeoffset_t = (quint16)qFromBigEndian<quint16>(idx);
1090 if (idRangeoffset_t) {
1091 quint16 id = qFromBigEndian<quint16>(idRangeoffset_t + 2*(unicode - startIndex) + idx);
1093 glyphIndex = (idDelta + id) % 0x10000;
1097 glyphIndex = (idDelta + unicode) % 0x10000;
1100 } else if (format == 6) {
1101 quint16 tableSize = qFromBigEndian<quint16>(cmap + 2);
1103 quint16 firstCode6 = qFromBigEndian<quint16>(cmap + 6);
1104 if (unicode < firstCode6)
1107 quint16 entryCount6 = qFromBigEndian<quint16>(cmap + 8);
1108 if (entryCount6 * 2 + 10 > tableSize)
1111 quint16 sentinel6 = firstCode6 + entryCount6;
1112 if (unicode >= sentinel6)
1115 quint16 entryIndex6 = unicode - firstCode6;
1116 return qFromBigEndian<quint16>(cmap + 10 + (entryIndex6 * 2));
1117 } else if (format == 12) {
1118 quint32 nGroups = qFromBigEndian<quint32>(cmap + 12);
1120 cmap += 16; // move to start of groups
1122 int left = 0, right = nGroups - 1;
1123 while (left <= right) {
1124 int middle = left + ( ( right - left ) >> 1 );
1126 quint32 startCharCode = qFromBigEndian<quint32>(cmap + 12*middle);
1127 if(unicode < startCharCode)
1130 quint32 endCharCode = qFromBigEndian<quint32>(cmap + 12*middle + 4);
1131 if(unicode <= endCharCode)
1132 return qFromBigEndian<quint32>(cmap + 12*middle + 8) + unicode - startCharCode;
1137 qDebug("cmap table of format %d not implemented", format);
1143 QByteArray QFontEngine::convertToPostscriptFontFamilyName(const QByteArray &family)
1145 QByteArray f = family;
1160 QFixed QFontEngine::lastRightBearing(const QGlyphLayout &glyphs, bool round)
1162 if (glyphs.numGlyphs >= 1) {
1163 glyph_t glyph = glyphs.glyphs[glyphs.numGlyphs - 1];
1164 glyph_metrics_t gi = boundingBox(glyph);
1166 return round ? QFixed(qRound(gi.xoff - gi.x - gi.width))
1167 : QFixed(gi.xoff - gi.x - gi.width);
1172 // ------------------------------------------------------------------
1173 // The box font engine
1174 // ------------------------------------------------------------------
1176 QFontEngineBox::QFontEngineBox(int size)
1179 cache_cost = sizeof(QFontEngineBox);
1182 QFontEngineBox::~QFontEngineBox()
1186 bool QFontEngineBox::stringToCMap(const QChar *, int len, QGlyphLayout *glyphs, int *nglyphs, QTextEngine::ShaperFlags flags) const
1188 if (*nglyphs < len) {
1193 for (int i = 0; i < len; i++) {
1194 glyphs->glyphs[i] = 0;
1195 if (!(flags & QTextEngine::GlyphIndicesOnly)) {
1196 glyphs->advances_x[i] = _size;
1197 glyphs->advances_y[i] = 0;
1202 glyphs->numGlyphs = len;
1206 void QFontEngineBox::recalcAdvances(QGlyphLayout *glyphs, QTextEngine::ShaperFlags) const
1208 for (int i = 0; i < glyphs->numGlyphs; i++) {
1209 glyphs->advances_x[i] = _size;
1210 glyphs->advances_y[i] = 0;
1214 void QFontEngineBox::addOutlineToPath(qreal x, qreal y, const QGlyphLayout &glyphs, QPainterPath *path, QTextItem::RenderFlags flags)
1216 if (!glyphs.numGlyphs)
1219 QVarLengthArray<QFixedPoint> positions;
1220 QVarLengthArray<glyph_t> positioned_glyphs;
1221 QTransform matrix = QTransform::fromTranslate(x, y - _size);
1222 getGlyphPositions(glyphs, matrix, flags, positioned_glyphs, positions);
1224 QSize s(_size - 3, _size - 3);
1225 for (int k = 0; k < positions.size(); k++)
1226 path->addRect(QRectF(positions[k].toPointF(), s));
1229 glyph_metrics_t QFontEngineBox::boundingBox(const QGlyphLayout &glyphs)
1231 glyph_metrics_t overall;
1232 overall.width = _size*glyphs.numGlyphs;
1233 overall.height = _size;
1234 overall.xoff = overall.width;
1238 void QFontEngineBox::draw(QPaintEngine *p, qreal x, qreal y, const QTextItemInt &ti)
1240 if (!ti.glyphs.numGlyphs)
1243 // any fixes here should probably also be done in QPaintEnginePrivate::drawBoxTextItem
1244 QSize s(_size - 3, _size - 3);
1246 QVarLengthArray<QFixedPoint> positions;
1247 QVarLengthArray<glyph_t> glyphs;
1248 QTransform matrix = QTransform::fromTranslate(x, y - _size);
1249 ti.fontEngine->getGlyphPositions(ti.glyphs, matrix, ti.flags, glyphs, positions);
1250 if (glyphs.size() == 0)
1254 QPainter *painter = p->painter();
1256 painter->setBrush(Qt::NoBrush);
1257 QPen pen = painter->pen();
1258 pen.setWidthF(lineThickness().toReal());
1259 painter->setPen(pen);
1260 for (int k = 0; k < positions.size(); k++)
1261 painter->drawRect(QRectF(positions[k].toPointF(), s));
1265 glyph_metrics_t QFontEngineBox::boundingBox(glyph_t)
1267 return glyph_metrics_t(0, -_size, _size, _size, _size, 0);
1270 QFontEngine *QFontEngineBox::cloneWithSize(qreal pixelSize) const
1272 QFontEngineBox *fe = new QFontEngineBox(pixelSize);
1276 QFixed QFontEngineBox::ascent() const
1281 QFixed QFontEngineBox::descent() const
1286 QFixed QFontEngineBox::leading() const
1288 QFixed l = _size * QFixed::fromReal(qreal(0.15));
1292 qreal QFontEngineBox::maxCharWidth() const
1298 const char *QFontEngineBox::name() const
1303 bool QFontEngineBox::canRender(const QChar *, int)
1308 QFontEngine::Type QFontEngineBox::type() const
1313 QImage QFontEngineBox::alphaMapForGlyph(glyph_t)
1315 QImage image(_size, _size, QImage::Format_Indexed8);
1316 QVector<QRgb> colors(256);
1317 for (int i=0; i<256; ++i)
1318 colors[i] = qRgba(0, 0, 0, i);
1319 image.setColorTable(colors);
1322 // can't use qpainter for index8; so use setPixel to draw our rectangle.
1323 for (int i=2; i <= _size-3; ++i) {
1324 image.setPixel(i, 2, 255);
1325 image.setPixel(i, _size-3, 255);
1326 image.setPixel(2, i, 255);
1327 image.setPixel(_size-3, i, 255);
1332 // ------------------------------------------------------------------
1334 // ------------------------------------------------------------------
1336 static inline uchar highByte(glyph_t glyph)
1337 { return glyph >> 24; }
1339 // strip high byte from glyph
1340 static inline glyph_t stripped(glyph_t glyph)
1341 { return glyph & 0x00ffffff; }
1343 QFontEngineMulti::QFontEngineMulti(int engineCount)
1345 engines.fill(0, engineCount);
1349 QFontEngineMulti::~QFontEngineMulti()
1351 for (int i = 0; i < engines.size(); ++i) {
1352 QFontEngine *fontEngine = engines.at(i);
1354 fontEngine->ref.deref();
1355 if (fontEngine->cache_count == 0 && fontEngine->ref.load() == 0)
1361 bool QFontEngineMulti::stringToCMap(const QChar *str, int len,
1362 QGlyphLayout *glyphs, int *nglyphs,
1363 QTextEngine::ShaperFlags flags) const
1366 if (!engine(0)->stringToCMap(str, len, glyphs, &ng, flags))
1369 const_cast<QFontEngineMulti *>(this)->ensureFallbackFamiliesQueried();
1371 for (int i = 0; i < len; ++i) {
1372 bool surrogate = (str[i].isHighSurrogate() && i < len-1 && str[i+1].isLowSurrogate());
1373 uint ucs4 = surrogate ? QChar::surrogateToUcs4(str[i], str[i+1]) : str[i].unicode();
1374 if (glyphs->glyphs[glyph_pos] == 0 && str[i].category() != QChar::Separator_Line) {
1375 QGlyphLayoutInstance tmp = glyphs->instance(glyph_pos);
1376 for (int x=1; x < engines.size(); ++x) {
1377 if (engines.at(x) == 0 && !shouldLoadFontEngineForCharacter(x, ucs4))
1380 QFontEngine *engine = engines.at(x);
1382 const_cast<QFontEngineMulti *>(this)->loadEngine(x);
1383 engine = engines.at(x);
1385 Q_ASSERT(engine != 0);
1386 if (engine->type() == Box)
1389 glyphs->advances_x[glyph_pos] = glyphs->advances_y[glyph_pos] = 0;
1390 glyphs->offsets[glyph_pos] = QFixedPoint();
1392 QGlyphLayout offs = glyphs->mid(glyph_pos, num);
1393 engine->stringToCMap(str + i, surrogate ? 2 : 1, &offs, &num, flags);
1394 Q_ASSERT(num == 1); // surrogates only give 1 glyph
1395 if (glyphs->glyphs[glyph_pos]) {
1396 // set the high byte to indicate which engine the glyph came from
1397 glyphs->glyphs[glyph_pos] |= (x << 24);
1402 // ensure we use metrics from the 1st font when we use the fallback image.
1403 if (!glyphs->glyphs[glyph_pos]) {
1404 glyphs->setInstance(glyph_pos, tmp);
1414 glyphs->numGlyphs = ng;
1418 bool QFontEngineMulti::shouldLoadFontEngineForCharacter(int at, uint ucs4) const
1425 glyph_metrics_t QFontEngineMulti::boundingBox(const QGlyphLayout &glyphs)
1427 if (glyphs.numGlyphs <= 0)
1428 return glyph_metrics_t();
1430 glyph_metrics_t overall;
1432 int which = highByte(glyphs.glyphs[0]);
1435 for (end = 0; end < glyphs.numGlyphs; ++end) {
1436 const int e = highByte(glyphs.glyphs[end]);
1440 // set the high byte to zero
1441 for (i = start; i < end; ++i)
1442 glyphs.glyphs[i] = stripped(glyphs.glyphs[i]);
1444 // merge the bounding box for this run
1445 const glyph_metrics_t gm = engine(which)->boundingBox(glyphs.mid(start, end - start));
1447 overall.x = qMin(overall.x, gm.x);
1448 overall.y = qMin(overall.y, gm.y);
1449 overall.width = overall.xoff + gm.width;
1450 overall.height = qMax(overall.height + overall.y, gm.height + gm.y) -
1451 qMin(overall.y, gm.y);
1452 overall.xoff += gm.xoff;
1453 overall.yoff += gm.yoff;
1455 // reset the high byte for all glyphs
1456 const int hi = which << 24;
1457 for (i = start; i < end; ++i)
1458 glyphs.glyphs[i] = hi | glyphs.glyphs[i];
1465 // set the high byte to zero
1466 for (i = start; i < end; ++i)
1467 glyphs.glyphs[i] = stripped(glyphs.glyphs[i]);
1469 // merge the bounding box for this run
1470 const glyph_metrics_t gm = engine(which)->boundingBox(glyphs.mid(start, end - start));
1472 overall.x = qMin(overall.x, gm.x);
1473 overall.y = qMin(overall.y, gm.y);
1474 overall.width = overall.xoff + gm.width;
1475 overall.height = qMax(overall.height + overall.y, gm.height + gm.y) -
1476 qMin(overall.y, gm.y);
1477 overall.xoff += gm.xoff;
1478 overall.yoff += gm.yoff;
1480 // reset the high byte for all glyphs
1481 const int hi = which << 24;
1482 for (i = start; i < end; ++i)
1483 glyphs.glyphs[i] = hi | glyphs.glyphs[i];
1488 void QFontEngineMulti::getGlyphBearings(glyph_t glyph, qreal *leftBearing, qreal *rightBearing)
1490 int which = highByte(glyph);
1491 engine(which)->getGlyphBearings(stripped(glyph), leftBearing, rightBearing);
1494 void QFontEngineMulti::addOutlineToPath(qreal x, qreal y, const QGlyphLayout &glyphs,
1495 QPainterPath *path, QTextItem::RenderFlags flags)
1497 if (glyphs.numGlyphs <= 0)
1500 int which = highByte(glyphs.glyphs[0]);
1503 if (flags & QTextItem::RightToLeft) {
1504 for (int gl = 0; gl < glyphs.numGlyphs; gl++) {
1505 x += glyphs.advances_x[gl].toReal();
1506 y += glyphs.advances_y[gl].toReal();
1509 for (end = 0; end < glyphs.numGlyphs; ++end) {
1510 const int e = highByte(glyphs.glyphs[end]);
1514 if (flags & QTextItem::RightToLeft) {
1515 for (i = start; i < end; ++i) {
1516 x -= glyphs.advances_x[i].toReal();
1517 y -= glyphs.advances_y[i].toReal();
1521 // set the high byte to zero
1522 for (i = start; i < end; ++i)
1523 glyphs.glyphs[i] = stripped(glyphs.glyphs[i]);
1524 engine(which)->addOutlineToPath(x, y, glyphs.mid(start, end - start), path, flags);
1525 // reset the high byte for all glyphs and update x and y
1526 const int hi = which << 24;
1527 for (i = start; i < end; ++i)
1528 glyphs.glyphs[i] = hi | glyphs.glyphs[i];
1530 if (!(flags & QTextItem::RightToLeft)) {
1531 for (i = start; i < end; ++i) {
1532 x += glyphs.advances_x[i].toReal();
1533 y += glyphs.advances_y[i].toReal();
1542 if (flags & QTextItem::RightToLeft) {
1543 for (i = start; i < end; ++i) {
1544 x -= glyphs.advances_x[i].toReal();
1545 y -= glyphs.advances_y[i].toReal();
1549 // set the high byte to zero
1550 for (i = start; i < end; ++i)
1551 glyphs.glyphs[i] = stripped(glyphs.glyphs[i]);
1553 engine(which)->addOutlineToPath(x, y, glyphs.mid(start, end - start), path, flags);
1555 // reset the high byte for all glyphs
1556 const int hi = which << 24;
1557 for (i = start; i < end; ++i)
1558 glyphs.glyphs[i] = hi | glyphs.glyphs[i];
1561 void QFontEngineMulti::recalcAdvances(QGlyphLayout *glyphs, QTextEngine::ShaperFlags flags) const
1563 if (glyphs->numGlyphs <= 0)
1566 int which = highByte(glyphs->glyphs[0]);
1569 for (end = 0; end < glyphs->numGlyphs; ++end) {
1570 const int e = highByte(glyphs->glyphs[end]);
1574 // set the high byte to zero
1575 for (i = start; i < end; ++i)
1576 glyphs->glyphs[i] = stripped(glyphs->glyphs[i]);
1578 QGlyphLayout offs = glyphs->mid(start, end - start);
1579 engine(which)->recalcAdvances(&offs, flags);
1581 // reset the high byte for all glyphs and update x and y
1582 const int hi = which << 24;
1583 for (i = start; i < end; ++i)
1584 glyphs->glyphs[i] = hi | glyphs->glyphs[i];
1591 // set the high byte to zero
1592 for (i = start; i < end; ++i)
1593 glyphs->glyphs[i] = stripped(glyphs->glyphs[i]);
1595 QGlyphLayout offs = glyphs->mid(start, end - start);
1596 engine(which)->recalcAdvances(&offs, flags);
1598 // reset the high byte for all glyphs
1599 const int hi = which << 24;
1600 for (i = start; i < end; ++i)
1601 glyphs->glyphs[i] = hi | glyphs->glyphs[i];
1604 void QFontEngineMulti::doKerning(QGlyphLayout *glyphs, QTextEngine::ShaperFlags flags) const
1606 if (glyphs->numGlyphs <= 0)
1609 int which = highByte(glyphs->glyphs[0]);
1612 for (end = 0; end < glyphs->numGlyphs; ++end) {
1613 const int e = highByte(glyphs->glyphs[end]);
1617 // set the high byte to zero
1618 for (i = start; i < end; ++i)
1619 glyphs->glyphs[i] = stripped(glyphs->glyphs[i]);
1621 QGlyphLayout offs = glyphs->mid(start, end - start);
1622 engine(which)->doKerning(&offs, flags);
1624 // reset the high byte for all glyphs and update x and y
1625 const int hi = which << 24;
1626 for (i = start; i < end; ++i)
1627 glyphs->glyphs[i] = hi | glyphs->glyphs[i];
1634 // set the high byte to zero
1635 for (i = start; i < end; ++i)
1636 glyphs->glyphs[i] = stripped(glyphs->glyphs[i]);
1638 QGlyphLayout offs = glyphs->mid(start, end - start);
1639 engine(which)->doKerning(&offs, flags);
1641 // reset the high byte for all glyphs
1642 const int hi = which << 24;
1643 for (i = start; i < end; ++i)
1644 glyphs->glyphs[i] = hi | glyphs->glyphs[i];
1647 glyph_metrics_t QFontEngineMulti::boundingBox(glyph_t glyph)
1649 const int which = highByte(glyph);
1650 Q_ASSERT(which < engines.size());
1651 return engine(which)->boundingBox(stripped(glyph));
1654 QFixed QFontEngineMulti::ascent() const
1655 { return engine(0)->ascent(); }
1657 QFixed QFontEngineMulti::descent() const
1658 { return engine(0)->descent(); }
1660 QFixed QFontEngineMulti::leading() const
1662 return engine(0)->leading();
1665 QFixed QFontEngineMulti::xHeight() const
1667 return engine(0)->xHeight();
1670 QFixed QFontEngineMulti::averageCharWidth() const
1672 return engine(0)->averageCharWidth();
1675 QFixed QFontEngineMulti::lineThickness() const
1677 return engine(0)->lineThickness();
1680 QFixed QFontEngineMulti::underlinePosition() const
1682 return engine(0)->underlinePosition();
1685 qreal QFontEngineMulti::maxCharWidth() const
1687 return engine(0)->maxCharWidth();
1690 qreal QFontEngineMulti::minLeftBearing() const
1692 return engine(0)->minLeftBearing();
1695 qreal QFontEngineMulti::minRightBearing() const
1697 return engine(0)->minRightBearing();
1700 bool QFontEngineMulti::canRender(const QChar *string, int len)
1702 if (engine(0)->canRender(string, len))
1705 QVarLengthGlyphLayoutArray glyphs(len);
1707 if (stringToCMap(string, len, &glyphs, &nglyphs, QTextEngine::GlyphIndicesOnly) == false) {
1708 glyphs.resize(nglyphs);
1709 stringToCMap(string, len, &glyphs, &nglyphs, QTextEngine::GlyphIndicesOnly);
1712 bool allExist = true;
1713 for (int i = 0; i < nglyphs; i++) {
1714 if (!glyphs.glyphs[i]) {
1723 /* Implement alphaMapForGlyph() which is called by Lighthouse/Windows code.
1724 * Ideally, that code should be fixed to correctly handle QFontEngineMulti. */
1726 QImage QFontEngineMulti::alphaMapForGlyph(glyph_t glyph)
1728 const int which = highByte(glyph);
1729 Q_ASSERT(which < engines.size());
1730 return engine(which)->alphaMapForGlyph(stripped(glyph));
1733 QImage QFontEngineMulti::alphaMapForGlyph(glyph_t glyph, QFixed subPixelPosition)
1735 const int which = highByte(glyph);
1736 Q_ASSERT(which < engines.size());
1737 return engine(which)->alphaMapForGlyph(stripped(glyph), subPixelPosition);
1740 QImage QFontEngineMulti::alphaMapForGlyph(glyph_t glyph, const QTransform &t)
1742 const int which = highByte(glyph);
1743 Q_ASSERT(which < engines.size());
1744 return engine(which)->alphaMapForGlyph(stripped(glyph), t);
1747 QImage QFontEngineMulti::alphaMapForGlyph(glyph_t glyph, QFixed subPixelPosition, const QTransform &t)
1749 const int which = highByte(glyph);
1750 Q_ASSERT(which < engines.size());
1751 return engine(which)->alphaMapForGlyph(stripped(glyph), subPixelPosition, t);
1754 QImage QFontEngineMulti::alphaRGBMapForGlyph(glyph_t glyph, QFixed subPixelPosition, const QTransform &t)
1756 const int which = highByte(glyph);
1757 Q_ASSERT(which < engines.size());
1758 return engine(which)->alphaRGBMapForGlyph(stripped(glyph), subPixelPosition, t);