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