| 1 |
/* |
| 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 |
|
| 8 |
#include "include/core/SkTypes.h" |
| 9 |
#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) |
| 10 |
|
| 11 |
#ifdef SK_BUILD_FOR_MAC |
| 12 |
#import <ApplicationServices/ApplicationServices.h> |
| 13 |
#endif |
| 14 |
|
| 15 |
#ifdef SK_BUILD_FOR_IOS |
| 16 |
#include <CoreText/CoreText.h> |
| 17 |
#include <CoreText/CTFontManager.h> |
| 18 |
#include <CoreGraphics/CoreGraphics.h> |
| 19 |
#include <CoreFoundation/CoreFoundation.h> |
| 20 |
#include <dlfcn.h> |
| 21 |
#endif |
| 22 |
|
| 23 |
#include "include/core/SkData.h" |
| 24 |
#include "include/core/SkFontArguments.h" |
| 25 |
#include "include/core/SkFontMgr.h" |
| 26 |
#include "include/core/SkFontStyle.h" |
| 27 |
#include "include/core/SkStream.h" |
| 28 |
#include "include/core/SkString.h" |
| 29 |
#include "include/core/SkTypeface.h" |
| 30 |
#include "include/ports/SkFontMgr_mac_ct.h" |
| 31 |
#include "include/private/base/SkOnce.h" |
| 32 |
#include "include/private/base/SkTo.h" |
| 33 |
#include "src/base/SkUTF.h" |
| 34 |
#include "src/ports/SkTypeface_mac_ct.h" |
| 35 |
#include "src/utils/mac/SkUniqueCFRef.h" |
| 36 |
|
| 37 |
#include <string.h> |
| 38 |
#include <memory> |
| 39 |
|
| 40 |
using namespace skia_private; |
| 41 |
|
| 42 |
static SkUniqueCFRef<CFStringRef> make_CFString(const char s[]) { |
| 43 |
return SkUniqueCFRef<CFStringRef>(CFStringCreateWithCString(nullptr, s, kCFStringEncodingUTF8)); |
| 44 |
} |
| 45 |
|
| 46 |
/** Creates a typeface from a descriptor, searching the cache. */ |
| 47 |
static sk_sp<SkTypeface> create_from_desc(CTFontDescriptorRef desc) { |
| 48 |
SkUniqueCFRef<CTFontRef> ctFont(CTFontCreateWithFontDescriptor(desc, 0, nullptr)); |
| 49 |
if (!ctFont) { |
| 50 |
return nullptr; |
| 51 |
} |
| 52 |
|
| 53 |
return SkTypeface_Mac::Make(std::move(ctFont), OpszVariation(), nullptr); |
| 54 |
} |
| 55 |
|
| 56 |
static SkUniqueCFRef<CTFontDescriptorRef> create_descriptor(CFStringRef familyName, |
| 57 |
const SkFontStyle& style) { |
| 58 |
SkUniqueCFRef<CFMutableDictionaryRef> cfAttributes( |
| 59 |
CFDictionaryCreateMutable(kCFAllocatorDefault, 0, |
| 60 |
&kCFTypeDictionaryKeyCallBacks, |
| 61 |
&kCFTypeDictionaryValueCallBacks)); |
| 62 |
|
| 63 |
SkUniqueCFRef<CFMutableDictionaryRef> cfTraits( |
| 64 |
CFDictionaryCreateMutable(kCFAllocatorDefault, 0, |
| 65 |
&kCFTypeDictionaryKeyCallBacks, |
| 66 |
&kCFTypeDictionaryValueCallBacks)); |
| 67 |
|
| 68 |
if (!cfAttributes || !cfTraits) { |
| 69 |
return nullptr; |
| 70 |
} |
| 71 |
|
| 72 |
// Setting both kCTFontSymbolicTrait and the specific WWS traits may lead to strange outcomes. |
| 73 |
|
| 74 |
// CTFontTraits (weight) |
| 75 |
CGFloat ctWeight = SkCTFontCTWeightForCSSWeight(style.weight()); |
| 76 |
SkUniqueCFRef<CFNumberRef> cfFontWeight( |
| 77 |
CFNumberCreate(kCFAllocatorDefault, kCFNumberCGFloatType, &ctWeight)); |
| 78 |
if (cfFontWeight) { |
| 79 |
CFDictionaryAddValue(cfTraits.get(), kCTFontWeightTrait, cfFontWeight.get()); |
| 80 |
} |
| 81 |
// CTFontTraits (width) |
| 82 |
CGFloat ctWidth = SkCTFontCTWidthForCSSWidth(style.width()); |
| 83 |
SkUniqueCFRef<CFNumberRef> cfFontWidth( |
| 84 |
CFNumberCreate(kCFAllocatorDefault, kCFNumberCGFloatType, &ctWidth)); |
| 85 |
if (cfFontWidth) { |
| 86 |
CFDictionaryAddValue(cfTraits.get(), kCTFontWidthTrait, cfFontWidth.get()); |
| 87 |
} |
| 88 |
// CTFontTraits (slant) |
| 89 |
// Slope value set to 0.07 based on WebKit's implementation for better font matching |
| 90 |
static const CGFloat kSystemFontItalicSlope = 0.07; |
| 91 |
CGFloat ctSlant = style.slant() == SkFontStyle::kUpright_Slant ? 0 : kSystemFontItalicSlope; |
| 92 |
SkUniqueCFRef<CFNumberRef> cfFontSlant( |
| 93 |
CFNumberCreate(kCFAllocatorDefault, kCFNumberCGFloatType, &ctSlant)); |
| 94 |
if (cfFontSlant) { |
| 95 |
CFDictionaryAddValue(cfTraits.get(), kCTFontSlantTrait, cfFontSlant.get()); |
| 96 |
} |
| 97 |
// CTFontTraits |
| 98 |
CFDictionaryAddValue(cfAttributes.get(), kCTFontTraitsAttribute, cfTraits.get()); |
| 99 |
|
| 100 |
// CTFontFamilyName |
| 101 |
if (familyName) { |
| 102 |
CFDictionaryAddValue(cfAttributes.get(), kCTFontFamilyNameAttribute, familyName); |
| 103 |
} |
| 104 |
|
| 105 |
return SkUniqueCFRef<CTFontDescriptorRef>( |
| 106 |
CTFontDescriptorCreateWithAttributes(cfAttributes.get())); |
| 107 |
} |
| 108 |
|
| 109 |
static SkUniqueCFRef<CTFontDescriptorRef> create_descriptor(const char familyName[], |
| 110 |
const SkFontStyle& style) { |
| 111 |
SkUniqueCFRef<CFStringRef> cfFamilyName; |
| 112 |
if (familyName) { |
| 113 |
cfFamilyName = make_CFString(familyName); |
| 114 |
} |
| 115 |
return create_descriptor(cfFamilyName.get(), style); |
| 116 |
} |
| 117 |
|
| 118 |
// Same as the above function except style is included so we can |
| 119 |
// compare whether the created font conforms to the style. If not, we need |
| 120 |
// to recreate the font with symbolic traits. This is needed due to MacOS 10.11 |
| 121 |
// font creation problem https://bugs.chromium.org/p/skia/issues/detail?id=8447. |
| 122 |
static sk_sp<SkTypeface> create_from_desc_and_style(CTFontDescriptorRef desc, |
| 123 |
const SkFontStyle& style) { |
| 124 |
SkUniqueCFRef<CTFontRef> ctFont(CTFontCreateWithFontDescriptor(desc, 0, nullptr)); |
| 125 |
if (!ctFont) { |
| 126 |
return nullptr; |
| 127 |
} |
| 128 |
|
| 129 |
const CTFontSymbolicTraits traits = CTFontGetSymbolicTraits(ctFont.get()); |
| 130 |
CTFontSymbolicTraits expected_traits = traits; |
| 131 |
if (style.slant() != SkFontStyle::kUpright_Slant) { |
| 132 |
expected_traits |= kCTFontItalicTrait; |
| 133 |
} |
| 134 |
if (style.weight() >= SkFontStyle::kBold_Weight) { |
| 135 |
expected_traits |= kCTFontBoldTrait; |
| 136 |
} |
| 137 |
|
| 138 |
if (expected_traits != traits) { |
| 139 |
SkUniqueCFRef<CTFontRef> ctNewFont(CTFontCreateCopyWithSymbolicTraits( |
| 140 |
ctFont.get(), 0, nullptr, expected_traits, expected_traits)); |
| 141 |
if (ctNewFont) { |
| 142 |
ctFont = std::move(ctNewFont); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
return SkTypeface_Mac::Make(std::move(ctFont), OpszVariation(), nullptr); |
| 147 |
} |
| 148 |
|
| 149 |
/** Creates a typeface from a name, searching the cache. */ |
| 150 |
static sk_sp<SkTypeface> create_from_name(const char familyName[], const SkFontStyle& style) { |
| 151 |
SkUniqueCFRef<CTFontDescriptorRef> desc = create_descriptor(familyName, style); |
| 152 |
if (!desc) { |
| 153 |
return nullptr; |
| 154 |
} |
| 155 |
return create_from_desc_and_style(desc.get(), style); |
| 156 |
} |
| 157 |
|
| 158 |
static const char* map_css_names(const char* name) { |
| 159 |
static const struct { |
| 160 |
const char* fFrom; // name the caller specified |
| 161 |
const char* fTo; // "canonical" name we map to |
| 162 |
} gPairs[] = { |
| 163 |
{ "sans-serif", "Helvetica" }, |
| 164 |
{ "serif", "Times" }, |
| 165 |
{ "monospace", "Courier" } |
| 166 |
}; |
| 167 |
|
| 168 |
for (size_t i = 0; i < std::size(gPairs); i++) { |
| 169 |
if (strcmp(name, gPairs[i].fFrom) == 0) { |
| 170 |
return gPairs[i].fTo; |
| 171 |
} |
| 172 |
} |
| 173 |
return name; // no change |
| 174 |
} |
| 175 |
|
| 176 |
namespace { |
| 177 |
|
| 178 |
static bool find_desc_str(CTFontDescriptorRef desc, CFStringRef name, SkString* value) { |
| 179 |
SkUniqueCFRef<CFStringRef> ref((CFStringRef)CTFontDescriptorCopyAttribute(desc, name)); |
| 180 |
if (!ref) { |
| 181 |
return false; |
| 182 |
} |
| 183 |
SkStringFromCFString(ref.get(), value); |
| 184 |
return true; |
| 185 |
} |
| 186 |
|
| 187 |
static inline int sqr(int value) { |
| 188 |
SkASSERT(SkAbs32(value) < 0x7FFF); // check for overflow |
| 189 |
return value * value; |
| 190 |
} |
| 191 |
|
| 192 |
// We normalize each axis (weight, width, italic) to be base-900 |
| 193 |
static int compute_metric(const SkFontStyle& a, const SkFontStyle& b) { |
| 194 |
return sqr(a.weight() - b.weight()) + |
| 195 |
sqr((a.width() - b.width()) * 100) + |
| 196 |
sqr((a.slant() != b.slant()) * 900); |
| 197 |
} |
| 198 |
|
| 199 |
static SkUniqueCFRef<CFSetRef> name_required() { |
| 200 |
CFStringRef set_values[] = {kCTFontFamilyNameAttribute}; |
| 201 |
return SkUniqueCFRef<CFSetRef>(CFSetCreate(kCFAllocatorDefault, |
| 202 |
reinterpret_cast<const void**>(set_values), std::size(set_values), |
| 203 |
&kCFTypeSetCallBacks)); |
| 204 |
} |
| 205 |
|
| 206 |
class SkFontStyleSet_Mac : public SkFontStyleSet { |
| 207 |
public: |
| 208 |
SkFontStyleSet_Mac(CTFontDescriptorRef desc) |
| 209 |
: fArray(CTFontDescriptorCreateMatchingFontDescriptors(desc, name_required().get())) |
| 210 |
, fCount(0) |
| 211 |
{ |
| 212 |
if (!fArray) { |
| 213 |
fArray.reset(CFArrayCreate(nullptr, nullptr, 0, nullptr)); |
| 214 |
} |
| 215 |
fCount = SkToInt(CFArrayGetCount(fArray.get())); |
| 216 |
} |
| 217 |
|
| 218 |
int count() override { |
| 219 |
return fCount; |
| 220 |
} |
| 221 |
|
| 222 |
void getStyle(int index, SkFontStyle* style, SkString* name) override { |
| 223 |
SkASSERT((unsigned)index < (unsigned)fCount); |
| 224 |
CTFontDescriptorRef desc = (CTFontDescriptorRef)CFArrayGetValueAtIndex(fArray.get(), index); |
| 225 |
if (style) { |
| 226 |
*style = SkCTFontDescriptorGetSkFontStyle(desc, false); |
| 227 |
} |
| 228 |
if (name) { |
| 229 |
if (!find_desc_str(desc, kCTFontStyleNameAttribute, name)) { |
| 230 |
name->reset(); |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
sk_sp<SkTypeface> createTypeface(int index) override { |
| 236 |
SkASSERT((unsigned)index < (unsigned)CFArrayGetCount(fArray.get())); |
| 237 |
CTFontDescriptorRef desc = (CTFontDescriptorRef)CFArrayGetValueAtIndex(fArray.get(), index); |
| 238 |
|
| 239 |
return create_from_desc(desc); |
| 240 |
} |
| 241 |
|
| 242 |
sk_sp<SkTypeface> matchStyle(const SkFontStyle& pattern) override { |
| 243 |
if (0 == fCount) { |
| 244 |
return nullptr; |
| 245 |
} |
| 246 |
return create_from_desc(findMatchingDesc(pattern)); |
| 247 |
} |
| 248 |
|
| 249 |
private: |
| 250 |
SkUniqueCFRef<CFArrayRef> fArray; |
| 251 |
int fCount; |
| 252 |
|
| 253 |
CTFontDescriptorRef findMatchingDesc(const SkFontStyle& pattern) const { |
| 254 |
int bestMetric = SK_MaxS32; |
| 255 |
CTFontDescriptorRef bestDesc = nullptr; |
| 256 |
|
| 257 |
for (int i = 0; i < fCount; ++i) { |
| 258 |
CTFontDescriptorRef desc = (CTFontDescriptorRef)CFArrayGetValueAtIndex(fArray.get(), i); |
| 259 |
int metric = compute_metric(pattern, SkCTFontDescriptorGetSkFontStyle(desc, false)); |
| 260 |
if (0 == metric) { |
| 261 |
return desc; |
| 262 |
} |
| 263 |
if (metric < bestMetric) { |
| 264 |
bestMetric = metric; |
| 265 |
bestDesc = desc; |
| 266 |
} |
| 267 |
} |
| 268 |
SkASSERT(bestDesc); |
| 269 |
return bestDesc; |
| 270 |
} |
| 271 |
}; |
| 272 |
|
| 273 |
SkUniqueCFRef<CFArrayRef> SkCopyAvailableFontFamilyNames(CTFontCollectionRef collection) { |
| 274 |
// Create a CFArray of all available font descriptors. |
| 275 |
SkUniqueCFRef<CFArrayRef> descriptors( |
| 276 |
CTFontCollectionCreateMatchingFontDescriptors(collection)); |
| 277 |
|
| 278 |
// Copy the font family names of the font descriptors into a CFSet. |
| 279 |
auto addDescriptorFamilyNameToSet = [](const void* value, void* context) -> void { |
| 280 |
CTFontDescriptorRef descriptor = static_cast<CTFontDescriptorRef>(value); |
| 281 |
CFMutableSetRef familyNameSet = static_cast<CFMutableSetRef>(context); |
| 282 |
SkUniqueCFRef<CFTypeRef> familyName( |
| 283 |
CTFontDescriptorCopyAttribute(descriptor, kCTFontFamilyNameAttribute)); |
| 284 |
if (familyName) { |
| 285 |
CFSetAddValue(familyNameSet, familyName.get()); |
| 286 |
} |
| 287 |
}; |
| 288 |
SkUniqueCFRef<CFMutableSetRef> familyNameSet( |
| 289 |
CFSetCreateMutable(kCFAllocatorDefault, 0, &kCFTypeSetCallBacks)); |
| 290 |
CFArrayApplyFunction(descriptors.get(), CFRangeMake(0, CFArrayGetCount(descriptors.get())), |
| 291 |
addDescriptorFamilyNameToSet, familyNameSet.get()); |
| 292 |
|
| 293 |
// Get the set of family names into an array; this does not retain. |
| 294 |
CFIndex count = CFSetGetCount(familyNameSet.get()); |
| 295 |
std::unique_ptr<const void*[]> familyNames(new const void*[count]); |
| 296 |
CFSetGetValues(familyNameSet.get(), familyNames.get()); |
| 297 |
|
| 298 |
// Sort the array of family names (to match CTFontManagerCopyAvailableFontFamilyNames). |
| 299 |
std::sort(familyNames.get(), familyNames.get() + count, [](const void* a, const void* b){ |
| 300 |
return CFStringCompare((CFStringRef)a, (CFStringRef)b, 0) == kCFCompareLessThan; |
| 301 |
}); |
| 302 |
|
| 303 |
// Copy family names into a CFArray; this does retain. |
| 304 |
return SkUniqueCFRef<CFArrayRef>( |
| 305 |
CFArrayCreate(kCFAllocatorDefault, familyNames.get(), count, &kCFTypeArrayCallBacks)); |
| 306 |
} |
| 307 |
|
| 308 |
/** Use CTFontManagerCopyAvailableFontFamilyNames if available, simulate if not. */ |
| 309 |
SkUniqueCFRef<CFArrayRef> SkCTFontManagerCopyAvailableFontFamilyNames() { |
| 310 |
#ifdef SK_BUILD_FOR_IOS |
| 311 |
using CTFontManagerCopyAvailableFontFamilyNamesProc = CFArrayRef (*)(void); |
| 312 |
CTFontManagerCopyAvailableFontFamilyNamesProc ctFontManagerCopyAvailableFontFamilyNames; |
| 313 |
*(void**)(&ctFontManagerCopyAvailableFontFamilyNames) = |
| 314 |
dlsym(RTLD_DEFAULT, "CTFontManagerCopyAvailableFontFamilyNames"); |
| 315 |
if (ctFontManagerCopyAvailableFontFamilyNames) { |
| 316 |
return SkUniqueCFRef<CFArrayRef>(ctFontManagerCopyAvailableFontFamilyNames()); |
| 317 |
} |
| 318 |
SkUniqueCFRef<CTFontCollectionRef> collection( |
| 319 |
CTFontCollectionCreateFromAvailableFonts(nullptr)); |
| 320 |
return SkUniqueCFRef<CFArrayRef>(SkCopyAvailableFontFamilyNames(collection.get())); |
| 321 |
#else |
| 322 |
return SkUniqueCFRef<CFArrayRef>(CTFontManagerCopyAvailableFontFamilyNames()); |
| 323 |
#endif |
| 324 |
} |
| 325 |
|
| 326 |
} // namespace |
| 327 |
|
| 328 |
class SkFontMgr_Mac : public SkFontMgr { |
| 329 |
SkUniqueCFRef<CFArrayRef> fNames; |
| 330 |
int fCount; |
| 331 |
|
| 332 |
CFStringRef getFamilyNameAt(int index) const { |
| 333 |
SkASSERT((unsigned)index < (unsigned)fCount); |
| 334 |
return (CFStringRef)CFArrayGetValueAtIndex(fNames.get(), index); |
| 335 |
} |
| 336 |
|
| 337 |
static sk_sp<SkFontStyleSet> CreateSet(CFStringRef cfFamilyName) { |
| 338 |
SkUniqueCFRef<CFMutableDictionaryRef> cfAttr( |
| 339 |
CFDictionaryCreateMutable(kCFAllocatorDefault, 0, |
| 340 |
&kCFTypeDictionaryKeyCallBacks, |
| 341 |
&kCFTypeDictionaryValueCallBacks)); |
| 342 |
|
| 343 |
CFDictionaryAddValue(cfAttr.get(), kCTFontFamilyNameAttribute, cfFamilyName); |
| 344 |
|
| 345 |
SkUniqueCFRef<CTFontDescriptorRef> desc( |
| 346 |
CTFontDescriptorCreateWithAttributes(cfAttr.get())); |
| 347 |
return sk_sp<SkFontStyleSet>(new SkFontStyleSet_Mac(desc.get())); |
| 348 |
} |
| 349 |
|
| 350 |
public: |
| 351 |
SkUniqueCFRef<CTFontCollectionRef> fFontCollection; |
| 352 |
SkFontMgr_Mac(CTFontCollectionRef fontCollection) |
| 353 |
: fNames(fontCollection ? SkCopyAvailableFontFamilyNames(fontCollection) |
| 354 |
: SkCTFontManagerCopyAvailableFontFamilyNames()) |
| 355 |
, fCount(fNames ? SkToInt(CFArrayGetCount(fNames.get())) : 0) |
| 356 |
, fFontCollection(fontCollection ? (CTFontCollectionRef)CFRetain(fontCollection) |
| 357 |
: CTFontCollectionCreateFromAvailableFonts(nullptr)) |
| 358 |
{} |
| 359 |
|
| 360 |
protected: |
| 361 |
int onCountFamilies() const override { |
| 362 |
return fCount; |
| 363 |
} |
| 364 |
|
| 365 |
void onGetFamilyName(int index, SkString* familyName) const override { |
| 366 |
if ((unsigned)index < (unsigned)fCount) { |
| 367 |
SkStringFromCFString(this->getFamilyNameAt(index), familyName); |
| 368 |
} else { |
| 369 |
familyName->reset(); |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
sk_sp<SkFontStyleSet> onCreateStyleSet(int index) const override { |
| 374 |
if ((unsigned)index >= (unsigned)fCount) { |
| 375 |
return nullptr; |
| 376 |
} |
| 377 |
return CreateSet(this->getFamilyNameAt(index)); |
| 378 |
} |
| 379 |
|
| 380 |
sk_sp<SkFontStyleSet> onMatchFamily(const char familyName[]) const override { |
| 381 |
if (!familyName) { |
| 382 |
return nullptr; |
| 383 |
} |
| 384 |
SkUniqueCFRef<CFStringRef> cfName = make_CFString(familyName); |
| 385 |
return CreateSet(cfName.get()); |
| 386 |
} |
| 387 |
|
| 388 |
sk_sp<SkTypeface> onMatchFamilyStyle(const char familyName[], |
| 389 |
const SkFontStyle& style) const override { |
| 390 |
SkUniqueCFRef<CTFontDescriptorRef> reqDesc = create_descriptor(familyName, style); |
| 391 |
if (!reqDesc) { |
| 392 |
return nullptr; |
| 393 |
} |
| 394 |
if (!familyName) { |
| 395 |
return create_from_desc(reqDesc.get()); |
| 396 |
} |
| 397 |
SkUniqueCFRef<CTFontDescriptorRef> resolvedDesc( |
| 398 |
CTFontDescriptorCreateMatchingFontDescriptor(reqDesc.get(), name_required().get())); |
| 399 |
if (!resolvedDesc) { |
| 400 |
return nullptr; |
| 401 |
} |
| 402 |
return create_from_desc(resolvedDesc.get()); |
| 403 |
} |
| 404 |
|
| 405 |
static bool has_character(CTFontRef ctFont, SkUnichar character) { |
| 406 |
UniChar utf16[2] = {}; // UniChar is a UTF-16 16-bit code unit. |
| 407 |
CGGlyph glyph[2] = {}; |
| 408 |
int srcCount = SkUTF::ToUTF16(character, utf16); |
| 409 |
return CTFontGetGlyphsForCharacters(ctFont, utf16, glyph, srcCount); |
| 410 |
} |
| 411 |
sk_sp<SkTypeface> onMatchFamilyStyleCharacter(const char familyName[], |
| 412 |
const SkFontStyle& style, |
| 413 |
const char* bcp47[], int bcp47Count, |
| 414 |
SkUnichar character) const override { |
| 415 |
SkUniqueCFRef<CTFontDescriptorRef> desc = create_descriptor(familyName, style); |
| 416 |
if (!desc) { |
| 417 |
return nullptr; |
| 418 |
} |
| 419 |
SkUniqueCFRef<CTFontRef> familyFont(CTFontCreateWithFontDescriptor(desc.get(), 0, nullptr)); |
| 420 |
|
| 421 |
// kCFStringEncodingUTF32 is BE unless there is a BOM. |
| 422 |
// Since there is no machine endian option, explicitly state machine endian. |
| 423 |
#ifdef SK_CPU_LENDIAN |
| 424 |
constexpr CFStringEncoding encoding = kCFStringEncodingUTF32LE; |
| 425 |
#else |
| 426 |
constexpr CFStringEncoding encoding = kCFStringEncodingUTF32BE; |
| 427 |
#endif |
| 428 |
SkUniqueCFRef<CFStringRef> string(CFStringCreateWithBytes( |
| 429 |
kCFAllocatorDefault, reinterpret_cast<const UInt8 *>(&character), sizeof(character), |
| 430 |
encoding, false)); |
| 431 |
// If 0xD800 <= codepoint <= 0xDFFF || 0x10FFFF < codepoint 'string' may be nullptr. |
| 432 |
// No font should be covering such codepoints (even the magic fallback font). |
| 433 |
if (!string) { |
| 434 |
return nullptr; |
| 435 |
} |
| 436 |
CFRange range = CFRangeMake(0, CFStringGetLength(string.get())); // in UniChar units. |
| 437 |
|
| 438 |
const char* locale = bcp47Count ? bcp47[0] : ""; |
| 439 |
SkUniqueCFRef<CFStringRef> cfLocale(CFStringCreateWithBytes( |
| 440 |
kCFAllocatorDefault, reinterpret_cast<const UInt8 *>(locale), strlen(locale), |
| 441 |
kCFStringEncodingUTF8, false)); |
| 442 |
|
| 443 |
SkUniqueCFRef<CTFontRef> fallbackFont(CTFontCreateForStringWithLanguage( |
| 444 |
familyFont.get(), string.get(), range, cfLocale.get())); |
| 445 |
if (!fallbackFont || !has_character(fallbackFont.get(), character)) { |
| 446 |
return nullptr; |
| 447 |
} |
| 448 |
|
| 449 |
// The fallbackFont has the character but there may be a better style match since familyName |
| 450 |
// may have had different weights available than the fallbackFont's family. |
| 451 |
// Check to see if there is a better match with the fallbackFont's family name. |
| 452 |
|
| 453 |
// This is a good example of why fallback must be collection and not font based. |
| 454 |
// Being font based requires that fallback start from a font which, because it is resolved, |
| 455 |
// has lost request information. When fallback is on the collection then the request can be |
| 456 |
// made directly. |
| 457 |
|
| 458 |
// Because collection fallback does not appear to exist in CoreText and there is no direct |
| 459 |
// notion of families, it appears one must pretend like the family name is the family key |
| 460 |
// and try to make that work. This is generally very bad idea (one should never take the |
| 461 |
// family name from a font and then do a lookup based on it) but in this case there appears |
| 462 |
// to be no good work-around. Use of CTFontCreateCopyWithAttributes will do the match with |
| 463 |
// the font name (PostScript name) and so never find a different font. In any event, there |
| 464 |
// is a known backup (just return fallbackFont) if the lookup by family name does not work |
| 465 |
// or the result doesn't actually cover the character. |
| 466 |
SkUniqueCFRef<CFStringRef> fallbackFamilyName(CTFontCopyFamilyName(fallbackFont.get())); |
| 467 |
if (fallbackFamilyName && |
| 468 |
CFStringGetLength(fallbackFamilyName.get()) > 0 && |
| 469 |
CFStringGetCharacterAtIndex(fallbackFamilyName.get(), 0) != '.') |
| 470 |
{ |
| 471 |
SkUniqueCFRef<CTFontDescriptorRef> styledFallbackDesc = |
| 472 |
create_descriptor(fallbackFamilyName.get(), style); |
| 473 |
if (styledFallbackDesc) { |
| 474 |
SkUniqueCFRef<CTFontRef> styledFallbackFont( |
| 475 |
CTFontCreateWithFontDescriptor(styledFallbackDesc.get(), 0, nullptr)); |
| 476 |
if (styledFallbackFont && has_character(styledFallbackFont.get(), character)) { |
| 477 |
fallbackFont = std::move(styledFallbackFont); |
| 478 |
} |
| 479 |
} |
| 480 |
} |
| 481 |
|
| 482 |
return SkTypeface_Mac::Make(std::move(fallbackFont), OpszVariation(), nullptr); |
| 483 |
} |
| 484 |
|
| 485 |
sk_sp<SkTypeface> onMakeFromData(sk_sp<SkData> data, int ttcIndex) const override { |
| 486 |
return this->makeFromStream( |
| 487 |
std::unique_ptr<SkStreamAsset>(new SkMemoryStream(std::move(data))), ttcIndex); |
| 488 |
} |
| 489 |
|
| 490 |
sk_sp<SkTypeface> onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset> stream, |
| 491 |
int ttcIndex) const override { |
| 492 |
return this->makeFromStream(std::move(stream), |
| 493 |
SkFontArguments().setCollectionIndex(ttcIndex)); |
| 494 |
} |
| 495 |
|
| 496 |
sk_sp<SkTypeface> onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset> stream, |
| 497 |
const SkFontArguments& args) const override { |
| 498 |
return SkTypeface_Mac::MakeFromStream(std::move(stream), args); |
| 499 |
} |
| 500 |
|
| 501 |
sk_sp<SkTypeface> onMakeFromFile(const char path[], int ttcIndex) const override { |
| 502 |
sk_sp<SkData> data = SkData::MakeFromFileName(path); |
| 503 |
if (!data) { |
| 504 |
return nullptr; |
| 505 |
} |
| 506 |
|
| 507 |
return this->onMakeFromData(std::move(data), ttcIndex); |
| 508 |
} |
| 509 |
|
| 510 |
sk_sp<SkTypeface> onLegacyMakeTypeface(const char familyName[], SkFontStyle style) const override { |
| 511 |
if (familyName) { |
| 512 |
familyName = map_css_names(familyName); |
| 513 |
} |
| 514 |
|
| 515 |
sk_sp<SkTypeface> face = create_from_name(familyName, style); |
| 516 |
if (face) { |
| 517 |
return face; |
| 518 |
} |
| 519 |
|
| 520 |
static SkTypeface* gDefaultFace; |
| 521 |
static SkOnce lookupDefault; |
| 522 |
static const char FONT_DEFAULT_NAME[] = "Lucida Sans"; |
| 523 |
lookupDefault([]{ |
| 524 |
gDefaultFace = create_from_name(FONT_DEFAULT_NAME, SkFontStyle()).release(); |
| 525 |
}); |
| 526 |
return sk_ref_sp(gDefaultFace); |
| 527 |
} |
| 528 |
}; |
| 529 |
|
| 530 |
sk_sp<SkFontMgr> SkFontMgr_New_CoreText(CTFontCollectionRef fontCollection) { |
| 531 |
return sk_make_sp<SkFontMgr_Mac>(fontCollection); |
| 532 |
} |
| 533 |
|
| 534 |
#endif//defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) |