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