blob: fb2732385c5518569b79e39b3cfcc16302040014 [file] [log] [blame]
bungeman@google.com8ec99562012-02-07 21:30:21 +00001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -050011#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.coma9802692013-08-07 02:45:25 +000015
bungeman@google.coma544f292012-11-20 18:52:23 +000016class SkData;
bungeman@google.coma5501992012-05-18 19:06:41 +000017class SkStream;
Hal Canary4a851ca2017-11-09 11:09:34 -050018struct SkAdvancedTypefaceMetrics;
bungeman@google.com8ec99562012-02-07 21:30:21 +000019
20struct SkOTUtils {
bungeman@google.coma5501992012-05-18 19:06:41 +000021 /**
22 * Calculates the OpenType checksum for data.
23 */
bungeman@google.com8ec99562012-02-07 21:30:21 +000024 static uint32_t CalcTableChecksum(SK_OT_ULONG *data, size_t length);
bungeman@google.coma5501992012-05-18 19:06:41 +000025
26 /**
27 * Renames an sfnt font. On failure (invalid data or not an sfnt font)
halcanary96fcdcc2015-08-27 07:41:13 -070028 * returns nullptr.
bungeman@google.coma5501992012-05-18 19:06:41 +000029 *
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.
scroggoa1193e42015-01-21 12:09:53 -080038 *
39 * Does not affect fontData's ownership.
bungeman@google.coma5501992012-05-18 19:06:41 +000040 */
bungeman5f213d92015-01-27 05:39:10 -080041 static SkData* RenameFont(SkStreamAsset* fontData, const char* fontName, int fontNameLen);
bungeman@google.coma9802692013-08-07 02:45:25 +000042
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 Wagnerad031f52018-08-20 13:45:57 -040047 LocalizedStrings_NameTable(std::unique_ptr<uint8_t[]> nameTableData, size_t size,
48 SK_OT_USHORT types[],
bungeman@google.coma9802692013-08-07 02:45:25 +000049 int typesCount)
50 : fTypes(types), fTypesCount(typesCount), fTypesIndex(0)
Ben Wagnerad031f52018-08-20 13:45:57 -040051 , fNameTableData(std::move(nameTableData))
52 , fFamilyNameIter(fNameTableData.get(), size, fTypes[fTypesIndex])
bungeman@google.coma9802692013-08-07 02:45:25 +000053 { }
54
Ben Wagnerad031f52018-08-20 13:45:57 -040055 /** 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.coma9802692013-08-07 02:45:25 +000063 /** Creates an iterator over all the family names in the 'name' table of a typeface.
halcanary96fcdcc2015-08-27 07:41:13 -070064 * If no valid 'name' table can be found, returns nullptr.
bungeman@google.coma9802692013-08-07 02:45:25 +000065 */
Ben Wagnerad031f52018-08-20 13:45:57 -040066 static sk_sp<LocalizedStrings_NameTable> MakeForFamilyNames(const SkTypeface& typeface);
bungeman@google.coma9802692013-08-07 02:45:25 +000067
mtklein36352bf2015-03-25 18:17:31 -070068 bool next(SkTypeface::LocalizedString* localizedString) override;
bungeman@google.coma9802692013-08-07 02:45:25 +000069 private:
Ben Wagnerad031f52018-08-20 13:45:57 -040070 static SK_OT_USHORT familyNameTypes[3];
bungeman@google.coma9802692013-08-07 02:45:25 +000071
Ben Wagnerad031f52018-08-20 13:45:57 -040072 SK_OT_USHORT* fTypes;
bungeman@google.coma9802692013-08-07 02:45:25 +000073 int fTypesCount;
74 int fTypesIndex;
Ben Wagnerad031f52018-08-20 13:45:57 -040075 std::unique_ptr<uint8_t[]> fNameTableData;
bungeman@google.coma9802692013-08-07 02:45:25 +000076 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
mtklein36352bf2015-03-25 18:17:31 -070086 bool next(SkTypeface::LocalizedString* localizedString) override {
bungeman@google.coma9802692013-08-07 02:45:25 +000087 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 Canary4a851ca2017-11-09 11:09:34 -0500100
Hal Canarya865d252017-11-09 16:16:09 -0500101 static void SetAdvancedTypefaceFlags(SkOTTableOS2_V4::Type fsType,
Hal Canary4a851ca2017-11-09 11:09:34 -0500102 SkAdvancedTypefaceMetrics* info);
bungeman@google.com8ec99562012-02-07 21:30:21 +0000103};
104
105#endif