Line data Source code
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 "sfntly/tools/subsetter/glyph_table_subsetter.h"
18 :
19 : #include "sfntly/table/truetype/glyph_table.h"
20 : #include "sfntly/table/truetype/loca_table.h"
21 : #include "sfntly/tag.h"
22 : #include "sfntly/tools/subsetter/subsetter.h"
23 : #include "sfntly/port/exception_type.h"
24 :
25 : namespace sfntly {
26 :
27 : const int32_t kGlyphTableSubsetterTags[2] = {Tag::glyf, Tag::loca};
28 :
29 0 : GlyphTableSubsetter::GlyphTableSubsetter()
30 0 : : TableSubsetterImpl(kGlyphTableSubsetterTags, 2) {
31 0 : }
32 :
33 0 : GlyphTableSubsetter::~GlyphTableSubsetter() {}
34 :
35 0 : bool GlyphTableSubsetter::Subset(Subsetter* subsetter,
36 : Font* font,
37 : Font::Builder* font_builder) {
38 0 : assert(font);
39 0 : assert(subsetter);
40 0 : assert(font_builder);
41 :
42 0 : IntegerList* permutation_table = subsetter->GlyphPermutationTable();
43 0 : if (!permutation_table || permutation_table->empty())
44 0 : return false;
45 :
46 0 : GlyphTablePtr glyph_table = down_cast<GlyphTable*>(font->GetTable(Tag::glyf));
47 0 : LocaTablePtr loca_table = down_cast<LocaTable*>(font->GetTable(Tag::loca));
48 0 : if (glyph_table == NULL || loca_table == NULL) {
49 : #if !defined (SFNTLY_NO_EXCEPTION)
50 : throw RuntimeException("Font to subset is not valid.");
51 : #endif
52 0 : return false;
53 : }
54 :
55 : GlyphTableBuilderPtr glyph_table_builder =
56 : down_cast<GlyphTable::Builder*>
57 0 : (font_builder->NewTableBuilder(Tag::glyf));
58 : LocaTableBuilderPtr loca_table_builder =
59 : down_cast<LocaTable::Builder*>
60 0 : (font_builder->NewTableBuilder(Tag::loca));
61 0 : if (glyph_table_builder == NULL || loca_table_builder == NULL) {
62 : #if !defined (SFNTLY_NO_EXCEPTION)
63 : throw RuntimeException("Builder for subset is not valid.");
64 : #endif
65 0 : return false;
66 : }
67 : GlyphTable::GlyphBuilderList* glyph_builders =
68 0 : glyph_table_builder->GlyphBuilders();
69 0 : for (IntegerList::iterator old_glyph_id = permutation_table->begin(),
70 0 : old_glyph_id_end = permutation_table->end();
71 : old_glyph_id != old_glyph_id_end; ++old_glyph_id) {
72 0 : int old_offset = loca_table->GlyphOffset(*old_glyph_id);
73 0 : int old_length = loca_table->GlyphLength(*old_glyph_id);
74 0 : GlyphPtr glyph;
75 0 : glyph.Attach(glyph_table->GetGlyph(old_offset, old_length));
76 0 : ReadableFontDataPtr data = glyph->ReadFontData();
77 0 : WritableFontDataPtr copy_data;
78 0 : copy_data.Attach(WritableFontData::CreateWritableFontData(data->Length()));
79 0 : data->CopyTo(copy_data);
80 0 : GlyphBuilderPtr glyph_builder;
81 0 : glyph_builder.Attach(glyph_table_builder->GlyphBuilder(copy_data));
82 0 : glyph_builders->push_back(glyph_builder);
83 : }
84 0 : IntegerList loca_list;
85 0 : glyph_table_builder->GenerateLocaList(&loca_list);
86 0 : loca_table_builder->SetLocaList(&loca_list);
87 0 : return true;
88 : }
89 :
90 : } // namespace sfntly
|