diff options
Diffstat (limited to 'src/ports')
-rw-r--r-- | src/ports/SkFontConfigParser_android.cpp | 73 | ||||
-rw-r--r-- | src/ports/SkFontConfigParser_android.h | 105 | ||||
-rw-r--r-- | src/ports/SkFontHost_FreeType.cpp | 74 | ||||
-rw-r--r-- | src/ports/SkFontHost_FreeType_common.h | 10 | ||||
-rw-r--r-- | src/ports/SkFontHost_fontconfig.cpp | 1 | ||||
-rw-r--r-- | src/ports/SkFontHost_linux.cpp | 7 | ||||
-rwxr-xr-x | src/ports/SkFontHost_mac.cpp | 206 | ||||
-rw-r--r-- | src/ports/SkFontMgr_android.cpp | 119 | ||||
-rw-r--r-- | src/ports/SkFontMgr_fontconfig.cpp | 43 | ||||
-rw-r--r-- | src/ports/SkTypeface_win_dw.cpp | 1 |
10 files changed, 85 insertions, 554 deletions
diff --git a/src/ports/SkFontConfigParser_android.cpp b/src/ports/SkFontConfigParser_android.cpp index 3825a72903..73e4c47738 100644 --- a/src/ports/SkFontConfigParser_android.cpp +++ b/src/ports/SkFontConfigParser_android.cpp @@ -14,7 +14,9 @@ #include <expat.h> #include <dirent.h> +#include <stdio.h> +#include <limits> #include <stdlib.h> #define LMP_SYSTEM_FONTS_FILE "/system/etc/fonts.xml" @@ -109,6 +111,32 @@ struct FamilyData { SkTDArray<const TagHandler*> fHandler; // The stack of current tag handlers. }; +/** Parses a null terminated string into an integer type, checking for overflow. + * http://www.w3.org/TR/html-markup/datatypes.html#common.data.integer.non-negative-def + * + * If the string cannot be parsed into 'value', returns false and does not change 'value'. + */ +template <typename T> static bool parse_non_negative_integer(const char* s, T* value) { + SK_COMPILE_ASSERT(std::numeric_limits<T>::is_integer, T_must_be_integer); + const T nMax = std::numeric_limits<T>::max() / 10; + const T dMax = std::numeric_limits<T>::max() - (nMax * 10); + T n = 0; + for (; *s; ++s) { + // Check if digit + if (*s < '0' || '9' < *s) { + return false; + } + int d = *s - '0'; + // Check for overflow + if (n > nMax || (n == nMax && d > dMax)) { + return false; + } + n = (n * 10) + d; + } + *value = n; + return true; +} + static bool memeq(const char* s1, const char* s2, size_t n1, size_t n2) { return n1 == n2 && 0 == memcmp(s1, s2, n1); } @@ -146,43 +174,6 @@ static void trim_string(SkString* s) { namespace lmpParser { -static const TagHandler axisHandler = { - /*start*/[](FamilyData* self, const char* tag, const char** attributes) { - FontFileInfo& file = *self->fCurrentFontInfo; - FontFileInfo::Axis& axis = file.fAxes.push_back(); - for (size_t i = 0; ATTS_NON_NULL(attributes, i); i += 2) { - const char* name = attributes[i]; - const char* value = attributes[i+1]; - size_t nameLen = strlen(name); - if (MEMEQ("tag", name, nameLen)) { - size_t valueLen = strlen(value); - if (valueLen == 4) { - SkFourByteTag tag = SkSetFourByteTag(value[0], value[1], value[2], value[3]); - for (int j = 0; j < file.fAxes.count() - 1; ++j) { - if (file.fAxes[j].fTag == tag) { - SK_FONTCONFIGPARSER_WARNING("'%c%c%c%c' axis specified more than once", - (tag >> 24) & 0xFF, - (tag >> 16) & 0xFF, - (tag >> 8) & 0xFF, - (tag ) & 0xFF); - } - } - axis.fTag = SkSetFourByteTag(value[0], value[1], value[2], value[3]); - } else { - SK_FONTCONFIGPARSER_WARNING("'%s' is an invalid axis tag", value); - } - } else if (MEMEQ("stylevalue", name, nameLen)) { - if (!parse_fixed<16>(value, &axis.fValue)) { - SK_FONTCONFIGPARSER_WARNING("'%s' is an invalid axis stylevalue", value); - } - } - } - }, - /*end*/NULL, - /*tag*/NULL, - /*chars*/NULL, -}; - static const TagHandler fontHandler = { /*start*/[](FamilyData* self, const char* tag, const char** attributes) { // 'weight' (non-negative integer) [default 0] @@ -216,13 +207,7 @@ static const TagHandler fontHandler = { /*end*/[](FamilyData* self, const char* tag) { trim_string(&self->fCurrentFontInfo->fFileName); }, - /*tag*/[](FamilyData* self, const char* tag, const char** attributes) -> const TagHandler* { - size_t len = strlen(tag); - if (MEMEQ("axis", tag, len)) { - return &axisHandler; - } - return NULL; - }, + /*tag*/NULL, /*chars*/[](void* data, const char* s, int len) { FamilyData* self = static_cast<FamilyData*>(data); self->fCurrentFontInfo->fFileName.append(s, len); diff --git a/src/ports/SkFontConfigParser_android.h b/src/ports/SkFontConfigParser_android.h index c1327ff74e..dd856bf733 100644 --- a/src/ports/SkFontConfigParser_android.h +++ b/src/ports/SkFontConfigParser_android.h @@ -12,9 +12,6 @@ #include "SkString.h" #include "SkTDArray.h" -#include <climits> -#include <limits> - /** \class SkLanguage The SkLanguage class represents a human written language, and is used by @@ -71,12 +68,6 @@ struct FontFileInfo { int fIndex; int fWeight; enum class Style { kAuto, kNormal, kItalic } fStyle; - struct Axis { - Axis() : fTag(SkSetFourByteTag('\0','\0','\0','\0')), fValue(0) { } - SkFourByteTag fTag; - SkFixed fValue; - }; - SkSTArray<4, Axis, true> fAxes; }; /** @@ -117,100 +108,4 @@ void GetCustomFontFamilies(SkTDArray<FontFamily*>& fontFamilies, } // SkFontConfigParser namespace - -/** Parses a null terminated string into an integer type, checking for overflow. - * http://www.w3.org/TR/html-markup/datatypes.html#common.data.integer.non-negative-def - * - * If the string cannot be parsed into 'value', returns false and does not change 'value'. - */ -template <typename T> static bool parse_non_negative_integer(const char* s, T* value) { - SK_COMPILE_ASSERT(std::numeric_limits<T>::is_integer, T_must_be_integer); - - if (*s == '\0') { - return false; - } - - const T nMax = std::numeric_limits<T>::max() / 10; - const T dMax = std::numeric_limits<T>::max() - (nMax * 10); - T n = 0; - for (; *s; ++s) { - // Check if digit - if (*s < '0' || '9' < *s) { - return false; - } - T d = *s - '0'; - // Check for overflow - if (n > nMax || (n == nMax && d > dMax)) { - return false; - } - n = (n * 10) + d; - } - *value = n; - return true; -} - -/** Parses a null terminated string into a signed fixed point value with bias N. - * - * Like http://www.w3.org/TR/html-markup/datatypes.html#common.data.float-def , - * but may start with '.' and does not support 'e'. '-?((:digit:+(.:digit:+)?)|(.:digit:+))' - * - * Checks for overflow. - * Low bit rounding is not defined (is currently truncate). - * Bias (N) required to allow for the sign bit and 4 bits of integer. - * - * If the string cannot be parsed into 'value', returns false and does not change 'value'. - */ -template <int N, typename T> static bool parse_fixed(const char* s, T* value) { - SK_COMPILE_ASSERT(std::numeric_limits<T>::is_integer, T_must_be_integer); - SK_COMPILE_ASSERT(std::numeric_limits<T>::is_signed, T_must_be_signed); - SK_COMPILE_ASSERT(sizeof(T) * CHAR_BIT - N >= 5, N_must_leave_four_bits_plus_sign); - - bool negate = false; - if (*s == '-') { - ++s; - negate = true; - } - if (*s == '\0') { - return false; - } - - const T nMax = (std::numeric_limits<T>::max() >> N) / 10; - const T dMax = (std::numeric_limits<T>::max() >> N) - (nMax * 10); - T n = 0; - T frac = 0; - for (; *s; ++s) { - // Check if digit - if (*s < '0' || '9' < *s) { - // If it wasn't a digit, check if it is a '.' followed by something. - if (*s != '.' || s[1] == '\0') { - return false; - } - // Find the end, verify digits. - for (++s; *s; ++s) { - if (*s < '0' || '9' < *s) { - return false; - } - } - // Read back toward the '.'. - for (--s; *s != '.'; --s) { - T d = *s - '0'; - frac = (frac + (d << N)) / 10; // This requires four bits overhead. - } - break; - } - T d = *s - '0'; - // Check for overflow - if (n > nMax || (n == nMax && d > dMax)) { - return false; - } - n = (n * 10) + d; - } - if (negate) { - n = -n; - frac = -frac; - } - *value = (n << N) + frac; - return true; -} - #endif /* SKFONTCONFIGPARSER_ANDROID_H_ */ diff --git a/src/ports/SkFontHost_FreeType.cpp b/src/ports/SkFontHost_FreeType.cpp index a4305bfa87..5c53b9d913 100644 --- a/src/ports/SkFontHost_FreeType.cpp +++ b/src/ports/SkFontHost_FreeType.cpp @@ -11,7 +11,6 @@ #include "SkColorPriv.h" #include "SkDescriptor.h" #include "SkFDot6.h" -#include "SkFontDescriptor.h" #include "SkFontHost_FreeType_common.h" #include "SkGlyph.h" #include "SkMask.h" @@ -34,7 +33,6 @@ #include FT_FREETYPE_H #include FT_LCD_FILTER_H #include FT_MODULE_H -#include FT_MULTIPLE_MASTERS_H #include FT_OUTLINE_H #include FT_SIZES_H #include FT_SYSTEM_H @@ -275,37 +273,6 @@ SkFaceRec::SkFaceRec(SkStreamAsset* stream, uint32_t fontID) fFTStream.close = sk_ft_stream_close; } -static void ft_face_setup_axes(FT_Face face, const SkFontData& data) { - if (!(face->face_flags & FT_FACE_FLAG_MULTIPLE_MASTERS)) { - return; - } - - SkDEBUGCODE( - FT_MM_Var* variations = NULL; - if (FT_Get_MM_Var(face, &variations)) { - SkDEBUGF(("INFO: font %s claims variations, but none found.\n", face->family_name)); - return; - } - SkAutoFree autoFreeVariations(variations); - - if (static_cast<FT_UInt>(data.getAxisCount()) != variations->num_axis) { - SkDEBUGF(("INFO: font %s has %d variations, but %d were specified.\n", - face->family_name, variations->num_axis, data.getAxisCount())); - return; - } - ) - - SkAutoSTMalloc<4, FT_Fixed> coords(data.getAxisCount()); - for (int i = 0; i < data.getAxisCount(); ++i) { - coords[i] = data.getAxis()[i]; - } - if (FT_Set_Var_Design_Coordinates(face, data.getAxisCount(), coords.get())) { - SkDEBUGF(("INFO: font %s has variations, but specified variations could not be set.\n", - face->family_name)); - return; - } -} - // Will return 0 on failure // Caller must lock gFTMutex before calling this function. static SkFaceRec* ref_ft_face(const SkTypeface* typeface) { @@ -322,36 +289,34 @@ static SkFaceRec* ref_ft_face(const SkTypeface* typeface) { rec = rec->fNext; } - SkAutoTDelete<SkFontData> data(typeface->createFontData()); - if (NULL == data || !data->hasStream()) { + int face_index; + SkStreamAsset* stream = typeface->openStream(&face_index); + if (NULL == stream) { return NULL; } // this passes ownership of stream to the rec - rec = SkNEW_ARGS(SkFaceRec, (data->detachStream(), fontID)); + rec = SkNEW_ARGS(SkFaceRec, (stream, fontID)); FT_Open_Args args; memset(&args, 0, sizeof(args)); - const void* memoryBase = rec->fSkStream->getMemoryBase(); + const void* memoryBase = stream->getMemoryBase(); if (memoryBase) { args.flags = FT_OPEN_MEMORY; args.memory_base = (const FT_Byte*)memoryBase; - args.memory_size = rec->fSkStream->getLength(); + args.memory_size = stream->getLength(); } else { args.flags = FT_OPEN_STREAM; args.stream = &rec->fFTStream; } - FT_Error err = FT_Open_Face(gFTLibrary->library(), &args, data->getIndex(), &rec->fFace); - if (err) { + FT_Error err = FT_Open_Face(gFTLibrary->library(), &args, face_index, &rec->fFace); + if (err) { // bad filename, try the default font SkDEBUGF(("ERROR: unable to open font '%x'\n", fontID)); SkDELETE(rec); return NULL; } SkASSERT(rec->fFace); - - ft_face_setup_axes(rec->fFace, *data); - rec->fNext = gFaceRecHead; gFaceRecHead = rec; return rec; @@ -1684,8 +1649,7 @@ bool SkTypeface_FreeType::Scanner::recognizedFont(SkStream* stream, int* numFace #include "SkTSearch.h" bool SkTypeface_FreeType::Scanner::scanFont( - SkStream* stream, int ttcIndex, - SkString* name, SkFontStyle* style, bool* isFixedPitch, AxisDefinitions* axes) const + SkStream* stream, int ttcIndex, SkString* name, SkFontStyle* style, bool* isFixedPitch) const { SkAutoMutexAcquire libraryLock(fLibraryMutex); @@ -1761,26 +1725,6 @@ bool SkTypeface_FreeType::Scanner::scanFont( *isFixedPitch = FT_IS_FIXED_WIDTH(face); } - if (axes && face->face_flags & FT_FACE_FLAG_MULTIPLE_MASTERS) { - FT_MM_Var* variations = NULL; - FT_Error err = FT_Get_MM_Var(face, &variations); - if (err) { - SkDEBUGF(("INFO: font %s claims to have variations, but none found.\n", - face->family_name)); - return false; - } - SkAutoFree autoFreeVariations(variations); - - axes->reset(variations->num_axis); - for (FT_UInt i = 0; i < variations->num_axis; ++i) { - const FT_Var_Axis& ftAxis = variations->axis[i]; - (*axes)[i].fTag = ftAxis.tag; - (*axes)[i].fMinimum = ftAxis.minimum; - (*axes)[i].fDefault = ftAxis.def; - (*axes)[i].fMaximum = ftAxis.maximum; - } - } - FT_Done_Face(face); return true; } diff --git a/src/ports/SkFontHost_FreeType_common.h b/src/ports/SkFontHost_FreeType_common.h index 8ae872c1da..857d2c8990 100644 --- a/src/ports/SkFontHost_FreeType_common.h +++ b/src/ports/SkFontHost_FreeType_common.h @@ -43,17 +43,9 @@ public: public: Scanner(); ~Scanner(); - struct AxisDefinition { - SkFourByteTag fTag; - SkFixed fMinimum; - SkFixed fDefault; - SkFixed fMaximum; - }; - using AxisDefinitions = SkSTArray<4, AxisDefinition, true>; bool recognizedFont(SkStream* stream, int* numFonts) const; bool scanFont(SkStream* stream, int ttcIndex, - SkString* name, SkFontStyle* style, bool* isFixedPitch, - AxisDefinitions* axes) const; + SkString* name, SkFontStyle* style, bool* isFixedPitch) const; private: FT_Face openFace(SkStream* stream, int ttcIndex, FT_Stream ftStream) const; FT_Library fLibrary; diff --git a/src/ports/SkFontHost_fontconfig.cpp b/src/ports/SkFontHost_fontconfig.cpp index 2fdb16e364..fa74f5121f 100644 --- a/src/ports/SkFontHost_fontconfig.cpp +++ b/src/ports/SkFontHost_fontconfig.cpp @@ -155,5 +155,6 @@ void FontConfigTypeface::onGetFontDescriptor(SkFontDescriptor* desc, SkString name; this->getFamilyName(&name); desc->setFamilyName(name.c_str()); + desc->setFontIndex(this->getIdentity().fTTCIndex); *isLocalStream = SkToBool(this->getLocalStream()); } diff --git a/src/ports/SkFontHost_linux.cpp b/src/ports/SkFontHost_linux.cpp index b4404ca745..da2c2d00d1 100644 --- a/src/ports/SkFontHost_linux.cpp +++ b/src/ports/SkFontHost_linux.cpp @@ -39,6 +39,7 @@ protected: void onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocal) const override { desc->setFamilyName(fFamilyName.c_str()); + desc->setFontIndex(fIndex); *isLocal = !this->isSysFont(); } @@ -305,7 +306,7 @@ protected: bool isFixedPitch; SkFontStyle style; SkString name; - if (fScanner.scanFont(stream, ttcIndex, &name, &style, &isFixedPitch, NULL)) { + if (fScanner.scanFont(stream, ttcIndex, &name, &style, &isFixedPitch)) { return SkNEW_ARGS(SkTypeface_Stream, (style, isFixedPitch, false, name, stream.detach(), ttcIndex)); } else { @@ -404,7 +405,7 @@ private: bool isFixedPitch; SkString realname; SkFontStyle style = SkFontStyle(); // avoid uninitialized warning - if (!scanner.scanFont(stream, faceIndex, &realname, &style, &isFixedPitch, NULL)) { + if (!scanner.scanFont(stream, faceIndex, &realname, &style, &isFixedPitch)) { SkDebugf("---- failed to open <%s> <%d> as a font\n", filename.c_str(), faceIndex); continue; @@ -490,7 +491,7 @@ private: bool isFixedPitch; SkString realname; SkFontStyle style = SkFontStyle(); // avoid uninitialized warning - if (!scanner.scanFont(stream, faceIndex, &realname, &style, &isFixedPitch, NULL)) { + if (!scanner.scanFont(stream, faceIndex, &realname, &style, &isFixedPitch)) { SkDebugf("---- failed to open <%d> <%d> as a font\n", index, faceIndex); return; } diff --git a/src/ports/SkFontHost_mac.cpp b/src/ports/SkFontHost_mac.cpp index 2ef5f49913..3c7be46364 100755 --- a/src/ports/SkFontHost_mac.cpp +++ b/src/ports/SkFontHost_mac.cpp @@ -428,12 +428,10 @@ static const uint32_t SkCTFontColorGlyphsTrait = (1 << 13); class SkTypeface_Mac : public SkTypeface { public: SkTypeface_Mac(const SkFontStyle& fs, bool isFixedPitch, - CTFontRef fontRef, const char requestedName[], bool isLocalStream, - CGFontRef originatingCGFontRef = NULL) + CTFontRef fontRef, const char requestedName[], bool isLocalStream) : SkTypeface(fs, SkTypefaceCache::NewFontID(), isFixedPitch) , fRequestedName(requestedName) , fFontRef(fontRef) // caller has already called CFRetain for us - , fOriginatingCGFontRef(originatingCGFontRef) , fHasColorGlyphs(SkToBool(CTFontGetSymbolicTraits(fFontRef) & SkCTFontColorGlyphsTrait)) , fIsLocalStream(isLocalStream) { @@ -442,13 +440,11 @@ public: SkString fRequestedName; AutoCFRelease<CTFontRef> fFontRef; - AutoCFRelease<CGFontRef> fOriginatingCGFontRef; const bool fHasColorGlyphs; protected: int onGetUPEM() const override; SkStreamAsset* onOpenStream(int* ttcIndex) const override; - SkFontData* onCreateFontData() const override; void onGetFamilyName(SkString* familyName) const override; SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const override; int onGetTableTags(SkFontTableTag tags[]) const override; @@ -471,15 +467,12 @@ private: }; /** Creates a typeface without searching the cache. Takes ownership of the CTFontRef. */ -static SkTypeface* NewFromFontRef(CTFontRef fontRef, const char name[], bool isLocalStream, - CGFontRef originatingCGFontRef = NULL) -{ +static SkTypeface* NewFromFontRef(CTFontRef fontRef, const char name[], bool isLocalStream) { SkASSERT(fontRef); bool isFixedPitch; SkFontStyle style = SkFontStyle(computeStyleBits(fontRef, &isFixedPitch)); - return new SkTypeface_Mac(style, isFixedPitch, fontRef, name, isLocalStream, - originatingCGFontRef); + return new SkTypeface_Mac(style, isFixedPitch, fontRef, name, isLocalStream); } static bool find_by_CTFontRef(SkTypeface* cached, const SkFontStyle&, void* context) { @@ -1759,116 +1752,6 @@ SkStreamAsset* SkTypeface_Mac::onOpenStream(int* ttcIndex) const { return stream; } -struct NonDefaultAxesContext { - SkFixed* axisValue; - CFArrayRef cgAxes; -}; -static void set_non_default_axes(CFTypeRef key, CFTypeRef value, void* context) { - NonDefaultAxesContext* self = static_cast<NonDefaultAxesContext*>(context); - - if (CFGetTypeID(key) != CFStringGetTypeID() || CFGetTypeID(value) != CFNumberGetTypeID()) { - return; - } - - // The key is a CFString which is a string from the 'name' table. - // Search the cgAxes for an axis with this name, and use its index to store the value. - CFIndex keyIndex = -1; - CFStringRef keyString = static_cast<CFStringRef>(key); - for (CFIndex i = 0; i < CFArrayGetCount(self->cgAxes); ++i) { - CFTypeRef cgAxis = CFArrayGetValueAtIndex(self->cgAxes, i); - if (CFGetTypeID(cgAxis) != CFDictionaryGetTypeID()) { - continue; - } - - CFDictionaryRef cgAxisDict = static_cast<CFDictionaryRef>(cgAxis); - CFTypeRef cgAxisName = CFDictionaryGetValue(cgAxisDict, kCGFontVariationAxisName); - if (!cgAxisName || CFGetTypeID(cgAxisName) != CFStringGetTypeID()) { - continue; - } - CFStringRef cgAxisNameString = static_cast<CFStringRef>(cgAxisName); - if (CFStringCompare(keyString, cgAxisNameString, 0) == kCFCompareEqualTo) { - keyIndex = i; - break; - } - } - if (keyIndex == -1) { - return; - } - - CFNumberRef valueNumber = static_cast<CFNumberRef>(value); - double valueDouble; - if (!CFNumberGetValue(valueNumber, CFNumberType::kCFNumberDoubleType, &valueDouble) || - valueDouble < SkFixedToDouble(SK_FixedMin) || SkFixedToDouble(SK_FixedMax) < valueDouble) - { - return; - } - self->axisValue[keyIndex] = SkDoubleToFixed(valueDouble); -} -static bool get_variations(CTFontRef fFontRef, CFIndex* cgAxisCount, - SkAutoSTMalloc<4, SkFixed>* axisValues) -{ - // CTFontCopyVariationAxes and CTFontCopyVariation do not work when applied to fonts which - // started life with CGFontCreateWithDataProvider (they simply always return NULL). - // As a result, we are limited to CGFontCopyVariationAxes and CGFontCopyVariations. - AutoCFRelease<CGFontRef> cgFont(CTFontCopyGraphicsFont(fFontRef, NULL)); - - AutoCFRelease<CFDictionaryRef> cgVariations(CGFontCopyVariations(cgFont)); - // If a font has no variations CGFontCopyVariations returns NULL (instead of an empty dict). - if (!cgVariations.get()) { - return false; - } - - AutoCFRelease<CFArrayRef> cgAxes(CGFontCopyVariationAxes(cgFont)); - *cgAxisCount = CFArrayGetCount(cgAxes); - axisValues->reset(*cgAxisCount); - - // Set all of the axes to their default values. - // Fail if any default value cannot be determined. - for (CFIndex i = 0; i < *cgAxisCount; ++i) { - CFTypeRef cgAxis = CFArrayGetValueAtIndex(cgAxes, i); - if (CFGetTypeID(cgAxis) != CFDictionaryGetTypeID()) { - return false; - } - - CFDictionaryRef cgAxisDict = static_cast<CFDictionaryRef>(cgAxis); - CFTypeRef axisDefaultValue = CFDictionaryGetValue(cgAxisDict, - kCGFontVariationAxisDefaultValue); - if (!axisDefaultValue || CFGetTypeID(axisDefaultValue) != CFNumberGetTypeID()) { - return false; - } - CFNumberRef axisDefaultValueNumber = static_cast<CFNumberRef>(axisDefaultValue); - double axisDefaultValueDouble; - if (!CFNumberGetValue(axisDefaultValueNumber, CFNumberType::kCFNumberDoubleType, - &axisDefaultValueDouble)) - { - return false; - } - if (axisDefaultValueDouble < SkFixedToDouble(SK_FixedMin) || - SkFixedToDouble(SK_FixedMax) < axisDefaultValueDouble) - { - return false; - } - (*axisValues)[(int)i] = SkDoubleToFixed(axisDefaultValueDouble); - } - - // Override the default values with the given font's stated axis values. - NonDefaultAxesContext c = { axisValues->get(), cgAxes.get() }; - CFDictionaryApplyFunction(cgVariations, set_non_default_axes, &c); - - return true; -} -SkFontData* SkTypeface_Mac::onCreateFontData() const { - int index; - SkAutoTDelete<SkStreamAsset> stream(this->onOpenStream(&index)); - - CFIndex cgAxisCount; - SkAutoSTMalloc<4, SkFixed> axisValues; - if (get_variations(fFontRef, &cgAxisCount, &axisValues)) { - return new SkFontData(stream.detach(), index, axisValues.get(), cgAxisCount); - } - return new SkFontData(stream.detach(), index, NULL, 0); -} - /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// @@ -2364,89 +2247,6 @@ protected: return create_from_dataProvider(pr); } - static CFDictionaryRef get_axes(CGFontRef cg, SkFontData* fontData) { - AutoCFRelease<CFArrayRef> cgAxes(CGFontCopyVariationAxes(cg)); - if (!cgAxes) { - return NULL; - } - - CFIndex axisCount = CFArrayGetCount(cgAxes); - if (0 == axisCount || axisCount != fontData->getAxisCount()) { - return NULL; - } - - CFMutableDictionaryRef dict = CFDictionaryCreateMutable(kCFAllocatorDefault, axisCount, - &kCFTypeDictionaryKeyCallBacks, - &kCFTypeDictionaryValueCallBacks); - for (int i = 0; i < fontData->getAxisCount(); ++i) { - CFTypeRef axisInfo = CFArrayGetValueAtIndex(cgAxes, i); - if (CFDictionaryGetTypeID() != CFGetTypeID(axisInfo)) { - return NULL; - } - CFDictionaryRef axisInfoDict = static_cast<CFDictionaryRef>(axisInfo); - - CFTypeRef axisName = CFDictionaryGetValue(axisInfoDict, kCGFontVariationAxisName); - if (!axisName || CFGetTypeID(axisName) != CFStringGetTypeID()) { - return NULL; - } - - // The variation axes can be set to any value, but cg will effectively pin them. - // Pin them here to normalize. - CFTypeRef min = CFDictionaryGetValue(axisInfoDict, kCGFontVariationAxisMinValue); - CFTypeRef max = CFDictionaryGetValue(axisInfoDict, kCGFontVariationAxisMaxValue); - if (!min || CFGetTypeID(min) != CFNumberGetTypeID() || - !max || CFGetTypeID(max) != CFNumberGetTypeID()) - { - return NULL; - } - CFNumberRef minNumber = static_cast<CFNumberRef>(min); - CFNumberRef maxNumber = static_cast<CFNumberRef>(max); - double minDouble; - double maxDouble; - if (!CFNumberGetValue(minNumber, kCFNumberDoubleType, &minDouble) || - !CFNumberGetValue(maxNumber, kCFNumberDoubleType, &maxDouble)) - { - return NULL; - } - double value = SkTPin(SkFixedToDouble(fontData->getAxis()[i]), minDouble, maxDouble); - CFNumberRef valueNumber = CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, - &value); - - CFDictionaryAddValue(dict, axisName, valueNumber); - CFRelease(valueNumber); - } - return dict; - } - SkTypeface* onCreateFromFontData(SkFontData* data) const override { - SkAutoTDelete<SkFontData> fontData(data); - SkStreamAsset* stream = fontData->detachStream(); - AutoCFRelease<CGDataProviderRef> provider(SkCreateDataProviderFromStream(stream)); - if (NULL == provider) { - return NULL; - } - AutoCFRelease<CGFontRef> cg(CGFontCreateWithDataProvider(provider)); - if (NULL == cg) { - return NULL; - } - - AutoCFRelease<CFDictionaryRef> cgVariations(get_axes(cg, fontData)); - // 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. - AutoCFRelease<CGFontRef> cgVariant; - if (cgVariations) { - cgVariant.reset(CGFontCreateCopyWithVariations(cg, cgVariations)); - } else { - cgVariant.reset(cg.detach()); - } - - CTFontRef ct = CTFontCreateWithGraphicsFont(cgVariant, 0, NULL, NULL); - if (!ct) { - return NULL; - } - return NewFromFontRef(ct, NULL, true, cg.detach()); - } - SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const override { AutoCFRelease<CGDataProviderRef> pr(CGDataProviderCreateWithFilename(path)); if (NULL == pr) { diff --git a/src/ports/SkFontMgr_android.cpp b/src/ports/SkFontMgr_android.cpp index a67c6814ea..ae1b663e26 100644 --- a/src/ports/SkFontMgr_android.cpp +++ b/src/ports/SkFontMgr_android.cpp @@ -27,18 +27,20 @@ static const char* gTestBasePath = NULL; class SkTypeface_Android : public SkTypeface_FreeType { public: - SkTypeface_Android(const SkFontStyle& style, + SkTypeface_Android(int index, + const SkFontStyle& style, bool isFixedPitch, const SkString& familyName) : INHERITED(style, SkTypefaceCache::NewFontID(), isFixedPitch) - , fFamilyName(familyName) - { } + , fIndex(index) + , fFamilyName(familyName) { } protected: void onGetFamilyName(SkString* familyName) const override { *familyName = fFamilyName; } + int fIndex; SkString fFamilyName; private: @@ -49,37 +51,30 @@ class SkTypeface_AndroidSystem : public SkTypeface_Android { public: SkTypeface_AndroidSystem(const SkString& pathName, int index, - const SkFixed* axes, int axesCount, const SkFontStyle& style, bool isFixedPitch, const SkString& familyName, const SkLanguage& lang, FontVariant variantStyle) - : INHERITED(style, isFixedPitch, familyName) + : INHERITED(index, style, isFixedPitch, familyName) , fPathName(pathName) - , fIndex(index) - , fAxes(axes, axesCount) , fLang(lang) , fVariantStyle(variantStyle) { } - virtual void onGetFontDescriptor(SkFontDescriptor* desc, bool* serialize) const override { + virtual void onGetFontDescriptor(SkFontDescriptor* desc, + bool* serialize) const override { SkASSERT(desc); SkASSERT(serialize); desc->setFamilyName(fFamilyName.c_str()); + desc->setFontIndex(fIndex); *serialize = false; } SkStreamAsset* onOpenStream(int* ttcIndex) const override { *ttcIndex = fIndex; return SkStream::NewFromFile(fPathName.c_str()); } - SkFontData* onCreateFontData() const override { - return new SkFontData(SkStream::NewFromFile(fPathName.c_str()), fIndex, - fAxes.begin(), fAxes.count()); - } const SkString fPathName; - int fIndex; - const SkSTArray<4, SkFixed, true> fAxes; const SkLanguage fLang; const FontVariant fVariantStyle; @@ -88,13 +83,13 @@ public: class SkTypeface_AndroidStream : public SkTypeface_Android { public: - SkTypeface_AndroidStream(SkFontData* data, + SkTypeface_AndroidStream(SkStreamAsset* stream, + int index, const SkFontStyle& style, bool isFixedPitch, const SkString& familyName) - : INHERITED(style, isFixedPitch, familyName) - , fData(data) - { } + : INHERITED(index, style, isFixedPitch, familyName) + , fStream(stream) { } virtual void onGetFontDescriptor(SkFontDescriptor* desc, bool* serialize) const override { @@ -105,24 +100,21 @@ public: } SkStreamAsset* onOpenStream(int* ttcIndex) const override { - *ttcIndex = fData->getIndex(); - return fData->duplicateStream(); - } - - SkFontData* onCreateFontData() const override { - return new SkFontData(*fData.get()); + *ttcIndex = fIndex; + return fStream->duplicate(); } private: - const SkAutoTDelete<const SkFontData> fData; + SkAutoTDelete<SkStreamAsset> fStream; + typedef SkTypeface_Android INHERITED; }; class SkFontStyleSet_Android : public SkFontStyleSet { - typedef SkTypeface_FreeType::Scanner Scanner; - public: - explicit SkFontStyleSet_Android(const FontFamily& family, const Scanner& scanner) { + explicit SkFontStyleSet_Android(const FontFamily& family, + const SkTypeface_FreeType::Scanner& scanner) + { const SkString* cannonicalFamilyName = NULL; if (family.fNames.count() > 0) { cannonicalFamilyName = &family.fNames[0]; @@ -145,10 +137,7 @@ public: SkString familyName; SkFontStyle style; bool isFixedWidth; - Scanner::AxisDefinitions axisDefinitions; - if (!scanner.scanFont(stream.get(), ttcIndex, - &familyName, &style, &isFixedWidth, &axisDefinitions)) - { + if (!scanner.scanFont(stream.get(), ttcIndex, &familyName, &style, &isFixedWidth)) { SkDEBUGF(("Requested font file %s exists, but is not a valid font.\n", pathName.c_str())); continue; @@ -177,57 +166,8 @@ public: familyName = *cannonicalFamilyName; } - SkAutoSTMalloc<4, SkFixed> axisValues(axisDefinitions.count()); - for (int i = 0; i < axisDefinitions.count(); ++i) { - const Scanner::AxisDefinition& axisDefinition = axisDefinitions[i]; - axisValues[i] = axisDefinition.fDefault; - for (int j = 0; j < fontFile.fAxes.count(); ++j) { - const FontFileInfo::Axis& axisSpecified = fontFile.fAxes[j]; - if (axisDefinition.fTag == axisSpecified.fTag) { - axisValues[i] = SkTPin(axisSpecified.fValue, axisDefinition.fMinimum, - axisDefinition.fMaximum); - if (axisValues[i] != axisSpecified.fValue) { - SkDEBUGF(("Requested font axis value out of range: " - "%s '%c%c%c%c' %f; pinned to %f.\n", - familyName.c_str(), - (axisDefinition.fTag >> 24) & 0xFF, - (axisDefinition.fTag >> 16) & 0xFF, - (axisDefinition.fTag >> 8) & 0xFF, - (axisDefinition.fTag ) & 0xFF, - SkFixedToDouble(axisSpecified.fValue), - SkFixedToDouble(axisValues[i]))); - } - break; - } - } - // TODO: warn on defaulted axis? - } - - SkDEBUGCODE ( - // Check for axis specified, but not matched in font. - for (int i = 0; i < fontFile.fAxes.count(); ++i) { - SkFourByteTag skTag = fontFile.fAxes[i].fTag; - bool found = false; - for (int j = 0; j < axisDefinitions.count(); ++j) { - if (skTag == axisDefinitions[j].fTag) { - found = true; - break; - } - } - if (!found) { - SkDEBUGF(("Requested font axis not found: %s '%c%c%c%c'\n", - familyName.c_str(), - (skTag >> 24) & 0xFF, - (skTag >> 16) & 0xFF, - (skTag >> 8) & 0xFF, - (skTag ) & 0xFF)); - } - } - ) - fStyles.push_back().reset(SkNEW_ARGS(SkTypeface_AndroidSystem, (pathName, ttcIndex, - axisValues.get(), axisDefinitions.count(), style, isFixedWidth, familyName, lang, variant))); } @@ -471,22 +411,11 @@ protected: bool isFixedPitch; SkFontStyle style; SkString name; - if (!fScanner.scanFont(stream, ttcIndex, &name, &style, &isFixedPitch, NULL)) { - return NULL; - } - SkFontData* data(new SkFontData(stream.detach(), ttcIndex, NULL, 0)); - return SkNEW_ARGS(SkTypeface_AndroidStream, (data, style, isFixedPitch, name)); - } - - SkTypeface* onCreateFromFontData(SkFontData* data) const override { - SkStreamAsset* stream(data->getStream()); - bool isFixedPitch; - SkFontStyle style; - SkString name; - if (!fScanner.scanFont(stream, data->getIndex(), &name, &style, &isFixedPitch, NULL)) { + if (!fScanner.scanFont(stream, ttcIndex, &name, &style, &isFixedPitch)) { return NULL; } - return SkNEW_ARGS(SkTypeface_AndroidStream, (data, style, isFixedPitch, name)); + return SkNEW_ARGS(SkTypeface_AndroidStream, (stream.detach(), ttcIndex, + style, isFixedPitch, name)); } diff --git a/src/ports/SkFontMgr_fontconfig.cpp b/src/ports/SkFontMgr_fontconfig.cpp index 3baec41cb5..f03e5acaf1 100644 --- a/src/ports/SkFontMgr_fontconfig.cpp +++ b/src/ports/SkFontMgr_fontconfig.cpp @@ -375,10 +375,11 @@ static void fcpattern_from_skfontstyle(SkFontStyle style, FcPattern* pattern) { class SkTypeface_stream : public SkTypeface_FreeType { public: - /** @param data takes ownership of the font data.*/ - SkTypeface_stream(SkFontData* data, const SkFontStyle& style, bool fixedWidth) + /** @param stream does not take ownership of the reference, does take ownership of the stream.*/ + SkTypeface_stream(const SkFontStyle& style, bool fixedWidth, int index, SkStreamAsset* stream) : INHERITED(style, SkTypefaceCache::NewFontID(), fixedWidth) - , fData(data) + , fStream(stream) + , fIndex(index) { }; void onGetFamilyName(SkString* familyName) const override { @@ -386,20 +387,18 @@ public: } void onGetFontDescriptor(SkFontDescriptor* desc, bool* serialize) const override { + desc->setFontIndex(fIndex); *serialize = true; } SkStreamAsset* onOpenStream(int* ttcIndex) const override { - *ttcIndex = fData->getIndex(); - return fData->duplicateStream(); - } - - SkFontData* onCreateFontData() const override { - return new SkFontData(*fData.get()); + *ttcIndex = fIndex; + return fStream->duplicate(); } private: - const SkAutoTDelete<const SkFontData> fData; + SkAutoTDelete<SkStreamAsset> fStream; + int fIndex; typedef SkTypeface_FreeType INHERITED; }; @@ -421,6 +420,7 @@ public: desc->setFamilyName(get_string(fPattern, FC_FAMILY)); desc->setFullName(get_string(fPattern, FC_FULLNAME)); desc->setPostscriptName(get_string(fPattern, FC_POSTSCRIPT_NAME)); + desc->setFontIndex(get_int(fPattern, FC_INDEX, 0)); *serialize = false; } @@ -822,12 +822,12 @@ protected: SkFontStyle style; bool isFixedWidth = false; - if (!fScanner.scanFont(stream, ttcIndex, NULL, &style, &isFixedWidth, NULL)) { + if (!fScanner.scanFont(stream, ttcIndex, NULL, &style, &isFixedWidth)) { return NULL; } - return SkNEW_ARGS(SkTypeface_stream, (new SkFontData(stream.detach(), ttcIndex, NULL, 0), - style, isFixedWidth)); + return SkNEW_ARGS(SkTypeface_stream, (style, isFixedWidth, ttcIndex, + static_cast<SkStreamAsset*>(stream.detach()))); } SkTypeface* onCreateFromData(SkData* data, int ttcIndex) const override { @@ -838,23 +838,6 @@ protected: return this->createFromStream(SkStream::NewFromFile(path), ttcIndex); } - SkTypeface* onCreateFromFontData(SkFontData* fontData) const override { - SkStreamAsset* stream(fontData->getStream()); - const size_t length = stream->getLength(); - if (length <= 0 || (1u << 30) < length) { - return NULL; - } - - const int ttcIndex = fontData->getIndex(); - SkFontStyle style; - bool isFixedWidth = false; - if (!fScanner.scanFont(stream, ttcIndex, NULL, &style, &isFixedWidth, NULL)) { - return NULL; - } - - return SkNEW_ARGS(SkTypeface_stream, (fontData, style, isFixedWidth)); - } - virtual SkTypeface* onLegacyCreateTypeface(const char familyName[], unsigned styleBits) const override { bool bold = styleBits & SkTypeface::kBold; diff --git a/src/ports/SkTypeface_win_dw.cpp b/src/ports/SkTypeface_win_dw.cpp index 25de1b6578..cf45c16b80 100644 --- a/src/ports/SkTypeface_win_dw.cpp +++ b/src/ports/SkTypeface_win_dw.cpp @@ -42,6 +42,7 @@ void DWriteFontTypeface::onGetFontDescriptor(SkFontDescriptor* desc, sk_get_locale_string(familyNames.get(), NULL/*fMgr->fLocaleName.get()*/, &utf8FamilyName); desc->setFamilyName(utf8FamilyName.c_str()); + desc->setFontIndex(fDWriteFontFace->GetIndex()); *isLocalStream = SkToBool(fDWriteFontFileLoader.get()); } |