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