blob: ef9f15863a4de964de141ca9819f2222f4eed391 [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// Glyph normalization
16
17#include "./normalize.h"
18
19#include <inttypes.h>
20#include <stddef.h>
21
22#include "./ots.h"
23#include "./port.h"
24#include "./font.h"
25#include "./glyph.h"
26#include "./round.h"
27#include "./store_bytes.h"
28
29namespace woff2 {
30
31namespace {
32
33void StoreLoca(int index_fmt, uint32_t value, size_t* offset, uint8_t* dst) {
34 if (index_fmt == 0) {
35 Store16(value >> 1, offset, dst);
36 } else {
37 StoreU32(value, offset, dst);
38 }
39}
40
41void NormalizeSimpleGlyphBoundingBox(Glyph* glyph) {
42 if (glyph->contours.empty() || glyph->contours[0].empty()) {
43 return;
44 }
45 int16_t x_min = glyph->contours[0][0].x;
46 int16_t y_min = glyph->contours[0][0].y;
47 int16_t x_max = x_min;
48 int16_t y_max = y_min;
49 for (const auto& contour : glyph->contours) {
50 for (const auto& point : contour) {
51 if (point.x < x_min) x_min = point.x;
52 if (point.x > x_max) x_max = point.x;
53 if (point.y < y_min) y_min = point.y;
54 if (point.y > y_max) y_max = point.y;
55 }
56 }
57 glyph->x_min = x_min;
58 glyph->y_min = y_min;
59 glyph->x_max = x_max;
60 glyph->y_max = y_max;
61}
62
63} // namespace
64
65bool NormalizeGlyphs(Font* font) {
66 Font::Table* head_table = font->FindTable(kHeadTableTag);
67 Font::Table* glyf_table = font->FindTable(kGlyfTableTag);
68 Font::Table* loca_table = font->FindTable(kLocaTableTag);
69 if (head_table == NULL || loca_table == NULL || glyf_table == NULL) {
70 return OTS_FAILURE();
71 }
72 int index_fmt = head_table->data[51];
73 int num_glyphs = NumGlyphs(*font);
74
75 // We need to allocate a bit more than its original length for the normalized
76 // glyf table, since it can happen that the glyphs in the original table are
77 // 2-byte aligned, while in the normalized table they are 4-byte aligned.
78 // That gives a maximum of 2 bytes increase per glyph. However, there is no
79 // theoretical guarantee that the total size of the flags plus the coordinates
80 // is the smallest possible in the normalized version, so we have to allow
81 // some general overhead.
82 // TODO(user) Figure out some more precise upper bound on the size of
83 // the overhead.
84 size_t max_normalized_glyf_size = 1.1 * glyf_table->length + 2 * num_glyphs;
85
86 glyf_table->buffer.resize(max_normalized_glyf_size);
87 loca_table->buffer.resize(Round4(loca_table->length));
88 uint8_t* glyf_dst = &glyf_table->buffer[0];
89 uint8_t* loca_dst = &loca_table->buffer[0];
90 uint32_t glyf_offset = 0;
91 size_t loca_offset = 0;
92
93 for (int i = 0; i < num_glyphs; ++i) {
94 StoreLoca(index_fmt, glyf_offset, &loca_offset, loca_dst);
95 Glyph glyph;
96 const uint8_t* glyph_data;
97 size_t glyph_size;
98 if (!GetGlyphData(*font, i, &glyph_data, &glyph_size) ||
99 (glyph_size > 0 && !ReadGlyph(glyph_data, glyph_size, &glyph))) {
100 return OTS_FAILURE();
101 }
102 NormalizeSimpleGlyphBoundingBox(&glyph);
103 size_t glyf_dst_size = glyf_table->buffer.size() - glyf_offset;
104 if (!StoreGlyph(glyph, glyf_dst + glyf_offset, &glyf_dst_size)) {
105 return OTS_FAILURE();
106 }
107 glyf_dst_size = Round4(glyf_dst_size);
108 if (glyf_dst_size > std::numeric_limits<uint32_t>::max() ||
109 glyf_offset + static_cast<uint32_t>(glyf_dst_size) < glyf_offset ||
110 (index_fmt == 0 && glyf_offset + glyf_dst_size >= (1UL << 17))) {
111 return OTS_FAILURE();
112 }
113 glyf_offset += glyf_dst_size;
114 }
115 StoreLoca(index_fmt, glyf_offset, &loca_offset, loca_dst);
116
117 glyf_table->buffer.resize(glyf_offset);
118 glyf_table->data = &glyf_table->buffer[0];
119 glyf_table->length = glyf_offset;
120 loca_table->data = &loca_table->buffer[0];
121
122 return true;
123}
124
125bool NormalizeOffsets(Font* font) {
126 uint32_t offset = 12 + 16 * font->num_tables;
127 for (auto& i : font->tables) {
128 i.second.offset = offset;
129 offset += Round4(i.second.length);
130 }
131 return true;
132}
133
134namespace {
135
136uint32_t ComputeChecksum(const uint8_t* buf, size_t size) {
137 uint32_t checksum = 0;
138 for (size_t i = 0; i < size; i += 4) {
139 checksum += ((buf[i] << 24) |
140 (buf[i + 1] << 16) |
141 (buf[i + 2] << 8) |
142 buf[i + 3]);
143 }
144 return checksum;
145}
146
147uint32_t ComputeHeaderChecksum(const Font& font) {
148 uint32_t checksum = font.flavor;
149 uint16_t max_pow2 = font.num_tables ? Log2Floor(font.num_tables) : 0;
150 uint16_t search_range = max_pow2 ? 1 << (max_pow2 + 4) : 0;
151 uint16_t range_shift = (font.num_tables << 4) - search_range;
152 checksum += (font.num_tables << 16 | search_range);
153 checksum += (max_pow2 << 16 | range_shift);
154 for (const auto& i : font.tables) {
155 checksum += i.second.tag;
156 checksum += i.second.checksum;
157 checksum += i.second.offset;
158 checksum += i.second.length;
159 }
160 return checksum;
161}
162
163} // namespace
164
165bool FixChecksums(Font* font) {
166 Font::Table* head_table = font->FindTable(kHeadTableTag);
167 if (head_table == NULL || head_table->length < 12) {
168 return OTS_FAILURE();
169 }
170 head_table->buffer.resize(Round4(head_table->length));
171 uint8_t* head_buf = &head_table->buffer[0];
172 memcpy(head_buf, head_table->data, Round4(head_table->length));
173 head_table->data = head_buf;
174 size_t offset = 8;
175 StoreU32(0, &offset, head_buf);
176 uint32_t file_checksum = 0;
177 for (auto& i : font->tables) {
178 Font::Table* table = &i.second;
179 table->checksum = ComputeChecksum(table->data, table->length);
180 file_checksum += table->checksum;
181 }
182 file_checksum += ComputeHeaderChecksum(*font);
183 offset = 8;
184 StoreU32(0xb1b0afba - file_checksum, &offset, head_buf);
185 return true;
186}
187
188bool NormalizeFont(Font* font) {
189 return (NormalizeGlyphs(font) &&
190 NormalizeOffsets(font) &&
191 FixChecksums(font));
192}
193
194} // namespace woff2