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" |
Hal Canary | 4a851ca | 2017-11-09 11:09:34 -0500 | [diff] [blame^] | 12 | #include "SkOTTable_OS_2_V4.h" |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 13 | #include "SkOTTable_name.h" |
| 14 | #include "SkTypeface.h" |
| 15 | |
bungeman@google.com | a544f29 | 2012-11-20 18:52:23 +0000 | [diff] [blame] | 16 | class SkData; |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 17 | class SkStream; |
Hal Canary | 4a851ca | 2017-11-09 11:09:34 -0500 | [diff] [blame^] | 18 | struct SkAdvancedTypefaceMetrics; |
bungeman@google.com | 8ec9956 | 2012-02-07 21:30:21 +0000 | [diff] [blame] | 19 | |
| 20 | struct SkOTUtils { |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 21 | /** |
| 22 | * Calculates the OpenType checksum for data. |
| 23 | */ |
bungeman@google.com | 8ec9956 | 2012-02-07 21:30:21 +0000 | [diff] [blame] | 24 | static uint32_t CalcTableChecksum(SK_OT_ULONG *data, size_t length); |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 25 | |
| 26 | /** |
| 27 | * Renames an sfnt font. On failure (invalid data or not an sfnt font) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 28 | * returns nullptr. |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 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. |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 38 | * |
| 39 | * Does not affect fontData's ownership. |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 40 | */ |
bungeman | 5f213d9 | 2015-01-27 05:39:10 -0800 | [diff] [blame] | 41 | static SkData* RenameFont(SkStreamAsset* fontData, const char* fontName, int fontNameLen); |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 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(SkOTTableName* nameTableData, |
| 48 | SkOTTableName::Record::NameID::Predefined::Value types[], |
| 49 | int typesCount) |
| 50 | : fTypes(types), fTypesCount(typesCount), fTypesIndex(0) |
| 51 | , fNameTableData(nameTableData), fFamilyNameIter(*nameTableData, fTypes[fTypesIndex]) |
| 52 | { } |
| 53 | |
| 54 | /** 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] | 55 | * If no valid 'name' table can be found, returns nullptr. |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 56 | */ |
| 57 | static LocalizedStrings_NameTable* CreateForFamilyNames(const SkTypeface& typeface); |
| 58 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 59 | bool next(SkTypeface::LocalizedString* localizedString) override; |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 60 | private: |
| 61 | static SkOTTableName::Record::NameID::Predefined::Value familyNameTypes[3]; |
| 62 | |
| 63 | SkOTTableName::Record::NameID::Predefined::Value* fTypes; |
| 64 | int fTypesCount; |
| 65 | int fTypesIndex; |
Ben Wagner | 7ecc596 | 2016-11-02 17:07:33 -0400 | [diff] [blame] | 66 | std::unique_ptr<SkOTTableName[]> fNameTableData; |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 67 | SkOTTableName::Iterator fFamilyNameIter; |
| 68 | }; |
| 69 | |
| 70 | /** An implementation of LocalizedStrings which has one name. */ |
| 71 | class LocalizedStrings_SingleName : public SkTypeface::LocalizedStrings { |
| 72 | public: |
| 73 | LocalizedStrings_SingleName(SkString name, SkString language) |
| 74 | : fName(name), fLanguage(language), fHasNext(true) |
| 75 | { } |
| 76 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 77 | bool next(SkTypeface::LocalizedString* localizedString) override { |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 78 | localizedString->fString = fName; |
| 79 | localizedString->fLanguage = fLanguage; |
| 80 | |
| 81 | bool hadNext = fHasNext; |
| 82 | fHasNext = false; |
| 83 | return hadNext; |
| 84 | } |
| 85 | |
| 86 | private: |
| 87 | SkString fName; |
| 88 | SkString fLanguage; |
| 89 | bool fHasNext; |
| 90 | }; |
Hal Canary | 4a851ca | 2017-11-09 11:09:34 -0500 | [diff] [blame^] | 91 | |
| 92 | static void SetAdvanvedTypefaceFlags(SkOTTableOS2_V4::Type fsType, |
| 93 | SkAdvancedTypefaceMetrics* info); |
bungeman@google.com | 8ec9956 | 2012-02-07 21:30:21 +0000 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | #endif |