bungeman@google.com | 8ec9956 | 2012-02-07 21:30:21 +0000 | [diff] [blame] | 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" |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 12 | #include "SkOTTable_name.h" |
| 13 | #include "SkTypeface.h" |
| 14 | |
bungeman@google.com | a544f29 | 2012-11-20 18:52:23 +0000 | [diff] [blame] | 15 | class SkData; |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 16 | class SkStream; |
bungeman@google.com | 8ec9956 | 2012-02-07 21:30:21 +0000 | [diff] [blame] | 17 | |
| 18 | struct SkOTUtils { |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 19 | /** |
| 20 | * Calculates the OpenType checksum for data. |
| 21 | */ |
bungeman@google.com | 8ec9956 | 2012-02-07 21:30:21 +0000 | [diff] [blame] | 22 | static uint32_t CalcTableChecksum(SK_OT_ULONG *data, size_t length); |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * Renames an sfnt font. On failure (invalid data or not an sfnt font) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 26 | * returns nullptr. |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 27 | * |
| 28 | * Essentially, this removes any existing 'name' table and replaces it |
| 29 | * with a new one in which FontFamilyName, FontSubfamilyName, |
| 30 | * UniqueFontIdentifier, FullFontName, and PostscriptName are fontName. |
| 31 | * |
| 32 | * The new 'name' table records will be written with the Windows, |
| 33 | * UnicodeBMPUCS2, and English_UnitedStates settings. |
| 34 | * |
| 35 | * fontName and fontNameLen must be specified in terms of ASCII chars. |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 36 | * |
| 37 | * Does not affect fontData's ownership. |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 38 | */ |
bungeman | 5f213d9 | 2015-01-27 05:39:10 -0800 | [diff] [blame] | 39 | static SkData* RenameFont(SkStreamAsset* fontData, const char* fontName, int fontNameLen); |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 40 | |
| 41 | /** An implementation of LocalizedStrings which obtains it's data from a 'name' table. */ |
| 42 | class LocalizedStrings_NameTable : public SkTypeface::LocalizedStrings { |
| 43 | public: |
| 44 | /** Takes ownership of the nameTableData and will free it with SK_DELETE. */ |
| 45 | LocalizedStrings_NameTable(SkOTTableName* nameTableData, |
| 46 | SkOTTableName::Record::NameID::Predefined::Value types[], |
| 47 | int typesCount) |
| 48 | : fTypes(types), fTypesCount(typesCount), fTypesIndex(0) |
| 49 | , fNameTableData(nameTableData), fFamilyNameIter(*nameTableData, fTypes[fTypesIndex]) |
| 50 | { } |
| 51 | |
| 52 | /** Creates an iterator over all the family names in the 'name' table of a typeface. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 53 | * If no valid 'name' table can be found, returns nullptr. |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 54 | */ |
| 55 | static LocalizedStrings_NameTable* CreateForFamilyNames(const SkTypeface& typeface); |
| 56 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 57 | bool next(SkTypeface::LocalizedString* localizedString) override; |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 58 | private: |
| 59 | static SkOTTableName::Record::NameID::Predefined::Value familyNameTypes[3]; |
| 60 | |
| 61 | SkOTTableName::Record::NameID::Predefined::Value* fTypes; |
| 62 | int fTypesCount; |
| 63 | int fTypesIndex; |
| 64 | SkAutoTDeleteArray<SkOTTableName> fNameTableData; |
| 65 | SkOTTableName::Iterator fFamilyNameIter; |
| 66 | }; |
| 67 | |
| 68 | /** An implementation of LocalizedStrings which has one name. */ |
| 69 | class LocalizedStrings_SingleName : public SkTypeface::LocalizedStrings { |
| 70 | public: |
| 71 | LocalizedStrings_SingleName(SkString name, SkString language) |
| 72 | : fName(name), fLanguage(language), fHasNext(true) |
| 73 | { } |
| 74 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 75 | bool next(SkTypeface::LocalizedString* localizedString) override { |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 76 | localizedString->fString = fName; |
| 77 | localizedString->fLanguage = fLanguage; |
| 78 | |
| 79 | bool hadNext = fHasNext; |
| 80 | fHasNext = false; |
| 81 | return hadNext; |
| 82 | } |
| 83 | |
| 84 | private: |
| 85 | SkString fName; |
| 86 | SkString fLanguage; |
| 87 | bool fHasNext; |
| 88 | }; |
bungeman@google.com | 8ec9956 | 2012-02-07 21:30:21 +0000 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | #endif |