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

Side by Side Diff: third_party/sfntly/src/subsetter/subsetter_impl.cc

Issue 8921027: Push subsetter code upstream (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Update path to make build system happy Created 9 years 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 | Annotate | Revision Log
« DEPS ('K') | « third_party/sfntly/src/subsetter/subsetter_impl.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright 2011 Google Inc. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "third_party/sfntly/src/subsetter/subsetter_impl.h"
18
19 #include <string.h>
20
21 #include <algorithm>
22 #include <iterator>
23 #include <map>
24 #include <set>
25
26 #include "third_party/sfntly/src/sfntly/table/bitmap/eblc_table.h"
27 #include "third_party/sfntly/src/sfntly/table/bitmap/ebdt_table.h"
28 #include "third_party/sfntly/src/sfntly/table/bitmap/index_sub_table.h"
29 #include "third_party/sfntly/src/sfntly/table/bitmap/index_sub_table_format1.h"
30 #include "third_party/sfntly/src/sfntly/table/bitmap/index_sub_table_format2.h"
31 #include "third_party/sfntly/src/sfntly/table/bitmap/index_sub_table_format3.h"
32 #include "third_party/sfntly/src/sfntly/table/bitmap/index_sub_table_format4.h"
33 #include "third_party/sfntly/src/sfntly/table/bitmap/index_sub_table_format5.h"
34 #include "third_party/sfntly/src/sfntly/table/core/name_table.h"
35 #include "third_party/sfntly/src/sfntly/tag.h"
36 #include "third_party/sfntly/src/sfntly/data/memory_byte_array.h"
37 #include "third_party/sfntly/src/sfntly/port/memory_input_stream.h"
38 #include "third_party/sfntly/src/sfntly/port/memory_output_stream.h"
39
40 #if defined U_USING_ICU_NAMESPACE
41 U_NAMESPACE_USE
42 #endif
43
44 namespace {
45
46 using namespace sfntly;
47
48 // The bitmap tables must be greater than 16KB to trigger bitmap subsetter.
49 static const int BITMAP_SIZE_THRESHOLD = 16384;
50
51 void ConstructName(UChar* name_part, UnicodeString* name, int32_t name_id) {
52 switch (name_id) {
53 case NameId::kFullFontName:
54 *name = name_part;
55 break;
56 case NameId::kFontFamilyName:
57 case NameId::kPreferredFamily:
58 case NameId::kWWSFamilyName: {
59 UnicodeString original = *name;
60 *name = name_part;
61 *name += original;
62 break;
63 }
64 case NameId::kFontSubfamilyName:
65 case NameId::kPreferredSubfamily:
66 case NameId::kWWSSubfamilyName:
67 *name += name_part;
68 break;
69 default:
70 // This name part is not used to construct font name (e.g. copyright).
71 // Simply ignore it.
72 break;
73 }
74 }
75
76 int32_t HashCode(int32_t platform_id, int32_t encoding_id, int32_t language_id,
77 int32_t name_id) {
78 int32_t result = platform_id << 24 | encoding_id << 16 | language_id << 8;
79 if (name_id == NameId::kFullFontName) {
80 result |= 0xff;
81 } else if (name_id == NameId::kPreferredFamily ||
82 name_id == NameId::kPreferredSubfamily) {
83 result |= 0xf;
84 } else if (name_id == NameId::kWWSFamilyName ||
85 name_id == NameId::kWWSSubfamilyName) {
86 result |= 1;
87 }
88 return result;
89 }
90
91 bool HasName(const char* font_name, Font* font) {
92 UnicodeString font_string = UnicodeString::fromUTF8(font_name);
93 if (font_string.isEmpty())
94 return false;
95 UnicodeString regular_suffix = UnicodeString::fromUTF8(" Regular");
96 UnicodeString alt_font_string = font_string;
97 alt_font_string += regular_suffix;
98
99 typedef std::map<int32_t, UnicodeString> NameMap;
100 NameMap names;
101 NameTablePtr name_table = down_cast<NameTable*>(font->GetTable(Tag::name));
102 if (name_table == NULL) {
103 return false;
104 }
105
106 for (int32_t i = 0; i < name_table->NameCount(); ++i) {
107 switch (name_table->NameId(i)) {
108 case NameId::kFontFamilyName:
109 case NameId::kFontSubfamilyName:
110 case NameId::kFullFontName:
111 case NameId::kPreferredFamily:
112 case NameId::kPreferredSubfamily:
113 case NameId::kWWSFamilyName:
114 case NameId::kWWSSubfamilyName: {
115 int32_t hash_code = HashCode(name_table->PlatformId(i),
116 name_table->EncodingId(i),
117 name_table->LanguageId(i),
118 name_table->NameId(i));
119 UChar* name_part = name_table->Name(i);
120 ConstructName(name_part, &(names[hash_code]), name_table->NameId(i));
121 delete[] name_part;
122 break;
123 }
124 default:
125 break;
126 }
127 }
128
129 if (!names.empty()) {
130 for (NameMap::iterator i = names.begin(), e = names.end(); i != e; ++i) {
131 if (i->second.caseCompare(font_string, 0) == 0 ||
132 i->second.caseCompare(alt_font_string, 0) == 0) {
133 return true;
134 }
135 }
136 }
137 return false;
138 }
139
140 Font* FindFont(const char* font_name, const FontArray& font_array) {
141 if (font_array.empty() || font_array[0] == NULL) {
142 return NULL;
143 }
144
145 if (font_name && strlen(font_name)) {
146 for (FontArray::const_iterator i = font_array.begin(), e = font_array.end();
147 i != e; ++i) {
148 if (HasName(font_name, i->p_)) {
149 return i->p_;
150 }
151 }
152 }
153
154 return font_array[0].p_;
155 }
156
157 bool ResolveCompositeGlyphs(GlyphTable* glyph_table,
158 LocaTable* loca_table,
159 const unsigned int* glyph_ids,
160 size_t glyph_count,
161 IntegerSet* glyph_id_processed) {
162 if (glyph_table == NULL || loca_table == NULL ||
163 glyph_ids == NULL || glyph_count == 0 || glyph_id_processed == NULL) {
164 return false;
165 }
166
167 // Sort and uniquify glyph ids.
168 IntegerSet glyph_id_remaining;
169 glyph_id_remaining.insert(0); // Always include glyph id 0.
170 for (size_t i = 0; i < glyph_count; ++i) {
171 glyph_id_remaining.insert(glyph_ids[i]);
172 }
173
174 // Identify if any given glyph id maps to a composite glyph. If so, include
175 // the glyphs referenced by that composite glyph.
176 while (!glyph_id_remaining.empty()) {
177 IntegerSet comp_glyph_id;
178 for (IntegerSet::iterator i = glyph_id_remaining.begin(),
179 e = glyph_id_remaining.end(); i != e; ++i) {
180 if (*i < 0 || *i >= loca_table->num_glyphs()) {
181 // Invalid glyph id, ignore.
182 continue;
183 }
184
185 int32_t length = loca_table->GlyphLength(*i);
186 if (length == 0) {
187 // Empty glyph, ignore.
188 continue;
189 }
190 int32_t offset = loca_table->GlyphOffset(*i);
191
192 GlyphPtr glyph;
193 glyph.Attach(glyph_table->GetGlyph(offset, length));
194 if (glyph == NULL) {
195 // Error finding glyph, ignore.
196 continue;
197 }
198
199 if (glyph->GlyphType() == GlyphType::kComposite) {
200 Ptr<GlyphTable::CompositeGlyph> comp_glyph =
201 down_cast<GlyphTable::CompositeGlyph*>(glyph.p_);
202 for (int32_t j = 0; j < comp_glyph->NumGlyphs(); ++j) {
203 int32_t glyph_id = comp_glyph->GlyphIndex(j);
204 if (glyph_id_processed->find(glyph_id) == glyph_id_processed->end() &&
205 glyph_id_remaining.find(glyph_id) == glyph_id_remaining.end()) {
206 comp_glyph_id.insert(comp_glyph->GlyphIndex(j));
207 }
208 }
209 }
210
211 glyph_id_processed->insert(*i);
212 }
213
214 glyph_id_remaining.clear();
215 glyph_id_remaining = comp_glyph_id;
216 }
217
218 return true;
219 }
220
221 bool SetupGlyfBuilders(Font::Builder* font_builder,
222 GlyphTable* glyph_table,
223 LocaTable* loca_table,
224 const IntegerSet& glyph_ids) {
225 if (!font_builder || !glyph_table || !loca_table) {
226 return false;
227 }
228
229 GlyphTableBuilderPtr glyph_table_builder =
230 down_cast<GlyphTable::Builder*>(font_builder->NewTableBuilder(Tag::glyf));
231 LocaTableBuilderPtr loca_table_builder =
232 down_cast<LocaTable::Builder*>(font_builder->NewTableBuilder(Tag::loca));
233 if (glyph_table_builder == NULL || loca_table_builder == NULL) {
234 // Out of memory.
235 return false;
236 }
237
238 // Extract glyphs and setup loca list.
239 IntegerList loca_list;
240 loca_list.resize(loca_table->num_glyphs());
241 loca_list.push_back(0);
242 int32_t last_glyph_id = 0;
243 int32_t last_offset = 0;
244 GlyphTable::GlyphBuilderList* glyph_builders =
245 glyph_table_builder->GlyphBuilders();
246 for (IntegerSet::const_iterator i = glyph_ids.begin(), e = glyph_ids.end();
247 i != e; ++i) {
248 int32_t length = loca_table->GlyphLength(*i);
249 int32_t offset = loca_table->GlyphOffset(*i);
250
251 GlyphPtr glyph;
252 glyph.Attach(glyph_table->GetGlyph(offset, length));
253
254 // Add glyph to new glyf table.
255 ReadableFontDataPtr data = glyph->ReadFontData();
256 WritableFontDataPtr copy_data;
257 copy_data.Attach(WritableFontData::CreateWritableFontData(data->Length()));
258 data->CopyTo(copy_data);
259 GlyphBuilderPtr glyph_builder;
260 glyph_builder.Attach(glyph_table_builder->GlyphBuilder(copy_data));
261 glyph_builders->push_back(glyph_builder);
262
263 // Configure loca list.
264 for (int32_t j = last_glyph_id + 1; j <= *i; ++j) {
265 loca_list[j] = last_offset;
266 }
267 last_offset += length;
268 loca_list[*i + 1] = last_offset;
269 last_glyph_id = *i;
270 }
271 for (int32_t j = last_glyph_id + 1; j <= loca_table->num_glyphs(); ++j) {
272 loca_list[j] = last_offset;
273 }
274 loca_table_builder->SetLocaList(&loca_list);
275
276 return true;
277 }
278
279 bool HasOverlap(int32_t range_begin, int32_t range_end,
280 const IntegerSet& glyph_ids) {
281 if (range_begin == range_end) {
282 return glyph_ids.find(range_begin) != glyph_ids.end();
283 } else if (range_end > range_begin) {
284 IntegerSet::const_iterator left = glyph_ids.lower_bound(range_begin);
285 IntegerSet::const_iterator right = glyph_ids.lower_bound(range_end);
286 return right != left;
287 }
288 return false;
289 }
290
291 // Initialize builder, returns false if glyph_id subset is not covered.
292 // Not thread-safe, caller to ensure object life-time.
293 bool InitializeBitmapBuilder(EbdtTable::Builder* ebdt, EblcTable::Builder* eblc,
294 const IntegerSet& glyph_ids) {
295 BitmapLocaList loca_list;
296 BitmapSizeTableBuilderList* strikes = eblc->BitmapSizeBuilders();
297
298 // Note: Do not call eblc_builder->GenerateLocaList(&loca_list) and then
299 // ebdt_builder->SetLoca(loca_list). For fonts like SimSun, there are
300 // >28K glyphs inside, where a typical usage will be <1K glyphs. Doing
301 // the calls improperly will result in creation of >100K objects that
302 // will be destroyed immediately, inducing significant slowness.
303 IntegerList removed_strikes;
304 for (size_t i = 0; i < strikes->size(); i++) {
305 if (!HasOverlap((*strikes)[i]->StartGlyphIndex(),
306 (*strikes)[i]->EndGlyphIndex(), glyph_ids)) {
307 removed_strikes.push_back(i);
308 continue;
309 }
310
311 IndexSubTableBuilderList* index_builders =
312 (*strikes)[i]->IndexSubTableBuilders();
313 IntegerList removed_indexes;
314 BitmapGlyphInfoMap info_map;
315 for (size_t j = 0; j < index_builders->size(); ++j) {
316 int32_t first_glyph_id = (*index_builders)[j]->first_glyph_index();
317 int32_t last_glyph_id = (*index_builders)[j]->last_glyph_index();
318 if (!HasOverlap(first_glyph_id, last_glyph_id, glyph_ids)) {
319 removed_indexes.push_back(j);
320 continue;
321 }
322 for (IntegerSet::const_iterator gid = glyph_ids.begin(),
323 gid_end = glyph_ids.end();
324 gid != gid_end; gid++) {
325 if (*gid < first_glyph_id) {
326 continue;
327 }
328 if (*gid > last_glyph_id) {
329 break;
330 }
331 BitmapGlyphInfoPtr info;
332 info.Attach((*index_builders)[j]->GlyphInfo(*gid));
333 if (info && info->length()) { // Do not include gid without bitmap
334 info_map[*gid] = info;
335 }
336 }
337 }
338 if (!info_map.empty()) {
339 loca_list.push_back(info_map);
340 } else {
341 removed_strikes.push_back(i); // Detected null entries.
342 }
343
344 // Remove unused index sub tables
345 for (IntegerList::reverse_iterator j = removed_indexes.rbegin(),
346 e = removed_indexes.rend();
347 j != e; j++) {
348 index_builders->erase(index_builders->begin() + *j);
349 }
350 }
351 if (removed_strikes.size() == strikes->size() || loca_list.empty()) {
352 return false;
353 }
354
355 for (IntegerList::reverse_iterator i = removed_strikes.rbegin(),
356 e = removed_strikes.rend(); i != e; i++) {
357 strikes->erase(strikes->begin() + *i);
358 }
359
360 if (strikes->empty()) { // no glyph covered, can safely drop the builders.
361 return false;
362 }
363
364 ebdt->SetLoca(&loca_list);
365 ebdt->GlyphBuilders(); // Initialize the builder.
366 return true;
367 }
368
369 void CopyBigGlyphMetrics(BigGlyphMetrics::Builder* source,
370 BigGlyphMetrics::Builder* target) {
371 target->SetHeight(static_cast<byte_t>(source->Height()));
372 target->SetWidth(static_cast<byte_t>(source->Width()));
373 target->SetHoriBearingX(static_cast<byte_t>(source->HoriBearingX()));
374 target->SetHoriBearingY(static_cast<byte_t>(source->HoriBearingY()));
375 target->SetHoriAdvance(static_cast<byte_t>(source->HoriAdvance()));
376 target->SetVertBearingX(static_cast<byte_t>(source->VertBearingX()));
377 target->SetVertBearingY(static_cast<byte_t>(source->VertBearingY()));
378 target->SetVertAdvance(static_cast<byte_t>(source->VertAdvance()));
379 }
380
381 CALLER_ATTACH IndexSubTable::Builder*
382 ConstructIndexFormat4(IndexSubTable::Builder* b, const BitmapGlyphInfoMap& loca,
383 int32_t* image_data_offset) {
384 IndexSubTableFormat4BuilderPtr builder4;
385 builder4.Attach(IndexSubTableFormat4::Builder::CreateBuilder());
386 CodeOffsetPairBuilderList offset_pairs;
387
388 size_t offset = 0;
389 int32_t lower_bound = b->first_glyph_index();
390 int32_t upper_bound = b->last_glyph_index();
391 int32_t last_gid = -1;
392 BitmapGlyphInfoMap::const_iterator i = loca.lower_bound(lower_bound);
393 BitmapGlyphInfoMap::const_iterator end = loca.end();
394 if (i != end) {
395 last_gid = i->first;
396 builder4->set_first_glyph_index(last_gid);
397 builder4->set_image_format(b->image_format());
398 builder4->set_image_data_offset(*image_data_offset);
399 }
400 for (; i != end; i++) {
401 int32_t gid = i->first;
402 if (gid > upper_bound) {
403 break;
404 }
405 offset_pairs.push_back(
406 IndexSubTableFormat4::CodeOffsetPairBuilder(gid, offset));
407 offset += i->second->length();
408 last_gid = gid;
409 }
410 offset_pairs.push_back(
411 IndexSubTableFormat4::CodeOffsetPairBuilder(-1, offset));
412 builder4->set_last_glyph_index(last_gid);
413 *image_data_offset += offset;
414 builder4->SetOffsetArray(offset_pairs);
415
416 return builder4.Detach();
417 }
418
419 CALLER_ATTACH IndexSubTable::Builder*
420 ConstructIndexFormat5(IndexSubTable::Builder* b, const BitmapGlyphInfoMap& loca,
421 int32_t* image_data_offset) {
422 IndexSubTableFormat5BuilderPtr new_builder;
423 new_builder.Attach(IndexSubTableFormat5::Builder::CreateBuilder());
424
425 // Copy BigMetrics
426 int32_t image_size = 0;
427 if (b->index_format() == IndexSubTable::Format::FORMAT_2) {
428 IndexSubTableFormat2BuilderPtr builder2 =
429 down_cast<IndexSubTableFormat2::Builder*>(b);
430 CopyBigGlyphMetrics(builder2->BigMetrics(), new_builder->BigMetrics());
431 image_size = builder2->ImageSize();
432 } else {
433 IndexSubTableFormat5BuilderPtr builder5 =
434 down_cast<IndexSubTableFormat5::Builder*>(b);
435 BigGlyphMetricsBuilderPtr metrics_builder;
436 CopyBigGlyphMetrics(builder5->BigMetrics(), new_builder->BigMetrics());
437 image_size = builder5->ImageSize();
438 }
439
440 IntegerList* glyph_array = new_builder->GlyphArray();
441 size_t offset = 0;
442 int32_t lower_bound = b->first_glyph_index();
443 int32_t upper_bound = b->last_glyph_index();
444 int32_t last_gid = -1;
445 BitmapGlyphInfoMap::const_iterator i = loca.lower_bound(lower_bound);
446 BitmapGlyphInfoMap::const_iterator end = loca.end();
447 if (i != end) {
448 last_gid = i->first;
449 new_builder->set_first_glyph_index(last_gid);
450 new_builder->set_image_format(b->image_format());
451 new_builder->set_image_data_offset(*image_data_offset);
452 new_builder->SetImageSize(image_size);
453 }
454 for (; i != end; i++) {
455 int32_t gid = i->first;
456 if (gid > upper_bound) {
457 break;
458 }
459 glyph_array->push_back(gid);
460 offset += i->second->length();
461 last_gid = gid;
462 }
463 new_builder->set_last_glyph_index(last_gid);
464 *image_data_offset += offset;
465 return new_builder.Detach();
466 }
467
468 CALLER_ATTACH IndexSubTable::Builder*
469 SubsetIndexSubTable(IndexSubTable::Builder* builder,
470 const BitmapGlyphInfoMap& loca,
471 int32_t* image_data_offset) {
472 switch (builder->index_format()) {
473 case IndexSubTable::Format::FORMAT_1:
474 case IndexSubTable::Format::FORMAT_3:
475 case IndexSubTable::Format::FORMAT_4:
476 return ConstructIndexFormat4(builder, loca, image_data_offset);
477 case IndexSubTable::Format::FORMAT_2:
478 case IndexSubTable::Format::FORMAT_5:
479 return ConstructIndexFormat5(builder, loca, image_data_offset);
480 default:
481 assert(false);
482 break;
483 }
484 return NULL;
485 }
486
487 }
488
489 namespace sfntly {
490
491 // Not thread-safe, caller to ensure object life-time.
492 void SubsetEBLC(EblcTable::Builder* eblc, const BitmapLocaList& new_loca) {
493 BitmapSizeTableBuilderList* size_builders = eblc->BitmapSizeBuilders();
494 if (size_builders == NULL) {
495 return;
496 }
497
498 int32_t image_data_offset = EbdtTable::Offset::kHeaderLength;
499 for (size_t strike = 0; strike < size_builders->size(); ++strike) {
500 IndexSubTableBuilderList* index_builders =
501 (*size_builders)[strike]->IndexSubTableBuilders();
502 for (size_t index = 0; index < index_builders->size(); ++index) {
503 IndexSubTable::Builder* new_builder_raw =
504 SubsetIndexSubTable((*index_builders)[index], new_loca[strike],
505 &image_data_offset);
506 if (NULL != new_builder_raw) {
507 (*index_builders)[index].Attach(new_builder_raw);
508 }
509 }
510 }
511 }
512
513 // EBLC structure (from stuartg)
514 // header
515 // bitmapSizeTable[]
516 // one per strike
517 // holds strike metrics - sbitLineMetrics
518 // holds info about indexSubTableArray
519 // indexSubTableArray[][]
520 // one per strike and then one per indexSubTable for that strike
521 // holds info about the indexSubTable
522 // the indexSubTable entries pointed to can be of different formats
523 // indexSubTable
524 // one per indexSubTableArray entry
525 // tells how to get the glyphs
526 // may hold the glyph metrics if they are uniform for all the glyphs in range
527 //
528 // There is nothing that says that the indexSubTableArray entries and/or the
529 // indexSubTable items need to be unique. They may be shared between strikes.
530 //
531 // EBDT structure:
532 // header
533 // glyphs
534 // amorphous blob of data
535 // different glyphs that are only able to be figured out from the EBLC table
536 // may hold metrics - depends on the EBLC entry that pointed to them
537
538 // Subsetting EBLC table (from arthurhsu)
539 // Most pages use only a fraction (hundreds or less) glyphs out of a given font
540 // (which can have >20K glyphs for CJK). It's safe to assume that the subset
541 // font will have sparse bitmap glyphs. So we reconstruct the EBLC table as
542 // format 4 or 5 here.
543
544 enum BuildersToRemove {
545 kRemoveNone,
546 kRemoveBDAT,
547 kRemoveBDATAndEBDT
548 };
549
550 int SetupBitmapBuilders(Font* font, Font::Builder* font_builder,
551 const IntegerSet& glyph_ids) {
552 if (!font || !font_builder) {
553 return false;
554 }
555
556 // Check if bitmap table exists.
557 EbdtTablePtr ebdt_table = down_cast<EbdtTable*>(font->GetTable(Tag::EBDT));
558 EblcTablePtr eblc_table = down_cast<EblcTable*>(font->GetTable(Tag::EBLC));
559 bool use_ebdt = (ebdt_table != NULL && eblc_table != NULL);
560 if (!use_ebdt) {
561 ebdt_table = down_cast<EbdtTable*>(font->GetTable(Tag::bdat));
562 eblc_table = down_cast<EblcTable*>(font->GetTable(Tag::bloc));
563 if (ebdt_table == NULL || eblc_table == NULL) {
564 return kRemoveNone;
565 }
566 }
567
568 // If the bitmap table's size is too small, skip subsetting.
569 if (ebdt_table->DataLength() + eblc_table->DataLength() <
570 BITMAP_SIZE_THRESHOLD) {
571 return use_ebdt ? kRemoveBDAT : kRemoveNone;
572 }
573
574 // Get the builders.
575 EbdtTableBuilderPtr ebdt_table_builder = down_cast<EbdtTable::Builder*>(
576 font_builder->NewTableBuilder(use_ebdt ? Tag::EBDT : Tag::bdat,
577 ebdt_table->ReadFontData()));
578 EblcTableBuilderPtr eblc_table_builder = down_cast<EblcTable::Builder*>(
579 font_builder->NewTableBuilder(use_ebdt ? Tag::EBLC : Tag::bloc,
580 eblc_table->ReadFontData()));
581 if (ebdt_table_builder == NULL || eblc_table_builder == NULL) {
582 // Out of memory.
583 return use_ebdt ? kRemoveBDAT : kRemoveNone;
584 }
585
586 if (!InitializeBitmapBuilder(ebdt_table_builder, eblc_table_builder,
587 glyph_ids)) {
588 // Bitmap tables do not cover the glyphs in our subset.
589 font_builder->RemoveTableBuilder(use_ebdt ? Tag::EBLC : Tag::bloc);
590 font_builder->RemoveTableBuilder(use_ebdt ? Tag::EBDT : Tag::bdat);
591 return kRemoveBDATAndEBDT;
592 }
593
594 BitmapLocaList new_loca;
595 ebdt_table_builder->GenerateLocaList(&new_loca);
596 SubsetEBLC(eblc_table_builder, new_loca);
597
598 return use_ebdt ? kRemoveBDAT : kRemoveNone;
599 }
600
601 SubsetterImpl::SubsetterImpl() {
602 }
603
604 SubsetterImpl::~SubsetterImpl() {
605 }
606
607 bool SubsetterImpl::LoadFont(const char* font_name,
608 const unsigned char* original_font,
609 size_t font_size) {
610 MemoryInputStream mis;
611 mis.Attach(original_font, font_size);
612 if (factory_ == NULL) {
613 factory_.Attach(FontFactory::GetInstance());
614 }
615
616 FontArray font_array;
617 factory_->LoadFonts(&mis, &font_array);
618 font_ = FindFont(font_name, font_array);
619 if (font_ == NULL) {
620 return false;
621 }
622
623 return true;
624 }
625
626 int SubsetterImpl::SubsetFont(const unsigned int* glyph_ids,
627 size_t glyph_count,
628 unsigned char** output_buffer) {
629 if (factory_ == NULL || font_ == NULL) {
630 return -1;
631 }
632
633 // Find glyf and loca table.
634 GlyphTablePtr glyph_table =
635 down_cast<GlyphTable*>(font_->GetTable(Tag::glyf));
636 LocaTablePtr loca_table = down_cast<LocaTable*>(font_->GetTable(Tag::loca));
637 if (glyph_table == NULL || loca_table == NULL) {
638 // We are not able to subset the font.
639 return 0;
640 }
641
642 IntegerSet glyph_id_processed;
643 if (!ResolveCompositeGlyphs(glyph_table, loca_table,
644 glyph_ids, glyph_count, &glyph_id_processed) ||
645 glyph_id_processed.empty()) {
646 return 0;
647 }
648
649 FontPtr new_font;
650 new_font.Attach(Subset(glyph_id_processed, glyph_table, loca_table));
651 if (new_font == NULL) {
652 return 0;
653 }
654
655 MemoryOutputStream output_stream;
656 factory_->SerializeFont(new_font, &output_stream);
657 int length = static_cast<int>(output_stream.Size());
658 if (length > 0) {
659 *output_buffer = new unsigned char[length];
660 memcpy(*output_buffer, output_stream.Get(), length);
661 }
662
663 return length;
664 }
665
666 // Long comments regarding TTF tables and PDF (from stuartg)
667 //
668 // According to PDF spec 1.4 (section 5.8), the following tables must be
669 // present:
670 // head, hhea, loca, maxp, cvt, prep, glyf, hmtx, fpgm
671 // cmap if font is used with a simple font dict and not a CIDFont dict
672 //
673 // Other tables we need to keep for PDF rendering to support zoom in/out:
674 // bdat, bloc, ebdt, eblc, ebsc, gasp
675 //
676 // Special table:
677 // CFF - if you have this table then you shouldn't have a glyf table and this
678 // is the table with all the glyphs. Shall skip subsetting completely
679 // since sfntly is not capable of subsetting it for now.
680 // post - extra info here for printing on PostScript printers but maybe not
681 // enough to outweigh the space taken by the names
682 //
683 // Tables to break apart:
684 // name - could throw away all but one language and one platform strings/ might
685 // throw away some of the name entries
686 // cmap - could strip out non-needed cmap subtables
687 // - format 4 subtable can be subsetted as well using sfntly
688 //
689 // Graphite tables:
690 // silf, glat, gloc, feat - should be okay to strip out
691 //
692 // Tables that can be discarded:
693 // OS/2 - everything here is for layout and description of the font that is
694 // elsewhere (some in the PDF objects)
695 // BASE, GDEF, GSUB, GPOS, JSTF - all used for layout
696 // kern - old style layout
697 // DSIG - this will be invalid after subsetting
698 // hdmx - layout
699 // PCLT - metadata that's not needed
700 // vmtx - layout
701 // vhea - layout
702 // VDMX
703 // VORG - not used by TT/OT - used by CFF
704 // hsty - would be surprised to see one of these - used on the Newton
705 // AAT tables - mort, morx, feat, acnt, bsin, just, lcar, fdsc, fmtx, prop,
706 // Zapf, opbd, trak, fvar, gvar, avar, cvar
707 // - these are all layout tables and once layout happens are not
708 // needed anymore
709 // LTSH - layout
710
711 CALLER_ATTACH
712 Font* SubsetterImpl::Subset(const IntegerSet& glyph_ids, GlyphTable* glyf,
713 LocaTable* loca) {
714 // The const is initialized here to workaround VC bug of rendering all Tag::*
715 // as 0. These tags represents the TTF tables that we will embed in subset
716 // font.
717 const int32_t TABLES_IN_SUBSET[] = {
718 Tag::head, Tag::hhea, Tag::loca, Tag::maxp, Tag::cvt,
719 Tag::prep, Tag::glyf, Tag::hmtx, Tag::fpgm, Tag::EBDT,
720 Tag::EBLC, Tag::EBSC, Tag::bdat, Tag::bloc, Tag::bhed,
721 Tag::cmap, // Keep here for future tagged PDF development.
722 Tag::name, // Keep here due to legal concerns: copyright info inside.
723 };
724
725 // Setup font builders we need.
726 FontBuilderPtr font_builder;
727 font_builder.Attach(factory_->NewFontBuilder());
728 IntegerSet remove_tags;
729
730 if (SetupGlyfBuilders(font_builder, glyf, loca, glyph_ids)) {
731 remove_tags.insert(Tag::glyf);
732 remove_tags.insert(Tag::loca);
733 }
734
735 switch (SetupBitmapBuilders(font_, font_builder, glyph_ids)) {
736 case kRemoveBDATAndEBDT:
737 remove_tags.insert(Tag::EBDT);
738 remove_tags.insert(Tag::EBLC);
739 remove_tags.insert(Tag::EBSC);
740 case kRemoveBDAT:
741 remove_tags.insert(Tag::bdat);
742 remove_tags.insert(Tag::bloc);
743 remove_tags.insert(Tag::bhed);
744 break;
745 default: // kRemoveNone
746 break;
747 }
748
749 IntegerSet allowed_tags;
750 for (size_t i = 0; i < sizeof(TABLES_IN_SUBSET) / sizeof(int32_t); ++i) {
751 allowed_tags.insert(TABLES_IN_SUBSET[i]);
752 }
753
754 IntegerSet result;
755 std::set_difference(allowed_tags.begin(), allowed_tags.end(),
756 remove_tags.begin(), remove_tags.end(),
757 std::inserter(result, result.end()));
758 allowed_tags = result;
759
760 // Setup remaining builders.
761 for (IntegerSet::iterator i = allowed_tags.begin(), e = allowed_tags.end();
762 i != e; ++i) {
763 Table* table = font_->GetTable(*i);
764 if (table) {
765 font_builder->NewTableBuilder(*i, table->ReadFontData());
766 }
767 }
768
769 return font_builder->Build();
770 }
771
772 } // namespace sfntly
OLDNEW
« DEPS ('K') | « third_party/sfntly/src/subsetter/subsetter_impl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698