1464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com/*
2464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com * Copyright 2011 Google Inc. All Rights Reserved.
3464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com *
4464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com * Licensed under the Apache License, Version 2.0 (the "License");
5464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com * you may not use this file except in compliance with the License.
6464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com * You may obtain a copy of the License at
7464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com *
8464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com *      http://www.apache.org/licenses/LICENSE-2.0
9464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com *
10464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com * Unless required by applicable law or agreed to in writing, software
11464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com * distributed under the License is distributed on an "AS IS" BASIS,
12464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com * See the License for the specific language governing permissions and
14464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com * limitations under the License.
15464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com */
16464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
175af34fd773f8cfee82321393504f558ddf67c628arthurhsu@google.com#ifndef SFNTLY_CPP_SRC_SFNTLY_TABLE_FONT_DATA_TABLE_H_
185af34fd773f8cfee82321393504f558ddf67c628arthurhsu@google.com#define SFNTLY_CPP_SRC_SFNTLY_TABLE_FONT_DATA_TABLE_H_
19464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
20464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com#include "sfntly/data/readable_font_data.h"
21b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com#include "sfntly/data/writable_font_data.h"
22464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com#include "sfntly/port/refcount.h"
23464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
24464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.comnamespace sfntly {
25464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
26b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com// An abstract base for any table that contains a FontData. This is the root of
27b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com// the table class hierarchy.
28464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.comclass FontDataTable : virtual public RefCount {
29464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com public:
30464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // Note: original version is abstract Builder<T extends FontDataTable>
31464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  //       C++ template is not designed that way so plain class is chosen.
32464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  class Builder : virtual public RefCount {
33464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com   public:
34464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    // Get a snapshot copy of the internal data of the builder.
35464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    // This causes any internal data structures to be serialized to a new data
36464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    // object. This data object belongs to the caller and must be properly
37464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    // disposed of. No changes are made to the builder and any changes to the
38464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    // data directly do not affect the internal state. To do that a subsequent
39246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    // call must be made to {@link #SetData(WritableFontData)}.
40464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    // @return a copy of the internal data of the builder
41246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    CALLER_ATTACH WritableFontData* Data();
42246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    virtual void SetData(ReadableFontData* data);
43246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
44246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    // Note: changed from protected to avoid accessibility error in C++
45246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    virtual CALLER_ATTACH FontDataTable* Build();
46246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    virtual bool ReadyToBuild();
47246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
48246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    ReadableFontData* InternalReadData();
49246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    WritableFontData* InternalWriteData();
50246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
51246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    bool data_changed() { return data_changed_; }
52b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com    bool model_changed() {
53b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com      return current_model_changed() || contained_model_changed();
54b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com    }
55b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com    bool current_model_changed() { return model_changed_; }
56b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com    bool contained_model_changed() { return contained_model_changed_; }
57b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com
58246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    bool set_model_changed() { return set_model_changed(true); }
59246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    bool set_model_changed(bool changed) {
60246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com      bool old = model_changed_;
61246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com      model_changed_ = changed;
62246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com      return old;
63246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    }
64464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
65246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com   protected:
66b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com    explicit Builder();
6753847b66af7e0c1af9928e4da2e978ed3b7c2a25arthurhsu@google.com
6853847b66af7e0c1af9928e4da2e978ed3b7c2a25arthurhsu@google.com    // Construct a FontDataTable.Builder with a WritableFontData backing store
6953847b66af7e0c1af9928e4da2e978ed3b7c2a25arthurhsu@google.com    // of size given. A positive size will create a fixed size backing store and
7053847b66af7e0c1af9928e4da2e978ed3b7c2a25arthurhsu@google.com    // a 0 or less size is an estimate for a growable backing store with the
7153847b66af7e0c1af9928e4da2e978ed3b7c2a25arthurhsu@google.com    // estimate being the absolute of the size.
7253847b66af7e0c1af9928e4da2e978ed3b7c2a25arthurhsu@google.com    // @param dataSize if positive then a fixed size; if 0 or less then an
7353847b66af7e0c1af9928e4da2e978ed3b7c2a25arthurhsu@google.com    //        estimate for a growable size
7453847b66af7e0c1af9928e4da2e978ed3b7c2a25arthurhsu@google.com    Builder(int32_t data_size);
75b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com    Builder(WritableFontData* data);
76b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com    Builder(ReadableFontData* data);
77246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    virtual ~Builder();
78246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
79246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    // subclass API
80246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    virtual void NotifyPostTableBuild(FontDataTable* table);
81246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    virtual int32_t SubSerialize(WritableFontData* new_data) = 0;
82246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    virtual bool SubReadyToSerialize() = 0;
83246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    virtual int32_t SubDataSizeToSerialize() = 0;
84246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    virtual void SubDataSet() = 0;
85464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    virtual CALLER_ATTACH FontDataTable*
86246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com        SubBuildTable(ReadableFontData* data) = 0;
87464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
88464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com   private:
89246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    void InternalSetData(WritableFontData* data, bool data_changed);
90246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    void InternalSetData(ReadableFontData* data, bool data_changed);
91246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
92464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    WritableFontDataPtr w_data_;
93464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    ReadableFontDataPtr r_data_;
94464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    bool model_changed_;
95b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com    bool contained_model_changed_;  // may expand to list of submodel states
96464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    bool data_changed_;
97464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  };
98464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
99246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  explicit FontDataTable(ReadableFontData* data);
100246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  virtual ~FontDataTable();
101246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
102246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  // Get the readable font data for this table.
103246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  ReadableFontData* ReadFontData();
104246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
105246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  // Get the length of the data for this table in bytes. This is the full
106b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  // allocated length of the data underlying the table and may or may not
107b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  // include any padding.
108246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  virtual int32_t DataLength();
109246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
110246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  virtual int32_t Serialize(OutputStream* os);
111246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
112464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com protected:
113b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  virtual int32_t Serialize(WritableFontData* data);
114b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com
115246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  // TODO(arthurhsu): style guide violation: protected member, need refactoring
116464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  ReadableFontDataPtr data_;
117464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com};
118464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.comtypedef Ptr<FontDataTable> FontDataTablePtr;
1198427926b2267e501fa38649cffeb9d62a550af56dfilimon@google.comtypedef Ptr<FontDataTable::Builder> FontDataTableBuilderPtr;
120464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
121464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com}  // namespace sfntly
122464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
1235af34fd773f8cfee82321393504f558ddf67c628arthurhsu@google.com#endif  // SFNTLY_CPP_SRC_SFNTLY_TABLE_FONT_DATA_TABLE_H_
124