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