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_mac_p.h"
44 #include <private/qapplication_p.h>
45 #include <private/qfontengine_p.h>
46 #include <private/qpainter_p.h>
47 #include <private/qtextengine_p.h>
49 #include <private/qpaintengine_mac_p.h>
50 #include <private/qprintengine_mac_p.h>
53 #include <qpixmapcache.h>
54 #include <qvarlengtharray.h>
58 #include <private/qimage_p.h>
60 #include <ApplicationServices/ApplicationServices.h>
61 #include <AppKit/AppKit.h>
65 /*****************************************************************************
66 QFontEngine debug facilities
67 *****************************************************************************/
68 //#define DEBUG_ADVANCES
70 extern int qt_antialiasing_threshold; // QApplication.cpp
73 #define FixedToQFixed(a) QFixed::fromFixed((a) >> 10)
74 #define QFixedToFixed(x) ((x).value() << 10)
82 inline QMacFontPath(float _x, float _y, QPainterPath *_path) : x(_x), y(_y), path(_path) { }
83 inline void setPosition(float _x, float _y) { x = _x; y = _y; }
84 inline void advance(float _x) { x += _x; }
85 static OSStatus lineTo(const Float32Point *, void *);
86 static OSStatus cubicTo(const Float32Point *, const Float32Point *,
87 const Float32Point *, void *);
88 static OSStatus moveTo(const Float32Point *, void *);
89 static OSStatus closePath(void *);
92 OSStatus QMacFontPath::lineTo(const Float32Point *pt, void *data)
95 QMacFontPath *p = static_cast<QMacFontPath*>(data);
96 p->path->lineTo(p->x + pt->x, p->y + pt->y);
100 OSStatus QMacFontPath::cubicTo(const Float32Point *cp1, const Float32Point *cp2,
101 const Float32Point *ep, void *data)
104 QMacFontPath *p = static_cast<QMacFontPath*>(data);
105 p->path->cubicTo(p->x + cp1->x, p->y + cp1->y,
106 p->x + cp2->x, p->y + cp2->y,
107 p->x + ep->x, p->y + ep->y);
111 OSStatus QMacFontPath::moveTo(const Float32Point *pt, void *data)
113 QMacFontPath *p = static_cast<QMacFontPath*>(data);
114 p->path->moveTo(p->x + pt->x, p->y + pt->y);
118 OSStatus QMacFontPath::closePath(void *data)
120 static_cast<QMacFontPath*>(data)->path->closeSubpath();
125 #ifndef QT_MAC_USE_COCOA
126 QFontEngineMacMulti::QFontEngineMacMulti(const ATSFontFamilyRef &atsFamily, const ATSFontRef &atsFontRef, const QFontDef &fontDef, bool kerning)
127 : QFontEngineMulti(0)
129 this->fontDef = fontDef;
130 this->kerning = kerning;
132 // hopefully (CTFontCreateWithName or CTFontCreateWithFontDescriptor) + CTFontCreateCopyWithSymbolicTraits
133 // (or CTFontCreateWithQuickdrawInstance)
134 FMFontFamily fmFamily;
135 FMFontStyle fntStyle = 0;
136 fmFamily = FMGetFontFamilyFromATSFontFamilyRef(atsFamily);
137 if (fmFamily == kInvalidFontFamily) {
138 // Use the ATSFont then...
139 fontID = FMGetFontFromATSFontRef(atsFontRef);
141 if (fontDef.weight >= QFont::Bold)
143 if (fontDef.style != QFont::StyleNormal)
144 fntStyle |= ::italic;
146 FMFontStyle intrinsicStyle;
148 if (FMGetFontFromFontFamilyInstance(fmFamily, fntStyle, &fnt, &intrinsicStyle) == noErr)
149 fontID = FMGetATSFontRefFromFont(fnt);
152 // CFDictionaryRef, <CTStringAttributes.h>
155 status = ATSUCreateTextLayout(&textLayout);
156 Q_ASSERT(status == noErr);
158 const int maxAttributeCount = 5;
159 ATSUAttributeTag tags[maxAttributeCount + 1];
160 ByteCount sizes[maxAttributeCount + 1];
161 ATSUAttributeValuePtr values[maxAttributeCount + 1];
162 int attributeCount = 0;
164 Fixed size = FixRatio(fontDef.pixelSize, 1);
165 tags[attributeCount] = kATSUSizeTag;
166 sizes[attributeCount] = sizeof(size);
167 values[attributeCount] = &size;
170 tags[attributeCount] = kATSUFontTag;
171 sizes[attributeCount] = sizeof(fontID);
172 values[attributeCount] = &this->fontID;
175 transform = CGAffineTransformIdentity;
176 if (fontDef.stretch != 100) {
177 transform = CGAffineTransformMakeScale(float(fontDef.stretch) / float(100), 1);
178 tags[attributeCount] = kATSUFontMatrixTag;
179 sizes[attributeCount] = sizeof(transform);
180 values[attributeCount] = &transform;
184 status = ATSUCreateStyle(&style);
185 Q_ASSERT(status == noErr);
187 Q_ASSERT(attributeCount < maxAttributeCount + 1);
188 status = ATSUSetAttributes(style, attributeCount, tags, sizes, values);
189 Q_ASSERT(status == noErr);
191 QFontEngineMac *fe = new QFontEngineMac(style, fontID, fontDef, this);
196 QFontEngineMacMulti::~QFontEngineMacMulti()
198 ATSUDisposeTextLayout(textLayout);
199 ATSUDisposeStyle(style);
201 for (int i = 0; i < engines.count(); ++i) {
202 QFontEngineMac *fe = const_cast<QFontEngineMac *>(static_cast<const QFontEngineMac *>(engines.at(i)));
204 if (!fe->ref.deref())
210 struct QGlyphLayoutInfo
212 QGlyphLayout *glyphs;
216 QTextEngine::ShaperFlags flags;
217 QFontEngineMacMulti::ShaperItem *shaperItem;
218 unsigned int styleStrategy;
221 static OSStatus atsuPostLayoutCallback(ATSULayoutOperationSelector selector, ATSULineRef lineRef, URefCon refCon,
222 void *operationExtraParameter, ATSULayoutOperationCallbackStatus *callbackStatus)
225 Q_UNUSED(operationExtraParameter);
227 QGlyphLayoutInfo *nfo = reinterpret_cast<QGlyphLayoutInfo *>(refCon);
228 nfo->callbackCalled = true;
230 ATSLayoutRecord *layoutData = 0;
231 ItemCount itemCount = 0;
234 e = ATSUDirectGetLayoutDataArrayPtrFromLineRef(lineRef, kATSUDirectDataLayoutRecordATSLayoutRecordCurrent,
236 (void **) &layoutData,
241 *nfo->numGlyphs = itemCount - 1;
243 Fixed *baselineDeltas = 0;
245 e = ATSUDirectGetLayoutDataArrayPtrFromLineRef(lineRef, kATSUDirectDataBaselineDeltaFixedArray,
247 (void **) &baselineDeltas,
252 int nextCharStop = -1;
253 int currentClusterGlyph = -1; // first glyph in log cluster
254 QFontEngineMacMulti::ShaperItem *item = nfo->shaperItem;
255 if (item->charAttributes) {
256 item = nfo->shaperItem;
257 #if !defined(QT_NO_DEBUG)
259 const QChar *str = item->string;
260 for (int i = item->from; i < item->from + item->length - 1; ++i) {
261 surrogates += (str[i].unicode() >= 0xd800 && str[i].unicode() < 0xdc00
262 && str[i+1].unicode() >= 0xdc00 && str[i+1].unicode() < 0xe000);
265 for (nextCharStop = item->from; nextCharStop < item->from + item->length; ++nextCharStop)
266 if (item->charAttributes[nextCharStop].charStop)
268 nextCharStop -= item->from;
271 nfo->glyphs->attributes[0].clusterStart = true;
273 int glyphIncrement = 1;
274 if (nfo->flags & QTextEngine::RightToLeft) {
275 glyphIdx = itemCount - 2;
278 for (int i = 0; i < *nfo->numGlyphs; ++i, glyphIdx += glyphIncrement) {
280 int charOffset = layoutData[glyphIdx].originalOffset / sizeof(UniChar);
281 const int fontIdx = nfo->mappedFonts[charOffset];
283 ATSGlyphRef glyphId = layoutData[glyphIdx].glyphID;
285 QFixed yAdvance = FixedToQFixed(baselineDeltas[glyphIdx]);
286 QFixed xAdvance = FixedToQFixed(layoutData[glyphIdx + 1].realPos - layoutData[glyphIdx].realPos);
288 if (nfo->styleStrategy & QFont::ForceIntegerMetrics) {
289 yAdvance = yAdvance.round();
290 xAdvance = xAdvance.round();
293 if (glyphId != 0xffff || i == 0) {
294 if (i < nfo->glyphs->numGlyphs)
296 nfo->glyphs->glyphs[i] = (glyphId & 0x00ffffff) | (fontIdx << 24);
298 nfo->glyphs->advances_y[i] = yAdvance;
299 nfo->glyphs->advances_x[i] = xAdvance;
302 // ATSUI gives us 0xffff as glyph id at the index in the glyph array for
303 // a character position that maps to a ligtature. Such a glyph id does not
304 // result in any visual glyph, but it may have an advance, which is why we
305 // sum up the glyph advances.
307 nfo->glyphs->advances_y[i] += yAdvance;
308 nfo->glyphs->advances_x[i] += xAdvance;
309 *nfo->numGlyphs -= 1;
312 if (item->log_clusters) {
313 if (charOffset >= nextCharStop) {
314 nfo->glyphs->attributes[i].clusterStart = true;
315 currentClusterGlyph = i;
318 for (; nextCharStop < item->length; ++nextCharStop)
319 if (item->charAttributes[item->from + nextCharStop].charStop)
322 if (currentClusterGlyph == -1)
323 currentClusterGlyph = i;
325 item->log_clusters[charOffset] = currentClusterGlyph;
327 // surrogate handling
328 if (charOffset < item->length - 1) {
329 QChar current = item->string[item->from + charOffset];
330 QChar next = item->string[item->from + charOffset + 1];
331 if (current.unicode() >= 0xd800 && current.unicode() < 0xdc00
332 && next.unicode() >= 0xdc00 && next.unicode() < 0xe000) {
333 item->log_clusters[charOffset + 1] = currentClusterGlyph;
341 qDebug() << "resulting logclusters:";
342 for (int i = 0; i < item->length; ++i)
343 qDebug() << "logClusters[" << i << "] =" << item->log_clusters[i];
344 qDebug() << "clusterstarts:";
345 for (int i = 0; i < *nfo->numGlyphs; ++i)
346 qDebug() << "clusterStart[" << i << "] =" << nfo->glyphs[i].attributes.clusterStart;
350 ATSUDirectReleaseLayoutDataArrayPtr(lineRef, kATSUDirectDataBaselineDeltaFixedArray,
351 (void **) &baselineDeltas);
353 ATSUDirectReleaseLayoutDataArrayPtr(lineRef, kATSUDirectDataLayoutRecordATSLayoutRecordCurrent,
354 (void **) &layoutData);
356 *callbackStatus = kATSULayoutOperationCallbackStatusHandled;
360 int QFontEngineMacMulti::fontIndexForFontID(ATSUFontID id) const
362 for (int i = 0; i < engines.count(); ++i) {
363 if (engineAt(i)->fontID == id)
367 QFontEngineMacMulti *that = const_cast<QFontEngineMacMulti *>(this);
368 QFontEngineMac *fe = new QFontEngineMac(style, id, fontDef, that);
370 that->engines.append(fe);
371 return engines.count() - 1;
374 bool QFontEngineMacMulti::stringToCMap(const QChar *str, int len, QGlyphLayout *glyphs, int *nglyphs, QTextEngine::ShaperFlags flags) const
376 return stringToCMap(str, len, glyphs, nglyphs, flags, /*logClusters=*/0, /*charAttributes=*/0);
379 bool QFontEngineMacMulti::stringToCMap(const QChar *str, int len, QGlyphLayout *glyphs, int *nglyphs, QTextEngine::ShaperFlags flags,
380 unsigned short *logClusters, const HB_CharAttributes *charAttributes, QScriptItem *) const
382 if (*nglyphs < len) {
387 ShaperItem shaperItem;
388 shaperItem.string = str;
390 shaperItem.length = len;
391 shaperItem.glyphs = *glyphs;
392 shaperItem.glyphs.numGlyphs = *nglyphs;
393 shaperItem.flags = flags;
394 shaperItem.log_clusters = logClusters;
395 shaperItem.charAttributes = charAttributes;
397 const int maxChars = qMax(1,
398 int(SHRT_MAX / maxCharWidth())
399 - 10 // subtract a few to be on the safe side
401 if (len < maxChars || !charAttributes)
402 return stringToCMapInternal(str, len, glyphs, nglyphs, flags, &shaperItem);
406 ShaperItem tmpItem = shaperItem;
409 tmpItem.from = shaperItem.from + charIdx;
411 int charCount = qMin(maxChars, len - charIdx);
413 int lastWhitespace = tmpItem.from + charCount - 1;
414 int lastSoftBreak = lastWhitespace;
415 int lastCharStop = lastSoftBreak;
416 for (int i = lastCharStop; i >= tmpItem.from; --i) {
417 if (tmpItem.charAttributes[i].whiteSpace) {
420 } if (tmpItem.charAttributes[i].lineBreakType != HB_NoBreak) {
422 } if (tmpItem.charAttributes[i].charStop) {
426 charCount = qMin(lastWhitespace, qMin(lastSoftBreak, lastCharStop)) - tmpItem.from + 1;
428 int glyphCount = shaperItem.glyphs.numGlyphs - glyphIdx;
431 tmpItem.length = charCount;
432 tmpItem.glyphs = shaperItem.glyphs.mid(glyphIdx, glyphCount);
433 tmpItem.log_clusters = shaperItem.log_clusters + charIdx;
434 if (!stringToCMapInternal(tmpItem.string + tmpItem.from, tmpItem.length,
435 &tmpItem.glyphs, &glyphCount, flags,
437 *nglyphs = glyphIdx + glyphCount;
440 for (int i = 0; i < charCount; ++i)
441 tmpItem.log_clusters[i] += glyphIdx;
442 glyphIdx += glyphCount;
443 charIdx += charCount;
444 } while (charIdx < len);
446 glyphs->numGlyphs = glyphIdx;
451 bool QFontEngineMacMulti::stringToCMapInternal(const QChar *str, int len, QGlyphLayout *glyphs, int *nglyphs, QTextEngine::ShaperFlags flags,ShaperItem *shaperItem) const
453 //qDebug() << "stringToCMap" << QString(str, len);
457 e = ATSUSetTextPointerLocation(textLayout, (UniChar *)(str), 0, len, len);
459 qWarning("Qt: internal: %ld: Error ATSUSetTextPointerLocation %s: %d", long(e), __FILE__, __LINE__);
463 QGlyphLayoutInfo nfo;
465 nfo.numGlyphs = nglyphs;
466 nfo.callbackCalled = false;
468 nfo.shaperItem = shaperItem;
469 nfo.styleStrategy = fontDef.styleStrategy;
471 int prevNumGlyphs = *nglyphs;
473 QVarLengthArray<int> mappedFonts(len);
474 for (int i = 0; i < len; ++i)
476 nfo.mappedFonts = mappedFonts.data();
478 Q_ASSERT(sizeof(void *) <= sizeof(URefCon));
479 e = ATSUSetTextLayoutRefCon(textLayout, (URefCon)&nfo);
481 qWarning("Qt: internal: %ld: Error ATSUSetTextLayoutRefCon %s: %d", long(e), __FILE__, __LINE__);
486 const int maxAttributeCount = 3;
487 ATSUAttributeTag tags[maxAttributeCount + 1];
488 ByteCount sizes[maxAttributeCount + 1];
489 ATSUAttributeValuePtr values[maxAttributeCount + 1];
490 int attributeCount = 0;
492 tags[attributeCount] = kATSULineLayoutOptionsTag;
493 ATSLineLayoutOptions layopts = kATSLineHasNoOpticalAlignment
494 | kATSLineIgnoreFontLeading
495 | kATSLineNoSpecialJustification // we do kashidas ourselves
496 | kATSLineDisableAllJustification
499 if (fontDef.styleStrategy & QFont::NoAntialias)
500 layopts |= kATSLineNoAntiAliasing;
503 layopts |= kATSLineDisableAllKerningAdjustments;
505 values[attributeCount] = &layopts;
506 sizes[attributeCount] = sizeof(layopts);
509 tags[attributeCount] = kATSULayoutOperationOverrideTag;
510 ATSULayoutOperationOverrideSpecifier spec;
511 spec.operationSelector = kATSULayoutOperationPostLayoutAdjustment;
512 spec.overrideUPP = atsuPostLayoutCallback;
513 values[attributeCount] = &spec;
514 sizes[attributeCount] = sizeof(spec);
517 // CTWritingDirection
519 if (flags & QTextEngine::RightToLeft)
520 direction = kATSURightToLeftBaseDirection;
522 direction = kATSULeftToRightBaseDirection;
523 tags[attributeCount] = kATSULineDirectionTag;
524 values[attributeCount] = &direction;
525 sizes[attributeCount] = sizeof(direction);
528 Q_ASSERT(attributeCount < maxAttributeCount + 1);
529 e = ATSUSetLayoutControls(textLayout, attributeCount, tags, sizes, values);
531 qWarning("Qt: internal: %ld: Error ATSUSetLayoutControls %s: %d", long(e), __FILE__, __LINE__);
537 e = ATSUSetRunStyle(textLayout, style, 0, len);
539 qWarning("Qt: internal: %ld: Error ATSUSetRunStyle %s: %d", long(e), __FILE__, __LINE__);
543 if (!(fontDef.styleStrategy & QFont::NoFontMerging)) {
546 ATSUFontID substFont = 0;
547 UniCharArrayOffset changedOffset = 0;
548 UniCharCount changeCount = 0;
550 e = ATSUMatchFontsToText(textLayout, pos, len - pos,
551 &substFont, &changedOffset,
553 if (e == kATSUFontsMatched) {
554 int fontIdx = fontIndexForFontID(substFont);
555 for (uint i = 0; i < changeCount; ++i)
556 mappedFonts[changedOffset + i] = fontIdx;
557 pos = changedOffset + changeCount;
558 ATSUSetRunStyle(textLayout, engineAt(fontIdx)->style, changedOffset, changeCount);
559 } else if (e == kATSUFontsNotMatched) {
560 pos = changedOffset + changeCount;
562 } while (pos < len && e != noErr);
564 { // trigger the a layout
565 // CFAttributedStringCreate, CTFramesetterCreateWithAttributedString (or perhaps Typesetter)
567 e = ATSUMeasureTextImage(textLayout, kATSUFromTextBeginning, kATSUToTextEnd,
568 /*iLocationX =*/ 0, /*iLocationY =*/ 0,
571 qWarning("Qt: internal: %ld: Error ATSUMeasureTextImage %s: %d", long(e), __FILE__, __LINE__);
576 if (!nfo.callbackCalled) {
577 qWarning("Qt: internal: %ld: Error ATSUMeasureTextImage did not trigger callback %s: %d", long(e), __FILE__, __LINE__);
581 ATSUClearLayoutCache(textLayout, kATSUFromTextBeginning);
582 if (prevNumGlyphs < *nfo.numGlyphs)
587 void QFontEngineMacMulti::recalcAdvances(QGlyphLayout *glyphs, QTextEngine::ShaperFlags flags) const
594 void QFontEngineMacMulti::doKerning(QGlyphLayout *, QTextEngine::ShaperFlags) const
599 void QFontEngineMacMulti::loadEngine(int /*at*/)
601 // should never be called!
605 bool QFontEngineMacMulti::canRender(const QChar *string, int len)
607 ATSUSetTextPointerLocation(textLayout, reinterpret_cast<const UniChar *>(string), 0, len, len);
608 ATSUSetRunStyle(textLayout, style, 0, len);
613 FMFont substFont = 0;
614 UniCharArrayOffset changedOffset = 0;
615 UniCharCount changeCount = 0;
617 // CTFontCreateForString
618 e = ATSUMatchFontsToText(textLayout, pos, len - pos,
619 &substFont, &changedOffset,
621 if (e == kATSUFontsMatched) {
622 pos = changedOffset + changeCount;
623 } else if (e == kATSUFontsNotMatched) {
626 } while (pos < len && e != noErr);
628 return e == noErr || e == kATSUFontsMatched;
631 QFontEngineMac::QFontEngineMac(ATSUStyle baseStyle, ATSUFontID fontID, const QFontDef &def, QFontEngineMacMulti *multiEngine)
632 : fontID(fontID), multiEngine(multiEngine), cmap(0), symbolCMap(false)
635 ATSUCreateAndCopyStyle(baseStyle, &style);
636 ATSFontRef atsFont = FMGetATSFontRefFromFont(fontID);
637 cgFont = CGFontCreateWithPlatformFont(&atsFont);
639 const int maxAttributeCount = 4;
640 ATSUAttributeTag tags[maxAttributeCount + 1];
641 ByteCount sizes[maxAttributeCount + 1];
642 ATSUAttributeValuePtr values[maxAttributeCount + 1];
643 int attributeCount = 0;
647 // synthesizing using CG is not recommended
648 quint16 macStyle = 0;
652 if (ATSFontGetTable(atsFont, MAKE_TAG('h', 'e', 'a', 'd'), 44, 4, &data, &len) == noErr)
653 macStyle = qFromBigEndian<quint16>(data);
656 Boolean atsuBold = false;
657 Boolean atsuItalic = false;
658 if (fontDef.weight >= QFont::Bold) {
659 if (!(macStyle & 1)) {
660 synthesisFlags |= SynthesizedBold;
662 tags[attributeCount] = kATSUQDBoldfaceTag;
663 sizes[attributeCount] = sizeof(atsuBold);
664 values[attributeCount] = &atsuBold;
668 if (fontDef.style != QFont::StyleNormal) {
669 if (!(macStyle & 2)) {
670 synthesisFlags |= SynthesizedItalic;
672 tags[attributeCount] = kATSUQDItalicTag;
673 sizes[attributeCount] = sizeof(atsuItalic);
674 values[attributeCount] = &atsuItalic;
679 tags[attributeCount] = kATSUFontTag;
680 values[attributeCount] = &fontID;
681 sizes[attributeCount] = sizeof(fontID);
684 Q_ASSERT(attributeCount < maxAttributeCount + 1);
685 OSStatus err = ATSUSetAttributes(style, attributeCount, tags, sizes, values);
686 Q_ASSERT(err == noErr);
691 if (ATSFontGetTable(atsFont, MAKE_TAG('O', 'S', '/', '2'), 8, 2, &tmpFsType, 0) == noErr)
692 fsType = qFromBigEndian<quint16>(tmpFsType);
697 transform = multiEngine->transform;
699 transform = CGAffineTransformIdentity;
701 ATSUTextMeasurement metric;
703 ATSUGetAttribute(style, kATSUAscentTag, sizeof(metric), &metric, 0);
704 m_ascent = FixRound(metric);
706 ATSUGetAttribute(style, kATSUDescentTag, sizeof(metric), &metric, 0);
707 m_descent = FixRound(metric);
709 ATSUGetAttribute(style, kATSULeadingTag, sizeof(metric), &metric, 0);
710 m_leading = FixRound(metric);
712 ATSFontMetrics metrics;
714 ATSFontGetHorizontalMetrics(FMGetATSFontRefFromFont(fontID), kATSOptionFlagsDefault, &metrics);
715 m_maxCharWidth = metrics.maxAdvanceWidth * fontDef.pointSize;
717 ATSFontGetHorizontalMetrics(FMGetATSFontRefFromFont(fontID), kATSOptionFlagsDefault, &metrics);
718 m_xHeight = QFixed::fromReal(metrics.xHeight * fontDef.pointSize);
720 ATSFontGetHorizontalMetrics(FMGetATSFontRefFromFont(fontID), kATSOptionFlagsDefault, &metrics);
721 m_averageCharWidth = QFixed::fromReal(metrics.avgAdvanceWidth * fontDef.pointSize);
723 // Use width of 'X' if ATSFontGetHorizontalMetrics returns 0 for avgAdvanceWidth.
724 if (m_averageCharWidth == QFixed(0)) {
726 QGlyphLayoutArray<1> glyphs;
728 stringToCMap(&c, 1, &glyphs, &nglyphs, 0);
729 glyph_metrics_t metrics = boundingBox(glyphs);
730 m_averageCharWidth = metrics.width;
734 QFontEngineMac::~QFontEngineMac()
736 ATSUDisposeStyle(style);
739 static inline unsigned int getChar(const QChar *str, int &i, const int len)
741 unsigned int uc = str[i].unicode();
742 if (uc >= 0xd800 && uc < 0xdc00 && i < len-1) {
743 uint low = str[i+1].unicode();
744 if (low >= 0xdc00 && low < 0xe000) {
745 uc = (uc - 0xd800)*0x400 + (low - 0xdc00) + 0x10000;
752 // Not used directly for shaping, only used to calculate m_averageCharWidth
753 bool QFontEngineMac::stringToCMap(const QChar *str, int len, QGlyphLayout *glyphs, int *nglyphs, QTextEngine::ShaperFlags flags) const
756 cmapTable = getSfntTable(MAKE_TAG('c', 'm', 'a', 'p'));
758 cmap = getCMap(reinterpret_cast<const uchar *>(cmapTable.constData()), cmapTable.size(), &symbolCMap, &size);
763 for (int i = 0; i < len; ++i) {
764 unsigned int uc = getChar(str, i, len);
765 glyphs->glyphs[i] = getTrueTypeGlyphIndex(cmap, uc);
766 if(!glyphs->glyphs[i] && uc < 0x100)
767 glyphs->glyphs[i] = getTrueTypeGlyphIndex(cmap, uc + 0xf000);
770 for (int i = 0; i < len; ++i) {
771 unsigned int uc = getChar(str, i, len);
772 glyphs->glyphs[i] = getTrueTypeGlyphIndex(cmap, uc);
777 glyphs->numGlyphs = *nglyphs;
779 if (!(flags & QTextEngine::GlyphIndicesOnly))
780 recalcAdvances(glyphs, flags);
785 void QFontEngineMac::recalcAdvances(QGlyphLayout *glyphs, QTextEngine::ShaperFlags flags) const
789 QVarLengthArray<GlyphID> atsuGlyphs(glyphs->numGlyphs);
790 for (int i = 0; i < glyphs->numGlyphs; ++i)
791 atsuGlyphs[i] = glyphs->glyphs[i];
793 QVarLengthArray<ATSGlyphScreenMetrics> metrics(glyphs->numGlyphs);
795 ATSUGlyphGetScreenMetrics(style, glyphs->numGlyphs, atsuGlyphs.data(), sizeof(GlyphID),
796 /* iForcingAntiAlias =*/ false,
797 /* iAntiAliasSwitch =*/true,
800 for (int i = 0; i < glyphs->numGlyphs; ++i) {
801 glyphs->advances_x[i] = QFixed::fromReal(metrics[i].deviceAdvance.x);
802 glyphs->advances_y[i] = QFixed::fromReal(metrics[i].deviceAdvance.y);
804 if (fontDef.styleStrategy & QFont::ForceIntegerMetrics) {
805 glyphs->advances_x[i] = glyphs->advances_x[i].round();
806 glyphs->advances_y[i] = glyphs->advances_y[i].round();
811 glyph_metrics_t QFontEngineMac::boundingBox(const QGlyphLayout &glyphs)
814 bool round = fontDef.styleStrategy & QFont::ForceIntegerMetrics;
815 for (int i = 0; i < glyphs.numGlyphs; ++i) {
816 w += round ? glyphs.effectiveAdvance(i).round()
817 : glyphs.effectiveAdvance(i);
819 return glyph_metrics_t(0, -(ascent()), w - lastRightBearing(glyphs, round), ascent()+descent(), w, 0);
822 glyph_metrics_t QFontEngineMac::boundingBox(glyph_t glyph)
824 GlyphID atsuGlyph = glyph;
826 ATSGlyphScreenMetrics metrics;
828 ATSUGlyphGetScreenMetrics(style, 1, &atsuGlyph, 0,
829 /* iForcingAntiAlias =*/ false,
830 /* iAntiAliasSwitch =*/true,
836 gm.width = int(metrics.width);
837 gm.height = int(metrics.height);
838 gm.x = QFixed::fromReal(metrics.topLeft.x);
839 gm.y = -QFixed::fromReal(metrics.topLeft.y);
840 gm.xoff = QFixed::fromReal(metrics.deviceAdvance.x);
841 gm.yoff = QFixed::fromReal(metrics.deviceAdvance.y);
843 if (fontDef.styleStrategy & QFont::ForceIntegerMetrics) {
846 gm.xoff = gm.xoff.round();
847 gm.yoff = gm.yoff.round();
853 QFixed QFontEngineMac::ascent() const
855 return (fontDef.styleStrategy & QFont::ForceIntegerMetrics)
860 QFixed QFontEngineMac::descent() const
862 // subtract a pixel to even out the historical +1 in QFontMetrics::height().
864 return (fontDef.styleStrategy & QFont::ForceIntegerMetrics)
865 ? m_descent.round() - 1
869 QFixed QFontEngineMac::leading() const
871 return (fontDef.styleStrategy & QFont::ForceIntegerMetrics)
876 qreal QFontEngineMac::maxCharWidth() const
878 return (fontDef.styleStrategy & QFont::ForceIntegerMetrics)
879 ? qRound(m_maxCharWidth)
883 QFixed QFontEngineMac::xHeight() const
885 return (fontDef.styleStrategy & QFont::ForceIntegerMetrics)
890 QFixed QFontEngineMac::averageCharWidth() const
892 return (fontDef.styleStrategy & QFont::ForceIntegerMetrics)
893 ? m_averageCharWidth.round()
894 : m_averageCharWidth;
897 static void addGlyphsToPathHelper(ATSUStyle style, glyph_t *glyphs, QFixedPoint *positions, int numGlyphs, QPainterPath *path)
904 QMacFontPath fontpath(0, 0, path);
905 ATSCubicMoveToUPP moveTo = NewATSCubicMoveToUPP(QMacFontPath::moveTo);
906 ATSCubicLineToUPP lineTo = NewATSCubicLineToUPP(QMacFontPath::lineTo);
907 ATSCubicCurveToUPP cubicTo = NewATSCubicCurveToUPP(QMacFontPath::cubicTo);
908 ATSCubicClosePathUPP closePath = NewATSCubicClosePathUPP(QMacFontPath::closePath);
910 // CTFontCreatePathForGlyph
911 for (int i = 0; i < numGlyphs; ++i) {
912 GlyphID glyph = glyphs[i];
914 fontpath.setPosition(positions[i].x.toReal(), positions[i].y.toReal());
915 ATSUGlyphGetCubicPaths(style, glyph, moveTo, lineTo,
916 cubicTo, closePath, &fontpath, &e);
919 DisposeATSCubicMoveToUPP(moveTo);
920 DisposeATSCubicLineToUPP(lineTo);
921 DisposeATSCubicCurveToUPP(cubicTo);
922 DisposeATSCubicClosePathUPP(closePath);
925 void QFontEngineMac::addGlyphsToPath(glyph_t *glyphs, QFixedPoint *positions, int numGlyphs, QPainterPath *path,
926 QTextItem::RenderFlags)
928 addGlyphsToPathHelper(style, glyphs, positions, numGlyphs, path);
933 Helper function for alphaMapForGlyph and alphaRGBMapForGlyph. The two are identical, except for
934 the subpixel antialiasing...
936 QImage QFontEngineMac::imageForGlyph(glyph_t glyph, int margin, bool colorful)
938 const glyph_metrics_t br = boundingBox(glyph);
939 QImage im(qRound(br.width)+2, qRound(br.height)+4, QImage::Format_RGB32);
942 CGColorSpaceRef colorspace = QCoreGraphicsPaintEngine::macGenericColorSpace();
943 uint cgflags = kCGImageAlphaNoneSkipFirst;
944 #ifdef kCGBitmapByteOrder32Host //only needed because CGImage.h added symbols in the minor version
945 cgflags |= kCGBitmapByteOrder32Host;
947 CGContextRef ctx = CGBitmapContextCreate(im.bits(), im.width(), im.height(),
948 8, im.bytesPerLine(), colorspace,
950 CGContextSetFontSize(ctx, fontDef.pixelSize);
951 CGContextSetShouldAntialias(ctx, fontDef.pointSize > qt_antialiasing_threshold && !(fontDef.styleStrategy & QFont::NoAntialias));
952 // turn off sub-pixel hinting - no support for that in OpenGL
953 CGContextSetShouldSmoothFonts(ctx, colorful);
954 CGAffineTransform oldTextMatrix = CGContextGetTextMatrix(ctx);
955 CGAffineTransform cgMatrix = CGAffineTransformMake(1, 0, 0, 1, 0, 0);
956 CGAffineTransformConcat(cgMatrix, oldTextMatrix);
958 if (synthesisFlags & QFontEngine::SynthesizedItalic)
959 cgMatrix = CGAffineTransformConcat(cgMatrix, CGAffineTransformMake(1, 0, tanf(14 * acosf(0) / 90), 1, 0, 0));
961 cgMatrix = CGAffineTransformConcat(cgMatrix, transform);
963 CGContextSetTextMatrix(ctx, cgMatrix);
964 CGContextSetRGBFillColor(ctx, 1, 1, 1, 1);
965 CGContextSetTextDrawingMode(ctx, kCGTextFill);
966 CGContextSetFont(ctx, cgFont);
968 qreal pos_x = -br.x.toReal() + 1;
969 qreal pos_y = im.height() + br.y.toReal() - 2;
970 CGContextSetTextPosition(ctx, pos_x, pos_y);
975 CGGlyph cgGlyph = glyph;
976 CGContextShowGlyphsWithAdvances(ctx, &cgGlyph, &advance, 1);
978 if (synthesisFlags & QFontEngine::SynthesizedBold) {
979 CGContextSetTextPosition(ctx, pos_x + 0.5 * lineThickness().toReal(), pos_y);
980 CGContextShowGlyphsWithAdvances(ctx, &cgGlyph, &advance, 1);
983 CGContextRelease(ctx);
988 QImage QFontEngineMac::alphaMapForGlyph(glyph_t glyph)
990 QImage im = imageForGlyph(glyph, 2, false);
992 QImage indexed(im.width(), im.height(), QImage::Format_Indexed8);
993 QVector<QRgb> colors(256);
994 for (int i=0; i<256; ++i)
995 colors[i] = qRgba(0, 0, 0, i);
996 indexed.setColorTable(colors);
998 for (int y=0; y<im.height(); ++y) {
999 uint *src = (uint*) im.scanLine(y);
1000 uchar *dst = indexed.scanLine(y);
1001 for (int x=0; x<im.width(); ++x) {
1011 QImage QFontEngineMac::alphaRGBMapForGlyph(glyph_t glyph, QFixed, int margin, const QTransform &t)
1013 QImage im = imageForGlyph(glyph, margin, true);
1015 if (t.type() >= QTransform::TxScale) {
1016 im = im.transformed(t);
1019 qGamma_correct_back_to_linear_cs(&im);
1025 bool QFontEngineMac::canRender(const QChar *string, int len)
1033 void QFontEngineMac::draw(CGContextRef ctx, qreal x, qreal y, const QTextItemInt &ti, int paintDeviceHeight)
1035 QVarLengthArray<QFixedPoint> positions;
1036 QVarLengthArray<glyph_t> glyphs;
1038 matrix.translate(x, y);
1039 getGlyphPositions(ti.glyphs, matrix, ti.flags, glyphs, positions);
1040 if (glyphs.size() == 0)
1043 CGContextSetFontSize(ctx, fontDef.pixelSize);
1045 CGAffineTransform oldTextMatrix = CGContextGetTextMatrix(ctx);
1047 CGAffineTransform cgMatrix = CGAffineTransformMake(1, 0, 0, -1, 0, -paintDeviceHeight);
1049 CGAffineTransformConcat(cgMatrix, oldTextMatrix);
1051 if (synthesisFlags & QFontEngine::SynthesizedItalic)
1052 cgMatrix = CGAffineTransformConcat(cgMatrix, CGAffineTransformMake(1, 0, -tanf(14 * acosf(0) / 90), 1, 0, 0));
1054 cgMatrix = CGAffineTransformConcat(cgMatrix, transform);
1056 CGContextSetTextMatrix(ctx, cgMatrix);
1058 CGContextSetTextDrawingMode(ctx, kCGTextFill);
1061 QVarLengthArray<CGSize> advances(glyphs.size());
1062 QVarLengthArray<CGGlyph> cgGlyphs(glyphs.size());
1064 for (int i = 0; i < glyphs.size() - 1; ++i) {
1065 advances[i].width = (positions[i + 1].x - positions[i].x).toReal();
1066 advances[i].height = (positions[i + 1].y - positions[i].y).toReal();
1067 cgGlyphs[i] = glyphs[i];
1069 advances[glyphs.size() - 1].width = 0;
1070 advances[glyphs.size() - 1].height = 0;
1071 cgGlyphs[glyphs.size() - 1] = glyphs[glyphs.size() - 1];
1073 CGContextSetFont(ctx, cgFont);
1075 CGContextSetTextPosition(ctx, positions[0].x.toReal(), positions[0].y.toReal());
1077 CGContextShowGlyphsWithAdvances(ctx, cgGlyphs.data(), advances.data(), glyphs.size());
1079 if (synthesisFlags & QFontEngine::SynthesizedBold) {
1080 CGContextSetTextPosition(ctx, positions[0].x.toReal() + 0.5 * lineThickness().toReal(),
1081 positions[0].y.toReal());
1083 CGContextShowGlyphsWithAdvances(ctx, cgGlyphs.data(), advances.data(), glyphs.size());
1086 CGContextSetTextMatrix(ctx, oldTextMatrix);
1089 QFontEngine::FaceId QFontEngineMac::faceId() const
1092 #if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
1093 if (QSysInfo::MacintoshVersion >= QSysInfo::MV_10_5) {
1094 // CTFontGetPlatformFont
1096 if (ATSFontGetFileReference(FMGetATSFontRefFromFont(fontID), &ref) != noErr)
1098 ret.filename = QByteArray(128, 0);
1100 FSRefMakePath(&ref, (UInt8 *)ret.filename.data(), ret.filename.size());
1105 if (ATSFontGetFileSpecification(FMGetATSFontRefFromFont(fontID), &spec) != noErr)
1109 FSpMakeFSRef(&spec, &ref);
1110 ret.filename = QByteArray(128, 0);
1112 FSRefMakePath(&ref, (UInt8 *)ret.filename.data(), ret.filename.size());
1117 QByteArray QFontEngineMac::getSfntTable(uint tag) const
1119 ATSFontRef atsFont = FMGetATSFontRefFromFont(fontID);
1122 OSStatus status = ATSFontGetTable(atsFont, tag, 0, 0, 0, &length);
1123 if (status != noErr)
1124 return QByteArray();
1125 QByteArray table(length, 0);
1127 status = ATSFontGetTable(atsFont, tag, 0, table.length(), table.data(), &length);
1128 if (status != noErr)
1129 return QByteArray();
1133 QFontEngine::Properties QFontEngineMac::properties() const
1135 QFontEngine::Properties props;
1136 ATSFontRef atsFont = FMGetATSFontRefFromFont(fontID);
1138 // CTFontGetUnitsPerEm
1139 if (ATSFontGetTable(atsFont, MAKE_TAG('h', 'e', 'a', 'd'), 18, 2, &tmp, 0) == noErr)
1140 props.emSquare = qFromBigEndian<quint16>(tmp);
1147 bbox.xMin = bbox.xMax = bbox.yMin = bbox.yMax = 0;
1148 // CTFontGetBoundingBox
1149 if (ATSFontGetTable(atsFont, MAKE_TAG('h', 'e', 'a', 'd'), 36, 8, &bbox, 0) == noErr) {
1150 bbox.xMin = qFromBigEndian<quint16>(bbox.xMin);
1151 bbox.yMin = qFromBigEndian<quint16>(bbox.yMin);
1152 bbox.xMax = qFromBigEndian<quint16>(bbox.xMax);
1153 bbox.yMax = qFromBigEndian<quint16>(bbox.yMax);
1160 metrics.ascender = metrics.descender = metrics.linegap = 0;
1161 // CTFontGetAscent, etc.
1162 if (ATSFontGetTable(atsFont, MAKE_TAG('h', 'h', 'e', 'a'), 4, 6, &metrics, 0) == noErr) {
1163 metrics.ascender = qFromBigEndian<quint16>(metrics.ascender);
1164 metrics.descender = qFromBigEndian<quint16>(metrics.descender);
1165 metrics.linegap = qFromBigEndian<quint16>(metrics.linegap);
1167 props.ascent = metrics.ascender;
1168 props.descent = -metrics.descender;
1169 props.leading = metrics.linegap;
1170 props.boundingBox = QRectF(bbox.xMin, -bbox.yMax,
1171 bbox.xMax - bbox.xMin,
1172 bbox.yMax - bbox.yMin);
1173 props.italicAngle = 0;
1174 props.capHeight = props.ascent;
1178 if (ATSFontGetTable(atsFont, MAKE_TAG('p', 'o', 's', 't'), 10, 2, &lw, 0) == noErr)
1179 lw = qFromBigEndian<quint16>(lw);
1180 props.lineWidth = lw;
1182 // CTFontCopyPostScriptName
1184 if (ATSFontGetPostScriptName(FMGetATSFontRefFromFont(fontID), kATSOptionFlagsDefault, &psName) == noErr)
1185 props.postscriptName = QString(psName).toUtf8();
1186 props.postscriptName = QFontEngine::convertToPostscriptFontFamilyName(props.postscriptName);
1190 void QFontEngineMac::getUnscaledGlyph(glyph_t glyph, QPainterPath *path, glyph_metrics_t *metrics)
1192 ATSUStyle unscaledStyle;
1193 ATSUCreateAndCopyStyle(style, &unscaledStyle);
1195 int emSquare = properties().emSquare.toInt();
1197 const int maxAttributeCount = 4;
1198 ATSUAttributeTag tags[maxAttributeCount + 1];
1199 ByteCount sizes[maxAttributeCount + 1];
1200 ATSUAttributeValuePtr values[maxAttributeCount + 1];
1201 int attributeCount = 0;
1203 Fixed size = FixRatio(emSquare, 1);
1204 tags[attributeCount] = kATSUSizeTag;
1205 sizes[attributeCount] = sizeof(size);
1206 values[attributeCount] = &size;
1209 Q_ASSERT(attributeCount < maxAttributeCount + 1);
1210 OSStatus err = ATSUSetAttributes(unscaledStyle, attributeCount, tags, sizes, values);
1211 Q_ASSERT(err == noErr);
1214 // various CTFont metrics functions: CTFontGetBoundingRectsForGlyphs, CTFontGetAdvancesForGlyphs
1215 GlyphID atsuGlyph = glyph;
1216 ATSGlyphScreenMetrics atsuMetrics;
1217 ATSUGlyphGetScreenMetrics(unscaledStyle, 1, &atsuGlyph, 0,
1218 /* iForcingAntiAlias =*/ false,
1219 /* iAntiAliasSwitch =*/true,
1222 metrics->width = int(atsuMetrics.width);
1223 metrics->height = int(atsuMetrics.height);
1224 metrics->x = QFixed::fromReal(atsuMetrics.topLeft.x);
1225 metrics->y = -QFixed::fromReal(atsuMetrics.topLeft.y);
1226 metrics->xoff = QFixed::fromReal(atsuMetrics.deviceAdvance.x);
1227 metrics->yoff = QFixed::fromReal(atsuMetrics.deviceAdvance.y);
1230 addGlyphsToPathHelper(unscaledStyle, &glyph, &p, 1, path);
1232 ATSUDisposeStyle(unscaledStyle);
1234 #endif // !QT_MAC_USE_COCOA