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
18#ifndef WOFF2_FONT_H_
19#define WOFF2_FONT_H_
20
21#include <stddef.h>
22#include <inttypes.h>
23#include <map>
24#include <vector>
25
26namespace woff2 {
27
28// Represents an sfnt font file. Only the table directory is parsed, for the
29// table data we only store a raw pointer, therefore a font object is valid only
30// as long the data from which it was parsed is around.
31struct Font {
32  uint32_t flavor;
33  uint16_t num_tables;
34
35  struct Table {
36    uint32_t tag;
37    uint32_t checksum;
38    uint32_t offset;
39    uint32_t length;
40    const uint8_t* data;
41
42    // Buffer used to mutate the data before writing out.
43    std::vector<uint8_t> buffer;
44  };
45  std::map<uint32_t, Table> tables;
46
47  Table* FindTable(uint32_t tag);
48  const Table* FindTable(uint32_t tag) const;
49};
50
51// Parses the font from the given data. Returns false on parsing failure or
52// buffer overflow. The font is valid only so long the input data pointer is
53// valid.
54bool ReadFont(const uint8_t* data, size_t len, Font* font);
55
56// Returns the file size of the font.
57size_t FontFileSize(const Font& font);
58
59// Writes the font into the specified dst buffer. The dst_size should be the
60// same as returned by FontFileSize(). Returns false upon buffer overflow (which
61// should not happen if dst_size was computed by FontFileSize()).
62bool WriteFont(const Font& font, uint8_t* dst, size_t dst_size);
63
64// Returns the number of glyphs in the font.
65// NOTE: Currently this works only for TrueType-flavored fonts, will return
66// zero for CFF-flavored fonts.
67int NumGlyphs(const Font& font);
68
69// Sets *glyph_data and *glyph_size to point to the location of the glyph data
70// with the given index. Returns false if the glyph is not found.
71bool GetGlyphData(const Font& font, int glyph_index,
72                  const uint8_t** glyph_data, size_t* glyph_size);
73
74// Removes the digital signature (DSIG) table
75bool RemoveDigitalSignature(Font* font);
76
77} // namespace woff2
78
79#endif  // WOFF2_FONT_H_
80