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 SFNTLY_CPP_SRC_SFNTLY_TABLE_BITMAP_INDEX_SUBTABLE_FORMAT4_H_
18 #define SFNTLY_CPP_SRC_SFNTLY_TABLE_BITMAP_INDEX_SUBTABLE_FORMAT4_H_
19 
20 #include "sfntly/table/bitmap/index_sub_table.h"
21 
22 namespace sfntly {
23 
24 class IndexSubTableFormat4 : public IndexSubTable,
25                              public RefCounted<IndexSubTableFormat4> {
26  public:
27   class CodeOffsetPair {
28    public:
glyph_code()29     int32_t glyph_code() const { return glyph_code_; }
offset()30     int32_t offset() const { return offset_; }
31 
32    protected:
33     CodeOffsetPair(int32_t glyph_code, int32_t offset);
34 
35     // TODO(arthurhsu): C++ style guide prohibits protected members.
36     int32_t glyph_code_;
37     int32_t offset_;
38   };
39 
40   class CodeOffsetPairBuilder : public CodeOffsetPair {
41    public:
42     CodeOffsetPairBuilder();
43     CodeOffsetPairBuilder(int32_t glyph_code, int32_t offset);
set_glyph_code(int32_t v)44     void set_glyph_code(int32_t v) { glyph_code_ = v; }
set_offset(int32_t v)45     void set_offset(int32_t v) { offset_ = v; }
46   };
47 
48   class CodeOffsetPairGlyphCodeComparator {
49    public:
50     bool operator()(const CodeOffsetPair& lhs, const CodeOffsetPair& rhs);
51   };
52 
53   class Builder : public IndexSubTable::Builder,
54                   public RefCounted<Builder> {
55    public:
56     class BitmapGlyphInfoIterator
57         : public RefIterator<BitmapGlyphInfo, Builder, IndexSubTable::Builder> {
58      public:
59       explicit BitmapGlyphInfoIterator(Builder* container);
~BitmapGlyphInfoIterator()60       virtual ~BitmapGlyphInfoIterator() {}
61 
62       virtual bool HasNext();
63       CALLER_ATTACH virtual BitmapGlyphInfo* Next();
64 
65      private:
66       int32_t code_offset_pair_index_;
67     };
68 
69     virtual ~Builder();
70     virtual int32_t NumGlyphs();
71     virtual int32_t GlyphLength(int32_t glyph_id);
72     virtual int32_t GlyphStartOffset(int32_t glyph_id);
73     CALLER_ATTACH virtual BitmapGlyphInfoIterator* GetIterator();
74 
75     virtual CALLER_ATTACH FontDataTable* SubBuildTable(ReadableFontData* data);
76     virtual void SubDataSet();
77     virtual int32_t SubDataSizeToSerialize();
78     virtual bool SubReadyToSerialize();
79     virtual int32_t SubSerialize(WritableFontData* new_data);
80 
81     void Revert();
82     void SetOffsetArray(const std::vector<CodeOffsetPairBuilder>& pair_array);
83 
84     static CALLER_ATTACH Builder* CreateBuilder();
85     static CALLER_ATTACH Builder* CreateBuilder(ReadableFontData* data,
86                                                 int32_t index_sub_table_offset,
87                                                 int32_t first_glyph_index,
88                                                 int32_t last_glyph_index);
89     static CALLER_ATTACH Builder* CreateBuilder(WritableFontData* data,
90                                                 int32_t index_sub_table_offset,
91                                                 int32_t first_glyph_index,
92                                                 int32_t last_glyph_index);
93    private:
94     Builder();
95     Builder(WritableFontData* data,
96             int32_t first_glyph_index,
97             int32_t last_glyph_index);
98     Builder(ReadableFontData* data,
99             int32_t first_glyph_index,
100             int32_t last_glyph_index);
101     std::vector<CodeOffsetPairBuilder>* GetOffsetArray();
102     void Initialize(ReadableFontData* data);
103     int32_t FindCodeOffsetPair(int32_t glyph_id);
104 
105     static int32_t DataLength(ReadableFontData* data,
106                               int32_t index_sub_table_offset,
107                               int32_t first_glyph_index,
108                               int32_t last_glyph_index);
109 
110     std::vector<CodeOffsetPairBuilder> offset_pair_array_;
111   };
112 
113   virtual ~IndexSubTableFormat4();
114 
115   virtual int32_t NumGlyphs();
116   virtual int32_t GlyphStartOffset(int32_t glyph_id);
117   virtual int32_t GlyphLength(int32_t glyph_id);
118 
119  private:
120   IndexSubTableFormat4(ReadableFontData* data,
121                        int32_t first_glyph_index,
122                        int32_t last_glyph_index);
123 
124   int32_t FindCodeOffsetPair(int32_t glyph_id);
125   static int32_t NumGlyphs(ReadableFontData* data, int32_t table_offset);
126 
127   friend class Builder;
128 };
129 typedef Ptr<IndexSubTableFormat4> IndexSubTableFormat4Ptr;
130 typedef Ptr<IndexSubTableFormat4::Builder> IndexSubTableFormat4BuilderPtr;
131 typedef std::vector<IndexSubTableFormat4::CodeOffsetPairBuilder>
132             CodeOffsetPairBuilderList;
133 }  // namespace sfntly
134 
135 #endif  // SFNTLY_CPP_SRC_SFNTLY_TABLE_BITMAP_INDEX_SUBTABLE_FORMAT4_H_
136