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_FONT_DATA_TABLE_H_
18 #define SFNTLY_CPP_SRC_SFNTLY_TABLE_FONT_DATA_TABLE_H_
19 
20 #include "sfntly/data/readable_font_data.h"
21 #include "sfntly/data/writable_font_data.h"
22 #include "sfntly/port/refcount.h"
23 
24 namespace sfntly {
25 
26 // An abstract base for any table that contains a FontData. This is the root of
27 // the table class hierarchy.
28 class FontDataTable : virtual public RefCount {
29  public:
30   // Note: original version is abstract Builder<T extends FontDataTable>
31   //       C++ template is not designed that way so plain class is chosen.
32   class Builder : virtual public RefCount {
33    public:
34     // Get a snapshot copy of the internal data of the builder.
35     // This causes any internal data structures to be serialized to a new data
36     // object. This data object belongs to the caller and must be properly
37     // disposed of. No changes are made to the builder and any changes to the
38     // data directly do not affect the internal state. To do that a subsequent
39     // call must be made to {@link #SetData(WritableFontData)}.
40     // @return a copy of the internal data of the builder
41     CALLER_ATTACH WritableFontData* Data();
42     virtual void SetData(ReadableFontData* data);
43 
44     // Note: changed from protected to avoid accessibility error in C++
45     virtual CALLER_ATTACH FontDataTable* Build();
46     virtual bool ReadyToBuild();
47 
48     ReadableFontData* InternalReadData();
49     WritableFontData* InternalWriteData();
50 
data_changed()51     bool data_changed() { return data_changed_; }
model_changed()52     bool model_changed() {
53       return current_model_changed() || contained_model_changed();
54     }
current_model_changed()55     bool current_model_changed() { return model_changed_; }
contained_model_changed()56     bool contained_model_changed() { return contained_model_changed_; }
57 
set_model_changed()58     bool set_model_changed() { return set_model_changed(true); }
set_model_changed(bool changed)59     bool set_model_changed(bool changed) {
60       bool old = model_changed_;
61       model_changed_ = changed;
62       return old;
63     }
64 
65    protected:
66     explicit Builder();
67 
68     // Construct a FontDataTable.Builder with a WritableFontData backing store
69     // of size given. A positive size will create a fixed size backing store and
70     // a 0 or less size is an estimate for a growable backing store with the
71     // estimate being the absolute of the size.
72     // @param dataSize if positive then a fixed size; if 0 or less then an
73     //        estimate for a growable size
74     Builder(int32_t data_size);
75     Builder(WritableFontData* data);
76     Builder(ReadableFontData* data);
77     virtual ~Builder();
78 
79     // subclass API
80     virtual void NotifyPostTableBuild(FontDataTable* table);
81     virtual int32_t SubSerialize(WritableFontData* new_data) = 0;
82     virtual bool SubReadyToSerialize() = 0;
83     virtual int32_t SubDataSizeToSerialize() = 0;
84     virtual void SubDataSet() = 0;
85     virtual CALLER_ATTACH FontDataTable*
86         SubBuildTable(ReadableFontData* data) = 0;
87 
88    private:
89     void InternalSetData(WritableFontData* data, bool data_changed);
90     void InternalSetData(ReadableFontData* data, bool data_changed);
91 
92     WritableFontDataPtr w_data_;
93     ReadableFontDataPtr r_data_;
94     bool model_changed_;
95     bool contained_model_changed_;  // may expand to list of submodel states
96     bool data_changed_;
97   };
98 
99   explicit FontDataTable(ReadableFontData* data);
100   virtual ~FontDataTable();
101 
102   // Get the readable font data for this table.
103   ReadableFontData* ReadFontData();
104 
105   // Get the length of the data for this table in bytes. This is the full
106   // allocated length of the data underlying the table and may or may not
107   // include any padding.
108   virtual int32_t DataLength();
109 
110   virtual int32_t Serialize(OutputStream* os);
111 
112  protected:
113   virtual int32_t Serialize(WritableFontData* data);
114 
115   // TODO(arthurhsu): style guide violation: protected member, need refactoring
116   ReadableFontDataPtr data_;
117 };
118 typedef Ptr<FontDataTable> FontDataTablePtr;
119 typedef Ptr<FontDataTable::Builder> FontDataTableBuilderPtr;
120 
121 }  // namespace sfntly
122 
123 #endif  // SFNTLY_CPP_SRC_SFNTLY_TABLE_FONT_DATA_TABLE_H_
124