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