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_TRUETYPE_LOCA_TABLE_H_
18#define SFNTLY_CPP_SRC_SFNTLY_TABLE_TRUETYPE_LOCA_TABLE_H_
19
20#include "sfntly/port/java_iterator.h"
21#include "sfntly/table/table.h"
22#include "sfntly/table/core/font_header_table.h"
23
24namespace sfntly {
25
26// A Loca table - 'loca'.
27class LocaTable : public Table, public RefCounted<LocaTable> {
28 public:
29  class LocaIterator : public PODIterator<int32_t, LocaTable> {
30   public:
31    explicit LocaIterator(LocaTable* table);
32    virtual ~LocaIterator() {}
33
34    virtual bool HasNext();
35    virtual int32_t Next();
36
37   private:
38    int32_t index_;
39  };
40
41  class Builder : public Table::Builder, public RefCounted<Builder> {
42   public:
43    // Constructor scope altered to public for base class to instantiate.
44    Builder(Header* header, WritableFontData* data);
45    Builder(Header* header, ReadableFontData* data);
46    virtual ~Builder();
47
48    static CALLER_ATTACH Builder* CreateBuilder(Header* header,
49                                                WritableFontData* data);
50
51    // Get the format version that will be used when the loca table is
52    // generated.
53    // @return the loca table format version
54    int32_t format_version() { return format_version_; }
55    void set_format_version(int32_t value) { format_version_ = value; }
56
57    // Gets the List of locas for loca table builder. These may be manipulated
58    // in any way by the caller and the changes will be reflected in the final
59    // loca table produced as long as no subsequent call is made to the
60    // SetLocaList(List) method.
61    // If there is no current data for the loca table builder or the loca list
62    // have not been previously set then this will return an empty List.
63    IntegerList* LocaList();
64
65    // Set the list of locas to be used for building this table. If any existing
66    // list was already retrieved with the LocaList() method then the
67    // connection of that previous list to this builder will be broken.
68    void SetLocaList(IntegerList* list);
69
70    // Return the offset for the given glyph id. Valid glyph ids are from 0 to
71    // one less than the number of glyphs. The zero entry is the special entry
72    // for the notdef glyph. The final entry beyond the last glyph id is used to
73    // calculate the size of the last glyph.
74    // @param glyphId the glyph id to get the offset for; must be less than or
75    //        equal to one more than the number of glyph ids
76    // @return the offset in the glyph table to the specified glyph id
77    int32_t GlyphOffset(int32_t glyph_id);
78
79    // Get the length of the data in the glyph table for the specified glyph id.
80    int32_t GlyphLength(int32_t glyph_id);
81
82    // Set the number of glyphs.
83    // This method sets the number of glyphs that the builder will attempt to
84    // parse location data for from the raw binary data. This method only needs
85    // to be called (and <b>must</b> be) when the raw data for this builder has
86    // been changed. It does not by itself reset the data or clear any set loca
87    // list.
88    void SetNumGlyphs(int32_t num_glyphs);
89
90    // Get the number of glyphs that this builder has support for.
91    int NumGlyphs();
92
93    // Revert the loca table builder to the state contained in the last raw data
94    // set on the builder. That raw data may be that read from a font file when
95    // the font builder was created, that set by a user of the loca table
96    // builder, or null data if this builder was created as a new empty builder.
97    void Revert();
98
99    // Get the number of locations or locas. This will be one more than the
100    // number of glyphs for this table since the last loca position is used to
101    // indicate the size of the final glyph.
102    int32_t NumLocas();
103
104    // Get the value from the loca table for the index specified. These are the
105    // raw values from the table that are used to compute the offset and size of
106    // a glyph in the glyph table. Valid index values run from 0 to the number
107    // of glyphs in the font.
108    int32_t Loca(int32_t index);
109
110    virtual CALLER_ATTACH FontDataTable* SubBuildTable(ReadableFontData* data);
111    virtual void SubDataSet();
112    virtual int32_t SubDataSizeToSerialize();
113    virtual bool SubReadyToSerialize();
114    virtual int32_t SubSerialize(WritableFontData* new_data);
115
116   private:
117    // Initialize the internal state from the data. Done lazily since in many
118    // cases the builder will be just creating a table object with no parsing
119    // required.
120    // @param data the data to initialize from
121    void Initialize(ReadableFontData* data);
122
123    // Checks that the glyph id is within the correct range.
124    // @return glyph_id if correct, -1 otherwise.
125    int32_t CheckGlyphRange(int32_t glyph_id);
126
127    int32_t LastGlyphIndex();
128
129    // Internal method to get the loca list if already generated and if not to
130    // initialize the state of the builder.
131    // @return the loca list
132    IntegerList* GetLocaList();
133
134    void ClearLoca(bool nullify);
135
136    int32_t format_version_;  // Note: IndexToLocFormat
137    int32_t num_glyphs_;
138    IntegerList loca_;
139  };
140
141  virtual ~LocaTable();
142
143  int32_t format_version() { return format_version_; }
144  int32_t num_glyphs() { return num_glyphs_; }
145
146  // Return the offset for the given glyph id. Valid glyph ids are from 0 to the
147  // one less than the number of glyphs. The zero entry is the special entry for
148  // the notdef glyph. The final entry beyond the last glyph id is used to
149  // calculate the size of the last glyph.
150  // @param glyphId the glyph id to get the offset for; must be less than or
151  //        equal to one more than the number of glyph ids
152  // @return the offset in the glyph table to the specified glyph id
153  int32_t GlyphOffset(int32_t glyph_id);
154
155  // Get the length of the data in the glyph table for the specified glyph id.
156  int32_t GlyphLength(int32_t glyph_id);
157
158  // Get the number of locations or locas. This will be one more than the number
159  // of glyphs for this table since the last loca position is used to indicate
160  // the size of the final glyph.
161  int32_t NumLocas();
162
163  // Get the value from the loca table for the index specified. Valid index
164  // values run from 0 to the number of glyphs in the font.
165  int32_t Loca(int32_t index);
166
167 private:
168  LocaTable(Header* header,
169            ReadableFontData* data,
170            int32_t format_version,
171            int32_t num_glyphs);
172
173  int32_t format_version_;  // Note: Java's version, renamed to format_version_
174  int32_t num_glyphs_;
175
176  friend class LocaIterator;
177};
178typedef Ptr<LocaTable> LocaTablePtr;
179typedef Ptr<LocaTable::Builder> LocaTableBuilderPtr;
180
181}  // namespace sfntly
182
183#endif  // SFNTLY_CPP_SRC_SFNTLY_TABLE_TRUETYPE_LOCA_TABLE_H_
184