blob: 4825fbeb2f74a925ff15c1f36fa6b793ddb12b03 [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"
bungeman@google.coma9802692013-08-07 02:45:25 +000012#include "SkOTTable_name.h"
13#include "SkTypeface.h"
14
bungeman@google.coma544f292012-11-20 18:52:23 +000015class SkData;
bungeman@google.coma5501992012-05-18 19:06:41 +000016class SkStream;
bungeman@google.com8ec99562012-02-07 21:30:21 +000017
18struct SkOTUtils {
bungeman@google.coma5501992012-05-18 19:06:41 +000019 /**
20 * Calculates the OpenType checksum for data.
21 */
bungeman@google.com8ec99562012-02-07 21:30:21 +000022 static uint32_t CalcTableChecksum(SK_OT_ULONG *data, size_t length);
bungeman@google.coma5501992012-05-18 19:06:41 +000023
24 /**
25 * Renames an sfnt font. On failure (invalid data or not an sfnt font)
26 * returns NULL.
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.
36 */
bungeman@google.coma544f292012-11-20 18:52:23 +000037 static SkData* RenameFont(SkStream* fontData, const char* fontName, int fontNameLen);
bungeman@google.coma9802692013-08-07 02:45:25 +000038
39 /** An implementation of LocalizedStrings which obtains it's data from a 'name' table. */
40 class LocalizedStrings_NameTable : public SkTypeface::LocalizedStrings {
41 public:
42 /** Takes ownership of the nameTableData and will free it with SK_DELETE. */
43 LocalizedStrings_NameTable(SkOTTableName* nameTableData,
44 SkOTTableName::Record::NameID::Predefined::Value types[],
45 int typesCount)
46 : fTypes(types), fTypesCount(typesCount), fTypesIndex(0)
47 , fNameTableData(nameTableData), fFamilyNameIter(*nameTableData, fTypes[fTypesIndex])
48 { }
49
50 /** Creates an iterator over all the family names in the 'name' table of a typeface.
51 * If no valid 'name' table can be found, returns NULL.
52 */
53 static LocalizedStrings_NameTable* CreateForFamilyNames(const SkTypeface& typeface);
54
55 virtual bool next(SkTypeface::LocalizedString* localizedString) SK_OVERRIDE;
56 private:
57 static SkOTTableName::Record::NameID::Predefined::Value familyNameTypes[3];
58
59 SkOTTableName::Record::NameID::Predefined::Value* fTypes;
60 int fTypesCount;
61 int fTypesIndex;
62 SkAutoTDeleteArray<SkOTTableName> fNameTableData;
63 SkOTTableName::Iterator fFamilyNameIter;
64 };
65
66 /** An implementation of LocalizedStrings which has one name. */
67 class LocalizedStrings_SingleName : public SkTypeface::LocalizedStrings {
68 public:
69 LocalizedStrings_SingleName(SkString name, SkString language)
70 : fName(name), fLanguage(language), fHasNext(true)
71 { }
72
73 virtual bool next(SkTypeface::LocalizedString* localizedString) SK_OVERRIDE {
74 localizedString->fString = fName;
75 localizedString->fLanguage = fLanguage;
76
77 bool hadNext = fHasNext;
78 fHasNext = false;
79 return hadNext;
80 }
81
82 private:
83 SkString fName;
84 SkString fLanguage;
85 bool fHasNext;
86 };
bungeman@google.com8ec99562012-02-07 21:30:21 +000087};
88
89#endif