Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(50)

Side by Side Diff: third_party/woff2/src/font.cc

Issue 954163002: Add woff2 to src/third_party (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase as third_party/ots/src/woff2.cc has changed Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/woff2/src/font.h ('k') | third_party/woff2/src/glyph.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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 // Font management utilities
16
17 #include "./font.h"
18
19 #include <algorithm>
20
21 #include "./buffer.h"
22 #include "./port.h"
23 #include "./store_bytes.h"
24 #include "./table_tags.h"
25
26 namespace woff2 {
27
28 Font::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
33 const 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
38 bool ReadFont(const uint8_t* data, size_t len, Font* font) {
39 Buffer file(data, len);
40
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)) {
46 return FONT_COMPRESSION_FAILURE();
47 }
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)) {
56 return FONT_COMPRESSION_FAILURE();
57 }
58 if ((table.offset & 3) != 0 ||
59 table.length > len ||
60 len - table.length < table.offset) {
61 return FONT_COMPRESSION_FAILURE();
62 }
63 intervals[table.offset] = table.length;
64 table.data = data + table.offset;
65 if (font->tables.find(table.tag) != font->tables.end()) {
66 return FONT_COMPRESSION_FAILURE();
67 }
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) {
75 return FONT_COMPRESSION_FAILURE();
76 }
77 last_offset = i.first + i.second;
78 }
79 return true;
80 }
81
82 size_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
93 bool WriteFont(const Font& font, uint8_t* dst, size_t dst_size) {
94 if (dst_size < 12ULL + 16ULL * font.num_tables) {
95 return FONT_COMPRESSION_FAILURE();
96 }
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) {
114 return FONT_COMPRESSION_FAILURE();
115 }
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) {
120 return FONT_COMPRESSION_FAILURE();
121 }
122 memset(dst + table.offset + table.length, 0, padding_size);
123 }
124 return true;
125 }
126
127 int 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 = IndexFormat(font);
134 int num_glyphs = (loca_table->length / (index_fmt == 0 ? 2 : 4)) - 1;
135 return num_glyphs;
136 }
137
138 int IndexFormat(const Font& font) {
139 const Font::Table* head_table = font.FindTable(kHeadTableTag);
140 if (head_table == NULL) {
141 return 0;
142 }
143 return head_table->data[51];
144 }
145
146 bool GetGlyphData(const Font& font, int glyph_index,
147 const uint8_t** glyph_data, size_t* glyph_size) {
148 if (glyph_index < 0) {
149 return FONT_COMPRESSION_FAILURE();
150 }
151 const Font::Table* head_table = font.FindTable(kHeadTableTag);
152 const Font::Table* loca_table = font.FindTable(kLocaTableTag);
153 const Font::Table* glyf_table = font.FindTable(kGlyfTableTag);
154 if (head_table == NULL || loca_table == NULL || glyf_table == NULL ||
155 head_table->length < 52) {
156 return FONT_COMPRESSION_FAILURE();
157 }
158
159 int index_fmt = IndexFormat(font);
160
161 Buffer loca_buf(loca_table->data, loca_table->length);
162 if (index_fmt == 0) {
163 uint16_t offset1, offset2;
164 if (!loca_buf.Skip(2 * glyph_index) ||
165 !loca_buf.ReadU16(&offset1) ||
166 !loca_buf.ReadU16(&offset2) ||
167 offset2 < offset1 ||
168 2 * offset2 > glyf_table->length) {
169 return FONT_COMPRESSION_FAILURE();
170 }
171 *glyph_data = glyf_table->data + 2 * offset1;
172 *glyph_size = 2 * (offset2 - offset1);
173 } else {
174 uint32_t offset1, offset2;
175 if (!loca_buf.Skip(4 * glyph_index) ||
176 !loca_buf.ReadU32(&offset1) ||
177 !loca_buf.ReadU32(&offset2) ||
178 offset2 < offset1 ||
179 offset2 > glyf_table->length) {
180 return FONT_COMPRESSION_FAILURE();
181 }
182 *glyph_data = glyf_table->data + offset1;
183 *glyph_size = offset2 - offset1;
184 }
185 return true;
186 }
187
188 bool RemoveDigitalSignature(Font* font) {
189 std::map<uint32_t, Font::Table>::iterator it =
190 font->tables.find(kDsigTableTag);
191 if (it != font->tables.end()) {
192 font->tables.erase(it);
193 font->num_tables = font->tables.size();
194 }
195 return true;
196 }
197
198 } // namespace woff2
OLDNEW
« no previous file with comments | « third_party/woff2/src/font.h ('k') | third_party/woff2/src/glyph.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698