font.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* Copyright 2013 Google Inc. All Rights Reserved.
  2. Distributed under MIT license.
  3. See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
  4. */
  5. /* Data model for a font file in sfnt format, reading and writing functions and
  6. accessors for the glyph data. */
  7. #ifndef WOFF2_FONT_H_
  8. #define WOFF2_FONT_H_
  9. #include <stddef.h>
  10. #include <inttypes.h>
  11. #include <map>
  12. #include <vector>
  13. namespace woff2 {
  14. // Represents an sfnt font file. Only the table directory is parsed, for the
  15. // table data we only store a raw pointer, therefore a font object is valid only
  16. // as long the data from which it was parsed is around.
  17. struct Font {
  18. uint32_t flavor;
  19. uint16_t num_tables;
  20. struct Table {
  21. uint32_t tag;
  22. uint32_t checksum;
  23. uint32_t offset;
  24. uint32_t length;
  25. const uint8_t* data;
  26. // Buffer used to mutate the data before writing out.
  27. std::vector<uint8_t> buffer;
  28. // If we've seen this tag/offset before, pointer to the first time we saw it
  29. // If this is the first time we've seen this table, NULL
  30. // Intended use is to bypass re-processing tables
  31. Font::Table* reuse_of;
  32. uint8_t flag_byte;
  33. // Is this table reused by a TTC
  34. bool IsReused() const;
  35. };
  36. std::map<uint32_t, Table> tables;
  37. std::vector<uint32_t> OutputOrderedTags() const;
  38. Table* FindTable(uint32_t tag);
  39. const Table* FindTable(uint32_t tag) const;
  40. };
  41. // Accomodates both singular (OTF, TTF) and collection (TTC) fonts
  42. struct FontCollection {
  43. uint32_t flavor;
  44. uint32_t header_version;
  45. // (offset, first use of table*) pairs
  46. std::map<uint32_t, Font::Table*> tables;
  47. std::vector<Font> fonts;
  48. };
  49. // Parses the font from the given data. Returns false on parsing failure or
  50. // buffer overflow. The font is valid only so long the input data pointer is
  51. // valid. Does NOT support collections.
  52. bool ReadFont(const uint8_t* data, size_t len, Font* font);
  53. // Parses the font from the given data. Returns false on parsing failure or
  54. // buffer overflow. The font is valid only so long the input data pointer is
  55. // valid. Supports collections.
  56. bool ReadFontCollection(const uint8_t* data, size_t len, FontCollection* fonts);
  57. // Returns the file size of the font.
  58. size_t FontFileSize(const Font& font);
  59. size_t FontCollectionFileSize(const FontCollection& font);
  60. // Writes the font into the specified dst buffer. The dst_size should be the
  61. // same as returned by FontFileSize(). Returns false upon buffer overflow (which
  62. // should not happen if dst_size was computed by FontFileSize()).
  63. bool WriteFont(const Font& font, uint8_t* dst, size_t dst_size);
  64. // Write the font at a specific offset
  65. bool WriteFont(const Font& font, size_t* offset, uint8_t* dst, size_t dst_size);
  66. bool WriteFontCollection(const FontCollection& font_collection, uint8_t* dst,
  67. size_t dst_size);
  68. // Returns the number of glyphs in the font.
  69. // NOTE: Currently this works only for TrueType-flavored fonts, will return
  70. // zero for CFF-flavored fonts.
  71. int NumGlyphs(const Font& font);
  72. // Returns the index format of the font
  73. int IndexFormat(const Font& font);
  74. // Sets *glyph_data and *glyph_size to point to the location of the glyph data
  75. // with the given index. Returns false if the glyph is not found.
  76. bool GetGlyphData(const Font& font, int glyph_index,
  77. const uint8_t** glyph_data, size_t* glyph_size);
  78. // Removes the digital signature (DSIG) table
  79. bool RemoveDigitalSignature(Font* font);
  80. } // namespace woff2
  81. #endif // WOFF2_FONT_H_