1 /* 2 * Copyright 2012 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 SkOTUtils_DEFINED 9 #define SkOTUtils_DEFINED 10 11 #include "SkOTTableTypes.h" 12 #include "SkOTTable_OS_2_V4.h" 13 #include "SkOTTable_name.h" 14 #include "SkTypeface.h" 15 16 class SkData; 17 class SkStream; 18 struct SkAdvancedTypefaceMetrics; 19 20 struct SkOTUtils { 21 /** 22 * Calculates the OpenType checksum for data. 23 */ 24 static uint32_t CalcTableChecksum(SK_OT_ULONG *data, size_t length); 25 26 /** 27 * Renames an sfnt font. On failure (invalid data or not an sfnt font) 28 * returns nullptr. 29 * 30 * Essentially, this removes any existing 'name' table and replaces it 31 * with a new one in which FontFamilyName, FontSubfamilyName, 32 * UniqueFontIdentifier, FullFontName, and PostscriptName are fontName. 33 * 34 * The new 'name' table records will be written with the Windows, 35 * UnicodeBMPUCS2, and English_UnitedStates settings. 36 * 37 * fontName and fontNameLen must be specified in terms of ASCII chars. 38 * 39 * Does not affect fontData's ownership. 40 */ 41 static SkData* RenameFont(SkStreamAsset* fontData, const char* fontName, int fontNameLen); 42 43 /** An implementation of LocalizedStrings which obtains it's data from a 'name' table. */ 44 class LocalizedStrings_NameTable : public SkTypeface::LocalizedStrings { 45 public: 46 /** Takes ownership of the nameTableData and will free it with SK_DELETE. */ 47 LocalizedStrings_NameTable(std::unique_ptr<uint8_t[]> nameTableData, size_t size, 48 SK_OT_USHORT types[], 49 int typesCount) 50 : fTypes(types), fTypesCount(typesCount), fTypesIndex(0) 51 , fNameTableData(std::move(nameTableData)) 52 , fFamilyNameIter(fNameTableData.get(), size, fTypes[fTypesIndex]) 53 { } 54 55 /** Creates an iterator over all data in the 'name' table of a typeface. 56 * If no valid 'name' table can be found, returns nullptr. 57 */ 58 static sk_sp<LocalizedStrings_NameTable> Make( 59 const SkTypeface& typeface, 60 SK_OT_USHORT types[], 61 int typesCount); 62 63 /** Creates an iterator over all the family names in the 'name' table of a typeface. 64 * If no valid 'name' table can be found, returns nullptr. 65 */ 66 static sk_sp<LocalizedStrings_NameTable> MakeForFamilyNames(const SkTypeface& typeface); 67 68 bool next(SkTypeface::LocalizedString* localizedString) override; 69 private: 70 static SK_OT_USHORT familyNameTypes[3]; 71 72 SK_OT_USHORT* fTypes; 73 int fTypesCount; 74 int fTypesIndex; 75 std::unique_ptr<uint8_t[]> fNameTableData; 76 SkOTTableName::Iterator fFamilyNameIter; 77 }; 78 79 /** An implementation of LocalizedStrings which has one name. */ 80 class LocalizedStrings_SingleName : public SkTypeface::LocalizedStrings { 81 public: 82 LocalizedStrings_SingleName(SkString name, SkString language) 83 : fName(name), fLanguage(language), fHasNext(true) 84 { } 85 86 bool next(SkTypeface::LocalizedString* localizedString) override { 87 localizedString->fString = fName; 88 localizedString->fLanguage = fLanguage; 89 90 bool hadNext = fHasNext; 91 fHasNext = false; 92 return hadNext; 93 } 94 95 private: 96 SkString fName; 97 SkString fLanguage; 98 bool fHasNext; 99 }; 100 101 static void SetAdvancedTypefaceFlags(SkOTTableOS2_V4::Type fsType, 102 SkAdvancedTypefaceMetrics* info); 103 }; 104 105 #endif 106