summaryrefslogtreecommitdiff
path: root/sfntly/table/bitmap/index_sub_table.h
blob: 0993e761adbbbc4bbcdbbd8b8279b0c62d824510 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * Copyright 2011 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef SFNTLY_CPP_SRC_SFNTLY_TABLE_BITMAP_INDEX_SUBTABLE_H_
#define SFNTLY_CPP_SRC_SFNTLY_TABLE_BITMAP_INDEX_SUBTABLE_H_

#include <vector>

#include "sfntly/port/java_iterator.h"
#include "sfntly/table/subtable.h"
#include "sfntly/table/bitmap/bitmap_glyph_info.h"

namespace sfntly {

class IndexSubTable : public SubTable {
 public:
  class Builder : public SubTable::Builder {
   public:
    virtual ~Builder();

    void Revert();

    int32_t index_format() { return index_format_; }
    int32_t first_glyph_index() { return first_glyph_index_; }
    void set_first_glyph_index(int32_t v) { first_glyph_index_ = v; }
    int32_t last_glyph_index() { return last_glyph_index_; }
    void set_last_glyph_index(int32_t v) { last_glyph_index_ = v; }
    int32_t image_format() { return image_format_; }
    void set_image_format(int32_t v) { image_format_ = v; }
    int32_t image_data_offset() { return image_data_offset_; }
    void set_image_data_offset(int32_t v) { image_data_offset_ = v; }

    virtual int32_t NumGlyphs() = 0;

    // Gets the glyph info for the specified glyph id.
    // @param glyphId the glyph id to look up
    // @return the glyph info
    CALLER_ATTACH virtual BitmapGlyphInfo* GlyphInfo(int32_t glyph_id);

    // Gets the full offset of the glyph within the EBDT table.
    // @param glyphId the glyph id
    // @return the glyph offset
    virtual int32_t GlyphOffset(int32_t glyph_id);

    // Gets the offset of the glyph relative to the block for this index
    // subtable.
    // @param glyphId the glyph id
    // @return the glyph offset
    virtual int32_t GlyphStartOffset(int32_t glyph_id) = 0;

    // Gets the length of the glyph within the EBDT table.
    // @param glyphId the glyph id
    // @return the glyph offset
    virtual int32_t GlyphLength(int32_t glyph_id) = 0;

    // Note: renamed from java iterator()
    CALLER_ATTACH virtual Iterator<BitmapGlyphInfo, IndexSubTable::Builder>*
        GetIterator() = 0;

    // Static instantiation function.
    static CALLER_ATTACH Builder*
        CreateBuilder(ReadableFontData* data,
                      int32_t offset_to_index_sub_table_array,
                      int32_t array_index);

    // The following methods will never be called but they need to be here to
    // allow the BitmapSizeTable to see these methods through an abstract
    // reference.
    virtual CALLER_ATTACH FontDataTable* SubBuildTable(ReadableFontData* data);
    virtual void SubDataSet();
    virtual int32_t SubDataSizeToSerialize();
    virtual bool SubReadyToSerialize();
    virtual int32_t SubSerialize(WritableFontData* new_data);

   protected:
    Builder(WritableFontData* data,
            int32_t first_glyph_index,
            int32_t last_glyph_index);
    Builder(ReadableFontData* data,
            int32_t first_glyph_index,
            int32_t last_glyph_index);
    Builder(int32_t index_format,
            int32_t image_format,
            int32_t image_data_offset);

    // Checks that the glyph id is within the correct range. If it returns the
    // offset of the glyph id from the start of the range.
    // @param glyphId
    // @return the offset of the glyphId from the start of the glyph range
    // @throws IndexOutOfBoundsException if the glyph id is not within the
    //         correct range
    int32_t CheckGlyphRange(int32_t glyph_id);
    int32_t SerializeIndexSubHeader(WritableFontData* data);

   private:
    void Initialize(ReadableFontData* data);

    int32_t first_glyph_index_;
    int32_t last_glyph_index_;
    int32_t index_format_;
    int32_t image_format_;
    int32_t image_data_offset_;
  };

  int32_t index_format() { return index_format_; }
  int32_t first_glyph_index() { return first_glyph_index_; }
  int32_t last_glyph_index() { return last_glyph_index_; }
  int32_t image_format() { return image_format_; }
  int32_t image_data_offset() { return image_data_offset_; }

  CALLER_ATTACH BitmapGlyphInfo* GlyphInfo(int32_t glyph_id);
  virtual int32_t GlyphOffset(int32_t glyph_id);
  virtual int32_t GlyphStartOffset(int32_t glyph_id) = 0;
  virtual int32_t GlyphLength(int32_t glyph_id) = 0;
  virtual int32_t NumGlyphs() = 0;

  static CALLER_ATTACH IndexSubTable*
      CreateIndexSubTable(ReadableFontData* data,
                          int32_t offset_to_index_sub_table_array,
                          int32_t array_index);

 protected:
  // Note: the constructor does not implement offset/length form provided in
  //       Java to avoid heavy lifting in constructors.  Callers to call
  //       GetDataLength() static method of the derived class to get proper
  //       length and slice ahead.
  IndexSubTable(ReadableFontData* data,
                int32_t first_glyph_index,
                int32_t last_glyph_index);

  int32_t CheckGlyphRange(int32_t glyph_id);
  static int32_t CheckGlyphRange(int32_t glyph_id,
                                 int32_t first_glyph_id,
                                 int32_t last_glyph_id);

 private:
  int32_t first_glyph_index_;
  int32_t last_glyph_index_;
  int32_t index_format_;
  int32_t image_format_;
  int32_t image_data_offset_;
};
typedef Ptr<IndexSubTable> IndexSubTablePtr;
typedef std::vector<IndexSubTablePtr> IndexSubTableList;
typedef Ptr<IndexSubTable::Builder> IndexSubTableBuilderPtr;
typedef std::vector<IndexSubTableBuilderPtr> IndexSubTableBuilderList;
typedef Iterator<BitmapGlyphInfo, IndexSubTable::Builder> BitmapGlyphInfoIter;
typedef Ptr<BitmapGlyphInfoIter> BitmapGlyphInfoIterPtr;

}  // namespace sfntly

#endif  // SFNTLY_CPP_SRC_SFNTLY_TABLE_BITMAP_INDEX_SUBTABLE_H_