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_DATA_WRITABLE_FONT_DATA_H_
18 #define SFNTLY_CPP_SRC_SFNTLY_DATA_WRITABLE_FONT_DATA_H_
19 
20 #include "sfntly/data/readable_font_data.h"
21 
22 namespace sfntly {
23 
24 // Writable font data wrapper. Supports writing of data primitives in the
25 // TrueType / OpenType spec.
26 class WritableFontData : public ReadableFontData {
27  public:
28   explicit WritableFontData(ByteArray* ba);
29   virtual ~WritableFontData();
30 
31   // Constructs a writable font data object. If the length is specified as
32   // positive then a fixed size font data object will be created. If the length
33   // is zero or less then a growable font data object will be created and the
34   // size will be used as an estimate to help in allocating the original space.
35   // @param length if length > 0 create a fixed length font data; otherwise
36   //        create a growable font data
37   // @return a new writable font data
38   static CALLER_ATTACH WritableFontData* CreateWritableFontData(int32_t length);
39 
40   // Constructs a writable font data object. The new font data object will wrap
41   // the bytes passed in to the factory and it will take make a copy of those
42   // bytes.
43   // @param b the byte vector to wrap
44   // @return a new writable font data
45   static CALLER_ATTACH WritableFontData* CreateWritableFontData(ByteVector* b);
46 
47   // Write a byte at the given index.
48   // @param index index into the font data
49   // @param b the byte to write
50   // @return the number of bytes written
51   virtual int32_t WriteByte(int32_t index, byte_t b);
52 
53   // Write the bytes from the array.
54   // @param index index into the font data
55   // @param b the source for the bytes to be written
56   // @param offset offset in the byte array
57   // @param length the length of the bytes to be written
58   // @return the number of bytes actually written; -1 if the index is outside
59   //         the FontData's range
60   virtual int32_t WriteBytes(int32_t index,
61                              byte_t* b,
62                              int32_t offset,
63                              int32_t length);
64 
65   // Write the bytes from the array.
66   // @param index index into the font data
67   // @param b the source for the bytes to be written
68   // @return the number of bytes actually written; -1 if the index is outside
69   //         the FontData's range
70   virtual int32_t WriteBytes(int32_t index, ByteVector* b);
71 
72   // Write the bytes from the array and pad if necessary.
73   // Write to the length given using the byte array provided and if there are
74   // not enough bytes in the array then pad to the requested length using the
75   // pad byte specified.
76   // @param index index into the font data
77   // @param b the source for the bytes to be written
78   // @param offset offset in the byte array
79   // @param length the length of the bytes to be written
80   // @param pad the padding byte to be used if necessary
81   // @return the number of bytes actually written
82   virtual int32_t WriteBytesPad(int32_t index,
83                                 ByteVector* b,
84                                 int32_t offset,
85                                 int32_t length,
86                                 byte_t pad);
87 
88   // Writes padding to the FontData. The padding byte written is 0x00.
89   // @param index index into the font data
90   // @param count the number of pad bytes to write
91   // @return the number of pad bytes written
92   virtual int32_t WritePadding(int32_t index, int32_t count);
93 
94   // Writes padding to the FontData.
95   // @param index index into the font data
96   // @param count the number of pad bytes to write
97   // @param pad the byte value to use as padding
98   // @return the number of pad bytes written
99   virtual int32_t WritePadding(int32_t index, int32_t count, byte_t pad);
100 
101   // Write the CHAR at the given index.
102   // @param index index into the font data
103   // @param c the CHAR
104   // @return the number of bytes actually written
105   // @throws IndexOutOfBoundsException if index is outside the FontData's range
106   virtual int32_t WriteChar(int32_t index, byte_t c);
107 
108   // Write the USHORT at the given index.
109   // @param index index into the font data
110   // @param us the USHORT
111   // @return the number of bytes actually written
112   // @throws IndexOutOfBoundsException if index is outside the FontData's range
113   virtual int32_t WriteUShort(int32_t index, int32_t us);
114 
115   // Write the USHORT at the given index in little endian format.
116   // @param index index into the font data
117   // @param us the USHORT
118   // @return the number of bytes actually written
119   // @throws IndexOutOfBoundsException if index is outside the FontData's range
120   virtual int32_t WriteUShortLE(int32_t index, int32_t us);
121 
122   // Write the SHORT at the given index.
123   // @param index index into the font data
124   // @param s the SHORT
125   // @return the number of bytes actually written
126   // @throws IndexOutOfBoundsException if index is outside the FontData's range
127   virtual int32_t WriteShort(int32_t index, int32_t s);
128 
129   // Write the UINT24 at the given index.
130   // @param index index into the font data
131   // @param ui the UINT24
132   // @return the number of bytes actually written
133   // @throws IndexOutOfBoundsException if index is outside the FontData's range
134   virtual int32_t WriteUInt24(int32_t index, int32_t ui);
135 
136   // Write the ULONG at the given index.
137   // @param index index into the font data
138   // @param ul the ULONG
139   // @return the number of bytes actually written
140   // @throws IndexOutOfBoundsException if index is outside the FontData's range
141   virtual int32_t WriteULong(int32_t index, int64_t ul);
142 
143   // Write the ULONG at the given index in little endian format.
144   // @param index index into the font data
145   // @param ul the ULONG
146   // @return the number of bytes actually written
147   // @throws IndexOutOfBoundsException if index is outside the FontData's range
148   virtual int32_t WriteULongLE(int32_t index, int64_t ul);
149 
150   // Write the LONG at the given index.
151   // @param index index into the font data
152   // @param l the LONG
153   // @return the number of bytes actually written
154   // @throws IndexOutOfBoundsException if index is outside the FontData's range
155   virtual int32_t WriteLong(int32_t index, int64_t l);
156 
157   // Write the Fixed at the given index.
158   // @param index index into the font data
159   // @param f the Fixed
160   // @return the number of bytes actually written
161   // @throws IndexOutOfBoundsException if index is outside the FontData's range
162   virtual int32_t WriteFixed(int32_t index, int32_t f);
163 
164   // Write the LONGDATETIME at the given index.
165   // @param index index into the font data
166   // @param date the LONGDATETIME
167   // @return the number of bytes actually written
168   // @throws IndexOutOfBoundsException if index is outside the FontData's range
169   virtual int32_t WriteDateTime(int32_t index, int64_t date);
170 
171   // Copy from the InputStream into this FontData.
172   // @param is the source
173   // @param length the number of bytes to copy
174   // @throws IOException
175   virtual void CopyFrom(InputStream* is, int32_t length);
176 
177   // Copy everything from the InputStream into this FontData.
178   // @param is the source
179   // @throws IOException
180   virtual void CopyFrom(InputStream* is);
181 
182   // Makes a slice of this FontData. The returned slice will share the data with
183   // the original FontData.
184   // @param offset the start of the slice
185   // @param length the number of bytes in the slice
186   // @return a slice of the original FontData
187   virtual CALLER_ATTACH FontData* Slice(int32_t offset, int32_t length);
188 
189   // Makes a bottom bound only slice of this array. The returned slice will
190   // share the data with the original FontData.
191   // @param offset the start of the slice
192   // @return a slice of the original FontData
193   virtual CALLER_ATTACH FontData* Slice(int32_t offset);
194 
195  private:
196   // Constructor with a lower bound.
197   // @param data other WritableFontData object to share data with
198   // @param offset offset from the other WritableFontData's data
199   WritableFontData(WritableFontData* data, int32_t offset);
200 
201   // Constructor with lower bound and a length bound.
202   // @param data other WritableFontData object to share data with
203   // @param offset offset from the other WritableFontData's data
204   // @param length length of other WritableFontData's data to use
205   WritableFontData(WritableFontData* data, int32_t offset, int32_t length);
206 };
207 typedef Ptr<WritableFontData> WritableFontDataPtr;
208 
209 }  // namespace sfntly
210 
211 #endif  // SFNTLY_CPP_SRC_SFNTLY_DATA_WRITABLE_FONT_DATA_H_
212