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