diff options
author | Ben Wagner <bungeman@google.com> | 2017-02-23 20:48:43 +0000 |
---|---|---|
committer | Skia Commit-Bot <skia-commit-bot@chromium.org> | 2017-02-23 20:48:53 +0000 |
commit | 97043299cb352f50f604878b7a1dc8ad127fc1c6 (patch) | |
tree | 3363bfc674ab808e735336898386076d9c3b9558 /src/ports/SkFontHost_mac.cpp | |
parent | 3f81f3703a68755c88f5cc4a87728b98f34c4cd4 (diff) |
Revert "Add SkTypeface::getVariationDesignPosition."
This reverts commit 0f3d0c37dbcaf4ec271d5fe847becc9b1aa6f537.
Reason for revert: <INSERT REASONING HERE>
Original change's description:
> Add SkTypeface::getVariationDesignPosition.
>
> Allow users to query a typeface's position in variation design space.
>
> Change-Id: I173ee9eefdddee6b2613435ebcc6b08c25b382ed
> Reviewed-on: https://skia-review.googlesource.com/8684
> Commit-Queue: Ben Wagner <bungeman@google.com>
> Reviewed-by: Mike Reed <reed@google.com>
>
TBR=bungeman@google.com,reed@google.com,reviews@skia.org,drott@chromium.org
NOPRESUBMIT=true
NOTREECHECKS=true
NOTRY=true
Change-Id: I484fe52c1f89e7b6d0024dcabf7c59d0e8b5b5e7
Reviewed-on: https://skia-review.googlesource.com/8929
Reviewed-by: Ben Wagner <bungeman@google.com>
Commit-Queue: Ben Wagner <bungeman@google.com>
Diffstat (limited to 'src/ports/SkFontHost_mac.cpp')
-rw-r--r-- | src/ports/SkFontHost_mac.cpp | 232 |
1 files changed, 63 insertions, 169 deletions
diff --git a/src/ports/SkFontHost_mac.cpp b/src/ports/SkFontHost_mac.cpp index b5022d9af9..7a46d40ec6 100644 --- a/src/ports/SkFontHost_mac.cpp +++ b/src/ports/SkFontHost_mac.cpp @@ -492,8 +492,6 @@ protected: int onGetUPEM() const override; SkStreamAsset* onOpenStream(int* ttcIndex) const override; std::unique_ptr<SkFontData> onMakeFontData() const override; - int onGetVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[], - int coordinateCount) const override; void onGetFamilyName(SkString* familyName) const override; SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const override; int onGetTableTags(SkFontTableTag tags[]) const override; @@ -1376,10 +1374,7 @@ void SkScalerContext_Mac::CTPathElement(void *info, const CGPathElement *element // Returns nullptr on failure // Call must still manage its ownership of provider -static SkTypeface* create_from_dataProvider(UniqueCFRef<CGDataProviderRef> provider, int ttcIndex) { - if (ttcIndex != 0) { - return nullptr; - } +static SkTypeface* create_from_dataProvider(UniqueCFRef<CGDataProviderRef> provider) { UniqueCFRef<CGFontRef> cg(CGFontCreateWithDataProvider(provider.get())); if (!cg) { return nullptr; @@ -1700,14 +1695,13 @@ static void set_non_default_axes(CFTypeRef key, CFTypeRef value, void* context) } self->axisValue[keyIndex] = SkDoubleToFixed(valueDouble); } -static bool get_variations(CTFontRef ctFont, CFIndex* cgAxisCount, +static bool get_variations(CTFontRef fFontRef, CFIndex* cgAxisCount, SkAutoSTMalloc<4, SkFixed>* axisValues) { - // In 10.10 and earlier, CTFontCopyVariationAxes and CTFontCopyVariation do not work when - // applied to fonts which started life with CGFontCreateWithDataProvider (they simply always - // return nullptr). As a result, we are limited to CGFontCopyVariationAxes and - // CGFontCopyVariations here until support for 10.10 and earlier is removed. - UniqueCFRef<CGFontRef> cgFont(CTFontCopyGraphicsFont(ctFont, nullptr)); + // CTFontCopyVariationAxes and CTFontCopyVariation do not work when applied to fonts which + // started life with CGFontCreateWithDataProvider (they simply always return nullptr). + // As a result, we are limited to CGFontCopyVariationAxes and CGFontCopyVariations. + UniqueCFRef<CGFontRef> cgFont(CTFontCopyGraphicsFont(fFontRef, nullptr)); if (!cgFont) { return false; } @@ -1772,126 +1766,6 @@ std::unique_ptr<SkFontData> SkTypeface_Mac::onMakeFontData() const { return skstd::make_unique<SkFontData>(std::move(stream), index, nullptr, 0); } -/** Creates a CT variation dictionary {tag, value} from a CG variation dictionary {name, value}. */ -static UniqueCFRef<CFDictionaryRef> ct_variation_from_cg_variation(CFDictionaryRef cgVariations, - CFArrayRef ctAxes) { - - UniqueCFRef<CFMutableDictionaryRef> ctVariations( - CFDictionaryCreateMutable(kCFAllocatorDefault, 0, - &kCFTypeDictionaryKeyCallBacks, - &kCFTypeDictionaryValueCallBacks)); - - CFIndex axisCount = CFArrayGetCount(ctAxes); - for (CFIndex i = 0; i < axisCount; ++i) { - CFTypeRef axisInfo = CFArrayGetValueAtIndex(ctAxes, i); - if (CFDictionaryGetTypeID() != CFGetTypeID(axisInfo)) { - return nullptr; - } - CFDictionaryRef axisInfoDict = static_cast<CFDictionaryRef>(axisInfo); - - // The assumption is that values produced by kCTFontVariationAxisNameKey and - // kCGFontVariationAxisName will always be equal. - CFTypeRef axisName = CFDictionaryGetValue(axisInfoDict, kCTFontVariationAxisNameKey); - if (!axisName || CFGetTypeID(axisName) != CFStringGetTypeID()) { - return nullptr; - } - - CFTypeRef axisValue = CFDictionaryGetValue(cgVariations, axisName); - if (!axisValue || CFGetTypeID(axisValue) != CFNumberGetTypeID()) { - return nullptr; - } - - CFTypeRef axisTag = CFDictionaryGetValue(axisInfoDict, kCTFontVariationAxisIdentifierKey); - if (!axisTag || CFGetTypeID(axisTag) != CFNumberGetTypeID()) { - return nullptr; - } - - CFDictionaryAddValue(ctVariations.get(), axisTag, axisValue); - } - return std::move(ctVariations); -} - -int SkTypeface_Mac::onGetVariationDesignPosition( - SkFontArguments::VariationPosition::Coordinate coordinates[], int coordinateCount) const -{ - // The CGFont variation data does not contain the tag. - - // This call always returns nullptr on 10.10 and under for CGFontCreateWithDataProvider fonts. - // When this happens, there is no API to provide the tag. - UniqueCFRef<CFArrayRef> ctAxes(CTFontCopyVariationAxes(fFontRef.get())); - if (!ctAxes) { - return -1; - } - CFIndex axisCount = CFArrayGetCount(ctAxes.get()); - if (!coordinates || coordinateCount < axisCount) { - return axisCount; - } - - // This call always returns nullptr on 10.11 and under for CGFontCreateWithDataProvider fonts. - // When this happens, try converting the CG variation to a CT variation. - // On 10.12 and later, this only returns non-default variations. - UniqueCFRef<CFDictionaryRef> ctVariations(CTFontCopyVariation(fFontRef.get())); - if (!ctVariations) { - // When 10.11 and earlier are no longer supported, the following code can be replaced with - // return -1 and ct_variation_from_cg_variation can be removed. - UniqueCFRef<CGFontRef> cgFont(CTFontCopyGraphicsFont(fFontRef.get(), nullptr)); - if (!cgFont) { - return -1; - } - UniqueCFRef<CFDictionaryRef> cgVariations(CGFontCopyVariations(cgFont.get())); - if (!cgVariations) { - return -1; - } - ctVariations = ct_variation_from_cg_variation(cgVariations.get(), ctAxes.get()); - if (!ctVariations) { - return -1; - } - } - - for (int i = 0; i < axisCount; ++i) { - CFTypeRef axisInfo = CFArrayGetValueAtIndex(ctAxes.get(), i); - if (CFDictionaryGetTypeID() != CFGetTypeID(axisInfo)) { - return -1; - } - CFDictionaryRef axisInfoDict = static_cast<CFDictionaryRef>(axisInfo); - - CFTypeRef tag = CFDictionaryGetValue(axisInfoDict, kCTFontVariationAxisIdentifierKey); - if (!tag || CFGetTypeID(tag) != CFNumberGetTypeID()) { - return -1; - } - CFNumberRef tagNumber = static_cast<CFNumberRef>(tag); - int64_t tagLong; - if (!CFNumberGetValue(tagNumber, kCFNumberSInt64Type, &tagLong)) { - return -1; - } - coordinates[i].axis = tagLong; - - CGFloat variationCGFloat; - CFTypeRef variationValue = CFDictionaryGetValue(ctVariations.get(), tagNumber); - if (variationValue) { - if (CFGetTypeID(variationValue) != CFNumberGetTypeID()) { - return -1; - } - CFNumberRef variationNumber = static_cast<CFNumberRef>(variationValue); - if (!CFNumberGetValue(variationNumber, kCFNumberCGFloatType, &variationCGFloat)) { - return -1; - } - } else { - CFTypeRef def = CFDictionaryGetValue(axisInfoDict, kCTFontVariationAxisDefaultValueKey); - if (!def || CFGetTypeID(def) != CFNumberGetTypeID()) { - return -1; - } - CFNumberRef defNumber = static_cast<CFNumberRef>(def); - if (!CFNumberGetValue(defNumber, kCFNumberCGFloatType, &variationCGFloat)) { - return -1; - } - } - coordinates[i].value = CGToScalar(variationCGFloat); - - } - return axisCount; -} - /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// @@ -2373,7 +2247,7 @@ protected: if (!pr) { return nullptr; } - return create_from_dataProvider(std::move(pr), ttcIndex); + return create_from_dataProvider(std::move(pr)); } SkTypeface* onCreateFromStream(SkStreamAsset* bareStream, int ttcIndex) const override { @@ -2382,26 +2256,55 @@ protected: if (!pr) { return nullptr; } - return create_from_dataProvider(std::move(pr), ttcIndex); + return create_from_dataProvider(std::move(pr)); } - /** Creates a dictionary suitable for setting the axes on a CGFont. */ - static UniqueCFRef<CFDictionaryRef> copy_axes(CGFontRef cg, const SkFontArguments& args) { - // The CGFont variation data is keyed by name, but lacks the tag. + static CFNumberRef get_tag_for_name(CFStringRef name, CFArrayRef ctAxes) { + CFIndex ctAxisCount = CFArrayGetCount(ctAxes); + for (int i = 0; i < ctAxisCount; ++i) { + CFTypeRef ctAxisInfo = CFArrayGetValueAtIndex(ctAxes, i); + if (CFDictionaryGetTypeID() != CFGetTypeID(ctAxisInfo)) { + return nullptr; + } + CFDictionaryRef ctAxisInfoDict = static_cast<CFDictionaryRef>(ctAxisInfo); + + CFTypeRef ctAxisName = CFDictionaryGetValue(ctAxisInfoDict, + kCTFontVariationAxisNameKey); + if (!ctAxisName || CFGetTypeID(ctAxisName) != CFStringGetTypeID()) { + return nullptr; + } + + if (CFEqual(name, ctAxisName)) { + CFTypeRef tag = CFDictionaryGetValue(ctAxisInfoDict, + kCTFontVariationAxisIdentifierKey); + if (!tag || CFGetTypeID(tag) != CFNumberGetTypeID()) { + return nullptr; + } + return static_cast<CFNumberRef>(tag); + } + } + return nullptr; + } + static UniqueCFRef<CFDictionaryRef> copy_axes(CGFontRef cg, const FontParameters& params) { + UniqueCFRef<CFArrayRef> cgAxes(CGFontCopyVariationAxes(cg)); + if (!cgAxes) { + return nullptr; + } + CFIndex axisCount = CFArrayGetCount(cgAxes.get()); + + // The CGFont variation data is keyed by name, and lacks the tag. // The CTFont variation data is keyed by tag, and also has the name. - // We would like to work with CTFont variations, but creating a CTFont font with + // We would like to work with CTFont variaitons, but creating a CTFont font with // CTFont variation dictionary runs into bugs. So use the CTFont variation data // to match names to tags to create the appropriate CGFont. UniqueCFRef<CTFontRef> ct(CTFontCreateWithGraphicsFont(cg, 0, nullptr, nullptr)); - // This call always returns nullptr on 10.10 and under. - // When this happens, there is no API to provide the tag. UniqueCFRef<CFArrayRef> ctAxes(CTFontCopyVariationAxes(ct.get())); - if (!ctAxes) { + if (!ctAxes || CFArrayGetCount(ctAxes.get()) != axisCount) { return nullptr; } - CFIndex axisCount = CFArrayGetCount(ctAxes.get()); - const SkFontArguments::VariationPosition position = args.getVariationDesignPosition(); + int paramAxisCount; + const FontParameters::Axis* paramAxes = params.getAxes(¶mAxisCount); UniqueCFRef<CFMutableDictionaryRef> dict( CFDictionaryCreateMutable(kCFAllocatorDefault, axisCount, @@ -2409,25 +2312,24 @@ protected: &kCFTypeDictionaryValueCallBacks)); for (int i = 0; i < axisCount; ++i) { - CFTypeRef axisInfo = CFArrayGetValueAtIndex(ctAxes.get(), i); + CFTypeRef axisInfo = CFArrayGetValueAtIndex(cgAxes.get(), i); if (CFDictionaryGetTypeID() != CFGetTypeID(axisInfo)) { return nullptr; } CFDictionaryRef axisInfoDict = static_cast<CFDictionaryRef>(axisInfo); - // The assumption is that values produced by kCTFontVariationAxisNameKey and - // kCGFontVariationAxisName will always be equal. - // If they are ever not, seach the project history for "get_tag_for_name". - CFTypeRef axisName = CFDictionaryGetValue(axisInfoDict, kCTFontVariationAxisNameKey); + CFTypeRef axisName = CFDictionaryGetValue(axisInfoDict, kCGFontVariationAxisName); if (!axisName || CFGetTypeID(axisName) != CFStringGetTypeID()) { return nullptr; } - CFTypeRef tag = CFDictionaryGetValue(axisInfoDict, kCTFontVariationAxisIdentifierKey); - if (!tag || CFGetTypeID(tag) != CFNumberGetTypeID()) { - return nullptr; + CFNumberRef tagNumber = + get_tag_for_name(static_cast<CFStringRef>(axisName), ctAxes.get()); + if (!tagNumber) { + // Could not find a tag to go with the name of this index. + // This would be a bug in CG/CT. + continue; } - CFNumberRef tagNumber = static_cast<CFNumberRef>(tag); int64_t tagLong; if (!CFNumberGetValue(tagNumber, kCFNumberSInt64Type, &tagLong)) { return nullptr; @@ -2435,9 +2337,9 @@ protected: // The variation axes can be set to any value, but cg will effectively pin them. // Pin them here to normalize. - CFTypeRef min = CFDictionaryGetValue(axisInfoDict, kCTFontVariationAxisMinimumValueKey); - CFTypeRef max = CFDictionaryGetValue(axisInfoDict, kCTFontVariationAxisMaximumValueKey); - CFTypeRef def = CFDictionaryGetValue(axisInfoDict, kCTFontVariationAxisDefaultValueKey); + CFTypeRef min = CFDictionaryGetValue(axisInfoDict, kCGFontVariationAxisMinValue); + CFTypeRef max = CFDictionaryGetValue(axisInfoDict, kCGFontVariationAxisMaxValue); + CFTypeRef def = CFDictionaryGetValue(axisInfoDict, kCGFontVariationAxisDefaultValue); if (!min || CFGetTypeID(min) != CFNumberGetTypeID() || !max || CFGetTypeID(max) != CFNumberGetTypeID() || !def || CFGetTypeID(def) != CFNumberGetTypeID()) @@ -2458,10 +2360,9 @@ protected: } double value = defDouble; - for (int j = 0; j < position.coordinateCount; ++j) { - if (position.coordinates[j].axis == tagLong) { - value = SkTPin(SkScalarToDouble(position.coordinates[j].value), - minDouble, maxDouble); + for (int j = 0; j < paramAxisCount; ++j) { + if (paramAxes[j].fTag == tagLong) { + value = SkTPin(SkScalarToDouble(paramAxes[j].fStyleValue),minDouble,maxDouble); break; } } @@ -2471,11 +2372,8 @@ protected: } return std::move(dict); } - SkTypeface* onCreateFromStream(SkStreamAsset* bs, const SkFontArguments& args) const override { + SkTypeface* onCreateFromStream(SkStreamAsset* bs, const FontParameters& params) const override { std::unique_ptr<SkStreamAsset> s(bs); - if (args.getCollectionIndex() != 0) { - return nullptr; - } UniqueCFRef<CGDataProviderRef> provider(SkCreateDataProviderFromStream(std::move(s))); if (!provider) { return nullptr; @@ -2485,7 +2383,7 @@ protected: return nullptr; } - UniqueCFRef<CFDictionaryRef> cgVariations = copy_axes(cg.get(), args); + UniqueCFRef<CFDictionaryRef> cgVariations = copy_axes(cg.get(), params); // The CGFontRef returned by CGFontCreateCopyWithVariations when the passed CGFontRef was // created from a data provider does not appear to have any ownership of the underlying // data. The original CGFontRef must be kept alive until the copy will no longer be used. @@ -2504,7 +2402,6 @@ protected: return create_from_CTFontRef(std::move(ct), std::move(cg), true); } - /** Creates a dictionary suitable for setting the axes on a CGFont. */ static UniqueCFRef<CFDictionaryRef> copy_axes(CGFontRef cg, SkFontData* fontData) { UniqueCFRef<CFArrayRef> cgAxes(CGFontCopyVariationAxes(cg)); if (!cgAxes) { @@ -2559,9 +2456,6 @@ protected: return std::move(dict); } SkTypeface* onCreateFromFontData(std::unique_ptr<SkFontData> fontData) const override { - if (fontData->getIndex() != 0) { - return nullptr; - } UniqueCFRef<CGDataProviderRef> provider( SkCreateDataProviderFromStream(fontData->detachStream())); if (!provider) { @@ -2596,7 +2490,7 @@ protected: if (!pr) { return nullptr; } - return create_from_dataProvider(std::move(pr), ttcIndex); + return create_from_dataProvider(std::move(pr)); } SkTypeface* onLegacyCreateTypeface(const char familyName[], SkFontStyle style) const override { |