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> |
reed@google.com | 3dcbd46 | 2013-03-27 13:56:34 +0000 | [diff] [blame] | 16 | #include <CoreText/CTFontManager.h> |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 17 | #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.com | ce8b3de | 2013-03-26 19:30:16 +0000 | [diff] [blame] | 44 | #include "SkFontMgr.h" |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 45 | #include "SkUtils.h" |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 46 | |
reed@google.com | f77b35d | 2013-05-02 20:39:44 +0000 | [diff] [blame] | 47 | //#define HACK_COLORGLYPHS |
| 48 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 49 | class SkScalerContext_Mac; |
| 50 | |
reed@google.com | 3dcbd46 | 2013-03-27 13:56:34 +0000 | [diff] [blame] | 51 | // CTFontManagerCopyAvailableFontFamilyNames() is not always available, so we |
| 52 | // provide a wrapper here that will return an empty array if need be. |
| 53 | static CFArrayRef SkCTFontManagerCopyAvailableFontFamilyNames() { |
| 54 | #ifdef SK_BUILD_FOR_IOS |
| 55 | return CFArrayCreate(NULL, NULL, 0, NULL); |
| 56 | #else |
| 57 | return CTFontManagerCopyAvailableFontFamilyNames(); |
| 58 | #endif |
| 59 | } |
| 60 | |
| 61 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 62 | // Being templated and taking const T* prevents calling |
| 63 | // CFSafeRelease(autoCFRelease) through implicit conversion. |
| 64 | template <typename T> static void CFSafeRelease(/*CFTypeRef*/const T* cfTypeRef) { |
| 65 | if (cfTypeRef) { |
| 66 | CFRelease(cfTypeRef); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | // Being templated and taking const T* prevents calling |
| 71 | // CFSafeRetain(autoCFRelease) through implicit conversion. |
| 72 | template <typename T> static void CFSafeRetain(/*CFTypeRef*/const T* cfTypeRef) { |
| 73 | if (cfTypeRef) { |
| 74 | CFRetain(cfTypeRef); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /** Acts like a CFRef, but calls CFSafeRelease when it goes out of scope. */ |
| 79 | template<typename CFRef> class AutoCFRelease : private SkNoncopyable { |
| 80 | public: |
| 81 | explicit AutoCFRelease(CFRef cfRef = NULL) : fCFRef(cfRef) { } |
| 82 | ~AutoCFRelease() { CFSafeRelease(fCFRef); } |
| 83 | |
| 84 | void reset(CFRef that = NULL) { |
| 85 | CFSafeRetain(that); |
| 86 | CFSafeRelease(fCFRef); |
| 87 | fCFRef = that; |
| 88 | } |
| 89 | |
| 90 | AutoCFRelease& operator =(CFRef that) { |
| 91 | reset(that); |
| 92 | return *this; |
| 93 | } |
| 94 | |
| 95 | operator CFRef() const { return fCFRef; } |
| 96 | CFRef get() const { return fCFRef; } |
| 97 | |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 98 | CFRef* operator&() { SkASSERT(fCFRef == NULL); return &fCFRef; } |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 99 | private: |
| 100 | CFRef fCFRef; |
| 101 | }; |
| 102 | |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 103 | static CFStringRef make_CFString(const char str[]) { |
| 104 | return CFStringCreateWithCString(NULL, str, kCFStringEncodingUTF8); |
| 105 | } |
| 106 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 107 | template<typename T> class AutoCGTable : SkNoncopyable { |
| 108 | public: |
| 109 | AutoCGTable(CGFontRef font) |
| 110 | //Undocumented: the tag parameter in this call is expected in machine order and not BE order. |
| 111 | : fCFData(CGFontCopyTableForTag(font, SkSetFourByteTag(T::TAG0, T::TAG1, T::TAG2, T::TAG3))) |
| 112 | , fData(fCFData ? reinterpret_cast<const T*>(CFDataGetBytePtr(fCFData)) : NULL) |
| 113 | { } |
| 114 | |
| 115 | const T* operator->() const { return fData; } |
| 116 | |
| 117 | private: |
| 118 | AutoCFRelease<CFDataRef> fCFData; |
| 119 | public: |
| 120 | const T* fData; |
| 121 | }; |
| 122 | |
| 123 | // inline versions of these rect helpers |
| 124 | |
| 125 | static bool CGRectIsEmpty_inline(const CGRect& rect) { |
| 126 | return rect.size.width <= 0 || rect.size.height <= 0; |
| 127 | } |
| 128 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 129 | static CGFloat CGRectGetMinX_inline(const CGRect& rect) { |
| 130 | return rect.origin.x; |
| 131 | } |
| 132 | |
| 133 | static CGFloat CGRectGetMaxX_inline(const CGRect& rect) { |
| 134 | return rect.origin.x + rect.size.width; |
| 135 | } |
| 136 | |
| 137 | static CGFloat CGRectGetMinY_inline(const CGRect& rect) { |
| 138 | return rect.origin.y; |
| 139 | } |
| 140 | |
| 141 | static CGFloat CGRectGetMaxY_inline(const CGRect& rect) { |
| 142 | return rect.origin.y + rect.size.height; |
| 143 | } |
| 144 | |
| 145 | static CGFloat CGRectGetWidth_inline(const CGRect& rect) { |
| 146 | return rect.size.width; |
| 147 | } |
| 148 | |
| 149 | /////////////////////////////////////////////////////////////////////////////// |
| 150 | |
| 151 | static void sk_memset_rect32(uint32_t* ptr, uint32_t value, |
| 152 | size_t width, size_t height, size_t rowBytes) { |
| 153 | SkASSERT(width); |
| 154 | SkASSERT(width * sizeof(uint32_t) <= rowBytes); |
| 155 | |
| 156 | if (width >= 32) { |
| 157 | while (height) { |
| 158 | sk_memset32(ptr, value, width); |
| 159 | ptr = (uint32_t*)((char*)ptr + rowBytes); |
| 160 | height -= 1; |
| 161 | } |
| 162 | return; |
| 163 | } |
| 164 | |
| 165 | rowBytes -= width * sizeof(uint32_t); |
| 166 | |
| 167 | if (width >= 8) { |
| 168 | while (height) { |
| 169 | int w = width; |
| 170 | do { |
| 171 | *ptr++ = value; *ptr++ = value; |
| 172 | *ptr++ = value; *ptr++ = value; |
| 173 | *ptr++ = value; *ptr++ = value; |
| 174 | *ptr++ = value; *ptr++ = value; |
| 175 | w -= 8; |
| 176 | } while (w >= 8); |
| 177 | while (--w >= 0) { |
| 178 | *ptr++ = value; |
| 179 | } |
| 180 | ptr = (uint32_t*)((char*)ptr + rowBytes); |
| 181 | height -= 1; |
| 182 | } |
| 183 | } else { |
| 184 | while (height) { |
| 185 | int w = width; |
| 186 | do { |
| 187 | *ptr++ = value; |
| 188 | } while (--w > 0); |
| 189 | ptr = (uint32_t*)((char*)ptr + rowBytes); |
| 190 | height -= 1; |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | #include <sys/utsname.h> |
| 196 | |
| 197 | typedef uint32_t CGRGBPixel; |
| 198 | |
| 199 | static unsigned CGRGBPixel_getAlpha(CGRGBPixel pixel) { |
| 200 | return pixel & 0xFF; |
| 201 | } |
| 202 | |
| 203 | // The calls to support subpixel are present in 10.5, but are not included in |
| 204 | // the 10.5 SDK. The needed calls have been extracted from the 10.6 SDK and are |
| 205 | // included below. To verify that CGContextSetShouldSubpixelQuantizeFonts, for |
| 206 | // instance, is present in the 10.5 CoreGraphics libary, use: |
| 207 | // cd /Developer/SDKs/MacOSX10.5.sdk/System/Library/Frameworks/ |
| 208 | // cd ApplicationServices.framework/Frameworks/CoreGraphics.framework/ |
| 209 | // nm CoreGraphics | grep CGContextSetShouldSubpixelQuantizeFonts |
| 210 | |
| 211 | #if !defined(MAC_OS_X_VERSION_10_6) || (MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_6) |
| 212 | CG_EXTERN void CGContextSetAllowsFontSmoothing(CGContextRef context, bool value); |
| 213 | CG_EXTERN void CGContextSetAllowsFontSubpixelPositioning(CGContextRef context, bool value); |
| 214 | CG_EXTERN void CGContextSetShouldSubpixelPositionFonts(CGContextRef context, bool value); |
| 215 | CG_EXTERN void CGContextSetAllowsFontSubpixelQuantization(CGContextRef context, bool value); |
| 216 | CG_EXTERN void CGContextSetShouldSubpixelQuantizeFonts(CGContextRef context, bool value); |
| 217 | #endif |
| 218 | |
| 219 | static const char FONT_DEFAULT_NAME[] = "Lucida Sans"; |
| 220 | |
| 221 | // See Source/WebKit/chromium/base/mac/mac_util.mm DarwinMajorVersionInternal for original source. |
| 222 | static int readVersion() { |
| 223 | struct utsname info; |
| 224 | if (uname(&info) != 0) { |
| 225 | SkDebugf("uname failed\n"); |
| 226 | return 0; |
| 227 | } |
| 228 | if (strcmp(info.sysname, "Darwin") != 0) { |
| 229 | SkDebugf("unexpected uname sysname %s\n", info.sysname); |
| 230 | return 0; |
| 231 | } |
| 232 | char* dot = strchr(info.release, '.'); |
| 233 | if (!dot) { |
| 234 | SkDebugf("expected dot in uname release %s\n", info.release); |
| 235 | return 0; |
| 236 | } |
| 237 | int version = atoi(info.release); |
| 238 | if (version == 0) { |
| 239 | SkDebugf("could not parse uname release %s\n", info.release); |
| 240 | } |
| 241 | return version; |
| 242 | } |
| 243 | |
| 244 | static int darwinVersion() { |
| 245 | static int darwin_version = readVersion(); |
| 246 | return darwin_version; |
| 247 | } |
| 248 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 249 | static bool isSnowLeopard() { |
| 250 | return darwinVersion() == 10; |
| 251 | } |
| 252 | |
| 253 | static bool isLion() { |
| 254 | return darwinVersion() == 11; |
| 255 | } |
| 256 | |
| 257 | static bool isMountainLion() { |
| 258 | return darwinVersion() == 12; |
| 259 | } |
| 260 | |
| 261 | static bool isLCDFormat(unsigned format) { |
| 262 | return SkMask::kLCD16_Format == format || SkMask::kLCD32_Format == format; |
| 263 | } |
| 264 | |
| 265 | static CGFloat ScalarToCG(SkScalar scalar) { |
| 266 | if (sizeof(CGFloat) == sizeof(float)) { |
| 267 | return SkScalarToFloat(scalar); |
| 268 | } else { |
| 269 | SkASSERT(sizeof(CGFloat) == sizeof(double)); |
| 270 | return (CGFloat) SkScalarToDouble(scalar); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | static SkScalar CGToScalar(CGFloat cgFloat) { |
| 275 | if (sizeof(CGFloat) == sizeof(float)) { |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame^] | 276 | return cgFloat; |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 277 | } else { |
| 278 | SkASSERT(sizeof(CGFloat) == sizeof(double)); |
| 279 | return SkDoubleToScalar(cgFloat); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | static CGAffineTransform MatrixToCGAffineTransform(const SkMatrix& matrix, |
| 284 | SkScalar sx = SK_Scalar1, |
| 285 | SkScalar sy = SK_Scalar1) { |
| 286 | return CGAffineTransformMake( ScalarToCG(matrix[SkMatrix::kMScaleX] * sx), |
| 287 | -ScalarToCG(matrix[SkMatrix::kMSkewY] * sy), |
| 288 | -ScalarToCG(matrix[SkMatrix::kMSkewX] * sx), |
| 289 | ScalarToCG(matrix[SkMatrix::kMScaleY] * sy), |
| 290 | ScalarToCG(matrix[SkMatrix::kMTransX] * sx), |
| 291 | ScalarToCG(matrix[SkMatrix::kMTransY] * sy)); |
| 292 | } |
| 293 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 294 | /////////////////////////////////////////////////////////////////////////////// |
| 295 | |
| 296 | #define BITMAP_INFO_RGB (kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Host) |
| 297 | #define BITMAP_INFO_GRAY (kCGImageAlphaNone) |
| 298 | |
| 299 | /** |
| 300 | * There does not appear to be a publicly accessable API for determining if lcd |
| 301 | * font smoothing will be applied if we request it. The main issue is that if |
| 302 | * smoothing is applied a gamma of 2.0 will be used, if not a gamma of 1.0. |
| 303 | */ |
| 304 | static bool supports_LCD() { |
| 305 | static int gSupportsLCD = -1; |
| 306 | if (gSupportsLCD >= 0) { |
| 307 | return (bool) gSupportsLCD; |
| 308 | } |
| 309 | uint32_t rgb = 0; |
| 310 | AutoCFRelease<CGColorSpaceRef> colorspace(CGColorSpaceCreateDeviceRGB()); |
| 311 | AutoCFRelease<CGContextRef> cgContext(CGBitmapContextCreate(&rgb, 1, 1, 8, 4, |
| 312 | colorspace, BITMAP_INFO_RGB)); |
| 313 | CGContextSelectFont(cgContext, "Helvetica", 16, kCGEncodingMacRoman); |
| 314 | CGContextSetShouldSmoothFonts(cgContext, true); |
| 315 | CGContextSetShouldAntialias(cgContext, true); |
| 316 | CGContextSetTextDrawingMode(cgContext, kCGTextFill); |
| 317 | CGContextSetGrayFillColor(cgContext, 1, 1); |
| 318 | CGContextShowTextAtPoint(cgContext, -1, 0, "|", 1); |
| 319 | uint32_t r = (rgb >> 16) & 0xFF; |
| 320 | uint32_t g = (rgb >> 8) & 0xFF; |
| 321 | uint32_t b = (rgb >> 0) & 0xFF; |
| 322 | gSupportsLCD = (r != g || r != b); |
| 323 | return (bool) gSupportsLCD; |
| 324 | } |
| 325 | |
| 326 | class Offscreen { |
| 327 | public: |
| 328 | Offscreen(); |
| 329 | |
| 330 | CGRGBPixel* getCG(const SkScalerContext_Mac& context, const SkGlyph& glyph, |
| 331 | CGGlyph glyphID, size_t* rowBytesPtr, |
| 332 | bool generateA8FromLCD); |
| 333 | |
| 334 | private: |
| 335 | enum { |
| 336 | kSize = 32 * 32 * sizeof(CGRGBPixel) |
| 337 | }; |
| 338 | SkAutoSMalloc<kSize> fImageStorage; |
| 339 | AutoCFRelease<CGColorSpaceRef> fRGBSpace; |
| 340 | |
| 341 | // cached state |
| 342 | AutoCFRelease<CGContextRef> fCG; |
| 343 | SkISize fSize; |
| 344 | bool fDoAA; |
| 345 | bool fDoLCD; |
| 346 | |
| 347 | static int RoundSize(int dimension) { |
| 348 | return SkNextPow2(dimension); |
| 349 | } |
| 350 | }; |
| 351 | |
skia.committer@gmail.com | d55846d | 2013-03-30 07:01:27 +0000 | [diff] [blame] | 352 | Offscreen::Offscreen() : fRGBSpace(NULL), fCG(NULL), |
robertphillips@google.com | 87379e1 | 2013-03-29 12:11:10 +0000 | [diff] [blame] | 353 | fDoAA(false), fDoLCD(false) { |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 354 | fSize.set(0, 0); |
| 355 | } |
| 356 | |
| 357 | /////////////////////////////////////////////////////////////////////////////// |
| 358 | |
bungeman@google.com | fe74765 | 2013-03-25 19:36:11 +0000 | [diff] [blame] | 359 | static SkTypeface::Style computeStyleBits(CTFontRef font, bool* isFixedPitch) { |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 360 | unsigned style = SkTypeface::kNormal; |
| 361 | CTFontSymbolicTraits traits = CTFontGetSymbolicTraits(font); |
| 362 | |
| 363 | if (traits & kCTFontBoldTrait) { |
| 364 | style |= SkTypeface::kBold; |
| 365 | } |
| 366 | if (traits & kCTFontItalicTrait) { |
| 367 | style |= SkTypeface::kItalic; |
| 368 | } |
bungeman@google.com | fe74765 | 2013-03-25 19:36:11 +0000 | [diff] [blame] | 369 | if (isFixedPitch) { |
| 370 | *isFixedPitch = (traits & kCTFontMonoSpaceTrait) != 0; |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 371 | } |
| 372 | return (SkTypeface::Style)style; |
| 373 | } |
| 374 | |
| 375 | static SkFontID CTFontRef_to_SkFontID(CTFontRef fontRef) { |
| 376 | SkFontID id = 0; |
| 377 | // CTFontGetPlatformFont and ATSFontRef are not supported on iOS, so we have to |
| 378 | // bracket this to be Mac only. |
| 379 | #ifdef SK_BUILD_FOR_MAC |
| 380 | ATSFontRef ats = CTFontGetPlatformFont(fontRef, NULL); |
| 381 | id = (SkFontID)ats; |
| 382 | if (id != 0) { |
| 383 | id &= 0x3FFFFFFF; // make top two bits 00 |
| 384 | return id; |
| 385 | } |
| 386 | #endif |
| 387 | // CTFontGetPlatformFont returns NULL if the font is local |
| 388 | // (e.g., was created by a CSS3 @font-face rule). |
| 389 | AutoCFRelease<CGFontRef> cgFont(CTFontCopyGraphicsFont(fontRef, NULL)); |
| 390 | AutoCGTable<SkOTTableHead> headTable(cgFont); |
| 391 | if (headTable.fData) { |
| 392 | id = (SkFontID) headTable->checksumAdjustment; |
| 393 | id = (id & 0x3FFFFFFF) | 0x40000000; // make top two bits 01 |
| 394 | } |
| 395 | // well-formed fonts have checksums, but as a last resort, use the pointer. |
| 396 | if (id == 0) { |
| 397 | id = (SkFontID) (uintptr_t) fontRef; |
| 398 | id = (id & 0x3FFFFFFF) | 0x80000000; // make top two bits 10 |
| 399 | } |
| 400 | return id; |
| 401 | } |
| 402 | |
reed@google.com | ce8b3de | 2013-03-26 19:30:16 +0000 | [diff] [blame] | 403 | static SkFontStyle stylebits2fontstyle(SkTypeface::Style styleBits) { |
| 404 | return SkFontStyle((styleBits & SkTypeface::kBold) |
| 405 | ? SkFontStyle::kBold_Weight |
| 406 | : SkFontStyle::kNormal_Weight, |
| 407 | SkFontStyle::kNormal_Width, |
| 408 | (styleBits & SkTypeface::kItalic) |
| 409 | ? SkFontStyle::kItalic_Slant |
| 410 | : SkFontStyle::kUpright_Slant); |
| 411 | } |
| 412 | |
| 413 | #define WEIGHT_THRESHOLD ((SkFontStyle::kNormal_Weight + SkFontStyle::kBold_Weight)/2) |
| 414 | |
| 415 | static SkTypeface::Style fontstyle2stylebits(const SkFontStyle& fs) { |
| 416 | unsigned style = 0; |
| 417 | if (fs.width() >= WEIGHT_THRESHOLD) { |
| 418 | style |= SkTypeface::kBold; |
| 419 | } |
| 420 | if (fs.isItalic()) { |
| 421 | style |= SkTypeface::kItalic; |
| 422 | } |
| 423 | return (SkTypeface::Style)style; |
| 424 | } |
| 425 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 426 | class SkTypeface_Mac : public SkTypeface { |
| 427 | public: |
bungeman@google.com | fe74765 | 2013-03-25 19:36:11 +0000 | [diff] [blame] | 428 | SkTypeface_Mac(SkTypeface::Style style, SkFontID fontID, bool isFixedPitch, |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 429 | CTFontRef fontRef, const char name[]) |
reed@google.com | ce8b3de | 2013-03-26 19:30:16 +0000 | [diff] [blame] | 430 | : SkTypeface(style, fontID, isFixedPitch) |
| 431 | , fName(name) |
| 432 | , fFontRef(fontRef) // caller has already called CFRetain for us |
| 433 | , fFontStyle(stylebits2fontstyle(style)) |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 434 | { |
| 435 | SkASSERT(fontRef); |
| 436 | } |
skia.committer@gmail.com | 37cbc7f | 2013-03-27 07:01:04 +0000 | [diff] [blame] | 437 | |
reed@google.com | ce8b3de | 2013-03-26 19:30:16 +0000 | [diff] [blame] | 438 | SkTypeface_Mac(const SkFontStyle& fs, SkFontID fontID, bool isFixedPitch, |
| 439 | CTFontRef fontRef, const char name[]) |
| 440 | : SkTypeface(fontstyle2stylebits(fs), fontID, isFixedPitch) |
| 441 | , fName(name) |
| 442 | , fFontRef(fontRef) // caller has already called CFRetain for us |
| 443 | , fFontStyle(fs) |
| 444 | { |
| 445 | SkASSERT(fontRef); |
| 446 | } |
skia.committer@gmail.com | 37cbc7f | 2013-03-27 07:01:04 +0000 | [diff] [blame] | 447 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 448 | SkString fName; |
| 449 | AutoCFRelease<CTFontRef> fFontRef; |
reed@google.com | ce8b3de | 2013-03-26 19:30:16 +0000 | [diff] [blame] | 450 | SkFontStyle fFontStyle; |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 451 | |
| 452 | protected: |
| 453 | friend class SkFontHost; // to access our protected members for deprecated methods |
| 454 | |
| 455 | virtual int onGetUPEM() const SK_OVERRIDE; |
reed@google.com | cc9aad5 | 2013-03-21 19:28:10 +0000 | [diff] [blame] | 456 | virtual SkStream* onOpenStream(int* ttcIndex) const SK_OVERRIDE; |
bungeman@google.com | 839702b | 2013-08-07 17:09:22 +0000 | [diff] [blame] | 457 | virtual SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const SK_OVERRIDE; |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 458 | virtual int onGetTableTags(SkFontTableTag tags[]) const SK_OVERRIDE; |
| 459 | virtual size_t onGetTableData(SkFontTableTag, size_t offset, |
| 460 | size_t length, void* data) const SK_OVERRIDE; |
| 461 | virtual SkScalerContext* onCreateScalerContext(const SkDescriptor*) const SK_OVERRIDE; |
| 462 | virtual void onFilterRec(SkScalerContextRec*) const SK_OVERRIDE; |
reed@google.com | 5526ede | 2013-03-25 13:03:37 +0000 | [diff] [blame] | 463 | virtual void onGetFontDescriptor(SkFontDescriptor*, bool*) const SK_OVERRIDE; |
reed@google.com | 2689f61 | 2013-03-20 20:01:47 +0000 | [diff] [blame] | 464 | virtual SkAdvancedTypefaceMetrics* onGetAdvancedTypefaceMetrics( |
| 465 | SkAdvancedTypefaceMetrics::PerGlyphInfo, |
| 466 | const uint32_t*, uint32_t) const SK_OVERRIDE; |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 467 | virtual int onCharsToGlyphs(const void* chars, Encoding, uint16_t glyphs[], |
| 468 | int glyphCount) const SK_OVERRIDE; |
| 469 | virtual int onCountGlyphs() const SK_OVERRIDE; |
skia.committer@gmail.com | c1641fc | 2013-03-21 07:01:39 +0000 | [diff] [blame] | 470 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 471 | private: |
reed@google.com | ce8b3de | 2013-03-26 19:30:16 +0000 | [diff] [blame] | 472 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 473 | typedef SkTypeface INHERITED; |
| 474 | }; |
| 475 | |
| 476 | static SkTypeface* NewFromFontRef(CTFontRef fontRef, const char name[]) { |
| 477 | SkASSERT(fontRef); |
bungeman@google.com | fe74765 | 2013-03-25 19:36:11 +0000 | [diff] [blame] | 478 | bool isFixedPitch; |
| 479 | SkTypeface::Style style = computeStyleBits(fontRef, &isFixedPitch); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 480 | SkFontID fontID = CTFontRef_to_SkFontID(fontRef); |
| 481 | |
bungeman@google.com | fe74765 | 2013-03-25 19:36:11 +0000 | [diff] [blame] | 482 | return new SkTypeface_Mac(style, fontID, isFixedPitch, fontRef, name); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | static SkTypeface* NewFromName(const char familyName[], SkTypeface::Style theStyle) { |
| 486 | CTFontRef ctFont = NULL; |
| 487 | |
| 488 | CTFontSymbolicTraits ctFontTraits = 0; |
| 489 | if (theStyle & SkTypeface::kBold) { |
| 490 | ctFontTraits |= kCTFontBoldTrait; |
| 491 | } |
| 492 | if (theStyle & SkTypeface::kItalic) { |
| 493 | ctFontTraits |= kCTFontItalicTrait; |
| 494 | } |
| 495 | |
| 496 | // Create the font info |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 497 | AutoCFRelease<CFStringRef> cfFontName(make_CFString(familyName)); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 498 | |
| 499 | AutoCFRelease<CFNumberRef> cfFontTraits( |
| 500 | CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &ctFontTraits)); |
| 501 | |
| 502 | AutoCFRelease<CFMutableDictionaryRef> cfAttributes( |
| 503 | CFDictionaryCreateMutable(kCFAllocatorDefault, 0, |
| 504 | &kCFTypeDictionaryKeyCallBacks, |
| 505 | &kCFTypeDictionaryValueCallBacks)); |
| 506 | |
| 507 | AutoCFRelease<CFMutableDictionaryRef> cfTraits( |
| 508 | CFDictionaryCreateMutable(kCFAllocatorDefault, 0, |
| 509 | &kCFTypeDictionaryKeyCallBacks, |
| 510 | &kCFTypeDictionaryValueCallBacks)); |
| 511 | |
| 512 | // Create the font |
| 513 | if (cfFontName != NULL && cfFontTraits != NULL && cfAttributes != NULL && cfTraits != NULL) { |
| 514 | CFDictionaryAddValue(cfTraits, kCTFontSymbolicTrait, cfFontTraits); |
| 515 | |
| 516 | CFDictionaryAddValue(cfAttributes, kCTFontFamilyNameAttribute, cfFontName); |
| 517 | CFDictionaryAddValue(cfAttributes, kCTFontTraitsAttribute, cfTraits); |
| 518 | |
| 519 | AutoCFRelease<CTFontDescriptorRef> ctFontDesc( |
| 520 | CTFontDescriptorCreateWithAttributes(cfAttributes)); |
| 521 | |
| 522 | if (ctFontDesc != NULL) { |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 523 | ctFont = CTFontCreateWithFontDescriptor(ctFontDesc, 0, NULL); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | |
| 527 | return ctFont ? NewFromFontRef(ctFont, familyName) : NULL; |
| 528 | } |
| 529 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 530 | static SkTypeface* GetDefaultFace() { |
| 531 | SK_DECLARE_STATIC_MUTEX(gMutex); |
| 532 | SkAutoMutexAcquire ma(gMutex); |
| 533 | |
| 534 | static SkTypeface* gDefaultFace; |
| 535 | |
| 536 | if (NULL == gDefaultFace) { |
| 537 | gDefaultFace = NewFromName(FONT_DEFAULT_NAME, SkTypeface::kNormal); |
| 538 | SkTypefaceCache::Add(gDefaultFace, SkTypeface::kNormal); |
| 539 | } |
| 540 | return gDefaultFace; |
| 541 | } |
| 542 | |
| 543 | /////////////////////////////////////////////////////////////////////////////// |
| 544 | |
| 545 | extern CTFontRef SkTypeface_GetCTFontRef(const SkTypeface* face); |
| 546 | CTFontRef SkTypeface_GetCTFontRef(const SkTypeface* face) { |
| 547 | const SkTypeface_Mac* macface = (const SkTypeface_Mac*)face; |
| 548 | return macface ? macface->fFontRef.get() : NULL; |
| 549 | } |
| 550 | |
| 551 | /* This function is visible on the outside. It first searches the cache, and if |
| 552 | * not found, returns a new entry (after adding it to the cache). |
| 553 | */ |
| 554 | SkTypeface* SkCreateTypefaceFromCTFont(CTFontRef fontRef) { |
| 555 | SkFontID fontID = CTFontRef_to_SkFontID(fontRef); |
| 556 | SkTypeface* face = SkTypefaceCache::FindByID(fontID); |
| 557 | if (face) { |
| 558 | face->ref(); |
| 559 | } else { |
| 560 | face = NewFromFontRef(fontRef, NULL); |
| 561 | SkTypefaceCache::Add(face, face->style()); |
| 562 | // NewFromFontRef doesn't retain the parameter, but the typeface it |
| 563 | // creates does release it in its destructor, so we balance that with |
| 564 | // a retain call here. |
| 565 | CFRetain(fontRef); |
| 566 | } |
| 567 | SkASSERT(face->getRefCnt() > 1); |
| 568 | return face; |
| 569 | } |
| 570 | |
| 571 | struct NameStyleRec { |
| 572 | const char* fName; |
| 573 | SkTypeface::Style fStyle; |
| 574 | }; |
| 575 | |
| 576 | static bool FindByNameStyle(SkTypeface* face, SkTypeface::Style style, |
| 577 | void* ctx) { |
| 578 | const SkTypeface_Mac* mface = reinterpret_cast<SkTypeface_Mac*>(face); |
| 579 | const NameStyleRec* rec = reinterpret_cast<const NameStyleRec*>(ctx); |
| 580 | |
| 581 | return rec->fStyle == style && mface->fName.equals(rec->fName); |
| 582 | } |
| 583 | |
| 584 | static const char* map_css_names(const char* name) { |
| 585 | static const struct { |
| 586 | const char* fFrom; // name the caller specified |
| 587 | const char* fTo; // "canonical" name we map to |
| 588 | } gPairs[] = { |
| 589 | { "sans-serif", "Helvetica" }, |
| 590 | { "serif", "Times" }, |
| 591 | { "monospace", "Courier" } |
| 592 | }; |
| 593 | |
| 594 | for (size_t i = 0; i < SK_ARRAY_COUNT(gPairs); i++) { |
| 595 | if (strcmp(name, gPairs[i].fFrom) == 0) { |
| 596 | return gPairs[i].fTo; |
| 597 | } |
| 598 | } |
| 599 | return name; // no change |
| 600 | } |
| 601 | |
reed@google.com | 7fdcd44 | 2013-07-30 21:25:49 +0000 | [diff] [blame] | 602 | static SkTypeface* create_typeface(const SkTypeface* familyFace, |
| 603 | const char familyName[], |
| 604 | SkTypeface::Style style) { |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 605 | if (familyName) { |
| 606 | familyName = map_css_names(familyName); |
| 607 | } |
skia.committer@gmail.com | 76015b0 | 2013-07-31 07:01:00 +0000 | [diff] [blame] | 608 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 609 | // Clone an existing typeface |
| 610 | // TODO: only clone if style matches the familyFace's style... |
| 611 | if (familyName == NULL && familyFace != NULL) { |
| 612 | familyFace->ref(); |
| 613 | return const_cast<SkTypeface*>(familyFace); |
| 614 | } |
skia.committer@gmail.com | 76015b0 | 2013-07-31 07:01:00 +0000 | [diff] [blame] | 615 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 616 | if (!familyName || !*familyName) { |
| 617 | familyName = FONT_DEFAULT_NAME; |
| 618 | } |
skia.committer@gmail.com | 76015b0 | 2013-07-31 07:01:00 +0000 | [diff] [blame] | 619 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 620 | NameStyleRec rec = { familyName, style }; |
| 621 | SkTypeface* face = SkTypefaceCache::FindByProcAndRef(FindByNameStyle, &rec); |
skia.committer@gmail.com | 76015b0 | 2013-07-31 07:01:00 +0000 | [diff] [blame] | 622 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 623 | if (NULL == face) { |
| 624 | face = NewFromName(familyName, style); |
| 625 | if (face) { |
| 626 | SkTypefaceCache::Add(face, style); |
| 627 | } else { |
| 628 | face = GetDefaultFace(); |
| 629 | face->ref(); |
| 630 | } |
| 631 | } |
| 632 | return face; |
| 633 | } |
| 634 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 635 | /////////////////////////////////////////////////////////////////////////////// |
| 636 | |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 637 | /** GlyphRect is in FUnits (em space, y up). */ |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 638 | struct GlyphRect { |
| 639 | int16_t fMinX; |
| 640 | int16_t fMinY; |
| 641 | int16_t fMaxX; |
| 642 | int16_t fMaxY; |
| 643 | }; |
| 644 | |
| 645 | class SkScalerContext_Mac : public SkScalerContext { |
| 646 | public: |
reed@google.com | 0da4861 | 2013-03-19 16:06:52 +0000 | [diff] [blame] | 647 | SkScalerContext_Mac(SkTypeface_Mac*, const SkDescriptor*); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 648 | |
| 649 | protected: |
| 650 | unsigned generateGlyphCount(void) SK_OVERRIDE; |
| 651 | uint16_t generateCharToGlyph(SkUnichar uni) SK_OVERRIDE; |
| 652 | void generateAdvance(SkGlyph* glyph) SK_OVERRIDE; |
| 653 | void generateMetrics(SkGlyph* glyph) SK_OVERRIDE; |
| 654 | void generateImage(const SkGlyph& glyph) SK_OVERRIDE; |
| 655 | void generatePath(const SkGlyph& glyph, SkPath* path) SK_OVERRIDE; |
| 656 | void generateFontMetrics(SkPaint::FontMetrics* mX, SkPaint::FontMetrics* mY) SK_OVERRIDE; |
| 657 | |
| 658 | private: |
| 659 | static void CTPathElement(void *info, const CGPathElement *element); |
skia.committer@gmail.com | 539f364 | 2013-05-16 07:01:00 +0000 | [diff] [blame] | 660 | |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 661 | /** Returns the offset from the horizontal origin to the vertical origin in SkGlyph units. */ |
| 662 | void getVerticalOffset(CGGlyph glyphID, SkPoint* offset) const; |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 663 | |
| 664 | /** Initializes and returns the value of fFBoundingBoxesGlyphOffset. |
| 665 | * |
| 666 | * For use with (and must be called before) generateBBoxes. |
| 667 | */ |
| 668 | uint16_t getFBoundingBoxesGlyphOffset(); |
skia.committer@gmail.com | 539f364 | 2013-05-16 07:01:00 +0000 | [diff] [blame] | 669 | |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 670 | /** Initializes fFBoundingBoxes and returns true on success. |
| 671 | * |
| 672 | * On Lion and Mountain Lion, CTFontGetBoundingRectsForGlyphs has a bug which causes it to |
| 673 | * return a bad value in bounds.origin.x for SFNT fonts whose hhea::numberOfHMetrics is |
| 674 | * less than its maxp::numGlyphs. When this is the case we try to read the bounds from the |
| 675 | * font directly. |
| 676 | * |
| 677 | * This routine initializes fFBoundingBoxes to an array of |
| 678 | * fGlyphCount - fFBoundingBoxesGlyphOffset GlyphRects which contain the bounds in FUnits |
| 679 | * (em space, y up) of glyphs with ids in the range [fFBoundingBoxesGlyphOffset, fGlyphCount). |
| 680 | * |
| 681 | * Returns true if fFBoundingBoxes is properly initialized. The table can only be properly |
| 682 | * initialized for a TrueType font with 'head', 'loca', and 'glyf' tables. |
| 683 | * |
| 684 | * TODO: A future optimization will compute fFBoundingBoxes once per fCTFont. |
| 685 | */ |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 686 | bool generateBBoxes(); |
| 687 | |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 688 | /** Converts from FUnits (em space, y up) to SkGlyph units (pixels, y down). |
| 689 | * |
| 690 | * Used on Snow Leopard to correct CTFontGetVerticalTranslationsForGlyphs. |
| 691 | * Used on Lion to correct CTFontGetBoundingRectsForGlyphs. |
| 692 | */ |
| 693 | SkMatrix fFUnitMatrix; |
skia.committer@gmail.com | 539f364 | 2013-05-16 07:01:00 +0000 | [diff] [blame] | 694 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 695 | Offscreen fOffscreen; |
| 696 | AutoCFRelease<CTFontRef> fCTFont; |
skia.committer@gmail.com | 539f364 | 2013-05-16 07:01:00 +0000 | [diff] [blame] | 697 | |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 698 | /** Vertical variant of fCTFont. |
| 699 | * |
| 700 | * CT vertical metrics are pre-rotated (in em space, before transform) 90deg clock-wise. |
| 701 | * This makes kCTFontDefaultOrientation dangerous, because the metrics from |
| 702 | * kCTFontHorizontalOrientation are in a different space from kCTFontVerticalOrientation. |
| 703 | * Use fCTVerticalFont with kCTFontVerticalOrientation to get metrics in the same space. |
| 704 | */ |
| 705 | AutoCFRelease<CTFontRef> fCTVerticalFont; |
skia.committer@gmail.com | 539f364 | 2013-05-16 07:01:00 +0000 | [diff] [blame] | 706 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 707 | AutoCFRelease<CGFontRef> fCGFont; |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 708 | SkAutoTMalloc<GlyphRect> fFBoundingBoxes; |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 709 | uint16_t fFBoundingBoxesGlyphOffset; |
| 710 | uint16_t fGlyphCount; |
| 711 | bool fGeneratedFBoundingBoxes; |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 712 | const bool fDoSubPosition; |
| 713 | const bool fVertical; |
| 714 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 715 | friend class Offscreen; |
reed@google.com | 0da4861 | 2013-03-19 16:06:52 +0000 | [diff] [blame] | 716 | |
| 717 | typedef SkScalerContext INHERITED; |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 718 | }; |
| 719 | |
reed@google.com | 0da4861 | 2013-03-19 16:06:52 +0000 | [diff] [blame] | 720 | SkScalerContext_Mac::SkScalerContext_Mac(SkTypeface_Mac* typeface, |
| 721 | const SkDescriptor* desc) |
| 722 | : INHERITED(typeface, desc) |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 723 | , fFBoundingBoxes() |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 724 | , fFBoundingBoxesGlyphOffset(0) |
| 725 | , fGeneratedFBoundingBoxes(false) |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 726 | , fDoSubPosition(SkToBool(fRec.fFlags & kSubpixelPositioning_Flag)) |
| 727 | , fVertical(SkToBool(fRec.fFlags & kVertical_Flag)) |
| 728 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 729 | { |
reed@google.com | 2689f61 | 2013-03-20 20:01:47 +0000 | [diff] [blame] | 730 | CTFontRef ctFont = typeface->fFontRef.get(); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 731 | CFIndex numGlyphs = CTFontGetGlyphCount(ctFont); |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 732 | SkASSERT(numGlyphs >= 1 && numGlyphs <= 0xFFFF); |
| 733 | fGlyphCount = SkToU16(numGlyphs); |
skia.committer@gmail.com | 539f364 | 2013-05-16 07:01:00 +0000 | [diff] [blame] | 734 | |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 735 | fRec.getSingleMatrix(&fFUnitMatrix); |
| 736 | CGAffineTransform transform = MatrixToCGAffineTransform(fFUnitMatrix); |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 737 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 738 | AutoCFRelease<CTFontDescriptorRef> ctFontDesc; |
| 739 | if (fVertical) { |
| 740 | AutoCFRelease<CFMutableDictionaryRef> cfAttributes(CFDictionaryCreateMutable( |
| 741 | kCFAllocatorDefault, 0, |
| 742 | &kCFTypeDictionaryKeyCallBacks, |
| 743 | &kCFTypeDictionaryValueCallBacks)); |
| 744 | if (cfAttributes) { |
| 745 | CTFontOrientation ctOrientation = kCTFontVerticalOrientation; |
| 746 | AutoCFRelease<CFNumberRef> cfVertical(CFNumberCreate( |
| 747 | kCFAllocatorDefault, kCFNumberSInt32Type, &ctOrientation)); |
| 748 | CFDictionaryAddValue(cfAttributes, kCTFontOrientationAttribute, cfVertical); |
| 749 | ctFontDesc = CTFontDescriptorCreateWithAttributes(cfAttributes); |
| 750 | } |
| 751 | } |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 752 | // Since our matrix includes everything, we pass 1 for size. |
| 753 | fCTFont = CTFontCreateCopyWithAttributes(ctFont, 1, &transform, ctFontDesc); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 754 | fCGFont = CTFontCopyGraphicsFont(fCTFont, NULL); |
| 755 | if (fVertical) { |
| 756 | CGAffineTransform rotateLeft = CGAffineTransformMake(0, -1, 1, 0, 0, 0); |
| 757 | transform = CGAffineTransformConcat(rotateLeft, transform); |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 758 | fCTVerticalFont = CTFontCreateCopyWithAttributes(ctFont, 1, &transform, NULL); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 759 | } |
commit-bot@chromium.org | 371add8 | 2013-06-26 21:08:11 +0000 | [diff] [blame] | 760 | |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 761 | SkScalar emPerFUnit = SkScalarInvert(SkIntToScalar(CGFontGetUnitsPerEm(fCGFont))); |
| 762 | fFUnitMatrix.preScale(emPerFUnit, -emPerFUnit); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | CGRGBPixel* Offscreen::getCG(const SkScalerContext_Mac& context, const SkGlyph& glyph, |
| 766 | CGGlyph glyphID, size_t* rowBytesPtr, |
| 767 | bool generateA8FromLCD) { |
| 768 | if (!fRGBSpace) { |
| 769 | //It doesn't appear to matter what color space is specified. |
| 770 | //Regular blends and antialiased text are always (s*a + d*(1-a)) |
| 771 | //and smoothed text is always g=2.0. |
| 772 | fRGBSpace = CGColorSpaceCreateDeviceRGB(); |
| 773 | } |
| 774 | |
| 775 | // default to kBW_Format |
| 776 | bool doAA = false; |
| 777 | bool doLCD = false; |
| 778 | |
| 779 | if (SkMask::kBW_Format != glyph.fMaskFormat) { |
| 780 | doLCD = true; |
| 781 | doAA = true; |
| 782 | } |
| 783 | |
| 784 | // FIXME: lcd smoothed un-hinted rasterization unsupported. |
| 785 | if (!generateA8FromLCD && SkMask::kA8_Format == glyph.fMaskFormat) { |
| 786 | doLCD = false; |
| 787 | doAA = true; |
| 788 | } |
| 789 | |
| 790 | size_t rowBytes = fSize.fWidth * sizeof(CGRGBPixel); |
| 791 | if (!fCG || fSize.fWidth < glyph.fWidth || fSize.fHeight < glyph.fHeight) { |
| 792 | if (fSize.fWidth < glyph.fWidth) { |
| 793 | fSize.fWidth = RoundSize(glyph.fWidth); |
| 794 | } |
| 795 | if (fSize.fHeight < glyph.fHeight) { |
| 796 | fSize.fHeight = RoundSize(glyph.fHeight); |
| 797 | } |
| 798 | |
| 799 | rowBytes = fSize.fWidth * sizeof(CGRGBPixel); |
| 800 | void* image = fImageStorage.reset(rowBytes * fSize.fHeight); |
| 801 | fCG = CGBitmapContextCreate(image, fSize.fWidth, fSize.fHeight, 8, |
| 802 | rowBytes, fRGBSpace, BITMAP_INFO_RGB); |
| 803 | |
| 804 | // skia handles quantization itself, so we disable this for cg to get |
| 805 | // full fractional data from them. |
| 806 | CGContextSetAllowsFontSubpixelQuantization(fCG, false); |
| 807 | CGContextSetShouldSubpixelQuantizeFonts(fCG, false); |
| 808 | |
| 809 | CGContextSetTextDrawingMode(fCG, kCGTextFill); |
| 810 | CGContextSetFont(fCG, context.fCGFont); |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 811 | CGContextSetFontSize(fCG, 1 /*CTFontGetSize(context.fCTFont)*/); |
| 812 | CGContextSetTextMatrix(fCG, CTFontGetMatrix(context.fCTFont)); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 813 | |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 814 | // Because CG always draws from the horizontal baseline, |
| 815 | // if there is a non-integral translation from the horizontal origin to the vertical origin, |
| 816 | // then CG cannot draw the glyph in the correct location without subpixel positioning. |
| 817 | CGContextSetAllowsFontSubpixelPositioning(fCG, context.fDoSubPosition || context.fVertical); |
| 818 | CGContextSetShouldSubpixelPositionFonts(fCG, context.fDoSubPosition || context.fVertical); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 819 | |
| 820 | // Draw white on black to create mask. |
| 821 | // TODO: Draw black on white and invert, CG has a special case codepath. |
| 822 | CGContextSetGrayFillColor(fCG, 1.0f, 1.0f); |
| 823 | |
| 824 | // force our checks below to happen |
| 825 | fDoAA = !doAA; |
| 826 | fDoLCD = !doLCD; |
| 827 | } |
| 828 | |
| 829 | if (fDoAA != doAA) { |
| 830 | CGContextSetShouldAntialias(fCG, doAA); |
| 831 | fDoAA = doAA; |
| 832 | } |
| 833 | if (fDoLCD != doLCD) { |
| 834 | CGContextSetShouldSmoothFonts(fCG, doLCD); |
| 835 | fDoLCD = doLCD; |
| 836 | } |
| 837 | |
| 838 | CGRGBPixel* image = (CGRGBPixel*)fImageStorage.get(); |
| 839 | // skip rows based on the glyph's height |
| 840 | image += (fSize.fHeight - glyph.fHeight) * fSize.fWidth; |
| 841 | |
| 842 | // erase to black |
| 843 | sk_memset_rect32(image, 0, glyph.fWidth, glyph.fHeight, rowBytes); |
| 844 | |
| 845 | float subX = 0; |
| 846 | float subY = 0; |
| 847 | if (context.fDoSubPosition) { |
| 848 | subX = SkFixedToFloat(glyph.getSubXFixed()); |
| 849 | subY = SkFixedToFloat(glyph.getSubYFixed()); |
| 850 | } |
skia.committer@gmail.com | 539f364 | 2013-05-16 07:01:00 +0000 | [diff] [blame] | 851 | |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 852 | // CGContextShowGlyphsAtPoint always draws using the horizontal baseline origin. |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 853 | if (context.fVertical) { |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 854 | SkPoint offset; |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 855 | context.getVerticalOffset(glyphID, &offset); |
| 856 | subX += offset.fX; |
| 857 | subY += offset.fY; |
| 858 | } |
skia.committer@gmail.com | 539f364 | 2013-05-16 07:01:00 +0000 | [diff] [blame] | 859 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 860 | CGContextShowGlyphsAtPoint(fCG, -glyph.fLeft + subX, |
| 861 | glyph.fTop + glyph.fHeight - subY, |
| 862 | &glyphID, 1); |
| 863 | |
| 864 | SkASSERT(rowBytesPtr); |
| 865 | *rowBytesPtr = rowBytes; |
| 866 | return image; |
| 867 | } |
| 868 | |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 869 | void SkScalerContext_Mac::getVerticalOffset(CGGlyph glyphID, SkPoint* offset) const { |
| 870 | // Snow Leopard returns cgVertOffset in completely un-transformed FUnits (em space, y up). |
| 871 | // Lion and Leopard return cgVertOffset in CG units (pixels, y up). |
| 872 | CGSize cgVertOffset; |
| 873 | CTFontGetVerticalTranslationsForGlyphs(fCTFont, &glyphID, &cgVertOffset, 1); |
| 874 | |
| 875 | SkPoint skVertOffset = { CGToScalar(cgVertOffset.width), CGToScalar(cgVertOffset.height) }; |
| 876 | if (isSnowLeopard()) { |
| 877 | // From FUnits (em space, y up) to SkGlyph units (pixels, y down). |
| 878 | fFUnitMatrix.mapPoints(&skVertOffset, 1); |
| 879 | } else { |
| 880 | // From CG units (pixels, y up) to SkGlyph units (pixels, y down). |
| 881 | skVertOffset.fY = -skVertOffset.fY; |
| 882 | } |
skia.committer@gmail.com | 539f364 | 2013-05-16 07:01:00 +0000 | [diff] [blame] | 883 | |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 884 | *offset = skVertOffset; |
| 885 | } |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 886 | |
| 887 | uint16_t SkScalerContext_Mac::getFBoundingBoxesGlyphOffset() { |
| 888 | if (fFBoundingBoxesGlyphOffset) { |
| 889 | return fFBoundingBoxesGlyphOffset; |
| 890 | } |
| 891 | fFBoundingBoxesGlyphOffset = fGlyphCount; // fallback for all fonts |
| 892 | AutoCGTable<SkOTTableHorizontalHeader> hheaTable(fCGFont); |
| 893 | if (hheaTable.fData) { |
| 894 | fFBoundingBoxesGlyphOffset = SkEndian_SwapBE16(hheaTable->numberOfHMetrics); |
| 895 | } |
| 896 | return fFBoundingBoxesGlyphOffset; |
| 897 | } |
reed@android.com | 0680d6c | 2008-12-19 19:46:15 +0000 | [diff] [blame] | 898 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 899 | bool SkScalerContext_Mac::generateBBoxes() { |
| 900 | if (fGeneratedFBoundingBoxes) { |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 901 | return NULL != fFBoundingBoxes.get(); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 902 | } |
| 903 | fGeneratedFBoundingBoxes = true; |
reed@android.com | 0bf64d4 | 2009-03-09 17:22:22 +0000 | [diff] [blame] | 904 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 905 | AutoCGTable<SkOTTableHead> headTable(fCGFont); |
| 906 | if (!headTable.fData) { |
| 907 | return false; |
| 908 | } |
| 909 | |
| 910 | AutoCGTable<SkOTTableIndexToLocation> locaTable(fCGFont); |
| 911 | if (!locaTable.fData) { |
| 912 | return false; |
| 913 | } |
| 914 | |
| 915 | AutoCGTable<SkOTTableGlyph> glyfTable(fCGFont); |
| 916 | if (!glyfTable.fData) { |
| 917 | return false; |
| 918 | } |
| 919 | |
| 920 | uint16_t entries = fGlyphCount - fFBoundingBoxesGlyphOffset; |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 921 | fFBoundingBoxes.reset(entries); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 922 | |
| 923 | SkOTTableHead::IndexToLocFormat locaFormat = headTable->indexToLocFormat; |
| 924 | SkOTTableGlyph::Iterator glyphDataIter(*glyfTable.fData, *locaTable.fData, locaFormat); |
| 925 | glyphDataIter.advance(fFBoundingBoxesGlyphOffset); |
| 926 | for (uint16_t boundingBoxesIndex = 0; boundingBoxesIndex < entries; ++boundingBoxesIndex) { |
| 927 | const SkOTTableGlyphData* glyphData = glyphDataIter.next(); |
| 928 | GlyphRect& rect = fFBoundingBoxes[boundingBoxesIndex]; |
| 929 | rect.fMinX = SkEndian_SwapBE16(glyphData->xMin); |
| 930 | rect.fMinY = SkEndian_SwapBE16(glyphData->yMin); |
| 931 | rect.fMaxX = SkEndian_SwapBE16(glyphData->xMax); |
| 932 | rect.fMaxY = SkEndian_SwapBE16(glyphData->yMax); |
| 933 | } |
commit-bot@chromium.org | 371add8 | 2013-06-26 21:08:11 +0000 | [diff] [blame] | 934 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 935 | return true; |
| 936 | } |
| 937 | |
| 938 | unsigned SkScalerContext_Mac::generateGlyphCount(void) { |
| 939 | return fGlyphCount; |
| 940 | } |
| 941 | |
| 942 | uint16_t SkScalerContext_Mac::generateCharToGlyph(SkUnichar uni) { |
bungeman@google.com | fb1663a | 2013-10-24 22:32:43 +0000 | [diff] [blame] | 943 | CGGlyph cgGlyph[2]; |
bungeman@google.com | 72b8cb2 | 2013-10-25 17:49:08 +0000 | [diff] [blame] | 944 | UniChar theChar[2]; // UniChar is a UTF-16 16-bit code unit. |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 945 | |
| 946 | // Get the glyph |
bungeman@google.com | fb1663a | 2013-10-24 22:32:43 +0000 | [diff] [blame] | 947 | size_t numUniChar = SkUTF16_FromUnichar(uni, theChar); |
| 948 | SkASSERT(sizeof(CGGlyph) <= sizeof(uint16_t)); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 949 | |
bungeman@google.com | 72b8cb2 | 2013-10-25 17:49:08 +0000 | [diff] [blame] | 950 | // Undocumented behavior of CTFontGetGlyphsForCharacters with non-bmp code points: |
| 951 | // When a surrogate pair is detected, the glyph index used is the index of the high surrogate. |
| 952 | // It is documented that if a mapping is unavailable, the glyph will be set to 0. |
| 953 | CTFontGetGlyphsForCharacters(fCTFont, theChar, cgGlyph, numUniChar); |
bungeman@google.com | fb1663a | 2013-10-24 22:32:43 +0000 | [diff] [blame] | 954 | return cgGlyph[0]; |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | void SkScalerContext_Mac::generateAdvance(SkGlyph* glyph) { |
| 958 | this->generateMetrics(glyph); |
| 959 | } |
| 960 | |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 961 | void SkScalerContext_Mac::generateMetrics(SkGlyph* glyph) { |
| 962 | const CGGlyph cgGlyph = (CGGlyph) glyph->getGlyphID(fBaseGlyphCount); |
| 963 | glyph->zeroMetrics(); |
skia.committer@gmail.com | 539f364 | 2013-05-16 07:01:00 +0000 | [diff] [blame] | 964 | |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 965 | // The following block produces cgAdvance in CG units (pixels, y up). |
| 966 | CGSize cgAdvance; |
| 967 | if (fVertical) { |
| 968 | CTFontGetAdvancesForGlyphs(fCTVerticalFont, kCTFontVerticalOrientation, |
| 969 | &cgGlyph, &cgAdvance, 1); |
| 970 | } else { |
| 971 | CTFontGetAdvancesForGlyphs(fCTFont, kCTFontHorizontalOrientation, |
| 972 | &cgGlyph, &cgAdvance, 1); |
| 973 | } |
| 974 | glyph->fAdvanceX = SkFloatToFixed_Check(cgAdvance.width); |
| 975 | glyph->fAdvanceY = -SkFloatToFixed_Check(cgAdvance.height); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 976 | |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 977 | // The following produces skBounds in SkGlyph units (pixels, y down), |
| 978 | // or returns early if skBounds would be empty. |
| 979 | SkRect skBounds; |
skia.committer@gmail.com | 539f364 | 2013-05-16 07:01:00 +0000 | [diff] [blame] | 980 | |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 981 | // On Mountain Lion, CTFontGetBoundingRectsForGlyphs with kCTFontVerticalOrientation and |
| 982 | // CTFontGetVerticalTranslationsForGlyphs do not agree when using OTF CFF fonts. |
| 983 | // For TTF fonts these two do agree and we can use CTFontGetBoundingRectsForGlyphs to get |
| 984 | // the bounding box and CTFontGetVerticalTranslationsForGlyphs to then draw the glyph |
| 985 | // inside that bounding box. However, with OTF CFF fonts this does not work. It appears that |
| 986 | // CTFontGetBoundingRectsForGlyphs with kCTFontVerticalOrientation on OTF CFF fonts tries |
| 987 | // to center the glyph along the vertical baseline and also perform some mysterious shift |
| 988 | // along the baseline. CTFontGetVerticalTranslationsForGlyphs does not appear to perform |
| 989 | // these steps. |
| 990 | // |
| 991 | // It is not known which is correct (or if either is correct). However, we must always draw |
| 992 | // from the horizontal origin and must use CTFontGetVerticalTranslationsForGlyphs to draw. |
| 993 | // As a result, we do not call CTFontGetBoundingRectsForGlyphs for vertical glyphs. |
skia.committer@gmail.com | 539f364 | 2013-05-16 07:01:00 +0000 | [diff] [blame] | 994 | |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 995 | // On Snow Leopard, CTFontGetBoundingRectsForGlyphs ignores kCTFontVerticalOrientation and |
| 996 | // returns horizontal bounds. |
skia.committer@gmail.com | 539f364 | 2013-05-16 07:01:00 +0000 | [diff] [blame] | 997 | |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 998 | // On Lion and Mountain Lion, CTFontGetBoundingRectsForGlyphs has a bug which causes it to |
| 999 | // return a bad value in cgBounds.origin.x for SFNT fonts whose hhea::numberOfHMetrics is |
| 1000 | // less than its maxp::numGlyphs. When this is the case we try to read the bounds from the |
| 1001 | // font directly. |
| 1002 | if ((isLion() || isMountainLion()) && |
| 1003 | (cgGlyph < fGlyphCount && cgGlyph >= getFBoundingBoxesGlyphOffset() && generateBBoxes())) |
| 1004 | { |
| 1005 | const GlyphRect& gRect = fFBoundingBoxes[cgGlyph - fFBoundingBoxesGlyphOffset]; |
| 1006 | if (gRect.fMinX >= gRect.fMaxX || gRect.fMinY >= gRect.fMaxY) { |
| 1007 | return; |
| 1008 | } |
| 1009 | skBounds = SkRect::MakeLTRB(gRect.fMinX, gRect.fMinY, gRect.fMaxX, gRect.fMaxY); |
| 1010 | // From FUnits (em space, y up) to SkGlyph units (pixels, y down). |
| 1011 | fFUnitMatrix.mapRect(&skBounds); |
| 1012 | |
| 1013 | } else { |
| 1014 | // CTFontGetBoundingRectsForGlyphs produces cgBounds in CG units (pixels, y up). |
| 1015 | CGRect cgBounds; |
| 1016 | CTFontGetBoundingRectsForGlyphs(fCTFont, kCTFontHorizontalOrientation, |
| 1017 | &cgGlyph, &cgBounds, 1); |
skia.committer@gmail.com | 539f364 | 2013-05-16 07:01:00 +0000 | [diff] [blame] | 1018 | |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 1019 | // BUG? |
| 1020 | // 0x200B (zero-advance space) seems to return a huge (garbage) bounds, when |
| 1021 | // it should be empty. So, if we see a zero-advance, we check if it has an |
| 1022 | // empty path or not, and if so, we jam the bounds to 0. Hopefully a zero-advance |
| 1023 | // is rare, so we won't incur a big performance cost for this extra check. |
| 1024 | if (0 == cgAdvance.width && 0 == cgAdvance.height) { |
| 1025 | AutoCFRelease<CGPathRef> path(CTFontCreatePathForGlyph(fCTFont, cgGlyph, NULL)); |
| 1026 | if (NULL == path || CGPathIsEmpty(path)) { |
| 1027 | return; |
| 1028 | } |
| 1029 | } |
| 1030 | |
| 1031 | if (CGRectIsEmpty_inline(cgBounds)) { |
| 1032 | return; |
| 1033 | } |
| 1034 | |
| 1035 | // Convert cgBounds to SkGlyph units (pixels, y down). |
| 1036 | skBounds = SkRect::MakeXYWH(cgBounds.origin.x, -cgBounds.origin.y - cgBounds.size.height, |
| 1037 | cgBounds.size.width, cgBounds.size.height); |
| 1038 | } |
| 1039 | |
| 1040 | if (fVertical) { |
| 1041 | // Due to all of the vertical bounds bugs, skBounds is always the horizontal bounds. |
| 1042 | // Convert these horizontal bounds into vertical bounds. |
| 1043 | SkPoint offset; |
| 1044 | getVerticalOffset(cgGlyph, &offset); |
| 1045 | skBounds.offset(offset); |
| 1046 | } |
skia.committer@gmail.com | 539f364 | 2013-05-16 07:01:00 +0000 | [diff] [blame] | 1047 | |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 1048 | // Currently the bounds are based on being rendered at (0,0). |
| 1049 | // The top left must not move, since that is the base from which subpixel positioning is offset. |
| 1050 | if (fDoSubPosition) { |
| 1051 | skBounds.fRight += SkFixedToFloat(glyph->getSubXFixed()); |
| 1052 | skBounds.fBottom += SkFixedToFloat(glyph->getSubYFixed()); |
| 1053 | } |
skia.committer@gmail.com | 539f364 | 2013-05-16 07:01:00 +0000 | [diff] [blame] | 1054 | |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 1055 | SkIRect skIBounds; |
| 1056 | skBounds.roundOut(&skIBounds); |
| 1057 | // Expand the bounds by 1 pixel, to give CG room for anti-aliasing. |
| 1058 | // Note that this outset is to allow room for LCD smoothed glyphs. However, the correct outset |
| 1059 | // is not currently known, as CG dilates the outlines by some percentage. |
| 1060 | // Note that if this context is A8 and not back-forming from LCD, there is no need to outset. |
| 1061 | skIBounds.outset(1, 1); |
| 1062 | glyph->fLeft = SkToS16(skIBounds.fLeft); |
| 1063 | glyph->fTop = SkToS16(skIBounds.fTop); |
| 1064 | glyph->fWidth = SkToU16(skIBounds.width()); |
| 1065 | glyph->fHeight = SkToU16(skIBounds.height()); |
skia.committer@gmail.com | 539f364 | 2013-05-16 07:01:00 +0000 | [diff] [blame] | 1066 | |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 1067 | #ifdef HACK_COLORGLYPHS |
| 1068 | glyph->fMaskFormat = SkMask::kARGB32_Format; |
| 1069 | #endif |
| 1070 | } |
| 1071 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1072 | #include "SkColorPriv.h" |
| 1073 | |
| 1074 | static void build_power_table(uint8_t table[], float ee) { |
| 1075 | for (int i = 0; i < 256; i++) { |
| 1076 | float x = i / 255.f; |
| 1077 | x = sk_float_pow(x, ee); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame^] | 1078 | int xx = SkScalarRoundToInt(x * 255); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1079 | table[i] = SkToU8(xx); |
| 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | /** |
| 1084 | * This will invert the gamma applied by CoreGraphics, so we can get linear |
| 1085 | * values. |
| 1086 | * |
| 1087 | * CoreGraphics obscurely defaults to 2.0 as the smoothing gamma value. |
| 1088 | * The color space used does not appear to affect this choice. |
| 1089 | */ |
| 1090 | static const uint8_t* getInverseGammaTableCoreGraphicSmoothing() { |
| 1091 | static bool gInited; |
| 1092 | static uint8_t gTableCoreGraphicsSmoothing[256]; |
| 1093 | if (!gInited) { |
| 1094 | build_power_table(gTableCoreGraphicsSmoothing, 2.0f); |
| 1095 | gInited = true; |
| 1096 | } |
| 1097 | return gTableCoreGraphicsSmoothing; |
| 1098 | } |
| 1099 | |
| 1100 | static void cgpixels_to_bits(uint8_t dst[], const CGRGBPixel src[], int count) { |
| 1101 | while (count > 0) { |
| 1102 | uint8_t mask = 0; |
| 1103 | for (int i = 7; i >= 0; --i) { |
| 1104 | mask |= (CGRGBPixel_getAlpha(*src++) >> 7) << i; |
| 1105 | if (0 == --count) { |
| 1106 | break; |
| 1107 | } |
| 1108 | } |
| 1109 | *dst++ = mask; |
| 1110 | } |
| 1111 | } |
| 1112 | |
| 1113 | template<bool APPLY_PREBLEND> |
| 1114 | static inline uint8_t rgb_to_a8(CGRGBPixel rgb, const uint8_t* table8) { |
| 1115 | U8CPU r = (rgb >> 16) & 0xFF; |
| 1116 | U8CPU g = (rgb >> 8) & 0xFF; |
| 1117 | U8CPU b = (rgb >> 0) & 0xFF; |
| 1118 | return sk_apply_lut_if<APPLY_PREBLEND>(SkComputeLuminance(r, g, b), table8); |
| 1119 | } |
| 1120 | template<bool APPLY_PREBLEND> |
| 1121 | static void rgb_to_a8(const CGRGBPixel* SK_RESTRICT cgPixels, size_t cgRowBytes, |
| 1122 | const SkGlyph& glyph, const uint8_t* table8) { |
| 1123 | const int width = glyph.fWidth; |
| 1124 | size_t dstRB = glyph.rowBytes(); |
| 1125 | uint8_t* SK_RESTRICT dst = (uint8_t*)glyph.fImage; |
| 1126 | |
| 1127 | for (int y = 0; y < glyph.fHeight; y++) { |
| 1128 | for (int i = 0; i < width; ++i) { |
| 1129 | dst[i] = rgb_to_a8<APPLY_PREBLEND>(cgPixels[i], table8); |
| 1130 | } |
| 1131 | cgPixels = (CGRGBPixel*)((char*)cgPixels + cgRowBytes); |
| 1132 | dst += dstRB; |
| 1133 | } |
| 1134 | } |
| 1135 | |
| 1136 | template<bool APPLY_PREBLEND> |
| 1137 | static inline uint16_t rgb_to_lcd16(CGRGBPixel rgb, const uint8_t* tableR, |
| 1138 | const uint8_t* tableG, |
| 1139 | const uint8_t* tableB) { |
| 1140 | U8CPU r = sk_apply_lut_if<APPLY_PREBLEND>((rgb >> 16) & 0xFF, tableR); |
| 1141 | U8CPU g = sk_apply_lut_if<APPLY_PREBLEND>((rgb >> 8) & 0xFF, tableG); |
| 1142 | U8CPU b = sk_apply_lut_if<APPLY_PREBLEND>((rgb >> 0) & 0xFF, tableB); |
| 1143 | return SkPack888ToRGB16(r, g, b); |
| 1144 | } |
| 1145 | template<bool APPLY_PREBLEND> |
| 1146 | static void rgb_to_lcd16(const CGRGBPixel* SK_RESTRICT cgPixels, size_t cgRowBytes, const SkGlyph& glyph, |
| 1147 | const uint8_t* tableR, const uint8_t* tableG, const uint8_t* tableB) { |
| 1148 | const int width = glyph.fWidth; |
| 1149 | size_t dstRB = glyph.rowBytes(); |
| 1150 | uint16_t* SK_RESTRICT dst = (uint16_t*)glyph.fImage; |
| 1151 | |
| 1152 | for (int y = 0; y < glyph.fHeight; y++) { |
| 1153 | for (int i = 0; i < width; i++) { |
| 1154 | dst[i] = rgb_to_lcd16<APPLY_PREBLEND>(cgPixels[i], tableR, tableG, tableB); |
| 1155 | } |
| 1156 | cgPixels = (CGRGBPixel*)((char*)cgPixels + cgRowBytes); |
| 1157 | dst = (uint16_t*)((char*)dst + dstRB); |
| 1158 | } |
| 1159 | } |
| 1160 | |
| 1161 | template<bool APPLY_PREBLEND> |
| 1162 | static inline uint32_t rgb_to_lcd32(CGRGBPixel rgb, const uint8_t* tableR, |
| 1163 | const uint8_t* tableG, |
| 1164 | const uint8_t* tableB) { |
| 1165 | U8CPU r = sk_apply_lut_if<APPLY_PREBLEND>((rgb >> 16) & 0xFF, tableR); |
| 1166 | U8CPU g = sk_apply_lut_if<APPLY_PREBLEND>((rgb >> 8) & 0xFF, tableG); |
| 1167 | U8CPU b = sk_apply_lut_if<APPLY_PREBLEND>((rgb >> 0) & 0xFF, tableB); |
| 1168 | return SkPackARGB32(0xFF, r, g, b); |
| 1169 | } |
| 1170 | template<bool APPLY_PREBLEND> |
| 1171 | static void rgb_to_lcd32(const CGRGBPixel* SK_RESTRICT cgPixels, size_t cgRowBytes, const SkGlyph& glyph, |
| 1172 | const uint8_t* tableR, const uint8_t* tableG, const uint8_t* tableB) { |
| 1173 | const int width = glyph.fWidth; |
| 1174 | size_t dstRB = glyph.rowBytes(); |
| 1175 | uint32_t* SK_RESTRICT dst = (uint32_t*)glyph.fImage; |
| 1176 | for (int y = 0; y < glyph.fHeight; y++) { |
| 1177 | for (int i = 0; i < width; i++) { |
| 1178 | dst[i] = rgb_to_lcd32<APPLY_PREBLEND>(cgPixels[i], tableR, tableG, tableB); |
| 1179 | } |
| 1180 | cgPixels = (CGRGBPixel*)((char*)cgPixels + cgRowBytes); |
| 1181 | dst = (uint32_t*)((char*)dst + dstRB); |
| 1182 | } |
| 1183 | } |
| 1184 | |
reed@google.com | f77b35d | 2013-05-02 20:39:44 +0000 | [diff] [blame] | 1185 | #ifdef HACK_COLORGLYPHS |
| 1186 | // hack to colorize the output for testing kARGB32_Format |
| 1187 | static SkPMColor cgpixels_to_pmcolor(CGRGBPixel rgb, const SkGlyph& glyph, |
| 1188 | int x, int y) { |
| 1189 | U8CPU r = (rgb >> 16) & 0xFF; |
| 1190 | U8CPU g = (rgb >> 8) & 0xFF; |
| 1191 | U8CPU b = (rgb >> 0) & 0xFF; |
| 1192 | unsigned a = SkComputeLuminance(r, g, b); |
skia.committer@gmail.com | 2fd42c4 | 2013-05-03 07:01:00 +0000 | [diff] [blame] | 1193 | |
reed@google.com | f77b35d | 2013-05-02 20:39:44 +0000 | [diff] [blame] | 1194 | // compute gradient from x,y |
| 1195 | r = x * 255 / glyph.fWidth; |
| 1196 | g = 0; |
| 1197 | b = (glyph.fHeight - y) * 255 / glyph.fHeight; |
| 1198 | return SkPreMultiplyARGB(a, r, g, b); // red |
| 1199 | } |
| 1200 | #endif |
| 1201 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1202 | template <typename T> T* SkTAddByteOffset(T* ptr, size_t byteOffset) { |
| 1203 | return (T*)((char*)ptr + byteOffset); |
| 1204 | } |
| 1205 | |
| 1206 | void SkScalerContext_Mac::generateImage(const SkGlyph& glyph) { |
| 1207 | CGGlyph cgGlyph = (CGGlyph) glyph.getGlyphID(fBaseGlyphCount); |
| 1208 | |
| 1209 | // FIXME: lcd smoothed un-hinted rasterization unsupported. |
| 1210 | bool generateA8FromLCD = fRec.getHinting() != SkPaint::kNo_Hinting; |
| 1211 | |
| 1212 | // Draw the glyph |
| 1213 | size_t cgRowBytes; |
| 1214 | CGRGBPixel* cgPixels = fOffscreen.getCG(*this, glyph, cgGlyph, &cgRowBytes, generateA8FromLCD); |
| 1215 | if (cgPixels == NULL) { |
| 1216 | return; |
| 1217 | } |
| 1218 | |
| 1219 | //TODO: see if drawing black on white and inverting is faster (at least in |
| 1220 | //lcd case) as core graphics appears to have special case code for drawing |
| 1221 | //black text. |
| 1222 | |
| 1223 | // Fix the glyph |
| 1224 | const bool isLCD = isLCDFormat(glyph.fMaskFormat); |
| 1225 | if (isLCD || (glyph.fMaskFormat == SkMask::kA8_Format && supports_LCD() && generateA8FromLCD)) { |
| 1226 | const uint8_t* table = getInverseGammaTableCoreGraphicSmoothing(); |
| 1227 | |
| 1228 | //Note that the following cannot really be integrated into the |
| 1229 | //pre-blend, since we may not be applying the pre-blend; when we aren't |
| 1230 | //applying the pre-blend it means that a filter wants linear anyway. |
| 1231 | //Other code may also be applying the pre-blend, so we'd need another |
| 1232 | //one with this and one without. |
| 1233 | CGRGBPixel* addr = cgPixels; |
| 1234 | for (int y = 0; y < glyph.fHeight; ++y) { |
| 1235 | for (int x = 0; x < glyph.fWidth; ++x) { |
| 1236 | int r = (addr[x] >> 16) & 0xFF; |
| 1237 | int g = (addr[x] >> 8) & 0xFF; |
| 1238 | int b = (addr[x] >> 0) & 0xFF; |
| 1239 | addr[x] = (table[r] << 16) | (table[g] << 8) | table[b]; |
| 1240 | } |
| 1241 | addr = SkTAddByteOffset(addr, cgRowBytes); |
| 1242 | } |
| 1243 | } |
| 1244 | |
| 1245 | // Convert glyph to mask |
| 1246 | switch (glyph.fMaskFormat) { |
| 1247 | case SkMask::kLCD32_Format: { |
| 1248 | if (fPreBlend.isApplicable()) { |
| 1249 | rgb_to_lcd32<true>(cgPixels, cgRowBytes, glyph, |
| 1250 | fPreBlend.fR, fPreBlend.fG, fPreBlend.fB); |
| 1251 | } else { |
| 1252 | rgb_to_lcd32<false>(cgPixels, cgRowBytes, glyph, |
| 1253 | fPreBlend.fR, fPreBlend.fG, fPreBlend.fB); |
| 1254 | } |
| 1255 | } break; |
| 1256 | case SkMask::kLCD16_Format: { |
| 1257 | if (fPreBlend.isApplicable()) { |
| 1258 | rgb_to_lcd16<true>(cgPixels, cgRowBytes, glyph, |
| 1259 | fPreBlend.fR, fPreBlend.fG, fPreBlend.fB); |
| 1260 | } else { |
| 1261 | rgb_to_lcd16<false>(cgPixels, cgRowBytes, glyph, |
| 1262 | fPreBlend.fR, fPreBlend.fG, fPreBlend.fB); |
| 1263 | } |
| 1264 | } break; |
| 1265 | case SkMask::kA8_Format: { |
| 1266 | if (fPreBlend.isApplicable()) { |
| 1267 | rgb_to_a8<true>(cgPixels, cgRowBytes, glyph, fPreBlend.fG); |
| 1268 | } else { |
| 1269 | rgb_to_a8<false>(cgPixels, cgRowBytes, glyph, fPreBlend.fG); |
| 1270 | } |
| 1271 | } break; |
| 1272 | case SkMask::kBW_Format: { |
| 1273 | const int width = glyph.fWidth; |
| 1274 | size_t dstRB = glyph.rowBytes(); |
| 1275 | uint8_t* dst = (uint8_t*)glyph.fImage; |
| 1276 | for (int y = 0; y < glyph.fHeight; y++) { |
| 1277 | cgpixels_to_bits(dst, cgPixels, width); |
| 1278 | cgPixels = (CGRGBPixel*)((char*)cgPixels + cgRowBytes); |
| 1279 | dst += dstRB; |
| 1280 | } |
| 1281 | } break; |
reed@google.com | f77b35d | 2013-05-02 20:39:44 +0000 | [diff] [blame] | 1282 | #ifdef HACK_COLORGLYPHS |
| 1283 | case SkMask::kARGB32_Format: { |
| 1284 | const int width = glyph.fWidth; |
| 1285 | size_t dstRB = glyph.rowBytes(); |
| 1286 | SkPMColor* dst = (SkPMColor*)glyph.fImage; |
| 1287 | for (int y = 0; y < glyph.fHeight; y++) { |
| 1288 | for (int x = 0; x < width; ++x) { |
| 1289 | dst[x] = cgpixels_to_pmcolor(cgPixels[x], glyph, x, y); |
| 1290 | } |
| 1291 | cgPixels = (CGRGBPixel*)((char*)cgPixels + cgRowBytes); |
| 1292 | dst = (SkPMColor*)((char*)dst + dstRB); |
| 1293 | } |
| 1294 | } break; |
| 1295 | #endif |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1296 | default: |
| 1297 | SkDEBUGFAIL("unexpected mask format"); |
| 1298 | break; |
| 1299 | } |
| 1300 | } |
| 1301 | |
| 1302 | /* |
| 1303 | * Our subpixel resolution is only 2 bits in each direction, so a scale of 4 |
| 1304 | * seems sufficient, and possibly even correct, to allow the hinted outline |
| 1305 | * to be subpixel positioned. |
| 1306 | */ |
| 1307 | #define kScaleForSubPixelPositionHinting (4.0f) |
| 1308 | |
| 1309 | void SkScalerContext_Mac::generatePath(const SkGlyph& glyph, SkPath* path) { |
| 1310 | CTFontRef font = fCTFont; |
| 1311 | SkScalar scaleX = SK_Scalar1; |
| 1312 | SkScalar scaleY = SK_Scalar1; |
| 1313 | |
| 1314 | /* |
| 1315 | * For subpixel positioning, we want to return an unhinted outline, so it |
| 1316 | * can be positioned nicely at fractional offsets. However, we special-case |
| 1317 | * if the baseline of the (horizontal) text is axis-aligned. In those cases |
| 1318 | * we want to retain hinting in the direction orthogonal to the baseline. |
| 1319 | * e.g. for horizontal baseline, we want to retain hinting in Y. |
| 1320 | * The way we remove hinting is to scale the font by some value (4) in that |
| 1321 | * direction, ask for the path, and then scale the path back down. |
| 1322 | */ |
| 1323 | if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) { |
| 1324 | SkMatrix m; |
| 1325 | fRec.getSingleMatrix(&m); |
| 1326 | |
| 1327 | // start out by assuming that we want no hining in X and Y |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame^] | 1328 | scaleX = scaleY = kScaleForSubPixelPositionHinting; |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1329 | // now see if we need to restore hinting for axis-aligned baselines |
| 1330 | switch (SkComputeAxisAlignmentForHText(m)) { |
| 1331 | case kX_SkAxisAlignment: |
| 1332 | scaleY = SK_Scalar1; // want hinting in the Y direction |
| 1333 | break; |
| 1334 | case kY_SkAxisAlignment: |
| 1335 | scaleX = SK_Scalar1; // want hinting in the X direction |
| 1336 | break; |
| 1337 | default: |
| 1338 | break; |
| 1339 | } |
| 1340 | |
| 1341 | CGAffineTransform xform = MatrixToCGAffineTransform(m, scaleX, scaleY); |
| 1342 | // need to release font when we're done |
| 1343 | font = CTFontCreateCopyWithAttributes(fCTFont, 1, &xform, NULL); |
| 1344 | } |
| 1345 | |
| 1346 | CGGlyph cgGlyph = (CGGlyph)glyph.getGlyphID(fBaseGlyphCount); |
| 1347 | AutoCFRelease<CGPathRef> cgPath(CTFontCreatePathForGlyph(font, cgGlyph, NULL)); |
| 1348 | |
| 1349 | path->reset(); |
| 1350 | if (cgPath != NULL) { |
| 1351 | CGPathApply(cgPath, path, SkScalerContext_Mac::CTPathElement); |
| 1352 | } |
| 1353 | |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 1354 | if (fDoSubPosition) { |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1355 | SkMatrix m; |
| 1356 | m.setScale(SkScalarInvert(scaleX), SkScalarInvert(scaleY)); |
| 1357 | path->transform(m); |
| 1358 | // balance the call to CTFontCreateCopyWithAttributes |
| 1359 | CFSafeRelease(font); |
| 1360 | } |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 1361 | if (fVertical) { |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 1362 | SkPoint offset; |
| 1363 | getVerticalOffset(cgGlyph, &offset); |
| 1364 | path->offset(offset.fX, offset.fY); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1365 | } |
| 1366 | } |
| 1367 | |
| 1368 | void SkScalerContext_Mac::generateFontMetrics(SkPaint::FontMetrics* mx, |
| 1369 | SkPaint::FontMetrics* my) { |
| 1370 | CGRect theBounds = CTFontGetBoundingBox(fCTFont); |
| 1371 | |
| 1372 | SkPaint::FontMetrics theMetrics; |
| 1373 | theMetrics.fTop = CGToScalar(-CGRectGetMaxY_inline(theBounds)); |
| 1374 | theMetrics.fAscent = CGToScalar(-CTFontGetAscent(fCTFont)); |
| 1375 | theMetrics.fDescent = CGToScalar( CTFontGetDescent(fCTFont)); |
| 1376 | theMetrics.fBottom = CGToScalar(-CGRectGetMinY_inline(theBounds)); |
| 1377 | theMetrics.fLeading = CGToScalar( CTFontGetLeading(fCTFont)); |
| 1378 | theMetrics.fAvgCharWidth = CGToScalar( CGRectGetWidth_inline(theBounds)); |
| 1379 | theMetrics.fXMin = CGToScalar( CGRectGetMinX_inline(theBounds)); |
| 1380 | theMetrics.fXMax = CGToScalar( CGRectGetMaxX_inline(theBounds)); |
| 1381 | theMetrics.fXHeight = CGToScalar( CTFontGetXHeight(fCTFont)); |
| 1382 | |
| 1383 | if (mx != NULL) { |
| 1384 | *mx = theMetrics; |
| 1385 | } |
| 1386 | if (my != NULL) { |
| 1387 | *my = theMetrics; |
| 1388 | } |
| 1389 | } |
| 1390 | |
| 1391 | void SkScalerContext_Mac::CTPathElement(void *info, const CGPathElement *element) { |
| 1392 | SkPath* skPath = (SkPath*)info; |
| 1393 | |
| 1394 | // Process the path element |
| 1395 | switch (element->type) { |
| 1396 | case kCGPathElementMoveToPoint: |
| 1397 | skPath->moveTo(element->points[0].x, -element->points[0].y); |
| 1398 | break; |
| 1399 | |
| 1400 | case kCGPathElementAddLineToPoint: |
| 1401 | skPath->lineTo(element->points[0].x, -element->points[0].y); |
| 1402 | break; |
| 1403 | |
| 1404 | case kCGPathElementAddQuadCurveToPoint: |
| 1405 | skPath->quadTo(element->points[0].x, -element->points[0].y, |
| 1406 | element->points[1].x, -element->points[1].y); |
| 1407 | break; |
| 1408 | |
| 1409 | case kCGPathElementAddCurveToPoint: |
| 1410 | skPath->cubicTo(element->points[0].x, -element->points[0].y, |
| 1411 | element->points[1].x, -element->points[1].y, |
| 1412 | element->points[2].x, -element->points[2].y); |
| 1413 | break; |
| 1414 | |
| 1415 | case kCGPathElementCloseSubpath: |
| 1416 | skPath->close(); |
| 1417 | break; |
| 1418 | |
| 1419 | default: |
| 1420 | SkDEBUGFAIL("Unknown path element!"); |
| 1421 | break; |
| 1422 | } |
| 1423 | } |
| 1424 | |
| 1425 | |
| 1426 | /////////////////////////////////////////////////////////////////////////////// |
| 1427 | |
| 1428 | // Returns NULL on failure |
| 1429 | // Call must still manage its ownership of provider |
| 1430 | static SkTypeface* create_from_dataProvider(CGDataProviderRef provider) { |
| 1431 | AutoCFRelease<CGFontRef> cg(CGFontCreateWithDataProvider(provider)); |
| 1432 | if (NULL == cg) { |
| 1433 | return NULL; |
| 1434 | } |
| 1435 | CTFontRef ct = CTFontCreateWithGraphicsFont(cg, 0, NULL, NULL); |
| 1436 | return cg ? SkCreateTypefaceFromCTFont(ct) : NULL; |
| 1437 | } |
| 1438 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1439 | // Web fonts added to the the CTFont registry do not return their character set. |
| 1440 | // Iterate through the font in this case. The existing caller caches the result, |
| 1441 | // so the performance impact isn't too bad. |
| 1442 | static void populate_glyph_to_unicode_slow(CTFontRef ctFont, CFIndex glyphCount, |
| 1443 | SkTDArray<SkUnichar>* glyphToUnicode) { |
| 1444 | glyphToUnicode->setCount(glyphCount); |
| 1445 | SkUnichar* out = glyphToUnicode->begin(); |
| 1446 | sk_bzero(out, glyphCount * sizeof(SkUnichar)); |
| 1447 | UniChar unichar = 0; |
| 1448 | while (glyphCount > 0) { |
| 1449 | CGGlyph glyph; |
| 1450 | if (CTFontGetGlyphsForCharacters(ctFont, &unichar, &glyph, 1)) { |
| 1451 | out[glyph] = unichar; |
| 1452 | --glyphCount; |
| 1453 | } |
| 1454 | if (++unichar == 0) { |
| 1455 | break; |
| 1456 | } |
| 1457 | } |
| 1458 | } |
| 1459 | |
| 1460 | // Construct Glyph to Unicode table. |
| 1461 | // Unicode code points that require conjugate pairs in utf16 are not |
| 1462 | // supported. |
| 1463 | static void populate_glyph_to_unicode(CTFontRef ctFont, CFIndex glyphCount, |
| 1464 | SkTDArray<SkUnichar>* glyphToUnicode) { |
| 1465 | AutoCFRelease<CFCharacterSetRef> charSet(CTFontCopyCharacterSet(ctFont)); |
| 1466 | if (!charSet) { |
| 1467 | populate_glyph_to_unicode_slow(ctFont, glyphCount, glyphToUnicode); |
| 1468 | return; |
| 1469 | } |
| 1470 | |
| 1471 | AutoCFRelease<CFDataRef> bitmap(CFCharacterSetCreateBitmapRepresentation(kCFAllocatorDefault, |
| 1472 | charSet)); |
| 1473 | if (!bitmap) { |
| 1474 | return; |
| 1475 | } |
| 1476 | CFIndex length = CFDataGetLength(bitmap); |
| 1477 | if (!length) { |
| 1478 | return; |
| 1479 | } |
| 1480 | if (length > 8192) { |
| 1481 | // TODO: Add support for Unicode above 0xFFFF |
| 1482 | // Consider only the BMP portion of the Unicode character points. |
| 1483 | // The bitmap may contain other planes, up to plane 16. |
| 1484 | // See http://developer.apple.com/library/ios/#documentation/CoreFoundation/Reference/CFCharacterSetRef/Reference/reference.html |
| 1485 | length = 8192; |
| 1486 | } |
| 1487 | const UInt8* bits = CFDataGetBytePtr(bitmap); |
| 1488 | glyphToUnicode->setCount(glyphCount); |
| 1489 | SkUnichar* out = glyphToUnicode->begin(); |
| 1490 | sk_bzero(out, glyphCount * sizeof(SkUnichar)); |
| 1491 | for (int i = 0; i < length; i++) { |
| 1492 | int mask = bits[i]; |
| 1493 | if (!mask) { |
| 1494 | continue; |
| 1495 | } |
| 1496 | for (int j = 0; j < 8; j++) { |
| 1497 | CGGlyph glyph; |
| 1498 | UniChar unichar = static_cast<UniChar>((i << 3) + j); |
| 1499 | if (mask & (1 << j) && CTFontGetGlyphsForCharacters(ctFont, &unichar, &glyph, 1)) { |
| 1500 | out[glyph] = unichar; |
| 1501 | } |
| 1502 | } |
| 1503 | } |
| 1504 | } |
| 1505 | |
| 1506 | static bool getWidthAdvance(CTFontRef ctFont, int gId, int16_t* data) { |
| 1507 | CGSize advance; |
| 1508 | advance.width = 0; |
| 1509 | CGGlyph glyph = gId; |
| 1510 | CTFontGetAdvancesForGlyphs(ctFont, kCTFontHorizontalOrientation, &glyph, &advance, 1); |
| 1511 | *data = sk_float_round2int(advance.width); |
| 1512 | return true; |
| 1513 | } |
| 1514 | |
| 1515 | // we might move this into our CGUtils... |
| 1516 | static void CFStringToSkString(CFStringRef src, SkString* dst) { |
| 1517 | // Reserve enough room for the worst-case string, |
| 1518 | // plus 1 byte for the trailing null. |
| 1519 | CFIndex length = CFStringGetMaximumSizeForEncoding(CFStringGetLength(src), |
| 1520 | kCFStringEncodingUTF8) + 1; |
| 1521 | dst->resize(length); |
| 1522 | CFStringGetCString(src, dst->writable_str(), length, kCFStringEncodingUTF8); |
| 1523 | // Resize to the actual UTF-8 length used, stripping the null character. |
| 1524 | dst->resize(strlen(dst->c_str())); |
| 1525 | } |
| 1526 | |
reed@google.com | 2689f61 | 2013-03-20 20:01:47 +0000 | [diff] [blame] | 1527 | SkAdvancedTypefaceMetrics* SkTypeface_Mac::onGetAdvancedTypefaceMetrics( |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1528 | SkAdvancedTypefaceMetrics::PerGlyphInfo perGlyphInfo, |
| 1529 | const uint32_t* glyphIDs, |
reed@google.com | 2689f61 | 2013-03-20 20:01:47 +0000 | [diff] [blame] | 1530 | uint32_t glyphIDsCount) const { |
| 1531 | |
| 1532 | CTFontRef originalCTFont = fFontRef.get(); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1533 | AutoCFRelease<CTFontRef> ctFont(CTFontCreateCopyWithAttributes( |
| 1534 | originalCTFont, CTFontGetUnitsPerEm(originalCTFont), NULL, NULL)); |
| 1535 | SkAdvancedTypefaceMetrics* info = new SkAdvancedTypefaceMetrics; |
| 1536 | |
| 1537 | { |
| 1538 | AutoCFRelease<CFStringRef> fontName(CTFontCopyPostScriptName(ctFont)); |
| 1539 | CFStringToSkString(fontName, &info->fFontName); |
| 1540 | } |
| 1541 | |
| 1542 | info->fMultiMaster = false; |
| 1543 | CFIndex glyphCount = CTFontGetGlyphCount(ctFont); |
| 1544 | info->fLastGlyphID = SkToU16(glyphCount - 1); |
| 1545 | info->fEmSize = CTFontGetUnitsPerEm(ctFont); |
| 1546 | |
| 1547 | if (perGlyphInfo & SkAdvancedTypefaceMetrics::kToUnicode_PerGlyphInfo) { |
| 1548 | populate_glyph_to_unicode(ctFont, glyphCount, &info->fGlyphToUnicode); |
| 1549 | } |
| 1550 | |
| 1551 | info->fStyle = 0; |
| 1552 | |
| 1553 | // If it's not a truetype font, mark it as 'other'. Assume that TrueType |
| 1554 | // fonts always have both glyf and loca tables. At the least, this is what |
| 1555 | // sfntly needs to subset the font. CTFontCopyAttribute() does not always |
| 1556 | // succeed in determining this directly. |
reed@google.com | 2689f61 | 2013-03-20 20:01:47 +0000 | [diff] [blame] | 1557 | if (!this->getTableSize('glyf') || !this->getTableSize('loca')) { |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1558 | info->fType = SkAdvancedTypefaceMetrics::kOther_Font; |
| 1559 | info->fItalicAngle = 0; |
| 1560 | info->fAscent = 0; |
| 1561 | info->fDescent = 0; |
| 1562 | info->fStemV = 0; |
| 1563 | info->fCapHeight = 0; |
| 1564 | info->fBBox = SkIRect::MakeEmpty(); |
| 1565 | return info; |
| 1566 | } |
| 1567 | |
| 1568 | info->fType = SkAdvancedTypefaceMetrics::kTrueType_Font; |
| 1569 | CTFontSymbolicTraits symbolicTraits = CTFontGetSymbolicTraits(ctFont); |
| 1570 | if (symbolicTraits & kCTFontMonoSpaceTrait) { |
| 1571 | info->fStyle |= SkAdvancedTypefaceMetrics::kFixedPitch_Style; |
| 1572 | } |
| 1573 | if (symbolicTraits & kCTFontItalicTrait) { |
| 1574 | info->fStyle |= SkAdvancedTypefaceMetrics::kItalic_Style; |
| 1575 | } |
| 1576 | CTFontStylisticClass stylisticClass = symbolicTraits & kCTFontClassMaskTrait; |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1577 | if (stylisticClass >= kCTFontOldStyleSerifsClass && stylisticClass <= kCTFontSlabSerifsClass) { |
| 1578 | info->fStyle |= SkAdvancedTypefaceMetrics::kSerif_Style; |
| 1579 | } else if (stylisticClass & kCTFontScriptsClass) { |
| 1580 | info->fStyle |= SkAdvancedTypefaceMetrics::kScript_Style; |
| 1581 | } |
| 1582 | info->fItalicAngle = (int16_t) CTFontGetSlantAngle(ctFont); |
| 1583 | info->fAscent = (int16_t) CTFontGetAscent(ctFont); |
| 1584 | info->fDescent = (int16_t) CTFontGetDescent(ctFont); |
| 1585 | info->fCapHeight = (int16_t) CTFontGetCapHeight(ctFont); |
| 1586 | CGRect bbox = CTFontGetBoundingBox(ctFont); |
| 1587 | |
| 1588 | SkRect r; |
| 1589 | r.set( CGToScalar(CGRectGetMinX_inline(bbox)), // Left |
| 1590 | CGToScalar(CGRectGetMaxY_inline(bbox)), // Top |
| 1591 | CGToScalar(CGRectGetMaxX_inline(bbox)), // Right |
| 1592 | CGToScalar(CGRectGetMinY_inline(bbox))); // Bottom |
| 1593 | |
| 1594 | r.roundOut(&(info->fBBox)); |
| 1595 | |
| 1596 | // Figure out a good guess for StemV - Min width of i, I, !, 1. |
| 1597 | // This probably isn't very good with an italic font. |
| 1598 | int16_t min_width = SHRT_MAX; |
| 1599 | info->fStemV = 0; |
| 1600 | static const UniChar stem_chars[] = {'i', 'I', '!', '1'}; |
| 1601 | const size_t count = sizeof(stem_chars) / sizeof(stem_chars[0]); |
| 1602 | CGGlyph glyphs[count]; |
| 1603 | CGRect boundingRects[count]; |
| 1604 | if (CTFontGetGlyphsForCharacters(ctFont, stem_chars, glyphs, count)) { |
| 1605 | CTFontGetBoundingRectsForGlyphs(ctFont, kCTFontHorizontalOrientation, |
| 1606 | glyphs, boundingRects, count); |
| 1607 | for (size_t i = 0; i < count; i++) { |
| 1608 | int16_t width = (int16_t) boundingRects[i].size.width; |
| 1609 | if (width > 0 && width < min_width) { |
| 1610 | min_width = width; |
| 1611 | info->fStemV = min_width; |
| 1612 | } |
| 1613 | } |
| 1614 | } |
| 1615 | |
| 1616 | if (false) { // TODO: haven't figured out how to know if font is embeddable |
| 1617 | // (information is in the OS/2 table) |
| 1618 | info->fType = SkAdvancedTypefaceMetrics::kNotEmbeddable_Font; |
| 1619 | } else if (perGlyphInfo & SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo) { |
| 1620 | if (info->fStyle & SkAdvancedTypefaceMetrics::kFixedPitch_Style) { |
| 1621 | skia_advanced_typeface_metrics_utils::appendRange(&info->fGlyphWidths, 0); |
| 1622 | info->fGlyphWidths->fAdvance.append(1, &min_width); |
| 1623 | skia_advanced_typeface_metrics_utils::finishRange(info->fGlyphWidths.get(), 0, |
| 1624 | SkAdvancedTypefaceMetrics::WidthRange::kDefault); |
| 1625 | } else { |
| 1626 | info->fGlyphWidths.reset( |
| 1627 | skia_advanced_typeface_metrics_utils::getAdvanceData(ctFont.get(), |
| 1628 | glyphCount, |
| 1629 | glyphIDs, |
| 1630 | glyphIDsCount, |
| 1631 | &getWidthAdvance)); |
| 1632 | } |
| 1633 | } |
| 1634 | return info; |
| 1635 | } |
| 1636 | |
| 1637 | /////////////////////////////////////////////////////////////////////////////// |
| 1638 | |
reed@google.com | cc9aad5 | 2013-03-21 19:28:10 +0000 | [diff] [blame] | 1639 | static SK_SFNT_ULONG get_font_type_tag(const SkTypeface_Mac* typeface) { |
| 1640 | CTFontRef ctFont = typeface->fFontRef.get(); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1641 | AutoCFRelease<CFNumberRef> fontFormatRef( |
| 1642 | static_cast<CFNumberRef>(CTFontCopyAttribute(ctFont, kCTFontFormatAttribute))); |
| 1643 | if (!fontFormatRef) { |
| 1644 | return 0; |
| 1645 | } |
| 1646 | |
| 1647 | SInt32 fontFormatValue; |
| 1648 | if (!CFNumberGetValue(fontFormatRef, kCFNumberSInt32Type, &fontFormatValue)) { |
| 1649 | return 0; |
| 1650 | } |
| 1651 | |
| 1652 | switch (fontFormatValue) { |
| 1653 | case kCTFontFormatOpenTypePostScript: |
| 1654 | return SkSFNTHeader::fontType_OpenTypeCFF::TAG; |
| 1655 | case kCTFontFormatOpenTypeTrueType: |
| 1656 | return SkSFNTHeader::fontType_WindowsTrueType::TAG; |
| 1657 | case kCTFontFormatTrueType: |
| 1658 | return SkSFNTHeader::fontType_MacTrueType::TAG; |
| 1659 | case kCTFontFormatPostScript: |
| 1660 | return SkSFNTHeader::fontType_PostScript::TAG; |
| 1661 | case kCTFontFormatBitmap: |
| 1662 | return SkSFNTHeader::fontType_MacTrueType::TAG; |
| 1663 | case kCTFontFormatUnrecognized: |
| 1664 | default: |
| 1665 | //CT seems to be unreliable in being able to obtain the type, |
| 1666 | //even if all we want is the first four bytes of the font resource. |
| 1667 | //Just the presence of the FontForge 'FFTM' table seems to throw it off. |
| 1668 | return SkSFNTHeader::fontType_WindowsTrueType::TAG; |
| 1669 | } |
| 1670 | } |
| 1671 | |
reed@google.com | cc9aad5 | 2013-03-21 19:28:10 +0000 | [diff] [blame] | 1672 | SkStream* SkTypeface_Mac::onOpenStream(int* ttcIndex) const { |
| 1673 | SK_SFNT_ULONG fontType = get_font_type_tag(this); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1674 | if (0 == fontType) { |
| 1675 | return NULL; |
| 1676 | } |
| 1677 | |
| 1678 | // get table tags |
reed@google.com | cc9aad5 | 2013-03-21 19:28:10 +0000 | [diff] [blame] | 1679 | int numTables = this->countTables(); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1680 | SkTDArray<SkFontTableTag> tableTags; |
| 1681 | tableTags.setCount(numTables); |
reed@google.com | cc9aad5 | 2013-03-21 19:28:10 +0000 | [diff] [blame] | 1682 | this->getTableTags(tableTags.begin()); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1683 | |
| 1684 | // calc total size for font, save sizes |
| 1685 | SkTDArray<size_t> tableSizes; |
| 1686 | size_t totalSize = sizeof(SkSFNTHeader) + sizeof(SkSFNTHeader::TableDirectoryEntry) * numTables; |
| 1687 | for (int tableIndex = 0; tableIndex < numTables; ++tableIndex) { |
reed@google.com | cc9aad5 | 2013-03-21 19:28:10 +0000 | [diff] [blame] | 1688 | size_t tableSize = this->getTableSize(tableTags[tableIndex]); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1689 | totalSize += (tableSize + 3) & ~3; |
| 1690 | *tableSizes.append() = tableSize; |
| 1691 | } |
| 1692 | |
| 1693 | // reserve memory for stream, and zero it (tables must be zero padded) |
| 1694 | SkMemoryStream* stream = new SkMemoryStream(totalSize); |
| 1695 | char* dataStart = (char*)stream->getMemoryBase(); |
| 1696 | sk_bzero(dataStart, totalSize); |
| 1697 | char* dataPtr = dataStart; |
| 1698 | |
| 1699 | // compute font header entries |
| 1700 | uint16_t entrySelector = 0; |
| 1701 | uint16_t searchRange = 1; |
| 1702 | while (searchRange < numTables >> 1) { |
| 1703 | entrySelector++; |
| 1704 | searchRange <<= 1; |
| 1705 | } |
| 1706 | searchRange <<= 4; |
| 1707 | uint16_t rangeShift = (numTables << 4) - searchRange; |
| 1708 | |
| 1709 | // write font header |
| 1710 | SkSFNTHeader* header = (SkSFNTHeader*)dataPtr; |
| 1711 | header->fontType = fontType; |
| 1712 | header->numTables = SkEndian_SwapBE16(numTables); |
| 1713 | header->searchRange = SkEndian_SwapBE16(searchRange); |
| 1714 | header->entrySelector = SkEndian_SwapBE16(entrySelector); |
| 1715 | header->rangeShift = SkEndian_SwapBE16(rangeShift); |
| 1716 | dataPtr += sizeof(SkSFNTHeader); |
| 1717 | |
| 1718 | // write tables |
| 1719 | SkSFNTHeader::TableDirectoryEntry* entry = (SkSFNTHeader::TableDirectoryEntry*)dataPtr; |
| 1720 | dataPtr += sizeof(SkSFNTHeader::TableDirectoryEntry) * numTables; |
| 1721 | for (int tableIndex = 0; tableIndex < numTables; ++tableIndex) { |
| 1722 | size_t tableSize = tableSizes[tableIndex]; |
reed@google.com | cc9aad5 | 2013-03-21 19:28:10 +0000 | [diff] [blame] | 1723 | this->getTableData(tableTags[tableIndex], 0, tableSize, dataPtr); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1724 | entry->tag = SkEndian_SwapBE32(tableTags[tableIndex]); |
| 1725 | entry->checksum = SkEndian_SwapBE32(SkOTUtils::CalcTableChecksum((SK_OT_ULONG*)dataPtr, |
| 1726 | tableSize)); |
| 1727 | entry->offset = SkEndian_SwapBE32(dataPtr - dataStart); |
| 1728 | entry->logicalLength = SkEndian_SwapBE32(tableSize); |
| 1729 | |
| 1730 | dataPtr += (tableSize + 3) & ~3; |
| 1731 | ++entry; |
| 1732 | } |
| 1733 | |
| 1734 | return stream; |
| 1735 | } |
| 1736 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1737 | /////////////////////////////////////////////////////////////////////////////// |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1738 | /////////////////////////////////////////////////////////////////////////////// |
| 1739 | |
| 1740 | int SkTypeface_Mac::onGetUPEM() const { |
| 1741 | AutoCFRelease<CGFontRef> cgFont(CTFontCopyGraphicsFont(fFontRef, NULL)); |
| 1742 | return CGFontGetUnitsPerEm(cgFont); |
| 1743 | } |
| 1744 | |
bungeman@google.com | 839702b | 2013-08-07 17:09:22 +0000 | [diff] [blame] | 1745 | SkTypeface::LocalizedStrings* SkTypeface_Mac::onCreateFamilyNameIterator() const { |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 1746 | SkTypeface::LocalizedStrings* nameIter = |
| 1747 | SkOTUtils::LocalizedStrings_NameTable::CreateForFamilyNames(*this); |
| 1748 | if (NULL == nameIter) { |
| 1749 | AutoCFRelease<CFStringRef> cfLanguage; |
| 1750 | AutoCFRelease<CFStringRef> cfFamilyName( |
| 1751 | CTFontCopyLocalizedName(fFontRef, kCTFontFamilyNameKey, &cfLanguage)); |
| 1752 | |
| 1753 | SkString skLanguage; |
| 1754 | SkString skFamilyName; |
| 1755 | if (cfLanguage.get()) { |
| 1756 | CFStringToSkString(cfLanguage.get(), &skLanguage); |
| 1757 | } else { |
| 1758 | skLanguage = "und"; //undetermined |
| 1759 | } |
| 1760 | if (cfFamilyName.get()) { |
| 1761 | CFStringToSkString(cfFamilyName.get(), &skFamilyName); |
| 1762 | } |
| 1763 | |
| 1764 | nameIter = new SkOTUtils::LocalizedStrings_SingleName(skFamilyName, skLanguage); |
| 1765 | } |
| 1766 | return nameIter; |
| 1767 | } |
| 1768 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1769 | // If, as is the case with web fonts, the CTFont data isn't available, |
| 1770 | // the CGFont data may work. While the CGFont may always provide the |
| 1771 | // right result, leave the CTFont code path to minimize disruption. |
| 1772 | static CFDataRef copyTableFromFont(CTFontRef ctFont, SkFontTableTag tag) { |
| 1773 | CFDataRef data = CTFontCopyTable(ctFont, (CTFontTableTag) tag, |
| 1774 | kCTFontTableOptionNoOptions); |
| 1775 | if (NULL == data) { |
| 1776 | AutoCFRelease<CGFontRef> cgFont(CTFontCopyGraphicsFont(ctFont, NULL)); |
| 1777 | data = CGFontCopyTableForTag(cgFont, tag); |
| 1778 | } |
| 1779 | return data; |
| 1780 | } |
| 1781 | |
| 1782 | int SkTypeface_Mac::onGetTableTags(SkFontTableTag tags[]) const { |
| 1783 | AutoCFRelease<CFArrayRef> cfArray(CTFontCopyAvailableTables(fFontRef, |
| 1784 | kCTFontTableOptionNoOptions)); |
| 1785 | if (NULL == cfArray) { |
| 1786 | return 0; |
| 1787 | } |
| 1788 | int count = CFArrayGetCount(cfArray); |
| 1789 | if (tags) { |
| 1790 | for (int i = 0; i < count; ++i) { |
| 1791 | uintptr_t fontTag = reinterpret_cast<uintptr_t>(CFArrayGetValueAtIndex(cfArray, i)); |
| 1792 | tags[i] = static_cast<SkFontTableTag>(fontTag); |
| 1793 | } |
| 1794 | } |
| 1795 | return count; |
| 1796 | } |
| 1797 | |
| 1798 | size_t SkTypeface_Mac::onGetTableData(SkFontTableTag tag, size_t offset, |
| 1799 | size_t length, void* dstData) const { |
| 1800 | AutoCFRelease<CFDataRef> srcData(copyTableFromFont(fFontRef, tag)); |
| 1801 | if (NULL == srcData) { |
| 1802 | return 0; |
| 1803 | } |
| 1804 | |
| 1805 | size_t srcSize = CFDataGetLength(srcData); |
| 1806 | if (offset >= srcSize) { |
| 1807 | return 0; |
| 1808 | } |
| 1809 | if (length > srcSize - offset) { |
| 1810 | length = srcSize - offset; |
| 1811 | } |
| 1812 | if (dstData) { |
| 1813 | memcpy(dstData, CFDataGetBytePtr(srcData) + offset, length); |
| 1814 | } |
| 1815 | return length; |
| 1816 | } |
| 1817 | |
| 1818 | SkScalerContext* SkTypeface_Mac::onCreateScalerContext(const SkDescriptor* desc) const { |
reed@google.com | 0da4861 | 2013-03-19 16:06:52 +0000 | [diff] [blame] | 1819 | return new SkScalerContext_Mac(const_cast<SkTypeface_Mac*>(this), desc); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1820 | } |
| 1821 | |
| 1822 | void SkTypeface_Mac::onFilterRec(SkScalerContextRec* rec) const { |
commit-bot@chromium.org | c5fd461 | 2013-05-06 22:23:08 +0000 | [diff] [blame] | 1823 | if (rec->fFlags & SkScalerContext::kLCD_BGROrder_Flag || |
| 1824 | rec->fFlags & SkScalerContext::kLCD_Vertical_Flag) |
| 1825 | { |
| 1826 | rec->fMaskFormat = SkMask::kA8_Format; |
| 1827 | // Render the glyphs as close as possible to what was requested. |
| 1828 | // The above turns off subpixel rendering, but the user requested it. |
| 1829 | // Normal hinting will cause the A8 masks to be generated from CoreGraphics subpixel masks. |
| 1830 | // See comments below for more details. |
| 1831 | rec->setHinting(SkPaint::kNormal_Hinting); |
| 1832 | } |
skia.committer@gmail.com | e944de7 | 2013-05-07 07:01:03 +0000 | [diff] [blame] | 1833 | |
commit-bot@chromium.org | c5fd461 | 2013-05-06 22:23:08 +0000 | [diff] [blame] | 1834 | unsigned flagsWeDontSupport = SkScalerContext::kDevKernText_Flag | |
| 1835 | SkScalerContext::kAutohinting_Flag | |
| 1836 | SkScalerContext::kLCD_BGROrder_Flag | |
| 1837 | SkScalerContext::kLCD_Vertical_Flag; |
skia.committer@gmail.com | 0c23faf | 2013-03-03 07:16:29 +0000 | [diff] [blame] | 1838 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1839 | rec->fFlags &= ~flagsWeDontSupport; |
skia.committer@gmail.com | 0c23faf | 2013-03-03 07:16:29 +0000 | [diff] [blame] | 1840 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1841 | bool lcdSupport = supports_LCD(); |
skia.committer@gmail.com | 0c23faf | 2013-03-03 07:16:29 +0000 | [diff] [blame] | 1842 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1843 | // Only two levels of hinting are supported. |
| 1844 | // kNo_Hinting means avoid CoreGraphics outline dilation. |
| 1845 | // kNormal_Hinting means CoreGraphics outline dilation is allowed. |
| 1846 | // If there is no lcd support, hinting (dilation) cannot be supported. |
| 1847 | SkPaint::Hinting hinting = rec->getHinting(); |
| 1848 | if (SkPaint::kSlight_Hinting == hinting || !lcdSupport) { |
| 1849 | hinting = SkPaint::kNo_Hinting; |
| 1850 | } else if (SkPaint::kFull_Hinting == hinting) { |
| 1851 | hinting = SkPaint::kNormal_Hinting; |
| 1852 | } |
| 1853 | rec->setHinting(hinting); |
skia.committer@gmail.com | 0c23faf | 2013-03-03 07:16:29 +0000 | [diff] [blame] | 1854 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1855 | // FIXME: lcd smoothed un-hinted rasterization unsupported. |
| 1856 | // Tracked by http://code.google.com/p/skia/issues/detail?id=915 . |
| 1857 | // There is no current means to honor a request for unhinted lcd, |
| 1858 | // so arbitrarilly ignore the hinting request and honor lcd. |
skia.committer@gmail.com | 0c23faf | 2013-03-03 07:16:29 +0000 | [diff] [blame] | 1859 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1860 | // Hinting and smoothing should be orthogonal, but currently they are not. |
| 1861 | // CoreGraphics has no API to influence hinting. However, its lcd smoothed |
| 1862 | // output is drawn from auto-dilated outlines (the amount of which is |
| 1863 | // determined by AppleFontSmoothing). Its regular anti-aliased output is |
| 1864 | // drawn from un-dilated outlines. |
skia.committer@gmail.com | 0c23faf | 2013-03-03 07:16:29 +0000 | [diff] [blame] | 1865 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1866 | // The behavior of Skia is as follows: |
| 1867 | // [AA][no-hint]: generate AA using CoreGraphic's AA output. |
| 1868 | // [AA][yes-hint]: use CoreGraphic's LCD output and reduce it to a single |
| 1869 | // channel. This matches [LCD][yes-hint] in weight. |
| 1870 | // [LCD][no-hint]: curently unable to honor, and must pick which to respect. |
| 1871 | // Currenly side with LCD, effectively ignoring the hinting setting. |
| 1872 | // [LCD][yes-hint]: generate LCD using CoreGraphic's LCD output. |
skia.committer@gmail.com | 0c23faf | 2013-03-03 07:16:29 +0000 | [diff] [blame] | 1873 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1874 | if (isLCDFormat(rec->fMaskFormat)) { |
| 1875 | if (lcdSupport) { |
| 1876 | //CoreGraphics creates 555 masks for smoothed text anyway. |
| 1877 | rec->fMaskFormat = SkMask::kLCD16_Format; |
| 1878 | rec->setHinting(SkPaint::kNormal_Hinting); |
| 1879 | } else { |
| 1880 | rec->fMaskFormat = SkMask::kA8_Format; |
| 1881 | } |
| 1882 | } |
skia.committer@gmail.com | 0c23faf | 2013-03-03 07:16:29 +0000 | [diff] [blame] | 1883 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1884 | // Unhinted A8 masks (those not derived from LCD masks) must respect SK_GAMMA_APPLY_TO_A8. |
| 1885 | // All other masks can use regular gamma. |
| 1886 | if (SkMask::kA8_Format == rec->fMaskFormat && SkPaint::kNo_Hinting == hinting) { |
| 1887 | #ifndef SK_GAMMA_APPLY_TO_A8 |
| 1888 | rec->ignorePreBlend(); |
reed@android.com | feda2f9 | 2010-05-19 13:47:05 +0000 | [diff] [blame] | 1889 | #endif |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1890 | } else { |
| 1891 | //CoreGraphics dialates smoothed text as needed. |
| 1892 | rec->setContrast(0); |
| 1893 | } |
| 1894 | } |
| 1895 | |
| 1896 | // we take ownership of the ref |
| 1897 | static const char* get_str(CFStringRef ref, SkString* str) { |
| 1898 | CFStringToSkString(ref, str); |
| 1899 | CFSafeRelease(ref); |
| 1900 | return str->c_str(); |
| 1901 | } |
| 1902 | |
reed@google.com | 5526ede | 2013-03-25 13:03:37 +0000 | [diff] [blame] | 1903 | void SkTypeface_Mac::onGetFontDescriptor(SkFontDescriptor* desc, |
| 1904 | bool* isLocalStream) const { |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1905 | SkString tmpStr; |
skia.committer@gmail.com | 0c23faf | 2013-03-03 07:16:29 +0000 | [diff] [blame] | 1906 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1907 | desc->setFamilyName(get_str(CTFontCopyFamilyName(fFontRef), &tmpStr)); |
| 1908 | desc->setFullName(get_str(CTFontCopyFullName(fFontRef), &tmpStr)); |
| 1909 | desc->setPostscriptName(get_str(CTFontCopyPostScriptName(fFontRef), &tmpStr)); |
reed@google.com | 5526ede | 2013-03-25 13:03:37 +0000 | [diff] [blame] | 1910 | // TODO: need to add support for local-streams (here and openStream) |
| 1911 | *isLocalStream = false; |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1912 | } |
reed@google.com | 5526ede | 2013-03-25 13:03:37 +0000 | [diff] [blame] | 1913 | |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 1914 | int SkTypeface_Mac::onCharsToGlyphs(const void* chars, Encoding encoding, |
bungeman@google.com | 72b8cb2 | 2013-10-25 17:49:08 +0000 | [diff] [blame] | 1915 | uint16_t glyphs[], int glyphCount) const |
| 1916 | { |
| 1917 | // Undocumented behavior of CTFontGetGlyphsForCharacters with non-bmp code points: |
| 1918 | // When a surrogate pair is detected, the glyph index used is the index of the high surrogate. |
| 1919 | // It is documented that if a mapping is unavailable, the glyph will be set to 0. |
skia.committer@gmail.com | 0673efe | 2013-10-26 07:01:53 +0000 | [diff] [blame] | 1920 | |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 1921 | SkAutoSTMalloc<1024, UniChar> charStorage; |
bungeman@google.com | 72b8cb2 | 2013-10-25 17:49:08 +0000 | [diff] [blame] | 1922 | const UniChar* src; // UniChar is a UTF-16 16-bit code unit. |
| 1923 | int srcCount; |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 1924 | switch (encoding) { |
| 1925 | case kUTF8_Encoding: { |
bungeman@google.com | 72b8cb2 | 2013-10-25 17:49:08 +0000 | [diff] [blame] | 1926 | const char* utf8 = reinterpret_cast<const char*>(chars); |
| 1927 | UniChar* utf16 = charStorage.reset(2 * glyphCount); |
| 1928 | src = utf16; |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 1929 | for (int i = 0; i < glyphCount; ++i) { |
bungeman@google.com | 72b8cb2 | 2013-10-25 17:49:08 +0000 | [diff] [blame] | 1930 | SkUnichar uni = SkUTF8_NextUnichar(&utf8); |
| 1931 | utf16 += SkUTF16_FromUnichar(uni, utf16); |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 1932 | } |
bungeman@google.com | 72b8cb2 | 2013-10-25 17:49:08 +0000 | [diff] [blame] | 1933 | srcCount = utf16 - src; |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 1934 | break; |
| 1935 | } |
bungeman@google.com | 72b8cb2 | 2013-10-25 17:49:08 +0000 | [diff] [blame] | 1936 | case kUTF16_Encoding: { |
| 1937 | src = reinterpret_cast<const UniChar*>(chars); |
| 1938 | int extra = 0; |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 1939 | for (int i = 0; i < glyphCount; ++i) { |
bungeman@google.com | 72b8cb2 | 2013-10-25 17:49:08 +0000 | [diff] [blame] | 1940 | if (SkUTF16_IsHighSurrogate(src[i + extra])) { |
| 1941 | ++extra; |
| 1942 | } |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 1943 | } |
bungeman@google.com | 72b8cb2 | 2013-10-25 17:49:08 +0000 | [diff] [blame] | 1944 | srcCount = glyphCount + extra; |
| 1945 | break; |
| 1946 | } |
| 1947 | case kUTF32_Encoding: { |
| 1948 | const SkUnichar* utf32 = reinterpret_cast<const SkUnichar*>(chars); |
| 1949 | UniChar* utf16 = charStorage.reset(2 * glyphCount); |
| 1950 | src = utf16; |
| 1951 | for (int i = 0; i < glyphCount; ++i) { |
| 1952 | utf16 += SkUTF16_FromUnichar(utf32[i], utf16); |
| 1953 | } |
| 1954 | srcCount = utf16 - src; |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 1955 | break; |
| 1956 | } |
| 1957 | } |
| 1958 | |
bungeman@google.com | 72b8cb2 | 2013-10-25 17:49:08 +0000 | [diff] [blame] | 1959 | // If glyphs is NULL, CT still needs glyph storage for finding the first failure. |
| 1960 | // Also, if there are any non-bmp code points, the provided 'glyphs' storage will be inadequate. |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 1961 | SkAutoSTMalloc<1024, uint16_t> glyphStorage; |
| 1962 | uint16_t* macGlyphs = glyphs; |
bungeman@google.com | 72b8cb2 | 2013-10-25 17:49:08 +0000 | [diff] [blame] | 1963 | if (NULL == macGlyphs || srcCount > glyphCount) { |
| 1964 | macGlyphs = glyphStorage.reset(srcCount); |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 1965 | } |
| 1966 | |
bungeman@google.com | 72b8cb2 | 2013-10-25 17:49:08 +0000 | [diff] [blame] | 1967 | bool allEncoded = CTFontGetGlyphsForCharacters(fFontRef, src, macGlyphs, srcCount); |
| 1968 | |
| 1969 | // If there were any non-bmp, then copy and compact. |
| 1970 | // If 'glyphs' is NULL, then compact glyphStorage in-place. |
| 1971 | // If all are bmp and 'glyphs' is non-NULL, 'glyphs' already contains the compact glyphs. |
| 1972 | // If some are non-bmp and 'glyphs' is non-NULL, copy and compact into 'glyphs'. |
| 1973 | uint16_t* compactedGlyphs = glyphs; |
| 1974 | if (NULL == compactedGlyphs) { |
| 1975 | compactedGlyphs = macGlyphs; |
| 1976 | } |
| 1977 | if (srcCount > glyphCount) { |
| 1978 | int extra = 0; |
| 1979 | for (int i = 0; i < glyphCount; ++i) { |
| 1980 | if (SkUTF16_IsHighSurrogate(src[i + extra])) { |
| 1981 | ++extra; |
| 1982 | } |
| 1983 | compactedGlyphs[i] = macGlyphs[i + extra]; |
| 1984 | } |
| 1985 | } |
| 1986 | |
| 1987 | if (allEncoded) { |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 1988 | return glyphCount; |
| 1989 | } |
bungeman@google.com | 72b8cb2 | 2013-10-25 17:49:08 +0000 | [diff] [blame] | 1990 | |
| 1991 | // If we got false, then we need to manually look for first failure. |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 1992 | for (int i = 0; i < glyphCount; ++i) { |
bungeman@google.com | 72b8cb2 | 2013-10-25 17:49:08 +0000 | [diff] [blame] | 1993 | if (0 == compactedGlyphs[i]) { |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 1994 | return i; |
| 1995 | } |
| 1996 | } |
bungeman@google.com | 72b8cb2 | 2013-10-25 17:49:08 +0000 | [diff] [blame] | 1997 | // Odd to get here, as we expected CT to have returned true up front. |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 1998 | return glyphCount; |
| 1999 | } |
| 2000 | |
| 2001 | int SkTypeface_Mac::onCountGlyphs() const { |
| 2002 | return CTFontGetGlyphCount(fFontRef); |
| 2003 | } |
| 2004 | |
reed@google.com | 95625db | 2013-03-25 20:44:02 +0000 | [diff] [blame] | 2005 | /////////////////////////////////////////////////////////////////////////////// |
| 2006 | /////////////////////////////////////////////////////////////////////////////// |
reed@google.com | 95625db | 2013-03-25 20:44:02 +0000 | [diff] [blame] | 2007 | #if 1 |
reed@google.com | 83787c5 | 2013-03-26 17:19:15 +0000 | [diff] [blame] | 2008 | |
| 2009 | static bool find_desc_str(CTFontDescriptorRef desc, CFStringRef name, SkString* value) { |
| 2010 | AutoCFRelease<CFStringRef> ref((CFStringRef)CTFontDescriptorCopyAttribute(desc, name)); |
| 2011 | if (NULL == ref.get()) { |
| 2012 | return false; |
| 2013 | } |
| 2014 | CFStringToSkString(ref, value); |
| 2015 | return true; |
| 2016 | } |
| 2017 | |
| 2018 | static bool find_dict_float(CFDictionaryRef dict, CFStringRef name, float* value) { |
| 2019 | CFNumberRef num; |
| 2020 | return CFDictionaryGetValueIfPresent(dict, name, (const void**)&num) |
| 2021 | && CFNumberIsFloatType(num) |
| 2022 | && CFNumberGetValue(num, kCFNumberFloatType, value); |
| 2023 | } |
| 2024 | |
reed@google.com | 95625db | 2013-03-25 20:44:02 +0000 | [diff] [blame] | 2025 | #include "SkFontMgr.h" |
| 2026 | |
reed@google.com | 83787c5 | 2013-03-26 17:19:15 +0000 | [diff] [blame] | 2027 | static int unit_weight_to_fontstyle(float unit) { |
| 2028 | float value; |
| 2029 | if (unit < 0) { |
| 2030 | value = 100 + (1 + unit) * 300; |
| 2031 | } else { |
| 2032 | value = 400 + unit * 500; |
| 2033 | } |
| 2034 | return sk_float_round2int(value); |
| 2035 | } |
| 2036 | |
| 2037 | static int unit_width_to_fontstyle(float unit) { |
| 2038 | float value; |
| 2039 | if (unit < 0) { |
| 2040 | value = 1 + (1 + unit) * 4; |
| 2041 | } else { |
| 2042 | value = 5 + unit * 4; |
| 2043 | } |
| 2044 | return sk_float_round2int(value); |
| 2045 | } |
| 2046 | |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 2047 | static inline int sqr(int value) { |
| 2048 | SkASSERT(SkAbs32(value) < 0x7FFF); // check for overflow |
| 2049 | return value * value; |
| 2050 | } |
| 2051 | |
| 2052 | // We normalize each axis (weight, width, italic) to be base-900 |
| 2053 | static int compute_metric(const SkFontStyle& a, const SkFontStyle& b) { |
| 2054 | return sqr(a.weight() - b.weight()) + |
| 2055 | sqr((a.width() - b.width()) * 100) + |
| 2056 | sqr((a.isItalic() != b.isItalic()) * 900); |
| 2057 | } |
| 2058 | |
reed@google.com | 83787c5 | 2013-03-26 17:19:15 +0000 | [diff] [blame] | 2059 | static SkFontStyle desc2fontstyle(CTFontDescriptorRef desc) { |
| 2060 | AutoCFRelease<CFDictionaryRef> dict( |
| 2061 | (CFDictionaryRef)CTFontDescriptorCopyAttribute(desc, |
| 2062 | kCTFontTraitsAttribute)); |
| 2063 | if (NULL == dict.get()) { |
| 2064 | return SkFontStyle(); |
| 2065 | } |
| 2066 | |
| 2067 | float weight, width, slant; |
| 2068 | if (!find_dict_float(dict, kCTFontWeightTrait, &weight)) { |
| 2069 | weight = 0; |
| 2070 | } |
| 2071 | if (!find_dict_float(dict, kCTFontWidthTrait, &width)) { |
| 2072 | width = 0; |
| 2073 | } |
| 2074 | if (!find_dict_float(dict, kCTFontSlantTrait, &slant)) { |
| 2075 | slant = 0; |
| 2076 | } |
skia.committer@gmail.com | 37cbc7f | 2013-03-27 07:01:04 +0000 | [diff] [blame] | 2077 | |
reed@google.com | 83787c5 | 2013-03-26 17:19:15 +0000 | [diff] [blame] | 2078 | return SkFontStyle(unit_weight_to_fontstyle(weight), |
| 2079 | unit_width_to_fontstyle(width), |
| 2080 | slant ? SkFontStyle::kItalic_Slant |
| 2081 | : SkFontStyle::kUpright_Slant); |
| 2082 | } |
| 2083 | |
reed@google.com | dea7ee0 | 2013-03-28 14:12:10 +0000 | [diff] [blame] | 2084 | struct NameFontStyleRec { |
| 2085 | SkString fFamilyName; |
| 2086 | SkFontStyle fFontStyle; |
| 2087 | }; |
| 2088 | |
| 2089 | static bool nameFontStyleProc(SkTypeface* face, SkTypeface::Style, |
| 2090 | void* ctx) { |
| 2091 | SkTypeface_Mac* macFace = (SkTypeface_Mac*)face; |
| 2092 | const NameFontStyleRec* rec = (const NameFontStyleRec*)ctx; |
| 2093 | |
| 2094 | return macFace->fFontStyle == rec->fFontStyle && |
| 2095 | macFace->fName == rec->fFamilyName; |
| 2096 | } |
| 2097 | |
reed@google.com | ce8b3de | 2013-03-26 19:30:16 +0000 | [diff] [blame] | 2098 | static SkTypeface* createFromDesc(CFStringRef cfFamilyName, |
| 2099 | CTFontDescriptorRef desc) { |
reed@google.com | dea7ee0 | 2013-03-28 14:12:10 +0000 | [diff] [blame] | 2100 | NameFontStyleRec rec; |
| 2101 | CFStringToSkString(cfFamilyName, &rec.fFamilyName); |
| 2102 | rec.fFontStyle = desc2fontstyle(desc); |
| 2103 | |
| 2104 | SkTypeface* face = SkTypefaceCache::FindByProcAndRef(nameFontStyleProc, |
| 2105 | &rec); |
| 2106 | if (face) { |
| 2107 | return face; |
| 2108 | } |
| 2109 | |
reed@google.com | ce8b3de | 2013-03-26 19:30:16 +0000 | [diff] [blame] | 2110 | AutoCFRelease<CTFontRef> ctNamed(CTFontCreateWithName(cfFamilyName, 1, NULL)); |
| 2111 | CTFontRef ctFont = CTFontCreateCopyWithAttributes(ctNamed, 1, NULL, desc); |
| 2112 | if (NULL == ctFont) { |
| 2113 | return NULL; |
| 2114 | } |
skia.committer@gmail.com | 37cbc7f | 2013-03-27 07:01:04 +0000 | [diff] [blame] | 2115 | |
reed@google.com | ce8b3de | 2013-03-26 19:30:16 +0000 | [diff] [blame] | 2116 | SkString str; |
| 2117 | CFStringToSkString(cfFamilyName, &str); |
skia.committer@gmail.com | 37cbc7f | 2013-03-27 07:01:04 +0000 | [diff] [blame] | 2118 | |
reed@google.com | ce8b3de | 2013-03-26 19:30:16 +0000 | [diff] [blame] | 2119 | bool isFixedPitch; |
| 2120 | (void)computeStyleBits(ctFont, &isFixedPitch); |
| 2121 | SkFontID fontID = CTFontRef_to_SkFontID(ctFont); |
skia.committer@gmail.com | 37cbc7f | 2013-03-27 07:01:04 +0000 | [diff] [blame] | 2122 | |
reed@google.com | dea7ee0 | 2013-03-28 14:12:10 +0000 | [diff] [blame] | 2123 | face = SkNEW_ARGS(SkTypeface_Mac, (rec.fFontStyle, fontID, isFixedPitch, |
| 2124 | ctFont, str.c_str())); |
| 2125 | SkTypefaceCache::Add(face, face->style()); |
| 2126 | return face; |
reed@google.com | ce8b3de | 2013-03-26 19:30:16 +0000 | [diff] [blame] | 2127 | } |
| 2128 | |
reed@google.com | 83787c5 | 2013-03-26 17:19:15 +0000 | [diff] [blame] | 2129 | class SkFontStyleSet_Mac : public SkFontStyleSet { |
reed@google.com | 95625db | 2013-03-25 20:44:02 +0000 | [diff] [blame] | 2130 | public: |
reed@google.com | 83787c5 | 2013-03-26 17:19:15 +0000 | [diff] [blame] | 2131 | SkFontStyleSet_Mac(CFStringRef familyName, CTFontDescriptorRef desc) |
| 2132 | : fArray(CTFontDescriptorCreateMatchingFontDescriptors(desc, NULL)) |
reed@google.com | dea7ee0 | 2013-03-28 14:12:10 +0000 | [diff] [blame] | 2133 | , fFamilyName(familyName) |
| 2134 | , fCount(0) { |
reed@google.com | 83787c5 | 2013-03-26 17:19:15 +0000 | [diff] [blame] | 2135 | CFRetain(familyName); |
reed@google.com | dea7ee0 | 2013-03-28 14:12:10 +0000 | [diff] [blame] | 2136 | if (NULL == fArray) { |
| 2137 | fArray = CFArrayCreate(NULL, NULL, 0, NULL); |
| 2138 | } |
| 2139 | fCount = CFArrayGetCount(fArray); |
reed@google.com | 83787c5 | 2013-03-26 17:19:15 +0000 | [diff] [blame] | 2140 | } |
skia.committer@gmail.com | 37cbc7f | 2013-03-27 07:01:04 +0000 | [diff] [blame] | 2141 | |
reed@google.com | 83787c5 | 2013-03-26 17:19:15 +0000 | [diff] [blame] | 2142 | virtual ~SkFontStyleSet_Mac() { |
reed@google.com | dea7ee0 | 2013-03-28 14:12:10 +0000 | [diff] [blame] | 2143 | CFRelease(fArray); |
reed@google.com | 83787c5 | 2013-03-26 17:19:15 +0000 | [diff] [blame] | 2144 | CFRelease(fFamilyName); |
| 2145 | } |
| 2146 | |
| 2147 | virtual int count() SK_OVERRIDE { |
reed@google.com | dea7ee0 | 2013-03-28 14:12:10 +0000 | [diff] [blame] | 2148 | return fCount; |
reed@google.com | 83787c5 | 2013-03-26 17:19:15 +0000 | [diff] [blame] | 2149 | } |
| 2150 | |
| 2151 | virtual void getStyle(int index, SkFontStyle* style, |
| 2152 | SkString* name) SK_OVERRIDE { |
reed@google.com | dea7ee0 | 2013-03-28 14:12:10 +0000 | [diff] [blame] | 2153 | SkASSERT((unsigned)index < (unsigned)fCount); |
reed@google.com | 83787c5 | 2013-03-26 17:19:15 +0000 | [diff] [blame] | 2154 | CTFontDescriptorRef desc = (CTFontDescriptorRef)CFArrayGetValueAtIndex(fArray, index); |
| 2155 | if (style) { |
| 2156 | *style = desc2fontstyle(desc); |
| 2157 | } |
| 2158 | if (name) { |
| 2159 | if (!find_desc_str(desc, kCTFontStyleNameAttribute, name)) { |
| 2160 | name->reset(); |
| 2161 | } |
| 2162 | } |
| 2163 | } |
| 2164 | |
| 2165 | virtual SkTypeface* createTypeface(int index) SK_OVERRIDE { |
| 2166 | SkASSERT((unsigned)index < (unsigned)CFArrayGetCount(fArray)); |
| 2167 | CTFontDescriptorRef desc = (CTFontDescriptorRef)CFArrayGetValueAtIndex(fArray, index); |
skia.committer@gmail.com | 37cbc7f | 2013-03-27 07:01:04 +0000 | [diff] [blame] | 2168 | |
reed@google.com | 83787c5 | 2013-03-26 17:19:15 +0000 | [diff] [blame] | 2169 | return createFromDesc(fFamilyName, desc); |
| 2170 | } |
skia.committer@gmail.com | 37cbc7f | 2013-03-27 07:01:04 +0000 | [diff] [blame] | 2171 | |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 2172 | virtual SkTypeface* matchStyle(const SkFontStyle& pattern) SK_OVERRIDE { |
| 2173 | if (0 == fCount) { |
| 2174 | return NULL; |
| 2175 | } |
| 2176 | return createFromDesc(fFamilyName, findMatchingDesc(pattern)); |
| 2177 | } |
| 2178 | |
reed@google.com | 83787c5 | 2013-03-26 17:19:15 +0000 | [diff] [blame] | 2179 | private: |
| 2180 | CFArrayRef fArray; |
| 2181 | CFStringRef fFamilyName; |
reed@google.com | dea7ee0 | 2013-03-28 14:12:10 +0000 | [diff] [blame] | 2182 | int fCount; |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 2183 | |
| 2184 | CTFontDescriptorRef findMatchingDesc(const SkFontStyle& pattern) const { |
| 2185 | int bestMetric = SK_MaxS32; |
| 2186 | CTFontDescriptorRef bestDesc = NULL; |
skia.committer@gmail.com | d55846d | 2013-03-30 07:01:27 +0000 | [diff] [blame] | 2187 | |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 2188 | for (int i = 0; i < fCount; ++i) { |
| 2189 | CTFontDescriptorRef desc = (CTFontDescriptorRef)CFArrayGetValueAtIndex(fArray, i); |
| 2190 | int metric = compute_metric(pattern, desc2fontstyle(desc)); |
| 2191 | if (0 == metric) { |
| 2192 | return desc; |
| 2193 | } |
| 2194 | if (metric < bestMetric) { |
| 2195 | bestMetric = metric; |
| 2196 | bestDesc = desc; |
| 2197 | } |
| 2198 | } |
| 2199 | SkASSERT(bestDesc); |
| 2200 | return bestDesc; |
| 2201 | } |
reed@google.com | 83787c5 | 2013-03-26 17:19:15 +0000 | [diff] [blame] | 2202 | }; |
| 2203 | |
| 2204 | class SkFontMgr_Mac : public SkFontMgr { |
| 2205 | int fCount; |
| 2206 | CFArrayRef fNames; |
| 2207 | |
| 2208 | CFStringRef stringAt(int index) const { |
| 2209 | SkASSERT((unsigned)index < (unsigned)fCount); |
| 2210 | return (CFStringRef)CFArrayGetValueAtIndex(fNames, index); |
| 2211 | } |
| 2212 | |
| 2213 | void lazyInit() { |
| 2214 | if (NULL == fNames) { |
reed@google.com | 3dcbd46 | 2013-03-27 13:56:34 +0000 | [diff] [blame] | 2215 | fNames = SkCTFontManagerCopyAvailableFontFamilyNames(); |
reed@google.com | 83787c5 | 2013-03-26 17:19:15 +0000 | [diff] [blame] | 2216 | fCount = fNames ? CFArrayGetCount(fNames) : 0; |
| 2217 | } |
| 2218 | } |
| 2219 | |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 2220 | static SkFontStyleSet* CreateSet(CFStringRef cfFamilyName) { |
| 2221 | AutoCFRelease<CFMutableDictionaryRef> cfAttr( |
| 2222 | CFDictionaryCreateMutable(kCFAllocatorDefault, 0, |
| 2223 | &kCFTypeDictionaryKeyCallBacks, |
| 2224 | &kCFTypeDictionaryValueCallBacks)); |
skia.committer@gmail.com | d55846d | 2013-03-30 07:01:27 +0000 | [diff] [blame] | 2225 | |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 2226 | CFDictionaryAddValue(cfAttr, kCTFontFamilyNameAttribute, cfFamilyName); |
skia.committer@gmail.com | d55846d | 2013-03-30 07:01:27 +0000 | [diff] [blame] | 2227 | |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 2228 | AutoCFRelease<CTFontDescriptorRef> desc( |
| 2229 | CTFontDescriptorCreateWithAttributes(cfAttr)); |
| 2230 | return SkNEW_ARGS(SkFontStyleSet_Mac, (cfFamilyName, desc)); |
| 2231 | } |
skia.committer@gmail.com | d55846d | 2013-03-30 07:01:27 +0000 | [diff] [blame] | 2232 | |
reed@google.com | 83787c5 | 2013-03-26 17:19:15 +0000 | [diff] [blame] | 2233 | public: |
| 2234 | SkFontMgr_Mac() : fCount(0), fNames(NULL) {} |
| 2235 | |
| 2236 | virtual ~SkFontMgr_Mac() { |
| 2237 | CFSafeRelease(fNames); |
| 2238 | } |
reed@google.com | 95625db | 2013-03-25 20:44:02 +0000 | [diff] [blame] | 2239 | |
| 2240 | protected: |
| 2241 | virtual int onCountFamilies() SK_OVERRIDE { |
reed@google.com | 83787c5 | 2013-03-26 17:19:15 +0000 | [diff] [blame] | 2242 | this->lazyInit(); |
| 2243 | return fCount; |
reed@google.com | 95625db | 2013-03-25 20:44:02 +0000 | [diff] [blame] | 2244 | } |
| 2245 | |
| 2246 | virtual void onGetFamilyName(int index, SkString* familyName) SK_OVERRIDE { |
reed@google.com | 83787c5 | 2013-03-26 17:19:15 +0000 | [diff] [blame] | 2247 | this->lazyInit(); |
| 2248 | if ((unsigned)index < (unsigned)fCount) { |
| 2249 | CFStringToSkString(this->stringAt(index), familyName); |
| 2250 | } else { |
| 2251 | familyName->reset(); |
| 2252 | } |
reed@google.com | 95625db | 2013-03-25 20:44:02 +0000 | [diff] [blame] | 2253 | } |
| 2254 | |
| 2255 | virtual SkFontStyleSet* onCreateStyleSet(int index) SK_OVERRIDE { |
reed@google.com | 83787c5 | 2013-03-26 17:19:15 +0000 | [diff] [blame] | 2256 | this->lazyInit(); |
| 2257 | if ((unsigned)index >= (unsigned)fCount) { |
| 2258 | return NULL; |
| 2259 | } |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 2260 | return CreateSet(this->stringAt(index)); |
reed@google.com | 95625db | 2013-03-25 20:44:02 +0000 | [diff] [blame] | 2261 | } |
skia.committer@gmail.com | d55846d | 2013-03-30 07:01:27 +0000 | [diff] [blame] | 2262 | |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 2263 | virtual SkFontStyleSet* onMatchFamily(const char familyName[]) SK_OVERRIDE { |
| 2264 | AutoCFRelease<CFStringRef> cfName(make_CFString(familyName)); |
| 2265 | return CreateSet(cfName); |
| 2266 | } |
skia.committer@gmail.com | d55846d | 2013-03-30 07:01:27 +0000 | [diff] [blame] | 2267 | |
reed@google.com | 95625db | 2013-03-25 20:44:02 +0000 | [diff] [blame] | 2268 | virtual SkTypeface* onMatchFamilyStyle(const char familyName[], |
| 2269 | const SkFontStyle&) SK_OVERRIDE { |
| 2270 | return NULL; |
| 2271 | } |
skia.committer@gmail.com | e60ed08 | 2013-03-26 07:01:04 +0000 | [diff] [blame] | 2272 | |
reed@google.com | 95625db | 2013-03-25 20:44:02 +0000 | [diff] [blame] | 2273 | virtual SkTypeface* onMatchFaceStyle(const SkTypeface* familyMember, |
| 2274 | const SkFontStyle&) SK_OVERRIDE { |
| 2275 | return NULL; |
| 2276 | } |
skia.committer@gmail.com | e60ed08 | 2013-03-26 07:01:04 +0000 | [diff] [blame] | 2277 | |
reed@google.com | 95625db | 2013-03-25 20:44:02 +0000 | [diff] [blame] | 2278 | virtual SkTypeface* onCreateFromData(SkData* data, |
| 2279 | int ttcIndex) SK_OVERRIDE { |
| 2280 | AutoCFRelease<CGDataProviderRef> pr(SkCreateDataProviderFromData(data)); |
| 2281 | if (NULL == pr) { |
| 2282 | return NULL; |
| 2283 | } |
| 2284 | return create_from_dataProvider(pr); |
| 2285 | } |
| 2286 | |
| 2287 | virtual SkTypeface* onCreateFromStream(SkStream* stream, |
| 2288 | int ttcIndex) SK_OVERRIDE { |
| 2289 | AutoCFRelease<CGDataProviderRef> pr(SkCreateDataProviderFromStream(stream)); |
| 2290 | if (NULL == pr) { |
| 2291 | return NULL; |
| 2292 | } |
| 2293 | return create_from_dataProvider(pr); |
| 2294 | } |
| 2295 | |
| 2296 | virtual SkTypeface* onCreateFromFile(const char path[], |
| 2297 | int ttcIndex) SK_OVERRIDE { |
| 2298 | AutoCFRelease<CGDataProviderRef> pr(CGDataProviderCreateWithFilename(path)); |
| 2299 | if (NULL == pr) { |
| 2300 | return NULL; |
| 2301 | } |
| 2302 | return create_from_dataProvider(pr); |
| 2303 | } |
skia.committer@gmail.com | 76015b0 | 2013-07-31 07:01:00 +0000 | [diff] [blame] | 2304 | |
reed@google.com | 7fdcd44 | 2013-07-30 21:25:49 +0000 | [diff] [blame] | 2305 | virtual SkTypeface* onLegacyCreateTypeface(const char familyName[], |
| 2306 | unsigned styleBits) SK_OVERRIDE { |
| 2307 | return create_typeface(NULL, familyName, (SkTypeface::Style)styleBits); |
| 2308 | } |
reed@google.com | 95625db | 2013-03-25 20:44:02 +0000 | [diff] [blame] | 2309 | }; |
| 2310 | |
reed@google.com | 7fdcd44 | 2013-07-30 21:25:49 +0000 | [diff] [blame] | 2311 | /////////////////////////////////////////////////////////////////////////////// |
| 2312 | |
reed@google.com | 95625db | 2013-03-25 20:44:02 +0000 | [diff] [blame] | 2313 | SkFontMgr* SkFontMgr::Factory() { |
| 2314 | return SkNEW(SkFontMgr_Mac); |
| 2315 | } |
| 2316 | #endif |