michael@0: /* michael@0: * Copyright 2012 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #ifndef SkOTUtils_DEFINED michael@0: #define SkOTUtils_DEFINED michael@0: michael@0: #include "SkOTTableTypes.h" michael@0: #include "SkOTTable_name.h" michael@0: #include "SkTypeface.h" michael@0: michael@0: class SkData; michael@0: class SkStream; michael@0: michael@0: struct SkOTUtils { michael@0: /** michael@0: * Calculates the OpenType checksum for data. michael@0: */ michael@0: static uint32_t CalcTableChecksum(SK_OT_ULONG *data, size_t length); michael@0: michael@0: /** michael@0: * Renames an sfnt font. On failure (invalid data or not an sfnt font) michael@0: * returns NULL. michael@0: * michael@0: * Essentially, this removes any existing 'name' table and replaces it michael@0: * with a new one in which FontFamilyName, FontSubfamilyName, michael@0: * UniqueFontIdentifier, FullFontName, and PostscriptName are fontName. michael@0: * michael@0: * The new 'name' table records will be written with the Windows, michael@0: * UnicodeBMPUCS2, and English_UnitedStates settings. michael@0: * michael@0: * fontName and fontNameLen must be specified in terms of ASCII chars. michael@0: */ michael@0: static SkData* RenameFont(SkStream* fontData, const char* fontName, int fontNameLen); michael@0: michael@0: /** An implementation of LocalizedStrings which obtains it's data from a 'name' table. */ michael@0: class LocalizedStrings_NameTable : public SkTypeface::LocalizedStrings { michael@0: public: michael@0: /** Takes ownership of the nameTableData and will free it with SK_DELETE. */ michael@0: LocalizedStrings_NameTable(SkOTTableName* nameTableData, michael@0: SkOTTableName::Record::NameID::Predefined::Value types[], michael@0: int typesCount) michael@0: : fTypes(types), fTypesCount(typesCount), fTypesIndex(0) michael@0: , fNameTableData(nameTableData), fFamilyNameIter(*nameTableData, fTypes[fTypesIndex]) michael@0: { } michael@0: michael@0: /** Creates an iterator over all the family names in the 'name' table of a typeface. michael@0: * If no valid 'name' table can be found, returns NULL. michael@0: */ michael@0: static LocalizedStrings_NameTable* CreateForFamilyNames(const SkTypeface& typeface); michael@0: michael@0: virtual bool next(SkTypeface::LocalizedString* localizedString) SK_OVERRIDE; michael@0: private: michael@0: static SkOTTableName::Record::NameID::Predefined::Value familyNameTypes[3]; michael@0: michael@0: SkOTTableName::Record::NameID::Predefined::Value* fTypes; michael@0: int fTypesCount; michael@0: int fTypesIndex; michael@0: SkAutoTDeleteArray fNameTableData; michael@0: SkOTTableName::Iterator fFamilyNameIter; michael@0: }; michael@0: michael@0: /** An implementation of LocalizedStrings which has one name. */ michael@0: class LocalizedStrings_SingleName : public SkTypeface::LocalizedStrings { michael@0: public: michael@0: LocalizedStrings_SingleName(SkString name, SkString language) michael@0: : fName(name), fLanguage(language), fHasNext(true) michael@0: { } michael@0: michael@0: virtual bool next(SkTypeface::LocalizedString* localizedString) SK_OVERRIDE { michael@0: localizedString->fString = fName; michael@0: localizedString->fLanguage = fLanguage; michael@0: michael@0: bool hadNext = fHasNext; michael@0: fHasNext = false; michael@0: return hadNext; michael@0: } michael@0: michael@0: private: michael@0: SkString fName; michael@0: SkString fLanguage; michael@0: bool fHasNext; michael@0: }; michael@0: }; michael@0: michael@0: #endif