reed@android.com | 0680d6c | 2008-12-19 19:46:15 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
mtklein | de9e7a7 | 2015-03-11 12:01:25 -0700 | [diff] [blame] | 8 | #include "SkTypes.h" // Keep this before any #ifdef ... |
mtklein | 1ee7651 | 2015-11-02 10:20:27 -0800 | [diff] [blame] | 9 | #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) |
mtklein | de9e7a7 | 2015-03-11 12:01:25 -0700 | [diff] [blame] | 10 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 11 | #ifdef SK_BUILD_FOR_MAC |
| 12 | #import <ApplicationServices/ApplicationServices.h> |
| 13 | #endif |
reed@android.com | 0680d6c | 2008-12-19 19:46:15 +0000 | [diff] [blame] | 14 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 15 | #ifdef SK_BUILD_FOR_IOS |
| 16 | #include <CoreText/CoreText.h> |
reed@google.com | 3dcbd46 | 2013-03-27 13:56:34 +0000 | [diff] [blame] | 17 | #include <CoreText/CTFontManager.h> |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 18 | #include <CoreGraphics/CoreGraphics.h> |
| 19 | #include <CoreFoundation/CoreFoundation.h> |
| 20 | #endif |
| 21 | |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 22 | #include "mac/SkUniqueCFRef.h" |
reed | 39a9a50 | 2015-05-12 09:50:04 -0700 | [diff] [blame] | 23 | #include "SkAdvancedTypefaceMetrics.h" |
Hal Canary | 95e3c05 | 2017-01-11 12:44:43 -0500 | [diff] [blame] | 24 | #include "SkAutoMalloc.h" |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 25 | #include "SkCGUtils.h" |
Cary Clark | a4083c9 | 2017-09-15 11:59:23 -0400 | [diff] [blame] | 26 | #include "SkColorData.h" |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 27 | #include "SkDescriptor.h" |
| 28 | #include "SkEndian.h" |
jvanverth | 02802f6 | 2015-07-02 06:42:49 -0700 | [diff] [blame] | 29 | #include "SkFloatingPoint.h" |
mtklein | 1b24933 | 2015-07-07 12:21:21 -0700 | [diff] [blame] | 30 | #include "SkFontDescriptor.h" |
Mike Reed | 77f94ea | 2019-01-22 16:30:40 -0500 | [diff] [blame] | 31 | #include "SkFontMetrics.h" |
mtklein | 1b24933 | 2015-07-07 12:21:21 -0700 | [diff] [blame] | 32 | #include "SkFontMgr.h" |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 33 | #include "SkGlyph.h" |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 34 | #include "SkMakeUnique.h" |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 35 | #include "SkMaskGamma.h" |
halcanary | 4dbbd04 | 2016-06-07 17:21:10 -0700 | [diff] [blame] | 36 | #include "SkMathPriv.h" |
mtklein | 1b24933 | 2015-07-07 12:21:21 -0700 | [diff] [blame] | 37 | #include "SkMutex.h" |
bungeman | 4ec46aa | 2017-02-15 17:49:12 -0500 | [diff] [blame] | 38 | #include "SkOTTable_OS_2.h" |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 39 | #include "SkOTUtils.h" |
mtklein | ffa4a92 | 2016-05-05 16:05:56 -0700 | [diff] [blame] | 40 | #include "SkOnce.h" |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 41 | #include "SkPaint.h" |
| 42 | #include "SkPath.h" |
mtklein | 1b24933 | 2015-07-07 12:21:21 -0700 | [diff] [blame] | 43 | #include "SkSFNTHeader.h" |
jvanverth | 02802f6 | 2015-07-02 06:42:49 -0700 | [diff] [blame] | 44 | #include "SkStream.h" |
mtklein | 1b24933 | 2015-07-07 12:21:21 -0700 | [diff] [blame] | 45 | #include "SkString.h" |
bungeman | e280d06 | 2016-03-24 11:27:05 -0700 | [diff] [blame] | 46 | #include "SkTemplates.h" |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 47 | #include "SkTo.h" |
mtklein | 1b24933 | 2015-07-07 12:21:21 -0700 | [diff] [blame] | 48 | #include "SkTypefaceCache.h" |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 49 | #include "SkTypeface_mac.h" |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 50 | #include "SkUTF.h" |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 51 | #include "SkUtils.h" |
reed@google.com | f77b35d | 2013-05-02 20:39:44 +0000 | [diff] [blame] | 52 | |
bungeman | 3e306f6 | 2017-03-29 11:32:02 -0400 | [diff] [blame] | 53 | #include <dlfcn.h> |
| 54 | |
Ben Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame] | 55 | #include <utility> |
| 56 | |
reed | d0f4173 | 2015-07-10 12:08:38 -0700 | [diff] [blame] | 57 | // Experimental code to use a global lock whenever we access CG, to see if this reduces |
| 58 | // crashes in Chrome |
| 59 | #define USE_GLOBAL_MUTEX_FOR_CG_ACCESS |
| 60 | |
| 61 | #ifdef USE_GLOBAL_MUTEX_FOR_CG_ACCESS |
reed | 086eea9 | 2016-05-04 17:12:46 -0700 | [diff] [blame] | 62 | SK_DECLARE_STATIC_MUTEX(gCGMutex); |
reed | d0f4173 | 2015-07-10 12:08:38 -0700 | [diff] [blame] | 63 | #define AUTO_CG_LOCK() SkAutoMutexAcquire amc(gCGMutex) |
| 64 | #else |
| 65 | #define AUTO_CG_LOCK() |
| 66 | #endif |
| 67 | |
bungeman | 3b4b66c | 2015-01-08 08:33:44 -0800 | [diff] [blame] | 68 | // Set to make glyph bounding boxes visible. |
| 69 | #define SK_SHOW_TEXT_BLIT_COVERAGE 0 |
| 70 | |
Mike Reed | 342a9fa | 2017-05-03 15:48:22 -0400 | [diff] [blame] | 71 | CTFontRef SkTypeface_GetCTFontRef(const SkTypeface* face) { |
| 72 | return face ? (CTFontRef)face->internal_private_getCTFontRef() : nullptr; |
| 73 | } |
| 74 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 75 | class SkScalerContext_Mac; |
| 76 | |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 77 | static SkUniqueCFRef<CFStringRef> make_CFString(const char s[]) { |
| 78 | return SkUniqueCFRef<CFStringRef>(CFStringCreateWithCString(nullptr, s, kCFStringEncodingUTF8)); |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 79 | } |
| 80 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 81 | // inline versions of these rect helpers |
| 82 | |
| 83 | static bool CGRectIsEmpty_inline(const CGRect& rect) { |
| 84 | return rect.size.width <= 0 || rect.size.height <= 0; |
| 85 | } |
| 86 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 87 | static CGFloat CGRectGetMinX_inline(const CGRect& rect) { |
| 88 | return rect.origin.x; |
| 89 | } |
| 90 | |
| 91 | static CGFloat CGRectGetMaxX_inline(const CGRect& rect) { |
| 92 | return rect.origin.x + rect.size.width; |
| 93 | } |
| 94 | |
| 95 | static CGFloat CGRectGetMinY_inline(const CGRect& rect) { |
| 96 | return rect.origin.y; |
| 97 | } |
| 98 | |
| 99 | static CGFloat CGRectGetMaxY_inline(const CGRect& rect) { |
| 100 | return rect.origin.y + rect.size.height; |
| 101 | } |
| 102 | |
| 103 | static CGFloat CGRectGetWidth_inline(const CGRect& rect) { |
| 104 | return rect.size.width; |
| 105 | } |
| 106 | |
| 107 | /////////////////////////////////////////////////////////////////////////////// |
| 108 | |
| 109 | static void sk_memset_rect32(uint32_t* ptr, uint32_t value, |
reed@google.com | 7fa2a65 | 2014-01-27 13:42:58 +0000 | [diff] [blame] | 110 | int width, int height, size_t rowBytes) { |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 111 | SkASSERT(width); |
| 112 | SkASSERT(width * sizeof(uint32_t) <= rowBytes); |
| 113 | |
| 114 | if (width >= 32) { |
| 115 | while (height) { |
| 116 | sk_memset32(ptr, value, width); |
| 117 | ptr = (uint32_t*)((char*)ptr + rowBytes); |
| 118 | height -= 1; |
| 119 | } |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | rowBytes -= width * sizeof(uint32_t); |
| 124 | |
| 125 | if (width >= 8) { |
| 126 | while (height) { |
| 127 | int w = width; |
| 128 | do { |
| 129 | *ptr++ = value; *ptr++ = value; |
| 130 | *ptr++ = value; *ptr++ = value; |
| 131 | *ptr++ = value; *ptr++ = value; |
| 132 | *ptr++ = value; *ptr++ = value; |
| 133 | w -= 8; |
| 134 | } while (w >= 8); |
| 135 | while (--w >= 0) { |
| 136 | *ptr++ = value; |
| 137 | } |
| 138 | ptr = (uint32_t*)((char*)ptr + rowBytes); |
| 139 | height -= 1; |
| 140 | } |
| 141 | } else { |
| 142 | while (height) { |
| 143 | int w = width; |
| 144 | do { |
| 145 | *ptr++ = value; |
| 146 | } while (--w > 0); |
| 147 | ptr = (uint32_t*)((char*)ptr + rowBytes); |
| 148 | height -= 1; |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 153 | typedef uint32_t CGRGBPixel; |
| 154 | |
| 155 | static unsigned CGRGBPixel_getAlpha(CGRGBPixel pixel) { |
| 156 | return pixel & 0xFF; |
| 157 | } |
| 158 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 159 | static CGFloat ScalarToCG(SkScalar scalar) { |
| 160 | if (sizeof(CGFloat) == sizeof(float)) { |
| 161 | return SkScalarToFloat(scalar); |
| 162 | } else { |
| 163 | SkASSERT(sizeof(CGFloat) == sizeof(double)); |
| 164 | return (CGFloat) SkScalarToDouble(scalar); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | static SkScalar CGToScalar(CGFloat cgFloat) { |
| 169 | if (sizeof(CGFloat) == sizeof(float)) { |
benjaminwagner | 6b3eacb | 2016-03-24 19:07:58 -0700 | [diff] [blame] | 170 | return SkFloatToScalar(cgFloat); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 171 | } else { |
| 172 | SkASSERT(sizeof(CGFloat) == sizeof(double)); |
| 173 | return SkDoubleToScalar(cgFloat); |
| 174 | } |
| 175 | } |
| 176 | |
benjaminwagner | 6b3eacb | 2016-03-24 19:07:58 -0700 | [diff] [blame] | 177 | static float CGToFloat(CGFloat cgFloat) { |
| 178 | if (sizeof(CGFloat) == sizeof(float)) { |
| 179 | return cgFloat; |
| 180 | } else { |
| 181 | SkASSERT(sizeof(CGFloat) == sizeof(double)); |
| 182 | return static_cast<float>(cgFloat); |
| 183 | } |
| 184 | } |
| 185 | |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 186 | static CGAffineTransform MatrixToCGAffineTransform(const SkMatrix& matrix) { |
| 187 | return CGAffineTransformMake( ScalarToCG(matrix[SkMatrix::kMScaleX]), |
| 188 | -ScalarToCG(matrix[SkMatrix::kMSkewY] ), |
| 189 | -ScalarToCG(matrix[SkMatrix::kMSkewX] ), |
| 190 | ScalarToCG(matrix[SkMatrix::kMScaleY]), |
| 191 | ScalarToCG(matrix[SkMatrix::kMTransX]), |
| 192 | ScalarToCG(matrix[SkMatrix::kMTransY])); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 193 | } |
| 194 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 195 | /////////////////////////////////////////////////////////////////////////////// |
| 196 | |
| 197 | #define BITMAP_INFO_RGB (kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Host) |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 198 | |
Ben Wagner | 428ab73 | 2018-04-02 15:44:54 -0400 | [diff] [blame] | 199 | /** Drawn in FontForge, reduced with fonttools ttx, converted by xxd -i, |
| 200 | * this TrueType font contains a glyph of the spider. |
| 201 | * |
| 202 | * To re-forge the original bytes of the TrueType font file, |
| 203 | * remove all ',|( +0x)' from this definition, |
| 204 | * copy the data to the clipboard, |
| 205 | * run 'pbpaste | xxd -p -r - spider.ttf'. |
| 206 | */ |
| 207 | static constexpr const uint8_t kSpiderSymbol_ttf[] = { |
| 208 | 0x00, 0x01, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x80, 0x00, 0x03, 0x00, 0x40, |
| 209 | 0x47, 0x44, 0x45, 0x46, 0x00, 0x14, 0x00, 0x14, 0x00, 0x00, 0x07, 0xa8, |
| 210 | 0x00, 0x00, 0x00, 0x18, 0x4f, 0x53, 0x2f, 0x32, 0x8a, 0xf4, 0xfb, 0xdb, |
| 211 | 0x00, 0x00, 0x01, 0x48, 0x00, 0x00, 0x00, 0x60, 0x63, 0x6d, 0x61, 0x70, |
| 212 | 0xe0, 0x7f, 0x10, 0x7e, 0x00, 0x00, 0x01, 0xb8, 0x00, 0x00, 0x00, 0x54, |
| 213 | 0x67, 0x61, 0x73, 0x70, 0xff, 0xff, 0x00, 0x03, 0x00, 0x00, 0x07, 0xa0, |
| 214 | 0x00, 0x00, 0x00, 0x08, 0x67, 0x6c, 0x79, 0x66, 0x97, 0x0b, 0x6a, 0xf6, |
| 215 | 0x00, 0x00, 0x02, 0x18, 0x00, 0x00, 0x03, 0x40, 0x68, 0x65, 0x61, 0x64, |
| 216 | 0x0f, 0xa2, 0x24, 0x1a, 0x00, 0x00, 0x00, 0xcc, 0x00, 0x00, 0x00, 0x36, |
| 217 | 0x68, 0x68, 0x65, 0x61, 0x0e, 0xd3, 0x07, 0x3f, 0x00, 0x00, 0x01, 0x04, |
| 218 | 0x00, 0x00, 0x00, 0x24, 0x68, 0x6d, 0x74, 0x78, 0x10, 0x03, 0x00, 0x44, |
| 219 | 0x00, 0x00, 0x01, 0xa8, 0x00, 0x00, 0x00, 0x0e, 0x6c, 0x6f, 0x63, 0x61, |
| 220 | 0x01, 0xb4, 0x00, 0x28, 0x00, 0x00, 0x02, 0x0c, 0x00, 0x00, 0x00, 0x0a, |
| 221 | 0x6d, 0x61, 0x78, 0x70, 0x00, 0x4a, 0x01, 0x4d, 0x00, 0x00, 0x01, 0x28, |
| 222 | 0x00, 0x00, 0x00, 0x20, 0x6e, 0x61, 0x6d, 0x65, 0xc3, 0xe5, 0x39, 0xd4, |
| 223 | 0x00, 0x00, 0x05, 0x58, 0x00, 0x00, 0x02, 0x28, 0x70, 0x6f, 0x73, 0x74, |
| 224 | 0xff, 0x03, 0x00, 0x67, 0x00, 0x00, 0x07, 0x80, 0x00, 0x00, 0x00, 0x20, |
| 225 | 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x0b, 0x0f, 0x08, 0x1d, |
| 226 | 0x5f, 0x0f, 0x3c, 0xf5, 0x00, 0x0b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 227 | 0xd1, 0x97, 0xa8, 0x5a, 0x00, 0x00, 0x00, 0x00, 0xd6, 0xe8, 0x32, 0x33, |
| 228 | 0x00, 0x03, 0xff, 0x3b, 0x08, 0x00, 0x05, 0x55, 0x00, 0x00, 0x00, 0x08, |
| 229 | 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, |
| 230 | 0x05, 0x55, 0xff, 0x3b, 0x01, 0x79, 0x08, 0x00, 0x00, 0x03, 0x00, 0x00, |
| 231 | 0x08, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 232 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x01, 0x00, 0x00, |
| 233 | 0x00, 0x04, 0x01, 0x1c, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, |
| 234 | 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x40, 0x00, 0x2e, |
| 235 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x08, 0x00, 0x01, 0x90, 0x00, 0x05, |
| 236 | 0x00, 0x00, 0x05, 0x33, 0x05, 0x99, 0x00, 0x00, 0x01, 0x1e, 0x05, 0x33, |
| 237 | 0x05, 0x99, 0x00, 0x00, 0x03, 0xd7, 0x00, 0x66, 0x02, 0x12, 0x00, 0x00, |
| 238 | 0x05, 0x00, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 239 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 240 | 0x00, 0x00, 0x73, 0x6b, 0x69, 0x61, 0x00, 0xc0, 0x00, 0x00, 0xf0, 0x21, |
| 241 | 0x06, 0x66, 0xfe, 0x66, 0x01, 0x79, 0x05, 0x55, 0x00, 0xc5, 0x80, 0x00, |
| 242 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 243 | 0x00, 0x20, 0x00, 0x01, 0x08, 0x00, 0x00, 0x44, 0x00, 0x00, 0x00, 0x00, |
| 244 | 0x08, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, |
| 245 | 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x04, 0x00, 0x48, |
| 246 | 0x00, 0x00, 0x00, 0x0e, 0x00, 0x08, 0x00, 0x02, 0x00, 0x06, 0x00, 0x00, |
| 247 | 0x00, 0x09, 0x00, 0x0d, 0x00, 0x1d, 0x00, 0x21, 0xf0, 0x21, 0xff, 0xff, |
| 248 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x0d, 0x00, 0x1d, 0x00, 0x21, |
| 249 | 0xf0, 0x21, 0xff, 0xff, 0x00, 0x01, 0xff, 0xf9, 0xff, 0xf5, 0xff, 0xe4, |
| 250 | 0xff, 0xe2, 0x0f, 0xe2, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 251 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14, |
| 252 | 0x00, 0x14, 0x00, 0x14, 0x01, 0xa0, 0x00, 0x00, 0x00, 0x02, 0x00, 0x44, |
| 253 | 0x00, 0x00, 0x02, 0x64, 0x05, 0x55, 0x00, 0x03, 0x00, 0x07, 0x00, 0x00, |
| 254 | 0x33, 0x11, 0x21, 0x11, 0x25, 0x21, 0x11, 0x21, 0x44, 0x02, 0x20, 0xfe, |
| 255 | 0x24, 0x01, 0x98, 0xfe, 0x68, 0x05, 0x55, 0xfa, 0xab, 0x44, 0x04, 0xcd, |
| 256 | 0x00, 0x04, 0x00, 0x03, 0xff, 0x3b, 0x08, 0x00, 0x05, 0x4c, 0x00, 0x15, |
| 257 | 0x00, 0x1d, 0x00, 0x25, 0x01, 0x1b, 0x00, 0x00, 0x01, 0x36, 0x37, 0x36, |
| 258 | 0x27, 0x26, 0x07, 0x06, 0x06, 0x23, 0x22, 0x27, 0x26, 0x27, 0x26, 0x07, |
| 259 | 0x06, 0x17, 0x16, 0x17, 0x16, 0x32, 0x37, 0x32, 0x35, 0x34, 0x23, 0x22, |
| 260 | 0x15, 0x14, 0x27, 0x32, 0x35, 0x34, 0x23, 0x22, 0x15, 0x14, 0x03, 0x32, |
| 261 | 0x17, 0x30, 0x17, 0x31, 0x36, 0x37, 0x36, 0x37, 0x36, 0x37, 0x36, 0x33, |
| 262 | 0x32, 0x33, 0x16, 0x33, 0x32, 0x17, 0x16, 0x07, 0x06, 0x23, 0x22, 0x27, |
| 263 | 0x26, 0x27, 0x26, 0x23, 0x22, 0x07, 0x07, 0x06, 0x07, 0x06, 0x07, 0x06, |
| 264 | 0x1f, 0x02, 0x37, 0x36, 0x37, 0x36, 0x33, 0x32, 0x17, 0x17, 0x16, 0x33, |
| 265 | 0x16, 0x17, 0x16, 0x07, 0x06, 0x23, 0x22, 0x27, 0x27, 0x26, 0x23, 0x22, |
| 266 | 0x07, 0x06, 0x07, 0x06, 0x17, 0x16, 0x17, 0x16, 0x33, 0x32, 0x33, 0x32, |
| 267 | 0x37, 0x36, 0x37, 0x36, 0x17, 0x16, 0x1f, 0x02, 0x16, 0x17, 0x16, 0x15, |
| 268 | 0x14, 0x23, 0x22, 0x27, 0x27, 0x26, 0x27, 0x27, 0x26, 0x27, 0x26, 0x07, |
| 269 | 0x06, 0x07, 0x06, 0x17, 0x16, 0x17, 0x16, 0x15, 0x14, 0x07, 0x06, 0x07, |
| 270 | 0x06, 0x23, 0x22, 0x27, 0x26, 0x07, 0x06, 0x07, 0x06, 0x15, 0x14, 0x17, |
| 271 | 0x16, 0x17, 0x16, 0x15, 0x14, 0x07, 0x06, 0x23, 0x22, 0x27, 0x26, 0x27, |
| 272 | 0x26, 0x35, 0x34, 0x37, 0x36, 0x37, 0x36, 0x37, 0x34, 0x27, 0x26, 0x07, |
| 273 | 0x06, 0x07, 0x06, 0x0f, 0x02, 0x06, 0x23, 0x22, 0x27, 0x26, 0x35, 0x34, |
| 274 | 0x37, 0x37, 0x36, 0x37, 0x36, 0x37, 0x36, 0x37, 0x36, 0x27, 0x26, 0x27, |
| 275 | 0x26, 0x07, 0x06, 0x07, 0x06, 0x07, 0x06, 0x07, 0x07, 0x06, 0x23, 0x22, |
| 276 | 0x27, 0x26, 0x35, 0x34, 0x37, 0x36, 0x37, 0x37, 0x36, 0x37, 0x37, 0x36, |
| 277 | 0x37, 0x36, 0x37, 0x36, 0x35, 0x34, 0x27, 0x26, 0x27, 0x26, 0x27, 0x26, |
| 278 | 0x23, 0x22, 0x07, 0x06, 0x07, 0x06, 0x07, 0x06, 0x27, 0x26, 0x27, 0x26, |
| 279 | 0x27, 0x26, 0x35, 0x34, 0x37, 0x36, 0x37, 0x36, 0x37, 0x36, 0x33, 0x32, |
| 280 | 0x17, 0x16, 0x33, 0x32, 0x37, 0x36, 0x35, 0x34, 0x37, 0x36, 0x37, 0x36, |
| 281 | 0x33, 0x04, 0xf5, 0x23, 0x13, 0x11, 0x14, 0x16, 0x1d, 0x1b, 0x4c, 0x1f, |
| 282 | 0x0e, 0x2d, 0x23, 0x14, 0x2c, 0x13, 0x18, 0x25, 0x2c, 0x10, 0x3c, 0x71, |
| 283 | 0x1d, 0x5c, 0x5c, 0x3f, 0xae, 0x5c, 0x5c, 0x3f, 0x6a, 0x27, 0x31, 0x5b, |
| 284 | 0x09, 0x27, 0x36, 0x03, 0x0a, 0x26, 0x35, 0x2e, 0x09, 0x08, 0xc6, 0x13, |
| 285 | 0x81, 0x17, 0x20, 0x18, 0x21, 0x1e, 0x04, 0x04, 0x15, 0x5c, 0x22, 0x26, |
| 286 | 0x48, 0x56, 0x3b, 0x10, 0x21, 0x01, 0x0c, 0x06, 0x06, 0x0f, 0x31, 0x44, |
| 287 | 0x3c, 0x52, 0x4a, 0x1d, 0x11, 0x3f, 0xb4, 0x71, 0x01, 0x26, 0x06, 0x0d, |
| 288 | 0x15, 0x1a, 0x2a, 0x13, 0x53, 0xaa, 0x42, 0x1d, 0x0a, 0x33, 0x20, 0x21, |
| 289 | 0x2b, 0x01, 0x02, 0x3e, 0x21, 0x09, 0x02, 0x02, 0x0f, 0x2d, 0x4b, 0x0a, |
| 290 | 0x22, 0x15, 0x20, 0x1f, 0x72, 0x8b, 0x2d, 0x2f, 0x1d, 0x1f, 0x0e, 0x25, |
| 291 | 0x3f, 0x4d, 0x1b, 0x63, 0x2a, 0x2c, 0x14, 0x22, 0x18, 0x1c, 0x0f, 0x08, |
| 292 | 0x2a, 0x08, 0x08, 0x0d, 0x3b, 0x4c, 0x52, 0x74, 0x27, 0x71, 0x2e, 0x01, |
| 293 | 0x0c, 0x10, 0x15, 0x0d, 0x06, 0x0d, 0x05, 0x01, 0x06, 0x2c, 0x28, 0x14, |
| 294 | 0x1b, 0x05, 0x04, 0x10, 0x06, 0x12, 0x08, 0x0a, 0x16, 0x27, 0x03, 0x0d, |
| 295 | 0x30, 0x4c, 0x4c, 0x4b, 0x1f, 0x0b, 0x22, 0x26, 0x0d, 0x15, 0x0d, 0x2d, |
| 296 | 0x68, 0x34, 0x14, 0x3c, 0x25, 0x12, 0x04, 0x10, 0x18, 0x0b, 0x09, 0x30, |
| 297 | 0x2b, 0x44, 0x66, 0x14, 0x47, 0x47, 0x59, 0x73, 0x25, 0x05, 0x03, 0x1f, |
| 298 | 0x01, 0x08, 0x3f, 0x48, 0x4b, 0x4b, 0x76, 0x2f, 0x49, 0x2d, 0x22, 0x24, |
| 299 | 0x0c, 0x15, 0x08, 0x0e, 0x33, 0x03, 0x44, 0x4c, 0x10, 0x46, 0x13, 0x1f, |
| 300 | 0x27, 0x1b, 0x1d, 0x13, 0x02, 0x24, 0x08, 0x02, 0x42, 0x0e, 0x4d, 0x3c, |
| 301 | 0x19, 0x1b, 0x40, 0x2b, 0x2b, 0x1e, 0x16, 0x11, 0x04, 0x1f, 0x11, 0x04, |
| 302 | 0x18, 0x11, 0x35, 0x01, 0xa3, 0x13, 0x24, 0x1f, 0x0b, 0x0c, 0x19, 0x19, |
| 303 | 0x18, 0x13, 0x0f, 0x0c, 0x1a, 0x18, 0x1f, 0x19, 0x1e, 0x07, 0x1a, 0xc3, |
| 304 | 0x54, 0x51, 0x54, 0x51, 0x04, 0x53, 0x51, 0x54, 0x50, 0x02, 0x48, 0x1a, |
| 305 | 0x31, 0x18, 0x55, 0x74, 0x04, 0x0e, 0x09, 0x0d, 0x06, 0x10, 0x16, 0x1b, |
| 306 | 0x24, 0x01, 0x04, 0x0b, 0x04, 0x10, 0x3f, 0x0a, 0x41, 0x02, 0x41, 0x20, |
| 307 | 0x06, 0x12, 0x16, 0x21, 0x17, 0x2a, 0x1e, 0x15, 0x40, 0x27, 0x11, 0x0e, |
| 308 | 0x1e, 0x11, 0x15, 0x1f, 0x43, 0x13, 0x1a, 0x10, 0x15, 0x1b, 0x04, 0x09, |
| 309 | 0x4d, 0x2a, 0x0f, 0x19, 0x0a, 0x0a, 0x03, 0x05, 0x15, 0x3c, 0x64, 0x21, |
| 310 | 0x4b, 0x2e, 0x21, 0x28, 0x13, 0x47, 0x44, 0x19, 0x3f, 0x11, 0x18, 0x0b, |
| 311 | 0x0a, 0x07, 0x18, 0x0d, 0x07, 0x24, 0x2c, 0x2b, 0x21, 0x32, 0x10, 0x48, |
| 312 | 0x2a, 0x2d, 0x1e, 0x1a, 0x01, 0x0c, 0x43, 0x59, 0x28, 0x4e, 0x1c, 0x0d, |
| 313 | 0x5d, 0x24, 0x14, 0x0a, 0x05, 0x1f, 0x24, 0x32, 0x46, 0x3e, 0x5f, 0x3e, |
| 314 | 0x44, 0x1a, 0x30, 0x15, 0x0d, 0x07, 0x18, 0x2b, 0x03, 0x0d, 0x1a, 0x28, |
| 315 | 0x28, 0x57, 0xb2, 0x29, 0x27, 0x40, 0x2c, 0x23, 0x16, 0x63, 0x58, 0x1a, |
| 316 | 0x0a, 0x18, 0x11, 0x23, 0x08, 0x1b, 0x29, 0x05, 0x04, 0x0b, 0x15, 0x0d, |
| 317 | 0x14, 0x0b, 0x2a, 0x29, 0x5a, 0x62, 0x01, 0x19, 0x1e, 0x05, 0x05, 0x26, |
| 318 | 0x42, 0x42, 0x2a, 0x2a, 0x3f, 0x0d, 0x0f, 0x09, 0x05, 0x07, 0x01, 0x0b, |
| 319 | 0x25, 0x3e, 0x0d, 0x17, 0x11, 0x01, 0x03, 0x0d, 0x13, 0x20, 0x19, 0x11, |
| 320 | 0x03, 0x02, 0x01, 0x04, 0x11, 0x04, 0x05, 0x1b, 0x3d, 0x10, 0x29, 0x20, |
| 321 | 0x04, 0x04, 0x0a, 0x07, 0x04, 0x1f, 0x15, 0x20, 0x3e, 0x0f, 0x2a, 0x1e, |
| 322 | 0x00, 0x00, 0x00, 0x1b, 0x01, 0x4a, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, |
| 323 | 0x00, 0x00, 0x00, 0x1b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, |
| 324 | 0x00, 0x01, 0x00, 0x0c, 0x00, 0x1b, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, |
| 325 | 0x00, 0x02, 0x00, 0x07, 0x00, 0x27, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, |
| 326 | 0x00, 0x03, 0x00, 0x0c, 0x00, 0x1b, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, |
| 327 | 0x00, 0x04, 0x00, 0x0c, 0x00, 0x1b, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, |
| 328 | 0x00, 0x05, 0x00, 0x02, 0x00, 0x2e, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, |
| 329 | 0x00, 0x06, 0x00, 0x0c, 0x00, 0x1b, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, |
| 330 | 0x00, 0x0d, 0x00, 0x1b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, |
| 331 | 0x00, 0x0e, 0x00, 0x1a, 0x00, 0x30, 0x00, 0x03, 0x00, 0x00, 0x04, 0x09, |
| 332 | 0x00, 0x00, 0x00, 0x36, 0x00, 0x4a, 0x00, 0x03, 0x00, 0x00, 0x04, 0x09, |
| 333 | 0x00, 0x01, 0x00, 0x18, 0x00, 0x80, 0x00, 0x03, 0x00, 0x00, 0x04, 0x09, |
| 334 | 0x00, 0x02, 0x00, 0x0e, 0x00, 0x98, 0x00, 0x03, 0x00, 0x00, 0x04, 0x09, |
| 335 | 0x00, 0x03, 0x00, 0x18, 0x00, 0x80, 0x00, 0x03, 0x00, 0x00, 0x04, 0x09, |
| 336 | 0x00, 0x04, 0x00, 0x18, 0x00, 0x80, 0x00, 0x03, 0x00, 0x00, 0x04, 0x09, |
| 337 | 0x00, 0x05, 0x00, 0x04, 0x00, 0xa6, 0x00, 0x03, 0x00, 0x00, 0x04, 0x09, |
| 338 | 0x00, 0x06, 0x00, 0x18, 0x00, 0x80, 0x00, 0x03, 0x00, 0x00, 0x04, 0x09, |
| 339 | 0x00, 0x0d, 0x00, 0x36, 0x00, 0x4a, 0x00, 0x03, 0x00, 0x00, 0x04, 0x09, |
| 340 | 0x00, 0x0e, 0x00, 0x34, 0x00, 0xaa, 0x00, 0x03, 0x00, 0x01, 0x04, 0x09, |
| 341 | 0x00, 0x00, 0x00, 0x36, 0x00, 0x4a, 0x00, 0x03, 0x00, 0x01, 0x04, 0x09, |
| 342 | 0x00, 0x01, 0x00, 0x18, 0x00, 0x80, 0x00, 0x03, 0x00, 0x01, 0x04, 0x09, |
| 343 | 0x00, 0x02, 0x00, 0x0e, 0x00, 0x98, 0x00, 0x03, 0x00, 0x01, 0x04, 0x09, |
| 344 | 0x00, 0x03, 0x00, 0x18, 0x00, 0x80, 0x00, 0x03, 0x00, 0x01, 0x04, 0x09, |
| 345 | 0x00, 0x04, 0x00, 0x18, 0x00, 0x80, 0x00, 0x03, 0x00, 0x01, 0x04, 0x09, |
| 346 | 0x00, 0x05, 0x00, 0x04, 0x00, 0xa6, 0x00, 0x03, 0x00, 0x01, 0x04, 0x09, |
| 347 | 0x00, 0x06, 0x00, 0x18, 0x00, 0x80, 0x00, 0x03, 0x00, 0x01, 0x04, 0x09, |
| 348 | 0x00, 0x0d, 0x00, 0x36, 0x00, 0x4a, 0x00, 0x03, 0x00, 0x01, 0x04, 0x09, |
| 349 | 0x00, 0x0e, 0x00, 0x34, 0x00, 0xaa, 0x43, 0x6f, 0x70, 0x79, 0x72, 0x69, |
| 350 | 0x67, 0x68, 0x74, 0x20, 0x28, 0x63, 0x29, 0x20, 0x32, 0x30, 0x31, 0x35, |
| 351 | 0x2c, 0x20, 0x47, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x53, 0x70, 0x69, |
| 352 | 0x64, 0x65, 0x72, 0x53, 0x79, 0x6d, 0x62, 0x6f, 0x6c, 0x52, 0x65, 0x67, |
| 353 | 0x75, 0x6c, 0x61, 0x72, 0x56, 0x31, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, |
| 354 | 0x2f, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x73, 0x2e, 0x73, 0x69, 0x6c, |
| 355 | 0x2e, 0x6f, 0x72, 0x67, 0x2f, 0x4f, 0x46, 0x4c, 0x00, 0x43, 0x00, 0x6f, |
| 356 | 0x00, 0x70, 0x00, 0x79, 0x00, 0x72, 0x00, 0x69, 0x00, 0x67, 0x00, 0x68, |
| 357 | 0x00, 0x74, 0x00, 0x20, 0x00, 0x28, 0x00, 0x63, 0x00, 0x29, 0x00, 0x20, |
| 358 | 0x00, 0x32, 0x00, 0x30, 0x00, 0x31, 0x00, 0x35, 0x00, 0x2c, 0x00, 0x20, |
| 359 | 0x00, 0x47, 0x00, 0x6f, 0x00, 0x6f, 0x00, 0x67, 0x00, 0x6c, 0x00, 0x65, |
| 360 | 0x00, 0x2e, 0x00, 0x53, 0x00, 0x70, 0x00, 0x69, 0x00, 0x64, 0x00, 0x65, |
| 361 | 0x00, 0x72, 0x00, 0x53, 0x00, 0x79, 0x00, 0x6d, 0x00, 0x62, 0x00, 0x6f, |
| 362 | 0x00, 0x6c, 0x00, 0x52, 0x00, 0x65, 0x00, 0x67, 0x00, 0x75, 0x00, 0x6c, |
| 363 | 0x00, 0x61, 0x00, 0x72, 0x00, 0x56, 0x00, 0x31, 0x00, 0x68, 0x00, 0x74, |
| 364 | 0x00, 0x74, 0x00, 0x70, 0x00, 0x3a, 0x00, 0x2f, 0x00, 0x2f, 0x00, 0x73, |
| 365 | 0x00, 0x63, 0x00, 0x72, 0x00, 0x69, 0x00, 0x70, 0x00, 0x74, 0x00, 0x73, |
| 366 | 0x00, 0x2e, 0x00, 0x73, 0x00, 0x69, 0x00, 0x6c, 0x00, 0x2e, 0x00, 0x6f, |
| 367 | 0x00, 0x72, 0x00, 0x67, 0x00, 0x2f, 0x00, 0x4f, 0x00, 0x46, 0x00, 0x4c, |
| 368 | 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x66, |
| 369 | 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 370 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, |
| 371 | 0xff, 0xff, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 372 | 0x00, 0x0c, 0x00, 0x14, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, |
| 373 | 0x00, 0x02, 0x00, 0x00 |
| 374 | }; |
| 375 | |
Ben Wagner | 6112f0b | 2018-09-27 15:44:56 -0400 | [diff] [blame] | 376 | enum class SmoothBehavior { |
| 377 | none, // SmoothFonts produces no effect. |
| 378 | some, // SmoothFonts produces some effect, but not subpixel coverage. |
| 379 | subpixel, // SmoothFonts produces some effect and provides subpixel coverage. |
| 380 | }; |
| 381 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 382 | /** |
| 383 | * There does not appear to be a publicly accessable API for determining if lcd |
| 384 | * font smoothing will be applied if we request it. The main issue is that if |
| 385 | * smoothing is applied a gamma of 2.0 will be used, if not a gamma of 1.0. |
| 386 | */ |
Ben Wagner | 6112f0b | 2018-09-27 15:44:56 -0400 | [diff] [blame] | 387 | static SmoothBehavior smooth_behavior() { |
| 388 | static SmoothBehavior gSmoothBehavior = []{ |
| 389 | uint32_t noSmoothBitmap[16][16] = {}; |
| 390 | uint32_t smoothBitmap[16][16] = {}; |
| 391 | |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 392 | SkUniqueCFRef<CGColorSpaceRef> colorspace(CGColorSpaceCreateDeviceRGB()); |
Ben Wagner | 6112f0b | 2018-09-27 15:44:56 -0400 | [diff] [blame] | 393 | SkUniqueCFRef<CGContextRef> noSmoothContext( |
| 394 | CGBitmapContextCreate(&noSmoothBitmap, 16, 16, 8, 16*4, |
| 395 | colorspace.get(), BITMAP_INFO_RGB)); |
| 396 | SkUniqueCFRef<CGContextRef> smoothContext( |
| 397 | CGBitmapContextCreate(&smoothBitmap, 16, 16, 8, 16*4, |
| 398 | colorspace.get(), BITMAP_INFO_RGB)); |
Ben Wagner | 428ab73 | 2018-04-02 15:44:54 -0400 | [diff] [blame] | 399 | |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 400 | SkUniqueCFRef<CGDataProviderRef> data( |
Ben Wagner | 428ab73 | 2018-04-02 15:44:54 -0400 | [diff] [blame] | 401 | CGDataProviderCreateWithData(nullptr, kSpiderSymbol_ttf, |
| 402 | SK_ARRAY_COUNT(kSpiderSymbol_ttf), nullptr)); |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 403 | SkUniqueCFRef<CGFontRef> cgFont(CGFontCreateWithDataProvider(data.get())); |
Ben Wagner | 428ab73 | 2018-04-02 15:44:54 -0400 | [diff] [blame] | 404 | SkASSERT(cgFont); |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 405 | SkUniqueCFRef<CTFontRef> ctFont( |
Ben Wagner | 428ab73 | 2018-04-02 15:44:54 -0400 | [diff] [blame] | 406 | CTFontCreateWithGraphicsFont(cgFont.get(), 16, nullptr, nullptr)); |
| 407 | SkASSERT(ctFont); |
| 408 | |
Ben Wagner | 6112f0b | 2018-09-27 15:44:56 -0400 | [diff] [blame] | 409 | CGContextSetShouldSmoothFonts(noSmoothContext.get(), false); |
| 410 | CGContextSetShouldAntialias(noSmoothContext.get(), true); |
| 411 | CGContextSetTextDrawingMode(noSmoothContext.get(), kCGTextFill); |
| 412 | CGContextSetGrayFillColor(noSmoothContext.get(), 1, 1); |
| 413 | |
| 414 | CGContextSetShouldSmoothFonts(smoothContext.get(), true); |
| 415 | CGContextSetShouldAntialias(smoothContext.get(), true); |
| 416 | CGContextSetTextDrawingMode(smoothContext.get(), kCGTextFill); |
| 417 | CGContextSetGrayFillColor(smoothContext.get(), 1, 1); |
| 418 | |
Ben Wagner | 428ab73 | 2018-04-02 15:44:54 -0400 | [diff] [blame] | 419 | CGPoint point = CGPointMake(0, 3); |
| 420 | CGGlyph spiderGlyph = 3; |
Ben Wagner | 6112f0b | 2018-09-27 15:44:56 -0400 | [diff] [blame] | 421 | CTFontDrawGlyphs(ctFont.get(), &spiderGlyph, &point, 1, noSmoothContext.get()); |
| 422 | CTFontDrawGlyphs(ctFont.get(), &spiderGlyph, &point, 1, smoothContext.get()); |
ccameron | f8ee5b4 | 2016-10-04 15:02:02 -0700 | [diff] [blame] | 423 | |
Ben Wagner | 428ab73 | 2018-04-02 15:44:54 -0400 | [diff] [blame] | 424 | // For debugging. |
Ben Wagner | 6112f0b | 2018-09-27 15:44:56 -0400 | [diff] [blame] | 425 | //SkUniqueCFRef<CGImageRef> image(CGBitmapContextCreateImage(noSmoothContext())); |
| 426 | //SkUniqueCFRef<CGImageRef> image(CGBitmapContextCreateImage(smoothContext())); |
Ben Wagner | 428ab73 | 2018-04-02 15:44:54 -0400 | [diff] [blame] | 427 | |
Ben Wagner | 6112f0b | 2018-09-27 15:44:56 -0400 | [diff] [blame] | 428 | SmoothBehavior smoothBehavior = SmoothBehavior::none; |
Ben Wagner | 428ab73 | 2018-04-02 15:44:54 -0400 | [diff] [blame] | 429 | for (int x = 0; x < 16; ++x) { |
| 430 | for (int y = 0; y < 16; ++y) { |
Ben Wagner | 6112f0b | 2018-09-27 15:44:56 -0400 | [diff] [blame] | 431 | uint32_t smoothPixel = smoothBitmap[x][y]; |
| 432 | uint32_t r = (smoothPixel >> 16) & 0xFF; |
| 433 | uint32_t g = (smoothPixel >> 8) & 0xFF; |
| 434 | uint32_t b = (smoothPixel >> 0) & 0xFF; |
Ben Wagner | 428ab73 | 2018-04-02 15:44:54 -0400 | [diff] [blame] | 435 | if (r != g || r != b) { |
Ben Wagner | 6112f0b | 2018-09-27 15:44:56 -0400 | [diff] [blame] | 436 | return SmoothBehavior::subpixel; |
| 437 | } |
| 438 | if (noSmoothBitmap[x][y] != smoothPixel) { |
| 439 | smoothBehavior = SmoothBehavior::some; |
Ben Wagner | 428ab73 | 2018-04-02 15:44:54 -0400 | [diff] [blame] | 440 | } |
| 441 | } |
| 442 | } |
Ben Wagner | 6112f0b | 2018-09-27 15:44:56 -0400 | [diff] [blame] | 443 | return smoothBehavior; |
Mike Klein | 7028443 | 2017-09-19 13:07:27 -0400 | [diff] [blame] | 444 | }(); |
Ben Wagner | 6112f0b | 2018-09-27 15:44:56 -0400 | [diff] [blame] | 445 | return gSmoothBehavior; |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | class Offscreen { |
| 449 | public: |
bungeman | 3490263 | 2014-12-10 21:43:27 -0800 | [diff] [blame] | 450 | Offscreen() |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 451 | : fRGBSpace(nullptr) |
| 452 | , fCG(nullptr) |
bungeman | 3490263 | 2014-12-10 21:43:27 -0800 | [diff] [blame] | 453 | , fDoAA(false) |
| 454 | , fDoLCD(false) |
| 455 | { |
| 456 | fSize.set(0, 0); |
| 457 | } |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 458 | |
| 459 | CGRGBPixel* getCG(const SkScalerContext_Mac& context, const SkGlyph& glyph, |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 460 | CGGlyph glyphID, size_t* rowBytesPtr, bool generateA8FromLCD); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 461 | |
| 462 | private: |
| 463 | enum { |
| 464 | kSize = 32 * 32 * sizeof(CGRGBPixel) |
| 465 | }; |
| 466 | SkAutoSMalloc<kSize> fImageStorage; |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 467 | SkUniqueCFRef<CGColorSpaceRef> fRGBSpace; |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 468 | |
| 469 | // cached state |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 470 | SkUniqueCFRef<CGContextRef> fCG; |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 471 | SkISize fSize; |
| 472 | bool fDoAA; |
| 473 | bool fDoLCD; |
| 474 | |
| 475 | static int RoundSize(int dimension) { |
| 476 | return SkNextPow2(dimension); |
| 477 | } |
| 478 | }; |
| 479 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 480 | /////////////////////////////////////////////////////////////////////////////// |
| 481 | |
bungeman | 6e45bda | 2016-07-25 15:11:49 -0700 | [diff] [blame] | 482 | static bool find_dict_CGFloat(CFDictionaryRef dict, CFStringRef name, CGFloat* value) { |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 483 | CFNumberRef num; |
| 484 | return CFDictionaryGetValueIfPresent(dict, name, (const void**)&num) |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 485 | && CFNumberIsFloatType(num) |
| 486 | && CFNumberGetValue(num, kCFNumberCGFloatType, value); |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 487 | } |
| 488 | |
bungeman | 6e45bda | 2016-07-25 15:11:49 -0700 | [diff] [blame] | 489 | template <typename S, typename D, typename C> struct LinearInterpolater { |
| 490 | struct Mapping { |
| 491 | S src_val; |
| 492 | D dst_val; |
| 493 | }; |
| 494 | constexpr LinearInterpolater(Mapping const mapping[], int mappingCount) |
| 495 | : fMapping(mapping), fMappingCount(mappingCount) {} |
| 496 | |
| 497 | static D map(S value, S src_min, S src_max, D dst_min, D dst_max) { |
| 498 | SkASSERT(src_min < src_max); |
| 499 | SkASSERT(dst_min <= dst_max); |
| 500 | return C()(dst_min + (((value - src_min) * (dst_max - dst_min)) / (src_max - src_min))); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 501 | } |
bungeman | 6e45bda | 2016-07-25 15:11:49 -0700 | [diff] [blame] | 502 | |
bungeman | f1ef1fa | 2017-02-09 14:23:46 -0500 | [diff] [blame] | 503 | D map(S val) const { |
bungeman | 6e45bda | 2016-07-25 15:11:49 -0700 | [diff] [blame] | 504 | // -Inf to [0] |
| 505 | if (val < fMapping[0].src_val) { |
| 506 | return fMapping[0].dst_val; |
| 507 | } |
| 508 | |
| 509 | // Linear from [i] to [i+1] |
| 510 | for (int i = 0; i < fMappingCount - 1; ++i) { |
| 511 | if (val < fMapping[i+1].src_val) { |
| 512 | return map(val, fMapping[i].src_val, fMapping[i+1].src_val, |
| 513 | fMapping[i].dst_val, fMapping[i+1].dst_val); |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | // From [n] to +Inf |
| 518 | // if (fcweight < Inf) |
| 519 | return fMapping[fMappingCount - 1].dst_val; |
| 520 | } |
| 521 | |
| 522 | Mapping const * fMapping; |
| 523 | int fMappingCount; |
| 524 | }; |
| 525 | |
| 526 | struct RoundCGFloatToInt { |
| 527 | int operator()(CGFloat s) { return s + 0.5; } |
| 528 | }; |
bungeman | f1ef1fa | 2017-02-09 14:23:46 -0500 | [diff] [blame] | 529 | struct CGFloatIdentity { |
| 530 | CGFloat operator()(CGFloat s) { return s; } |
| 531 | }; |
| 532 | |
bungeman | 3e306f6 | 2017-03-29 11:32:02 -0400 | [diff] [blame] | 533 | /** Returns the [-1, 1] CTFontDescriptor weights for the |
| 534 | * <0, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000> CSS weights. |
| 535 | * |
| 536 | * It is assumed that the values will be interpolated linearly between these points. |
| 537 | * NSFontWeightXXX were added in 10.11, appear in 10.10, but do not appear in 10.9. |
| 538 | * The actual values appear to be stable, but they may change in the future without notice. |
| 539 | */ |
| 540 | static CGFloat(&get_NSFontWeight_mapping())[11] { |
| 541 | |
| 542 | // Declarations in <AppKit/AppKit.h> on macOS, <UIKit/UIKit.h> on iOS |
| 543 | #ifdef SK_BUILD_FOR_MAC |
| 544 | # define SK_KIT_FONT_WEIGHT_PREFIX "NS" |
| 545 | #endif |
| 546 | #ifdef SK_BUILD_FOR_IOS |
| 547 | # define SK_KIT_FONT_WEIGHT_PREFIX "UI" |
| 548 | #endif |
| 549 | static constexpr struct { |
| 550 | CGFloat defaultValue; |
| 551 | const char* name; |
| 552 | } nsFontWeightLoaderInfos[] = { |
| 553 | { -0.80f, SK_KIT_FONT_WEIGHT_PREFIX "FontWeightUltraLight" }, |
| 554 | { -0.60f, SK_KIT_FONT_WEIGHT_PREFIX "FontWeightThin" }, |
| 555 | { -0.40f, SK_KIT_FONT_WEIGHT_PREFIX "FontWeightLight" }, |
| 556 | { 0.00f, SK_KIT_FONT_WEIGHT_PREFIX "FontWeightRegular" }, |
| 557 | { 0.23f, SK_KIT_FONT_WEIGHT_PREFIX "FontWeightMedium" }, |
| 558 | { 0.30f, SK_KIT_FONT_WEIGHT_PREFIX "FontWeightSemibold" }, |
| 559 | { 0.40f, SK_KIT_FONT_WEIGHT_PREFIX "FontWeightBold" }, |
| 560 | { 0.56f, SK_KIT_FONT_WEIGHT_PREFIX "FontWeightHeavy" }, |
| 561 | { 0.62f, SK_KIT_FONT_WEIGHT_PREFIX "FontWeightBlack" }, |
| 562 | }; |
| 563 | |
| 564 | static_assert(SK_ARRAY_COUNT(nsFontWeightLoaderInfos) == 9, ""); |
| 565 | static CGFloat nsFontWeights[11]; |
| 566 | static SkOnce once; |
| 567 | once([&] { |
| 568 | size_t i = 0; |
| 569 | nsFontWeights[i++] = -1.00; |
| 570 | for (const auto& nsFontWeightLoaderInfo : nsFontWeightLoaderInfos) { |
| 571 | void* nsFontWeightValuePtr = dlsym(RTLD_DEFAULT, nsFontWeightLoaderInfo.name); |
| 572 | if (nsFontWeightValuePtr) { |
| 573 | nsFontWeights[i++] = *(static_cast<CGFloat*>(nsFontWeightValuePtr)); |
| 574 | } else { |
| 575 | nsFontWeights[i++] = nsFontWeightLoaderInfo.defaultValue; |
| 576 | } |
| 577 | } |
| 578 | nsFontWeights[i++] = 1.00; |
| 579 | }); |
| 580 | return nsFontWeights; |
| 581 | } |
| 582 | |
bungeman | f1ef1fa | 2017-02-09 14:23:46 -0500 | [diff] [blame] | 583 | /** Convert the [0, 1000] CSS weight to [-1, 1] CTFontDescriptor weight (for system fonts). |
| 584 | * |
| 585 | * The -1 to 1 weights reported by CTFontDescriptors have different mappings depending on if the |
| 586 | * CTFont is native or created from a CGDataProvider. |
| 587 | */ |
| 588 | static CGFloat fontstyle_to_ct_weight(int fontstyleWeight) { |
| 589 | using Interpolator = LinearInterpolater<int, CGFloat, CGFloatIdentity>; |
| 590 | |
| 591 | // Note that Mac supports the old OS2 version A so 0 through 10 are as if multiplied by 100. |
| 592 | // However, on this end we can't tell, so this is ignored. |
| 593 | |
bungeman | 3e306f6 | 2017-03-29 11:32:02 -0400 | [diff] [blame] | 594 | static Interpolator::Mapping nativeWeightMappings[11]; |
| 595 | static SkOnce once; |
| 596 | once([&] { |
| 597 | CGFloat(&nsFontWeights)[11] = get_NSFontWeight_mapping(); |
| 598 | for (int i = 0; i < 11; ++i) { |
| 599 | nativeWeightMappings[i].src_val = i * 100; |
| 600 | nativeWeightMappings[i].dst_val = nsFontWeights[i]; |
| 601 | } |
| 602 | }); |
bungeman | f1ef1fa | 2017-02-09 14:23:46 -0500 | [diff] [blame] | 603 | static constexpr Interpolator nativeInterpolator( |
| 604 | nativeWeightMappings, SK_ARRAY_COUNT(nativeWeightMappings)); |
| 605 | |
| 606 | return nativeInterpolator.map(fontstyleWeight); |
| 607 | } |
| 608 | |
bungeman | 6e45bda | 2016-07-25 15:11:49 -0700 | [diff] [blame] | 609 | |
bungeman | 2873c76 | 2017-01-13 11:40:21 -0500 | [diff] [blame] | 610 | /** Convert the [-1, 1] CTFontDescriptor weight to [0, 1000] CSS weight. |
| 611 | * |
| 612 | * The -1 to 1 weights reported by CTFontDescriptors have different mappings depending on if the |
| 613 | * CTFont is native or created from a CGDataProvider. |
| 614 | */ |
| 615 | static int ct_weight_to_fontstyle(CGFloat cgWeight, bool fromDataProvider) { |
bungeman | 6e45bda | 2016-07-25 15:11:49 -0700 | [diff] [blame] | 616 | using Interpolator = LinearInterpolater<CGFloat, int, RoundCGFloatToInt>; |
| 617 | |
bungeman | 6e45bda | 2016-07-25 15:11:49 -0700 | [diff] [blame] | 618 | // Note that Mac supports the old OS2 version A so 0 through 10 are as if multiplied by 100. |
bungeman | 2873c76 | 2017-01-13 11:40:21 -0500 | [diff] [blame] | 619 | // However, on this end we can't tell, so this is ignored. |
| 620 | |
| 621 | /** This mapping for CGDataProvider created fonts is determined by creating font data with every |
| 622 | * weight, creating a CTFont, and asking the CTFont for its weight. See the TypefaceStyle test |
| 623 | * in tests/TypefaceTest.cpp for the code used to determine these values. |
| 624 | */ |
| 625 | static constexpr Interpolator::Mapping dataProviderWeightMappings[] = { |
bungeman | 6e45bda | 2016-07-25 15:11:49 -0700 | [diff] [blame] | 626 | { -1.00, 0 }, |
| 627 | { -0.70, 100 }, |
| 628 | { -0.50, 200 }, |
| 629 | { -0.23, 300 }, |
| 630 | { 0.00, 400 }, |
| 631 | { 0.20, 500 }, |
| 632 | { 0.30, 600 }, |
| 633 | { 0.40, 700 }, |
| 634 | { 0.60, 800 }, |
| 635 | { 0.80, 900 }, |
| 636 | { 1.00, 1000 }, |
| 637 | }; |
bungeman | 2873c76 | 2017-01-13 11:40:21 -0500 | [diff] [blame] | 638 | static constexpr Interpolator dataProviderInterpolator( |
| 639 | dataProviderWeightMappings, SK_ARRAY_COUNT(dataProviderWeightMappings)); |
| 640 | |
bungeman | 3e306f6 | 2017-03-29 11:32:02 -0400 | [diff] [blame] | 641 | static Interpolator::Mapping nativeWeightMappings[11]; |
| 642 | static SkOnce once; |
| 643 | once([&] { |
| 644 | CGFloat(&nsFontWeights)[11] = get_NSFontWeight_mapping(); |
| 645 | for (int i = 0; i < 11; ++i) { |
| 646 | nativeWeightMappings[i].src_val = nsFontWeights[i]; |
| 647 | nativeWeightMappings[i].dst_val = i * 100; |
| 648 | } |
| 649 | }); |
bungeman | 2873c76 | 2017-01-13 11:40:21 -0500 | [diff] [blame] | 650 | static constexpr Interpolator nativeInterpolator( |
| 651 | nativeWeightMappings, SK_ARRAY_COUNT(nativeWeightMappings)); |
| 652 | |
| 653 | return fromDataProvider ? dataProviderInterpolator.map(cgWeight) |
| 654 | : nativeInterpolator.map(cgWeight); |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 655 | } |
| 656 | |
bungeman | f1ef1fa | 2017-02-09 14:23:46 -0500 | [diff] [blame] | 657 | /** Convert the [0, 10] CSS weight to [-1, 1] CTFontDescriptor width. */ |
| 658 | static int fontstyle_to_ct_width(int fontstyleWidth) { |
| 659 | using Interpolator = LinearInterpolater<int, CGFloat, CGFloatIdentity>; |
| 660 | |
| 661 | // Values determined by creating font data with every width, creating a CTFont, |
| 662 | // and asking the CTFont for its width. See TypefaceStyle test for basics. |
| 663 | static constexpr Interpolator::Mapping widthMappings[] = { |
| 664 | { 0, -0.5 }, |
| 665 | { 10, 0.5 }, |
| 666 | }; |
| 667 | static constexpr Interpolator interpolator(widthMappings, SK_ARRAY_COUNT(widthMappings)); |
| 668 | return interpolator.map(fontstyleWidth); |
| 669 | } |
| 670 | |
| 671 | /** Convert the [-1, 1] CTFontDescriptor width to [0, 10] CSS weight. */ |
bungeman | 6e45bda | 2016-07-25 15:11:49 -0700 | [diff] [blame] | 672 | static int ct_width_to_fontstyle(CGFloat cgWidth) { |
| 673 | using Interpolator = LinearInterpolater<CGFloat, int, RoundCGFloatToInt>; |
| 674 | |
| 675 | // Values determined by creating font data with every width, creating a CTFont, |
| 676 | // and asking the CTFont for its width. See TypefaceStyle test for basics. |
| 677 | static constexpr Interpolator::Mapping widthMappings[] = { |
| 678 | { -0.5, 0 }, |
| 679 | { 0.5, 10 }, |
| 680 | }; |
bungeman | 2873c76 | 2017-01-13 11:40:21 -0500 | [diff] [blame] | 681 | static constexpr Interpolator interpolator(widthMappings, SK_ARRAY_COUNT(widthMappings)); |
| 682 | return interpolator.map(cgWidth); |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 683 | } |
| 684 | |
bungeman | 2873c76 | 2017-01-13 11:40:21 -0500 | [diff] [blame] | 685 | static SkFontStyle fontstyle_from_descriptor(CTFontDescriptorRef desc, bool fromDataProvider) { |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 686 | SkUniqueCFRef<CFTypeRef> traits(CTFontDescriptorCopyAttribute(desc, kCTFontTraitsAttribute)); |
| 687 | if (!traits || CFDictionaryGetTypeID() != CFGetTypeID(traits.get())) { |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 688 | return SkFontStyle(); |
| 689 | } |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 690 | SkUniqueCFRef<CFDictionaryRef> fontTraitsDict(static_cast<CFDictionaryRef>(traits.release())); |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 691 | |
bungeman | 6e45bda | 2016-07-25 15:11:49 -0700 | [diff] [blame] | 692 | CGFloat weight, width, slant; |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 693 | if (!find_dict_CGFloat(fontTraitsDict.get(), kCTFontWeightTrait, &weight)) { |
bungeman | 336fdf2 | 2014-11-10 07:48:55 -0800 | [diff] [blame] | 694 | weight = 0; |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 695 | } |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 696 | if (!find_dict_CGFloat(fontTraitsDict.get(), kCTFontWidthTrait, &width)) { |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 697 | width = 0; |
| 698 | } |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 699 | if (!find_dict_CGFloat(fontTraitsDict.get(), kCTFontSlantTrait, &slant)) { |
bungeman | 336fdf2 | 2014-11-10 07:48:55 -0800 | [diff] [blame] | 700 | slant = 0; |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 701 | } |
| 702 | |
bungeman | 2873c76 | 2017-01-13 11:40:21 -0500 | [diff] [blame] | 703 | return SkFontStyle(ct_weight_to_fontstyle(weight, fromDataProvider), |
bungeman | 6e45bda | 2016-07-25 15:11:49 -0700 | [diff] [blame] | 704 | ct_width_to_fontstyle(width), |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 705 | slant ? SkFontStyle::kItalic_Slant |
bungeman | b4bb7d8 | 2016-04-27 10:21:04 -0700 | [diff] [blame] | 706 | : SkFontStyle::kUpright_Slant); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 707 | } |
| 708 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 709 | class SkTypeface_Mac : public SkTypeface { |
| 710 | public: |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 711 | SkTypeface_Mac(SkUniqueCFRef<CTFontRef> fontRef, SkUniqueCFRef<CFTypeRef> resourceRef, |
bungeman | 7888401 | 2015-06-08 13:39:12 -0700 | [diff] [blame] | 712 | const SkFontStyle& fs, bool isFixedPitch, |
bungeman | 53d5c6e | 2016-04-08 07:22:29 -0700 | [diff] [blame] | 713 | bool isLocalStream) |
bungeman | e3aea10 | 2016-07-13 05:16:58 -0700 | [diff] [blame] | 714 | : SkTypeface(fs, isFixedPitch) |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 715 | , fFontRef(std::move(fontRef)) |
| 716 | , fOriginatingCFTypeRef(std::move(resourceRef)) |
| 717 | , fHasColorGlyphs( |
| 718 | SkToBool(CTFontGetSymbolicTraits(fFontRef.get()) & kCTFontColorGlyphsTrait)) |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 719 | , fIsLocalStream(isLocalStream) |
reed@google.com | ce8b3de | 2013-03-26 19:30:16 +0000 | [diff] [blame] | 720 | { |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 721 | SkASSERT(fFontRef); |
reed@google.com | ce8b3de | 2013-03-26 19:30:16 +0000 | [diff] [blame] | 722 | } |
skia.committer@gmail.com | 37cbc7f | 2013-03-27 07:01:04 +0000 | [diff] [blame] | 723 | |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 724 | SkUniqueCFRef<CTFontRef> fFontRef; |
| 725 | SkUniqueCFRef<CFTypeRef> fOriginatingCFTypeRef; |
bungeman | e3bea5c | 2015-04-07 07:34:36 -0700 | [diff] [blame] | 726 | const bool fHasColorGlyphs; |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 727 | |
| 728 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 729 | int onGetUPEM() const override; |
| 730 | SkStreamAsset* onOpenStream(int* ttcIndex) const override; |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 731 | std::unique_ptr<SkFontData> onMakeFontData() const override; |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 732 | int onGetVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[], |
| 733 | int coordinateCount) const override; |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 734 | void onGetFamilyName(SkString* familyName) const override; |
| 735 | SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const override; |
| 736 | int onGetTableTags(SkFontTableTag tags[]) const override; |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 737 | size_t onGetTableData(SkFontTableTag, size_t offset, size_t length, void* data) const override; |
reed | a9322c2 | 2016-04-12 06:47:05 -0700 | [diff] [blame] | 738 | SkScalerContext* onCreateScalerContext(const SkScalerContextEffects&, |
| 739 | const SkDescriptor*) const override; |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 740 | void onFilterRec(SkScalerContextRec*) const override; |
| 741 | void onGetFontDescriptor(SkFontDescriptor*, bool*) const override; |
Hal Canary | 46cc3da | 2018-05-09 11:50:34 -0400 | [diff] [blame] | 742 | void getGlyphToUnicodeMap(SkUnichar*) const override; |
Hal Canary | 209e4b1 | 2017-05-04 14:23:55 -0400 | [diff] [blame] | 743 | std::unique_ptr<SkAdvancedTypefaceMetrics> onGetAdvancedMetrics() const override; |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 744 | int onCharsToGlyphs(const void* chars, Encoding, |
| 745 | uint16_t glyphs[], int glyphCount) const override; |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 746 | int onCountGlyphs() const override; |
skia.committer@gmail.com | c1641fc | 2013-03-21 07:01:39 +0000 | [diff] [blame] | 747 | |
Mike Reed | 342a9fa | 2017-05-03 15:48:22 -0400 | [diff] [blame] | 748 | void* onGetCTFontRef() const override { return (void*)fFontRef.get(); } |
| 749 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 750 | private: |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 751 | bool fIsLocalStream; |
reed@google.com | ce8b3de | 2013-03-26 19:30:16 +0000 | [diff] [blame] | 752 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 753 | typedef SkTypeface INHERITED; |
| 754 | }; |
| 755 | |
bungeman | 82a455f | 2016-04-14 08:04:45 -0700 | [diff] [blame] | 756 | static bool find_by_CTFontRef(SkTypeface* cached, void* context) { |
bungeman | 64fb51d | 2015-05-04 12:03:50 -0700 | [diff] [blame] | 757 | CTFontRef self = (CTFontRef)context; |
Mike Reed | 342a9fa | 2017-05-03 15:48:22 -0400 | [diff] [blame] | 758 | CTFontRef other = (CTFontRef)cached->internal_private_getCTFontRef(); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 759 | |
bungeman | 64fb51d | 2015-05-04 12:03:50 -0700 | [diff] [blame] | 760 | return CFEqual(self, other); |
| 761 | } |
| 762 | |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 763 | /** Creates a typeface, searching the cache if isLocalStream is false. */ |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 764 | static sk_sp<SkTypeface> create_from_CTFontRef(SkUniqueCFRef<CTFontRef> font, |
| 765 | SkUniqueCFRef<CFTypeRef> resource, |
Mike Reed | 5922739 | 2017-09-26 09:46:08 -0400 | [diff] [blame] | 766 | bool isLocalStream) { |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 767 | SkASSERT(font); |
bungeman | bea9748 | 2016-08-24 08:29:50 -0700 | [diff] [blame] | 768 | |
| 769 | if (!isLocalStream) { |
| 770 | SkTypeface* face = SkTypefaceCache::FindByProcAndRef(find_by_CTFontRef, (void*)font.get()); |
| 771 | if (face) { |
Mike Reed | 5922739 | 2017-09-26 09:46:08 -0400 | [diff] [blame] | 772 | return sk_sp<SkTypeface>(face); |
bungeman | bea9748 | 2016-08-24 08:29:50 -0700 | [diff] [blame] | 773 | } |
| 774 | } |
| 775 | |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 776 | SkUniqueCFRef<CTFontDescriptorRef> desc(CTFontCopyFontDescriptor(font.get())); |
bungeman | 2873c76 | 2017-01-13 11:40:21 -0500 | [diff] [blame] | 777 | SkFontStyle style = fontstyle_from_descriptor(desc.get(), isLocalStream); |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 778 | CTFontSymbolicTraits traits = CTFontGetSymbolicTraits(font.get()); |
bungeman | bea9748 | 2016-08-24 08:29:50 -0700 | [diff] [blame] | 779 | bool isFixedPitch = SkToBool(traits & kCTFontMonoSpaceTrait); |
| 780 | |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 781 | SkTypeface* face = new SkTypeface_Mac(std::move(font), std::move(resource), |
bungeman | bea9748 | 2016-08-24 08:29:50 -0700 | [diff] [blame] | 782 | style, isFixedPitch, isLocalStream); |
| 783 | if (!isLocalStream) { |
| 784 | SkTypefaceCache::Add(face); |
| 785 | } |
Mike Reed | 5922739 | 2017-09-26 09:46:08 -0400 | [diff] [blame] | 786 | return sk_sp<SkTypeface>(face); |
bungeman | bea9748 | 2016-08-24 08:29:50 -0700 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | /** Creates a typeface from a descriptor, searching the cache. */ |
Mike Reed | 5922739 | 2017-09-26 09:46:08 -0400 | [diff] [blame] | 790 | static sk_sp<SkTypeface> create_from_desc(CTFontDescriptorRef desc) { |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 791 | SkUniqueCFRef<CTFontRef> ctFont(CTFontCreateWithFontDescriptor(desc, 0, nullptr)); |
bungeman | bea9748 | 2016-08-24 08:29:50 -0700 | [diff] [blame] | 792 | if (!ctFont) { |
| 793 | return nullptr; |
| 794 | } |
| 795 | |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 796 | return create_from_CTFontRef(std::move(ctFont), nullptr, false); |
bungeman | bea9748 | 2016-08-24 08:29:50 -0700 | [diff] [blame] | 797 | } |
| 798 | |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 799 | static SkUniqueCFRef<CTFontDescriptorRef> create_descriptor(const char familyName[], |
| 800 | const SkFontStyle& style) { |
| 801 | SkUniqueCFRef<CFMutableDictionaryRef> cfAttributes( |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 802 | CFDictionaryCreateMutable(kCFAllocatorDefault, 0, |
| 803 | &kCFTypeDictionaryKeyCallBacks, |
| 804 | &kCFTypeDictionaryValueCallBacks)); |
| 805 | |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 806 | SkUniqueCFRef<CFMutableDictionaryRef> cfTraits( |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 807 | CFDictionaryCreateMutable(kCFAllocatorDefault, 0, |
| 808 | &kCFTypeDictionaryKeyCallBacks, |
| 809 | &kCFTypeDictionaryValueCallBacks)); |
| 810 | |
bungeman | 83f1f44 | 2017-02-06 13:21:33 -0500 | [diff] [blame] | 811 | if (!cfAttributes || !cfTraits) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 812 | return nullptr; |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 813 | } |
| 814 | |
bungeman | f1ef1fa | 2017-02-09 14:23:46 -0500 | [diff] [blame] | 815 | // CTFontTraits (symbolic) |
Ben Wagner | 9f9c2ea | 2018-10-01 16:04:09 -0400 | [diff] [blame] | 816 | // macOS 14 and iOS 12 seem to behave badly when kCTFontSymbolicTrait is set. |
| 817 | |
bungeman | f1ef1fa | 2017-02-09 14:23:46 -0500 | [diff] [blame] | 818 | // CTFontTraits (weight) |
| 819 | CGFloat ctWeight = fontstyle_to_ct_weight(style.weight()); |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 820 | SkUniqueCFRef<CFNumberRef> cfFontWeight( |
bungeman | f1ef1fa | 2017-02-09 14:23:46 -0500 | [diff] [blame] | 821 | CFNumberCreate(kCFAllocatorDefault, kCFNumberCGFloatType, &ctWeight)); |
| 822 | if (cfFontWeight) { |
| 823 | CFDictionaryAddValue(cfTraits.get(), kCTFontWeightTrait, cfFontWeight.get()); |
| 824 | } |
| 825 | // CTFontTraits (width) |
Ben Wagner | 9df4392 | 2018-11-26 14:46:13 -0500 | [diff] [blame] | 826 | CGFloat ctWidth = fontstyle_to_ct_width(style.width()); |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 827 | SkUniqueCFRef<CFNumberRef> cfFontWidth( |
bungeman | f1ef1fa | 2017-02-09 14:23:46 -0500 | [diff] [blame] | 828 | CFNumberCreate(kCFAllocatorDefault, kCFNumberCGFloatType, &ctWidth)); |
| 829 | if (cfFontWidth) { |
| 830 | CFDictionaryAddValue(cfTraits.get(), kCTFontWidthTrait, cfFontWidth.get()); |
| 831 | } |
| 832 | // CTFontTraits (slant) |
| 833 | CGFloat ctSlant = style.slant() == SkFontStyle::kUpright_Slant ? 0 : 1; |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 834 | SkUniqueCFRef<CFNumberRef> cfFontSlant( |
bungeman | f1ef1fa | 2017-02-09 14:23:46 -0500 | [diff] [blame] | 835 | CFNumberCreate(kCFAllocatorDefault, kCFNumberCGFloatType, &ctSlant)); |
| 836 | if (cfFontSlant) { |
| 837 | CFDictionaryAddValue(cfTraits.get(), kCTFontSlantTrait, cfFontSlant.get()); |
| 838 | } |
| 839 | // CTFontTraits |
| 840 | CFDictionaryAddValue(cfAttributes.get(), kCTFontTraitsAttribute, cfTraits.get()); |
bungeman | 83f1f44 | 2017-02-06 13:21:33 -0500 | [diff] [blame] | 841 | |
| 842 | // CTFontFamilyName |
| 843 | if (familyName) { |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 844 | SkUniqueCFRef<CFStringRef> cfFontName = make_CFString(familyName); |
bungeman | 83f1f44 | 2017-02-06 13:21:33 -0500 | [diff] [blame] | 845 | if (cfFontName) { |
| 846 | CFDictionaryAddValue(cfAttributes.get(), kCTFontFamilyNameAttribute, cfFontName.get()); |
| 847 | } |
| 848 | } |
bungeman | 64fb51d | 2015-05-04 12:03:50 -0700 | [diff] [blame] | 849 | |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 850 | return SkUniqueCFRef<CTFontDescriptorRef>( |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 851 | CTFontDescriptorCreateWithAttributes(cfAttributes.get())); |
bungeman | bea9748 | 2016-08-24 08:29:50 -0700 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | /** Creates a typeface from a name, searching the cache. */ |
Mike Reed | 5922739 | 2017-09-26 09:46:08 -0400 | [diff] [blame] | 855 | static sk_sp<SkTypeface> create_from_name(const char familyName[], const SkFontStyle& style) { |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 856 | SkUniqueCFRef<CTFontDescriptorRef> desc = create_descriptor(familyName, style); |
bungeman | bea9748 | 2016-08-24 08:29:50 -0700 | [diff] [blame] | 857 | if (!desc) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 858 | return nullptr; |
bungeman | 64fb51d | 2015-05-04 12:03:50 -0700 | [diff] [blame] | 859 | } |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 860 | return create_from_desc(desc.get()); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | /////////////////////////////////////////////////////////////////////////////// |
| 864 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 865 | /* This function is visible on the outside. It first searches the cache, and if |
| 866 | * not found, returns a new entry (after adding it to the cache). |
| 867 | */ |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 868 | SkTypeface* SkCreateTypefaceFromCTFont(CTFontRef font, CFTypeRef resource) { |
| 869 | CFRetain(font); |
| 870 | if (resource) { |
| 871 | CFRetain(resource); |
bungeman | 53d5c6e | 2016-04-08 07:22:29 -0700 | [diff] [blame] | 872 | } |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 873 | return create_from_CTFontRef(SkUniqueCFRef<CTFontRef>(font), |
| 874 | SkUniqueCFRef<CFTypeRef>(resource), |
Mike Reed | 5922739 | 2017-09-26 09:46:08 -0400 | [diff] [blame] | 875 | false).release(); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 876 | } |
| 877 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 878 | static const char* map_css_names(const char* name) { |
| 879 | static const struct { |
| 880 | const char* fFrom; // name the caller specified |
| 881 | const char* fTo; // "canonical" name we map to |
| 882 | } gPairs[] = { |
| 883 | { "sans-serif", "Helvetica" }, |
| 884 | { "serif", "Times" }, |
| 885 | { "monospace", "Courier" } |
| 886 | }; |
| 887 | |
| 888 | for (size_t i = 0; i < SK_ARRAY_COUNT(gPairs); i++) { |
| 889 | if (strcmp(name, gPairs[i].fFrom) == 0) { |
| 890 | return gPairs[i].fTo; |
| 891 | } |
| 892 | } |
| 893 | return name; // no change |
| 894 | } |
| 895 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 896 | /////////////////////////////////////////////////////////////////////////////// |
| 897 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 898 | class SkScalerContext_Mac : public SkScalerContext { |
| 899 | public: |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 900 | SkScalerContext_Mac(sk_sp<SkTypeface_Mac>, const SkScalerContextEffects&, const SkDescriptor*); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 901 | |
| 902 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 903 | unsigned generateGlyphCount(void) override; |
| 904 | uint16_t generateCharToGlyph(SkUnichar uni) override; |
Ben Wagner | e541645 | 2018-08-09 14:03:42 -0400 | [diff] [blame] | 905 | bool generateAdvance(SkGlyph* glyph) override; |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 906 | void generateMetrics(SkGlyph* glyph) override; |
| 907 | void generateImage(const SkGlyph& glyph) override; |
Ben Wagner | 5ddb308 | 2018-03-29 11:18:06 -0400 | [diff] [blame] | 908 | bool generatePath(SkGlyphID glyph, SkPath* path) override; |
Mike Reed | b5784ac | 2018-11-12 09:35:15 -0500 | [diff] [blame] | 909 | void generateFontMetrics(SkFontMetrics*) override; |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 910 | |
| 911 | private: |
| 912 | static void CTPathElement(void *info, const CGPathElement *element); |
skia.committer@gmail.com | 539f364 | 2013-05-16 07:01:00 +0000 | [diff] [blame] | 913 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 914 | Offscreen fOffscreen; |
skia.committer@gmail.com | 539f364 | 2013-05-16 07:01:00 +0000 | [diff] [blame] | 915 | |
bungeman | 3b4b66c | 2015-01-08 08:33:44 -0800 | [diff] [blame] | 916 | /** Unrotated variant of fCTFont. |
| 917 | * |
| 918 | * In 10.10.1 CTFontGetAdvancesForGlyphs applies the font transform to the width of the |
| 919 | * advances, but always sets the height to 0. This font is used to get the advances of the |
| 920 | * unrotated glyph, and then the rotation is applied separately. |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 921 | * |
| 922 | * CT vertical metrics are pre-rotated (in em space, before transform) 90deg clock-wise. |
bungeman | 18ec7b9 | 2017-02-09 17:15:59 -0500 | [diff] [blame] | 923 | * This makes kCTFontOrientationDefault dangerous, because the metrics from |
| 924 | * kCTFontOrientationHorizontal are in a different space from kCTFontOrientationVertical. |
| 925 | * With kCTFontOrientationVertical the advances must be unrotated. |
bungeman | ef27ce3 | 2015-10-29 09:30:32 -0700 | [diff] [blame] | 926 | * |
| 927 | * Sometimes, creating a copy of a CTFont with the same size but different trasform will select |
| 928 | * different underlying font data. As a result, avoid ever creating more than one CTFont per |
| 929 | * SkScalerContext to ensure that only one CTFont is used. |
| 930 | * |
| 931 | * As a result of the above (and other constraints) this font contains the size, but not the |
| 932 | * transform. The transform must always be applied separately. |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 933 | */ |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 934 | SkUniqueCFRef<CTFontRef> fCTFont; |
bungeman | ef27ce3 | 2015-10-29 09:30:32 -0700 | [diff] [blame] | 935 | |
| 936 | /** The transform without the font size. */ |
| 937 | CGAffineTransform fTransform; |
| 938 | CGAffineTransform fInvTransform; |
skia.committer@gmail.com | 539f364 | 2013-05-16 07:01:00 +0000 | [diff] [blame] | 939 | |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 940 | SkUniqueCFRef<CGFontRef> fCGFont; |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 941 | uint16_t fGlyphCount; |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 942 | const bool fDoSubPosition; |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 943 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 944 | friend class Offscreen; |
reed@google.com | 0da4861 | 2013-03-19 16:06:52 +0000 | [diff] [blame] | 945 | |
| 946 | typedef SkScalerContext INHERITED; |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 947 | }; |
| 948 | |
bungeman | 7cbeaae | 2015-09-22 09:54:56 -0700 | [diff] [blame] | 949 | // CTFontCreateCopyWithAttributes or CTFontCreateCopyWithSymbolicTraits cannot be used on 10.10 |
bungeman | 0584631 | 2015-09-23 12:51:28 -0700 | [diff] [blame] | 950 | // and later, as they will return different underlying fonts depending on the size requested. |
| 951 | // It is not possible to use descriptors with CTFontCreateWithFontDescriptor, since that does not |
| 952 | // work with non-system fonts. As a result, create the strike specific CTFonts from the underlying |
| 953 | // CGFont. |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 954 | static SkUniqueCFRef<CTFontRef> ctfont_create_exact_copy(CTFontRef baseFont, CGFloat textSize, |
| 955 | const CGAffineTransform* transform) |
bungeman | 7cbeaae | 2015-09-22 09:54:56 -0700 | [diff] [blame] | 956 | { |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 957 | SkUniqueCFRef<CGFontRef> baseCGFont(CTFontCopyGraphicsFont(baseFont, nullptr)); |
bungeman | 7cbeaae | 2015-09-22 09:54:56 -0700 | [diff] [blame] | 958 | |
bungeman | 0584631 | 2015-09-23 12:51:28 -0700 | [diff] [blame] | 959 | // The last parameter (CTFontDescriptorRef attributes) *must* be nullptr. |
| 960 | // If non-nullptr then with fonts with variation axes, the copy will fail in |
| 961 | // CGFontVariationFromDictCallback when it assumes kCGFontVariationAxisName is CFNumberRef |
| 962 | // which it quite obviously is not. |
bungeman | 7cbeaae | 2015-09-22 09:54:56 -0700 | [diff] [blame] | 963 | |
bungeman | 0584631 | 2015-09-23 12:51:28 -0700 | [diff] [blame] | 964 | // Because we cannot setup the CTFont descriptor to match, the same restriction applies here |
| 965 | // as other uses of CTFontCreateWithGraphicsFont which is that such CTFonts should not escape |
| 966 | // the scaler context, since they aren't 'normal'. |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 967 | return SkUniqueCFRef<CTFontRef>( |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 968 | CTFontCreateWithGraphicsFont(baseCGFont.get(), textSize, transform, nullptr)); |
bungeman | 7cbeaae | 2015-09-22 09:54:56 -0700 | [diff] [blame] | 969 | } |
| 970 | |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 971 | SkScalerContext_Mac::SkScalerContext_Mac(sk_sp<SkTypeface_Mac> typeface, |
reed | a9322c2 | 2016-04-12 06:47:05 -0700 | [diff] [blame] | 972 | const SkScalerContextEffects& effects, |
reed@google.com | 0da4861 | 2013-03-19 16:06:52 +0000 | [diff] [blame] | 973 | const SkDescriptor* desc) |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 974 | : INHERITED(std::move(typeface), effects, desc) |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 975 | , fDoSubPosition(SkToBool(fRec.fFlags & kSubpixelPositioning_Flag)) |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 976 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 977 | { |
reed | d0f4173 | 2015-07-10 12:08:38 -0700 | [diff] [blame] | 978 | AUTO_CG_LOCK(); |
| 979 | |
Mike Reed | 342a9fa | 2017-05-03 15:48:22 -0400 | [diff] [blame] | 980 | CTFontRef ctFont = (CTFontRef)this->getTypeface()->internal_private_getCTFontRef(); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 981 | CFIndex numGlyphs = CTFontGetGlyphCount(ctFont); |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 982 | SkASSERT(numGlyphs >= 1 && numGlyphs <= 0xFFFF); |
| 983 | fGlyphCount = SkToU16(numGlyphs); |
skia.committer@gmail.com | 539f364 | 2013-05-16 07:01:00 +0000 | [diff] [blame] | 984 | |
bungeman | 3490263 | 2014-12-10 21:43:27 -0800 | [diff] [blame] | 985 | // CT on (at least) 10.9 will size color glyphs down from the requested size, but not up. |
| 986 | // As a result, it is necessary to know the actual device size and request that. |
| 987 | SkVector scale; |
bungeman | be2284d | 2014-11-25 08:08:09 -0800 | [diff] [blame] | 988 | SkMatrix skTransform; |
bungeman | e55131c | 2016-08-24 12:01:31 -0700 | [diff] [blame] | 989 | bool invertible = fRec.computeMatrices(SkScalerContextRec::kVertical_PreMatrixScale, |
bungeman | 4ec46aa | 2017-02-15 17:49:12 -0500 | [diff] [blame] | 990 | &scale, &skTransform, nullptr, nullptr, nullptr); |
bungeman | aae3091 | 2015-03-02 13:43:26 -0800 | [diff] [blame] | 991 | fTransform = MatrixToCGAffineTransform(skTransform); |
bungeman | 1f0e78d | 2016-08-23 13:19:01 -0700 | [diff] [blame] | 992 | // CGAffineTransformInvert documents that if the transform is non-invertible it will return the |
| 993 | // passed transform unchanged. It does so, but then also prints a message to stdout. Avoid this. |
bungeman | e55131c | 2016-08-24 12:01:31 -0700 | [diff] [blame] | 994 | if (invertible) { |
bungeman | 1f0e78d | 2016-08-23 13:19:01 -0700 | [diff] [blame] | 995 | fInvTransform = CGAffineTransformInvert(fTransform); |
| 996 | } else { |
| 997 | fInvTransform = fTransform; |
| 998 | } |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 999 | |
bungeman | be2284d | 2014-11-25 08:08:09 -0800 | [diff] [blame] | 1000 | // The transform contains everything except the requested text size. |
| 1001 | // 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] | 1002 | CGFloat textSize = ScalarToCG(scale.y()); |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 1003 | fCTFont = ctfont_create_exact_copy(ctFont, textSize, nullptr); |
| 1004 | fCGFont.reset(CTFontCopyGraphicsFont(fCTFont.get(), nullptr)); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | CGRGBPixel* Offscreen::getCG(const SkScalerContext_Mac& context, const SkGlyph& glyph, |
| 1008 | CGGlyph glyphID, size_t* rowBytesPtr, |
mtklein | bbd4018 | 2015-09-08 08:19:33 -0700 | [diff] [blame] | 1009 | bool generateA8FromLCD) { |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1010 | if (!fRGBSpace) { |
| 1011 | //It doesn't appear to matter what color space is specified. |
| 1012 | //Regular blends and antialiased text are always (s*a + d*(1-a)) |
Ben Wagner | 6112f0b | 2018-09-27 15:44:56 -0400 | [diff] [blame] | 1013 | //and subpixel antialiased text is always g=2.0. |
bungeman | e194c49 | 2014-07-16 13:46:06 -0700 | [diff] [blame] | 1014 | fRGBSpace.reset(CGColorSpaceCreateDeviceRGB()); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1015 | } |
| 1016 | |
| 1017 | // default to kBW_Format |
| 1018 | bool doAA = false; |
| 1019 | bool doLCD = false; |
| 1020 | |
| 1021 | if (SkMask::kBW_Format != glyph.fMaskFormat) { |
| 1022 | doLCD = true; |
| 1023 | doAA = true; |
| 1024 | } |
| 1025 | |
| 1026 | // FIXME: lcd smoothed un-hinted rasterization unsupported. |
| 1027 | if (!generateA8FromLCD && SkMask::kA8_Format == glyph.fMaskFormat) { |
| 1028 | doLCD = false; |
| 1029 | doAA = true; |
| 1030 | } |
| 1031 | |
bungeman | 3490263 | 2014-12-10 21:43:27 -0800 | [diff] [blame] | 1032 | // If this font might have color glyphs, disable LCD as there's no way to support it. |
| 1033 | // CoreText doesn't tell us which format it ended up using, so we can't detect it. |
bungeman | e280d06 | 2016-03-24 11:27:05 -0700 | [diff] [blame] | 1034 | // A8 will end up black on transparent, but TODO: we can detect gray and set to A8. |
bungeman | 3490263 | 2014-12-10 21:43:27 -0800 | [diff] [blame] | 1035 | if (SkMask::kARGB32_Format == glyph.fMaskFormat) { |
| 1036 | doLCD = false; |
| 1037 | } |
| 1038 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1039 | size_t rowBytes = fSize.fWidth * sizeof(CGRGBPixel); |
| 1040 | if (!fCG || fSize.fWidth < glyph.fWidth || fSize.fHeight < glyph.fHeight) { |
| 1041 | if (fSize.fWidth < glyph.fWidth) { |
| 1042 | fSize.fWidth = RoundSize(glyph.fWidth); |
| 1043 | } |
| 1044 | if (fSize.fHeight < glyph.fHeight) { |
| 1045 | fSize.fHeight = RoundSize(glyph.fHeight); |
| 1046 | } |
| 1047 | |
| 1048 | rowBytes = fSize.fWidth * sizeof(CGRGBPixel); |
| 1049 | void* image = fImageStorage.reset(rowBytes * fSize.fHeight); |
bungeman | 3490263 | 2014-12-10 21:43:27 -0800 | [diff] [blame] | 1050 | const CGImageAlphaInfo alpha = (SkMask::kARGB32_Format == glyph.fMaskFormat) |
| 1051 | ? kCGImageAlphaPremultipliedFirst |
| 1052 | : kCGImageAlphaNoneSkipFirst; |
| 1053 | const CGBitmapInfo bitmapInfo = kCGBitmapByteOrder32Host | alpha; |
bungeman | e194c49 | 2014-07-16 13:46:06 -0700 | [diff] [blame] | 1054 | fCG.reset(CGBitmapContextCreate(image, fSize.fWidth, fSize.fHeight, 8, |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 1055 | rowBytes, fRGBSpace.get(), bitmapInfo)); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1056 | |
bungeman | 3490263 | 2014-12-10 21:43:27 -0800 | [diff] [blame] | 1057 | // Skia handles quantization and subpixel positioning, |
| 1058 | // so disable quantization and enabe subpixel positioning in CG. |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 1059 | CGContextSetAllowsFontSubpixelQuantization(fCG.get(), false); |
| 1060 | CGContextSetShouldSubpixelQuantizeFonts(fCG.get(), false); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1061 | |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 1062 | // Because CG always draws from the horizontal baseline, |
| 1063 | // if there is a non-integral translation from the horizontal origin to the vertical origin, |
| 1064 | // then CG cannot draw the glyph in the correct location without subpixel positioning. |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 1065 | CGContextSetAllowsFontSubpixelPositioning(fCG.get(), true); |
| 1066 | CGContextSetShouldSubpixelPositionFonts(fCG.get(), true); |
bungeman | 3490263 | 2014-12-10 21:43:27 -0800 | [diff] [blame] | 1067 | |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 1068 | CGContextSetTextDrawingMode(fCG.get(), kCGTextFill); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1069 | |
bungeman | e280d06 | 2016-03-24 11:27:05 -0700 | [diff] [blame] | 1070 | // Draw black on white to create mask. (Special path exists to speed this up in CG.) |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 1071 | CGContextSetGrayFillColor(fCG.get(), 0.0f, 1.0f); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1072 | |
| 1073 | // force our checks below to happen |
| 1074 | fDoAA = !doAA; |
| 1075 | fDoLCD = !doLCD; |
bungeman | 3490263 | 2014-12-10 21:43:27 -0800 | [diff] [blame] | 1076 | |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 1077 | CGContextSetTextMatrix(fCG.get(), context.fTransform); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | if (fDoAA != doAA) { |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 1081 | CGContextSetShouldAntialias(fCG.get(), doAA); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1082 | fDoAA = doAA; |
| 1083 | } |
| 1084 | if (fDoLCD != doLCD) { |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 1085 | CGContextSetShouldSmoothFonts(fCG.get(), doLCD); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1086 | fDoLCD = doLCD; |
| 1087 | } |
| 1088 | |
| 1089 | CGRGBPixel* image = (CGRGBPixel*)fImageStorage.get(); |
| 1090 | // skip rows based on the glyph's height |
| 1091 | image += (fSize.fHeight - glyph.fHeight) * fSize.fWidth; |
| 1092 | |
bungeman | e280d06 | 2016-03-24 11:27:05 -0700 | [diff] [blame] | 1093 | // Erase to white (or transparent black if it's a color glyph, to not composite against white). |
| 1094 | uint32_t bgColor = (SkMask::kARGB32_Format != glyph.fMaskFormat) ? 0xFFFFFFFF : 0x00000000; |
| 1095 | sk_memset_rect32(image, bgColor, glyph.fWidth, glyph.fHeight, rowBytes); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1096 | |
| 1097 | float subX = 0; |
| 1098 | float subY = 0; |
| 1099 | if (context.fDoSubPosition) { |
| 1100 | subX = SkFixedToFloat(glyph.getSubXFixed()); |
| 1101 | subY = SkFixedToFloat(glyph.getSubYFixed()); |
| 1102 | } |
skia.committer@gmail.com | 539f364 | 2013-05-16 07:01:00 +0000 | [diff] [blame] | 1103 | |
bungeman | 3490263 | 2014-12-10 21:43:27 -0800 | [diff] [blame] | 1104 | CGPoint point = CGPointMake(-glyph.fLeft + subX, glyph.fTop + glyph.fHeight - subY); |
bungeman | aae3091 | 2015-03-02 13:43:26 -0800 | [diff] [blame] | 1105 | // Prior to 10.10, CTFontDrawGlyphs acted like CGContextShowGlyphsAtPositions and took |
| 1106 | // 'positions' which are in text space. The glyph location (in device space) must be |
| 1107 | // mapped into text space, so that CG can convert it back into device space. |
Hal Canary | 55325b7 | 2017-01-03 10:36:17 -0500 | [diff] [blame] | 1108 | // In 10.10.1, this is handled directly in CTFontDrawGlyphs. |
bungeman | 1b5f25c | 2015-06-08 14:32:24 -0700 | [diff] [blame] | 1109 | // |
bungeman | aae3091 | 2015-03-02 13:43:26 -0800 | [diff] [blame] | 1110 | // However, in 10.10.2 color glyphs no longer rotate based on the font transform. |
| 1111 | // So always make the font transform identity and place the transform on the context. |
| 1112 | point = CGPointApplyAffineTransform(point, context.fInvTransform); |
| 1113 | |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 1114 | CTFontDrawGlyphs(context.fCTFont.get(), &glyphID, &point, 1, fCG.get()); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1115 | |
| 1116 | SkASSERT(rowBytesPtr); |
| 1117 | *rowBytesPtr = rowBytes; |
| 1118 | return image; |
| 1119 | } |
| 1120 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1121 | unsigned SkScalerContext_Mac::generateGlyphCount(void) { |
| 1122 | return fGlyphCount; |
| 1123 | } |
| 1124 | |
| 1125 | uint16_t SkScalerContext_Mac::generateCharToGlyph(SkUnichar uni) { |
reed | d0f4173 | 2015-07-10 12:08:38 -0700 | [diff] [blame] | 1126 | AUTO_CG_LOCK(); |
| 1127 | |
bungeman@google.com | fb1663a | 2013-10-24 22:32:43 +0000 | [diff] [blame] | 1128 | CGGlyph cgGlyph[2]; |
bungeman@google.com | 72b8cb2 | 2013-10-25 17:49:08 +0000 | [diff] [blame] | 1129 | 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] | 1130 | |
| 1131 | // Get the glyph |
Hal Canary | f107a2f | 2018-07-25 16:52:48 -0400 | [diff] [blame] | 1132 | size_t numUniChar = SkUTF::ToUTF16(uni, theChar); |
bungeman@google.com | fb1663a | 2013-10-24 22:32:43 +0000 | [diff] [blame] | 1133 | SkASSERT(sizeof(CGGlyph) <= sizeof(uint16_t)); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1134 | |
bungeman@google.com | 72b8cb2 | 2013-10-25 17:49:08 +0000 | [diff] [blame] | 1135 | // Undocumented behavior of CTFontGetGlyphsForCharacters with non-bmp code points: |
| 1136 | // When a surrogate pair is detected, the glyph index used is the index of the high surrogate. |
| 1137 | // It is documented that if a mapping is unavailable, the glyph will be set to 0. |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 1138 | CTFontGetGlyphsForCharacters(fCTFont.get(), theChar, cgGlyph, numUniChar); |
bungeman@google.com | fb1663a | 2013-10-24 22:32:43 +0000 | [diff] [blame] | 1139 | return cgGlyph[0]; |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1140 | } |
| 1141 | |
Ben Wagner | e541645 | 2018-08-09 14:03:42 -0400 | [diff] [blame] | 1142 | bool SkScalerContext_Mac::generateAdvance(SkGlyph* glyph) { |
| 1143 | return false; |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 1146 | void SkScalerContext_Mac::generateMetrics(SkGlyph* glyph) { |
reed | d0f4173 | 2015-07-10 12:08:38 -0700 | [diff] [blame] | 1147 | AUTO_CG_LOCK(); |
| 1148 | |
Ben Wagner | e541645 | 2018-08-09 14:03:42 -0400 | [diff] [blame] | 1149 | glyph->fMaskFormat = fRec.fMaskFormat; |
| 1150 | |
djsollen | 1b27704 | 2014-08-06 06:58:06 -0700 | [diff] [blame] | 1151 | const CGGlyph cgGlyph = (CGGlyph) glyph->getGlyphID(); |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 1152 | glyph->zeroMetrics(); |
skia.committer@gmail.com | 539f364 | 2013-05-16 07:01:00 +0000 | [diff] [blame] | 1153 | |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 1154 | // The following block produces cgAdvance in CG units (pixels, y up). |
| 1155 | CGSize cgAdvance; |
Mike Reed | c88cc77 | 2018-10-23 12:05:47 -0400 | [diff] [blame] | 1156 | CTFontGetAdvancesForGlyphs(fCTFont.get(), kCTFontOrientationHorizontal, |
| 1157 | &cgGlyph, &cgAdvance, 1); |
bungeman | ef27ce3 | 2015-10-29 09:30:32 -0700 | [diff] [blame] | 1158 | cgAdvance = CGSizeApplyAffineTransform(cgAdvance, fTransform); |
benjaminwagner | 6b3eacb | 2016-03-24 19:07:58 -0700 | [diff] [blame] | 1159 | glyph->fAdvanceX = CGToFloat(cgAdvance.width); |
| 1160 | glyph->fAdvanceY = -CGToFloat(cgAdvance.height); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1161 | |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 1162 | // The following produces skBounds in SkGlyph units (pixels, y down), |
| 1163 | // or returns early if skBounds would be empty. |
| 1164 | SkRect skBounds; |
skia.committer@gmail.com | 539f364 | 2013-05-16 07:01:00 +0000 | [diff] [blame] | 1165 | |
bungeman | c2ba75b | 2016-12-14 16:53:00 -0500 | [diff] [blame] | 1166 | // Glyphs are always drawn from the horizontal origin. The caller must manually use the result |
| 1167 | // of CTFontGetVerticalTranslationsForGlyphs to calculate where to draw the glyph for vertical |
| 1168 | // glyphs. As a result, always get the horizontal bounds of a glyph and translate it if the |
| 1169 | // glyph is vertical. This avoids any diagreement between the various means of retrieving |
| 1170 | // vertical metrics. |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 1171 | { |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 1172 | // CTFontGetBoundingRectsForGlyphs produces cgBounds in CG units (pixels, y up). |
| 1173 | CGRect cgBounds; |
bungeman | 18ec7b9 | 2017-02-09 17:15:59 -0500 | [diff] [blame] | 1174 | CTFontGetBoundingRectsForGlyphs(fCTFont.get(), kCTFontOrientationHorizontal, |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 1175 | &cgGlyph, &cgBounds, 1); |
bungeman | ef27ce3 | 2015-10-29 09:30:32 -0700 | [diff] [blame] | 1176 | cgBounds = CGRectApplyAffineTransform(cgBounds, fTransform); |
skia.committer@gmail.com | 539f364 | 2013-05-16 07:01:00 +0000 | [diff] [blame] | 1177 | |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 1178 | // BUG? |
| 1179 | // 0x200B (zero-advance space) seems to return a huge (garbage) bounds, when |
| 1180 | // it should be empty. So, if we see a zero-advance, we check if it has an |
| 1181 | // empty path or not, and if so, we jam the bounds to 0. Hopefully a zero-advance |
| 1182 | // is rare, so we won't incur a big performance cost for this extra check. |
| 1183 | if (0 == cgAdvance.width && 0 == cgAdvance.height) { |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 1184 | SkUniqueCFRef<CGPathRef> path(CTFontCreatePathForGlyph(fCTFont.get(), cgGlyph,nullptr)); |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 1185 | if (!path || CGPathIsEmpty(path.get())) { |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 1186 | return; |
| 1187 | } |
| 1188 | } |
| 1189 | |
| 1190 | if (CGRectIsEmpty_inline(cgBounds)) { |
| 1191 | return; |
| 1192 | } |
| 1193 | |
| 1194 | // Convert cgBounds to SkGlyph units (pixels, y down). |
| 1195 | skBounds = SkRect::MakeXYWH(cgBounds.origin.x, -cgBounds.origin.y - cgBounds.size.height, |
| 1196 | cgBounds.size.width, cgBounds.size.height); |
| 1197 | } |
| 1198 | |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 1199 | // Currently the bounds are based on being rendered at (0,0). |
| 1200 | // The top left must not move, since that is the base from which subpixel positioning is offset. |
| 1201 | if (fDoSubPosition) { |
| 1202 | skBounds.fRight += SkFixedToFloat(glyph->getSubXFixed()); |
| 1203 | skBounds.fBottom += SkFixedToFloat(glyph->getSubYFixed()); |
| 1204 | } |
skia.committer@gmail.com | 539f364 | 2013-05-16 07:01:00 +0000 | [diff] [blame] | 1205 | |
Mike Klein | d94e00c | 2017-10-05 11:40:57 -0400 | [diff] [blame] | 1206 | // We're trying to pack left and top into int16_t, |
| 1207 | // and width and height into uint16_t, after outsetting by 1. |
| 1208 | if (!SkRect::MakeXYWH(-32767, -32767, 65535, 65535).contains(skBounds)) { |
| 1209 | return; |
| 1210 | } |
| 1211 | |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 1212 | SkIRect skIBounds; |
| 1213 | skBounds.roundOut(&skIBounds); |
| 1214 | // Expand the bounds by 1 pixel, to give CG room for anti-aliasing. |
| 1215 | // Note that this outset is to allow room for LCD smoothed glyphs. However, the correct outset |
| 1216 | // is not currently known, as CG dilates the outlines by some percentage. |
| 1217 | // Note that if this context is A8 and not back-forming from LCD, there is no need to outset. |
| 1218 | skIBounds.outset(1, 1); |
| 1219 | glyph->fLeft = SkToS16(skIBounds.fLeft); |
| 1220 | glyph->fTop = SkToS16(skIBounds.fTop); |
| 1221 | glyph->fWidth = SkToU16(skIBounds.width()); |
| 1222 | glyph->fHeight = SkToU16(skIBounds.height()); |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 1223 | } |
| 1224 | |
Cary Clark | a4083c9 | 2017-09-15 11:59:23 -0400 | [diff] [blame] | 1225 | #include "SkColorData.h" |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1226 | |
Ben Wagner | 7a0248f | 2017-10-18 11:30:56 -0400 | [diff] [blame] | 1227 | static constexpr uint8_t sk_pow2_table(size_t i) { |
| 1228 | return SkToU8(((i * i + 128) / 255)); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1229 | } |
| 1230 | |
| 1231 | /** |
| 1232 | * This will invert the gamma applied by CoreGraphics, so we can get linear |
| 1233 | * values. |
| 1234 | * |
Ben Wagner | 6112f0b | 2018-09-27 15:44:56 -0400 | [diff] [blame] | 1235 | * CoreGraphics obscurely defaults to 2.0 as the subpixel coverage gamma value. |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1236 | * The color space used does not appear to affect this choice. |
| 1237 | */ |
Ben Wagner | 7a0248f | 2017-10-18 11:30:56 -0400 | [diff] [blame] | 1238 | static constexpr auto gLinearCoverageFromCGLCDValue = SkMakeArray<256>(sk_pow2_table); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1239 | |
| 1240 | static void cgpixels_to_bits(uint8_t dst[], const CGRGBPixel src[], int count) { |
| 1241 | while (count > 0) { |
| 1242 | uint8_t mask = 0; |
| 1243 | for (int i = 7; i >= 0; --i) { |
bungeman | e280d06 | 2016-03-24 11:27:05 -0700 | [diff] [blame] | 1244 | mask |= ((CGRGBPixel_getAlpha(*src++) >> 7) ^ 0x1) << i; |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1245 | if (0 == --count) { |
| 1246 | break; |
| 1247 | } |
| 1248 | } |
| 1249 | *dst++ = mask; |
| 1250 | } |
| 1251 | } |
| 1252 | |
| 1253 | template<bool APPLY_PREBLEND> |
| 1254 | static inline uint8_t rgb_to_a8(CGRGBPixel rgb, const uint8_t* table8) { |
bungeman | e280d06 | 2016-03-24 11:27:05 -0700 | [diff] [blame] | 1255 | U8CPU r = 0xFF - ((rgb >> 16) & 0xFF); |
| 1256 | U8CPU g = 0xFF - ((rgb >> 8) & 0xFF); |
| 1257 | U8CPU b = 0xFF - ((rgb >> 0) & 0xFF); |
bungeman | 3b4b66c | 2015-01-08 08:33:44 -0800 | [diff] [blame] | 1258 | U8CPU lum = sk_apply_lut_if<APPLY_PREBLEND>(SkComputeLuminance(r, g, b), table8); |
| 1259 | #if SK_SHOW_TEXT_BLIT_COVERAGE |
| 1260 | lum = SkTMax(lum, (U8CPU)0x30); |
| 1261 | #endif |
| 1262 | return lum; |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1263 | } |
| 1264 | template<bool APPLY_PREBLEND> |
| 1265 | static void rgb_to_a8(const CGRGBPixel* SK_RESTRICT cgPixels, size_t cgRowBytes, |
| 1266 | const SkGlyph& glyph, const uint8_t* table8) { |
| 1267 | const int width = glyph.fWidth; |
| 1268 | size_t dstRB = glyph.rowBytes(); |
| 1269 | uint8_t* SK_RESTRICT dst = (uint8_t*)glyph.fImage; |
| 1270 | |
| 1271 | for (int y = 0; y < glyph.fHeight; y++) { |
| 1272 | for (int i = 0; i < width; ++i) { |
| 1273 | dst[i] = rgb_to_a8<APPLY_PREBLEND>(cgPixels[i], table8); |
| 1274 | } |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 1275 | cgPixels = SkTAddOffset<const CGRGBPixel>(cgPixels, cgRowBytes); |
| 1276 | dst = SkTAddOffset<uint8_t>(dst, dstRB); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1277 | } |
| 1278 | } |
| 1279 | |
| 1280 | template<bool APPLY_PREBLEND> |
| 1281 | static inline uint16_t rgb_to_lcd16(CGRGBPixel rgb, const uint8_t* tableR, |
| 1282 | const uint8_t* tableG, |
| 1283 | const uint8_t* tableB) { |
bungeman | e280d06 | 2016-03-24 11:27:05 -0700 | [diff] [blame] | 1284 | U8CPU r = sk_apply_lut_if<APPLY_PREBLEND>(0xFF - ((rgb >> 16) & 0xFF), tableR); |
| 1285 | U8CPU g = sk_apply_lut_if<APPLY_PREBLEND>(0xFF - ((rgb >> 8) & 0xFF), tableG); |
| 1286 | U8CPU b = sk_apply_lut_if<APPLY_PREBLEND>(0xFF - ((rgb >> 0) & 0xFF), tableB); |
bungeman | 3b4b66c | 2015-01-08 08:33:44 -0800 | [diff] [blame] | 1287 | #if SK_SHOW_TEXT_BLIT_COVERAGE |
| 1288 | r = SkTMax(r, (U8CPU)0x30); |
| 1289 | g = SkTMax(g, (U8CPU)0x30); |
| 1290 | b = SkTMax(b, (U8CPU)0x30); |
| 1291 | #endif |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1292 | return SkPack888ToRGB16(r, g, b); |
| 1293 | } |
| 1294 | template<bool APPLY_PREBLEND> |
| 1295 | static void rgb_to_lcd16(const CGRGBPixel* SK_RESTRICT cgPixels, size_t cgRowBytes, const SkGlyph& glyph, |
| 1296 | const uint8_t* tableR, const uint8_t* tableG, const uint8_t* tableB) { |
| 1297 | const int width = glyph.fWidth; |
| 1298 | size_t dstRB = glyph.rowBytes(); |
| 1299 | uint16_t* SK_RESTRICT dst = (uint16_t*)glyph.fImage; |
| 1300 | |
| 1301 | for (int y = 0; y < glyph.fHeight; y++) { |
| 1302 | for (int i = 0; i < width; i++) { |
| 1303 | dst[i] = rgb_to_lcd16<APPLY_PREBLEND>(cgPixels[i], tableR, tableG, tableB); |
| 1304 | } |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 1305 | cgPixels = SkTAddOffset<const CGRGBPixel>(cgPixels, cgRowBytes); |
| 1306 | dst = SkTAddOffset<uint16_t>(dst, dstRB); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1307 | } |
| 1308 | } |
| 1309 | |
bungeman | 3490263 | 2014-12-10 21:43:27 -0800 | [diff] [blame] | 1310 | static SkPMColor cgpixels_to_pmcolor(CGRGBPixel rgb) { |
| 1311 | U8CPU a = (rgb >> 24) & 0xFF; |
reed@google.com | f77b35d | 2013-05-02 20:39:44 +0000 | [diff] [blame] | 1312 | U8CPU r = (rgb >> 16) & 0xFF; |
| 1313 | U8CPU g = (rgb >> 8) & 0xFF; |
| 1314 | U8CPU b = (rgb >> 0) & 0xFF; |
bungeman | 3b4b66c | 2015-01-08 08:33:44 -0800 | [diff] [blame] | 1315 | #if SK_SHOW_TEXT_BLIT_COVERAGE |
| 1316 | a = SkTMax(a, (U8CPU)0x30); |
| 1317 | #endif |
bungeman | 3490263 | 2014-12-10 21:43:27 -0800 | [diff] [blame] | 1318 | return SkPackARGB32(a, r, g, b); |
reed@google.com | f77b35d | 2013-05-02 20:39:44 +0000 | [diff] [blame] | 1319 | } |
reed@google.com | f77b35d | 2013-05-02 20:39:44 +0000 | [diff] [blame] | 1320 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1321 | void SkScalerContext_Mac::generateImage(const SkGlyph& glyph) { |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 1322 | CGGlyph cgGlyph = SkTo<CGGlyph>(glyph.getGlyphID()); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1323 | |
| 1324 | // FIXME: lcd smoothed un-hinted rasterization unsupported. |
Mike Reed | 04346d5 | 2018-11-05 12:45:32 -0500 | [diff] [blame] | 1325 | bool requestSmooth = fRec.getHinting() != kNo_SkFontHinting; |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1326 | |
| 1327 | // Draw the glyph |
| 1328 | size_t cgRowBytes; |
Ben Wagner | 6112f0b | 2018-09-27 15:44:56 -0400 | [diff] [blame] | 1329 | CGRGBPixel* cgPixels = fOffscreen.getCG(*this, glyph, cgGlyph, &cgRowBytes, requestSmooth); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1330 | if (cgPixels == nullptr) { |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1331 | return; |
| 1332 | } |
| 1333 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1334 | // Fix the glyph |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 1335 | if ((glyph.fMaskFormat == SkMask::kLCD16_Format) || |
Ben Wagner | 6112f0b | 2018-09-27 15:44:56 -0400 | [diff] [blame] | 1336 | (glyph.fMaskFormat == SkMask::kA8_Format |
| 1337 | && requestSmooth |
Ben Wagner | 16d00ee | 2018-12-14 15:43:07 -0500 | [diff] [blame] | 1338 | && smooth_behavior() != SmoothBehavior::none)) |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 1339 | { |
Ben Wagner | 7a0248f | 2017-10-18 11:30:56 -0400 | [diff] [blame] | 1340 | const uint8_t* linear = gLinearCoverageFromCGLCDValue.data(); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1341 | |
| 1342 | //Note that the following cannot really be integrated into the |
| 1343 | //pre-blend, since we may not be applying the pre-blend; when we aren't |
| 1344 | //applying the pre-blend it means that a filter wants linear anyway. |
| 1345 | //Other code may also be applying the pre-blend, so we'd need another |
| 1346 | //one with this and one without. |
| 1347 | CGRGBPixel* addr = cgPixels; |
| 1348 | for (int y = 0; y < glyph.fHeight; ++y) { |
| 1349 | for (int x = 0; x < glyph.fWidth; ++x) { |
| 1350 | int r = (addr[x] >> 16) & 0xFF; |
| 1351 | int g = (addr[x] >> 8) & 0xFF; |
| 1352 | int b = (addr[x] >> 0) & 0xFF; |
Ben Wagner | 7a0248f | 2017-10-18 11:30:56 -0400 | [diff] [blame] | 1353 | addr[x] = (linear[r] << 16) | (linear[g] << 8) | linear[b]; |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1354 | } |
bungeman | e280d06 | 2016-03-24 11:27:05 -0700 | [diff] [blame] | 1355 | addr = SkTAddOffset<CGRGBPixel>(addr, cgRowBytes); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1356 | } |
| 1357 | } |
| 1358 | |
| 1359 | // Convert glyph to mask |
| 1360 | switch (glyph.fMaskFormat) { |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1361 | case SkMask::kLCD16_Format: { |
| 1362 | if (fPreBlend.isApplicable()) { |
| 1363 | rgb_to_lcd16<true>(cgPixels, cgRowBytes, glyph, |
| 1364 | fPreBlend.fR, fPreBlend.fG, fPreBlend.fB); |
| 1365 | } else { |
| 1366 | rgb_to_lcd16<false>(cgPixels, cgRowBytes, glyph, |
| 1367 | fPreBlend.fR, fPreBlend.fG, fPreBlend.fB); |
| 1368 | } |
| 1369 | } break; |
| 1370 | case SkMask::kA8_Format: { |
| 1371 | if (fPreBlend.isApplicable()) { |
| 1372 | rgb_to_a8<true>(cgPixels, cgRowBytes, glyph, fPreBlend.fG); |
| 1373 | } else { |
| 1374 | rgb_to_a8<false>(cgPixels, cgRowBytes, glyph, fPreBlend.fG); |
| 1375 | } |
| 1376 | } break; |
| 1377 | case SkMask::kBW_Format: { |
| 1378 | const int width = glyph.fWidth; |
| 1379 | size_t dstRB = glyph.rowBytes(); |
| 1380 | uint8_t* dst = (uint8_t*)glyph.fImage; |
| 1381 | for (int y = 0; y < glyph.fHeight; y++) { |
| 1382 | cgpixels_to_bits(dst, cgPixels, width); |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 1383 | cgPixels = SkTAddOffset<CGRGBPixel>(cgPixels, cgRowBytes); |
| 1384 | dst = SkTAddOffset<uint8_t>(dst, dstRB); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1385 | } |
| 1386 | } break; |
reed@google.com | f77b35d | 2013-05-02 20:39:44 +0000 | [diff] [blame] | 1387 | case SkMask::kARGB32_Format: { |
| 1388 | const int width = glyph.fWidth; |
| 1389 | size_t dstRB = glyph.rowBytes(); |
| 1390 | SkPMColor* dst = (SkPMColor*)glyph.fImage; |
| 1391 | for (int y = 0; y < glyph.fHeight; y++) { |
| 1392 | for (int x = 0; x < width; ++x) { |
bungeman | 3490263 | 2014-12-10 21:43:27 -0800 | [diff] [blame] | 1393 | dst[x] = cgpixels_to_pmcolor(cgPixels[x]); |
reed@google.com | f77b35d | 2013-05-02 20:39:44 +0000 | [diff] [blame] | 1394 | } |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 1395 | cgPixels = SkTAddOffset<CGRGBPixel>(cgPixels, cgRowBytes); |
| 1396 | dst = SkTAddOffset<SkPMColor>(dst, dstRB); |
reed@google.com | f77b35d | 2013-05-02 20:39:44 +0000 | [diff] [blame] | 1397 | } |
| 1398 | } break; |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1399 | default: |
| 1400 | SkDEBUGFAIL("unexpected mask format"); |
| 1401 | break; |
| 1402 | } |
| 1403 | } |
| 1404 | |
| 1405 | /* |
| 1406 | * Our subpixel resolution is only 2 bits in each direction, so a scale of 4 |
| 1407 | * seems sufficient, and possibly even correct, to allow the hinted outline |
| 1408 | * to be subpixel positioned. |
| 1409 | */ |
| 1410 | #define kScaleForSubPixelPositionHinting (4.0f) |
| 1411 | |
Ben Wagner | 5ddb308 | 2018-03-29 11:18:06 -0400 | [diff] [blame] | 1412 | bool SkScalerContext_Mac::generatePath(SkGlyphID glyph, SkPath* path) { |
reed | d0f4173 | 2015-07-10 12:08:38 -0700 | [diff] [blame] | 1413 | AUTO_CG_LOCK(); |
| 1414 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1415 | SkScalar scaleX = SK_Scalar1; |
| 1416 | SkScalar scaleY = SK_Scalar1; |
| 1417 | |
bungeman | ef27ce3 | 2015-10-29 09:30:32 -0700 | [diff] [blame] | 1418 | CGAffineTransform xform = fTransform; |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1419 | /* |
| 1420 | * For subpixel positioning, we want to return an unhinted outline, so it |
| 1421 | * can be positioned nicely at fractional offsets. However, we special-case |
| 1422 | * if the baseline of the (horizontal) text is axis-aligned. In those cases |
| 1423 | * we want to retain hinting in the direction orthogonal to the baseline. |
| 1424 | * e.g. for horizontal baseline, we want to retain hinting in Y. |
| 1425 | * The way we remove hinting is to scale the font by some value (4) in that |
| 1426 | * direction, ask for the path, and then scale the path back down. |
| 1427 | */ |
bungeman | 7cbeaae | 2015-09-22 09:54:56 -0700 | [diff] [blame] | 1428 | if (fDoSubPosition) { |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1429 | // 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] | 1430 | scaleX = scaleY = kScaleForSubPixelPositionHinting; |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1431 | // now see if we need to restore hinting for axis-aligned baselines |
bungeman | 27876bc | 2016-02-29 11:22:55 -0800 | [diff] [blame] | 1432 | switch (this->computeAxisAlignmentForHText()) { |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1433 | case kX_SkAxisAlignment: |
| 1434 | scaleY = SK_Scalar1; // want hinting in the Y direction |
| 1435 | break; |
| 1436 | case kY_SkAxisAlignment: |
| 1437 | scaleX = SK_Scalar1; // want hinting in the X direction |
| 1438 | break; |
| 1439 | default: |
| 1440 | break; |
| 1441 | } |
| 1442 | |
bungeman | ef27ce3 | 2015-10-29 09:30:32 -0700 | [diff] [blame] | 1443 | CGAffineTransform scale(CGAffineTransformMakeScale(ScalarToCG(scaleX), ScalarToCG(scaleY))); |
| 1444 | xform = CGAffineTransformConcat(fTransform, scale); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
Ben Wagner | 6e9ac12 | 2016-11-11 14:31:06 -0500 | [diff] [blame] | 1447 | CGGlyph cgGlyph = SkTo<CGGlyph>(glyph); |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 1448 | SkUniqueCFRef<CGPathRef> cgPath(CTFontCreatePathForGlyph(fCTFont.get(), cgGlyph, &xform)); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1449 | |
| 1450 | path->reset(); |
Ben Wagner | 5ddb308 | 2018-03-29 11:18:06 -0400 | [diff] [blame] | 1451 | if (!cgPath) { |
| 1452 | return false; |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1453 | } |
| 1454 | |
Ben Wagner | 5ddb308 | 2018-03-29 11:18:06 -0400 | [diff] [blame] | 1455 | CGPathApply(cgPath.get(), path, SkScalerContext_Mac::CTPathElement); |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 1456 | if (fDoSubPosition) { |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1457 | SkMatrix m; |
| 1458 | m.setScale(SkScalarInvert(scaleX), SkScalarInvert(scaleY)); |
| 1459 | path->transform(m); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1460 | } |
Ben Wagner | 5ddb308 | 2018-03-29 11:18:06 -0400 | [diff] [blame] | 1461 | return true; |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
Mike Reed | b5784ac | 2018-11-12 09:35:15 -0500 | [diff] [blame] | 1464 | void SkScalerContext_Mac::generateFontMetrics(SkFontMetrics* metrics) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1465 | if (nullptr == metrics) { |
bungeman | 4107806 | 2014-07-07 08:16:37 -0700 | [diff] [blame] | 1466 | return; |
| 1467 | } |
| 1468 | |
reed | d0f4173 | 2015-07-10 12:08:38 -0700 | [diff] [blame] | 1469 | AUTO_CG_LOCK(); |
| 1470 | |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 1471 | CGRect theBounds = CTFontGetBoundingBox(fCTFont.get()); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1472 | |
bungeman | 4107806 | 2014-07-07 08:16:37 -0700 | [diff] [blame] | 1473 | metrics->fTop = CGToScalar(-CGRectGetMaxY_inline(theBounds)); |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 1474 | metrics->fAscent = CGToScalar(-CTFontGetAscent(fCTFont.get())); |
| 1475 | metrics->fDescent = CGToScalar( CTFontGetDescent(fCTFont.get())); |
bungeman | 4107806 | 2014-07-07 08:16:37 -0700 | [diff] [blame] | 1476 | metrics->fBottom = CGToScalar(-CGRectGetMinY_inline(theBounds)); |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 1477 | metrics->fLeading = CGToScalar( CTFontGetLeading(fCTFont.get())); |
bungeman | 4107806 | 2014-07-07 08:16:37 -0700 | [diff] [blame] | 1478 | metrics->fAvgCharWidth = CGToScalar( CGRectGetWidth_inline(theBounds)); |
| 1479 | metrics->fXMin = CGToScalar( CGRectGetMinX_inline(theBounds)); |
| 1480 | metrics->fXMax = CGToScalar( CGRectGetMaxX_inline(theBounds)); |
bungeman | 6bd8d1c | 2015-06-09 08:40:51 -0700 | [diff] [blame] | 1481 | metrics->fMaxCharWidth = metrics->fXMax - metrics->fXMin; |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 1482 | metrics->fXHeight = CGToScalar( CTFontGetXHeight(fCTFont.get())); |
| 1483 | metrics->fCapHeight = CGToScalar( CTFontGetCapHeight(fCTFont.get())); |
| 1484 | metrics->fUnderlineThickness = CGToScalar( CTFontGetUnderlineThickness(fCTFont.get())); |
| 1485 | metrics->fUnderlinePosition = -CGToScalar( CTFontGetUnderlinePosition(fCTFont.get())); |
commit-bot@chromium.org | 0bc406d | 2014-03-01 20:12:26 +0000 | [diff] [blame] | 1486 | |
Ben Wagner | 219f362 | 2017-07-17 15:32:25 -0400 | [diff] [blame] | 1487 | metrics->fFlags = 0; |
Mike Reed | b5784ac | 2018-11-12 09:35:15 -0500 | [diff] [blame] | 1488 | metrics->fFlags |= SkFontMetrics::kUnderlineThicknessIsValid_Flag; |
| 1489 | metrics->fFlags |= SkFontMetrics::kUnderlinePositionIsValid_Flag; |
bungeman | 4ec46aa | 2017-02-15 17:49:12 -0500 | [diff] [blame] | 1490 | |
| 1491 | // See https://bugs.chromium.org/p/skia/issues/detail?id=6203 |
| 1492 | // At least on 10.12.3 with memory based fonts the x-height is always 0.6666 of the ascent and |
| 1493 | // the cap-height is always 0.8888 of the ascent. It appears that the values from the 'OS/2' |
| 1494 | // table are read, but then overwritten if the font is not a system font. As a result, if there |
| 1495 | // is a valid 'OS/2' table available use the values from the table if they aren't too strange. |
| 1496 | struct OS2HeightMetrics { |
| 1497 | SK_OT_SHORT sxHeight; |
| 1498 | SK_OT_SHORT sCapHeight; |
| 1499 | } heights; |
| 1500 | size_t bytesRead = this->getTypeface()->getTableData( |
| 1501 | SkTEndian_SwapBE32(SkOTTableOS2::TAG), offsetof(SkOTTableOS2, version.v2.sxHeight), |
| 1502 | sizeof(heights), &heights); |
| 1503 | if (bytesRead == sizeof(heights)) { |
| 1504 | // 'fontSize' is correct because the entire resolved size is set by the constructor. |
| 1505 | CGFloat fontSize = CTFontGetSize(this->fCTFont.get()); |
| 1506 | unsigned upem = CTFontGetUnitsPerEm(this->fCTFont.get()); |
| 1507 | unsigned maxSaneHeight = upem * 2; |
| 1508 | uint16_t xHeight = SkEndian_SwapBE16(heights.sxHeight); |
| 1509 | if (xHeight && xHeight < maxSaneHeight) { |
| 1510 | metrics->fXHeight = CGToScalar(xHeight * fontSize / upem); |
| 1511 | } |
| 1512 | uint16_t capHeight = SkEndian_SwapBE16(heights.sCapHeight); |
| 1513 | if (capHeight && capHeight < maxSaneHeight) { |
| 1514 | metrics->fCapHeight = CGToScalar(capHeight * fontSize / upem); |
| 1515 | } |
| 1516 | } |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1517 | } |
| 1518 | |
| 1519 | void SkScalerContext_Mac::CTPathElement(void *info, const CGPathElement *element) { |
| 1520 | SkPath* skPath = (SkPath*)info; |
| 1521 | |
| 1522 | // Process the path element |
| 1523 | switch (element->type) { |
| 1524 | case kCGPathElementMoveToPoint: |
| 1525 | skPath->moveTo(element->points[0].x, -element->points[0].y); |
| 1526 | break; |
| 1527 | |
| 1528 | case kCGPathElementAddLineToPoint: |
| 1529 | skPath->lineTo(element->points[0].x, -element->points[0].y); |
| 1530 | break; |
| 1531 | |
| 1532 | case kCGPathElementAddQuadCurveToPoint: |
| 1533 | skPath->quadTo(element->points[0].x, -element->points[0].y, |
| 1534 | element->points[1].x, -element->points[1].y); |
| 1535 | break; |
| 1536 | |
| 1537 | case kCGPathElementAddCurveToPoint: |
| 1538 | skPath->cubicTo(element->points[0].x, -element->points[0].y, |
| 1539 | element->points[1].x, -element->points[1].y, |
| 1540 | element->points[2].x, -element->points[2].y); |
| 1541 | break; |
| 1542 | |
| 1543 | case kCGPathElementCloseSubpath: |
| 1544 | skPath->close(); |
| 1545 | break; |
| 1546 | |
| 1547 | default: |
| 1548 | SkDEBUGFAIL("Unknown path element!"); |
| 1549 | break; |
| 1550 | } |
| 1551 | } |
| 1552 | |
| 1553 | |
| 1554 | /////////////////////////////////////////////////////////////////////////////// |
| 1555 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1556 | // Returns nullptr on failure |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1557 | // Call must still manage its ownership of provider |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 1558 | static sk_sp<SkTypeface> create_from_dataProvider(SkUniqueCFRef<CGDataProviderRef> provider, |
Mike Reed | 5922739 | 2017-09-26 09:46:08 -0400 | [diff] [blame] | 1559 | int ttcIndex) { |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 1560 | if (ttcIndex != 0) { |
| 1561 | return nullptr; |
| 1562 | } |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 1563 | SkUniqueCFRef<CGFontRef> cg(CGFontCreateWithDataProvider(provider.get())); |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 1564 | if (!cg) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1565 | return nullptr; |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1566 | } |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 1567 | SkUniqueCFRef<CTFontRef> ct(CTFontCreateWithGraphicsFont(cg.get(), 0, nullptr, nullptr)); |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 1568 | if (!ct) { |
| 1569 | return nullptr; |
| 1570 | } |
| 1571 | return create_from_CTFontRef(std::move(ct), nullptr, true); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1572 | } |
| 1573 | |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 1574 | // Web fonts added to the CTFont registry do not return their character set. |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1575 | // Iterate through the font in this case. The existing caller caches the result, |
| 1576 | // so the performance impact isn't too bad. |
| 1577 | static void populate_glyph_to_unicode_slow(CTFontRef ctFont, CFIndex glyphCount, |
Hal Canary | 46cc3da | 2018-05-09 11:50:34 -0400 | [diff] [blame] | 1578 | SkUnichar* out) { |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1579 | sk_bzero(out, glyphCount * sizeof(SkUnichar)); |
| 1580 | UniChar unichar = 0; |
| 1581 | while (glyphCount > 0) { |
| 1582 | CGGlyph glyph; |
| 1583 | if (CTFontGetGlyphsForCharacters(ctFont, &unichar, &glyph, 1)) { |
Hal Canary | 266dcb0 | 2017-04-07 11:49:20 -0400 | [diff] [blame] | 1584 | if (out[glyph] != 0) { |
| 1585 | out[glyph] = unichar; |
| 1586 | --glyphCount; |
| 1587 | } |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1588 | } |
| 1589 | if (++unichar == 0) { |
| 1590 | break; |
| 1591 | } |
| 1592 | } |
| 1593 | } |
| 1594 | |
| 1595 | // Construct Glyph to Unicode table. |
| 1596 | // Unicode code points that require conjugate pairs in utf16 are not |
| 1597 | // supported. |
| 1598 | static void populate_glyph_to_unicode(CTFontRef ctFont, CFIndex glyphCount, |
Hal Canary | 46cc3da | 2018-05-09 11:50:34 -0400 | [diff] [blame] | 1599 | SkUnichar* glyphToUnicode) { |
| 1600 | sk_bzero(glyphToUnicode, sizeof(SkUnichar) * glyphCount); |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 1601 | SkUniqueCFRef<CFCharacterSetRef> charSet(CTFontCopyCharacterSet(ctFont)); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1602 | if (!charSet) { |
| 1603 | populate_glyph_to_unicode_slow(ctFont, glyphCount, glyphToUnicode); |
| 1604 | return; |
| 1605 | } |
| 1606 | |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 1607 | SkUniqueCFRef<CFDataRef> bitmap( |
| 1608 | CFCharacterSetCreateBitmapRepresentation(nullptr, charSet.get())); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1609 | if (!bitmap) { |
| 1610 | return; |
| 1611 | } |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 1612 | CFIndex length = CFDataGetLength(bitmap.get()); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1613 | if (!length) { |
| 1614 | return; |
| 1615 | } |
| 1616 | if (length > 8192) { |
| 1617 | // TODO: Add support for Unicode above 0xFFFF |
| 1618 | // Consider only the BMP portion of the Unicode character points. |
| 1619 | // The bitmap may contain other planes, up to plane 16. |
| 1620 | // See http://developer.apple.com/library/ios/#documentation/CoreFoundation/Reference/CFCharacterSetRef/Reference/reference.html |
| 1621 | length = 8192; |
| 1622 | } |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 1623 | const UInt8* bits = CFDataGetBytePtr(bitmap.get()); |
Hal Canary | 46cc3da | 2018-05-09 11:50:34 -0400 | [diff] [blame] | 1624 | sk_bzero(glyphToUnicode, glyphCount * sizeof(SkUnichar)); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1625 | for (int i = 0; i < length; i++) { |
| 1626 | int mask = bits[i]; |
| 1627 | if (!mask) { |
| 1628 | continue; |
| 1629 | } |
| 1630 | for (int j = 0; j < 8; j++) { |
| 1631 | CGGlyph glyph; |
| 1632 | UniChar unichar = static_cast<UniChar>((i << 3) + j); |
| 1633 | if (mask & (1 << j) && CTFontGetGlyphsForCharacters(ctFont, &unichar, &glyph, 1)) { |
Hal Canary | 46cc3da | 2018-05-09 11:50:34 -0400 | [diff] [blame] | 1634 | glyphToUnicode[glyph] = unichar; |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1635 | } |
| 1636 | } |
| 1637 | } |
| 1638 | } |
| 1639 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1640 | /** Assumes src and dst are not nullptr. */ |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1641 | static void CFStringToSkString(CFStringRef src, SkString* dst) { |
| 1642 | // Reserve enough room for the worst-case string, |
| 1643 | // plus 1 byte for the trailing null. |
| 1644 | CFIndex length = CFStringGetMaximumSizeForEncoding(CFStringGetLength(src), |
| 1645 | kCFStringEncodingUTF8) + 1; |
| 1646 | dst->resize(length); |
| 1647 | CFStringGetCString(src, dst->writable_str(), length, kCFStringEncodingUTF8); |
| 1648 | // Resize to the actual UTF-8 length used, stripping the null character. |
| 1649 | dst->resize(strlen(dst->c_str())); |
| 1650 | } |
| 1651 | |
Hal Canary | 46cc3da | 2018-05-09 11:50:34 -0400 | [diff] [blame] | 1652 | void SkTypeface_Mac::getGlyphToUnicodeMap(SkUnichar* dstArray) const { |
| 1653 | AUTO_CG_LOCK(); |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 1654 | SkUniqueCFRef<CTFontRef> ctFont = |
Hal Canary | 46cc3da | 2018-05-09 11:50:34 -0400 | [diff] [blame] | 1655 | ctfont_create_exact_copy(fFontRef.get(), CTFontGetUnitsPerEm(fFontRef.get()), nullptr); |
| 1656 | CFIndex glyphCount = CTFontGetGlyphCount(ctFont.get()); |
| 1657 | populate_glyph_to_unicode(ctFont.get(), glyphCount, dstArray); |
| 1658 | } |
| 1659 | |
Hal Canary | 209e4b1 | 2017-05-04 14:23:55 -0400 | [diff] [blame] | 1660 | std::unique_ptr<SkAdvancedTypefaceMetrics> SkTypeface_Mac::onGetAdvancedMetrics() const { |
reed@google.com | 2689f61 | 2013-03-20 20:01:47 +0000 | [diff] [blame] | 1661 | |
reed | d0f4173 | 2015-07-10 12:08:38 -0700 | [diff] [blame] | 1662 | AUTO_CG_LOCK(); |
| 1663 | |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 1664 | SkUniqueCFRef<CTFontRef> ctFont = |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 1665 | ctfont_create_exact_copy(fFontRef.get(), CTFontGetUnitsPerEm(fFontRef.get()), nullptr); |
bungeman | 7cbeaae | 2015-09-22 09:54:56 -0700 | [diff] [blame] | 1666 | |
Hal Canary | 209e4b1 | 2017-05-04 14:23:55 -0400 | [diff] [blame] | 1667 | std::unique_ptr<SkAdvancedTypefaceMetrics> info(new SkAdvancedTypefaceMetrics); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1668 | |
| 1669 | { |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 1670 | SkUniqueCFRef<CFStringRef> fontName(CTFontCopyPostScriptName(ctFont.get())); |
bungeman | 256b351 | 2014-07-02 07:57:59 -0700 | [diff] [blame] | 1671 | if (fontName.get()) { |
Hal Canary | 8031b32 | 2018-03-29 13:20:30 -0700 | [diff] [blame] | 1672 | CFStringToSkString(fontName.get(), &info->fPostScriptName); |
| 1673 | info->fFontName = info->fPostScriptName; |
bungeman | 256b351 | 2014-07-02 07:57:59 -0700 | [diff] [blame] | 1674 | } |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1675 | } |
| 1676 | |
Ben Wagner | c663905 | 2017-03-02 13:31:16 -0500 | [diff] [blame] | 1677 | // In 10.10 and earlier, CTFontCopyVariationAxes and CTFontCopyVariation do not work when |
| 1678 | // applied to fonts which started life with CGFontCreateWithDataProvider (they simply always |
| 1679 | // return nullptr). As a result, we are limited to CGFontCopyVariationAxes and |
| 1680 | // CGFontCopyVariations here until support for 10.10 and earlier is removed. |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 1681 | SkUniqueCFRef<CGFontRef> cgFont(CTFontCopyGraphicsFont(ctFont.get(), nullptr)); |
Ben Wagner | c663905 | 2017-03-02 13:31:16 -0500 | [diff] [blame] | 1682 | if (cgFont) { |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 1683 | SkUniqueCFRef<CFArrayRef> cgAxes(CGFontCopyVariationAxes(cgFont.get())); |
Ben Wagner | c663905 | 2017-03-02 13:31:16 -0500 | [diff] [blame] | 1684 | if (cgAxes && CFArrayGetCount(cgAxes.get()) > 0) { |
| 1685 | info->fFlags |= SkAdvancedTypefaceMetrics::kMultiMaster_FontFlag; |
| 1686 | } |
| 1687 | } |
| 1688 | |
Hal Canary | 4a851ca | 2017-11-09 11:09:34 -0500 | [diff] [blame] | 1689 | SkOTTableOS2_V4::Type fsType; |
| 1690 | if (sizeof(fsType) == this->getTableData(SkTEndian_SwapBE32(SkOTTableOS2::TAG), |
| 1691 | offsetof(SkOTTableOS2_V4, fsType), |
| 1692 | sizeof(fsType), |
| 1693 | &fsType)) { |
Hal Canary | a865d25 | 2017-11-09 16:16:09 -0500 | [diff] [blame] | 1694 | SkOTUtils::SetAdvancedTypefaceFlags(fsType, info.get()); |
Hal Canary | 4a851ca | 2017-11-09 11:09:34 -0500 | [diff] [blame] | 1695 | } |
| 1696 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1697 | // If it's not a truetype font, mark it as 'other'. Assume that TrueType |
| 1698 | // fonts always have both glyf and loca tables. At the least, this is what |
| 1699 | // sfntly needs to subset the font. CTFontCopyAttribute() does not always |
| 1700 | // succeed in determining this directly. |
reed@google.com | 2689f61 | 2013-03-20 20:01:47 +0000 | [diff] [blame] | 1701 | if (!this->getTableSize('glyf') || !this->getTableSize('loca')) { |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1702 | return info; |
| 1703 | } |
| 1704 | |
| 1705 | info->fType = SkAdvancedTypefaceMetrics::kTrueType_Font; |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 1706 | CTFontSymbolicTraits symbolicTraits = CTFontGetSymbolicTraits(ctFont.get()); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1707 | if (symbolicTraits & kCTFontMonoSpaceTrait) { |
| 1708 | info->fStyle |= SkAdvancedTypefaceMetrics::kFixedPitch_Style; |
| 1709 | } |
| 1710 | if (symbolicTraits & kCTFontItalicTrait) { |
| 1711 | info->fStyle |= SkAdvancedTypefaceMetrics::kItalic_Style; |
| 1712 | } |
| 1713 | CTFontStylisticClass stylisticClass = symbolicTraits & kCTFontClassMaskTrait; |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1714 | if (stylisticClass >= kCTFontOldStyleSerifsClass && stylisticClass <= kCTFontSlabSerifsClass) { |
| 1715 | info->fStyle |= SkAdvancedTypefaceMetrics::kSerif_Style; |
| 1716 | } else if (stylisticClass & kCTFontScriptsClass) { |
| 1717 | info->fStyle |= SkAdvancedTypefaceMetrics::kScript_Style; |
| 1718 | } |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 1719 | info->fItalicAngle = (int16_t) CTFontGetSlantAngle(ctFont.get()); |
| 1720 | info->fAscent = (int16_t) CTFontGetAscent(ctFont.get()); |
| 1721 | info->fDescent = (int16_t) CTFontGetDescent(ctFont.get()); |
| 1722 | info->fCapHeight = (int16_t) CTFontGetCapHeight(ctFont.get()); |
| 1723 | CGRect bbox = CTFontGetBoundingBox(ctFont.get()); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1724 | |
| 1725 | SkRect r; |
| 1726 | r.set( CGToScalar(CGRectGetMinX_inline(bbox)), // Left |
| 1727 | CGToScalar(CGRectGetMaxY_inline(bbox)), // Top |
| 1728 | CGToScalar(CGRectGetMaxX_inline(bbox)), // Right |
| 1729 | CGToScalar(CGRectGetMinY_inline(bbox))); // Bottom |
| 1730 | |
| 1731 | r.roundOut(&(info->fBBox)); |
| 1732 | |
| 1733 | // Figure out a good guess for StemV - Min width of i, I, !, 1. |
| 1734 | // This probably isn't very good with an italic font. |
| 1735 | int16_t min_width = SHRT_MAX; |
| 1736 | info->fStemV = 0; |
| 1737 | static const UniChar stem_chars[] = {'i', 'I', '!', '1'}; |
| 1738 | const size_t count = sizeof(stem_chars) / sizeof(stem_chars[0]); |
| 1739 | CGGlyph glyphs[count]; |
| 1740 | CGRect boundingRects[count]; |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 1741 | if (CTFontGetGlyphsForCharacters(ctFont.get(), stem_chars, glyphs, count)) { |
bungeman | 18ec7b9 | 2017-02-09 17:15:59 -0500 | [diff] [blame] | 1742 | CTFontGetBoundingRectsForGlyphs(ctFont.get(), kCTFontOrientationHorizontal, |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1743 | glyphs, boundingRects, count); |
| 1744 | for (size_t i = 0; i < count; i++) { |
| 1745 | int16_t width = (int16_t) boundingRects[i].size.width; |
| 1746 | if (width > 0 && width < min_width) { |
| 1747 | min_width = width; |
| 1748 | info->fStemV = min_width; |
| 1749 | } |
| 1750 | } |
| 1751 | } |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1752 | return info; |
| 1753 | } |
| 1754 | |
| 1755 | /////////////////////////////////////////////////////////////////////////////// |
| 1756 | |
Ben Wagner | 93e4ea5 | 2018-04-05 11:57:17 -0400 | [diff] [blame] | 1757 | static SK_SFNT_ULONG get_font_type_tag(CTFontRef ctFont) { |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 1758 | SkUniqueCFRef<CFNumberRef> fontFormatRef( |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1759 | static_cast<CFNumberRef>(CTFontCopyAttribute(ctFont, kCTFontFormatAttribute))); |
| 1760 | if (!fontFormatRef) { |
| 1761 | return 0; |
| 1762 | } |
| 1763 | |
| 1764 | SInt32 fontFormatValue; |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 1765 | if (!CFNumberGetValue(fontFormatRef.get(), kCFNumberSInt32Type, &fontFormatValue)) { |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1766 | return 0; |
| 1767 | } |
| 1768 | |
| 1769 | switch (fontFormatValue) { |
| 1770 | case kCTFontFormatOpenTypePostScript: |
| 1771 | return SkSFNTHeader::fontType_OpenTypeCFF::TAG; |
| 1772 | case kCTFontFormatOpenTypeTrueType: |
| 1773 | return SkSFNTHeader::fontType_WindowsTrueType::TAG; |
| 1774 | case kCTFontFormatTrueType: |
| 1775 | return SkSFNTHeader::fontType_MacTrueType::TAG; |
| 1776 | case kCTFontFormatPostScript: |
| 1777 | return SkSFNTHeader::fontType_PostScript::TAG; |
| 1778 | case kCTFontFormatBitmap: |
| 1779 | return SkSFNTHeader::fontType_MacTrueType::TAG; |
| 1780 | case kCTFontFormatUnrecognized: |
| 1781 | default: |
Ben Wagner | 93e4ea5 | 2018-04-05 11:57:17 -0400 | [diff] [blame] | 1782 | return 0; |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1783 | } |
| 1784 | } |
| 1785 | |
bungeman | 5f213d9 | 2015-01-27 05:39:10 -0800 | [diff] [blame] | 1786 | SkStreamAsset* SkTypeface_Mac::onOpenStream(int* ttcIndex) const { |
Ben Wagner | 93e4ea5 | 2018-04-05 11:57:17 -0400 | [diff] [blame] | 1787 | SK_SFNT_ULONG fontType = get_font_type_tag(fFontRef.get()); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1788 | |
| 1789 | // get table tags |
reed@google.com | cc9aad5 | 2013-03-21 19:28:10 +0000 | [diff] [blame] | 1790 | int numTables = this->countTables(); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1791 | SkTDArray<SkFontTableTag> tableTags; |
| 1792 | tableTags.setCount(numTables); |
reed@google.com | cc9aad5 | 2013-03-21 19:28:10 +0000 | [diff] [blame] | 1793 | this->getTableTags(tableTags.begin()); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1794 | |
Ben Wagner | 93e4ea5 | 2018-04-05 11:57:17 -0400 | [diff] [blame] | 1795 | // CT seems to be unreliable in being able to obtain the type, |
| 1796 | // even if all we want is the first four bytes of the font resource. |
| 1797 | // Just the presence of the FontForge 'FFTM' table seems to throw it off. |
| 1798 | if (fontType == 0) { |
| 1799 | fontType = SkSFNTHeader::fontType_WindowsTrueType::TAG; |
| 1800 | |
| 1801 | // see https://skbug.com/7630#c7 |
| 1802 | bool couldBeCFF = false; |
| 1803 | constexpr SkFontTableTag CFFTag = SkSetFourByteTag('C', 'F', 'F', ' '); |
| 1804 | constexpr SkFontTableTag CFF2Tag = SkSetFourByteTag('C', 'F', 'F', '2'); |
| 1805 | for (int tableIndex = 0; tableIndex < numTables; ++tableIndex) { |
| 1806 | if (CFFTag == tableTags[tableIndex] || CFF2Tag == tableTags[tableIndex]) { |
| 1807 | couldBeCFF = true; |
| 1808 | } |
| 1809 | } |
| 1810 | if (couldBeCFF) { |
| 1811 | fontType = SkSFNTHeader::fontType_OpenTypeCFF::TAG; |
| 1812 | } |
| 1813 | } |
| 1814 | |
| 1815 | // Sometimes CoreGraphics incorrectly thinks a font is kCTFontFormatPostScript. |
| 1816 | // It is exceedingly unlikely that this is the case, so double check |
| 1817 | // (see https://crbug.com/809763 ). |
| 1818 | if (fontType == SkSFNTHeader::fontType_PostScript::TAG) { |
| 1819 | // see if there are any required 'typ1' tables (see Adobe Technical Note #5180) |
| 1820 | bool couldBeTyp1 = false; |
| 1821 | constexpr SkFontTableTag TYPE1Tag = SkSetFourByteTag('T', 'Y', 'P', '1'); |
| 1822 | constexpr SkFontTableTag CIDTag = SkSetFourByteTag('C', 'I', 'D', ' '); |
| 1823 | for (int tableIndex = 0; tableIndex < numTables; ++tableIndex) { |
| 1824 | if (TYPE1Tag == tableTags[tableIndex] || CIDTag == tableTags[tableIndex]) { |
| 1825 | couldBeTyp1 = true; |
| 1826 | } |
| 1827 | } |
| 1828 | if (!couldBeTyp1) { |
| 1829 | fontType = SkSFNTHeader::fontType_OpenTypeCFF::TAG; |
| 1830 | } |
| 1831 | } |
| 1832 | |
Ben Wagner | 03cd6e6 | 2018-03-08 16:02:55 -0500 | [diff] [blame] | 1833 | // get the table sizes and accumulate the total size of the font |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1834 | SkTDArray<size_t> tableSizes; |
| 1835 | size_t totalSize = sizeof(SkSFNTHeader) + sizeof(SkSFNTHeader::TableDirectoryEntry) * numTables; |
| 1836 | for (int tableIndex = 0; tableIndex < numTables; ++tableIndex) { |
reed@google.com | cc9aad5 | 2013-03-21 19:28:10 +0000 | [diff] [blame] | 1837 | size_t tableSize = this->getTableSize(tableTags[tableIndex]); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1838 | totalSize += (tableSize + 3) & ~3; |
| 1839 | *tableSizes.append() = tableSize; |
| 1840 | } |
| 1841 | |
| 1842 | // reserve memory for stream, and zero it (tables must be zero padded) |
| 1843 | SkMemoryStream* stream = new SkMemoryStream(totalSize); |
| 1844 | char* dataStart = (char*)stream->getMemoryBase(); |
| 1845 | sk_bzero(dataStart, totalSize); |
| 1846 | char* dataPtr = dataStart; |
| 1847 | |
| 1848 | // compute font header entries |
| 1849 | uint16_t entrySelector = 0; |
| 1850 | uint16_t searchRange = 1; |
| 1851 | while (searchRange < numTables >> 1) { |
| 1852 | entrySelector++; |
| 1853 | searchRange <<= 1; |
| 1854 | } |
| 1855 | searchRange <<= 4; |
| 1856 | uint16_t rangeShift = (numTables << 4) - searchRange; |
| 1857 | |
| 1858 | // write font header |
| 1859 | SkSFNTHeader* header = (SkSFNTHeader*)dataPtr; |
| 1860 | header->fontType = fontType; |
| 1861 | header->numTables = SkEndian_SwapBE16(numTables); |
| 1862 | header->searchRange = SkEndian_SwapBE16(searchRange); |
| 1863 | header->entrySelector = SkEndian_SwapBE16(entrySelector); |
| 1864 | header->rangeShift = SkEndian_SwapBE16(rangeShift); |
| 1865 | dataPtr += sizeof(SkSFNTHeader); |
| 1866 | |
| 1867 | // write tables |
| 1868 | SkSFNTHeader::TableDirectoryEntry* entry = (SkSFNTHeader::TableDirectoryEntry*)dataPtr; |
| 1869 | dataPtr += sizeof(SkSFNTHeader::TableDirectoryEntry) * numTables; |
| 1870 | for (int tableIndex = 0; tableIndex < numTables; ++tableIndex) { |
| 1871 | size_t tableSize = tableSizes[tableIndex]; |
reed@google.com | cc9aad5 | 2013-03-21 19:28:10 +0000 | [diff] [blame] | 1872 | this->getTableData(tableTags[tableIndex], 0, tableSize, dataPtr); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1873 | entry->tag = SkEndian_SwapBE32(tableTags[tableIndex]); |
| 1874 | entry->checksum = SkEndian_SwapBE32(SkOTUtils::CalcTableChecksum((SK_OT_ULONG*)dataPtr, |
| 1875 | tableSize)); |
reed@google.com | 7fa2a65 | 2014-01-27 13:42:58 +0000 | [diff] [blame] | 1876 | entry->offset = SkEndian_SwapBE32(SkToU32(dataPtr - dataStart)); |
| 1877 | entry->logicalLength = SkEndian_SwapBE32(SkToU32(tableSize)); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1878 | |
| 1879 | dataPtr += (tableSize + 3) & ~3; |
| 1880 | ++entry; |
| 1881 | } |
| 1882 | |
bungeman | b3310c2 | 2015-03-02 09:05:36 -0800 | [diff] [blame] | 1883 | *ttcIndex = 0; |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 1884 | return stream; |
| 1885 | } |
| 1886 | |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 1887 | struct NonDefaultAxesContext { |
| 1888 | SkFixed* axisValue; |
| 1889 | CFArrayRef cgAxes; |
| 1890 | }; |
| 1891 | static void set_non_default_axes(CFTypeRef key, CFTypeRef value, void* context) { |
| 1892 | NonDefaultAxesContext* self = static_cast<NonDefaultAxesContext*>(context); |
| 1893 | |
| 1894 | if (CFGetTypeID(key) != CFStringGetTypeID() || CFGetTypeID(value) != CFNumberGetTypeID()) { |
| 1895 | return; |
| 1896 | } |
| 1897 | |
| 1898 | // The key is a CFString which is a string from the 'name' table. |
| 1899 | // Search the cgAxes for an axis with this name, and use its index to store the value. |
| 1900 | CFIndex keyIndex = -1; |
| 1901 | CFStringRef keyString = static_cast<CFStringRef>(key); |
| 1902 | for (CFIndex i = 0; i < CFArrayGetCount(self->cgAxes); ++i) { |
| 1903 | CFTypeRef cgAxis = CFArrayGetValueAtIndex(self->cgAxes, i); |
| 1904 | if (CFGetTypeID(cgAxis) != CFDictionaryGetTypeID()) { |
| 1905 | continue; |
| 1906 | } |
| 1907 | |
| 1908 | CFDictionaryRef cgAxisDict = static_cast<CFDictionaryRef>(cgAxis); |
| 1909 | CFTypeRef cgAxisName = CFDictionaryGetValue(cgAxisDict, kCGFontVariationAxisName); |
| 1910 | if (!cgAxisName || CFGetTypeID(cgAxisName) != CFStringGetTypeID()) { |
| 1911 | continue; |
| 1912 | } |
| 1913 | CFStringRef cgAxisNameString = static_cast<CFStringRef>(cgAxisName); |
| 1914 | if (CFStringCompare(keyString, cgAxisNameString, 0) == kCFCompareEqualTo) { |
| 1915 | keyIndex = i; |
| 1916 | break; |
| 1917 | } |
| 1918 | } |
| 1919 | if (keyIndex == -1) { |
| 1920 | return; |
| 1921 | } |
| 1922 | |
| 1923 | CFNumberRef valueNumber = static_cast<CFNumberRef>(value); |
| 1924 | double valueDouble; |
| 1925 | if (!CFNumberGetValue(valueNumber, kCFNumberDoubleType, &valueDouble) || |
| 1926 | valueDouble < SkFixedToDouble(SK_FixedMin) || SkFixedToDouble(SK_FixedMax) < valueDouble) |
| 1927 | { |
| 1928 | return; |
| 1929 | } |
| 1930 | self->axisValue[keyIndex] = SkDoubleToFixed(valueDouble); |
| 1931 | } |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 1932 | static bool get_variations(CTFontRef ctFont, CFIndex* cgAxisCount, |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 1933 | SkAutoSTMalloc<4, SkFixed>* axisValues) |
| 1934 | { |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 1935 | // In 10.10 and earlier, CTFontCopyVariationAxes and CTFontCopyVariation do not work when |
| 1936 | // applied to fonts which started life with CGFontCreateWithDataProvider (they simply always |
| 1937 | // return nullptr). As a result, we are limited to CGFontCopyVariationAxes and |
| 1938 | // CGFontCopyVariations here until support for 10.10 and earlier is removed. |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 1939 | SkUniqueCFRef<CGFontRef> cgFont(CTFontCopyGraphicsFont(ctFont, nullptr)); |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 1940 | if (!cgFont) { |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 1941 | return false; |
| 1942 | } |
| 1943 | |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 1944 | SkUniqueCFRef<CFDictionaryRef> cgVariations(CGFontCopyVariations(cgFont.get())); |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 1945 | // If a font has no variations CGFontCopyVariations returns nullptr (instead of an empty dict). |
| 1946 | if (!cgVariations) { |
| 1947 | return false; |
| 1948 | } |
| 1949 | |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 1950 | SkUniqueCFRef<CFArrayRef> cgAxes(CGFontCopyVariationAxes(cgFont.get())); |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 1951 | if (!cgAxes) { |
| 1952 | return false; |
| 1953 | } |
| 1954 | *cgAxisCount = CFArrayGetCount(cgAxes.get()); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 1955 | axisValues->reset(*cgAxisCount); |
| 1956 | |
| 1957 | // Set all of the axes to their default values. |
| 1958 | // Fail if any default value cannot be determined. |
| 1959 | for (CFIndex i = 0; i < *cgAxisCount; ++i) { |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 1960 | CFTypeRef cgAxis = CFArrayGetValueAtIndex(cgAxes.get(), i); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 1961 | if (CFGetTypeID(cgAxis) != CFDictionaryGetTypeID()) { |
| 1962 | return false; |
| 1963 | } |
| 1964 | |
| 1965 | CFDictionaryRef cgAxisDict = static_cast<CFDictionaryRef>(cgAxis); |
| 1966 | CFTypeRef axisDefaultValue = CFDictionaryGetValue(cgAxisDict, |
| 1967 | kCGFontVariationAxisDefaultValue); |
| 1968 | if (!axisDefaultValue || CFGetTypeID(axisDefaultValue) != CFNumberGetTypeID()) { |
| 1969 | return false; |
| 1970 | } |
| 1971 | CFNumberRef axisDefaultValueNumber = static_cast<CFNumberRef>(axisDefaultValue); |
| 1972 | double axisDefaultValueDouble; |
| 1973 | if (!CFNumberGetValue(axisDefaultValueNumber, kCFNumberDoubleType, &axisDefaultValueDouble)) |
| 1974 | { |
| 1975 | return false; |
| 1976 | } |
| 1977 | if (axisDefaultValueDouble < SkFixedToDouble(SK_FixedMin) || |
| 1978 | SkFixedToDouble(SK_FixedMax) < axisDefaultValueDouble) |
| 1979 | { |
| 1980 | return false; |
| 1981 | } |
| 1982 | (*axisValues)[(int)i] = SkDoubleToFixed(axisDefaultValueDouble); |
| 1983 | } |
| 1984 | |
| 1985 | // Override the default values with the given font's stated axis values. |
| 1986 | NonDefaultAxesContext c = { axisValues->get(), cgAxes.get() }; |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 1987 | CFDictionaryApplyFunction(cgVariations.get(), set_non_default_axes, &c); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 1988 | |
| 1989 | return true; |
| 1990 | } |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 1991 | std::unique_ptr<SkFontData> SkTypeface_Mac::onMakeFontData() const { |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 1992 | int index; |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 1993 | std::unique_ptr<SkStreamAsset> stream(this->onOpenStream(&index)); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 1994 | |
| 1995 | CFIndex cgAxisCount; |
| 1996 | SkAutoSTMalloc<4, SkFixed> axisValues; |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 1997 | if (get_variations(fFontRef.get(), &cgAxisCount, &axisValues)) { |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 1998 | return skstd::make_unique<SkFontData>(std::move(stream), index, |
| 1999 | axisValues.get(), cgAxisCount); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 2000 | } |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 2001 | return skstd::make_unique<SkFontData>(std::move(stream), index, nullptr, 0); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 2002 | } |
| 2003 | |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 2004 | /** Creates a CT variation dictionary {tag, value} from a CG variation dictionary {name, value}. */ |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 2005 | static SkUniqueCFRef<CFDictionaryRef> ct_variation_from_cg_variation(CFDictionaryRef cgVariations, |
| 2006 | CFArrayRef ctAxes) { |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 2007 | |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 2008 | SkUniqueCFRef<CFMutableDictionaryRef> ctVariations( |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 2009 | CFDictionaryCreateMutable(kCFAllocatorDefault, 0, |
| 2010 | &kCFTypeDictionaryKeyCallBacks, |
| 2011 | &kCFTypeDictionaryValueCallBacks)); |
| 2012 | |
| 2013 | CFIndex axisCount = CFArrayGetCount(ctAxes); |
| 2014 | for (CFIndex i = 0; i < axisCount; ++i) { |
| 2015 | CFTypeRef axisInfo = CFArrayGetValueAtIndex(ctAxes, i); |
| 2016 | if (CFDictionaryGetTypeID() != CFGetTypeID(axisInfo)) { |
| 2017 | return nullptr; |
| 2018 | } |
| 2019 | CFDictionaryRef axisInfoDict = static_cast<CFDictionaryRef>(axisInfo); |
| 2020 | |
| 2021 | // The assumption is that values produced by kCTFontVariationAxisNameKey and |
| 2022 | // kCGFontVariationAxisName will always be equal. |
| 2023 | CFTypeRef axisName = CFDictionaryGetValue(axisInfoDict, kCTFontVariationAxisNameKey); |
| 2024 | if (!axisName || CFGetTypeID(axisName) != CFStringGetTypeID()) { |
| 2025 | return nullptr; |
| 2026 | } |
| 2027 | |
| 2028 | CFTypeRef axisValue = CFDictionaryGetValue(cgVariations, axisName); |
| 2029 | if (!axisValue || CFGetTypeID(axisValue) != CFNumberGetTypeID()) { |
| 2030 | return nullptr; |
| 2031 | } |
| 2032 | |
| 2033 | CFTypeRef axisTag = CFDictionaryGetValue(axisInfoDict, kCTFontVariationAxisIdentifierKey); |
| 2034 | if (!axisTag || CFGetTypeID(axisTag) != CFNumberGetTypeID()) { |
| 2035 | return nullptr; |
| 2036 | } |
| 2037 | |
| 2038 | CFDictionaryAddValue(ctVariations.get(), axisTag, axisValue); |
| 2039 | } |
| 2040 | return std::move(ctVariations); |
| 2041 | } |
| 2042 | |
| 2043 | int SkTypeface_Mac::onGetVariationDesignPosition( |
| 2044 | SkFontArguments::VariationPosition::Coordinate coordinates[], int coordinateCount) const |
| 2045 | { |
| 2046 | // The CGFont variation data does not contain the tag. |
| 2047 | |
bungeman | bc096bf | 2017-04-25 13:32:50 -0400 | [diff] [blame] | 2048 | // CTFontCopyVariationAxes returns nullptr for CGFontCreateWithDataProvider fonts with |
| 2049 | // macOS 10.10 and iOS 9 or earlier. When this happens, there is no API to provide the tag. |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 2050 | SkUniqueCFRef<CFArrayRef> ctAxes(CTFontCopyVariationAxes(fFontRef.get())); |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 2051 | if (!ctAxes) { |
| 2052 | return -1; |
| 2053 | } |
| 2054 | CFIndex axisCount = CFArrayGetCount(ctAxes.get()); |
| 2055 | if (!coordinates || coordinateCount < axisCount) { |
| 2056 | return axisCount; |
| 2057 | } |
| 2058 | |
| 2059 | // This call always returns nullptr on 10.11 and under for CGFontCreateWithDataProvider fonts. |
| 2060 | // When this happens, try converting the CG variation to a CT variation. |
| 2061 | // On 10.12 and later, this only returns non-default variations. |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 2062 | SkUniqueCFRef<CFDictionaryRef> ctVariations(CTFontCopyVariation(fFontRef.get())); |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 2063 | if (!ctVariations) { |
| 2064 | // When 10.11 and earlier are no longer supported, the following code can be replaced with |
| 2065 | // return -1 and ct_variation_from_cg_variation can be removed. |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 2066 | SkUniqueCFRef<CGFontRef> cgFont(CTFontCopyGraphicsFont(fFontRef.get(), nullptr)); |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 2067 | if (!cgFont) { |
| 2068 | return -1; |
| 2069 | } |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 2070 | SkUniqueCFRef<CFDictionaryRef> cgVariations(CGFontCopyVariations(cgFont.get())); |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 2071 | if (!cgVariations) { |
| 2072 | return -1; |
| 2073 | } |
| 2074 | ctVariations = ct_variation_from_cg_variation(cgVariations.get(), ctAxes.get()); |
| 2075 | if (!ctVariations) { |
| 2076 | return -1; |
| 2077 | } |
| 2078 | } |
| 2079 | |
| 2080 | for (int i = 0; i < axisCount; ++i) { |
| 2081 | CFTypeRef axisInfo = CFArrayGetValueAtIndex(ctAxes.get(), i); |
| 2082 | if (CFDictionaryGetTypeID() != CFGetTypeID(axisInfo)) { |
| 2083 | return -1; |
| 2084 | } |
| 2085 | CFDictionaryRef axisInfoDict = static_cast<CFDictionaryRef>(axisInfo); |
| 2086 | |
| 2087 | CFTypeRef tag = CFDictionaryGetValue(axisInfoDict, kCTFontVariationAxisIdentifierKey); |
| 2088 | if (!tag || CFGetTypeID(tag) != CFNumberGetTypeID()) { |
| 2089 | return -1; |
| 2090 | } |
| 2091 | CFNumberRef tagNumber = static_cast<CFNumberRef>(tag); |
| 2092 | int64_t tagLong; |
| 2093 | if (!CFNumberGetValue(tagNumber, kCFNumberSInt64Type, &tagLong)) { |
| 2094 | return -1; |
| 2095 | } |
| 2096 | coordinates[i].axis = tagLong; |
| 2097 | |
| 2098 | CGFloat variationCGFloat; |
| 2099 | CFTypeRef variationValue = CFDictionaryGetValue(ctVariations.get(), tagNumber); |
| 2100 | if (variationValue) { |
| 2101 | if (CFGetTypeID(variationValue) != CFNumberGetTypeID()) { |
| 2102 | return -1; |
| 2103 | } |
| 2104 | CFNumberRef variationNumber = static_cast<CFNumberRef>(variationValue); |
| 2105 | if (!CFNumberGetValue(variationNumber, kCFNumberCGFloatType, &variationCGFloat)) { |
| 2106 | return -1; |
| 2107 | } |
| 2108 | } else { |
| 2109 | CFTypeRef def = CFDictionaryGetValue(axisInfoDict, kCTFontVariationAxisDefaultValueKey); |
| 2110 | if (!def || CFGetTypeID(def) != CFNumberGetTypeID()) { |
| 2111 | return -1; |
| 2112 | } |
| 2113 | CFNumberRef defNumber = static_cast<CFNumberRef>(def); |
| 2114 | if (!CFNumberGetValue(defNumber, kCFNumberCGFloatType, &variationCGFloat)) { |
| 2115 | return -1; |
| 2116 | } |
| 2117 | } |
| 2118 | coordinates[i].value = CGToScalar(variationCGFloat); |
| 2119 | |
| 2120 | } |
| 2121 | return axisCount; |
| 2122 | } |
| 2123 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 2124 | /////////////////////////////////////////////////////////////////////////////// |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 2125 | /////////////////////////////////////////////////////////////////////////////// |
| 2126 | |
| 2127 | int SkTypeface_Mac::onGetUPEM() const { |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 2128 | SkUniqueCFRef<CGFontRef> cgFont(CTFontCopyGraphicsFont(fFontRef.get(), nullptr)); |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2129 | return CGFontGetUnitsPerEm(cgFont.get()); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 2130 | } |
| 2131 | |
bungeman@google.com | 839702b | 2013-08-07 17:09:22 +0000 | [diff] [blame] | 2132 | SkTypeface::LocalizedStrings* SkTypeface_Mac::onCreateFamilyNameIterator() const { |
Ben Wagner | ad031f5 | 2018-08-20 13:45:57 -0400 | [diff] [blame] | 2133 | sk_sp<SkTypeface::LocalizedStrings> nameIter = |
| 2134 | SkOTUtils::LocalizedStrings_NameTable::MakeForFamilyNames(*this); |
| 2135 | if (!nameIter) { |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2136 | CFStringRef cfLanguageRaw; |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 2137 | SkUniqueCFRef<CFStringRef> cfFamilyName( |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2138 | CTFontCopyLocalizedName(fFontRef.get(), kCTFontFamilyNameKey, &cfLanguageRaw)); |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 2139 | SkUniqueCFRef<CFStringRef> cfLanguage(cfLanguageRaw); |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 2140 | |
| 2141 | SkString skLanguage; |
| 2142 | SkString skFamilyName; |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2143 | if (cfLanguage) { |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 2144 | CFStringToSkString(cfLanguage.get(), &skLanguage); |
| 2145 | } else { |
| 2146 | skLanguage = "und"; //undetermined |
| 2147 | } |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2148 | if (cfFamilyName) { |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 2149 | CFStringToSkString(cfFamilyName.get(), &skFamilyName); |
| 2150 | } |
| 2151 | |
Ben Wagner | ad031f5 | 2018-08-20 13:45:57 -0400 | [diff] [blame] | 2152 | nameIter = sk_make_sp<SkOTUtils::LocalizedStrings_SingleName>(skFamilyName, skLanguage); |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 2153 | } |
Ben Wagner | ad031f5 | 2018-08-20 13:45:57 -0400 | [diff] [blame] | 2154 | return nameIter.release(); |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 2155 | } |
| 2156 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 2157 | int SkTypeface_Mac::onGetTableTags(SkFontTableTag tags[]) const { |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 2158 | SkUniqueCFRef<CFArrayRef> cfArray( |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2159 | CTFontCopyAvailableTables(fFontRef.get(), kCTFontTableOptionNoOptions)); |
| 2160 | if (!cfArray) { |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 2161 | return 0; |
| 2162 | } |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2163 | int count = SkToInt(CFArrayGetCount(cfArray.get())); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 2164 | if (tags) { |
| 2165 | for (int i = 0; i < count; ++i) { |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2166 | uintptr_t fontTag = reinterpret_cast<uintptr_t>( |
| 2167 | CFArrayGetValueAtIndex(cfArray.get(), i)); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 2168 | tags[i] = static_cast<SkFontTableTag>(fontTag); |
| 2169 | } |
| 2170 | } |
| 2171 | return count; |
| 2172 | } |
| 2173 | |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2174 | // If, as is the case with web fonts, the CTFont data isn't available, |
| 2175 | // the CGFont data may work. While the CGFont may always provide the |
| 2176 | // right result, leave the CTFont code path to minimize disruption. |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 2177 | static SkUniqueCFRef<CFDataRef> copy_table_from_font(CTFontRef ctFont, SkFontTableTag tag) { |
| 2178 | SkUniqueCFRef<CFDataRef> data(CTFontCopyTable(ctFont, (CTFontTableTag) tag, |
| 2179 | kCTFontTableOptionNoOptions)); |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2180 | if (!data) { |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 2181 | SkUniqueCFRef<CGFontRef> cgFont(CTFontCopyGraphicsFont(ctFont, nullptr)); |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2182 | data.reset(CGFontCopyTableForTag(cgFont.get(), tag)); |
| 2183 | } |
| 2184 | return data; |
| 2185 | } |
| 2186 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 2187 | size_t SkTypeface_Mac::onGetTableData(SkFontTableTag tag, size_t offset, |
| 2188 | size_t length, void* dstData) const { |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 2189 | SkUniqueCFRef<CFDataRef> srcData = copy_table_from_font(fFontRef.get(), tag); |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2190 | if (!srcData) { |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 2191 | return 0; |
| 2192 | } |
| 2193 | |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2194 | size_t srcSize = CFDataGetLength(srcData.get()); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 2195 | if (offset >= srcSize) { |
| 2196 | return 0; |
| 2197 | } |
| 2198 | if (length > srcSize - offset) { |
| 2199 | length = srcSize - offset; |
| 2200 | } |
| 2201 | if (dstData) { |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2202 | memcpy(dstData, CFDataGetBytePtr(srcData.get()) + offset, length); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 2203 | } |
| 2204 | return length; |
| 2205 | } |
| 2206 | |
reed | a9322c2 | 2016-04-12 06:47:05 -0700 | [diff] [blame] | 2207 | SkScalerContext* SkTypeface_Mac::onCreateScalerContext(const SkScalerContextEffects& effects, |
| 2208 | const SkDescriptor* desc) const { |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 2209 | return new SkScalerContext_Mac(sk_ref_sp(const_cast<SkTypeface_Mac*>(this)), effects, desc); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 2210 | } |
| 2211 | |
| 2212 | void SkTypeface_Mac::onFilterRec(SkScalerContextRec* rec) const { |
commit-bot@chromium.org | c5fd461 | 2013-05-06 22:23:08 +0000 | [diff] [blame] | 2213 | if (rec->fFlags & SkScalerContext::kLCD_BGROrder_Flag || |
| 2214 | rec->fFlags & SkScalerContext::kLCD_Vertical_Flag) |
| 2215 | { |
| 2216 | rec->fMaskFormat = SkMask::kA8_Format; |
| 2217 | // Render the glyphs as close as possible to what was requested. |
| 2218 | // The above turns off subpixel rendering, but the user requested it. |
| 2219 | // Normal hinting will cause the A8 masks to be generated from CoreGraphics subpixel masks. |
| 2220 | // See comments below for more details. |
Mike Reed | 04346d5 | 2018-11-05 12:45:32 -0500 | [diff] [blame] | 2221 | rec->setHinting(kNormal_SkFontHinting); |
commit-bot@chromium.org | c5fd461 | 2013-05-06 22:23:08 +0000 | [diff] [blame] | 2222 | } |
skia.committer@gmail.com | e944de7 | 2013-05-07 07:01:03 +0000 | [diff] [blame] | 2223 | |
Herb Derby | fcac00f | 2018-05-01 11:57:56 -0400 | [diff] [blame] | 2224 | unsigned flagsWeDontSupport = SkScalerContext::kForceAutohinting_Flag | |
commit-bot@chromium.org | c5fd461 | 2013-05-06 22:23:08 +0000 | [diff] [blame] | 2225 | SkScalerContext::kLCD_BGROrder_Flag | |
| 2226 | SkScalerContext::kLCD_Vertical_Flag; |
skia.committer@gmail.com | 0c23faf | 2013-03-03 07:16:29 +0000 | [diff] [blame] | 2227 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 2228 | rec->fFlags &= ~flagsWeDontSupport; |
skia.committer@gmail.com | 0c23faf | 2013-03-03 07:16:29 +0000 | [diff] [blame] | 2229 | |
Ben Wagner | 6112f0b | 2018-09-27 15:44:56 -0400 | [diff] [blame] | 2230 | SmoothBehavior smoothBehavior = smooth_behavior(); |
skia.committer@gmail.com | 0c23faf | 2013-03-03 07:16:29 +0000 | [diff] [blame] | 2231 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 2232 | // Only two levels of hinting are supported. |
| 2233 | // kNo_Hinting means avoid CoreGraphics outline dilation. |
| 2234 | // kNormal_Hinting means CoreGraphics outline dilation is allowed. |
| 2235 | // If there is no lcd support, hinting (dilation) cannot be supported. |
Mike Reed | 04346d5 | 2018-11-05 12:45:32 -0500 | [diff] [blame] | 2236 | SkFontHinting hinting = rec->getHinting(); |
| 2237 | if (kSlight_SkFontHinting == hinting || smoothBehavior == SmoothBehavior::none) { |
| 2238 | hinting = kNo_SkFontHinting; |
| 2239 | } else if (kFull_SkFontHinting == hinting) { |
| 2240 | hinting = kNormal_SkFontHinting; |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 2241 | } |
| 2242 | rec->setHinting(hinting); |
skia.committer@gmail.com | 0c23faf | 2013-03-03 07:16:29 +0000 | [diff] [blame] | 2243 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 2244 | // FIXME: lcd smoothed un-hinted rasterization unsupported. |
| 2245 | // Tracked by http://code.google.com/p/skia/issues/detail?id=915 . |
| 2246 | // There is no current means to honor a request for unhinted lcd, |
| 2247 | // so arbitrarilly ignore the hinting request and honor lcd. |
skia.committer@gmail.com | 0c23faf | 2013-03-03 07:16:29 +0000 | [diff] [blame] | 2248 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 2249 | // Hinting and smoothing should be orthogonal, but currently they are not. |
| 2250 | // CoreGraphics has no API to influence hinting. However, its lcd smoothed |
| 2251 | // output is drawn from auto-dilated outlines (the amount of which is |
| 2252 | // determined by AppleFontSmoothing). Its regular anti-aliased output is |
| 2253 | // drawn from un-dilated outlines. |
skia.committer@gmail.com | 0c23faf | 2013-03-03 07:16:29 +0000 | [diff] [blame] | 2254 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 2255 | // The behavior of Skia is as follows: |
| 2256 | // [AA][no-hint]: generate AA using CoreGraphic's AA output. |
| 2257 | // [AA][yes-hint]: use CoreGraphic's LCD output and reduce it to a single |
| 2258 | // channel. This matches [LCD][yes-hint] in weight. |
| 2259 | // [LCD][no-hint]: curently unable to honor, and must pick which to respect. |
| 2260 | // Currenly side with LCD, effectively ignoring the hinting setting. |
| 2261 | // [LCD][yes-hint]: generate LCD using CoreGraphic's LCD output. |
skia.committer@gmail.com | 0c23faf | 2013-03-03 07:16:29 +0000 | [diff] [blame] | 2262 | |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2263 | if (rec->fMaskFormat == SkMask::kLCD16_Format) { |
Ben Wagner | 6112f0b | 2018-09-27 15:44:56 -0400 | [diff] [blame] | 2264 | if (smoothBehavior == SmoothBehavior::subpixel) { |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 2265 | //CoreGraphics creates 555 masks for smoothed text anyway. |
| 2266 | rec->fMaskFormat = SkMask::kLCD16_Format; |
Mike Reed | 04346d5 | 2018-11-05 12:45:32 -0500 | [diff] [blame] | 2267 | rec->setHinting(kNormal_SkFontHinting); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 2268 | } else { |
| 2269 | rec->fMaskFormat = SkMask::kA8_Format; |
Ben Wagner | 6112f0b | 2018-09-27 15:44:56 -0400 | [diff] [blame] | 2270 | if (smoothBehavior == SmoothBehavior::some) { |
Mike Reed | 04346d5 | 2018-11-05 12:45:32 -0500 | [diff] [blame] | 2271 | rec->setHinting(kNormal_SkFontHinting); |
Ben Wagner | 6112f0b | 2018-09-27 15:44:56 -0400 | [diff] [blame] | 2272 | } |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 2273 | } |
| 2274 | } |
skia.committer@gmail.com | 0c23faf | 2013-03-03 07:16:29 +0000 | [diff] [blame] | 2275 | |
bungeman | 3490263 | 2014-12-10 21:43:27 -0800 | [diff] [blame] | 2276 | // CoreText provides no information as to whether a glyph will be color or not. |
| 2277 | // Fonts may mix outlines and bitmaps, so information is needed on a glyph by glyph basis. |
| 2278 | // 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] | 2279 | if (fHasColorGlyphs) { |
bungeman | 3490263 | 2014-12-10 21:43:27 -0800 | [diff] [blame] | 2280 | rec->fMaskFormat = SkMask::kARGB32_Format; |
| 2281 | } |
| 2282 | |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 2283 | // Unhinted A8 masks (those not derived from LCD masks) must respect SK_GAMMA_APPLY_TO_A8. |
| 2284 | // All other masks can use regular gamma. |
Mike Reed | 04346d5 | 2018-11-05 12:45:32 -0500 | [diff] [blame] | 2285 | if (SkMask::kA8_Format == rec->fMaskFormat && kNo_SkFontHinting == hinting) { |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 2286 | #ifndef SK_GAMMA_APPLY_TO_A8 |
brianosman | a1e8f8d | 2016-04-08 06:47:54 -0700 | [diff] [blame] | 2287 | // SRGBTODO: Is this correct? Do we want contrast boost? |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 2288 | rec->ignorePreBlend(); |
reed@android.com | feda2f9 | 2010-05-19 13:47:05 +0000 | [diff] [blame] | 2289 | #endif |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 2290 | } else { |
| 2291 | //CoreGraphics dialates smoothed text as needed. |
| 2292 | rec->setContrast(0); |
| 2293 | } |
| 2294 | } |
| 2295 | |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2296 | /** Takes ownership of the CFStringRef. */ |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 2297 | static const char* get_str(CFStringRef ref, SkString* str) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2298 | if (nullptr == ref) { |
| 2299 | return nullptr; |
bungeman | 256b351 | 2014-07-02 07:57:59 -0700 | [diff] [blame] | 2300 | } |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 2301 | CFStringToSkString(ref, str); |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2302 | CFRelease(ref); |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 2303 | return str->c_str(); |
| 2304 | } |
| 2305 | |
bungeman | b374d6a | 2014-09-17 07:48:59 -0700 | [diff] [blame] | 2306 | void SkTypeface_Mac::onGetFamilyName(SkString* familyName) const { |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2307 | get_str(CTFontCopyFamilyName(fFontRef.get()), familyName); |
bungeman | b374d6a | 2014-09-17 07:48:59 -0700 | [diff] [blame] | 2308 | } |
| 2309 | |
reed@google.com | 5526ede | 2013-03-25 13:03:37 +0000 | [diff] [blame] | 2310 | void SkTypeface_Mac::onGetFontDescriptor(SkFontDescriptor* desc, |
| 2311 | bool* isLocalStream) const { |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 2312 | SkString tmpStr; |
skia.committer@gmail.com | 0c23faf | 2013-03-03 07:16:29 +0000 | [diff] [blame] | 2313 | |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2314 | desc->setFamilyName(get_str(CTFontCopyFamilyName(fFontRef.get()), &tmpStr)); |
| 2315 | desc->setFullName(get_str(CTFontCopyFullName(fFontRef.get()), &tmpStr)); |
| 2316 | desc->setPostscriptName(get_str(CTFontCopyPostScriptName(fFontRef.get()), &tmpStr)); |
bungeman | b811378 | 2016-07-25 16:54:59 -0700 | [diff] [blame] | 2317 | desc->setStyle(this->fontStyle()); |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 2318 | *isLocalStream = fIsLocalStream; |
mike@reedtribe.org | b103ed4 | 2013-03-03 03:50:09 +0000 | [diff] [blame] | 2319 | } |
reed@google.com | 5526ede | 2013-03-25 13:03:37 +0000 | [diff] [blame] | 2320 | |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 2321 | int SkTypeface_Mac::onCharsToGlyphs(const void* chars, Encoding encoding, |
bungeman@google.com | 72b8cb2 | 2013-10-25 17:49:08 +0000 | [diff] [blame] | 2322 | uint16_t glyphs[], int glyphCount) const |
| 2323 | { |
| 2324 | // Undocumented behavior of CTFontGetGlyphsForCharacters with non-bmp code points: |
| 2325 | // When a surrogate pair is detected, the glyph index used is the index of the high surrogate. |
| 2326 | // 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] | 2327 | |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 2328 | SkAutoSTMalloc<1024, UniChar> charStorage; |
bungeman@google.com | 72b8cb2 | 2013-10-25 17:49:08 +0000 | [diff] [blame] | 2329 | const UniChar* src; // UniChar is a UTF-16 16-bit code unit. |
| 2330 | int srcCount; |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 2331 | switch (encoding) { |
| 2332 | case kUTF8_Encoding: { |
bungeman@google.com | 72b8cb2 | 2013-10-25 17:49:08 +0000 | [diff] [blame] | 2333 | const char* utf8 = reinterpret_cast<const char*>(chars); |
| 2334 | UniChar* utf16 = charStorage.reset(2 * glyphCount); |
| 2335 | src = utf16; |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 2336 | for (int i = 0; i < glyphCount; ++i) { |
bungeman@google.com | 72b8cb2 | 2013-10-25 17:49:08 +0000 | [diff] [blame] | 2337 | SkUnichar uni = SkUTF8_NextUnichar(&utf8); |
Hal Canary | f107a2f | 2018-07-25 16:52:48 -0400 | [diff] [blame] | 2338 | utf16 += SkUTF::ToUTF16(uni, utf16); |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 2339 | } |
reed@google.com | 7fa2a65 | 2014-01-27 13:42:58 +0000 | [diff] [blame] | 2340 | srcCount = SkToInt(utf16 - src); |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 2341 | break; |
| 2342 | } |
bungeman@google.com | 72b8cb2 | 2013-10-25 17:49:08 +0000 | [diff] [blame] | 2343 | case kUTF16_Encoding: { |
| 2344 | src = reinterpret_cast<const UniChar*>(chars); |
| 2345 | int extra = 0; |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 2346 | for (int i = 0; i < glyphCount; ++i) { |
Ben Wagner | ad031f5 | 2018-08-20 13:45:57 -0400 | [diff] [blame] | 2347 | if (SkUTF16_IsLeadingSurrogate(src[i + extra])) { |
bungeman@google.com | 72b8cb2 | 2013-10-25 17:49:08 +0000 | [diff] [blame] | 2348 | ++extra; |
| 2349 | } |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 2350 | } |
bungeman@google.com | 72b8cb2 | 2013-10-25 17:49:08 +0000 | [diff] [blame] | 2351 | srcCount = glyphCount + extra; |
| 2352 | break; |
| 2353 | } |
| 2354 | case kUTF32_Encoding: { |
| 2355 | const SkUnichar* utf32 = reinterpret_cast<const SkUnichar*>(chars); |
| 2356 | UniChar* utf16 = charStorage.reset(2 * glyphCount); |
| 2357 | src = utf16; |
| 2358 | for (int i = 0; i < glyphCount; ++i) { |
Hal Canary | f107a2f | 2018-07-25 16:52:48 -0400 | [diff] [blame] | 2359 | utf16 += SkUTF::ToUTF16(utf32[i], utf16); |
bungeman@google.com | 72b8cb2 | 2013-10-25 17:49:08 +0000 | [diff] [blame] | 2360 | } |
reed@google.com | 7fa2a65 | 2014-01-27 13:42:58 +0000 | [diff] [blame] | 2361 | srcCount = SkToInt(utf16 - src); |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 2362 | break; |
| 2363 | } |
| 2364 | } |
| 2365 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2366 | // 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] | 2367 | // 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] | 2368 | SkAutoSTMalloc<1024, uint16_t> glyphStorage; |
| 2369 | uint16_t* macGlyphs = glyphs; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2370 | if (nullptr == macGlyphs || srcCount > glyphCount) { |
bungeman@google.com | 72b8cb2 | 2013-10-25 17:49:08 +0000 | [diff] [blame] | 2371 | macGlyphs = glyphStorage.reset(srcCount); |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 2372 | } |
| 2373 | |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2374 | bool allEncoded = CTFontGetGlyphsForCharacters(fFontRef.get(), src, macGlyphs, srcCount); |
bungeman@google.com | 72b8cb2 | 2013-10-25 17:49:08 +0000 | [diff] [blame] | 2375 | |
| 2376 | // If there were any non-bmp, then copy and compact. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2377 | // If 'glyphs' is nullptr, then compact glyphStorage in-place. |
| 2378 | // If all are bmp and 'glyphs' is non-nullptr, 'glyphs' already contains the compact glyphs. |
| 2379 | // 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] | 2380 | uint16_t* compactedGlyphs = glyphs; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2381 | if (nullptr == compactedGlyphs) { |
bungeman@google.com | 72b8cb2 | 2013-10-25 17:49:08 +0000 | [diff] [blame] | 2382 | compactedGlyphs = macGlyphs; |
| 2383 | } |
| 2384 | if (srcCount > glyphCount) { |
| 2385 | int extra = 0; |
| 2386 | for (int i = 0; i < glyphCount; ++i) { |
bungeman | 7b09aab | 2014-09-24 11:04:41 -0700 | [diff] [blame] | 2387 | compactedGlyphs[i] = macGlyphs[i + extra]; |
Ben Wagner | ad031f5 | 2018-08-20 13:45:57 -0400 | [diff] [blame] | 2388 | if (SkUTF16_IsLeadingSurrogate(src[i + extra])) { |
bungeman@google.com | 72b8cb2 | 2013-10-25 17:49:08 +0000 | [diff] [blame] | 2389 | ++extra; |
| 2390 | } |
bungeman@google.com | 72b8cb2 | 2013-10-25 17:49:08 +0000 | [diff] [blame] | 2391 | } |
| 2392 | } |
| 2393 | |
| 2394 | if (allEncoded) { |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 2395 | return glyphCount; |
| 2396 | } |
bungeman@google.com | 72b8cb2 | 2013-10-25 17:49:08 +0000 | [diff] [blame] | 2397 | |
| 2398 | // 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] | 2399 | for (int i = 0; i < glyphCount; ++i) { |
bungeman@google.com | 72b8cb2 | 2013-10-25 17:49:08 +0000 | [diff] [blame] | 2400 | if (0 == compactedGlyphs[i]) { |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 2401 | return i; |
| 2402 | } |
| 2403 | } |
bungeman@google.com | 72b8cb2 | 2013-10-25 17:49:08 +0000 | [diff] [blame] | 2404 | // 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] | 2405 | return glyphCount; |
| 2406 | } |
| 2407 | |
| 2408 | int SkTypeface_Mac::onCountGlyphs() const { |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2409 | return SkToInt(CTFontGetGlyphCount(fFontRef.get())); |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 2410 | } |
| 2411 | |
reed@google.com | 95625db | 2013-03-25 20:44:02 +0000 | [diff] [blame] | 2412 | /////////////////////////////////////////////////////////////////////////////// |
| 2413 | /////////////////////////////////////////////////////////////////////////////// |
reed@google.com | 83787c5 | 2013-03-26 17:19:15 +0000 | [diff] [blame] | 2414 | |
| 2415 | static bool find_desc_str(CTFontDescriptorRef desc, CFStringRef name, SkString* value) { |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 2416 | SkUniqueCFRef<CFStringRef> ref((CFStringRef)CTFontDescriptorCopyAttribute(desc, name)); |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2417 | if (!ref) { |
reed@google.com | 83787c5 | 2013-03-26 17:19:15 +0000 | [diff] [blame] | 2418 | return false; |
| 2419 | } |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2420 | CFStringToSkString(ref.get(), value); |
reed@google.com | 83787c5 | 2013-03-26 17:19:15 +0000 | [diff] [blame] | 2421 | return true; |
| 2422 | } |
| 2423 | |
reed@google.com | 95625db | 2013-03-25 20:44:02 +0000 | [diff] [blame] | 2424 | #include "SkFontMgr.h" |
| 2425 | |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 2426 | static inline int sqr(int value) { |
| 2427 | SkASSERT(SkAbs32(value) < 0x7FFF); // check for overflow |
| 2428 | return value * value; |
| 2429 | } |
| 2430 | |
| 2431 | // We normalize each axis (weight, width, italic) to be base-900 |
| 2432 | static int compute_metric(const SkFontStyle& a, const SkFontStyle& b) { |
| 2433 | return sqr(a.weight() - b.weight()) + |
| 2434 | sqr((a.width() - b.width()) * 100) + |
bungeman | b4bb7d8 | 2016-04-27 10:21:04 -0700 | [diff] [blame] | 2435 | sqr((a.slant() != b.slant()) * 900); |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 2436 | } |
| 2437 | |
reed@google.com | 83787c5 | 2013-03-26 17:19:15 +0000 | [diff] [blame] | 2438 | class SkFontStyleSet_Mac : public SkFontStyleSet { |
reed@google.com | 95625db | 2013-03-25 20:44:02 +0000 | [diff] [blame] | 2439 | public: |
bungeman | 53d5c6e | 2016-04-08 07:22:29 -0700 | [diff] [blame] | 2440 | SkFontStyleSet_Mac(CTFontDescriptorRef desc) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2441 | : fArray(CTFontDescriptorCreateMatchingFontDescriptors(desc, nullptr)) |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2442 | , fCount(0) |
| 2443 | { |
| 2444 | if (!fArray) { |
| 2445 | fArray.reset(CFArrayCreate(nullptr, nullptr, 0, nullptr)); |
reed@google.com | dea7ee0 | 2013-03-28 14:12:10 +0000 | [diff] [blame] | 2446 | } |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2447 | fCount = SkToInt(CFArrayGetCount(fArray.get())); |
reed@google.com | 83787c5 | 2013-03-26 17:19:15 +0000 | [diff] [blame] | 2448 | } |
| 2449 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 2450 | int count() override { |
reed@google.com | dea7ee0 | 2013-03-28 14:12:10 +0000 | [diff] [blame] | 2451 | return fCount; |
reed@google.com | 83787c5 | 2013-03-26 17:19:15 +0000 | [diff] [blame] | 2452 | } |
| 2453 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 2454 | void getStyle(int index, SkFontStyle* style, SkString* name) override { |
reed@google.com | dea7ee0 | 2013-03-28 14:12:10 +0000 | [diff] [blame] | 2455 | SkASSERT((unsigned)index < (unsigned)fCount); |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2456 | CTFontDescriptorRef desc = (CTFontDescriptorRef)CFArrayGetValueAtIndex(fArray.get(), index); |
reed@google.com | 83787c5 | 2013-03-26 17:19:15 +0000 | [diff] [blame] | 2457 | if (style) { |
bungeman | 2873c76 | 2017-01-13 11:40:21 -0500 | [diff] [blame] | 2458 | *style = fontstyle_from_descriptor(desc, false); |
reed@google.com | 83787c5 | 2013-03-26 17:19:15 +0000 | [diff] [blame] | 2459 | } |
| 2460 | if (name) { |
| 2461 | if (!find_desc_str(desc, kCTFontStyleNameAttribute, name)) { |
| 2462 | name->reset(); |
| 2463 | } |
| 2464 | } |
| 2465 | } |
| 2466 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 2467 | SkTypeface* createTypeface(int index) override { |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2468 | SkASSERT((unsigned)index < (unsigned)CFArrayGetCount(fArray.get())); |
| 2469 | CTFontDescriptorRef desc = (CTFontDescriptorRef)CFArrayGetValueAtIndex(fArray.get(), index); |
skia.committer@gmail.com | 37cbc7f | 2013-03-27 07:01:04 +0000 | [diff] [blame] | 2470 | |
Mike Reed | 5922739 | 2017-09-26 09:46:08 -0400 | [diff] [blame] | 2471 | return create_from_desc(desc).release(); |
reed@google.com | 83787c5 | 2013-03-26 17:19:15 +0000 | [diff] [blame] | 2472 | } |
skia.committer@gmail.com | 37cbc7f | 2013-03-27 07:01:04 +0000 | [diff] [blame] | 2473 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 2474 | SkTypeface* matchStyle(const SkFontStyle& pattern) override { |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 2475 | if (0 == fCount) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2476 | return nullptr; |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 2477 | } |
Mike Reed | 5922739 | 2017-09-26 09:46:08 -0400 | [diff] [blame] | 2478 | return create_from_desc(findMatchingDesc(pattern)).release(); |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 2479 | } |
| 2480 | |
reed@google.com | 83787c5 | 2013-03-26 17:19:15 +0000 | [diff] [blame] | 2481 | private: |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 2482 | SkUniqueCFRef<CFArrayRef> fArray; |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2483 | int fCount; |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 2484 | |
| 2485 | CTFontDescriptorRef findMatchingDesc(const SkFontStyle& pattern) const { |
| 2486 | int bestMetric = SK_MaxS32; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2487 | CTFontDescriptorRef bestDesc = nullptr; |
skia.committer@gmail.com | d55846d | 2013-03-30 07:01:27 +0000 | [diff] [blame] | 2488 | |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 2489 | for (int i = 0; i < fCount; ++i) { |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2490 | CTFontDescriptorRef desc = (CTFontDescriptorRef)CFArrayGetValueAtIndex(fArray.get(), i); |
bungeman | 2873c76 | 2017-01-13 11:40:21 -0500 | [diff] [blame] | 2491 | int metric = compute_metric(pattern, fontstyle_from_descriptor(desc, false)); |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 2492 | if (0 == metric) { |
| 2493 | return desc; |
| 2494 | } |
| 2495 | if (metric < bestMetric) { |
| 2496 | bestMetric = metric; |
| 2497 | bestDesc = desc; |
| 2498 | } |
| 2499 | } |
| 2500 | SkASSERT(bestDesc); |
| 2501 | return bestDesc; |
| 2502 | } |
reed@google.com | 83787c5 | 2013-03-26 17:19:15 +0000 | [diff] [blame] | 2503 | }; |
| 2504 | |
| 2505 | class SkFontMgr_Mac : public SkFontMgr { |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 2506 | SkUniqueCFRef<CFArrayRef> fNames; |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2507 | int fCount; |
reed@google.com | 83787c5 | 2013-03-26 17:19:15 +0000 | [diff] [blame] | 2508 | |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2509 | CFStringRef getFamilyNameAt(int index) const { |
reed@google.com | 83787c5 | 2013-03-26 17:19:15 +0000 | [diff] [blame] | 2510 | SkASSERT((unsigned)index < (unsigned)fCount); |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2511 | return (CFStringRef)CFArrayGetValueAtIndex(fNames.get(), index); |
reed@google.com | 83787c5 | 2013-03-26 17:19:15 +0000 | [diff] [blame] | 2512 | } |
| 2513 | |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 2514 | static SkFontStyleSet* CreateSet(CFStringRef cfFamilyName) { |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 2515 | SkUniqueCFRef<CFMutableDictionaryRef> cfAttr( |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 2516 | CFDictionaryCreateMutable(kCFAllocatorDefault, 0, |
| 2517 | &kCFTypeDictionaryKeyCallBacks, |
| 2518 | &kCFTypeDictionaryValueCallBacks)); |
skia.committer@gmail.com | d55846d | 2013-03-30 07:01:27 +0000 | [diff] [blame] | 2519 | |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2520 | CFDictionaryAddValue(cfAttr.get(), kCTFontFamilyNameAttribute, cfFamilyName); |
skia.committer@gmail.com | d55846d | 2013-03-30 07:01:27 +0000 | [diff] [blame] | 2521 | |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 2522 | SkUniqueCFRef<CTFontDescriptorRef> desc( |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2523 | CTFontDescriptorCreateWithAttributes(cfAttr.get())); |
| 2524 | return new SkFontStyleSet_Mac(desc.get()); |
| 2525 | } |
| 2526 | |
| 2527 | /** CTFontManagerCopyAvailableFontFamilyNames() is not always available, so we |
| 2528 | * provide a wrapper here that will return an empty array if need be. |
| 2529 | */ |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 2530 | static SkUniqueCFRef<CFArrayRef> CopyAvailableFontFamilyNames() { |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2531 | #ifdef SK_BUILD_FOR_IOS |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 2532 | return SkUniqueCFRef<CFArrayRef>(CFArrayCreate(nullptr, nullptr, 0, nullptr)); |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2533 | #else |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 2534 | return SkUniqueCFRef<CFArrayRef>(CTFontManagerCopyAvailableFontFamilyNames()); |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2535 | #endif |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 2536 | } |
skia.committer@gmail.com | d55846d | 2013-03-30 07:01:27 +0000 | [diff] [blame] | 2537 | |
reed@google.com | 83787c5 | 2013-03-26 17:19:15 +0000 | [diff] [blame] | 2538 | public: |
commit-bot@chromium.org | 967dee3 | 2014-02-04 22:35:01 +0000 | [diff] [blame] | 2539 | SkFontMgr_Mac() |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2540 | : fNames(CopyAvailableFontFamilyNames()) |
| 2541 | , fCount(fNames ? SkToInt(CFArrayGetCount(fNames.get())) : 0) {} |
reed@google.com | 95625db | 2013-03-25 20:44:02 +0000 | [diff] [blame] | 2542 | |
| 2543 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 2544 | int onCountFamilies() const override { |
reed@google.com | 83787c5 | 2013-03-26 17:19:15 +0000 | [diff] [blame] | 2545 | return fCount; |
reed@google.com | 95625db | 2013-03-25 20:44:02 +0000 | [diff] [blame] | 2546 | } |
| 2547 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 2548 | void onGetFamilyName(int index, SkString* familyName) const override { |
reed@google.com | 83787c5 | 2013-03-26 17:19:15 +0000 | [diff] [blame] | 2549 | if ((unsigned)index < (unsigned)fCount) { |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2550 | CFStringToSkString(this->getFamilyNameAt(index), familyName); |
reed@google.com | 83787c5 | 2013-03-26 17:19:15 +0000 | [diff] [blame] | 2551 | } else { |
| 2552 | familyName->reset(); |
| 2553 | } |
reed@google.com | 95625db | 2013-03-25 20:44:02 +0000 | [diff] [blame] | 2554 | } |
| 2555 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 2556 | SkFontStyleSet* onCreateStyleSet(int index) const override { |
reed@google.com | 83787c5 | 2013-03-26 17:19:15 +0000 | [diff] [blame] | 2557 | if ((unsigned)index >= (unsigned)fCount) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2558 | return nullptr; |
reed@google.com | 83787c5 | 2013-03-26 17:19:15 +0000 | [diff] [blame] | 2559 | } |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2560 | return CreateSet(this->getFamilyNameAt(index)); |
reed@google.com | 95625db | 2013-03-25 20:44:02 +0000 | [diff] [blame] | 2561 | } |
skia.committer@gmail.com | d55846d | 2013-03-30 07:01:27 +0000 | [diff] [blame] | 2562 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 2563 | SkFontStyleSet* onMatchFamily(const char familyName[]) const override { |
bungeman | 7575bb1 | 2017-05-01 13:02:42 -0400 | [diff] [blame] | 2564 | if (!familyName) { |
| 2565 | return nullptr; |
| 2566 | } |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 2567 | SkUniqueCFRef<CFStringRef> cfName = make_CFString(familyName); |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2568 | return CreateSet(cfName.get()); |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 2569 | } |
skia.committer@gmail.com | d55846d | 2013-03-30 07:01:27 +0000 | [diff] [blame] | 2570 | |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2571 | SkTypeface* onMatchFamilyStyle(const char familyName[], |
bungeman | 7575bb1 | 2017-05-01 13:02:42 -0400 | [diff] [blame] | 2572 | const SkFontStyle& style) const override { |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 2573 | SkUniqueCFRef<CTFontDescriptorRef> desc = create_descriptor(familyName, style); |
Mike Reed | 5922739 | 2017-09-26 09:46:08 -0400 | [diff] [blame] | 2574 | return create_from_desc(desc.get()).release(); |
reed@google.com | 95625db | 2013-03-25 20:44:02 +0000 | [diff] [blame] | 2575 | } |
skia.committer@gmail.com | e60ed08 | 2013-03-26 07:01:04 +0000 | [diff] [blame] | 2576 | |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2577 | SkTypeface* onMatchFamilyStyleCharacter(const char familyName[], |
| 2578 | const SkFontStyle& style, |
| 2579 | const char* bcp47[], int bcp47Count, |
| 2580 | SkUnichar character) const override { |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 2581 | SkUniqueCFRef<CTFontDescriptorRef> desc = create_descriptor(familyName, style); |
| 2582 | SkUniqueCFRef<CTFontRef> familyFont(CTFontCreateWithFontDescriptor(desc.get(), 0, nullptr)); |
bungeman | bea9748 | 2016-08-24 08:29:50 -0700 | [diff] [blame] | 2583 | |
| 2584 | // kCFStringEncodingUTF32 is BE unless there is a BOM. |
| 2585 | // Since there is no machine endian option, explicitly state machine endian. |
| 2586 | #ifdef SK_CPU_LENDIAN |
| 2587 | constexpr CFStringEncoding encoding = kCFStringEncodingUTF32LE; |
| 2588 | #else |
| 2589 | constexpr CFStringEncoding encoding = kCFStringEncodingUTF32BE; |
| 2590 | #endif |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 2591 | SkUniqueCFRef<CFStringRef> string(CFStringCreateWithBytes( |
bungeman | bea9748 | 2016-08-24 08:29:50 -0700 | [diff] [blame] | 2592 | kCFAllocatorDefault, reinterpret_cast<const UInt8 *>(&character), sizeof(character), |
| 2593 | encoding, false)); |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2594 | CFRange range = CFRangeMake(0, CFStringGetLength(string.get())); // in UniChar units. |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 2595 | SkUniqueCFRef<CTFontRef> fallbackFont( |
| 2596 | CTFontCreateForString(familyFont.get(), string.get(), range)); |
Mike Reed | 5922739 | 2017-09-26 09:46:08 -0400 | [diff] [blame] | 2597 | return create_from_CTFontRef(std::move(fallbackFont), nullptr, false).release(); |
djsollen | 33068c1 | 2014-11-14 10:52:53 -0800 | [diff] [blame] | 2598 | } |
| 2599 | |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2600 | SkTypeface* onMatchFaceStyle(const SkTypeface* familyMember, |
| 2601 | const SkFontStyle&) const override { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2602 | return nullptr; |
reed@google.com | 95625db | 2013-03-25 20:44:02 +0000 | [diff] [blame] | 2603 | } |
skia.committer@gmail.com | e60ed08 | 2013-03-26 07:01:04 +0000 | [diff] [blame] | 2604 | |
Mike Reed | 5922739 | 2017-09-26 09:46:08 -0400 | [diff] [blame] | 2605 | sk_sp<SkTypeface> onMakeFromData(sk_sp<SkData> data, int ttcIndex) const override { |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 2606 | SkUniqueCFRef<CGDataProviderRef> pr(SkCreateDataProviderFromData(std::move(data))); |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2607 | if (!pr) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2608 | return nullptr; |
reed@google.com | 95625db | 2013-03-25 20:44:02 +0000 | [diff] [blame] | 2609 | } |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 2610 | return create_from_dataProvider(std::move(pr), ttcIndex); |
reed@google.com | 95625db | 2013-03-25 20:44:02 +0000 | [diff] [blame] | 2611 | } |
| 2612 | |
Mike Reed | 5922739 | 2017-09-26 09:46:08 -0400 | [diff] [blame] | 2613 | sk_sp<SkTypeface> onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset> stream, |
| 2614 | int ttcIndex) const override { |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 2615 | SkUniqueCFRef<CGDataProviderRef> pr(SkCreateDataProviderFromStream(std::move(stream))); |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2616 | if (!pr) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2617 | return nullptr; |
reed@google.com | 95625db | 2013-03-25 20:44:02 +0000 | [diff] [blame] | 2618 | } |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 2619 | return create_from_dataProvider(std::move(pr), ttcIndex); |
reed@google.com | 95625db | 2013-03-25 20:44:02 +0000 | [diff] [blame] | 2620 | } |
| 2621 | |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 2622 | /** Creates a dictionary suitable for setting the axes on a CGFont. */ |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 2623 | static SkUniqueCFRef<CFDictionaryRef> copy_axes(CGFontRef cg, const SkFontArguments& args) { |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 2624 | // The CGFont variation data is keyed by name, but lacks the tag. |
bungeman | f6c7107 | 2016-01-21 14:17:47 -0800 | [diff] [blame] | 2625 | // The CTFont variation data is keyed by tag, and also has the name. |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 2626 | // We would like to work with CTFont variations, but creating a CTFont font with |
bungeman | f6c7107 | 2016-01-21 14:17:47 -0800 | [diff] [blame] | 2627 | // CTFont variation dictionary runs into bugs. So use the CTFont variation data |
| 2628 | // to match names to tags to create the appropriate CGFont. |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 2629 | SkUniqueCFRef<CTFontRef> ct(CTFontCreateWithGraphicsFont(cg, 0, nullptr, nullptr)); |
bungeman | bc096bf | 2017-04-25 13:32:50 -0400 | [diff] [blame] | 2630 | // CTFontCopyVariationAxes returns nullptr for CGFontCreateWithDataProvider fonts with |
| 2631 | // macOS 10.10 and iOS 9 or earlier. When this happens, there is no API to provide the tag. |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 2632 | SkUniqueCFRef<CFArrayRef> ctAxes(CTFontCopyVariationAxes(ct.get())); |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 2633 | if (!ctAxes) { |
bungeman | f6c7107 | 2016-01-21 14:17:47 -0800 | [diff] [blame] | 2634 | return nullptr; |
| 2635 | } |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 2636 | CFIndex axisCount = CFArrayGetCount(ctAxes.get()); |
bungeman | f6c7107 | 2016-01-21 14:17:47 -0800 | [diff] [blame] | 2637 | |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 2638 | const SkFontArguments::VariationPosition position = args.getVariationDesignPosition(); |
bungeman | f6c7107 | 2016-01-21 14:17:47 -0800 | [diff] [blame] | 2639 | |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 2640 | SkUniqueCFRef<CFMutableDictionaryRef> dict( |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2641 | CFDictionaryCreateMutable(kCFAllocatorDefault, axisCount, |
| 2642 | &kCFTypeDictionaryKeyCallBacks, |
| 2643 | &kCFTypeDictionaryValueCallBacks)); |
| 2644 | |
bungeman | f6c7107 | 2016-01-21 14:17:47 -0800 | [diff] [blame] | 2645 | for (int i = 0; i < axisCount; ++i) { |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 2646 | CFTypeRef axisInfo = CFArrayGetValueAtIndex(ctAxes.get(), i); |
bungeman | f6c7107 | 2016-01-21 14:17:47 -0800 | [diff] [blame] | 2647 | if (CFDictionaryGetTypeID() != CFGetTypeID(axisInfo)) { |
| 2648 | return nullptr; |
| 2649 | } |
| 2650 | CFDictionaryRef axisInfoDict = static_cast<CFDictionaryRef>(axisInfo); |
| 2651 | |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 2652 | // The assumption is that values produced by kCTFontVariationAxisNameKey and |
| 2653 | // kCGFontVariationAxisName will always be equal. |
| 2654 | // If they are ever not, seach the project history for "get_tag_for_name". |
| 2655 | CFTypeRef axisName = CFDictionaryGetValue(axisInfoDict, kCTFontVariationAxisNameKey); |
bungeman | f6c7107 | 2016-01-21 14:17:47 -0800 | [diff] [blame] | 2656 | if (!axisName || CFGetTypeID(axisName) != CFStringGetTypeID()) { |
| 2657 | return nullptr; |
| 2658 | } |
| 2659 | |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 2660 | CFTypeRef tag = CFDictionaryGetValue(axisInfoDict, kCTFontVariationAxisIdentifierKey); |
| 2661 | if (!tag || CFGetTypeID(tag) != CFNumberGetTypeID()) { |
| 2662 | return nullptr; |
bungeman | f6c7107 | 2016-01-21 14:17:47 -0800 | [diff] [blame] | 2663 | } |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 2664 | CFNumberRef tagNumber = static_cast<CFNumberRef>(tag); |
bungeman | f6c7107 | 2016-01-21 14:17:47 -0800 | [diff] [blame] | 2665 | int64_t tagLong; |
| 2666 | if (!CFNumberGetValue(tagNumber, kCFNumberSInt64Type, &tagLong)) { |
| 2667 | return nullptr; |
| 2668 | } |
| 2669 | |
| 2670 | // The variation axes can be set to any value, but cg will effectively pin them. |
| 2671 | // Pin them here to normalize. |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 2672 | CFTypeRef min = CFDictionaryGetValue(axisInfoDict, kCTFontVariationAxisMinimumValueKey); |
| 2673 | CFTypeRef max = CFDictionaryGetValue(axisInfoDict, kCTFontVariationAxisMaximumValueKey); |
| 2674 | CFTypeRef def = CFDictionaryGetValue(axisInfoDict, kCTFontVariationAxisDefaultValueKey); |
bungeman | f6c7107 | 2016-01-21 14:17:47 -0800 | [diff] [blame] | 2675 | if (!min || CFGetTypeID(min) != CFNumberGetTypeID() || |
| 2676 | !max || CFGetTypeID(max) != CFNumberGetTypeID() || |
| 2677 | !def || CFGetTypeID(def) != CFNumberGetTypeID()) |
| 2678 | { |
| 2679 | return nullptr; |
| 2680 | } |
| 2681 | CFNumberRef minNumber = static_cast<CFNumberRef>(min); |
| 2682 | CFNumberRef maxNumber = static_cast<CFNumberRef>(max); |
| 2683 | CFNumberRef defNumber = static_cast<CFNumberRef>(def); |
| 2684 | double minDouble; |
| 2685 | double maxDouble; |
| 2686 | double defDouble; |
| 2687 | if (!CFNumberGetValue(minNumber, kCFNumberDoubleType, &minDouble) || |
| 2688 | !CFNumberGetValue(maxNumber, kCFNumberDoubleType, &maxDouble) || |
| 2689 | !CFNumberGetValue(defNumber, kCFNumberDoubleType, &defDouble)) |
| 2690 | { |
| 2691 | return nullptr; |
| 2692 | } |
| 2693 | |
| 2694 | double value = defDouble; |
bungeman | 9aec894 | 2017-03-29 13:38:53 -0400 | [diff] [blame] | 2695 | // The position may be over specified. If there are multiple values for a given axis, |
| 2696 | // use the last one since that's what css-fonts-4 requires. |
| 2697 | for (int j = position.coordinateCount; j --> 0;) { |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 2698 | if (position.coordinates[j].axis == tagLong) { |
| 2699 | value = SkTPin(SkScalarToDouble(position.coordinates[j].value), |
| 2700 | minDouble, maxDouble); |
bungeman | f6c7107 | 2016-01-21 14:17:47 -0800 | [diff] [blame] | 2701 | break; |
| 2702 | } |
| 2703 | } |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 2704 | SkUniqueCFRef<CFNumberRef> valueNumber( |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2705 | CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, &value)); |
| 2706 | CFDictionaryAddValue(dict.get(), axisName, valueNumber.get()); |
bungeman | f6c7107 | 2016-01-21 14:17:47 -0800 | [diff] [blame] | 2707 | } |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2708 | return std::move(dict); |
bungeman | f6c7107 | 2016-01-21 14:17:47 -0800 | [diff] [blame] | 2709 | } |
Mike Reed | 5922739 | 2017-09-26 09:46:08 -0400 | [diff] [blame] | 2710 | sk_sp<SkTypeface> onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset> s, |
| 2711 | const SkFontArguments& args) const override { |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 2712 | if (args.getCollectionIndex() != 0) { |
| 2713 | return nullptr; |
| 2714 | } |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 2715 | SkUniqueCFRef<CGDataProviderRef> provider(SkCreateDataProviderFromStream(std::move(s))); |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2716 | if (!provider) { |
bungeman | f6c7107 | 2016-01-21 14:17:47 -0800 | [diff] [blame] | 2717 | return nullptr; |
| 2718 | } |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 2719 | SkUniqueCFRef<CGFontRef> cg(CGFontCreateWithDataProvider(provider.get())); |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2720 | if (!cg) { |
bungeman | f6c7107 | 2016-01-21 14:17:47 -0800 | [diff] [blame] | 2721 | return nullptr; |
| 2722 | } |
| 2723 | |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 2724 | SkUniqueCFRef<CFDictionaryRef> cgVariations = copy_axes(cg.get(), args); |
bungeman | f6c7107 | 2016-01-21 14:17:47 -0800 | [diff] [blame] | 2725 | // The CGFontRef returned by CGFontCreateCopyWithVariations when the passed CGFontRef was |
| 2726 | // created from a data provider does not appear to have any ownership of the underlying |
| 2727 | // data. The original CGFontRef must be kept alive until the copy will no longer be used. |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 2728 | SkUniqueCFRef<CGFontRef> cgVariant; |
bungeman | f6c7107 | 2016-01-21 14:17:47 -0800 | [diff] [blame] | 2729 | if (cgVariations) { |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2730 | cgVariant.reset(CGFontCreateCopyWithVariations(cg.get(), cgVariations.get())); |
bungeman | f6c7107 | 2016-01-21 14:17:47 -0800 | [diff] [blame] | 2731 | } else { |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 2732 | cgVariant.reset(cg.release()); |
bungeman | f6c7107 | 2016-01-21 14:17:47 -0800 | [diff] [blame] | 2733 | } |
| 2734 | |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 2735 | SkUniqueCFRef<CTFontRef> ct( |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2736 | CTFontCreateWithGraphicsFont(cgVariant.get(), 0, nullptr, nullptr)); |
bungeman | f6c7107 | 2016-01-21 14:17:47 -0800 | [diff] [blame] | 2737 | if (!ct) { |
| 2738 | return nullptr; |
| 2739 | } |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2740 | return create_from_CTFontRef(std::move(ct), std::move(cg), true); |
bungeman | f6c7107 | 2016-01-21 14:17:47 -0800 | [diff] [blame] | 2741 | } |
| 2742 | |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 2743 | /** Creates a dictionary suitable for setting the axes on a CGFont. */ |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 2744 | static SkUniqueCFRef<CFDictionaryRef> copy_axes(CGFontRef cg, SkFontData* fontData) { |
| 2745 | SkUniqueCFRef<CFArrayRef> cgAxes(CGFontCopyVariationAxes(cg)); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 2746 | if (!cgAxes) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2747 | return nullptr; |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 2748 | } |
| 2749 | |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2750 | CFIndex axisCount = CFArrayGetCount(cgAxes.get()); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 2751 | if (0 == axisCount || axisCount != fontData->getAxisCount()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2752 | return nullptr; |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 2753 | } |
| 2754 | |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 2755 | SkUniqueCFRef<CFMutableDictionaryRef> dict( |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2756 | CFDictionaryCreateMutable(kCFAllocatorDefault, axisCount, |
| 2757 | &kCFTypeDictionaryKeyCallBacks, |
| 2758 | &kCFTypeDictionaryValueCallBacks)); |
| 2759 | |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 2760 | for (int i = 0; i < fontData->getAxisCount(); ++i) { |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2761 | CFTypeRef axisInfo = CFArrayGetValueAtIndex(cgAxes.get(), i); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 2762 | if (CFDictionaryGetTypeID() != CFGetTypeID(axisInfo)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2763 | return nullptr; |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 2764 | } |
| 2765 | CFDictionaryRef axisInfoDict = static_cast<CFDictionaryRef>(axisInfo); |
| 2766 | |
| 2767 | CFTypeRef axisName = CFDictionaryGetValue(axisInfoDict, kCGFontVariationAxisName); |
| 2768 | if (!axisName || CFGetTypeID(axisName) != CFStringGetTypeID()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2769 | return nullptr; |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 2770 | } |
| 2771 | |
| 2772 | // The variation axes can be set to any value, but cg will effectively pin them. |
| 2773 | // Pin them here to normalize. |
| 2774 | CFTypeRef min = CFDictionaryGetValue(axisInfoDict, kCGFontVariationAxisMinValue); |
| 2775 | CFTypeRef max = CFDictionaryGetValue(axisInfoDict, kCGFontVariationAxisMaxValue); |
| 2776 | if (!min || CFGetTypeID(min) != CFNumberGetTypeID() || |
| 2777 | !max || CFGetTypeID(max) != CFNumberGetTypeID()) |
| 2778 | { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2779 | return nullptr; |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 2780 | } |
| 2781 | CFNumberRef minNumber = static_cast<CFNumberRef>(min); |
| 2782 | CFNumberRef maxNumber = static_cast<CFNumberRef>(max); |
| 2783 | double minDouble; |
| 2784 | double maxDouble; |
| 2785 | if (!CFNumberGetValue(minNumber, kCFNumberDoubleType, &minDouble) || |
| 2786 | !CFNumberGetValue(maxNumber, kCFNumberDoubleType, &maxDouble)) |
| 2787 | { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2788 | return nullptr; |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 2789 | } |
| 2790 | double value = SkTPin(SkFixedToDouble(fontData->getAxis()[i]), minDouble, maxDouble); |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 2791 | SkUniqueCFRef<CFNumberRef> valueNumber( |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2792 | CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, &value)); |
| 2793 | CFDictionaryAddValue(dict.get(), axisName, valueNumber.get()); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 2794 | } |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2795 | return std::move(dict); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 2796 | } |
Mike Reed | 5922739 | 2017-09-26 09:46:08 -0400 | [diff] [blame] | 2797 | sk_sp<SkTypeface> onMakeFromFontData(std::unique_ptr<SkFontData> fontData) const override { |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 2798 | if (fontData->getIndex() != 0) { |
| 2799 | return nullptr; |
| 2800 | } |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 2801 | SkUniqueCFRef<CGDataProviderRef> provider( |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 2802 | SkCreateDataProviderFromStream(fontData->detachStream())); |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2803 | if (!provider) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2804 | return nullptr; |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 2805 | } |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 2806 | SkUniqueCFRef<CGFontRef> cg(CGFontCreateWithDataProvider(provider.get())); |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2807 | if (!cg) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2808 | return nullptr; |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 2809 | } |
| 2810 | |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 2811 | SkUniqueCFRef<CFDictionaryRef> cgVariations = copy_axes(cg.get(), fontData.get()); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 2812 | // The CGFontRef returned by CGFontCreateCopyWithVariations when the passed CGFontRef was |
| 2813 | // created from a data provider does not appear to have any ownership of the underlying |
| 2814 | // data. The original CGFontRef must be kept alive until the copy will no longer be used. |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 2815 | SkUniqueCFRef<CGFontRef> cgVariant; |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 2816 | if (cgVariations) { |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2817 | cgVariant.reset(CGFontCreateCopyWithVariations(cg.get(), cgVariations.get())); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 2818 | } else { |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 2819 | cgVariant.reset(cg.release()); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 2820 | } |
| 2821 | |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 2822 | SkUniqueCFRef<CTFontRef> ct( |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2823 | CTFontCreateWithGraphicsFont(cgVariant.get(), 0, nullptr, nullptr)); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 2824 | if (!ct) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2825 | return nullptr; |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 2826 | } |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2827 | return create_from_CTFontRef(std::move(ct), std::move(cg), true); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 2828 | } |
| 2829 | |
Mike Reed | 5922739 | 2017-09-26 09:46:08 -0400 | [diff] [blame] | 2830 | sk_sp<SkTypeface> onMakeFromFile(const char path[], int ttcIndex) const override { |
Ben Wagner | 7b56229 | 2018-09-19 22:31:07 -0400 | [diff] [blame] | 2831 | SkUniqueCFRef<CGDataProviderRef> pr(CGDataProviderCreateWithFilename(path)); |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2832 | if (!pr) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2833 | return nullptr; |
reed@google.com | 95625db | 2013-03-25 20:44:02 +0000 | [diff] [blame] | 2834 | } |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 2835 | return create_from_dataProvider(std::move(pr), ttcIndex); |
reed@google.com | 95625db | 2013-03-25 20:44:02 +0000 | [diff] [blame] | 2836 | } |
skia.committer@gmail.com | 76015b0 | 2013-07-31 07:01:00 +0000 | [diff] [blame] | 2837 | |
Mike Reed | 5922739 | 2017-09-26 09:46:08 -0400 | [diff] [blame] | 2838 | sk_sp<SkTypeface> onLegacyMakeTypeface(const char familyName[], SkFontStyle style) const override { |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 2839 | if (familyName) { |
| 2840 | familyName = map_css_names(familyName); |
| 2841 | } |
| 2842 | |
Mike Reed | 5922739 | 2017-09-26 09:46:08 -0400 | [diff] [blame] | 2843 | sk_sp<SkTypeface> face = create_from_name(familyName, style); |
bungeman | 53d5c6e | 2016-04-08 07:22:29 -0700 | [diff] [blame] | 2844 | if (face) { |
| 2845 | return face; |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 2846 | } |
bungeman | 53d5c6e | 2016-04-08 07:22:29 -0700 | [diff] [blame] | 2847 | |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2848 | static SkTypeface* gDefaultFace; |
| 2849 | static SkOnce lookupDefault; |
bungeman | 83f1f44 | 2017-02-06 13:21:33 -0500 | [diff] [blame] | 2850 | static const char FONT_DEFAULT_NAME[] = "Lucida Sans"; |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2851 | lookupDefault([]{ |
Mike Reed | 5922739 | 2017-09-26 09:46:08 -0400 | [diff] [blame] | 2852 | gDefaultFace = create_from_name(FONT_DEFAULT_NAME, SkFontStyle()).release(); |
bungeman | c292b5f | 2016-12-20 12:04:29 -0500 | [diff] [blame] | 2853 | }); |
Mike Reed | 5922739 | 2017-09-26 09:46:08 -0400 | [diff] [blame] | 2854 | return sk_ref_sp(gDefaultFace); |
reed@google.com | 7fdcd44 | 2013-07-30 21:25:49 +0000 | [diff] [blame] | 2855 | } |
reed@google.com | 95625db | 2013-03-25 20:44:02 +0000 | [diff] [blame] | 2856 | }; |
| 2857 | |
reed@google.com | 7fdcd44 | 2013-07-30 21:25:49 +0000 | [diff] [blame] | 2858 | /////////////////////////////////////////////////////////////////////////////// |
| 2859 | |
Ben Wagner | 3546ff1 | 2017-01-03 13:32:36 -0500 | [diff] [blame] | 2860 | sk_sp<SkFontMgr> SkFontMgr::Factory() { return sk_make_sp<SkFontMgr_Mac>(); } |
mtklein | 1ee7651 | 2015-11-02 10:20:27 -0800 | [diff] [blame] | 2861 | |
| 2862 | #endif//defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) |