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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkTypeface.h" |
| 12 | #include "src/sfnt/SkOTTableTypes.h" |
| 13 | #include "src/sfnt/SkOTTable_OS_2_V4.h" |
| 14 | #include "src/sfnt/SkOTTable_name.h" |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 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. */ |
Ben Wagner | ad031f5 | 2018-08-20 13:45:57 -0400 | [diff] [blame] | 47 | LocalizedStrings_NameTable(std::unique_ptr<uint8_t[]> nameTableData, size_t size, |
| 48 | SK_OT_USHORT types[], |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 49 | int typesCount) |
| 50 | : fTypes(types), fTypesCount(typesCount), fTypesIndex(0) |
Ben Wagner | ad031f5 | 2018-08-20 13:45:57 -0400 | [diff] [blame] | 51 | , fNameTableData(std::move(nameTableData)) |
| 52 | , fFamilyNameIter(fNameTableData.get(), size, fTypes[fTypesIndex]) |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 53 | { } |
| 54 | |
Ben Wagner | ad031f5 | 2018-08-20 13:45:57 -0400 | [diff] [blame] | 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 | |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 63 | /** 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] | 64 | * If no valid 'name' table can be found, returns nullptr. |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 65 | */ |
Ben Wagner | ad031f5 | 2018-08-20 13:45:57 -0400 | [diff] [blame] | 66 | static sk_sp<LocalizedStrings_NameTable> MakeForFamilyNames(const SkTypeface& typeface); |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 67 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 68 | bool next(SkTypeface::LocalizedString* localizedString) override; |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 69 | private: |
Ben Wagner | ad031f5 | 2018-08-20 13:45:57 -0400 | [diff] [blame] | 70 | static SK_OT_USHORT familyNameTypes[3]; |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 71 | |
Ben Wagner | ad031f5 | 2018-08-20 13:45:57 -0400 | [diff] [blame] | 72 | SK_OT_USHORT* fTypes; |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 73 | int fTypesCount; |
| 74 | int fTypesIndex; |
Ben Wagner | ad031f5 | 2018-08-20 13:45:57 -0400 | [diff] [blame] | 75 | std::unique_ptr<uint8_t[]> fNameTableData; |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 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 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 86 | bool next(SkTypeface::LocalizedString* localizedString) override { |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 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 | }; |
Hal Canary | 4a851ca | 2017-11-09 11:09:34 -0500 | [diff] [blame] | 100 | |
Hal Canary | a865d25 | 2017-11-09 16:16:09 -0500 | [diff] [blame] | 101 | static void SetAdvancedTypefaceFlags(SkOTTableOS2_V4::Type fsType, |
Hal Canary | 4a851ca | 2017-11-09 11:09:34 -0500 | [diff] [blame] | 102 | SkAdvancedTypefaceMetrics* info); |
bungeman@google.com | 8ec9956 | 2012-02-07 21:30:21 +0000 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | #endif |