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 ****************************************************************************/
42 #include "qfontengine_coretext_p.h"
44 #include <QtCore/qendian.h>
45 #include <QtCore/qsettings.h>
47 #include <private/qimage_p.h>
49 #if !defined(Q_WS_MAC) || (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
53 static float SYNTHETIC_ITALIC_SKEW = tanf(14 * acosf(0) / 90);
55 static void loadAdvancesForGlyphs(CTFontRef ctfont,
56 QVarLengthArray<CGGlyph> &cgGlyphs,
57 QGlyphLayout *glyphs, int len,
58 QTextEngine::ShaperFlags flags,
59 const QFontDef &fontDef)
62 QVarLengthArray<CGSize> advances(len);
63 CTFontGetAdvancesForGlyphs(ctfont, kCTFontHorizontalOrientation, cgGlyphs.data(), advances.data(), len);
65 for (int i = 0; i < len; ++i) {
66 if (glyphs->glyphs[i] & 0xff000000)
68 glyphs->advances_x[i] = QFixed::fromReal(advances[i].width);
69 glyphs->advances_y[i] = QFixed::fromReal(advances[i].height);
72 if (fontDef.styleStrategy & QFont::ForceIntegerMetrics) {
73 for (int i = 0; i < len; ++i) {
74 glyphs->advances_x[i] = glyphs->advances_x[i].round();
75 glyphs->advances_y[i] = glyphs->advances_y[i].round();
80 QCoreTextFontEngineMulti::QCoreTextFontEngineMulti(const QCFString &name, const QFontDef &fontDef, bool kerning)
83 this->fontDef = fontDef;
84 CTFontSymbolicTraits symbolicTraits = 0;
85 if (fontDef.weight >= QFont::Bold)
86 symbolicTraits |= kCTFontBoldTrait;
87 switch (fontDef.style) {
88 case QFont::StyleNormal:
90 case QFont::StyleItalic:
91 case QFont::StyleOblique:
92 symbolicTraits |= kCTFontItalicTrait;
96 transform = CGAffineTransformIdentity;
97 if (fontDef.stretch != 100) {
98 transform = CGAffineTransformMakeScale(float(fontDef.stretch) / float(100), 1);
101 QCFType<CTFontDescriptorRef> descriptor = CTFontDescriptorCreateWithNameAndSize(name, fontDef.pixelSize);
102 QCFType<CTFontRef> baseFont = CTFontCreateWithFontDescriptor(descriptor, fontDef.pixelSize, &transform);
104 // There is a side effect in Core Text: if we apply 0 as symbolic traits to a font in normal weight,
105 // we will get the light version of that font (while the way supposed to work doesn't:
106 // setting kCTFontWeightTrait to some value between -1.0 to 0.0 has no effect on font selection)
107 if (fontDef.weight != QFont::Normal || symbolicTraits)
108 ctfont = CTFontCreateCopyWithSymbolicTraits(baseFont, fontDef.pixelSize, &transform, symbolicTraits, symbolicTraits);
110 // CTFontCreateCopyWithSymbolicTraits returns NULL if we ask for a trait that does
111 // not exist for the given font. (for example italic)
119 QCoreTextFontEngineMulti::QCoreTextFontEngineMulti(CTFontRef ctFontRef, const QFontDef &fontDef, bool kerning)
120 : QFontEngineMulti(0)
122 this->fontDef = fontDef;
123 ctfont = (CTFontRef) CFRetain(ctFontRef);
127 QCoreTextFontEngineMulti::~QCoreTextFontEngineMulti()
132 void QCoreTextFontEngineMulti::init(bool kerning)
134 Q_ASSERT(ctfont != NULL);
135 attributeDict = CFDictionaryCreateMutable(0, 2,
136 &kCFTypeDictionaryKeyCallBacks,
137 &kCFTypeDictionaryValueCallBacks);
138 CFDictionaryAddValue(attributeDict, NSFontAttributeName, ctfont);
141 QCFType<CFNumberRef> noKern = CFNumberCreate(kCFAllocatorDefault, kCFNumberFloatType, &zero);
142 CFDictionaryAddValue(attributeDict, kCTKernAttributeName, noKern);
145 QCoreTextFontEngine *fe = new QCoreTextFontEngine(ctfont, fontDef);
146 fontDef.family = fe->fontDef.family;
147 fontDef.styleName = fe->fontDef.styleName;
148 transform = fe->transform;
153 uint QCoreTextFontEngineMulti::fontIndexForFont(CTFontRef font) const
155 for (int i = 0; i < engines.count(); ++i) {
156 if (CFEqual(engineAt(i)->ctfont, font))
160 QCoreTextFontEngineMulti *that = const_cast<QCoreTextFontEngineMulti *>(this);
161 QCoreTextFontEngine *fe = new QCoreTextFontEngine(font, fontDef);
163 that->engines.append(fe);
164 return engines.count() - 1;
167 bool QCoreTextFontEngineMulti::stringToCMap(const QChar *str, int len, QGlyphLayout *glyphs,
168 int *nglyphs, QTextEngine::ShaperFlags flags,
169 unsigned short *logClusters, const HB_CharAttributes *,
170 QScriptItem *si) const
172 QCFType<CFStringRef> cfstring = CFStringCreateWithCharactersNoCopy(0,
173 reinterpret_cast<const UniChar *>(str),
174 len, kCFAllocatorNull);
175 QCFType<CFAttributedStringRef> attributedString = CFAttributedStringCreate(0, cfstring, attributeDict);
176 QCFType<CTTypesetterRef> typeSetter;
178 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
179 if (flags & QTextEngine::RightToLeft) {
180 const void *optionKeys[] = { kCTTypesetterOptionForcedEmbeddingLevel };
181 const short rtlForcedEmbeddingLevelValue = 1;
182 const void *rtlOptionValues[] = { CFNumberCreate(kCFAllocatorDefault, kCFNumberShortType, &rtlForcedEmbeddingLevelValue) };
183 QCFType<CFDictionaryRef> options = CFDictionaryCreate(kCFAllocatorDefault, optionKeys, rtlOptionValues, 1,
184 &kCFCopyStringDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
185 typeSetter = CTTypesetterCreateWithAttributedStringAndOptions(attributedString, options);
190 typeSetter = CTTypesetterCreateWithAttributedString(attributedString);
192 CFRange range = {0, 0};
193 QCFType<CTLineRef> line = CTTypesetterCreateLine(typeSetter, range);
194 CFArrayRef array = CTLineGetGlyphRuns(line);
195 uint arraySize = CFArrayGetCount(array);
196 glyph_t *outGlyphs = glyphs->glyphs;
197 HB_GlyphAttributes *outAttributes = glyphs->attributes;
198 QFixed *outAdvances_x = glyphs->advances_x;
199 QFixed *outAdvances_y = glyphs->advances_y;
200 glyph_t *initialGlyph = outGlyphs;
202 if (arraySize == 0) {
203 // CoreText failed to shape the text we gave it, so we assume one glyph
204 // per character and build a list of invalid glyphs with zero advance
206 for (int i = 0; i < len; ++i) {
210 outAdvances_x[i] = QFixed();
211 outAdvances_y[i] = QFixed();
212 outAttributes[i].clusterStart = true;
217 const bool rtl = (CTRunGetStatus(static_cast<CTRunRef>(CFArrayGetValueAtIndex(array, 0))) & kCTRunStatusRightToLeft);
219 bool outOBounds = false;
220 for (uint i = 0; i < arraySize; ++i) {
221 CTRunRef run = static_cast<CTRunRef>(CFArrayGetValueAtIndex(array, rtl ? (arraySize - 1 - i) : i));
222 CFIndex glyphCount = CTRunGetGlyphCount(run);
226 Q_ASSERT((CTRunGetStatus(run) & kCTRunStatusRightToLeft) == rtl);
227 CFRange stringRange = CTRunGetStringRange(run);
229 #if MAC_OS_X_VERSION_MAX_ALLOWED == MAC_OS_X_VERSION_10_5
230 UniChar beginGlyph = CFStringGetCharacterAtIndex(cfstring, stringRange.location);
231 QChar dir = QChar::direction(beginGlyph);
232 bool beginWithOverride = dir == QChar::DirLRO || dir == QChar::DirRLO || dir == QChar::DirLRE || dir == QChar::DirRLE;
233 if (beginWithOverride) {
234 logClusters[stringRange.location] = 0;
235 outGlyphs[0] = 0xFFFF;
236 outAdvances_x[0] = 0;
237 outAdvances_y[0] = 0;
238 outAttributes[0].clusterStart = true;
239 outAttributes[0].dontPrint = true;
247 UniChar endGlyph = CFStringGetCharacterAtIndex(cfstring, stringRange.location + stringRange.length - 1);
248 bool endWithPDF = QChar::direction(endGlyph) == QChar::DirPDF;
252 if (!outOBounds && outGlyphs + glyphCount - initialGlyph > *nglyphs) {
256 CFDictionaryRef runAttribs = CTRunGetAttributes(run);
257 //NSLog(@"Dictionary %@", runAttribs);
259 runAttribs = attributeDict;
260 CTFontRef runFont = static_cast<CTFontRef>(CFDictionaryGetValue(runAttribs, NSFontAttributeName));
261 uint fontIndex = fontIndexForFont(runFont);
262 const QFontEngine *engine = engineAt(fontIndex);
264 si->ascent = qMax(engine->ascent(), si->ascent);
265 si->descent = qMax(engine->descent(), si->descent);
266 si->leading = qMax(engine->leading(), si->leading);
267 //NSLog(@"Run Font Name = %@", CTFontCopyFamilyName(runFont));
271 QVarLengthArray<CGGlyph, 512> cgglyphs(0);
272 const CGGlyph *tmpGlyphs = CTRunGetGlyphsPtr(run);
274 cgglyphs.resize(glyphCount);
275 CTRunGetGlyphs(run, range, cgglyphs.data());
276 tmpGlyphs = cgglyphs.constData();
278 QVarLengthArray<CGPoint, 512> cgpoints(0);
279 const CGPoint *tmpPoints = CTRunGetPositionsPtr(run);
281 cgpoints.resize(glyphCount);
282 CTRunGetPositions(run, range, cgpoints.data());
283 tmpPoints = cgpoints.constData();
286 const int rtlOffset = rtl ? (glyphCount - 1) : 0;
287 const int rtlSign = rtl ? -1 : 1;
290 CFRange stringRange = CTRunGetStringRange(run);
291 QVarLengthArray<CFIndex, 512> stringIndices(0);
292 const CFIndex *tmpIndices = CTRunGetStringIndicesPtr(run);
294 stringIndices.resize(glyphCount);
295 CTRunGetStringIndices(run, range, stringIndices.data());
296 tmpIndices = stringIndices.constData();
299 const int firstGlyphIndex = outGlyphs - initialGlyph;
300 outAttributes[0].clusterStart = true;
304 for (i = stringRange.location + prepend;
305 (i < stringRange.location + stringRange.length) && (k < glyphCount); ++i) {
306 if (tmpIndices[k * rtlSign + rtlOffset] == i || i == stringRange.location + prepend) {
307 logClusters[i] = k + firstGlyphIndex;
308 outAttributes[k].clusterStart = true;
311 logClusters[i] = k + firstGlyphIndex - 1;
314 // in case of a ligature at the end, fill the remaining logcluster entries
315 for (;i < stringRange.location + stringRange.length; i++) {
316 logClusters[i] = k + firstGlyphIndex - 1;
319 for (CFIndex i = 0; i < glyphCount - 1; ++i) {
320 int idx = rtlOffset + rtlSign * i;
321 outGlyphs[idx] = tmpGlyphs[i] | fontIndex;
322 outAdvances_x[idx] = QFixed::fromReal(tmpPoints[i + 1].x - tmpPoints[i].x);
323 // Use negative y advance for flipped coordinate system
324 outAdvances_y[idx] = QFixed::fromReal(tmpPoints[i].y - tmpPoints[i + 1].y);
326 if (fontDef.styleStrategy & QFont::ForceIntegerMetrics) {
327 outAdvances_x[idx] = outAdvances_x[idx].round();
328 outAdvances_y[idx] = outAdvances_y[idx].round();
331 CGSize lastGlyphAdvance;
332 CTFontGetAdvancesForGlyphs(runFont, kCTFontHorizontalOrientation, tmpGlyphs + glyphCount - 1, &lastGlyphAdvance, 1);
334 outGlyphs[rtl ? 0 : (glyphCount - 1)] = tmpGlyphs[glyphCount - 1] | fontIndex;
335 outAdvances_x[rtl ? 0 : (glyphCount - 1)] =
336 (fontDef.styleStrategy & QFont::ForceIntegerMetrics)
337 ? QFixed::fromReal(lastGlyphAdvance.width).round()
338 : QFixed::fromReal(lastGlyphAdvance.width);
341 logClusters[stringRange.location + stringRange.length - 1] = glyphCount + prepend;
342 outGlyphs[glyphCount] = 0xFFFF;
343 outAdvances_x[glyphCount] = 0;
344 outAdvances_y[glyphCount] = 0;
345 outAttributes[glyphCount].clusterStart = true;
346 outAttributes[glyphCount].dontPrint = true;
350 outGlyphs += glyphCount;
351 outAttributes += glyphCount;
352 outAdvances_x += glyphCount;
353 outAdvances_y += glyphCount;
355 *nglyphs = (outGlyphs - initialGlyph);
359 bool QCoreTextFontEngineMulti::stringToCMap(const QChar *str, int len, QGlyphLayout *glyphs,
360 int *nglyphs, QTextEngine::ShaperFlags flags) const
363 QCFType<CFStringRef> cfstring;
365 QVarLengthArray<CGGlyph> cgGlyphs(len);
366 CTFontGetGlyphsForCharacters(ctfont, (const UniChar*)str, cgGlyphs.data(), len);
368 for (int i = 0; i < len; ++i) {
370 glyphs->glyphs[i] = cgGlyphs[i];
373 cfstring = CFStringCreateWithCharactersNoCopy(0, reinterpret_cast<const UniChar *>(str), len, kCFAllocatorNull);
374 QCFType<CTFontRef> substituteFont = CTFontCreateForString(ctfont, cfstring, CFRangeMake(i, 1));
375 CGGlyph substituteGlyph = 0;
376 CTFontGetGlyphsForCharacters(substituteFont, (const UniChar*)str + i, &substituteGlyph, 1);
377 if (substituteGlyph) {
378 const uint fontIndex = (fontIndexForFont(substituteFont) << 24);
379 glyphs->glyphs[i] = substituteGlyph | fontIndex;
380 if (!(flags & QTextEngine::GlyphIndicesOnly)) {
382 CTFontGetAdvancesForGlyphs(substituteFont, kCTFontHorizontalOrientation, &substituteGlyph, &advance, 1);
383 glyphs->advances_x[i] = QFixed::fromReal(advance.width);
384 glyphs->advances_y[i] = QFixed::fromReal(advance.height);
390 if (flags & QTextEngine::GlyphIndicesOnly)
393 loadAdvancesForGlyphs(ctfont, cgGlyphs, glyphs, len, flags, fontDef);
397 void QCoreTextFontEngineMulti::loadEngine(int)
403 extern int qt_antialiasing_threshold; // from qapplication.cpp
405 CGAffineTransform qt_transform_from_fontdef(const QFontDef &fontDef)
407 CGAffineTransform transform = CGAffineTransformIdentity;
408 if (fontDef.stretch != 100)
409 transform = CGAffineTransformMakeScale(float(fontDef.stretch) / float(100), 1);
413 QCoreTextFontEngine::QCoreTextFontEngine(CTFontRef font, const QFontDef &def)
416 transform = qt_transform_from_fontdef(fontDef);
419 cgFont = CTFontCopyGraphicsFont(font, NULL);
423 QCoreTextFontEngine::QCoreTextFontEngine(CGFontRef font, const QFontDef &def)
426 transform = qt_transform_from_fontdef(fontDef);
428 // Keep reference count balanced
430 ctfont = CTFontCreateWithGraphicsFont(font, fontDef.pixelSize, &transform, NULL);
434 QCoreTextFontEngine::~QCoreTextFontEngine()
440 extern QFont::Weight weightFromInteger(int weight); // qfontdatabase.cpp
442 int getTraitValue(CFDictionaryRef allTraits, CFStringRef trait)
444 if (CFDictionaryContainsKey(allTraits, trait)) {
445 CFNumberRef traitNum = (CFNumberRef) CFDictionaryGetValue(allTraits, trait);
447 CFNumberGetValue(traitNum, kCFNumberFloatType, &v);
448 // the value we get from CFNumberRef is from -1.0 to 1.0
449 int value = v * 500 + 500;
456 void QCoreTextFontEngine::init()
458 Q_ASSERT(ctfont != NULL);
459 Q_ASSERT(cgFont != NULL);
461 QCFString family = CTFontCopyFamilyName(ctfont);
462 fontDef.family = family;
464 QCFString styleName = (CFStringRef) CTFontCopyAttribute(ctfont, kCTFontStyleNameAttribute);
465 fontDef.styleName = styleName;
468 CTFontSymbolicTraits traits = CTFontGetSymbolicTraits(ctfont);
469 if (traits & kCTFontItalicTrait)
470 fontDef.style = QFont::StyleItalic;
472 CFDictionaryRef allTraits = CTFontCopyTraits(ctfont);
473 fontDef.weight = weightFromInteger(getTraitValue(allTraits, kCTFontWeightTrait));
474 int slant = getTraitValue(allTraits, kCTFontSlantTrait);
475 if (slant > 500 && !(traits & kCTFontItalicTrait))
476 fontDef.style = QFont::StyleOblique;
477 CFRelease(allTraits);
479 if (fontDef.weight >= QFont::Bold && !(traits & kCTFontBoldTrait))
480 synthesisFlags |= SynthesizedBold;
481 // XXX: we probably don't need to synthesis italic for oblique font
482 if (fontDef.style != QFont::StyleNormal && !(traits & kCTFontItalicTrait))
483 synthesisFlags |= SynthesizedItalic;
486 QByteArray os2Table = getSfntTable(MAKE_TAG('O', 'S', '/', '2'));
487 unsigned emSize = CTFontGetUnitsPerEm(ctfont);
488 if (os2Table.size() >= 10) {
489 fsType = qFromBigEndian<quint16>(reinterpret_cast<const uchar *>(os2Table.constData() + 8));
490 // qAbs is a workaround for weird fonts like Lucida Grande
491 qint16 width = qAbs(qFromBigEndian<qint16>(reinterpret_cast<const uchar *>(os2Table.constData() + 2)));
492 avgCharWidth = QFixed::fromReal(width * fontDef.pixelSize / emSize);
494 avgCharWidth = QFontEngine::averageCharWidth();
497 bool QCoreTextFontEngine::stringToCMap(const QChar *str, int len, QGlyphLayout *glyphs,
498 int *nglyphs, QTextEngine::ShaperFlags flags) const
501 QCFType<CFStringRef> cfstring;
503 QVarLengthArray<CGGlyph> cgGlyphs(len);
504 CTFontGetGlyphsForCharacters(ctfont, (const UniChar*)str, cgGlyphs.data(), len);
506 for (int i = 0; i < len; ++i)
508 glyphs->glyphs[i] = cgGlyphs[i];
510 if (flags & QTextEngine::GlyphIndicesOnly)
513 loadAdvancesForGlyphs(ctfont, cgGlyphs, glyphs, len, flags, fontDef);
517 glyph_metrics_t QCoreTextFontEngine::boundingBox(const QGlyphLayout &glyphs)
520 bool round = fontDef.styleStrategy & QFont::ForceIntegerMetrics;
522 for (int i = 0; i < glyphs.numGlyphs; ++i) {
523 w += round ? glyphs.effectiveAdvance(i).round()
524 : glyphs.effectiveAdvance(i);
526 return glyph_metrics_t(0, -(ascent()), w - lastRightBearing(glyphs, round), ascent()+descent(), w, 0);
529 glyph_metrics_t QCoreTextFontEngine::boundingBox(glyph_t glyph)
533 CGRect rect = CTFontGetBoundingRectsForGlyphs(ctfont, kCTFontHorizontalOrientation, &g, 0, 1);
534 if (synthesisFlags & QFontEngine::SynthesizedItalic) {
535 rect.size.width += rect.size.height * SYNTHETIC_ITALIC_SKEW;
537 ret.width = QFixed::fromReal(rect.size.width);
538 ret.height = QFixed::fromReal(rect.size.height);
539 ret.x = QFixed::fromReal(rect.origin.x);
540 ret.y = -QFixed::fromReal(rect.origin.y) - ret.height;
542 CTFontGetAdvancesForGlyphs(ctfont, kCTFontHorizontalOrientation, &g, advances, 1);
543 ret.xoff = QFixed::fromReal(advances[0].width);
544 ret.yoff = QFixed::fromReal(advances[0].height);
546 if (fontDef.styleStrategy & QFont::ForceIntegerMetrics) {
547 ret.xoff = ret.xoff.round();
548 ret.yoff = ret.yoff.round();
554 QFixed QCoreTextFontEngine::ascent() const
556 return (fontDef.styleStrategy & QFont::ForceIntegerMetrics)
557 ? QFixed::fromReal(CTFontGetAscent(ctfont)).round()
558 : QFixed::fromReal(CTFontGetAscent(ctfont));
560 QFixed QCoreTextFontEngine::descent() const
562 QFixed d = QFixed::fromReal(CTFontGetDescent(ctfont));
563 if (fontDef.styleStrategy & QFont::ForceIntegerMetrics)
566 // subtract a pixel to even out the historical +1 in QFontMetrics::height().
570 QFixed QCoreTextFontEngine::leading() const
572 return (fontDef.styleStrategy & QFont::ForceIntegerMetrics)
573 ? QFixed::fromReal(CTFontGetLeading(ctfont)).round()
574 : QFixed::fromReal(CTFontGetLeading(ctfont));
576 QFixed QCoreTextFontEngine::xHeight() const
578 return (fontDef.styleStrategy & QFont::ForceIntegerMetrics)
579 ? QFixed::fromReal(CTFontGetXHeight(ctfont)).round()
580 : QFixed::fromReal(CTFontGetXHeight(ctfont));
583 QFixed QCoreTextFontEngine::averageCharWidth() const
585 return (fontDef.styleStrategy & QFont::ForceIntegerMetrics)
586 ? avgCharWidth.round() : avgCharWidth;
589 qreal QCoreTextFontEngine::maxCharWidth() const
594 qreal QCoreTextFontEngine::minLeftBearing() const
599 qreal QCoreTextFontEngine::minRightBearing() const
604 void QCoreTextFontEngine::draw(CGContextRef ctx, qreal x, qreal y, const QTextItemInt &ti, int paintDeviceHeight)
606 QVarLengthArray<QFixedPoint> positions;
607 QVarLengthArray<glyph_t> glyphs;
609 matrix.translate(x, y);
610 getGlyphPositions(ti.glyphs, matrix, ti.flags, glyphs, positions);
611 if (glyphs.size() == 0)
614 CGContextSetFontSize(ctx, fontDef.pixelSize);
616 CGAffineTransform oldTextMatrix = CGContextGetTextMatrix(ctx);
618 CGAffineTransform cgMatrix = CGAffineTransformMake(1, 0, 0, -1, 0, -paintDeviceHeight);
620 CGAffineTransformConcat(cgMatrix, oldTextMatrix);
622 if (synthesisFlags & QFontEngine::SynthesizedItalic)
623 cgMatrix = CGAffineTransformConcat(cgMatrix, CGAffineTransformMake(1, 0, -SYNTHETIC_ITALIC_SKEW, 1, 0, 0));
625 cgMatrix = CGAffineTransformConcat(cgMatrix, transform);
627 CGContextSetTextMatrix(ctx, cgMatrix);
629 CGContextSetTextDrawingMode(ctx, kCGTextFill);
632 QVarLengthArray<CGSize> advances(glyphs.size());
633 QVarLengthArray<CGGlyph> cgGlyphs(glyphs.size());
635 for (int i = 0; i < glyphs.size() - 1; ++i) {
636 advances[i].width = (positions[i + 1].x - positions[i].x).toReal();
637 advances[i].height = (positions[i + 1].y - positions[i].y).toReal();
638 cgGlyphs[i] = glyphs[i];
640 advances[glyphs.size() - 1].width = 0;
641 advances[glyphs.size() - 1].height = 0;
642 cgGlyphs[glyphs.size() - 1] = glyphs[glyphs.size() - 1];
644 CGContextSetFont(ctx, cgFont);
645 //NSLog(@"Font inDraw %@ ctfont %@", CGFontCopyFullName(cgFont), CTFontCopyFamilyName(ctfont));
647 CGContextSetTextPosition(ctx, positions[0].x.toReal(), positions[0].y.toReal());
649 CGContextShowGlyphsWithAdvances(ctx, cgGlyphs.data(), advances.data(), glyphs.size());
651 if (synthesisFlags & QFontEngine::SynthesizedBold) {
652 CGContextSetTextPosition(ctx, positions[0].x.toReal() + 0.5 * lineThickness().toReal(),
653 positions[0].y.toReal());
655 CGContextShowGlyphsWithAdvances(ctx, cgGlyphs.data(), advances.data(), glyphs.size());
658 CGContextSetTextMatrix(ctx, oldTextMatrix);
661 struct ConvertPathInfo
663 ConvertPathInfo(QPainterPath *newPath, const QPointF &newPos) : path(newPath), pos(newPos) {}
668 static void convertCGPathToQPainterPath(void *info, const CGPathElement *element)
670 ConvertPathInfo *myInfo = static_cast<ConvertPathInfo *>(info);
671 switch(element->type) {
672 case kCGPathElementMoveToPoint:
673 myInfo->path->moveTo(element->points[0].x + myInfo->pos.x(),
674 element->points[0].y + myInfo->pos.y());
676 case kCGPathElementAddLineToPoint:
677 myInfo->path->lineTo(element->points[0].x + myInfo->pos.x(),
678 element->points[0].y + myInfo->pos.y());
680 case kCGPathElementAddQuadCurveToPoint:
681 myInfo->path->quadTo(element->points[0].x + myInfo->pos.x(),
682 element->points[0].y + myInfo->pos.y(),
683 element->points[1].x + myInfo->pos.x(),
684 element->points[1].y + myInfo->pos.y());
686 case kCGPathElementAddCurveToPoint:
687 myInfo->path->cubicTo(element->points[0].x + myInfo->pos.x(),
688 element->points[0].y + myInfo->pos.y(),
689 element->points[1].x + myInfo->pos.x(),
690 element->points[1].y + myInfo->pos.y(),
691 element->points[2].x + myInfo->pos.x(),
692 element->points[2].y + myInfo->pos.y());
694 case kCGPathElementCloseSubpath:
695 myInfo->path->closeSubpath();
698 qDebug() << "Unhandled path transform type: " << element->type;
703 void QCoreTextFontEngine::addGlyphsToPath(glyph_t *glyphs, QFixedPoint *positions, int nGlyphs,
704 QPainterPath *path, QTextItem::RenderFlags)
706 CGAffineTransform cgMatrix = CGAffineTransformIdentity;
707 cgMatrix = CGAffineTransformScale(cgMatrix, 1, -1);
709 if (synthesisFlags & QFontEngine::SynthesizedItalic)
710 cgMatrix = CGAffineTransformConcat(cgMatrix, CGAffineTransformMake(1, 0, -SYNTHETIC_ITALIC_SKEW, 1, 0, 0));
712 for (int i = 0; i < nGlyphs; ++i) {
713 QCFType<CGPathRef> cgpath = CTFontCreatePathForGlyph(ctfont, glyphs[i], &cgMatrix);
714 ConvertPathInfo info(path, positions[i].toPointF());
715 CGPathApply(cgpath, &info, convertCGPathToQPainterPath);
719 QImage QCoreTextFontEngine::imageForGlyph(glyph_t glyph, QFixed subPixelPosition, int /*margin*/, bool aa)
721 const glyph_metrics_t br = boundingBox(glyph);
722 QImage im(qRound(br.width)+2, qRound(br.height)+2, QImage::Format_RGB32);
725 CGColorSpaceRef colorspace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
726 uint cgflags = kCGImageAlphaNoneSkipFirst;
727 #ifdef kCGBitmapByteOrder32Host //only needed because CGImage.h added symbols in the minor version
728 cgflags |= kCGBitmapByteOrder32Host;
730 CGContextRef ctx = CGBitmapContextCreate(im.bits(), im.width(), im.height(),
731 8, im.bytesPerLine(), colorspace,
733 CGContextSetFontSize(ctx, fontDef.pixelSize);
734 CGContextSetShouldAntialias(ctx, (aa || fontDef.pointSize > qt_antialiasing_threshold)
735 && !(fontDef.styleStrategy & QFont::NoAntialias));
736 CGContextSetShouldSmoothFonts(ctx, aa);
737 CGAffineTransform oldTextMatrix = CGContextGetTextMatrix(ctx);
738 CGAffineTransform cgMatrix = CGAffineTransformMake(1, 0, 0, 1, 0, 0);
740 CGAffineTransformConcat(cgMatrix, oldTextMatrix);
742 if (synthesisFlags & QFontEngine::SynthesizedItalic)
743 cgMatrix = CGAffineTransformConcat(cgMatrix, CGAffineTransformMake(1, 0, SYNTHETIC_ITALIC_SKEW, 1, 0, 0));
745 cgMatrix = CGAffineTransformConcat(cgMatrix, transform);
747 CGContextSetTextMatrix(ctx, cgMatrix);
748 CGContextSetRGBFillColor(ctx, 1, 1, 1, 1);
749 CGContextSetTextDrawingMode(ctx, kCGTextFill);
751 CGContextSetFont(ctx, cgFont);
753 qreal pos_x = -br.x.toReal() + subPixelPosition.toReal();
754 qreal pos_y = im.height() + br.y.toReal() - 1;
755 CGContextSetTextPosition(ctx, pos_x, pos_y);
760 CGGlyph cgGlyph = glyph;
761 CGContextShowGlyphsWithAdvances(ctx, &cgGlyph, &advance, 1);
763 if (synthesisFlags & QFontEngine::SynthesizedBold) {
764 CGContextSetTextPosition(ctx, pos_x + 0.5 * lineThickness().toReal(), pos_y);
765 CGContextShowGlyphsWithAdvances(ctx, &cgGlyph, &advance, 1);
768 CGContextRelease(ctx);
773 QImage QCoreTextFontEngine::alphaMapForGlyph(glyph_t glyph, QFixed subPixelPosition)
775 QImage im = imageForGlyph(glyph, subPixelPosition, 0, false);
777 QImage indexed(im.width(), im.height(), QImage::Format_Indexed8);
778 QVector<QRgb> colors(256);
779 for (int i=0; i<256; ++i)
780 colors[i] = qRgba(0, 0, 0, i);
781 indexed.setColorTable(colors);
783 for (int y=0; y<im.height(); ++y) {
784 uint *src = (uint*) im.scanLine(y);
785 uchar *dst = indexed.scanLine(y);
786 for (int x=0; x<im.width(); ++x) {
796 QImage QCoreTextFontEngine::alphaRGBMapForGlyph(glyph_t glyph, QFixed subPixelPosition, int margin, const QTransform &x)
798 if (x.type() >= QTransform::TxScale)
799 return QFontEngine::alphaRGBMapForGlyph(glyph, subPixelPosition, margin, x);
801 QImage im = imageForGlyph(glyph, subPixelPosition, margin, true);
802 qGamma_correct_back_to_linear_cs(&im);
806 void QCoreTextFontEngine::recalcAdvances(QGlyphLayout *glyphs, QTextEngine::ShaperFlags flags) const
808 int i, numGlyphs = glyphs->numGlyphs;
809 QVarLengthArray<CGGlyph> cgGlyphs(numGlyphs);
811 for (i = 0; i < numGlyphs; ++i) {
812 if (glyphs->glyphs[i] & 0xff000000)
815 cgGlyphs[i] = glyphs->glyphs[i];
818 loadAdvancesForGlyphs(ctfont, cgGlyphs, glyphs, numGlyphs, flags, fontDef);
821 QFontEngine::FaceId QCoreTextFontEngine::faceId() const
823 return QFontEngine::FaceId();
826 bool QCoreTextFontEngine::canRender(const QChar *string, int len)
828 QVarLengthArray<CGGlyph> cgGlyphs(len);
829 return CTFontGetGlyphsForCharacters(ctfont, (const UniChar *) string, cgGlyphs.data(), len);
832 bool QCoreTextFontEngine::getSfntTableData(uint tag, uchar *buffer, uint *length) const
834 QCFType<CFDataRef> table = CTFontCopyTable(ctfont, tag, 0);
835 if (!table || !length)
837 CFIndex tableLength = CFDataGetLength(table);
838 int availableLength = *length;
839 *length = tableLength;
841 if (tableLength > availableLength)
843 CFDataGetBytes(table, CFRangeMake(0, tableLength), buffer);
848 void QCoreTextFontEngine::getUnscaledGlyph(glyph_t, QPainterPath *, glyph_metrics_t *)
853 QFixed QCoreTextFontEngine::emSquareSize() const
855 return QFixed::QFixed(int(CTFontGetUnitsPerEm(ctfont)));
858 QFontEngine *QCoreTextFontEngine::cloneWithSize(qreal pixelSize) const
860 QFontDef newFontDef = fontDef;
861 newFontDef.pixelSize = pixelSize;
862 newFontDef.pointSize = pixelSize * 72.0 / qt_defaultDpi();
864 return new QCoreTextFontEngine(cgFont, newFontDef);
869 #endif// !defined(Q_WS_MAC) || (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)