blob: c0fb20606403de6eeb6aea3b16b8b3721793ff5e [file] [log] [blame]
Roderick Sheeter437bbad2013-11-19 14:32:56 -08001// 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// Font management utilities
16
17#include "./font.h"
18
19#include <algorithm>
20
Kenichi Ishibashi142d8882014-05-30 09:06:32 +090021#include "./buffer.h"
Roderick Sheeter437bbad2013-11-19 14:32:56 -080022#include "./port.h"
23#include "./store_bytes.h"
Kenichi Ishibashi142d8882014-05-30 09:06:32 +090024#include "./table_tags.h"
Roderick Sheeter437bbad2013-11-19 14:32:56 -080025
26namespace woff2 {
27
28Font::Table* Font::FindTable(uint32_t tag) {
29 std::map<uint32_t, Font::Table>::iterator it = tables.find(tag);
30 return it == tables.end() ? 0 : &it->second;
31}
32
33const Font::Table* Font::FindTable(uint32_t tag) const {
34 std::map<uint32_t, Font::Table>::const_iterator it = tables.find(tag);
35 return it == tables.end() ? 0 : &it->second;
36}
37
38bool ReadFont(const uint8_t* data, size_t len, Font* font) {
Kenichi Ishibashi142d8882014-05-30 09:06:32 +090039 Buffer file(data, len);
Roderick Sheeter437bbad2013-11-19 14:32:56 -080040
41 // We don't care about the search_range, entry_selector and range_shift
42 // fields, they will always be computed upon writing the font.
43 if (!file.ReadU32(&font->flavor) ||
44 !file.ReadU16(&font->num_tables) ||
45 !file.Skip(6)) {
Kenichi Ishibashi142d8882014-05-30 09:06:32 +090046 return FONT_COMPRESSION_FAILURE();
Roderick Sheeter437bbad2013-11-19 14:32:56 -080047 }
48
49 std::map<uint32_t, uint32_t> intervals;
50 for (uint16_t i = 0; i < font->num_tables; ++i) {
51 Font::Table table;
52 if (!file.ReadU32(&table.tag) ||
53 !file.ReadU32(&table.checksum) ||
54 !file.ReadU32(&table.offset) ||
55 !file.ReadU32(&table.length)) {
Kenichi Ishibashi142d8882014-05-30 09:06:32 +090056 return FONT_COMPRESSION_FAILURE();
Roderick Sheeter437bbad2013-11-19 14:32:56 -080057 }
58 if ((table.offset & 3) != 0 ||
59 table.length > len ||
60 len - table.length < table.offset) {
Kenichi Ishibashi142d8882014-05-30 09:06:32 +090061 return FONT_COMPRESSION_FAILURE();
Roderick Sheeter437bbad2013-11-19 14:32:56 -080062 }
63 intervals[table.offset] = table.length;
64 table.data = data + table.offset;
65 if (font->tables.find(table.tag) != font->tables.end()) {
Kenichi Ishibashi142d8882014-05-30 09:06:32 +090066 return FONT_COMPRESSION_FAILURE();
Roderick Sheeter437bbad2013-11-19 14:32:56 -080067 }
68 font->tables[table.tag] = table;
69 }
70
71 // Check that tables are non-overlapping.
72 uint32_t last_offset = 12UL + 16UL * font->num_tables;
73 for (const auto& i : intervals) {
74 if (i.first < last_offset || i.first + i.second < i.first) {
Kenichi Ishibashi142d8882014-05-30 09:06:32 +090075 return FONT_COMPRESSION_FAILURE();
Roderick Sheeter437bbad2013-11-19 14:32:56 -080076 }
77 last_offset = i.first + i.second;
78 }
79 return true;
80}
81
82size_t FontFileSize(const Font& font) {
83 size_t max_offset = 12ULL + 16ULL * font.num_tables;
84 for (const auto& i : font.tables) {
85 const Font::Table& table = i.second;
86 size_t padding_size = (4 - (table.length & 3)) & 3;
87 size_t end_offset = (padding_size + table.offset) + table.length;
88 max_offset = std::max(max_offset, end_offset);
89 }
90 return max_offset;
91}
92
93bool WriteFont(const Font& font, uint8_t* dst, size_t dst_size) {
94 if (dst_size < 12ULL + 16ULL * font.num_tables) {
Kenichi Ishibashi142d8882014-05-30 09:06:32 +090095 return FONT_COMPRESSION_FAILURE();
Roderick Sheeter437bbad2013-11-19 14:32:56 -080096 }
97 size_t offset = 0;
98 StoreU32(font.flavor, &offset, dst);
99 Store16(font.num_tables, &offset, dst);
100 uint16_t max_pow2 = font.num_tables ? Log2Floor(font.num_tables) : 0;
101 uint16_t search_range = max_pow2 ? 1 << (max_pow2 + 4) : 0;
102 uint16_t range_shift = (font.num_tables << 4) - search_range;
103 Store16(search_range, &offset, dst);
104 Store16(max_pow2, &offset, dst);
105 Store16(range_shift, &offset, dst);
106 for (const auto& i : font.tables) {
107 const Font::Table& table = i.second;
108 StoreU32(table.tag, &offset, dst);
109 StoreU32(table.checksum, &offset, dst);
110 StoreU32(table.offset, &offset, dst);
111 StoreU32(table.length, &offset, dst);
112 if (table.offset + table.length < table.offset ||
113 dst_size < table.offset + table.length) {
Kenichi Ishibashi142d8882014-05-30 09:06:32 +0900114 return FONT_COMPRESSION_FAILURE();
Roderick Sheeter437bbad2013-11-19 14:32:56 -0800115 }
116 memcpy(dst + table.offset, table.data, table.length);
117 size_t padding_size = (4 - (table.length & 3)) & 3;
118 if (table.offset + table.length + padding_size < padding_size ||
119 dst_size < table.offset + table.length + padding_size) {
Kenichi Ishibashi142d8882014-05-30 09:06:32 +0900120 return FONT_COMPRESSION_FAILURE();
Roderick Sheeter437bbad2013-11-19 14:32:56 -0800121 }
122 memset(dst + table.offset + table.length, 0, padding_size);
123 }
124 return true;
125}
126
127int NumGlyphs(const Font& font) {
128 const Font::Table* head_table = font.FindTable(kHeadTableTag);
129 const Font::Table* loca_table = font.FindTable(kLocaTableTag);
130 if (head_table == NULL || loca_table == NULL || head_table->length < 52) {
131 return 0;
132 }
133 int index_fmt = head_table->data[51];
134 return (loca_table->length / (index_fmt == 0 ? 2 : 4)) - 1;
135}
136
137bool GetGlyphData(const Font& font, int glyph_index,
138 const uint8_t** glyph_data, size_t* glyph_size) {
139 if (glyph_index < 0) {
Kenichi Ishibashi142d8882014-05-30 09:06:32 +0900140 return FONT_COMPRESSION_FAILURE();
Roderick Sheeter437bbad2013-11-19 14:32:56 -0800141 }
142 const Font::Table* head_table = font.FindTable(kHeadTableTag);
143 const Font::Table* loca_table = font.FindTable(kLocaTableTag);
144 const Font::Table* glyf_table = font.FindTable(kGlyfTableTag);
145 if (head_table == NULL || loca_table == NULL || glyf_table == NULL ||
146 head_table->length < 52) {
Kenichi Ishibashi142d8882014-05-30 09:06:32 +0900147 return FONT_COMPRESSION_FAILURE();
Roderick Sheeter437bbad2013-11-19 14:32:56 -0800148 }
149 int index_fmt = head_table->data[51];
Kenichi Ishibashi142d8882014-05-30 09:06:32 +0900150 Buffer loca_buf(loca_table->data, loca_table->length);
Roderick Sheeter437bbad2013-11-19 14:32:56 -0800151 if (index_fmt == 0) {
152 uint16_t offset1, offset2;
153 if (!loca_buf.Skip(2 * glyph_index) ||
154 !loca_buf.ReadU16(&offset1) ||
155 !loca_buf.ReadU16(&offset2) ||
156 offset2 < offset1 ||
157 2 * offset2 > glyf_table->length) {
Kenichi Ishibashi142d8882014-05-30 09:06:32 +0900158 return FONT_COMPRESSION_FAILURE();
Roderick Sheeter437bbad2013-11-19 14:32:56 -0800159 }
160 *glyph_data = glyf_table->data + 2 * offset1;
161 *glyph_size = 2 * (offset2 - offset1);
162 } else {
163 uint32_t offset1, offset2;
164 if (!loca_buf.Skip(4 * glyph_index) ||
165 !loca_buf.ReadU32(&offset1) ||
166 !loca_buf.ReadU32(&offset2) ||
167 offset2 < offset1 ||
168 offset2 > glyf_table->length) {
Kenichi Ishibashi142d8882014-05-30 09:06:32 +0900169 return FONT_COMPRESSION_FAILURE();
Roderick Sheeter437bbad2013-11-19 14:32:56 -0800170 }
171 *glyph_data = glyf_table->data + offset1;
172 *glyph_size = offset2 - offset1;
173 }
174 return true;
175}
176
Roderick Sheeter2ec0cb82014-05-07 11:01:07 -0700177bool RemoveDigitalSignature(Font* font) {
178 std::map<uint32_t, Font::Table>::iterator it =
179 font->tables.find(kDsigTableTag);
180 if (it != font->tables.end()) {
181 font->tables.erase(it);
182 font->num_tables = font->tables.size();
183 }
184 return true;
185}
186
Roderick Sheeter437bbad2013-11-19 14:32:56 -0800187} // namespace woff2