Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(268)

Side by Side Diff: src/ports/SkFontHost_mac.cpp

Issue 1163573007: Provide tag-along for SkCreateTypefaceFromCTFont. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Add reference to bug. Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « include/ports/SkTypeface_mac.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 1
2 /* 2 /*
3 * Copyright 2006 The Android Open Source Project 3 * Copyright 2006 The Android Open Source Project
4 * 4 *
5 * Use of this source code is governed by a BSD-style license that can be 5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file. 6 * found in the LICENSE file.
7 */ 7 */
8 8
9 #include "SkTypes.h" // Keep this before any #ifdef ... 9 #include "SkTypes.h" // Keep this before any #ifdef ...
10 10
(...skipping 409 matching lines...) Expand 10 before | Expand all | Expand 10 after
420 // kCTFontColorGlyphsTrait was added in the Mac 10.7 and iPhone 4.3 SDKs. 420 // kCTFontColorGlyphsTrait was added in the Mac 10.7 and iPhone 4.3 SDKs.
421 // Being an enum value it is not guarded by version macros, but old SDKs must st ill be supported. 421 // Being an enum value it is not guarded by version macros, but old SDKs must st ill be supported.
422 #if defined(__MAC_10_7) || defined(__IPHONE_4_3) 422 #if defined(__MAC_10_7) || defined(__IPHONE_4_3)
423 static const uint32_t SkCTFontColorGlyphsTrait = kCTFontColorGlyphsTrait; 423 static const uint32_t SkCTFontColorGlyphsTrait = kCTFontColorGlyphsTrait;
424 #else 424 #else
425 static const uint32_t SkCTFontColorGlyphsTrait = (1 << 13); 425 static const uint32_t SkCTFontColorGlyphsTrait = (1 << 13);
426 #endif 426 #endif
427 427
428 class SkTypeface_Mac : public SkTypeface { 428 class SkTypeface_Mac : public SkTypeface {
429 public: 429 public:
430 SkTypeface_Mac(const SkFontStyle& fs, bool isFixedPitch, 430 SkTypeface_Mac(CTFontRef fontRef, CFTypeRef resourceRef,
431 CTFontRef fontRef, const char requestedName[], bool isLocalSt ream, 431 const SkFontStyle& fs, bool isFixedPitch,
432 CGFontRef originatingCGFontRef = NULL) 432 const char requestedName[], bool isLocalStream)
433 : SkTypeface(fs, SkTypefaceCache::NewFontID(), isFixedPitch) 433 : SkTypeface(fs, SkTypefaceCache::NewFontID(), isFixedPitch)
434 , fRequestedName(requestedName) 434 , fRequestedName(requestedName)
435 , fFontRef(fontRef) // caller has already called CFRetain for us 435 , fFontRef(fontRef) // caller has already called CFRetain for us
436 , fOriginatingCGFontRef(originatingCGFontRef) 436 , fOriginatingCFTypeRef(resourceRef) // caller has already called CFReta in for us
437 , fHasColorGlyphs(SkToBool(CTFontGetSymbolicTraits(fFontRef) & SkCTFontC olorGlyphsTrait)) 437 , fHasColorGlyphs(SkToBool(CTFontGetSymbolicTraits(fFontRef) & SkCTFontC olorGlyphsTrait))
438 , fIsLocalStream(isLocalStream) 438 , fIsLocalStream(isLocalStream)
439 { 439 {
440 SkASSERT(fontRef); 440 SkASSERT(fontRef);
441 } 441 }
442 442
443 SkString fRequestedName; 443 SkString fRequestedName;
444 AutoCFRelease<CTFontRef> fFontRef; 444 AutoCFRelease<CTFontRef> fFontRef;
445 AutoCFRelease<CGFontRef> fOriginatingCGFontRef; 445 AutoCFRelease<CFTypeRef> fOriginatingCFTypeRef;
446 const bool fHasColorGlyphs; 446 const bool fHasColorGlyphs;
447 447
448 protected: 448 protected:
449 int onGetUPEM() const override; 449 int onGetUPEM() const override;
450 SkStreamAsset* onOpenStream(int* ttcIndex) const override; 450 SkStreamAsset* onOpenStream(int* ttcIndex) const override;
451 SkFontData* onCreateFontData() const override; 451 SkFontData* onCreateFontData() const override;
452 void onGetFamilyName(SkString* familyName) const override; 452 void onGetFamilyName(SkString* familyName) const override;
453 SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const override; 453 SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const override;
454 int onGetTableTags(SkFontTableTag tags[]) const override; 454 int onGetTableTags(SkFontTableTag tags[]) const override;
455 virtual size_t onGetTableData(SkFontTableTag, size_t offset, 455 virtual size_t onGetTableData(SkFontTableTag, size_t offset,
456 size_t length, void* data) const override; 456 size_t length, void* data) const override;
457 SkScalerContext* onCreateScalerContext(const SkDescriptor*) const override; 457 SkScalerContext* onCreateScalerContext(const SkDescriptor*) const override;
458 void onFilterRec(SkScalerContextRec*) const override; 458 void onFilterRec(SkScalerContextRec*) const override;
459 void onGetFontDescriptor(SkFontDescriptor*, bool*) const override; 459 void onGetFontDescriptor(SkFontDescriptor*, bool*) const override;
460 virtual SkAdvancedTypefaceMetrics* onGetAdvancedTypefaceMetrics( 460 virtual SkAdvancedTypefaceMetrics* onGetAdvancedTypefaceMetrics(
461 PerGlyphInfo, 461 PerGlyphInfo,
462 const uint32_t*, uint32_t) const override; 462 const uint32_t*, uint32_t) const override;
463 virtual int onCharsToGlyphs(const void* chars, Encoding, uint16_t glyphs[], 463 virtual int onCharsToGlyphs(const void* chars, Encoding, uint16_t glyphs[],
464 int glyphCount) const override; 464 int glyphCount) const override;
465 int onCountGlyphs() const override; 465 int onCountGlyphs() const override;
466 466
467 private: 467 private:
468 bool fIsLocalStream; 468 bool fIsLocalStream;
469 469
470 typedef SkTypeface INHERITED; 470 typedef SkTypeface INHERITED;
471 }; 471 };
472 472
473 /** Creates a typeface without searching the cache. Takes ownership of the CTFon tRef. */ 473 /** Creates a typeface without searching the cache. Takes ownership of the CTFon tRef. */
474 static SkTypeface* NewFromFontRef(CTFontRef fontRef, const char name[], bool isL ocalStream, 474 static SkTypeface* NewFromFontRef(CTFontRef fontRef, CFTypeRef resourceRef,
475 CGFontRef originatingCGFontRef = NULL) 475 const char name[], bool isLocalStream)
476 { 476 {
477 SkASSERT(fontRef); 477 SkASSERT(fontRef);
478 bool isFixedPitch; 478 bool isFixedPitch;
479 SkFontStyle style = SkFontStyle(computeStyleBits(fontRef, &isFixedPitch)); 479 SkFontStyle style = SkFontStyle(computeStyleBits(fontRef, &isFixedPitch));
480 480
481 return new SkTypeface_Mac(style, isFixedPitch, fontRef, name, isLocalStream, 481 return new SkTypeface_Mac(fontRef, resourceRef, style, isFixedPitch, name, i sLocalStream);
482 originatingCGFontRef);
483 } 482 }
484 483
485 static bool find_by_CTFontRef(SkTypeface* cached, const SkFontStyle&, void* cont ext) { 484 static bool find_by_CTFontRef(SkTypeface* cached, const SkFontStyle&, void* cont ext) {
486 CTFontRef self = (CTFontRef)context; 485 CTFontRef self = (CTFontRef)context;
487 CTFontRef other = ((SkTypeface_Mac*)cached)->fFontRef; 486 CTFontRef other = ((SkTypeface_Mac*)cached)->fFontRef;
488 487
489 return CFEqual(self, other); 488 return CFEqual(self, other);
490 } 489 }
491 490
492 /** Creates a typeface from a name, searching the cache. */ 491 /** Creates a typeface from a name, searching the cache. */
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
532 return NULL; 531 return NULL;
533 } 532 }
534 533
535 AutoCFRelease<CTFontRef> ctFont(CTFontCreateWithFontDescriptor(ctFontDesc, 0 , NULL)); 534 AutoCFRelease<CTFontRef> ctFont(CTFontCreateWithFontDescriptor(ctFontDesc, 0 , NULL));
536 if (!ctFont) { 535 if (!ctFont) {
537 return NULL; 536 return NULL;
538 } 537 }
539 538
540 SkTypeface* face = SkTypefaceCache::FindByProcAndRef(find_by_CTFontRef, (voi d*)ctFont.get()); 539 SkTypeface* face = SkTypefaceCache::FindByProcAndRef(find_by_CTFontRef, (voi d*)ctFont.get());
541 if (!face) { 540 if (!face) {
542 face = NewFromFontRef(ctFont.detach(), NULL, false); 541 face = NewFromFontRef(ctFont.detach(), NULL, NULL, false);
543 SkTypefaceCache::Add(face, face->fontStyle()); 542 SkTypefaceCache::Add(face, face->fontStyle());
544 } 543 }
545 return face; 544 return face;
546 } 545 }
547 546
548 SK_DECLARE_STATIC_MUTEX(gGetDefaultFaceMutex); 547 SK_DECLARE_STATIC_MUTEX(gGetDefaultFaceMutex);
549 static SkTypeface* GetDefaultFace() { 548 static SkTypeface* GetDefaultFace() {
550 SkAutoMutexAcquire ma(gGetDefaultFaceMutex); 549 SkAutoMutexAcquire ma(gGetDefaultFaceMutex);
551 550
552 static SkTypeface* gDefaultFace; 551 static SkTypeface* gDefaultFace;
553 552
554 if (NULL == gDefaultFace) { 553 if (NULL == gDefaultFace) {
555 gDefaultFace = NewFromName(FONT_DEFAULT_NAME, SkFontStyle()); 554 gDefaultFace = NewFromName(FONT_DEFAULT_NAME, SkFontStyle());
556 SkTypefaceCache::Add(gDefaultFace, SkFontStyle()); 555 SkTypefaceCache::Add(gDefaultFace, SkFontStyle());
557 } 556 }
558 return gDefaultFace; 557 return gDefaultFace;
559 } 558 }
560 559
561 /////////////////////////////////////////////////////////////////////////////// 560 ///////////////////////////////////////////////////////////////////////////////
562 561
563 extern CTFontRef SkTypeface_GetCTFontRef(const SkTypeface* face); 562 extern CTFontRef SkTypeface_GetCTFontRef(const SkTypeface* face);
564 CTFontRef SkTypeface_GetCTFontRef(const SkTypeface* face) { 563 CTFontRef SkTypeface_GetCTFontRef(const SkTypeface* face) {
565 const SkTypeface_Mac* macface = (const SkTypeface_Mac*)face; 564 const SkTypeface_Mac* macface = (const SkTypeface_Mac*)face;
566 return macface ? macface->fFontRef.get() : NULL; 565 return macface ? macface->fFontRef.get() : NULL;
567 } 566 }
568 567
569 /* This function is visible on the outside. It first searches the cache, and if 568 /* This function is visible on the outside. It first searches the cache, and if
570 * not found, returns a new entry (after adding it to the cache). 569 * not found, returns a new entry (after adding it to the cache).
571 */ 570 */
572 SkTypeface* SkCreateTypefaceFromCTFont(CTFontRef fontRef) { 571 SkTypeface* SkCreateTypefaceFromCTFont(CTFontRef fontRef, CFTypeRef resourceRef) {
573 SkTypeface* face = SkTypefaceCache::FindByProcAndRef(find_by_CTFontRef, (voi d*)fontRef); 572 SkTypeface* face = SkTypefaceCache::FindByProcAndRef(find_by_CTFontRef, (voi d*)fontRef);
574 if (!face) { 573 if (!face) {
575 CFRetain(fontRef); 574 CFRetain(fontRef);
576 face = NewFromFontRef(fontRef, NULL, false); 575 if (resourceRef) {
576 CFRetain(resourceRef);
577 }
578 face = NewFromFontRef(fontRef, resourceRef, NULL, false);
577 SkTypefaceCache::Add(face, face->fontStyle()); 579 SkTypefaceCache::Add(face, face->fontStyle());
578 } 580 }
579 return face; 581 return face;
580 } 582 }
581 583
582 struct NameStyle { 584 struct NameStyle {
583 const char* fName; 585 const char* fName;
584 SkFontStyle fStyle; 586 SkFontStyle fStyle;
585 }; 587 };
586 588
(...skipping 865 matching lines...) Expand 10 before | Expand all | Expand 10 after
1452 /////////////////////////////////////////////////////////////////////////////// 1454 ///////////////////////////////////////////////////////////////////////////////
1453 1455
1454 // Returns NULL on failure 1456 // Returns NULL on failure
1455 // Call must still manage its ownership of provider 1457 // Call must still manage its ownership of provider
1456 static SkTypeface* create_from_dataProvider(CGDataProviderRef provider) { 1458 static SkTypeface* create_from_dataProvider(CGDataProviderRef provider) {
1457 AutoCFRelease<CGFontRef> cg(CGFontCreateWithDataProvider(provider)); 1459 AutoCFRelease<CGFontRef> cg(CGFontCreateWithDataProvider(provider));
1458 if (NULL == cg) { 1460 if (NULL == cg) {
1459 return NULL; 1461 return NULL;
1460 } 1462 }
1461 CTFontRef ct = CTFontCreateWithGraphicsFont(cg, 0, NULL, NULL); 1463 CTFontRef ct = CTFontCreateWithGraphicsFont(cg, 0, NULL, NULL);
1462 return ct ? NewFromFontRef(ct, NULL, true) : NULL; 1464 return ct ? NewFromFontRef(ct, NULL, NULL, true) : NULL;
1463 } 1465 }
1464 1466
1465 // Web fonts added to the the CTFont registry do not return their character set. 1467 // Web fonts added to the the CTFont registry do not return their character set.
1466 // Iterate through the font in this case. The existing caller caches the result, 1468 // Iterate through the font in this case. The existing caller caches the result,
1467 // so the performance impact isn't too bad. 1469 // so the performance impact isn't too bad.
1468 static void populate_glyph_to_unicode_slow(CTFontRef ctFont, CFIndex glyphCount, 1470 static void populate_glyph_to_unicode_slow(CTFontRef ctFont, CFIndex glyphCount,
1469 SkTDArray<SkUnichar>* glyphToUnicode) { 1471 SkTDArray<SkUnichar>* glyphToUnicode) {
1470 glyphToUnicode->setCount(SkToInt(glyphCount)); 1472 glyphToUnicode->setCount(SkToInt(glyphCount));
1471 SkUnichar* out = glyphToUnicode->begin(); 1473 SkUnichar* out = glyphToUnicode->begin();
1472 sk_bzero(out, glyphCount * sizeof(SkUnichar)); 1474 sk_bzero(out, glyphCount * sizeof(SkUnichar));
(...skipping 715 matching lines...) Expand 10 before | Expand all | Expand 10 after
2188 } 2190 }
2189 2191
2190 AutoCFRelease<CTFontRef> ctFont(CTFontCreateWithFontDescriptor(desc, 0, NULL )); 2192 AutoCFRelease<CTFontRef> ctFont(CTFontCreateWithFontDescriptor(desc, 0, NULL ));
2191 if (!ctFont) { 2193 if (!ctFont) {
2192 return NULL; 2194 return NULL;
2193 } 2195 }
2194 2196
2195 bool isFixedPitch; 2197 bool isFixedPitch;
2196 (void)computeStyleBits(ctFont, &isFixedPitch); 2198 (void)computeStyleBits(ctFont, &isFixedPitch);
2197 2199
2198 face = SkNEW_ARGS(SkTypeface_Mac, (cacheRequest.fStyle, isFixedPitch, 2200 face = SkNEW_ARGS(SkTypeface_Mac, (ctFont.detach(), NULL,
2199 ctFont.detach(), skFamilyName.c_str(), fa lse)); 2201 cacheRequest.fStyle, isFixedPitch,
2202 skFamilyName.c_str(), false));
2200 SkTypefaceCache::Add(face, face->fontStyle()); 2203 SkTypefaceCache::Add(face, face->fontStyle());
2201 return face; 2204 return face;
2202 } 2205 }
2203 2206
2204 class SkFontStyleSet_Mac : public SkFontStyleSet { 2207 class SkFontStyleSet_Mac : public SkFontStyleSet {
2205 public: 2208 public:
2206 SkFontStyleSet_Mac(CFStringRef familyName, CTFontDescriptorRef desc) 2209 SkFontStyleSet_Mac(CFStringRef familyName, CTFontDescriptorRef desc)
2207 : fArray(CTFontDescriptorCreateMatchingFontDescriptors(desc, NULL)) 2210 : fArray(CTFontDescriptorCreateMatchingFontDescriptors(desc, NULL))
2208 , fFamilyName(familyName) 2211 , fFamilyName(familyName)
2209 , fCount(0) { 2212 , fCount(0) {
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after
2436 if (cgVariations) { 2439 if (cgVariations) {
2437 cgVariant.reset(CGFontCreateCopyWithVariations(cg, cgVariations)); 2440 cgVariant.reset(CGFontCreateCopyWithVariations(cg, cgVariations));
2438 } else { 2441 } else {
2439 cgVariant.reset(cg.detach()); 2442 cgVariant.reset(cg.detach());
2440 } 2443 }
2441 2444
2442 CTFontRef ct = CTFontCreateWithGraphicsFont(cgVariant, 0, NULL, NULL); 2445 CTFontRef ct = CTFontCreateWithGraphicsFont(cgVariant, 0, NULL, NULL);
2443 if (!ct) { 2446 if (!ct) {
2444 return NULL; 2447 return NULL;
2445 } 2448 }
2446 return NewFromFontRef(ct, NULL, true, cg.detach()); 2449 return NewFromFontRef(ct, cg.detach(), NULL, true);
2447 } 2450 }
2448 2451
2449 SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const override { 2452 SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const override {
2450 AutoCFRelease<CGDataProviderRef> pr(CGDataProviderCreateWithFilename(pat h)); 2453 AutoCFRelease<CGDataProviderRef> pr(CGDataProviderCreateWithFilename(pat h));
2451 if (NULL == pr) { 2454 if (NULL == pr) {
2452 return NULL; 2455 return NULL;
2453 } 2456 }
2454 return create_from_dataProvider(pr); 2457 return create_from_dataProvider(pr);
2455 } 2458 }
2456 2459
(...skipping 23 matching lines...) Expand all
2480 } 2483 }
2481 return face; 2484 return face;
2482 } 2485 }
2483 }; 2486 };
2484 2487
2485 /////////////////////////////////////////////////////////////////////////////// 2488 ///////////////////////////////////////////////////////////////////////////////
2486 2489
2487 SkFontMgr* SkFontMgr::Factory() { 2490 SkFontMgr* SkFontMgr::Factory() {
2488 return SkNEW(SkFontMgr_Mac); 2491 return SkNEW(SkFontMgr_Mac);
2489 } 2492 }
OLDNEW
« no previous file with comments | « include/ports/SkTypeface_mac.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698