1 /* 2 * Copyright 2020 Google Inc. 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 #ifndef SkTypeface_mac_ct_DEFINED 9 #define SkTypeface_mac_ct_DEFINED 10 11 #include "include/core/SkTypes.h" 12 #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) 13 14 #include "include/core/SkFontArguments.h" 15 #include "include/core/SkFontParameters.h" 16 #include "include/core/SkFontStyle.h" 17 #include "include/core/SkRefCnt.h" 18 #include "include/core/SkScalar.h" 19 #include "include/core/SkStream.h" 20 #include "include/core/SkTypeface.h" 21 #include "include/private/SkOnce.h" 22 #include "src/utils/mac/SkUniqueCFRef.h" 23 24 #ifdef SK_BUILD_FOR_MAC 25 #import <ApplicationServices/ApplicationServices.h> 26 #endif 27 28 #ifdef SK_BUILD_FOR_IOS 29 #include <CoreText/CoreText.h> 30 #include <CoreText/CTFontManager.h> 31 #include <CoreGraphics/CoreGraphics.h> 32 #include <CoreFoundation/CoreFoundation.h> 33 #endif 34 35 #include <memory> 36 37 class SkData; 38 class SkDescriptor; 39 class SkFontData; 40 class SkFontDescriptor; 41 class SkScalerContext; 42 class SkString; 43 struct SkAdvancedTypefaceMetrics; 44 struct SkScalerContextEffects; 45 struct SkScalerContextRec; 46 47 struct OpszVariation { 48 bool isSet = false; 49 double value = 0; 50 }; 51 52 struct CTFontVariation { 53 SkUniqueCFRef<CFDictionaryRef> variation; 54 SkUniqueCFRef<CFDictionaryRef> wrongOpszVariation; 55 OpszVariation opsz; 56 }; 57 58 CTFontVariation SkCTVariationFromSkFontArguments(CTFontRef ct, const SkFontArguments& args); 59 60 SkUniqueCFRef<CTFontRef> SkCTFontCreateExactCopy(CTFontRef baseFont, CGFloat textSize, 61 OpszVariation opsz); 62 63 SkFontStyle SkCTFontDescriptorGetSkFontStyle(CTFontDescriptorRef desc, bool fromDataProvider); 64 65 CGFloat SkCTFontCTWeightForCSSWeight(int fontstyleWeight); 66 CGFloat SkCTFontCTWidthForCSSWidth(int fontstyleWidth); 67 68 void SkStringFromCFString(CFStringRef src, SkString* dst); 69 70 class SkTypeface_Mac : public SkTypeface { 71 private: SkTypeface_Mac(SkUniqueCFRef<CTFontRef> fontRef,const SkFontStyle & fs,bool isFixedPitch,OpszVariation opszVariation,std::unique_ptr<SkStreamAsset> providedData)72 SkTypeface_Mac(SkUniqueCFRef<CTFontRef> fontRef, const SkFontStyle& fs, bool isFixedPitch, 73 OpszVariation opszVariation, std::unique_ptr<SkStreamAsset> providedData) 74 : SkTypeface(fs, isFixedPitch) 75 , fFontRef(std::move(fontRef)) 76 , fOpszVariation(opszVariation) 77 , fHasColorGlyphs( 78 SkToBool(CTFontGetSymbolicTraits(fFontRef.get()) & kCTFontColorGlyphsTrait)) 79 , fStream(std::move(providedData)) 80 , fIsFromStream(fStream) 81 { 82 SkASSERT(fFontRef); 83 } 84 85 public: 86 static sk_sp<SkTypeface> Make(SkUniqueCFRef<CTFontRef> font, 87 OpszVariation opszVariation, 88 std::unique_ptr<SkStreamAsset> providedData); 89 90 SkUniqueCFRef<CTFontRef> fFontRef; 91 const OpszVariation fOpszVariation; 92 const bool fHasColorGlyphs; 93 94 protected: 95 int onGetUPEM() const override; 96 std::unique_ptr<SkStreamAsset> onOpenStream(int* ttcIndex) const override; 97 int onGetVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[], 98 int coordinateCount) const override; 99 void onGetFamilyName(SkString* familyName) const override; 100 bool onGetPostScriptName(SkString*) const override; 101 SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const override; 102 int onGetTableTags(SkFontTableTag tags[]) const override; 103 size_t onGetTableData(SkFontTableTag, size_t offset, size_t length, void* data) const override; 104 sk_sp<SkData> onCopyTableData(SkFontTableTag) const override; 105 std::unique_ptr<SkScalerContext> onCreateScalerContext(const SkScalerContextEffects&, 106 const SkDescriptor*) const override; 107 void onFilterRec(SkScalerContextRec*) const override; 108 void onGetFontDescriptor(SkFontDescriptor*, bool*) const override; 109 void getGlyphToUnicodeMap(SkUnichar*) const override; 110 std::unique_ptr<SkAdvancedTypefaceMetrics> onGetAdvancedMetrics() const override; 111 void onCharsToGlyphs(const SkUnichar* chars, int count, SkGlyphID glyphs[]) const override; 112 int onCountGlyphs() const override; getPostScriptGlyphNames(SkString *)113 void getPostScriptGlyphNames(SkString*) const override {} 114 int onGetVariationDesignParameters(SkFontParameters::Variation::Axis parameters[], 115 int parameterCount) const override; 116 sk_sp<SkTypeface> onMakeClone(const SkFontArguments&) const override; 117 onGetCTFontRef()118 void* onGetCTFontRef() const override { return (void*)fFontRef.get(); } 119 120 private: 121 mutable std::unique_ptr<SkStreamAsset> fStream; 122 bool fIsFromStream; 123 mutable SkOnce fInitStream; 124 125 using INHERITED = SkTypeface; 126 }; 127 128 #endif 129 #endif //SkTypeface_mac_ct_DEFINED 130