Home | History | Annotate | Download | only in sfnt
      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_OS_2_V4.h"
     13 #include "SkOTTable_name.h"
     14 #include "SkTypeface.h"
     15 
     16 class SkData;
     17 class SkStream;
     18 struct SkAdvancedTypefaceMetrics;
     19 
     20 struct SkOTUtils {
     21     /**
     22       *  Calculates the OpenType checksum for data.
     23       */
     24     static uint32_t CalcTableChecksum(SK_OT_ULONG *data, size_t length);
     25 
     26     /**
     27       *  Renames an sfnt font. On failure (invalid data or not an sfnt font)
     28       *  returns nullptr.
     29       *
     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.
     38       *
     39       *  Does not affect fontData's ownership.
     40       */
     41     static SkData* RenameFont(SkStreamAsset* fontData, const char* fontName, int fontNameLen);
     42 
     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.
     55          *  If no valid 'name' table can be found, returns nullptr.
     56          */
     57         static LocalizedStrings_NameTable* CreateForFamilyNames(const SkTypeface& typeface);
     58 
     59         bool next(SkTypeface::LocalizedString* localizedString) override;
     60     private:
     61         static SkOTTableName::Record::NameID::Predefined::Value familyNameTypes[3];
     62 
     63         SkOTTableName::Record::NameID::Predefined::Value* fTypes;
     64         int fTypesCount;
     65         int fTypesIndex;
     66         std::unique_ptr<SkOTTableName[]> fNameTableData;
     67         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 
     77         bool next(SkTypeface::LocalizedString* localizedString) override {
     78             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     };
     91 
     92     static void SetAdvancedTypefaceFlags(SkOTTableOS2_V4::Type fsType,
     93                                          SkAdvancedTypefaceMetrics* info);
     94 };
     95 
     96 #endif
     97