blob: 2b2410996f98e1dc27a5fd0c7eaa2d8d9408ef57 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@android.com0680d6c2008-12-19 19:46:15 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2006 The Android Open Source Project
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
mike@reedtribe.orgb103ed42013-03-03 03:50:09 +00009#include <vector>
10#ifdef SK_BUILD_FOR_MAC
11#import <ApplicationServices/ApplicationServices.h>
12#endif
reed@android.com0680d6c2008-12-19 19:46:15 +000013
mike@reedtribe.orgb103ed42013-03-03 03:50:09 +000014#ifdef SK_BUILD_FOR_IOS
15#include <CoreText/CoreText.h>
reed@google.com3dcbd462013-03-27 13:56:34 +000016#include <CoreText/CTFontManager.h>
mike@reedtribe.orgb103ed42013-03-03 03:50:09 +000017#include <CoreGraphics/CoreGraphics.h>
18#include <CoreFoundation/CoreFoundation.h>
19#endif
20
21#include "SkFontHost.h"
22#include "SkCGUtils.h"
23#include "SkColorPriv.h"
24#include "SkDescriptor.h"
25#include "SkEndian.h"
26#include "SkFontDescriptor.h"
27#include "SkFloatingPoint.h"
28#include "SkGlyph.h"
29#include "SkMaskGamma.h"
30#include "SkSFNTHeader.h"
31#include "SkOTTable_glyf.h"
32#include "SkOTTable_head.h"
33#include "SkOTTable_hhea.h"
34#include "SkOTTable_loca.h"
35#include "SkOTUtils.h"
36#include "SkPaint.h"
37#include "SkPath.h"
38#include "SkString.h"
39#include "SkStream.h"
40#include "SkThread.h"
41#include "SkTypeface_mac.h"
42#include "SkUtils.h"
43#include "SkTypefaceCache.h"
reed@google.comce8b3de2013-03-26 19:30:16 +000044#include "SkFontMgr.h"
mike@reedtribe.orgb103ed42013-03-03 03:50:09 +000045
46class SkScalerContext_Mac;
47
reed@google.com3dcbd462013-03-27 13:56:34 +000048// CTFontManagerCopyAvailableFontFamilyNames() is not always available, so we
49// provide a wrapper here that will return an empty array if need be.
50static CFArrayRef SkCTFontManagerCopyAvailableFontFamilyNames() {
51#ifdef SK_BUILD_FOR_IOS
52 return CFArrayCreate(NULL, NULL, 0, NULL);
53#else
54 return CTFontManagerCopyAvailableFontFamilyNames();
55#endif
56}
57
58
mike@reedtribe.orgb103ed42013-03-03 03:50:09 +000059// Being templated and taking const T* prevents calling
60// CFSafeRelease(autoCFRelease) through implicit conversion.
61template <typename T> static void CFSafeRelease(/*CFTypeRef*/const T* cfTypeRef) {
62 if (cfTypeRef) {
63 CFRelease(cfTypeRef);
64 }
65}
66
67// Being templated and taking const T* prevents calling
68// CFSafeRetain(autoCFRelease) through implicit conversion.
69template <typename T> static void CFSafeRetain(/*CFTypeRef*/const T* cfTypeRef) {
70 if (cfTypeRef) {
71 CFRetain(cfTypeRef);
72 }
73}
74
75/** Acts like a CFRef, but calls CFSafeRelease when it goes out of scope. */
76template<typename CFRef> class AutoCFRelease : private SkNoncopyable {
77public:
78 explicit AutoCFRelease(CFRef cfRef = NULL) : fCFRef(cfRef) { }
79 ~AutoCFRelease() { CFSafeRelease(fCFRef); }
80
81 void reset(CFRef that = NULL) {
82 CFSafeRetain(that);
83 CFSafeRelease(fCFRef);
84 fCFRef = that;
85 }
86
87 AutoCFRelease& operator =(CFRef that) {
88 reset(that);
89 return *this;
90 }
91
92 operator CFRef() const { return fCFRef; }
93 CFRef get() const { return fCFRef; }
94
95private:
96 CFRef fCFRef;
97};
98
99template<typename T> class AutoCGTable : SkNoncopyable {
100public:
101 AutoCGTable(CGFontRef font)
102 //Undocumented: the tag parameter in this call is expected in machine order and not BE order.
103 : fCFData(CGFontCopyTableForTag(font, SkSetFourByteTag(T::TAG0, T::TAG1, T::TAG2, T::TAG3)))
104 , fData(fCFData ? reinterpret_cast<const T*>(CFDataGetBytePtr(fCFData)) : NULL)
105 { }
106
107 const T* operator->() const { return fData; }
108
109private:
110 AutoCFRelease<CFDataRef> fCFData;
111public:
112 const T* fData;
113};
114
115// inline versions of these rect helpers
116
117static bool CGRectIsEmpty_inline(const CGRect& rect) {
118 return rect.size.width <= 0 || rect.size.height <= 0;
119}
120
121static void CGRectInset_inline(CGRect* rect, CGFloat dx, CGFloat dy) {
122 rect->origin.x += dx;
123 rect->origin.y += dy;
124 rect->size.width -= dx * 2;
125 rect->size.height -= dy * 2;
126}
127
128static CGFloat CGRectGetMinX_inline(const CGRect& rect) {
129 return rect.origin.x;
130}
131
132static CGFloat CGRectGetMaxX_inline(const CGRect& rect) {
133 return rect.origin.x + rect.size.width;
134}
135
136static CGFloat CGRectGetMinY_inline(const CGRect& rect) {
137 return rect.origin.y;
138}
139
140static CGFloat CGRectGetMaxY_inline(const CGRect& rect) {
141 return rect.origin.y + rect.size.height;
142}
143
144static CGFloat CGRectGetWidth_inline(const CGRect& rect) {
145 return rect.size.width;
146}
147
148///////////////////////////////////////////////////////////////////////////////
149
150static void sk_memset_rect32(uint32_t* ptr, uint32_t value,
151 size_t width, size_t height, size_t rowBytes) {
152 SkASSERT(width);
153 SkASSERT(width * sizeof(uint32_t) <= rowBytes);
154
155 if (width >= 32) {
156 while (height) {
157 sk_memset32(ptr, value, width);
158 ptr = (uint32_t*)((char*)ptr + rowBytes);
159 height -= 1;
160 }
161 return;
162 }
163
164 rowBytes -= width * sizeof(uint32_t);
165
166 if (width >= 8) {
167 while (height) {
168 int w = width;
169 do {
170 *ptr++ = value; *ptr++ = value;
171 *ptr++ = value; *ptr++ = value;
172 *ptr++ = value; *ptr++ = value;
173 *ptr++ = value; *ptr++ = value;
174 w -= 8;
175 } while (w >= 8);
176 while (--w >= 0) {
177 *ptr++ = value;
178 }
179 ptr = (uint32_t*)((char*)ptr + rowBytes);
180 height -= 1;
181 }
182 } else {
183 while (height) {
184 int w = width;
185 do {
186 *ptr++ = value;
187 } while (--w > 0);
188 ptr = (uint32_t*)((char*)ptr + rowBytes);
189 height -= 1;
190 }
191 }
192}
193
194#include <sys/utsname.h>
195
196typedef uint32_t CGRGBPixel;
197
198static unsigned CGRGBPixel_getAlpha(CGRGBPixel pixel) {
199 return pixel & 0xFF;
200}
201
202// The calls to support subpixel are present in 10.5, but are not included in
203// the 10.5 SDK. The needed calls have been extracted from the 10.6 SDK and are
204// included below. To verify that CGContextSetShouldSubpixelQuantizeFonts, for
205// instance, is present in the 10.5 CoreGraphics libary, use:
206// cd /Developer/SDKs/MacOSX10.5.sdk/System/Library/Frameworks/
207// cd ApplicationServices.framework/Frameworks/CoreGraphics.framework/
208// nm CoreGraphics | grep CGContextSetShouldSubpixelQuantizeFonts
209
210#if !defined(MAC_OS_X_VERSION_10_6) || (MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_6)
211CG_EXTERN void CGContextSetAllowsFontSmoothing(CGContextRef context, bool value);
212CG_EXTERN void CGContextSetAllowsFontSubpixelPositioning(CGContextRef context, bool value);
213CG_EXTERN void CGContextSetShouldSubpixelPositionFonts(CGContextRef context, bool value);
214CG_EXTERN void CGContextSetAllowsFontSubpixelQuantization(CGContextRef context, bool value);
215CG_EXTERN void CGContextSetShouldSubpixelQuantizeFonts(CGContextRef context, bool value);
216#endif
217
218static const char FONT_DEFAULT_NAME[] = "Lucida Sans";
219
220// See Source/WebKit/chromium/base/mac/mac_util.mm DarwinMajorVersionInternal for original source.
221static int readVersion() {
222 struct utsname info;
223 if (uname(&info) != 0) {
224 SkDebugf("uname failed\n");
225 return 0;
226 }
227 if (strcmp(info.sysname, "Darwin") != 0) {
228 SkDebugf("unexpected uname sysname %s\n", info.sysname);
229 return 0;
230 }
231 char* dot = strchr(info.release, '.');
232 if (!dot) {
233 SkDebugf("expected dot in uname release %s\n", info.release);
234 return 0;
235 }
236 int version = atoi(info.release);
237 if (version == 0) {
238 SkDebugf("could not parse uname release %s\n", info.release);
239 }
240 return version;
241}
242
243static int darwinVersion() {
244 static int darwin_version = readVersion();
245 return darwin_version;
246}
247
248static bool isLeopard() {
249 return darwinVersion() == 9;
250}
251
252static bool isSnowLeopard() {
253 return darwinVersion() == 10;
254}
255
256static bool isLion() {
257 return darwinVersion() == 11;
258}
259
260static bool isMountainLion() {
261 return darwinVersion() == 12;
262}
263
264static bool isLCDFormat(unsigned format) {
265 return SkMask::kLCD16_Format == format || SkMask::kLCD32_Format == format;
266}
267
268static CGFloat ScalarToCG(SkScalar scalar) {
269 if (sizeof(CGFloat) == sizeof(float)) {
270 return SkScalarToFloat(scalar);
271 } else {
272 SkASSERT(sizeof(CGFloat) == sizeof(double));
273 return (CGFloat) SkScalarToDouble(scalar);
274 }
275}
276
277static SkScalar CGToScalar(CGFloat cgFloat) {
278 if (sizeof(CGFloat) == sizeof(float)) {
279 return SkFloatToScalar(cgFloat);
280 } else {
281 SkASSERT(sizeof(CGFloat) == sizeof(double));
282 return SkDoubleToScalar(cgFloat);
283 }
284}
285
286static CGAffineTransform MatrixToCGAffineTransform(const SkMatrix& matrix,
287 SkScalar sx = SK_Scalar1,
288 SkScalar sy = SK_Scalar1) {
289 return CGAffineTransformMake( ScalarToCG(matrix[SkMatrix::kMScaleX] * sx),
290 -ScalarToCG(matrix[SkMatrix::kMSkewY] * sy),
291 -ScalarToCG(matrix[SkMatrix::kMSkewX] * sx),
292 ScalarToCG(matrix[SkMatrix::kMScaleY] * sy),
293 ScalarToCG(matrix[SkMatrix::kMTransX] * sx),
294 ScalarToCG(matrix[SkMatrix::kMTransY] * sy));
295}
296
297static SkScalar getFontScale(CGFontRef cgFont) {
298 int unitsPerEm = CGFontGetUnitsPerEm(cgFont);
299 return SkScalarInvert(SkIntToScalar(unitsPerEm));
300}
301
302///////////////////////////////////////////////////////////////////////////////
303
304#define BITMAP_INFO_RGB (kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Host)
305#define BITMAP_INFO_GRAY (kCGImageAlphaNone)
306
307/**
308 * There does not appear to be a publicly accessable API for determining if lcd
309 * font smoothing will be applied if we request it. The main issue is that if
310 * smoothing is applied a gamma of 2.0 will be used, if not a gamma of 1.0.
311 */
312static bool supports_LCD() {
313 static int gSupportsLCD = -1;
314 if (gSupportsLCD >= 0) {
315 return (bool) gSupportsLCD;
316 }
317 uint32_t rgb = 0;
318 AutoCFRelease<CGColorSpaceRef> colorspace(CGColorSpaceCreateDeviceRGB());
319 AutoCFRelease<CGContextRef> cgContext(CGBitmapContextCreate(&rgb, 1, 1, 8, 4,
320 colorspace, BITMAP_INFO_RGB));
321 CGContextSelectFont(cgContext, "Helvetica", 16, kCGEncodingMacRoman);
322 CGContextSetShouldSmoothFonts(cgContext, true);
323 CGContextSetShouldAntialias(cgContext, true);
324 CGContextSetTextDrawingMode(cgContext, kCGTextFill);
325 CGContextSetGrayFillColor(cgContext, 1, 1);
326 CGContextShowTextAtPoint(cgContext, -1, 0, "|", 1);
327 uint32_t r = (rgb >> 16) & 0xFF;
328 uint32_t g = (rgb >> 8) & 0xFF;
329 uint32_t b = (rgb >> 0) & 0xFF;
330 gSupportsLCD = (r != g || r != b);
331 return (bool) gSupportsLCD;
332}
333
334class Offscreen {
335public:
336 Offscreen();
337
338 CGRGBPixel* getCG(const SkScalerContext_Mac& context, const SkGlyph& glyph,
339 CGGlyph glyphID, size_t* rowBytesPtr,
340 bool generateA8FromLCD);
341
342private:
343 enum {
344 kSize = 32 * 32 * sizeof(CGRGBPixel)
345 };
346 SkAutoSMalloc<kSize> fImageStorage;
347 AutoCFRelease<CGColorSpaceRef> fRGBSpace;
348
349 // cached state
350 AutoCFRelease<CGContextRef> fCG;
351 SkISize fSize;
352 bool fDoAA;
353 bool fDoLCD;
354
355 static int RoundSize(int dimension) {
356 return SkNextPow2(dimension);
357 }
358};
359
360Offscreen::Offscreen() : fRGBSpace(NULL), fCG(NULL) {
361 fSize.set(0, 0);
362}
363
364///////////////////////////////////////////////////////////////////////////////
365
bungeman@google.comfe747652013-03-25 19:36:11 +0000366static SkTypeface::Style computeStyleBits(CTFontRef font, bool* isFixedPitch) {
mike@reedtribe.orgb103ed42013-03-03 03:50:09 +0000367 unsigned style = SkTypeface::kNormal;
368 CTFontSymbolicTraits traits = CTFontGetSymbolicTraits(font);
369
370 if (traits & kCTFontBoldTrait) {
371 style |= SkTypeface::kBold;
372 }
373 if (traits & kCTFontItalicTrait) {
374 style |= SkTypeface::kItalic;
375 }
bungeman@google.comfe747652013-03-25 19:36:11 +0000376 if (isFixedPitch) {
377 *isFixedPitch = (traits & kCTFontMonoSpaceTrait) != 0;
mike@reedtribe.orgb103ed42013-03-03 03:50:09 +0000378 }
379 return (SkTypeface::Style)style;
380}
381
382static SkFontID CTFontRef_to_SkFontID(CTFontRef fontRef) {
383 SkFontID id = 0;
384// CTFontGetPlatformFont and ATSFontRef are not supported on iOS, so we have to
385// bracket this to be Mac only.
386#ifdef SK_BUILD_FOR_MAC
387 ATSFontRef ats = CTFontGetPlatformFont(fontRef, NULL);
388 id = (SkFontID)ats;
389 if (id != 0) {
390 id &= 0x3FFFFFFF; // make top two bits 00
391 return id;
392 }
393#endif
394 // CTFontGetPlatformFont returns NULL if the font is local
395 // (e.g., was created by a CSS3 @font-face rule).
396 AutoCFRelease<CGFontRef> cgFont(CTFontCopyGraphicsFont(fontRef, NULL));
397 AutoCGTable<SkOTTableHead> headTable(cgFont);
398 if (headTable.fData) {
399 id = (SkFontID) headTable->checksumAdjustment;
400 id = (id & 0x3FFFFFFF) | 0x40000000; // make top two bits 01
401 }
402 // well-formed fonts have checksums, but as a last resort, use the pointer.
403 if (id == 0) {
404 id = (SkFontID) (uintptr_t) fontRef;
405 id = (id & 0x3FFFFFFF) | 0x80000000; // make top two bits 10
406 }
407 return id;
408}
409
reed@google.comce8b3de2013-03-26 19:30:16 +0000410static SkFontStyle stylebits2fontstyle(SkTypeface::Style styleBits) {
411 return SkFontStyle((styleBits & SkTypeface::kBold)
412 ? SkFontStyle::kBold_Weight
413 : SkFontStyle::kNormal_Weight,
414 SkFontStyle::kNormal_Width,
415 (styleBits & SkTypeface::kItalic)
416 ? SkFontStyle::kItalic_Slant
417 : SkFontStyle::kUpright_Slant);
418}
419
420#define WEIGHT_THRESHOLD ((SkFontStyle::kNormal_Weight + SkFontStyle::kBold_Weight)/2)
421
422static SkTypeface::Style fontstyle2stylebits(const SkFontStyle& fs) {
423 unsigned style = 0;
424 if (fs.width() >= WEIGHT_THRESHOLD) {
425 style |= SkTypeface::kBold;
426 }
427 if (fs.isItalic()) {
428 style |= SkTypeface::kItalic;
429 }
430 return (SkTypeface::Style)style;
431}
432
mike@reedtribe.orgb103ed42013-03-03 03:50:09 +0000433class SkTypeface_Mac : public SkTypeface {
434public:
bungeman@google.comfe747652013-03-25 19:36:11 +0000435 SkTypeface_Mac(SkTypeface::Style style, SkFontID fontID, bool isFixedPitch,
mike@reedtribe.orgb103ed42013-03-03 03:50:09 +0000436 CTFontRef fontRef, const char name[])
reed@google.comce8b3de2013-03-26 19:30:16 +0000437 : SkTypeface(style, fontID, isFixedPitch)
438 , fName(name)
439 , fFontRef(fontRef) // caller has already called CFRetain for us
440 , fFontStyle(stylebits2fontstyle(style))
mike@reedtribe.orgb103ed42013-03-03 03:50:09 +0000441 {
442 SkASSERT(fontRef);
443 }
skia.committer@gmail.com37cbc7f2013-03-27 07:01:04 +0000444
reed@google.comce8b3de2013-03-26 19:30:16 +0000445 SkTypeface_Mac(const SkFontStyle& fs, SkFontID fontID, bool isFixedPitch,
446 CTFontRef fontRef, const char name[])
447 : SkTypeface(fontstyle2stylebits(fs), fontID, isFixedPitch)
448 , fName(name)
449 , fFontRef(fontRef) // caller has already called CFRetain for us
450 , fFontStyle(fs)
451 {
452 SkASSERT(fontRef);
453 }
skia.committer@gmail.com37cbc7f2013-03-27 07:01:04 +0000454
mike@reedtribe.orgb103ed42013-03-03 03:50:09 +0000455 SkString fName;
456 AutoCFRelease<CTFontRef> fFontRef;
reed@google.comce8b3de2013-03-26 19:30:16 +0000457 SkFontStyle fFontStyle;
mike@reedtribe.orgb103ed42013-03-03 03:50:09 +0000458
459protected:
460 friend class SkFontHost; // to access our protected members for deprecated methods
461
462 virtual int onGetUPEM() const SK_OVERRIDE;
reed@google.comcc9aad52013-03-21 19:28:10 +0000463 virtual SkStream* onOpenStream(int* ttcIndex) const SK_OVERRIDE;
mike@reedtribe.orgb103ed42013-03-03 03:50:09 +0000464 virtual int onGetTableTags(SkFontTableTag tags[]) const SK_OVERRIDE;
465 virtual size_t onGetTableData(SkFontTableTag, size_t offset,
466 size_t length, void* data) const SK_OVERRIDE;
467 virtual SkScalerContext* onCreateScalerContext(const SkDescriptor*) const SK_OVERRIDE;
468 virtual void onFilterRec(SkScalerContextRec*) const SK_OVERRIDE;
reed@google.com5526ede2013-03-25 13:03:37 +0000469 virtual void onGetFontDescriptor(SkFontDescriptor*, bool*) const SK_OVERRIDE;
reed@google.com2689f612013-03-20 20:01:47 +0000470 virtual SkAdvancedTypefaceMetrics* onGetAdvancedTypefaceMetrics(
471 SkAdvancedTypefaceMetrics::PerGlyphInfo,
472 const uint32_t*, uint32_t) const SK_OVERRIDE;
skia.committer@gmail.comc1641fc2013-03-21 07:01:39 +0000473
mike@reedtribe.orgb103ed42013-03-03 03:50:09 +0000474private:
reed@google.comce8b3de2013-03-26 19:30:16 +0000475
mike@reedtribe.orgb103ed42013-03-03 03:50:09 +0000476 typedef SkTypeface INHERITED;
477};
478
479static SkTypeface* NewFromFontRef(CTFontRef fontRef, const char name[]) {
480 SkASSERT(fontRef);
bungeman@google.comfe747652013-03-25 19:36:11 +0000481 bool isFixedPitch;
482 SkTypeface::Style style = computeStyleBits(fontRef, &isFixedPitch);
mike@reedtribe.orgb103ed42013-03-03 03:50:09 +0000483 SkFontID fontID = CTFontRef_to_SkFontID(fontRef);
484
bungeman@google.comfe747652013-03-25 19:36:11 +0000485 return new SkTypeface_Mac(style, fontID, isFixedPitch, fontRef, name);
mike@reedtribe.orgb103ed42013-03-03 03:50:09 +0000486}
487
488static SkTypeface* NewFromName(const char familyName[], SkTypeface::Style theStyle) {
489 CTFontRef ctFont = NULL;
490
491 CTFontSymbolicTraits ctFontTraits = 0;
492 if (theStyle & SkTypeface::kBold) {
493 ctFontTraits |= kCTFontBoldTrait;
494 }
495 if (theStyle & SkTypeface::kItalic) {
496 ctFontTraits |= kCTFontItalicTrait;
497 }
498
499 // Create the font info
500 AutoCFRelease<CFStringRef> cfFontName(
501 CFStringCreateWithCString(NULL, familyName, kCFStringEncodingUTF8));
502
503 AutoCFRelease<CFNumberRef> cfFontTraits(
504 CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &ctFontTraits));
505
506 AutoCFRelease<CFMutableDictionaryRef> cfAttributes(
507 CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
508 &kCFTypeDictionaryKeyCallBacks,
509 &kCFTypeDictionaryValueCallBacks));
510
511 AutoCFRelease<CFMutableDictionaryRef> cfTraits(
512 CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
513 &kCFTypeDictionaryKeyCallBacks,
514 &kCFTypeDictionaryValueCallBacks));
515
516 // Create the font
517 if (cfFontName != NULL && cfFontTraits != NULL && cfAttributes != NULL && cfTraits != NULL) {
518 CFDictionaryAddValue(cfTraits, kCTFontSymbolicTrait, cfFontTraits);
519
520 CFDictionaryAddValue(cfAttributes, kCTFontFamilyNameAttribute, cfFontName);
521 CFDictionaryAddValue(cfAttributes, kCTFontTraitsAttribute, cfTraits);
522
523 AutoCFRelease<CTFontDescriptorRef> ctFontDesc(
524 CTFontDescriptorCreateWithAttributes(cfAttributes));
525
526 if (ctFontDesc != NULL) {
527 if (isLeopard()) {
528 // CTFontCreateWithFontDescriptor on Leopard ignores the name
529 AutoCFRelease<CTFontRef> ctNamed(CTFontCreateWithName(cfFontName, 1, NULL));
530 ctFont = CTFontCreateCopyWithAttributes(ctNamed, 1, NULL, ctFontDesc);
531 } else {
532 ctFont = CTFontCreateWithFontDescriptor(ctFontDesc, 0, NULL);
533 }
534 }
535 }
536
537 return ctFont ? NewFromFontRef(ctFont, familyName) : NULL;
538}
539
mike@reedtribe.orgb103ed42013-03-03 03:50:09 +0000540static SkTypeface* GetDefaultFace() {
541 SK_DECLARE_STATIC_MUTEX(gMutex);
542 SkAutoMutexAcquire ma(gMutex);
543
544 static SkTypeface* gDefaultFace;
545
546 if (NULL == gDefaultFace) {
547 gDefaultFace = NewFromName(FONT_DEFAULT_NAME, SkTypeface::kNormal);
548 SkTypefaceCache::Add(gDefaultFace, SkTypeface::kNormal);
549 }
550 return gDefaultFace;
551}
552
553///////////////////////////////////////////////////////////////////////////////
554
555extern CTFontRef SkTypeface_GetCTFontRef(const SkTypeface* face);
556CTFontRef SkTypeface_GetCTFontRef(const SkTypeface* face) {
557 const SkTypeface_Mac* macface = (const SkTypeface_Mac*)face;
558 return macface ? macface->fFontRef.get() : NULL;
559}
560
561/* This function is visible on the outside. It first searches the cache, and if
562 * not found, returns a new entry (after adding it to the cache).
563 */
564SkTypeface* SkCreateTypefaceFromCTFont(CTFontRef fontRef) {
565 SkFontID fontID = CTFontRef_to_SkFontID(fontRef);
566 SkTypeface* face = SkTypefaceCache::FindByID(fontID);
567 if (face) {
568 face->ref();
569 } else {
570 face = NewFromFontRef(fontRef, NULL);
571 SkTypefaceCache::Add(face, face->style());
572 // NewFromFontRef doesn't retain the parameter, but the typeface it
573 // creates does release it in its destructor, so we balance that with
574 // a retain call here.
575 CFRetain(fontRef);
576 }
577 SkASSERT(face->getRefCnt() > 1);
578 return face;
579}
580
581struct NameStyleRec {
582 const char* fName;
583 SkTypeface::Style fStyle;
584};
585
586static bool FindByNameStyle(SkTypeface* face, SkTypeface::Style style,
587 void* ctx) {
588 const SkTypeface_Mac* mface = reinterpret_cast<SkTypeface_Mac*>(face);
589 const NameStyleRec* rec = reinterpret_cast<const NameStyleRec*>(ctx);
590
591 return rec->fStyle == style && mface->fName.equals(rec->fName);
592}
593
594static const char* map_css_names(const char* name) {
595 static const struct {
596 const char* fFrom; // name the caller specified
597 const char* fTo; // "canonical" name we map to
598 } gPairs[] = {
599 { "sans-serif", "Helvetica" },
600 { "serif", "Times" },
601 { "monospace", "Courier" }
602 };
603
604 for (size_t i = 0; i < SK_ARRAY_COUNT(gPairs); i++) {
605 if (strcmp(name, gPairs[i].fFrom) == 0) {
606 return gPairs[i].fTo;
607 }
608 }
609 return name; // no change
610}
611
612SkTypeface* SkFontHost::CreateTypeface(const SkTypeface* familyFace,
613 const char familyName[],
614 SkTypeface::Style style) {
615 if (familyName) {
616 familyName = map_css_names(familyName);
617 }
618
619 // Clone an existing typeface
620 // TODO: only clone if style matches the familyFace's style...
621 if (familyName == NULL && familyFace != NULL) {
622 familyFace->ref();
623 return const_cast<SkTypeface*>(familyFace);
624 }
625
626 if (!familyName || !*familyName) {
627 familyName = FONT_DEFAULT_NAME;
628 }
629
630 NameStyleRec rec = { familyName, style };
631 SkTypeface* face = SkTypefaceCache::FindByProcAndRef(FindByNameStyle, &rec);
632
633 if (NULL == face) {
634 face = NewFromName(familyName, style);
635 if (face) {
636 SkTypefaceCache::Add(face, style);
637 } else {
638 face = GetDefaultFace();
639 face->ref();
640 }
641 }
642 return face;
643}
644
645static void flip(SkMatrix* matrix) {
646 matrix->setSkewX(-matrix->getSkewX());
647 matrix->setSkewY(-matrix->getSkewY());
648}
649
650///////////////////////////////////////////////////////////////////////////////
651
652struct GlyphRect {
653 int16_t fMinX;
654 int16_t fMinY;
655 int16_t fMaxX;
656 int16_t fMaxY;
657};
658
659class SkScalerContext_Mac : public SkScalerContext {
660public:
reed@google.com0da48612013-03-19 16:06:52 +0000661 SkScalerContext_Mac(SkTypeface_Mac*, const SkDescriptor*);
662 virtual ~SkScalerContext_Mac();
mike@reedtribe.orgb103ed42013-03-03 03:50:09 +0000663
664
665protected:
666 unsigned generateGlyphCount(void) SK_OVERRIDE;
667 uint16_t generateCharToGlyph(SkUnichar uni) SK_OVERRIDE;
668 void generateAdvance(SkGlyph* glyph) SK_OVERRIDE;
669 void generateMetrics(SkGlyph* glyph) SK_OVERRIDE;
670 void generateImage(const SkGlyph& glyph) SK_OVERRIDE;
671 void generatePath(const SkGlyph& glyph, SkPath* path) SK_OVERRIDE;
672 void generateFontMetrics(SkPaint::FontMetrics* mX, SkPaint::FontMetrics* mY) SK_OVERRIDE;
673
674private:
675 static void CTPathElement(void *info, const CGPathElement *element);
676 uint16_t getFBoundingBoxesGlyphOffset();
677 void getVerticalOffset(CGGlyph glyphID, SkIPoint* offset) const;
678 bool generateBBoxes();
679
680 CGAffineTransform fTransform;
681 SkMatrix fUnitMatrix; // without font size
682 SkMatrix fVerticalMatrix; // unit rotated
683 SkMatrix fMatrix; // with font size
684 SkMatrix fFBoundingBoxesMatrix; // lion-specific fix
685 Offscreen fOffscreen;
686 AutoCFRelease<CTFontRef> fCTFont;
687 AutoCFRelease<CTFontRef> fCTVerticalFont; // for vertical advance
688 AutoCFRelease<CGFontRef> fCGFont;
689 GlyphRect* fFBoundingBoxes;
690 uint16_t fFBoundingBoxesGlyphOffset;
691 uint16_t fGlyphCount;
692 bool fGeneratedFBoundingBoxes;
693 bool fDoSubPosition;
694 bool fVertical;
695
696 friend class Offscreen;
reed@google.com0da48612013-03-19 16:06:52 +0000697
698 typedef SkScalerContext INHERITED;
mike@reedtribe.orgb103ed42013-03-03 03:50:09 +0000699};
700
reed@google.com0da48612013-03-19 16:06:52 +0000701SkScalerContext_Mac::SkScalerContext_Mac(SkTypeface_Mac* typeface,
702 const SkDescriptor* desc)
703 : INHERITED(typeface, desc)
mike@reedtribe.orgb103ed42013-03-03 03:50:09 +0000704 , fFBoundingBoxes(NULL)
705 , fFBoundingBoxesGlyphOffset(0)
706 , fGeneratedFBoundingBoxes(false)
707{
reed@google.com2689f612013-03-20 20:01:47 +0000708 CTFontRef ctFont = typeface->fFontRef.get();
mike@reedtribe.orgb103ed42013-03-03 03:50:09 +0000709 CFIndex numGlyphs = CTFontGetGlyphCount(ctFont);
710
711 // Get the state we need
712 fRec.getSingleMatrix(&fMatrix);
713 fUnitMatrix = fMatrix;
714
715 // extract the font size out of the matrix, but leave the skewing for italic
716 SkScalar reciprocal = SkScalarInvert(fRec.fTextSize);
717 fUnitMatrix.preScale(reciprocal, reciprocal);
718
719 SkASSERT(numGlyphs >= 1 && numGlyphs <= 0xFFFF);
720
721 fTransform = MatrixToCGAffineTransform(fMatrix);
722
723 CGAffineTransform transform;
724 CGFloat unitFontSize;
725 if (isLeopard()) {
726 // passing 1 for pointSize to Leopard sets the font size to 1 pt.
727 // pass the CoreText size explicitly
728 transform = MatrixToCGAffineTransform(fUnitMatrix);
729 unitFontSize = SkScalarToFloat(fRec.fTextSize);
730 } else {
731 // since our matrix includes everything, we pass 1 for pointSize
732 transform = fTransform;
733 unitFontSize = 1;
734 }
735 flip(&fUnitMatrix); // flip to fix up bounds later
736 fVertical = SkToBool(fRec.fFlags & kVertical_Flag);
737 AutoCFRelease<CTFontDescriptorRef> ctFontDesc;
738 if (fVertical) {
739 AutoCFRelease<CFMutableDictionaryRef> cfAttributes(CFDictionaryCreateMutable(
740 kCFAllocatorDefault, 0,
741 &kCFTypeDictionaryKeyCallBacks,
742 &kCFTypeDictionaryValueCallBacks));
743 if (cfAttributes) {
744 CTFontOrientation ctOrientation = kCTFontVerticalOrientation;
745 AutoCFRelease<CFNumberRef> cfVertical(CFNumberCreate(
746 kCFAllocatorDefault, kCFNumberSInt32Type, &ctOrientation));
747 CFDictionaryAddValue(cfAttributes, kCTFontOrientationAttribute, cfVertical);
748 ctFontDesc = CTFontDescriptorCreateWithAttributes(cfAttributes);
749 }
750 }
751 fCTFont = CTFontCreateCopyWithAttributes(ctFont, unitFontSize, &transform, ctFontDesc);
752 fCGFont = CTFontCopyGraphicsFont(fCTFont, NULL);
753 if (fVertical) {
754 CGAffineTransform rotateLeft = CGAffineTransformMake(0, -1, 1, 0, 0, 0);
755 transform = CGAffineTransformConcat(rotateLeft, transform);
756 fCTVerticalFont = CTFontCreateCopyWithAttributes(ctFont, unitFontSize, &transform, NULL);
757 fVerticalMatrix = fUnitMatrix;
758 if (isSnowLeopard()) {
759 SkScalar scale = SkScalarMul(fRec.fTextSize, getFontScale(fCGFont));
760 fVerticalMatrix.preScale(scale, scale);
761 } else {
762 fVerticalMatrix.preRotate(SkIntToScalar(90));
763 }
764 fVerticalMatrix.postScale(SK_Scalar1, -SK_Scalar1);
765 }
766 fGlyphCount = SkToU16(numGlyphs);
767 fDoSubPosition = SkToBool(fRec.fFlags & kSubpixelPositioning_Flag);
768}
769
770SkScalerContext_Mac::~SkScalerContext_Mac() {
771 delete[] fFBoundingBoxes;
772}
773
774CGRGBPixel* Offscreen::getCG(const SkScalerContext_Mac& context, const SkGlyph& glyph,
775 CGGlyph glyphID, size_t* rowBytesPtr,
776 bool generateA8FromLCD) {
777 if (!fRGBSpace) {
778 //It doesn't appear to matter what color space is specified.
779 //Regular blends and antialiased text are always (s*a + d*(1-a))
780 //and smoothed text is always g=2.0.
781 fRGBSpace = CGColorSpaceCreateDeviceRGB();
782 }
783
784 // default to kBW_Format
785 bool doAA = false;
786 bool doLCD = false;
787
788 if (SkMask::kBW_Format != glyph.fMaskFormat) {
789 doLCD = true;
790 doAA = true;
791 }
792
793 // FIXME: lcd smoothed un-hinted rasterization unsupported.
794 if (!generateA8FromLCD && SkMask::kA8_Format == glyph.fMaskFormat) {
795 doLCD = false;
796 doAA = true;
797 }
798
799 size_t rowBytes = fSize.fWidth * sizeof(CGRGBPixel);
800 if (!fCG || fSize.fWidth < glyph.fWidth || fSize.fHeight < glyph.fHeight) {
801 if (fSize.fWidth < glyph.fWidth) {
802 fSize.fWidth = RoundSize(glyph.fWidth);
803 }
804 if (fSize.fHeight < glyph.fHeight) {
805 fSize.fHeight = RoundSize(glyph.fHeight);
806 }
807
808 rowBytes = fSize.fWidth * sizeof(CGRGBPixel);
809 void* image = fImageStorage.reset(rowBytes * fSize.fHeight);
810 fCG = CGBitmapContextCreate(image, fSize.fWidth, fSize.fHeight, 8,
811 rowBytes, fRGBSpace, BITMAP_INFO_RGB);
812
813 // skia handles quantization itself, so we disable this for cg to get
814 // full fractional data from them.
815 CGContextSetAllowsFontSubpixelQuantization(fCG, false);
816 CGContextSetShouldSubpixelQuantizeFonts(fCG, false);
817
818 CGContextSetTextDrawingMode(fCG, kCGTextFill);
819 CGContextSetFont(fCG, context.fCGFont);
820 CGContextSetFontSize(fCG, 1);
821 CGContextSetTextMatrix(fCG, context.fTransform);
822
823 CGContextSetAllowsFontSubpixelPositioning(fCG, context.fDoSubPosition);
824 CGContextSetShouldSubpixelPositionFonts(fCG, context.fDoSubPosition);
825
826 // Draw white on black to create mask.
827 // TODO: Draw black on white and invert, CG has a special case codepath.
828 CGContextSetGrayFillColor(fCG, 1.0f, 1.0f);
829
830 // force our checks below to happen
831 fDoAA = !doAA;
832 fDoLCD = !doLCD;
833 }
834
835 if (fDoAA != doAA) {
836 CGContextSetShouldAntialias(fCG, doAA);
837 fDoAA = doAA;
838 }
839 if (fDoLCD != doLCD) {
840 CGContextSetShouldSmoothFonts(fCG, doLCD);
841 fDoLCD = doLCD;
842 }
843
844 CGRGBPixel* image = (CGRGBPixel*)fImageStorage.get();
845 // skip rows based on the glyph's height
846 image += (fSize.fHeight - glyph.fHeight) * fSize.fWidth;
847
848 // erase to black
849 sk_memset_rect32(image, 0, glyph.fWidth, glyph.fHeight, rowBytes);
850
851 float subX = 0;
852 float subY = 0;
853 if (context.fDoSubPosition) {
854 subX = SkFixedToFloat(glyph.getSubXFixed());
855 subY = SkFixedToFloat(glyph.getSubYFixed());
856 }
857 if (context.fVertical) {
858 SkIPoint offset;
859 context.getVerticalOffset(glyphID, &offset);
860 subX += offset.fX;
861 subY += offset.fY;
862 }
863 CGContextShowGlyphsAtPoint(fCG, -glyph.fLeft + subX,
864 glyph.fTop + glyph.fHeight - subY,
865 &glyphID, 1);
866
867 SkASSERT(rowBytesPtr);
868 *rowBytesPtr = rowBytes;
869 return image;
870}
871
872void SkScalerContext_Mac::getVerticalOffset(CGGlyph glyphID, SkIPoint* offset) const {
873 CGSize vertOffset;
874 CTFontGetVerticalTranslationsForGlyphs(fCTVerticalFont, &glyphID, &vertOffset, 1);
875 const SkPoint trans = {CGToScalar(vertOffset.width),
876 CGToScalar(vertOffset.height)};
877 SkPoint floatOffset;
878 fVerticalMatrix.mapPoints(&floatOffset, &trans, 1);
879 if (!isSnowLeopard()) {
880 // SnowLeopard fails to apply the font's matrix to the vertical metrics,
881 // but Lion and Leopard do. The unit matrix describes the font's matrix at
882 // point size 1. There may be some way to avoid mapping here by setting up
883 // fVerticalMatrix differently, but this works for now.
884 fUnitMatrix.mapPoints(&floatOffset, 1);
885 }
886 offset->fX = SkScalarRound(floatOffset.fX);
887 offset->fY = SkScalarRound(floatOffset.fY);
888}
889
890uint16_t SkScalerContext_Mac::getFBoundingBoxesGlyphOffset() {
891 if (fFBoundingBoxesGlyphOffset) {
892 return fFBoundingBoxesGlyphOffset;
893 }
894 fFBoundingBoxesGlyphOffset = fGlyphCount; // fallback for all fonts
895 AutoCGTable<SkOTTableHorizontalHeader> hheaTable(fCGFont);
896 if (hheaTable.fData) {
897 fFBoundingBoxesGlyphOffset = SkEndian_SwapBE16(hheaTable->numberOfHMetrics);
898 }
899 return fFBoundingBoxesGlyphOffset;
900}
reed@android.com0680d6c2008-12-19 19:46:15 +0000901
reed@android.comfeda2f92010-05-19 13:47:05 +0000902/*
mike@reedtribe.orgb103ed42013-03-03 03:50:09 +0000903 * Lion has a bug in CTFontGetBoundingRectsForGlyphs which returns a bad value
904 * in theBounds.origin.x for fonts whose numOfLogHorMetrics is less than its
905 * glyph count. This workaround reads the glyph bounds from the font directly.
906 *
907 * The table is computed only if the font is a TrueType font, if the glyph
908 * value is >= fFBoundingBoxesGlyphOffset. (called only if fFBoundingBoxesGlyphOffset < fGlyphCount).
909 *
910 * TODO: A future optimization will compute fFBoundingBoxes once per CGFont, and
911 * compute fFBoundingBoxesMatrix once per font context.
912 */
913bool SkScalerContext_Mac::generateBBoxes() {
914 if (fGeneratedFBoundingBoxes) {
915 return NULL != fFBoundingBoxes;
916 }
917 fGeneratedFBoundingBoxes = true;
reed@android.com0bf64d42009-03-09 17:22:22 +0000918
mike@reedtribe.orgb103ed42013-03-03 03:50:09 +0000919 AutoCGTable<SkOTTableHead> headTable(fCGFont);
920 if (!headTable.fData) {
921 return false;
922 }
923
924 AutoCGTable<SkOTTableIndexToLocation> locaTable(fCGFont);
925 if (!locaTable.fData) {
926 return false;
927 }
928
929 AutoCGTable<SkOTTableGlyph> glyfTable(fCGFont);
930 if (!glyfTable.fData) {
931 return false;
932 }
933
934 uint16_t entries = fGlyphCount - fFBoundingBoxesGlyphOffset;
935 fFBoundingBoxes = new GlyphRect[entries];
936
937 SkOTTableHead::IndexToLocFormat locaFormat = headTable->indexToLocFormat;
938 SkOTTableGlyph::Iterator glyphDataIter(*glyfTable.fData, *locaTable.fData, locaFormat);
939 glyphDataIter.advance(fFBoundingBoxesGlyphOffset);
940 for (uint16_t boundingBoxesIndex = 0; boundingBoxesIndex < entries; ++boundingBoxesIndex) {
941 const SkOTTableGlyphData* glyphData = glyphDataIter.next();
942 GlyphRect& rect = fFBoundingBoxes[boundingBoxesIndex];
943 rect.fMinX = SkEndian_SwapBE16(glyphData->xMin);
944 rect.fMinY = SkEndian_SwapBE16(glyphData->yMin);
945 rect.fMaxX = SkEndian_SwapBE16(glyphData->xMax);
946 rect.fMaxY = SkEndian_SwapBE16(glyphData->yMax);
947 }
948 fFBoundingBoxesMatrix = fMatrix;
949 flip(&fFBoundingBoxesMatrix);
950 SkScalar fontScale = getFontScale(fCGFont);
951 fFBoundingBoxesMatrix.preScale(fontScale, fontScale);
952 return true;
953}
954
955unsigned SkScalerContext_Mac::generateGlyphCount(void) {
956 return fGlyphCount;
957}
958
959uint16_t SkScalerContext_Mac::generateCharToGlyph(SkUnichar uni) {
960 CGGlyph cgGlyph;
961 UniChar theChar;
962
963 // Validate our parameters and state
964 SkASSERT(uni <= 0x0000FFFF);
965 SkASSERT(sizeof(CGGlyph) <= sizeof(uint16_t));
966
967 // Get the glyph
968 theChar = (UniChar) uni;
969
970 if (!CTFontGetGlyphsForCharacters(fCTFont, &theChar, &cgGlyph, 1)) {
971 cgGlyph = 0;
972 }
973
974 return cgGlyph;
975}
976
977void SkScalerContext_Mac::generateAdvance(SkGlyph* glyph) {
978 this->generateMetrics(glyph);
979}
980
981void SkScalerContext_Mac::generateMetrics(SkGlyph* glyph) {
982 CGSize advance;
983 CGRect bounds;
984 CGGlyph cgGlyph;
985
986 // Get the state we need
987 cgGlyph = (CGGlyph) glyph->getGlyphID(fBaseGlyphCount);
988
989 if (fVertical) {
990 if (!isSnowLeopard()) {
991 // Lion and Leopard respect the vertical font metrics.
992 CTFontGetBoundingRectsForGlyphs(fCTVerticalFont, kCTFontVerticalOrientation,
993 &cgGlyph, &bounds, 1);
994 } else {
995 // Snow Leopard and earlier respect the vertical font metrics for
996 // advances, but not bounds, so use the default box and adjust it below.
997 CTFontGetBoundingRectsForGlyphs(fCTFont, kCTFontDefaultOrientation,
998 &cgGlyph, &bounds, 1);
999 }
1000 CTFontGetAdvancesForGlyphs(fCTVerticalFont, kCTFontVerticalOrientation,
1001 &cgGlyph, &advance, 1);
1002 } else {
1003 CTFontGetBoundingRectsForGlyphs(fCTFont, kCTFontDefaultOrientation,
1004 &cgGlyph, &bounds, 1);
1005 CTFontGetAdvancesForGlyphs(fCTFont, kCTFontDefaultOrientation,
1006 &cgGlyph, &advance, 1);
1007 }
1008
1009 // BUG?
1010 // 0x200B (zero-advance space) seems to return a huge (garbage) bounds, when
1011 // it should be empty. So, if we see a zero-advance, we check if it has an
1012 // empty path or not, and if so, we jam the bounds to 0. Hopefully a zero-advance
1013 // is rare, so we won't incur a big performance cost for this extra check.
1014 if (0 == advance.width && 0 == advance.height) {
1015 AutoCFRelease<CGPathRef> path(CTFontCreatePathForGlyph(fCTFont, cgGlyph, NULL));
1016 if (NULL == path || CGPathIsEmpty(path)) {
1017 bounds = CGRectMake(0, 0, 0, 0);
1018 }
1019 }
1020
1021 glyph->zeroMetrics();
1022 glyph->fAdvanceX = SkFloatToFixed_Check(advance.width);
1023 glyph->fAdvanceY = -SkFloatToFixed_Check(advance.height);
1024
1025 if (CGRectIsEmpty_inline(bounds)) {
1026 return;
1027 }
1028
1029 if (isLeopard() && !fVertical) {
1030 // Leopard does not consider the matrix skew in its bounds.
1031 // Run the bounding rectangle through the skew matrix to determine
1032 // the true bounds. However, this doesn't work if the font is vertical.
1033 // FIXME (Leopard): If the font has synthetic italic (e.g., matrix skew)
1034 // and the font is vertical, the bounds need to be recomputed.
1035 SkRect glyphBounds = SkRect::MakeXYWH(
1036 bounds.origin.x, bounds.origin.y,
1037 bounds.size.width, bounds.size.height);
1038 fUnitMatrix.mapRect(&glyphBounds);
1039 bounds.origin.x = glyphBounds.fLeft;
1040 bounds.origin.y = glyphBounds.fTop;
1041 bounds.size.width = glyphBounds.width();
1042 bounds.size.height = glyphBounds.height();
1043 }
1044 // Adjust the bounds
1045 //
1046 // CTFontGetBoundingRectsForGlyphs ignores the font transform, so we need
1047 // to transform the bounding box ourselves.
1048 //
1049 // The bounds are also expanded by 1 pixel, to give CG room for anti-aliasing.
1050 CGRectInset_inline(&bounds, -1, -1);
1051
1052 // Get the metrics
1053 bool lionAdjustedMetrics = false;
1054 if (isLion() || isMountainLion()) {
1055 if (cgGlyph < fGlyphCount && cgGlyph >= getFBoundingBoxesGlyphOffset() && generateBBoxes()){
1056 lionAdjustedMetrics = true;
1057 SkRect adjust;
1058 const GlyphRect& gRect = fFBoundingBoxes[cgGlyph - fFBoundingBoxesGlyphOffset];
1059 adjust.set(gRect.fMinX, gRect.fMinY, gRect.fMaxX, gRect.fMaxY);
1060 fFBoundingBoxesMatrix.mapRect(&adjust);
1061 bounds.origin.x = SkScalarToFloat(adjust.fLeft) - 1;
1062 bounds.origin.y = SkScalarToFloat(adjust.fTop) - 1;
1063 }
1064 // Lion returns fractions in the bounds
1065 glyph->fWidth = SkToU16(sk_float_ceil2int(bounds.size.width));
1066 glyph->fHeight = SkToU16(sk_float_ceil2int(bounds.size.height));
1067 } else {
1068 glyph->fWidth = SkToU16(sk_float_round2int(bounds.size.width));
1069 glyph->fHeight = SkToU16(sk_float_round2int(bounds.size.height));
1070 }
1071 glyph->fTop = SkToS16(-sk_float_round2int(CGRectGetMaxY_inline(bounds)));
1072 glyph->fLeft = SkToS16(sk_float_round2int(CGRectGetMinX_inline(bounds)));
1073 SkIPoint offset;
1074 if (fVertical && (isSnowLeopard() || lionAdjustedMetrics)) {
1075 // SnowLeopard doesn't respect vertical metrics, so compute them manually.
1076 // Also compute them for Lion when the metrics were computed by hand.
1077 getVerticalOffset(cgGlyph, &offset);
1078 glyph->fLeft += offset.fX;
1079 glyph->fTop += offset.fY;
1080 }
1081}
1082
1083#include "SkColorPriv.h"
1084
1085static void build_power_table(uint8_t table[], float ee) {
1086 for (int i = 0; i < 256; i++) {
1087 float x = i / 255.f;
1088 x = sk_float_pow(x, ee);
1089 int xx = SkScalarRoundToInt(SkFloatToScalar(x * 255));
1090 table[i] = SkToU8(xx);
1091 }
1092}
1093
1094/**
1095 * This will invert the gamma applied by CoreGraphics, so we can get linear
1096 * values.
1097 *
1098 * CoreGraphics obscurely defaults to 2.0 as the smoothing gamma value.
1099 * The color space used does not appear to affect this choice.
1100 */
1101static const uint8_t* getInverseGammaTableCoreGraphicSmoothing() {
1102 static bool gInited;
1103 static uint8_t gTableCoreGraphicsSmoothing[256];
1104 if (!gInited) {
1105 build_power_table(gTableCoreGraphicsSmoothing, 2.0f);
1106 gInited = true;
1107 }
1108 return gTableCoreGraphicsSmoothing;
1109}
1110
1111static void cgpixels_to_bits(uint8_t dst[], const CGRGBPixel src[], int count) {
1112 while (count > 0) {
1113 uint8_t mask = 0;
1114 for (int i = 7; i >= 0; --i) {
1115 mask |= (CGRGBPixel_getAlpha(*src++) >> 7) << i;
1116 if (0 == --count) {
1117 break;
1118 }
1119 }
1120 *dst++ = mask;
1121 }
1122}
1123
1124template<bool APPLY_PREBLEND>
1125static inline uint8_t rgb_to_a8(CGRGBPixel rgb, const uint8_t* table8) {
1126 U8CPU r = (rgb >> 16) & 0xFF;
1127 U8CPU g = (rgb >> 8) & 0xFF;
1128 U8CPU b = (rgb >> 0) & 0xFF;
1129 return sk_apply_lut_if<APPLY_PREBLEND>(SkComputeLuminance(r, g, b), table8);
1130}
1131template<bool APPLY_PREBLEND>
1132static void rgb_to_a8(const CGRGBPixel* SK_RESTRICT cgPixels, size_t cgRowBytes,
1133 const SkGlyph& glyph, const uint8_t* table8) {
1134 const int width = glyph.fWidth;
1135 size_t dstRB = glyph.rowBytes();
1136 uint8_t* SK_RESTRICT dst = (uint8_t*)glyph.fImage;
1137
1138 for (int y = 0; y < glyph.fHeight; y++) {
1139 for (int i = 0; i < width; ++i) {
1140 dst[i] = rgb_to_a8<APPLY_PREBLEND>(cgPixels[i], table8);
1141 }
1142 cgPixels = (CGRGBPixel*)((char*)cgPixels + cgRowBytes);
1143 dst += dstRB;
1144 }
1145}
1146
1147template<bool APPLY_PREBLEND>
1148static inline uint16_t rgb_to_lcd16(CGRGBPixel rgb, const uint8_t* tableR,
1149 const uint8_t* tableG,
1150 const uint8_t* tableB) {
1151 U8CPU r = sk_apply_lut_if<APPLY_PREBLEND>((rgb >> 16) & 0xFF, tableR);
1152 U8CPU g = sk_apply_lut_if<APPLY_PREBLEND>((rgb >> 8) & 0xFF, tableG);
1153 U8CPU b = sk_apply_lut_if<APPLY_PREBLEND>((rgb >> 0) & 0xFF, tableB);
1154 return SkPack888ToRGB16(r, g, b);
1155}
1156template<bool APPLY_PREBLEND>
1157static void rgb_to_lcd16(const CGRGBPixel* SK_RESTRICT cgPixels, size_t cgRowBytes, const SkGlyph& glyph,
1158 const uint8_t* tableR, const uint8_t* tableG, const uint8_t* tableB) {
1159 const int width = glyph.fWidth;
1160 size_t dstRB = glyph.rowBytes();
1161 uint16_t* SK_RESTRICT dst = (uint16_t*)glyph.fImage;
1162
1163 for (int y = 0; y < glyph.fHeight; y++) {
1164 for (int i = 0; i < width; i++) {
1165 dst[i] = rgb_to_lcd16<APPLY_PREBLEND>(cgPixels[i], tableR, tableG, tableB);
1166 }
1167 cgPixels = (CGRGBPixel*)((char*)cgPixels + cgRowBytes);
1168 dst = (uint16_t*)((char*)dst + dstRB);
1169 }
1170}
1171
1172template<bool APPLY_PREBLEND>
1173static inline uint32_t rgb_to_lcd32(CGRGBPixel rgb, const uint8_t* tableR,
1174 const uint8_t* tableG,
1175 const uint8_t* tableB) {
1176 U8CPU r = sk_apply_lut_if<APPLY_PREBLEND>((rgb >> 16) & 0xFF, tableR);
1177 U8CPU g = sk_apply_lut_if<APPLY_PREBLEND>((rgb >> 8) & 0xFF, tableG);
1178 U8CPU b = sk_apply_lut_if<APPLY_PREBLEND>((rgb >> 0) & 0xFF, tableB);
1179 return SkPackARGB32(0xFF, r, g, b);
1180}
1181template<bool APPLY_PREBLEND>
1182static void rgb_to_lcd32(const CGRGBPixel* SK_RESTRICT cgPixels, size_t cgRowBytes, const SkGlyph& glyph,
1183 const uint8_t* tableR, const uint8_t* tableG, const uint8_t* tableB) {
1184 const int width = glyph.fWidth;
1185 size_t dstRB = glyph.rowBytes();
1186 uint32_t* SK_RESTRICT dst = (uint32_t*)glyph.fImage;
1187 for (int y = 0; y < glyph.fHeight; y++) {
1188 for (int i = 0; i < width; i++) {
1189 dst[i] = rgb_to_lcd32<APPLY_PREBLEND>(cgPixels[i], tableR, tableG, tableB);
1190 }
1191 cgPixels = (CGRGBPixel*)((char*)cgPixels + cgRowBytes);
1192 dst = (uint32_t*)((char*)dst + dstRB);
1193 }
1194}
1195
1196template <typename T> T* SkTAddByteOffset(T* ptr, size_t byteOffset) {
1197 return (T*)((char*)ptr + byteOffset);
1198}
1199
1200void SkScalerContext_Mac::generateImage(const SkGlyph& glyph) {
1201 CGGlyph cgGlyph = (CGGlyph) glyph.getGlyphID(fBaseGlyphCount);
1202
1203 // FIXME: lcd smoothed un-hinted rasterization unsupported.
1204 bool generateA8FromLCD = fRec.getHinting() != SkPaint::kNo_Hinting;
1205
1206 // Draw the glyph
1207 size_t cgRowBytes;
1208 CGRGBPixel* cgPixels = fOffscreen.getCG(*this, glyph, cgGlyph, &cgRowBytes, generateA8FromLCD);
1209 if (cgPixels == NULL) {
1210 return;
1211 }
1212
1213 //TODO: see if drawing black on white and inverting is faster (at least in
1214 //lcd case) as core graphics appears to have special case code for drawing
1215 //black text.
1216
1217 // Fix the glyph
1218 const bool isLCD = isLCDFormat(glyph.fMaskFormat);
1219 if (isLCD || (glyph.fMaskFormat == SkMask::kA8_Format && supports_LCD() && generateA8FromLCD)) {
1220 const uint8_t* table = getInverseGammaTableCoreGraphicSmoothing();
1221
1222 //Note that the following cannot really be integrated into the
1223 //pre-blend, since we may not be applying the pre-blend; when we aren't
1224 //applying the pre-blend it means that a filter wants linear anyway.
1225 //Other code may also be applying the pre-blend, so we'd need another
1226 //one with this and one without.
1227 CGRGBPixel* addr = cgPixels;
1228 for (int y = 0; y < glyph.fHeight; ++y) {
1229 for (int x = 0; x < glyph.fWidth; ++x) {
1230 int r = (addr[x] >> 16) & 0xFF;
1231 int g = (addr[x] >> 8) & 0xFF;
1232 int b = (addr[x] >> 0) & 0xFF;
1233 addr[x] = (table[r] << 16) | (table[g] << 8) | table[b];
1234 }
1235 addr = SkTAddByteOffset(addr, cgRowBytes);
1236 }
1237 }
1238
1239 // Convert glyph to mask
1240 switch (glyph.fMaskFormat) {
1241 case SkMask::kLCD32_Format: {
1242 if (fPreBlend.isApplicable()) {
1243 rgb_to_lcd32<true>(cgPixels, cgRowBytes, glyph,
1244 fPreBlend.fR, fPreBlend.fG, fPreBlend.fB);
1245 } else {
1246 rgb_to_lcd32<false>(cgPixels, cgRowBytes, glyph,
1247 fPreBlend.fR, fPreBlend.fG, fPreBlend.fB);
1248 }
1249 } break;
1250 case SkMask::kLCD16_Format: {
1251 if (fPreBlend.isApplicable()) {
1252 rgb_to_lcd16<true>(cgPixels, cgRowBytes, glyph,
1253 fPreBlend.fR, fPreBlend.fG, fPreBlend.fB);
1254 } else {
1255 rgb_to_lcd16<false>(cgPixels, cgRowBytes, glyph,
1256 fPreBlend.fR, fPreBlend.fG, fPreBlend.fB);
1257 }
1258 } break;
1259 case SkMask::kA8_Format: {
1260 if (fPreBlend.isApplicable()) {
1261 rgb_to_a8<true>(cgPixels, cgRowBytes, glyph, fPreBlend.fG);
1262 } else {
1263 rgb_to_a8<false>(cgPixels, cgRowBytes, glyph, fPreBlend.fG);
1264 }
1265 } break;
1266 case SkMask::kBW_Format: {
1267 const int width = glyph.fWidth;
1268 size_t dstRB = glyph.rowBytes();
1269 uint8_t* dst = (uint8_t*)glyph.fImage;
1270 for (int y = 0; y < glyph.fHeight; y++) {
1271 cgpixels_to_bits(dst, cgPixels, width);
1272 cgPixels = (CGRGBPixel*)((char*)cgPixels + cgRowBytes);
1273 dst += dstRB;
1274 }
1275 } break;
1276 default:
1277 SkDEBUGFAIL("unexpected mask format");
1278 break;
1279 }
1280}
1281
1282/*
1283 * Our subpixel resolution is only 2 bits in each direction, so a scale of 4
1284 * seems sufficient, and possibly even correct, to allow the hinted outline
1285 * to be subpixel positioned.
1286 */
1287#define kScaleForSubPixelPositionHinting (4.0f)
1288
1289void SkScalerContext_Mac::generatePath(const SkGlyph& glyph, SkPath* path) {
1290 CTFontRef font = fCTFont;
1291 SkScalar scaleX = SK_Scalar1;
1292 SkScalar scaleY = SK_Scalar1;
1293
1294 /*
1295 * For subpixel positioning, we want to return an unhinted outline, so it
1296 * can be positioned nicely at fractional offsets. However, we special-case
1297 * if the baseline of the (horizontal) text is axis-aligned. In those cases
1298 * we want to retain hinting in the direction orthogonal to the baseline.
1299 * e.g. for horizontal baseline, we want to retain hinting in Y.
1300 * The way we remove hinting is to scale the font by some value (4) in that
1301 * direction, ask for the path, and then scale the path back down.
1302 */
1303 if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) {
1304 SkMatrix m;
1305 fRec.getSingleMatrix(&m);
1306
1307 // start out by assuming that we want no hining in X and Y
1308 scaleX = scaleY = SkFloatToScalar(kScaleForSubPixelPositionHinting);
1309 // now see if we need to restore hinting for axis-aligned baselines
1310 switch (SkComputeAxisAlignmentForHText(m)) {
1311 case kX_SkAxisAlignment:
1312 scaleY = SK_Scalar1; // want hinting in the Y direction
1313 break;
1314 case kY_SkAxisAlignment:
1315 scaleX = SK_Scalar1; // want hinting in the X direction
1316 break;
1317 default:
1318 break;
1319 }
1320
1321 CGAffineTransform xform = MatrixToCGAffineTransform(m, scaleX, scaleY);
1322 // need to release font when we're done
1323 font = CTFontCreateCopyWithAttributes(fCTFont, 1, &xform, NULL);
1324 }
1325
1326 CGGlyph cgGlyph = (CGGlyph)glyph.getGlyphID(fBaseGlyphCount);
1327 AutoCFRelease<CGPathRef> cgPath(CTFontCreatePathForGlyph(font, cgGlyph, NULL));
1328
1329 path->reset();
1330 if (cgPath != NULL) {
1331 CGPathApply(cgPath, path, SkScalerContext_Mac::CTPathElement);
1332 }
1333
1334 if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) {
1335 SkMatrix m;
1336 m.setScale(SkScalarInvert(scaleX), SkScalarInvert(scaleY));
1337 path->transform(m);
1338 // balance the call to CTFontCreateCopyWithAttributes
1339 CFSafeRelease(font);
1340 }
1341 if (fRec.fFlags & SkScalerContext::kVertical_Flag) {
1342 SkIPoint offset;
1343 getVerticalOffset(cgGlyph, &offset);
1344 path->offset(SkIntToScalar(offset.fX), SkIntToScalar(offset.fY));
1345 }
1346}
1347
1348void SkScalerContext_Mac::generateFontMetrics(SkPaint::FontMetrics* mx,
1349 SkPaint::FontMetrics* my) {
1350 CGRect theBounds = CTFontGetBoundingBox(fCTFont);
1351
1352 SkPaint::FontMetrics theMetrics;
1353 theMetrics.fTop = CGToScalar(-CGRectGetMaxY_inline(theBounds));
1354 theMetrics.fAscent = CGToScalar(-CTFontGetAscent(fCTFont));
1355 theMetrics.fDescent = CGToScalar( CTFontGetDescent(fCTFont));
1356 theMetrics.fBottom = CGToScalar(-CGRectGetMinY_inline(theBounds));
1357 theMetrics.fLeading = CGToScalar( CTFontGetLeading(fCTFont));
1358 theMetrics.fAvgCharWidth = CGToScalar( CGRectGetWidth_inline(theBounds));
1359 theMetrics.fXMin = CGToScalar( CGRectGetMinX_inline(theBounds));
1360 theMetrics.fXMax = CGToScalar( CGRectGetMaxX_inline(theBounds));
1361 theMetrics.fXHeight = CGToScalar( CTFontGetXHeight(fCTFont));
1362
1363 if (mx != NULL) {
1364 *mx = theMetrics;
1365 }
1366 if (my != NULL) {
1367 *my = theMetrics;
1368 }
1369}
1370
1371void SkScalerContext_Mac::CTPathElement(void *info, const CGPathElement *element) {
1372 SkPath* skPath = (SkPath*)info;
1373
1374 // Process the path element
1375 switch (element->type) {
1376 case kCGPathElementMoveToPoint:
1377 skPath->moveTo(element->points[0].x, -element->points[0].y);
1378 break;
1379
1380 case kCGPathElementAddLineToPoint:
1381 skPath->lineTo(element->points[0].x, -element->points[0].y);
1382 break;
1383
1384 case kCGPathElementAddQuadCurveToPoint:
1385 skPath->quadTo(element->points[0].x, -element->points[0].y,
1386 element->points[1].x, -element->points[1].y);
1387 break;
1388
1389 case kCGPathElementAddCurveToPoint:
1390 skPath->cubicTo(element->points[0].x, -element->points[0].y,
1391 element->points[1].x, -element->points[1].y,
1392 element->points[2].x, -element->points[2].y);
1393 break;
1394
1395 case kCGPathElementCloseSubpath:
1396 skPath->close();
1397 break;
1398
1399 default:
1400 SkDEBUGFAIL("Unknown path element!");
1401 break;
1402 }
1403}
1404
1405
1406///////////////////////////////////////////////////////////////////////////////
1407
1408// Returns NULL on failure
1409// Call must still manage its ownership of provider
1410static SkTypeface* create_from_dataProvider(CGDataProviderRef provider) {
1411 AutoCFRelease<CGFontRef> cg(CGFontCreateWithDataProvider(provider));
1412 if (NULL == cg) {
1413 return NULL;
1414 }
1415 CTFontRef ct = CTFontCreateWithGraphicsFont(cg, 0, NULL, NULL);
1416 return cg ? SkCreateTypefaceFromCTFont(ct) : NULL;
1417}
1418
1419SkTypeface* SkFontHost::CreateTypefaceFromStream(SkStream* stream) {
1420 AutoCFRelease<CGDataProviderRef> provider(SkCreateDataProviderFromStream(stream));
1421 if (NULL == provider) {
1422 return NULL;
1423 }
1424 return create_from_dataProvider(provider);
1425}
1426
1427SkTypeface* SkFontHost::CreateTypefaceFromFile(const char path[]) {
1428 AutoCFRelease<CGDataProviderRef> provider(CGDataProviderCreateWithFilename(path));
1429 if (NULL == provider) {
1430 return NULL;
1431 }
1432 return create_from_dataProvider(provider);
1433}
1434
1435// Web fonts added to the the CTFont registry do not return their character set.
1436// Iterate through the font in this case. The existing caller caches the result,
1437// so the performance impact isn't too bad.
1438static void populate_glyph_to_unicode_slow(CTFontRef ctFont, CFIndex glyphCount,
1439 SkTDArray<SkUnichar>* glyphToUnicode) {
1440 glyphToUnicode->setCount(glyphCount);
1441 SkUnichar* out = glyphToUnicode->begin();
1442 sk_bzero(out, glyphCount * sizeof(SkUnichar));
1443 UniChar unichar = 0;
1444 while (glyphCount > 0) {
1445 CGGlyph glyph;
1446 if (CTFontGetGlyphsForCharacters(ctFont, &unichar, &glyph, 1)) {
1447 out[glyph] = unichar;
1448 --glyphCount;
1449 }
1450 if (++unichar == 0) {
1451 break;
1452 }
1453 }
1454}
1455
1456// Construct Glyph to Unicode table.
1457// Unicode code points that require conjugate pairs in utf16 are not
1458// supported.
1459static void populate_glyph_to_unicode(CTFontRef ctFont, CFIndex glyphCount,
1460 SkTDArray<SkUnichar>* glyphToUnicode) {
1461 AutoCFRelease<CFCharacterSetRef> charSet(CTFontCopyCharacterSet(ctFont));
1462 if (!charSet) {
1463 populate_glyph_to_unicode_slow(ctFont, glyphCount, glyphToUnicode);
1464 return;
1465 }
1466
1467 AutoCFRelease<CFDataRef> bitmap(CFCharacterSetCreateBitmapRepresentation(kCFAllocatorDefault,
1468 charSet));
1469 if (!bitmap) {
1470 return;
1471 }
1472 CFIndex length = CFDataGetLength(bitmap);
1473 if (!length) {
1474 return;
1475 }
1476 if (length > 8192) {
1477 // TODO: Add support for Unicode above 0xFFFF
1478 // Consider only the BMP portion of the Unicode character points.
1479 // The bitmap may contain other planes, up to plane 16.
1480 // See http://developer.apple.com/library/ios/#documentation/CoreFoundation/Reference/CFCharacterSetRef/Reference/reference.html
1481 length = 8192;
1482 }
1483 const UInt8* bits = CFDataGetBytePtr(bitmap);
1484 glyphToUnicode->setCount(glyphCount);
1485 SkUnichar* out = glyphToUnicode->begin();
1486 sk_bzero(out, glyphCount * sizeof(SkUnichar));
1487 for (int i = 0; i < length; i++) {
1488 int mask = bits[i];
1489 if (!mask) {
1490 continue;
1491 }
1492 for (int j = 0; j < 8; j++) {
1493 CGGlyph glyph;
1494 UniChar unichar = static_cast<UniChar>((i << 3) + j);
1495 if (mask & (1 << j) && CTFontGetGlyphsForCharacters(ctFont, &unichar, &glyph, 1)) {
1496 out[glyph] = unichar;
1497 }
1498 }
1499 }
1500}
1501
1502static bool getWidthAdvance(CTFontRef ctFont, int gId, int16_t* data) {
1503 CGSize advance;
1504 advance.width = 0;
1505 CGGlyph glyph = gId;
1506 CTFontGetAdvancesForGlyphs(ctFont, kCTFontHorizontalOrientation, &glyph, &advance, 1);
1507 *data = sk_float_round2int(advance.width);
1508 return true;
1509}
1510
1511// we might move this into our CGUtils...
1512static void CFStringToSkString(CFStringRef src, SkString* dst) {
1513 // Reserve enough room for the worst-case string,
1514 // plus 1 byte for the trailing null.
1515 CFIndex length = CFStringGetMaximumSizeForEncoding(CFStringGetLength(src),
1516 kCFStringEncodingUTF8) + 1;
1517 dst->resize(length);
1518 CFStringGetCString(src, dst->writable_str(), length, kCFStringEncodingUTF8);
1519 // Resize to the actual UTF-8 length used, stripping the null character.
1520 dst->resize(strlen(dst->c_str()));
1521}
1522
reed@google.com2689f612013-03-20 20:01:47 +00001523SkAdvancedTypefaceMetrics* SkTypeface_Mac::onGetAdvancedTypefaceMetrics(
mike@reedtribe.orgb103ed42013-03-03 03:50:09 +00001524 SkAdvancedTypefaceMetrics::PerGlyphInfo perGlyphInfo,
1525 const uint32_t* glyphIDs,
reed@google.com2689f612013-03-20 20:01:47 +00001526 uint32_t glyphIDsCount) const {
1527
1528 CTFontRef originalCTFont = fFontRef.get();
mike@reedtribe.orgb103ed42013-03-03 03:50:09 +00001529 AutoCFRelease<CTFontRef> ctFont(CTFontCreateCopyWithAttributes(
1530 originalCTFont, CTFontGetUnitsPerEm(originalCTFont), NULL, NULL));
1531 SkAdvancedTypefaceMetrics* info = new SkAdvancedTypefaceMetrics;
1532
1533 {
1534 AutoCFRelease<CFStringRef> fontName(CTFontCopyPostScriptName(ctFont));
1535 CFStringToSkString(fontName, &info->fFontName);
1536 }
1537
1538 info->fMultiMaster = false;
1539 CFIndex glyphCount = CTFontGetGlyphCount(ctFont);
1540 info->fLastGlyphID = SkToU16(glyphCount - 1);
1541 info->fEmSize = CTFontGetUnitsPerEm(ctFont);
1542
1543 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kToUnicode_PerGlyphInfo) {
1544 populate_glyph_to_unicode(ctFont, glyphCount, &info->fGlyphToUnicode);
1545 }
1546
1547 info->fStyle = 0;
1548
1549 // If it's not a truetype font, mark it as 'other'. Assume that TrueType
1550 // fonts always have both glyf and loca tables. At the least, this is what
1551 // sfntly needs to subset the font. CTFontCopyAttribute() does not always
1552 // succeed in determining this directly.
reed@google.com2689f612013-03-20 20:01:47 +00001553 if (!this->getTableSize('glyf') || !this->getTableSize('loca')) {
mike@reedtribe.orgb103ed42013-03-03 03:50:09 +00001554 info->fType = SkAdvancedTypefaceMetrics::kOther_Font;
1555 info->fItalicAngle = 0;
1556 info->fAscent = 0;
1557 info->fDescent = 0;
1558 info->fStemV = 0;
1559 info->fCapHeight = 0;
1560 info->fBBox = SkIRect::MakeEmpty();
1561 return info;
1562 }
1563
1564 info->fType = SkAdvancedTypefaceMetrics::kTrueType_Font;
1565 CTFontSymbolicTraits symbolicTraits = CTFontGetSymbolicTraits(ctFont);
1566 if (symbolicTraits & kCTFontMonoSpaceTrait) {
1567 info->fStyle |= SkAdvancedTypefaceMetrics::kFixedPitch_Style;
1568 }
1569 if (symbolicTraits & kCTFontItalicTrait) {
1570 info->fStyle |= SkAdvancedTypefaceMetrics::kItalic_Style;
1571 }
1572 CTFontStylisticClass stylisticClass = symbolicTraits & kCTFontClassMaskTrait;
mike@reedtribe.orgb103ed42013-03-03 03:50:09 +00001573 if (stylisticClass >= kCTFontOldStyleSerifsClass && stylisticClass <= kCTFontSlabSerifsClass) {
1574 info->fStyle |= SkAdvancedTypefaceMetrics::kSerif_Style;
1575 } else if (stylisticClass & kCTFontScriptsClass) {
1576 info->fStyle |= SkAdvancedTypefaceMetrics::kScript_Style;
1577 }
1578 info->fItalicAngle = (int16_t) CTFontGetSlantAngle(ctFont);
1579 info->fAscent = (int16_t) CTFontGetAscent(ctFont);
1580 info->fDescent = (int16_t) CTFontGetDescent(ctFont);
1581 info->fCapHeight = (int16_t) CTFontGetCapHeight(ctFont);
1582 CGRect bbox = CTFontGetBoundingBox(ctFont);
1583
1584 SkRect r;
1585 r.set( CGToScalar(CGRectGetMinX_inline(bbox)), // Left
1586 CGToScalar(CGRectGetMaxY_inline(bbox)), // Top
1587 CGToScalar(CGRectGetMaxX_inline(bbox)), // Right
1588 CGToScalar(CGRectGetMinY_inline(bbox))); // Bottom
1589
1590 r.roundOut(&(info->fBBox));
1591
1592 // Figure out a good guess for StemV - Min width of i, I, !, 1.
1593 // This probably isn't very good with an italic font.
1594 int16_t min_width = SHRT_MAX;
1595 info->fStemV = 0;
1596 static const UniChar stem_chars[] = {'i', 'I', '!', '1'};
1597 const size_t count = sizeof(stem_chars) / sizeof(stem_chars[0]);
1598 CGGlyph glyphs[count];
1599 CGRect boundingRects[count];
1600 if (CTFontGetGlyphsForCharacters(ctFont, stem_chars, glyphs, count)) {
1601 CTFontGetBoundingRectsForGlyphs(ctFont, kCTFontHorizontalOrientation,
1602 glyphs, boundingRects, count);
1603 for (size_t i = 0; i < count; i++) {
1604 int16_t width = (int16_t) boundingRects[i].size.width;
1605 if (width > 0 && width < min_width) {
1606 min_width = width;
1607 info->fStemV = min_width;
1608 }
1609 }
1610 }
1611
1612 if (false) { // TODO: haven't figured out how to know if font is embeddable
1613 // (information is in the OS/2 table)
1614 info->fType = SkAdvancedTypefaceMetrics::kNotEmbeddable_Font;
1615 } else if (perGlyphInfo & SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo) {
1616 if (info->fStyle & SkAdvancedTypefaceMetrics::kFixedPitch_Style) {
1617 skia_advanced_typeface_metrics_utils::appendRange(&info->fGlyphWidths, 0);
1618 info->fGlyphWidths->fAdvance.append(1, &min_width);
1619 skia_advanced_typeface_metrics_utils::finishRange(info->fGlyphWidths.get(), 0,
1620 SkAdvancedTypefaceMetrics::WidthRange::kDefault);
1621 } else {
1622 info->fGlyphWidths.reset(
1623 skia_advanced_typeface_metrics_utils::getAdvanceData(ctFont.get(),
1624 glyphCount,
1625 glyphIDs,
1626 glyphIDsCount,
1627 &getWidthAdvance));
1628 }
1629 }
1630 return info;
1631}
1632
1633///////////////////////////////////////////////////////////////////////////////
1634
reed@google.comcc9aad52013-03-21 19:28:10 +00001635static SK_SFNT_ULONG get_font_type_tag(const SkTypeface_Mac* typeface) {
1636 CTFontRef ctFont = typeface->fFontRef.get();
mike@reedtribe.orgb103ed42013-03-03 03:50:09 +00001637 AutoCFRelease<CFNumberRef> fontFormatRef(
1638 static_cast<CFNumberRef>(CTFontCopyAttribute(ctFont, kCTFontFormatAttribute)));
1639 if (!fontFormatRef) {
1640 return 0;
1641 }
1642
1643 SInt32 fontFormatValue;
1644 if (!CFNumberGetValue(fontFormatRef, kCFNumberSInt32Type, &fontFormatValue)) {
1645 return 0;
1646 }
1647
1648 switch (fontFormatValue) {
1649 case kCTFontFormatOpenTypePostScript:
1650 return SkSFNTHeader::fontType_OpenTypeCFF::TAG;
1651 case kCTFontFormatOpenTypeTrueType:
1652 return SkSFNTHeader::fontType_WindowsTrueType::TAG;
1653 case kCTFontFormatTrueType:
1654 return SkSFNTHeader::fontType_MacTrueType::TAG;
1655 case kCTFontFormatPostScript:
1656 return SkSFNTHeader::fontType_PostScript::TAG;
1657 case kCTFontFormatBitmap:
1658 return SkSFNTHeader::fontType_MacTrueType::TAG;
1659 case kCTFontFormatUnrecognized:
1660 default:
1661 //CT seems to be unreliable in being able to obtain the type,
1662 //even if all we want is the first four bytes of the font resource.
1663 //Just the presence of the FontForge 'FFTM' table seems to throw it off.
1664 return SkSFNTHeader::fontType_WindowsTrueType::TAG;
1665 }
1666}
1667
reed@google.comcc9aad52013-03-21 19:28:10 +00001668SkStream* SkTypeface_Mac::onOpenStream(int* ttcIndex) const {
1669 SK_SFNT_ULONG fontType = get_font_type_tag(this);
mike@reedtribe.orgb103ed42013-03-03 03:50:09 +00001670 if (0 == fontType) {
1671 return NULL;
1672 }
1673
1674 // get table tags
reed@google.comcc9aad52013-03-21 19:28:10 +00001675 int numTables = this->countTables();
mike@reedtribe.orgb103ed42013-03-03 03:50:09 +00001676 SkTDArray<SkFontTableTag> tableTags;
1677 tableTags.setCount(numTables);
reed@google.comcc9aad52013-03-21 19:28:10 +00001678 this->getTableTags(tableTags.begin());
mike@reedtribe.orgb103ed42013-03-03 03:50:09 +00001679
1680 // calc total size for font, save sizes
1681 SkTDArray<size_t> tableSizes;
1682 size_t totalSize = sizeof(SkSFNTHeader) + sizeof(SkSFNTHeader::TableDirectoryEntry) * numTables;
1683 for (int tableIndex = 0; tableIndex < numTables; ++tableIndex) {
reed@google.comcc9aad52013-03-21 19:28:10 +00001684 size_t tableSize = this->getTableSize(tableTags[tableIndex]);
mike@reedtribe.orgb103ed42013-03-03 03:50:09 +00001685 totalSize += (tableSize + 3) & ~3;
1686 *tableSizes.append() = tableSize;
1687 }
1688
1689 // reserve memory for stream, and zero it (tables must be zero padded)
1690 SkMemoryStream* stream = new SkMemoryStream(totalSize);
1691 char* dataStart = (char*)stream->getMemoryBase();
1692 sk_bzero(dataStart, totalSize);
1693 char* dataPtr = dataStart;
1694
1695 // compute font header entries
1696 uint16_t entrySelector = 0;
1697 uint16_t searchRange = 1;
1698 while (searchRange < numTables >> 1) {
1699 entrySelector++;
1700 searchRange <<= 1;
1701 }
1702 searchRange <<= 4;
1703 uint16_t rangeShift = (numTables << 4) - searchRange;
1704
1705 // write font header
1706 SkSFNTHeader* header = (SkSFNTHeader*)dataPtr;
1707 header->fontType = fontType;
1708 header->numTables = SkEndian_SwapBE16(numTables);
1709 header->searchRange = SkEndian_SwapBE16(searchRange);
1710 header->entrySelector = SkEndian_SwapBE16(entrySelector);
1711 header->rangeShift = SkEndian_SwapBE16(rangeShift);
1712 dataPtr += sizeof(SkSFNTHeader);
1713
1714 // write tables
1715 SkSFNTHeader::TableDirectoryEntry* entry = (SkSFNTHeader::TableDirectoryEntry*)dataPtr;
1716 dataPtr += sizeof(SkSFNTHeader::TableDirectoryEntry) * numTables;
1717 for (int tableIndex = 0; tableIndex < numTables; ++tableIndex) {
1718 size_t tableSize = tableSizes[tableIndex];
reed@google.comcc9aad52013-03-21 19:28:10 +00001719 this->getTableData(tableTags[tableIndex], 0, tableSize, dataPtr);
mike@reedtribe.orgb103ed42013-03-03 03:50:09 +00001720 entry->tag = SkEndian_SwapBE32(tableTags[tableIndex]);
1721 entry->checksum = SkEndian_SwapBE32(SkOTUtils::CalcTableChecksum((SK_OT_ULONG*)dataPtr,
1722 tableSize));
1723 entry->offset = SkEndian_SwapBE32(dataPtr - dataStart);
1724 entry->logicalLength = SkEndian_SwapBE32(tableSize);
1725
1726 dataPtr += (tableSize + 3) & ~3;
1727 ++entry;
1728 }
1729
1730 return stream;
1731}
1732
mike@reedtribe.orgb103ed42013-03-03 03:50:09 +00001733///////////////////////////////////////////////////////////////////////////////
mike@reedtribe.orgb103ed42013-03-03 03:50:09 +00001734///////////////////////////////////////////////////////////////////////////////
1735
1736int SkTypeface_Mac::onGetUPEM() const {
1737 AutoCFRelease<CGFontRef> cgFont(CTFontCopyGraphicsFont(fFontRef, NULL));
1738 return CGFontGetUnitsPerEm(cgFont);
1739}
1740
1741// If, as is the case with web fonts, the CTFont data isn't available,
1742// the CGFont data may work. While the CGFont may always provide the
1743// right result, leave the CTFont code path to minimize disruption.
1744static CFDataRef copyTableFromFont(CTFontRef ctFont, SkFontTableTag tag) {
1745 CFDataRef data = CTFontCopyTable(ctFont, (CTFontTableTag) tag,
1746 kCTFontTableOptionNoOptions);
1747 if (NULL == data) {
1748 AutoCFRelease<CGFontRef> cgFont(CTFontCopyGraphicsFont(ctFont, NULL));
1749 data = CGFontCopyTableForTag(cgFont, tag);
1750 }
1751 return data;
1752}
1753
1754int SkTypeface_Mac::onGetTableTags(SkFontTableTag tags[]) const {
1755 AutoCFRelease<CFArrayRef> cfArray(CTFontCopyAvailableTables(fFontRef,
1756 kCTFontTableOptionNoOptions));
1757 if (NULL == cfArray) {
1758 return 0;
1759 }
1760 int count = CFArrayGetCount(cfArray);
1761 if (tags) {
1762 for (int i = 0; i < count; ++i) {
1763 uintptr_t fontTag = reinterpret_cast<uintptr_t>(CFArrayGetValueAtIndex(cfArray, i));
1764 tags[i] = static_cast<SkFontTableTag>(fontTag);
1765 }
1766 }
1767 return count;
1768}
1769
1770size_t SkTypeface_Mac::onGetTableData(SkFontTableTag tag, size_t offset,
1771 size_t length, void* dstData) const {
1772 AutoCFRelease<CFDataRef> srcData(copyTableFromFont(fFontRef, tag));
1773 if (NULL == srcData) {
1774 return 0;
1775 }
1776
1777 size_t srcSize = CFDataGetLength(srcData);
1778 if (offset >= srcSize) {
1779 return 0;
1780 }
1781 if (length > srcSize - offset) {
1782 length = srcSize - offset;
1783 }
1784 if (dstData) {
1785 memcpy(dstData, CFDataGetBytePtr(srcData) + offset, length);
1786 }
1787 return length;
1788}
1789
1790SkScalerContext* SkTypeface_Mac::onCreateScalerContext(const SkDescriptor* desc) const {
reed@google.com0da48612013-03-19 16:06:52 +00001791 return new SkScalerContext_Mac(const_cast<SkTypeface_Mac*>(this), desc);
mike@reedtribe.orgb103ed42013-03-03 03:50:09 +00001792}
1793
1794void SkTypeface_Mac::onFilterRec(SkScalerContextRec* rec) const {
1795 unsigned flagsWeDontSupport = SkScalerContext::kDevKernText_Flag |
1796 SkScalerContext::kAutohinting_Flag;
skia.committer@gmail.com0c23faf2013-03-03 07:16:29 +00001797
mike@reedtribe.orgb103ed42013-03-03 03:50:09 +00001798 rec->fFlags &= ~flagsWeDontSupport;
skia.committer@gmail.com0c23faf2013-03-03 07:16:29 +00001799
mike@reedtribe.orgb103ed42013-03-03 03:50:09 +00001800 bool lcdSupport = supports_LCD();
skia.committer@gmail.com0c23faf2013-03-03 07:16:29 +00001801
mike@reedtribe.orgb103ed42013-03-03 03:50:09 +00001802 // Only two levels of hinting are supported.
1803 // kNo_Hinting means avoid CoreGraphics outline dilation.
1804 // kNormal_Hinting means CoreGraphics outline dilation is allowed.
1805 // If there is no lcd support, hinting (dilation) cannot be supported.
1806 SkPaint::Hinting hinting = rec->getHinting();
1807 if (SkPaint::kSlight_Hinting == hinting || !lcdSupport) {
1808 hinting = SkPaint::kNo_Hinting;
1809 } else if (SkPaint::kFull_Hinting == hinting) {
1810 hinting = SkPaint::kNormal_Hinting;
1811 }
1812 rec->setHinting(hinting);
skia.committer@gmail.com0c23faf2013-03-03 07:16:29 +00001813
mike@reedtribe.orgb103ed42013-03-03 03:50:09 +00001814 // FIXME: lcd smoothed un-hinted rasterization unsupported.
1815 // Tracked by http://code.google.com/p/skia/issues/detail?id=915 .
1816 // There is no current means to honor a request for unhinted lcd,
1817 // so arbitrarilly ignore the hinting request and honor lcd.
skia.committer@gmail.com0c23faf2013-03-03 07:16:29 +00001818
mike@reedtribe.orgb103ed42013-03-03 03:50:09 +00001819 // Hinting and smoothing should be orthogonal, but currently they are not.
1820 // CoreGraphics has no API to influence hinting. However, its lcd smoothed
1821 // output is drawn from auto-dilated outlines (the amount of which is
1822 // determined by AppleFontSmoothing). Its regular anti-aliased output is
1823 // drawn from un-dilated outlines.
skia.committer@gmail.com0c23faf2013-03-03 07:16:29 +00001824
mike@reedtribe.orgb103ed42013-03-03 03:50:09 +00001825 // The behavior of Skia is as follows:
1826 // [AA][no-hint]: generate AA using CoreGraphic's AA output.
1827 // [AA][yes-hint]: use CoreGraphic's LCD output and reduce it to a single
1828 // channel. This matches [LCD][yes-hint] in weight.
1829 // [LCD][no-hint]: curently unable to honor, and must pick which to respect.
1830 // Currenly side with LCD, effectively ignoring the hinting setting.
1831 // [LCD][yes-hint]: generate LCD using CoreGraphic's LCD output.
skia.committer@gmail.com0c23faf2013-03-03 07:16:29 +00001832
mike@reedtribe.orgb103ed42013-03-03 03:50:09 +00001833 if (isLCDFormat(rec->fMaskFormat)) {
1834 if (lcdSupport) {
1835 //CoreGraphics creates 555 masks for smoothed text anyway.
1836 rec->fMaskFormat = SkMask::kLCD16_Format;
1837 rec->setHinting(SkPaint::kNormal_Hinting);
1838 } else {
1839 rec->fMaskFormat = SkMask::kA8_Format;
1840 }
1841 }
skia.committer@gmail.com0c23faf2013-03-03 07:16:29 +00001842
mike@reedtribe.orgb103ed42013-03-03 03:50:09 +00001843 // Unhinted A8 masks (those not derived from LCD masks) must respect SK_GAMMA_APPLY_TO_A8.
1844 // All other masks can use regular gamma.
1845 if (SkMask::kA8_Format == rec->fMaskFormat && SkPaint::kNo_Hinting == hinting) {
1846#ifndef SK_GAMMA_APPLY_TO_A8
1847 rec->ignorePreBlend();
reed@android.comfeda2f92010-05-19 13:47:05 +00001848#endif
mike@reedtribe.orgb103ed42013-03-03 03:50:09 +00001849 } else {
1850 //CoreGraphics dialates smoothed text as needed.
1851 rec->setContrast(0);
1852 }
1853}
1854
1855// we take ownership of the ref
1856static const char* get_str(CFStringRef ref, SkString* str) {
1857 CFStringToSkString(ref, str);
1858 CFSafeRelease(ref);
1859 return str->c_str();
1860}
1861
reed@google.com5526ede2013-03-25 13:03:37 +00001862void SkTypeface_Mac::onGetFontDescriptor(SkFontDescriptor* desc,
1863 bool* isLocalStream) const {
mike@reedtribe.orgb103ed42013-03-03 03:50:09 +00001864 SkString tmpStr;
skia.committer@gmail.com0c23faf2013-03-03 07:16:29 +00001865
mike@reedtribe.orgb103ed42013-03-03 03:50:09 +00001866 desc->setFamilyName(get_str(CTFontCopyFamilyName(fFontRef), &tmpStr));
1867 desc->setFullName(get_str(CTFontCopyFullName(fFontRef), &tmpStr));
1868 desc->setPostscriptName(get_str(CTFontCopyPostScriptName(fFontRef), &tmpStr));
reed@google.com5526ede2013-03-25 13:03:37 +00001869 // TODO: need to add support for local-streams (here and openStream)
1870 *isLocalStream = false;
mike@reedtribe.orgb103ed42013-03-03 03:50:09 +00001871}
reed@google.com5526ede2013-03-25 13:03:37 +00001872
reed@google.com95625db2013-03-25 20:44:02 +00001873///////////////////////////////////////////////////////////////////////////////
1874///////////////////////////////////////////////////////////////////////////////
reed@google.com95625db2013-03-25 20:44:02 +00001875#if 1
reed@google.com83787c52013-03-26 17:19:15 +00001876
1877static bool find_desc_str(CTFontDescriptorRef desc, CFStringRef name, SkString* value) {
1878 AutoCFRelease<CFStringRef> ref((CFStringRef)CTFontDescriptorCopyAttribute(desc, name));
1879 if (NULL == ref.get()) {
1880 return false;
1881 }
1882 CFStringToSkString(ref, value);
1883 return true;
1884}
1885
1886static bool find_dict_float(CFDictionaryRef dict, CFStringRef name, float* value) {
1887 CFNumberRef num;
1888 return CFDictionaryGetValueIfPresent(dict, name, (const void**)&num)
1889 && CFNumberIsFloatType(num)
1890 && CFNumberGetValue(num, kCFNumberFloatType, value);
1891}
1892
reed@google.com95625db2013-03-25 20:44:02 +00001893#include "SkFontMgr.h"
1894
reed@google.com83787c52013-03-26 17:19:15 +00001895static int unit_weight_to_fontstyle(float unit) {
1896 float value;
1897 if (unit < 0) {
1898 value = 100 + (1 + unit) * 300;
1899 } else {
1900 value = 400 + unit * 500;
1901 }
1902 return sk_float_round2int(value);
1903}
1904
1905static int unit_width_to_fontstyle(float unit) {
1906 float value;
1907 if (unit < 0) {
1908 value = 1 + (1 + unit) * 4;
1909 } else {
1910 value = 5 + unit * 4;
1911 }
1912 return sk_float_round2int(value);
1913}
1914
1915static SkFontStyle desc2fontstyle(CTFontDescriptorRef desc) {
1916 AutoCFRelease<CFDictionaryRef> dict(
1917 (CFDictionaryRef)CTFontDescriptorCopyAttribute(desc,
1918 kCTFontTraitsAttribute));
1919 if (NULL == dict.get()) {
1920 return SkFontStyle();
1921 }
1922
1923 float weight, width, slant;
1924 if (!find_dict_float(dict, kCTFontWeightTrait, &weight)) {
1925 weight = 0;
1926 }
1927 if (!find_dict_float(dict, kCTFontWidthTrait, &width)) {
1928 width = 0;
1929 }
1930 if (!find_dict_float(dict, kCTFontSlantTrait, &slant)) {
1931 slant = 0;
1932 }
skia.committer@gmail.com37cbc7f2013-03-27 07:01:04 +00001933
reed@google.com83787c52013-03-26 17:19:15 +00001934 return SkFontStyle(unit_weight_to_fontstyle(weight),
1935 unit_width_to_fontstyle(width),
1936 slant ? SkFontStyle::kItalic_Slant
1937 : SkFontStyle::kUpright_Slant);
1938}
1939
reed@google.comce8b3de2013-03-26 19:30:16 +00001940static SkTypeface* createFromDesc(CFStringRef cfFamilyName,
1941 CTFontDescriptorRef desc) {
1942 AutoCFRelease<CTFontRef> ctNamed(CTFontCreateWithName(cfFamilyName, 1, NULL));
1943 CTFontRef ctFont = CTFontCreateCopyWithAttributes(ctNamed, 1, NULL, desc);
1944 if (NULL == ctFont) {
1945 return NULL;
1946 }
skia.committer@gmail.com37cbc7f2013-03-27 07:01:04 +00001947
reed@google.comce8b3de2013-03-26 19:30:16 +00001948 SkString str;
1949 CFStringToSkString(cfFamilyName, &str);
skia.committer@gmail.com37cbc7f2013-03-27 07:01:04 +00001950
reed@google.comce8b3de2013-03-26 19:30:16 +00001951 bool isFixedPitch;
1952 (void)computeStyleBits(ctFont, &isFixedPitch);
1953 SkFontID fontID = CTFontRef_to_SkFontID(ctFont);
skia.committer@gmail.com37cbc7f2013-03-27 07:01:04 +00001954
reed@google.comce8b3de2013-03-26 19:30:16 +00001955 return new SkTypeface_Mac(desc2fontstyle(desc), fontID, isFixedPitch,
1956 ctFont, str.c_str());
1957}
1958
reed@google.com83787c52013-03-26 17:19:15 +00001959class SkFontStyleSet_Mac : public SkFontStyleSet {
reed@google.com95625db2013-03-25 20:44:02 +00001960public:
reed@google.com83787c52013-03-26 17:19:15 +00001961 SkFontStyleSet_Mac(CFStringRef familyName, CTFontDescriptorRef desc)
1962 : fArray(CTFontDescriptorCreateMatchingFontDescriptors(desc, NULL))
1963 , fFamilyName(familyName) {
1964 CFRetain(familyName);
1965 }
skia.committer@gmail.com37cbc7f2013-03-27 07:01:04 +00001966
reed@google.com83787c52013-03-26 17:19:15 +00001967 virtual ~SkFontStyleSet_Mac() {
1968 CFSafeRelease(fArray);
1969 CFRelease(fFamilyName);
1970 }
1971
1972 virtual int count() SK_OVERRIDE {
1973 return CFArrayGetCount(fArray);
1974 }
1975
1976 virtual void getStyle(int index, SkFontStyle* style,
1977 SkString* name) SK_OVERRIDE {
1978 SkASSERT((unsigned)index < (unsigned)CFArrayGetCount(fArray));
1979 CTFontDescriptorRef desc = (CTFontDescriptorRef)CFArrayGetValueAtIndex(fArray, index);
1980 if (style) {
1981 *style = desc2fontstyle(desc);
1982 }
1983 if (name) {
1984 if (!find_desc_str(desc, kCTFontStyleNameAttribute, name)) {
1985 name->reset();
1986 }
1987 }
1988 }
1989
1990 virtual SkTypeface* createTypeface(int index) SK_OVERRIDE {
1991 SkASSERT((unsigned)index < (unsigned)CFArrayGetCount(fArray));
1992 CTFontDescriptorRef desc = (CTFontDescriptorRef)CFArrayGetValueAtIndex(fArray, index);
skia.committer@gmail.com37cbc7f2013-03-27 07:01:04 +00001993
reed@google.com83787c52013-03-26 17:19:15 +00001994 return createFromDesc(fFamilyName, desc);
1995 }
skia.committer@gmail.com37cbc7f2013-03-27 07:01:04 +00001996
reed@google.com83787c52013-03-26 17:19:15 +00001997private:
1998 CFArrayRef fArray;
1999 CFStringRef fFamilyName;
2000};
2001
2002class SkFontMgr_Mac : public SkFontMgr {
2003 int fCount;
2004 CFArrayRef fNames;
2005
2006 CFStringRef stringAt(int index) const {
2007 SkASSERT((unsigned)index < (unsigned)fCount);
2008 return (CFStringRef)CFArrayGetValueAtIndex(fNames, index);
2009 }
2010
2011 void lazyInit() {
2012 if (NULL == fNames) {
reed@google.com3dcbd462013-03-27 13:56:34 +00002013 fNames = SkCTFontManagerCopyAvailableFontFamilyNames();
reed@google.com83787c52013-03-26 17:19:15 +00002014 fCount = fNames ? CFArrayGetCount(fNames) : 0;
2015 }
2016 }
2017
2018public:
2019 SkFontMgr_Mac() : fCount(0), fNames(NULL) {}
2020
2021 virtual ~SkFontMgr_Mac() {
2022 CFSafeRelease(fNames);
2023 }
reed@google.com95625db2013-03-25 20:44:02 +00002024
2025protected:
2026 virtual int onCountFamilies() SK_OVERRIDE {
reed@google.com83787c52013-03-26 17:19:15 +00002027 this->lazyInit();
2028 return fCount;
reed@google.com95625db2013-03-25 20:44:02 +00002029 }
2030
2031 virtual void onGetFamilyName(int index, SkString* familyName) SK_OVERRIDE {
reed@google.com83787c52013-03-26 17:19:15 +00002032 this->lazyInit();
2033 if ((unsigned)index < (unsigned)fCount) {
2034 CFStringToSkString(this->stringAt(index), familyName);
2035 } else {
2036 familyName->reset();
2037 }
reed@google.com95625db2013-03-25 20:44:02 +00002038 }
2039
2040 virtual SkFontStyleSet* onCreateStyleSet(int index) SK_OVERRIDE {
reed@google.com83787c52013-03-26 17:19:15 +00002041 this->lazyInit();
2042 if ((unsigned)index >= (unsigned)fCount) {
2043 return NULL;
2044 }
2045
2046 AutoCFRelease<CFMutableDictionaryRef> cfAttr(
2047 CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
2048 &kCFTypeDictionaryKeyCallBacks,
2049 &kCFTypeDictionaryValueCallBacks));
skia.committer@gmail.com37cbc7f2013-03-27 07:01:04 +00002050
reed@google.com83787c52013-03-26 17:19:15 +00002051 CFDictionaryAddValue(cfAttr, kCTFontFamilyNameAttribute,
2052 this->stringAt(index));
skia.committer@gmail.com37cbc7f2013-03-27 07:01:04 +00002053
reed@google.com83787c52013-03-26 17:19:15 +00002054 AutoCFRelease<CTFontDescriptorRef> desc(
2055 CTFontDescriptorCreateWithAttributes(cfAttr));
2056 return SkNEW_ARGS(SkFontStyleSet_Mac, (this->stringAt(index), desc));
reed@google.com95625db2013-03-25 20:44:02 +00002057 }
skia.committer@gmail.come60ed082013-03-26 07:01:04 +00002058
reed@google.com95625db2013-03-25 20:44:02 +00002059 virtual SkTypeface* onMatchFamilyStyle(const char familyName[],
2060 const SkFontStyle&) SK_OVERRIDE {
2061 return NULL;
2062 }
skia.committer@gmail.come60ed082013-03-26 07:01:04 +00002063
reed@google.com95625db2013-03-25 20:44:02 +00002064 virtual SkTypeface* onMatchFaceStyle(const SkTypeface* familyMember,
2065 const SkFontStyle&) SK_OVERRIDE {
2066 return NULL;
2067 }
skia.committer@gmail.come60ed082013-03-26 07:01:04 +00002068
reed@google.com95625db2013-03-25 20:44:02 +00002069 virtual SkTypeface* onCreateFromData(SkData* data,
2070 int ttcIndex) SK_OVERRIDE {
2071 AutoCFRelease<CGDataProviderRef> pr(SkCreateDataProviderFromData(data));
2072 if (NULL == pr) {
2073 return NULL;
2074 }
2075 return create_from_dataProvider(pr);
2076 }
2077
2078 virtual SkTypeface* onCreateFromStream(SkStream* stream,
2079 int ttcIndex) SK_OVERRIDE {
2080 AutoCFRelease<CGDataProviderRef> pr(SkCreateDataProviderFromStream(stream));
2081 if (NULL == pr) {
2082 return NULL;
2083 }
2084 return create_from_dataProvider(pr);
2085 }
2086
2087 virtual SkTypeface* onCreateFromFile(const char path[],
2088 int ttcIndex) SK_OVERRIDE {
2089 AutoCFRelease<CGDataProviderRef> pr(CGDataProviderCreateWithFilename(path));
2090 if (NULL == pr) {
2091 return NULL;
2092 }
2093 return create_from_dataProvider(pr);
2094 }
2095};
2096
2097SkFontMgr* SkFontMgr::Factory() {
2098 return SkNEW(SkFontMgr_Mac);
2099}
2100#endif