SkOTUtils.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. #ifndef SkOTUtils_DEFINED
  8. #define SkOTUtils_DEFINED
  9. #include "include/core/SkTypeface.h"
  10. #include "src/sfnt/SkOTTableTypes.h"
  11. #include "src/sfnt/SkOTTable_OS_2_V4.h"
  12. #include "src/sfnt/SkOTTable_name.h"
  13. class SkData;
  14. class SkStream;
  15. struct SkAdvancedTypefaceMetrics;
  16. struct SkOTUtils {
  17. /**
  18. * Calculates the OpenType checksum for data.
  19. */
  20. static uint32_t CalcTableChecksum(SK_OT_ULONG *data, size_t length);
  21. /**
  22. * Renames an sfnt font. On failure (invalid data or not an sfnt font)
  23. * returns nullptr.
  24. *
  25. * Essentially, this removes any existing 'name' table and replaces it
  26. * with a new one in which FontFamilyName, FontSubfamilyName,
  27. * UniqueFontIdentifier, FullFontName, and PostscriptName are fontName.
  28. *
  29. * The new 'name' table records will be written with the Windows,
  30. * UnicodeBMPUCS2, and English_UnitedStates settings.
  31. *
  32. * fontName and fontNameLen must be specified in terms of ASCII chars.
  33. *
  34. * Does not affect fontData's ownership.
  35. */
  36. static SkData* RenameFont(SkStreamAsset* fontData, const char* fontName, int fontNameLen);
  37. /** An implementation of LocalizedStrings which obtains it's data from a 'name' table. */
  38. class LocalizedStrings_NameTable : public SkTypeface::LocalizedStrings {
  39. public:
  40. /** Takes ownership of the nameTableData and will free it with SK_DELETE. */
  41. LocalizedStrings_NameTable(std::unique_ptr<uint8_t[]> nameTableData, size_t size,
  42. SK_OT_USHORT types[],
  43. int typesCount)
  44. : fTypes(types), fTypesCount(typesCount), fTypesIndex(0)
  45. , fNameTableData(std::move(nameTableData))
  46. , fFamilyNameIter(fNameTableData.get(), size, fTypes[fTypesIndex])
  47. { }
  48. /** Creates an iterator over all data in the 'name' table of a typeface.
  49. * If no valid 'name' table can be found, returns nullptr.
  50. */
  51. static sk_sp<LocalizedStrings_NameTable> Make(
  52. const SkTypeface& typeface,
  53. SK_OT_USHORT types[],
  54. int typesCount);
  55. /** Creates an iterator over all the family names in the 'name' table of a typeface.
  56. * If no valid 'name' table can be found, returns nullptr.
  57. */
  58. static sk_sp<LocalizedStrings_NameTable> MakeForFamilyNames(const SkTypeface& typeface);
  59. bool next(SkTypeface::LocalizedString* localizedString) override;
  60. private:
  61. static SK_OT_USHORT familyNameTypes[3];
  62. SK_OT_USHORT* fTypes;
  63. int fTypesCount;
  64. int fTypesIndex;
  65. std::unique_ptr<uint8_t[]> fNameTableData;
  66. SkOTTableName::Iterator fFamilyNameIter;
  67. };
  68. /** An implementation of LocalizedStrings which has one name. */
  69. class LocalizedStrings_SingleName : public SkTypeface::LocalizedStrings {
  70. public:
  71. LocalizedStrings_SingleName(SkString name, SkString language)
  72. : fName(name), fLanguage(language), fHasNext(true)
  73. { }
  74. bool next(SkTypeface::LocalizedString* localizedString) override {
  75. localizedString->fString = fName;
  76. localizedString->fLanguage = fLanguage;
  77. bool hadNext = fHasNext;
  78. fHasNext = false;
  79. return hadNext;
  80. }
  81. private:
  82. SkString fName;
  83. SkString fLanguage;
  84. bool fHasNext;
  85. };
  86. static void SetAdvancedTypefaceFlags(SkOTTableOS2_V4::Type fsType,
  87. SkAdvancedTypefaceMetrics* info);
  88. };
  89. #endif