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_BITMAP_SIZE_TABLE_H_
18 #define SFNTLY_CPP_SRC_SFNTLY_TABLE_BITMAP_BITMAP_SIZE_TABLE_H_
19 
20 #include "sfntly/port/lock.h"
21 #include "sfntly/table/bitmap/bitmap_glyph_info.h"
22 #include "sfntly/table/bitmap/index_sub_table.h"
23 
24 namespace sfntly {
25 // Binary search would be faster but many fonts have index subtables that
26 // aren't sorted.
27 // Note: preprocessor define is used to avoid const expression warnings in C++
28 //       code.
29 #define SFNTLY_BITMAPSIZE_USE_BINARY_SEARCH 0
30 
31 class BitmapSizeTable : public SubTable,
32                         public RefCounted<BitmapSizeTable> {
33  public:
34   class Builder : public SubTable::Builder,
35                   public RefCounted<Builder> {
36    public:
37     class BitmapGlyphInfoIterator :
38         public RefIterator<BitmapGlyphInfo, Builder> {
39      public:
40       explicit BitmapGlyphInfoIterator(Builder* container);
~BitmapGlyphInfoIterator()41       virtual ~BitmapGlyphInfoIterator() {}
42 
43       virtual bool HasNext();
44       CALLER_ATTACH virtual BitmapGlyphInfo* Next();
45 
46      private:
47       bool HasNext(BitmapGlyphInfoIter* iterator_base);
48       CALLER_ATTACH BitmapGlyphInfo* Next(BitmapGlyphInfoIter* iterator_base);
49 
50       IndexSubTableBuilderList::iterator sub_table_iter_;
51       BitmapGlyphInfoIterPtr sub_table_glyph_info_iter_;
52     };
53 
54     virtual ~Builder();
55 
56     virtual CALLER_ATTACH FontDataTable* SubBuildTable(ReadableFontData* data);
57     virtual void SubDataSet();
58     virtual int32_t SubDataSizeToSerialize();
59     virtual bool SubReadyToSerialize();
60     virtual int32_t SubSerialize(WritableFontData* new_data);
61 
62     static CALLER_ATTACH Builder* CreateBuilder(WritableFontData* data,
63                                                 ReadableFontData* master_data);
64     static CALLER_ATTACH Builder* CreateBuilder(ReadableFontData* data,
65                                                 ReadableFontData* master_data);
66     // Gets the subtable array offset as set in the original table as read from
67     // the font file. This value cannot be explicitly set and will be generated
68     // during table building.
69     // @return the subtable array offset
70     int32_t IndexSubTableArrayOffset();
71 
72     // Sets the subtable array offset. This is used only during the building
73     // process when the objects are being serialized.
74     // @param offset the offset to the index subtable array
75     void SetIndexSubTableArrayOffset(int32_t offset);
76 
77     // Gets the subtable array size as set in the original table as read from
78     // the font file. This value cannot be explicitly set and will be generated
79     // during table building.
80     // @return the subtable array size
81     int32_t IndexTableSize();
82 
83     // Sets the subtable size. This is used only during the building process
84     // when the objects are being serialized.
85     // @param size the offset to the index subtable array
86     void SetIndexTableSize(int32_t size);
87 
88     int32_t NumberOfIndexSubTables();
89     int32_t ColorRef();
90     // TODO(stuartg): SBitLineMetrics hori();
91     // TODO(stuartg): SBitLineMetrics vert();
92     int32_t StartGlyphIndex();
93     int32_t EndGlyphIndex();
94     int32_t PpemX();
95     int32_t PpemY();
96     int32_t BitDepth();
97     int32_t FlagsAsInt();
98 
99     IndexSubTable::Builder* IndexSubTableBuilder(int32_t index);
100     CALLER_ATTACH BitmapGlyphInfo* GlyphInfo(int32_t glyph_id);
101     int32_t GlyphOffset(int32_t glyph_id);
102     int32_t GlyphLength(int32_t glyph_id);
103     int32_t GlyphFormat(int32_t glyph_id);
104     IndexSubTableBuilderList* IndexSubTableBuilders();
105     // Note: renamed from iterator(), type is the derived type.
106     CALLER_ATTACH BitmapGlyphInfoIterator* GetIterator();
107     void GenerateLocaMap(BitmapGlyphInfoMap* output);
108 
109    protected:
110     void Revert();
111 
112    private:
113     Builder(WritableFontData* data, ReadableFontData* master_data);
114     Builder(ReadableFontData* data, ReadableFontData* master_data);
115 
116     void SetNumberOfIndexSubTables(int32_t count);
117     IndexSubTable::Builder* SearchIndexSubTables(int32_t glyph_id);
118     IndexSubTable::Builder* LinearSearchIndexSubTables(int32_t glyph_id);
119     IndexSubTable::Builder* BinarySearchIndexSubTables(int32_t glyph_id);
120     IndexSubTableBuilderList* GetIndexSubTableBuilders();
121     void Initialize(ReadableFontData* data);
122     CALLER_ATTACH IndexSubTable::Builder* CreateIndexSubTableBuilder(
123         int32_t index);
124 
125     IndexSubTableBuilderList index_sub_tables_;
126   };
127 
128   virtual ~BitmapSizeTable();
129 
130   int32_t IndexSubTableArrayOffset();
131   int32_t IndexTableSize();
132   int32_t NumberOfIndexSubTables();
133   int32_t ColorRef();
134   // TODO(stuartg): SBitLineMetrics hori();
135   // TODO(stuartg): SBitLineMetrics vert();
136   int32_t StartGlyphIndex();
137   int32_t EndGlyphIndex();
138   int32_t PpemX();
139   int32_t PpemY();
140   int32_t BitDepth();
141   int32_t FlagsAsInt();
142 
143   // Note: renamed from indexSubTable()
144   IndexSubTable* GetIndexSubTable(int32_t index);
145   int32_t GlyphOffset(int32_t glyph_id);
146   int32_t GlyphLength(int32_t glyph_id);
147   CALLER_ATTACH BitmapGlyphInfo* GlyphInfo(int32_t glyph_id);
148   int32_t GlyphFormat(int32_t glyph_id);
149 
150  protected:
151   BitmapSizeTable(ReadableFontData* data,
152                   ReadableFontData* master_data);
153 
154  private:
155   static int32_t NumberOfIndexSubTables(ReadableFontData* data,
156                                         int32_t table_offset);
157   IndexSubTable* SearchIndexSubTables(int32_t glyph_id);
158   IndexSubTable* LinearSearchIndexSubTables(int32_t glyph_id);
159   IndexSubTable* BinarySearchIndexSubTables(int32_t glyph_id);
160   CALLER_ATTACH IndexSubTable* CreateIndexSubTable(int32_t index);
161   IndexSubTableList* GetIndexSubTableList();
162 
163   Lock index_subtables_lock_;
164   IndexSubTableList index_subtables_;
165 };
166 typedef Ptr<BitmapSizeTable> BitmapSizeTablePtr;
167 typedef std::vector<BitmapSizeTablePtr> BitmapSizeTableList;
168 typedef Ptr<BitmapSizeTable::Builder> BitmapSizeTableBuilderPtr;
169 typedef std::vector<BitmapSizeTableBuilderPtr> BitmapSizeTableBuilderList;
170 
171 }  // namespace sfntly
172 
173 #endif  // SFNTLY_CPP_SRC_SFNTLY_TABLE_BITMAP_BITMAP_SIZE_TABLE_H_
174