Home | History | Annotate | Download | only in subtly
      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 #ifndef TYPOGRAPHY_FONT_SFNTLY_SRC_SAMPLE_SUBTLY_FONT_INFO_H_
     18 #define TYPOGRAPHY_FONT_SFNTLY_SRC_SAMPLE_SUBTLY_FONT_INFO_H_
     19 
     20 #include <map>
     21 #include <set>
     22 
     23 #include "sfntly/font.h"
     24 #include "sfntly/port/type.h"
     25 #include "sfntly/port/refcount.h"
     26 #include "sfntly/table/core/cmap_table.h"
     27 #include "sfntly/table/truetype/glyph_table.h"
     28 #include "sfntly/table/truetype/loca_table.h"
     29 
     30 namespace subtly {
     31 class CharacterPredicate;
     32 
     33 typedef int32_t FontId;
     34 typedef std::map<FontId, sfntly::Ptr<sfntly::Font> > FontIdMap;
     35 
     36 // Glyph id pair that contains the loca table glyph id as well as the
     37 // font id that has the glyph table this glyph belongs to.
     38 class GlyphId {
     39  public:
     40   GlyphId(int32_t glyph_id, FontId font_id);
     41   ~GlyphId() {}
     42 
     43   bool operator==(const GlyphId& other) const;
     44   bool operator<(const GlyphId& other) const;
     45 
     46   int32_t glyph_id() const { return glyph_id_; }
     47   void set_glyph_id(const int32_t glyph_id) { glyph_id_ = glyph_id; }
     48   FontId font_id() const { return font_id_; }
     49   void set_font_id(const FontId font_id) { font_id_ = font_id; }
     50 
     51  private:
     52   int32_t glyph_id_;
     53   FontId font_id_;
     54 };
     55 
     56 typedef std::map<int32_t, GlyphId> CharacterMap;
     57 typedef std::set<GlyphId> GlyphIdSet;
     58 
     59 // Font information used for FontAssembler in the construction of a new font.
     60 // Will make copies of character map, glyph id set and font id map.
     61 class FontInfo : public sfntly::RefCounted<FontInfo> {
     62  public:
     63   // Empty FontInfo object.
     64   FontInfo();
     65   // chars_to_glyph_ids maps characters to GlyphIds for CMap construction
     66   // resolved_glyph_ids defines GlyphIds which should be in the final font
     67   // fonts is a map of font ids to fonts to reference any needed table
     68   FontInfo(CharacterMap* chars_to_glyph_ids,
     69            GlyphIdSet* resolved_glyph_ids,
     70            FontIdMap* fonts);
     71   virtual ~FontInfo();
     72 
     73   // Gets the table with the specified tag from the font corresponding to
     74   // font_id or NULL if there is no such font/table.
     75   // font_id is the id of the font that contains the table
     76   // tag identifies the table to be obtained
     77   virtual sfntly::FontDataTable* GetTable(FontId font_id, int32_t tag);
     78   // Gets the table map of the font whose id is font_id
     79   virtual const sfntly::TableMap* GetTableMap(FontId);
     80 
     81   CharacterMap* chars_to_glyph_ids() const { return chars_to_glyph_ids_; }
     82   // Takes ownership of the chars_to_glyph_ids CharacterMap.
     83   void set_chars_to_glyph_ids(CharacterMap* chars_to_glyph_ids);
     84   GlyphIdSet* resolved_glyph_ids() const { return resolved_glyph_ids_; }
     85   // Takes ownership of the glyph_ids GlyphIdSet.
     86   void set_resolved_glyph_ids(GlyphIdSet* glyph_ids);
     87   FontIdMap* fonts() const { return fonts_; }
     88   // Takes ownership of the fonts FontIdMap.
     89   void set_fonts(FontIdMap* fonts);
     90 
     91  private:
     92   CharacterMap* chars_to_glyph_ids_;
     93   GlyphIdSet* resolved_glyph_ids_;
     94   FontIdMap* fonts_;
     95 };
     96 
     97 // FontSourcedInfoBuilder is used to create a FontInfo object from a Font
     98 // optionally specifying a CharacterPredicate to filter out some of
     99 // the font's characters.
    100 // It does not take ownership or copy the values its constructor receives.
    101 class FontSourcedInfoBuilder :
    102       public sfntly::RefCounted<FontSourcedInfoBuilder> {
    103  public:
    104   FontSourcedInfoBuilder(sfntly::Font* font, FontId font_id);
    105   FontSourcedInfoBuilder(sfntly::Font* font,
    106                          FontId font_id,
    107                          CharacterPredicate* predicate);
    108   virtual ~FontSourcedInfoBuilder() { }
    109 
    110   virtual CALLER_ATTACH FontInfo* GetFontInfo();
    111 
    112  protected:
    113   bool GetCharacterMap(CharacterMap* chars_to_glyph_ids);
    114   bool ResolveCompositeGlyphs(CharacterMap* chars_to_glyph_ids,
    115                               GlyphIdSet* resolved_glyph_ids);
    116   void Initialize();
    117 
    118  private:
    119   sfntly::Ptr<sfntly::Font> font_;
    120   FontId font_id_;
    121   CharacterPredicate* predicate_;
    122 
    123   sfntly::Ptr<sfntly::CMapTable::CMap> cmap_;
    124   sfntly::Ptr<sfntly::LocaTable> loca_table_;
    125   sfntly::Ptr<sfntly::GlyphTable> glyph_table_;
    126 };
    127 }
    128 #endif  // TYPOGRAPHY_FONT_SFNTLY_SRC_SAMPLE_SUBTLY_FONT_INFO_H_
    129