blob: 26e6e61c21953228513907a54c566e6d78bd7c26 [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
11#include "SkOTTableTypes.h"
Hal Canary4a851ca2017-11-09 11:09:34 -050012#include "SkOTTable_OS_2_V4.h"
bungeman@google.coma9802692013-08-07 02:45:25 +000013#include "SkOTTable_name.h"
14#include "SkTypeface.h"
15
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. */
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.
halcanary96fcdcc2015-08-27 07:41:13 -070055 * If no valid 'name' table can be found, returns nullptr.
bungeman@google.coma9802692013-08-07 02:45:25 +000056 */
57 static LocalizedStrings_NameTable* CreateForFamilyNames(const SkTypeface& typeface);
58
mtklein36352bf2015-03-25 18:17:31 -070059 bool next(SkTypeface::LocalizedString* localizedString) override;
bungeman@google.coma9802692013-08-07 02:45:25 +000060 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 Wagner7ecc5962016-11-02 17:07:33 -040066 std::unique_ptr<SkOTTableName[]> fNameTableData;
bungeman@google.coma9802692013-08-07 02:45:25 +000067 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
mtklein36352bf2015-03-25 18:17:31 -070077 bool next(SkTypeface::LocalizedString* localizedString) override {
bungeman@google.coma9802692013-08-07 02:45:25 +000078 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 Canary4a851ca2017-11-09 11:09:34 -050091
92 static void SetAdvanvedTypefaceFlags(SkOTTableOS2_V4::Type fsType,
93 SkAdvancedTypefaceMetrics* info);
bungeman@google.com8ec99562012-02-07 21:30:21 +000094};
95
96#endif