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_H_
18#define SFNTLY_CPP_SRC_SFNTLY_TABLE_BITMAP_INDEX_SUBTABLE_H_
19
20#include <vector>
21
22#include "sfntly/port/java_iterator.h"
23#include "sfntly/table/subtable.h"
24#include "sfntly/table/bitmap/bitmap_glyph_info.h"
25
26namespace sfntly {
27
28class IndexSubTable : public SubTable {
29 public:
30  struct Format {
31    enum {
32      FORMAT_1 = 1,
33      FORMAT_2 = 2,
34      FORMAT_3 = 3,
35      FORMAT_4 = 4,
36      FORMAT_5 = 5,
37    };
38  };
39
40  class Builder : public SubTable::Builder {
41   public:
42    virtual ~Builder();
43
44    void Revert();
45
46    int32_t index_format() { return index_format_; }
47    int32_t first_glyph_index() { return first_glyph_index_; }
48    void set_first_glyph_index(int32_t v) { first_glyph_index_ = v; }
49    int32_t last_glyph_index() { return last_glyph_index_; }
50    void set_last_glyph_index(int32_t v) { last_glyph_index_ = v; }
51    int32_t image_format() { return image_format_; }
52    void set_image_format(int32_t v) { image_format_ = v; }
53    int32_t image_data_offset() { return image_data_offset_; }
54    void set_image_data_offset(int32_t v) { image_data_offset_ = v; }
55
56    virtual int32_t NumGlyphs() = 0;
57
58    // Gets the glyph info for the specified glyph id.
59    // @param glyphId the glyph id to look up
60    // @return the glyph info
61    CALLER_ATTACH virtual BitmapGlyphInfo* GlyphInfo(int32_t glyph_id);
62
63    // Gets the full offset of the glyph within the EBDT table.
64    // @param glyphId the glyph id
65    // @return the glyph offset
66    virtual int32_t GlyphOffset(int32_t glyph_id);
67
68    // Gets the offset of the glyph relative to the block for this index
69    // subtable.
70    // @param glyphId the glyph id
71    // @return the glyph offset
72    virtual int32_t GlyphStartOffset(int32_t glyph_id) = 0;
73
74    // Gets the length of the glyph within the EBDT table.
75    // @param glyphId the glyph id
76    // @return the glyph offset
77    virtual int32_t GlyphLength(int32_t glyph_id) = 0;
78
79    // Note: renamed from java iterator()
80    CALLER_ATTACH virtual Iterator<BitmapGlyphInfo, IndexSubTable::Builder>*
81        GetIterator() = 0;
82
83    // Static instantiation function.
84    static CALLER_ATTACH Builder* CreateBuilder(int32_t index_format);
85    static CALLER_ATTACH Builder*
86        CreateBuilder(ReadableFontData* data,
87                      int32_t offset_to_index_sub_table_array,
88                      int32_t array_index);
89
90    // The following methods will never be called but they need to be here to
91    // allow the BitmapSizeTable to see these methods through an abstract
92    // reference.
93    virtual CALLER_ATTACH FontDataTable* SubBuildTable(ReadableFontData* data);
94    virtual void SubDataSet();
95    virtual int32_t SubDataSizeToSerialize();
96    virtual bool SubReadyToSerialize();
97    virtual int32_t SubSerialize(WritableFontData* new_data);
98
99   protected:
100    Builder(int32_t data_size, int32_t index_format);
101    Builder(int32_t index_format,
102            int32_t image_format,
103            int32_t image_data_offset,
104            int32_t data_size);
105    Builder(WritableFontData* data,
106            int32_t first_glyph_index,
107            int32_t last_glyph_index);
108    Builder(ReadableFontData* data,
109            int32_t first_glyph_index,
110            int32_t last_glyph_index);
111
112    // Checks that the glyph id is within the correct range. If it returns the
113    // offset of the glyph id from the start of the range.
114    // @param glyphId
115    // @return the offset of the glyphId from the start of the glyph range
116    // @throws IndexOutOfBoundsException if the glyph id is not within the
117    //         correct range
118    int32_t CheckGlyphRange(int32_t glyph_id);
119    int32_t SerializeIndexSubHeader(WritableFontData* data);
120
121   private:
122    void Initialize(ReadableFontData* data);
123
124    int32_t first_glyph_index_;
125    int32_t last_glyph_index_;
126    int32_t index_format_;
127    int32_t image_format_;
128    int32_t image_data_offset_;
129  };
130
131  int32_t index_format() { return index_format_; }
132  int32_t first_glyph_index() { return first_glyph_index_; }
133  int32_t last_glyph_index() { return last_glyph_index_; }
134  int32_t image_format() { return image_format_; }
135  int32_t image_data_offset() { return image_data_offset_; }
136
137  CALLER_ATTACH BitmapGlyphInfo* GlyphInfo(int32_t glyph_id);
138  virtual int32_t GlyphOffset(int32_t glyph_id);
139  virtual int32_t GlyphStartOffset(int32_t glyph_id) = 0;
140  virtual int32_t GlyphLength(int32_t glyph_id) = 0;
141  virtual int32_t NumGlyphs() = 0;
142
143  static CALLER_ATTACH IndexSubTable*
144      CreateIndexSubTable(ReadableFontData* data,
145                          int32_t offset_to_index_sub_table_array,
146                          int32_t array_index);
147
148 protected:
149  // Note: the constructor does not implement offset/length form provided in
150  //       Java to avoid heavy lifting in constructors.  Callers to call
151  //       GetDataLength() static method of the derived class to get proper
152  //       length and slice ahead.
153  IndexSubTable(ReadableFontData* data,
154                int32_t first_glyph_index,
155                int32_t last_glyph_index);
156
157  int32_t CheckGlyphRange(int32_t glyph_id);
158  static int32_t CheckGlyphRange(int32_t glyph_id,
159                                 int32_t first_glyph_id,
160                                 int32_t last_glyph_id);
161
162 private:
163  int32_t first_glyph_index_;
164  int32_t last_glyph_index_;
165  int32_t index_format_;
166  int32_t image_format_;
167  int32_t image_data_offset_;
168};
169typedef Ptr<IndexSubTable> IndexSubTablePtr;
170typedef std::vector<IndexSubTablePtr> IndexSubTableList;
171typedef Ptr<IndexSubTable::Builder> IndexSubTableBuilderPtr;
172typedef std::vector<IndexSubTableBuilderPtr> IndexSubTableBuilderList;
173typedef Iterator<BitmapGlyphInfo, IndexSubTable::Builder> BitmapGlyphInfoIter;
174typedef Ptr<BitmapGlyphInfoIter> BitmapGlyphInfoIterPtr;
175
176}  // namespace sfntly
177
178#endif  // SFNTLY_CPP_SRC_SFNTLY_TABLE_BITMAP_INDEX_SUBTABLE_H_
179