Roderick Sheeter | 437bbad | 2013-11-19 14:32:56 -0800 | [diff] [blame] | 1 | // Copyright 2013 Google Inc. All Rights Reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | // |
| 15 | // Data model for a font file in sfnt format, reading and writing functions and |
| 16 | // accessors for the glyph data. |
| 17 | |
Zoltan Szabadka | 494c85c | 2014-03-20 14:35:41 +0100 | [diff] [blame] | 18 | #ifndef WOFF2_FONT_H_ |
| 19 | #define WOFF2_FONT_H_ |
Roderick Sheeter | 437bbad | 2013-11-19 14:32:56 -0800 | [diff] [blame] | 20 | |
| 21 | #include <stddef.h> |
| 22 | #include <inttypes.h> |
| 23 | #include <map> |
| 24 | #include <vector> |
| 25 | |
| 26 | namespace woff2 { |
| 27 | |
| 28 | // Tags of popular tables. |
| 29 | static const uint32_t kGlyfTableTag = 0x676c7966; |
| 30 | static const uint32_t kHeadTableTag = 0x68656164; |
| 31 | static const uint32_t kLocaTableTag = 0x6c6f6361; |
| 32 | |
| 33 | // Represents an sfnt font file. Only the table directory is parsed, for the |
| 34 | // table data we only store a raw pointer, therefore a font object is valid only |
| 35 | // as long the data from which it was parsed is around. |
| 36 | struct Font { |
| 37 | uint32_t flavor; |
| 38 | uint16_t num_tables; |
| 39 | |
| 40 | struct Table { |
| 41 | uint32_t tag; |
| 42 | uint32_t checksum; |
| 43 | uint32_t offset; |
| 44 | uint32_t length; |
| 45 | const uint8_t* data; |
| 46 | |
| 47 | // Buffer used to mutate the data before writing out. |
| 48 | std::vector<uint8_t> buffer; |
| 49 | }; |
| 50 | std::map<uint32_t, Table> tables; |
| 51 | |
| 52 | Table* FindTable(uint32_t tag); |
| 53 | const Table* FindTable(uint32_t tag) const; |
| 54 | }; |
| 55 | |
| 56 | // Parses the font from the given data. Returns false on parsing failure or |
| 57 | // buffer overflow. The font is valid only so long the input data pointer is |
| 58 | // valid. |
| 59 | bool ReadFont(const uint8_t* data, size_t len, Font* font); |
| 60 | |
| 61 | // Returns the file size of the font. |
| 62 | size_t FontFileSize(const Font& font); |
| 63 | |
| 64 | // Writes the font into the specified dst buffer. The dst_size should be the |
| 65 | // same as returned by FontFileSize(). Returns false upon buffer overflow (which |
| 66 | // should not happen if dst_size was computed by FontFileSize()). |
| 67 | bool WriteFont(const Font& font, uint8_t* dst, size_t dst_size); |
| 68 | |
| 69 | // Returns the number of glyphs in the font. |
| 70 | // NOTE: Currently this works only for TrueType-flavored fonts, will return |
| 71 | // zero for CFF-flavored fonts. |
| 72 | int NumGlyphs(const Font& font); |
| 73 | |
| 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 | |
| 79 | } // namespace woff2 |
| 80 | |
Zoltan Szabadka | 494c85c | 2014-03-20 14:35:41 +0100 | [diff] [blame] | 81 | #endif // WOFF2_FONT_H_ |