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; onGlyphMaskNeedsCurrentColor()97 bool onGlyphMaskNeedsCurrentColor() const override { return false; } 98 int onGetVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[], 99 int coordinateCount) const override; 100 void onGetFamilyName(SkString* familyName) const override; 101 bool onGetPostScriptName(SkString*) const override; 102 SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const override; 103 int onGetTableTags(SkFontTableTag tags[]) const override; 104 size_t onGetTableData(SkFontTableTag, size_t offset, size_t length, void* data) const override; 105 sk_sp<SkData> onCopyTableData(SkFontTableTag) const override; 106 std::unique_ptr<SkScalerContext> onCreateScalerContext(const SkScalerContextEffects&, 107 const SkDescriptor*) const override; 108 void onFilterRec(SkScalerContextRec*) const override; 109 void onGetFontDescriptor(SkFontDescriptor*, bool*) const override; 110 void getGlyphToUnicodeMap(SkUnichar*) const override; 111 std::unique_ptr<SkAdvancedTypefaceMetrics> onGetAdvancedMetrics() const override; 112 void onCharsToGlyphs(const SkUnichar* chars, int count, SkGlyphID glyphs[]) const override; 113 int onCountGlyphs() const override; getPostScriptGlyphNames(SkString *)114 void getPostScriptGlyphNames(SkString*) const override {} 115 int onGetVariationDesignParameters(SkFontParameters::Variation::Axis parameters[], 116 int parameterCount) const override; 117 sk_sp<SkTypeface> onMakeClone(const SkFontArguments&) const override; 118 onGetCTFontRef()119 void* onGetCTFontRef() const override { return (void*)fFontRef.get(); } 120 121 private: 122 mutable std::unique_ptr<SkStreamAsset> fStream; 123 bool fIsFromStream; 124 mutable SkOnce fInitStream; 125 126 using INHERITED = SkTypeface; 127 }; 128 129 #endif 130 #endif //SkTypeface_mac_ct_DEFINED 131