• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_GLYPH_TABLE_H_
18 #define SFNTLY_CPP_SRC_SFNTLY_TABLE_TRUETYPE_GLYPH_TABLE_H_
19 
20 #include <vector>
21 
22 #include "sfntly/table/table.h"
23 #include "sfntly/table/subtable.h"
24 #include "sfntly/table/subtable_container_table.h"
25 
26 namespace sfntly {
27 
28 struct GlyphType {
29   enum {
30     kSimple = 0,
31     kComposite = 1
32   };
33 };
34 
35 class GlyphTable : public SubTableContainerTable,
36                    public RefCounted<GlyphTable> {
37  public:
38   class Builder;
39   class Glyph : public SubTable {
40    public:
41     // Note: Contour is an empty class for the version ported
42     class Contour {
43      protected:
Contour()44       Contour() {}
~Contour()45       virtual ~Contour() {}
46     };
47 
48     class Builder : public SubTable::Builder {
49      public:
50       virtual ~Builder();
51 
52      protected:
53       // Incoming table_builder is GlyphTable::Builder*.
54       // Note: constructor refactored in C++ to avoid heavy lifting.
55       //       caller need to do data->Slice(offset, length) beforehand.
56       explicit Builder(WritableFontData* data);
57       explicit Builder(ReadableFontData* data);
58 
59       static CALLER_ATTACH Builder*
60           GetBuilder(GlyphTable::Builder* table_builder,
61                      ReadableFontData* data);
62       static CALLER_ATTACH Builder*
63           GetBuilder(GlyphTable::Builder* table_builder,
64                      ReadableFontData* data,
65                      int32_t offset,
66                      int32_t length);
67       virtual void SubDataSet();
68       virtual int32_t SubDataSizeToSerialize();
69       virtual bool SubReadyToSerialize();
70       virtual int32_t SubSerialize(WritableFontData* new_data);
71 
72      private:
73       friend class GlyphTable::Builder;
74     };
75 
76     virtual ~Glyph();
77     static CALLER_ATTACH Glyph* GetGlyph(GlyphTable* table,
78                                          ReadableFontData* data,
79                                          int32_t offset,
80                                          int32_t length);
81 
82     virtual int32_t Padding();
83     virtual int32_t GlyphType();
84     virtual int32_t NumberOfContours();
85     virtual int32_t XMin();
86     virtual int32_t XMax();
87     virtual int32_t YMin();
88     virtual int32_t YMax();
89 
90     virtual int32_t InstructionSize() = 0;
91     virtual ReadableFontData* Instructions() = 0;
92 
93    protected:
94     // Note: constructor refactored in C++ to avoid heavy lifting.
95     //       caller need to do data->Slice(offset, length) beforehand.
96     Glyph(ReadableFontData* data, int32_t glyph_type);
97     virtual void Initialize() = 0;
98     // Note: Derived class to define initialization_lock_.
99 
100    private:
101     static int32_t GlyphType(ReadableFontData* data,
102                              int32_t offset,
103                              int32_t length);
104 
105     int32_t glyph_type_;
106     int32_t number_of_contours_;
107   };  // class GlyphTable::Glyph
108   typedef Ptr<GlyphTable::Glyph::Builder> GlyphBuilderPtr;
109   typedef std::vector<GlyphBuilderPtr> GlyphBuilderList;
110 
111   class Builder : public SubTableContainerTable::Builder,
112                   public RefCounted<GlyphTable::Builder> {
113    public:
114     // Note: Constructor scope altered to public for base class to instantiate.
115     Builder(Header* header, ReadableFontData* data);
116     virtual ~Builder();
117 
118     virtual void SetLoca(const std::vector<int32_t>& loca);
119     virtual void GenerateLocaList(std::vector<int32_t>* locas);
120 
121     static CALLER_ATTACH Builder* CreateBuilder(Header* header,
122                                                 WritableFontData* data);
123 
124     // Gets the List of glyph builders for the glyph table builder. These may be
125     // manipulated in any way by the caller and the changes will be reflected in
126     // the final glyph table produced.
127     // If there is no current data for the glyph builder or the glyph builders
128     // have not been previously set then this will return an empty glyph builder
129     // List. If there is current data (i.e. data read from an existing font) and
130     // the <code>loca</code> list has not been set or is null, empty, or
131     // invalid, then an empty glyph builder List will be returned.
132     GlyphBuilderList* GlyphBuilders();
133 
134     // Replace the internal glyph builders with the one provided. The provided
135     // list and all contained objects belong to this builder.
136     // This call is only required if the entire set of glyphs in the glyph
137     // table builder are being replaced. If the glyph builder list provided from
138     // the GlyphTable.Builder::GlyphBuilders() is being used and modified
139     // then those changes will already be reflected in the glyph table builder.
140     void SetGlyphBuilders(GlyphBuilderList* glyph_builders);
141 
142     // Glyph builder factories
143     CALLER_ATTACH Glyph::Builder* GlyphBuilder(ReadableFontData* data);
144 
145    protected:  // internal API for building
146     virtual CALLER_ATTACH FontDataTable* SubBuildTable(ReadableFontData* data);
147     virtual void SubDataSet();
148     virtual int32_t SubDataSizeToSerialize();
149     virtual bool SubReadyToSerialize();
150     virtual int32_t SubSerialize(WritableFontData* new_data);
151 
152    private:
153     void Initialize(ReadableFontData* data, const std::vector<int32_t>& loca);
154     GlyphBuilderList* GetGlyphBuilders();
155     void Revert();
156 
157     GlyphBuilderList glyph_builders_;
158     std::vector<int32_t> loca_;
159   };
160 
161   class SimpleGlyph : public Glyph, public RefCounted<SimpleGlyph> {
162    public:
163     static const int32_t kFLAG_ONCURVE;
164     static const int32_t kFLAG_XSHORT;
165     static const int32_t kFLAG_YSHORT;
166     static const int32_t kFLAG_REPEAT;
167     static const int32_t kFLAG_XREPEATSIGN;
168     static const int32_t kFLAG_YREPEATSIGN;
169 
170     class SimpleContour : public Glyph::Contour {
171      protected:
SimpleContour()172       SimpleContour() {}
~SimpleContour()173       virtual ~SimpleContour() {}
174     };
175 
176     class SimpleGlyphBuilder : public Glyph::Builder,
177                                public RefCounted<SimpleGlyphBuilder> {
178      public:
179       virtual ~SimpleGlyphBuilder();
180 
181      protected:
182       // Note: constructor refactored in C++ to avoid heavy lifting.
183       //       caller need to do data->Slice(offset, length) beforehand.
184       explicit SimpleGlyphBuilder(WritableFontData* data);
185       explicit SimpleGlyphBuilder(ReadableFontData* data);
186       virtual CALLER_ATTACH FontDataTable*
187           SubBuildTable(ReadableFontData* data);
188 
189      private:
190       friend class Glyph::Builder;
191     };
192 
193     // Note: constructor refactored in C++ to avoid heavy lifting.
194     //       caller need to do data->Slice(offset, length) beforehand.
195     explicit SimpleGlyph(ReadableFontData* data);
196     virtual ~SimpleGlyph();
197 
198     virtual int32_t InstructionSize();
199     virtual CALLER_ATTACH ReadableFontData* Instructions();
200     virtual void Initialize();
201 
202    private:
203     void ParseData(bool fill_arrays);
204     int32_t FlagAsInt(int32_t index);
205     int32_t ContourEndPoint(int32_t contour);
206 
207     bool initialized_;
208     Lock initialization_lock_;
209     int32_t instruction_size_;
210     int32_t number_of_points_;
211 
212     // start offsets of the arrays
213     int32_t instructions_offset_;
214     int32_t flags_offset_;
215     int32_t x_coordinates_offset_;
216     int32_t y_coordinates_offset_;
217 
218     int32_t flag_byte_count_;
219     int32_t x_byte_count_;
220     int32_t y_byte_count_;
221 
222     std::vector<int32_t> x_coordinates_;
223     std::vector<int32_t> y_coordinates_;
224     std::vector<bool> on_curve_;
225     std::vector<int32_t> contour_index_;
226   };
227 
228   class CompositeGlyph : public Glyph, public RefCounted<CompositeGlyph> {
229    public:
230     static const int32_t kFLAG_ARG_1_AND_2_ARE_WORDS;
231     static const int32_t kFLAG_ARGS_ARE_XY_VALUES;
232     static const int32_t kFLAG_ROUND_XY_TO_GRID;
233     static const int32_t kFLAG_WE_HAVE_A_SCALE;
234     static const int32_t kFLAG_RESERVED;
235     static const int32_t kFLAG_MORE_COMPONENTS;
236     static const int32_t kFLAG_WE_HAVE_AN_X_AND_Y_SCALE;
237     static const int32_t kFLAG_WE_HAVE_A_TWO_BY_TWO;
238     static const int32_t kFLAG_WE_HAVE_INSTRUCTIONS;
239     static const int32_t kFLAG_USE_MY_METRICS;
240     static const int32_t kFLAG_OVERLAP_COMPOUND;
241     static const int32_t kFLAG_SCALED_COMPONENT_OFFSET;
242     static const int32_t kFLAG_UNSCALED_COMPONENT_OFFSET;
243 
244     class CompositeGlyphBuilder : public Glyph::Builder,
245                                   public RefCounted<CompositeGlyphBuilder> {
246      public:
247       virtual ~CompositeGlyphBuilder();
248 
249      protected:
250       // Note: constructor refactored in C++ to avoid heavy lifting.
251       //       caller need to do data->Slice(offset, length) beforehand.
252       explicit CompositeGlyphBuilder(WritableFontData* data);
253       explicit CompositeGlyphBuilder(ReadableFontData* data);
254 
255       virtual CALLER_ATTACH FontDataTable*
256           SubBuildTable(ReadableFontData* data);
257 
258      private:
259       friend class Glyph::Builder;
260     };
261 
262     // Note: constructor refactored in C++ to avoid heavy lifting.
263     //       caller need to do data->Slice(offset, length) beforehand.
264     explicit CompositeGlyph(ReadableFontData* data);
265     virtual ~CompositeGlyph();
266 
267     int32_t Flags(int32_t contour);
268     int32_t NumGlyphs();
269     int32_t GlyphIndex(int32_t contour);
270     virtual int32_t InstructionSize();
271     virtual CALLER_ATTACH ReadableFontData* Instructions();
272 
273    protected:
274     virtual void Initialize();
275 
276    private:
277     std::vector<int32_t> contour_index_;
278     int32_t instruction_size_;
279     int32_t instructions_offset_;
280     bool initialized_;
281     Lock initialization_lock_;
282   };
283 
284   virtual ~GlyphTable();
285 
286   // C++ port: rename glyph() to GetGlyph().
287   Glyph* GetGlyph(int32_t offset, int32_t length);
288 
289  private:
290   struct Offset {
291     enum {
292       // header
293       kNumberOfContours = 0,
294       kXMin = 2,
295       kYMin = 4,
296       kXMax = 6,
297       kYMax = 8,
298 
299       // Simple Glyph Description
300       kSimpleEndPtsOfCountours = 10,
301       // offset from the end of the contours array
302       kSimpleInstructionLength = 0,
303       kSimpleInstructions = 2,
304       // flags
305       // xCoordinates
306       // yCoordinates
307 
308       // Composite Glyph Description
309       kCompositeFlags = 0,
310       kCompositeGyphIndexWithoutFlag = 0,
311       kCompositeGlyphIndexWithFlag = 2,
312     };
313   };
314 
315   GlyphTable(Header* header, ReadableFontData* data);
316 };
317 typedef Ptr<GlyphTable> GlyphTablePtr;
318 typedef Ptr<GlyphTable::Builder> GlyphTableBuilderPtr;
319 typedef std::vector<GlyphTableBuilderPtr> GlyphTableBuilderList;
320 typedef Ptr<GlyphTable::Glyph> GlyphPtr;
321 typedef Ptr<GlyphTable::Glyph::Builder> GlyphBuilderPtr;
322 
323 }  // namespace sfntly
324 
325 #endif  // SFNTLY_CPP_SRC_SFNTLY_TABLE_TRUETYPE_GLYPH_TABLE_H_
326