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/subsetter.h"
18 :
19 : #include <algorithm>
20 : #include <iterator>
21 :
22 : #include "sfntly/tools/subsetter/glyph_table_subsetter.h"
23 :
24 : namespace sfntly {
25 :
26 0 : Subsetter::Subsetter(Font* font, FontFactory* font_factory) {
27 0 : font_ = font;
28 0 : font_factory_ = font_factory;
29 0 : TableSubsetterPtr subsetter = new GlyphTableSubsetter();
30 : // TODO(arthurhsu): IMPLEMENT: CMap table subsetter
31 0 : table_subsetters_.push_back(subsetter);
32 0 : }
33 :
34 0 : Subsetter::~Subsetter() {
35 0 : font_factory_.Release();
36 0 : font_.Release();
37 0 : table_subsetters_.clear();
38 0 : }
39 :
40 0 : void Subsetter::SetGlyphs(IntegerList* glyphs) {
41 0 : new_to_old_glyphs_ = *glyphs;
42 0 : }
43 :
44 0 : void Subsetter::SetCMaps(CMapIdList* cmap_ids, int32_t number) {
45 : UNREFERENCED_PARAMETER(cmap_ids);
46 : UNREFERENCED_PARAMETER(number);
47 : // TODO(arthurhsu): IMPLEMENT
48 0 : }
49 :
50 0 : void Subsetter::SetRemoveTables(IntegerSet* remove_tables) {
51 0 : remove_tables_ = *remove_tables;
52 0 : }
53 :
54 0 : CALLER_ATTACH Font::Builder* Subsetter::Subset() {
55 0 : FontBuilderPtr font_builder;
56 0 : font_builder.Attach(font_factory_->NewFontBuilder());
57 :
58 0 : IntegerSet table_tags;
59 0 : for (TableMap::const_iterator i = font_->GetTableMap()->begin(),
60 0 : e = font_->GetTableMap()->end(); i != e; ++i) {
61 0 : table_tags.insert(i->first);
62 : }
63 0 : if (!remove_tables_.empty()) {
64 0 : IntegerSet result;
65 : std::set_difference(table_tags.begin(), table_tags.end(),
66 : remove_tables_.begin(), remove_tables_.end(),
67 0 : std::inserter(result, result.end()));
68 0 : table_tags = result;
69 : }
70 0 : for (TableSubsetterList::iterator
71 0 : table_subsetter = table_subsetters_.begin(),
72 0 : table_subsetter_end = table_subsetters_.end();
73 : table_subsetter != table_subsetter_end; ++table_subsetter) {
74 0 : bool handled = (*table_subsetter)->Subset(this, font_, font_builder);
75 0 : if (handled) {
76 0 : IntegerSet* handled_tags = (*table_subsetter)->TagsHandled();
77 0 : IntegerSet result;
78 : std::set_difference(table_tags.begin(), table_tags.end(),
79 : handled_tags->begin(), handled_tags->end(),
80 0 : std::inserter(result, result.end()));
81 0 : table_tags = result;
82 : }
83 : }
84 0 : for (IntegerSet::iterator tag = table_tags.begin(),
85 0 : tag_end = table_tags.end(); tag != tag_end; ++tag) {
86 0 : Table* table = font_->GetTable(*tag);
87 0 : if (table) {
88 0 : font_builder->NewTableBuilder(*tag, table->ReadFontData());
89 : }
90 : }
91 0 : return font_builder.Detach();
92 : }
93 :
94 0 : IntegerList* Subsetter::GlyphPermutationTable() {
95 0 : return &new_to_old_glyphs_;
96 : }
97 :
98 0 : CMapIdList* Subsetter::CMapId() {
99 0 : return &cmap_ids_;
100 : }
101 :
102 : } // namespace sfntly
|