writable_font_data.h revision 584bf6606b53bda8bf0810e7c0ad57e24cacb4f1
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 22namespace sfntly { 23 24// Writable font data wrapper. Supports writing of data primitives in the 25// TrueType / OpenType spec. 26class 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 CHAR at the given index. 73 // @param index index into the font data 74 // @param c the CHAR 75 // @return the number of bytes actually written 76 // @throws IndexOutOfBoundsException if index is outside the FontData's range 77 virtual int32_t WriteChar(int32_t index, byte_t c); 78 79 // Write the USHORT at the given index. 80 // @param index index into the font data 81 // @param us the USHORT 82 // @return the number of bytes actually written 83 // @throws IndexOutOfBoundsException if index is outside the FontData's range 84 virtual int32_t WriteUShort(int32_t index, int32_t us); 85 86 // Write the USHORT at the given index in little endian format. 87 // @param index index into the font data 88 // @param us the USHORT 89 // @return the number of bytes actually written 90 // @throws IndexOutOfBoundsException if index is outside the FontData's range 91 virtual int32_t WriteUShortLE(int32_t index, int32_t us); 92 93 // Write the SHORT at the given index. 94 // @param index index into the font data 95 // @param s the SHORT 96 // @return the number of bytes actually written 97 // @throws IndexOutOfBoundsException if index is outside the FontData's range 98 virtual int32_t WriteShort(int32_t index, int32_t s); 99 100 // Write the UINT24 at the given index. 101 // @param index index into the font data 102 // @param ui the UINT24 103 // @return the number of bytes actually written 104 // @throws IndexOutOfBoundsException if index is outside the FontData's range 105 virtual int32_t WriteUInt24(int32_t index, int32_t ui); 106 107 // Write the ULONG at the given index. 108 // @param index index into the font data 109 // @param ul the ULONG 110 // @return the number of bytes actually written 111 // @throws IndexOutOfBoundsException if index is outside the FontData's range 112 virtual int32_t WriteULong(int32_t index, int64_t ul); 113 114 // Write the ULONG at the given index in little endian format. 115 // @param index index into the font data 116 // @param ul the ULONG 117 // @return the number of bytes actually written 118 // @throws IndexOutOfBoundsException if index is outside the FontData's range 119 virtual int32_t WriteULongLE(int32_t index, int64_t ul); 120 121 // Write the LONG at the given index. 122 // @param index index into the font data 123 // @param l the LONG 124 // @return the number of bytes actually written 125 // @throws IndexOutOfBoundsException if index is outside the FontData's range 126 virtual int32_t WriteLong(int32_t index, int64_t l); 127 128 // Write the Fixed at the given index. 129 // @param index index into the font data 130 // @param f the Fixed 131 // @return the number of bytes actually written 132 // @throws IndexOutOfBoundsException if index is outside the FontData's range 133 virtual int32_t WriteFixed(int32_t index, int32_t f); 134 135 // Write the LONGDATETIME at the given index. 136 // @param index index into the font data 137 // @param date the LONGDATETIME 138 // @return the number of bytes actually written 139 // @throws IndexOutOfBoundsException if index is outside the FontData's range 140 virtual int32_t WriteDateTime(int32_t index, int64_t date); 141 142 // Copy from the InputStream into this FontData. 143 // @param is the source 144 // @param length the number of bytes to copy 145 // @throws IOException 146 virtual void CopyFrom(InputStream* is, int32_t length); 147 148 // Copy everything from the InputStream into this FontData. 149 // @param is the source 150 // @throws IOException 151 virtual void CopyFrom(InputStream* is); 152 153 // Makes a slice of this FontData. The returned slice will share the data with 154 // the original FontData. 155 // @param offset the start of the slice 156 // @param length the number of bytes in the slice 157 // @return a slice of the original FontData 158 virtual CALLER_ATTACH FontData* Slice(int32_t offset, int32_t length); 159 160 // Makes a bottom bound only slice of this array. The returned slice will 161 // share the data with the original FontData. 162 // @param offset the start of the slice 163 // @return a slice of the original FontData 164 virtual CALLER_ATTACH FontData* Slice(int32_t offset); 165 166 private: 167 // Constructor with a lower bound. 168 // @param data other WritableFontData object to share data with 169 // @param offset offset from the other WritableFontData's data 170 WritableFontData(WritableFontData* data, int32_t offset); 171 172 // Constructor with lower bound and a length bound. 173 // @param data other WritableFontData object to share data with 174 // @param offset offset from the other WritableFontData's data 175 // @param length length of other WritableFontData's data to use 176 WritableFontData(WritableFontData* data, int32_t offset, int32_t length); 177}; 178typedef Ptr<WritableFontData> WritableFontDataPtr; 179 180} // namespace sfntly 181 182#endif // SFNTLY_CPP_SRC_SFNTLY_DATA_WRITABLE_FONT_DATA_H_ 183