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