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"
12#include "SkOTTable_name.h"
13#include "SkTypeface.h"
14
15class SkData;
16class SkStream;
17
18struct SkOTUtils {
19    /**
20      *  Calculates the OpenType checksum for data.
21      */
22    static uint32_t CalcTableChecksum(SK_OT_ULONG *data, size_t length);
23
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      */
37    static SkData* RenameFont(SkStream* fontData, const char* fontName, int fontNameLen);
38
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    };
87};
88
89#endif
90