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_DATA_READABLE_FONT_DATA_H_
185af34fd773f8cfee82321393504f558ddf67c628arthurhsu@google.com#define SFNTLY_CPP_SRC_SFNTLY_DATA_READABLE_FONT_DATA_H_
19464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
20464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com#include "sfntly/data/font_data.h"
2109f3dda615ba59d0b686e8c36f1e8426b9235746arthurhsu@google.com#include "sfntly/port/lock.h"
22464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
23464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.comnamespace sfntly {
24464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
25464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.comclass WritableFontData;
26464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.comclass OutputStream;
2732a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com
2832a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com// Writable font data wrapper. Supports reading of data primitives in the
2932a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com// TrueType / OpenType spec.
3032a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com// The data types used are as listed:
3132a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com// BYTE       8-bit unsigned integer.
3232a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com// CHAR       8-bit signed integer.
3332a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com// USHORT     16-bit unsigned integer.
3432a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com// SHORT      16-bit signed integer.
3532a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com// UINT24     24-bit unsigned integer.
3632a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com// ULONG      32-bit unsigned integer.
3732a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com// LONG       32-bit signed integer.
3832a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com// Fixed      32-bit signed fixed-point number (16.16)
3932a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com// FUNIT      Smallest measurable distance in the em space.
4032a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com// FWORD      16-bit signed integer (SHORT) that describes a quantity in FUnits.
4132a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com// UFWORD     16-bit unsigned integer (USHORT) that describes a quantity in
4232a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com//            FUnits.
4332a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com// F2DOT14    16-bit signed fixed number with the low 14 bits of fraction (2.14)
4432a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com// LONGDATETIME  Date represented in number of seconds since 12:00 midnight,
4532a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com//               January 1, 1904. The value is represented as a signed 64-bit
4632a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com//               integer.
4732a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com
48464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.comclass ReadableFontData : public FontData,
49464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com                         public RefCounted<ReadableFontData> {
50464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com public:
51464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  explicit ReadableFontData(ByteArray* array);
52464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  virtual ~ReadableFontData();
53464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
5432a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  static CALLER_ATTACH ReadableFontData* CreateReadableFontData(ByteVector* b);
5532a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com
5632a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // Gets a computed checksum for the data. This checksum uses the OpenType spec
57464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // calculation. Every ULong value (32 bit unsigned) in the data is summed and
58464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // the resulting value is truncated to 32 bits. If the data length in bytes is
59464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // not an integral multiple of 4 then any remaining bytes are treated as the
60464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // start of a 4 byte sequence whose remaining bytes are zero.
61464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // @return the checksum
62246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  int64_t Checksum();
63464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
64464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // Sets the bounds to use for computing the checksum. These bounds are in
65464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // begin and end pairs. If an odd number is given then the final range is
66464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // assumed to extend to the end of the data. The lengths of each range must be
67464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // a multiple of 4.
68464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // @param ranges the range bounds to use for the checksum
69246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  void SetCheckSumRanges(const IntegerList& ranges);
70464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
7132a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // Read the UBYTE at the given index.
7232a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @param index index into the font data
7332a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @return the UBYTE; -1 if outside the bounds of the font data
7432a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @throws IndexOutOfBoundsException if index is outside the FontData's range
75246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  virtual int32_t ReadUByte(int32_t index);
7632a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com
7732a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // Read the BYTE at the given index.
7832a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @param index index into the font data
7932a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @return the BYTE
8032a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @throws IndexOutOfBoundsException if index is outside the FontData's range
81246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  virtual int32_t ReadByte(int32_t index);
8232a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com
8332a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // Read the bytes at the given index into the array.
8432a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @param index index into the font data
8532a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @param b the destination for the bytes read
8632a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @param offset offset in the byte array to place the bytes
8732a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @param length the length of bytes to read
8832a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @return the number of bytes actually read; -1 if the index is outside the
8932a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  //         bounds of the font data
9032a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  virtual int32_t ReadBytes(int32_t index,
9132a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com                            byte_t* b,
9232a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com                            int32_t offset,
93464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com                            int32_t length);
9432a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com
9532a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // Read the CHAR at the given index.
9632a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @param index index into the font data
9732a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @return the CHAR
9832a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @throws IndexOutOfBoundsException if index is outside the FontData's range
99246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  virtual int32_t ReadChar(int32_t index);
10032a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com
10132a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // Read the USHORT at the given index.
10232a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @param index index into the font data
10332a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @return the USHORT
10432a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @throws IndexOutOfBoundsException if index is outside the FontData's range
105246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  virtual int32_t ReadUShort(int32_t index);
10632a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com
10732a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // Read the SHORT at the given index.
10832a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @param index index into the font data
10932a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @return the SHORT
11032a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @throws IndexOutOfBoundsException if index is outside the FontData's range
111246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  virtual int32_t ReadShort(int32_t index);
11232a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com
11332a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // Read the UINT24 at the given index.
11432a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @param index index into the font data
11532a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @return the UINT24
11632a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @throws IndexOutOfBoundsException if index is outside the FontData's range
117246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  virtual int32_t ReadUInt24(int32_t index);
11832a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com
11932a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // Read the ULONG at the given index.
12032a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @param index index into the font data
12132a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @return the ULONG
12232a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @throws IndexOutOfBoundsException if index is outside the FontData's range
123246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  virtual int64_t ReadULong(int32_t index);
12432a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com
12532a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // Read the ULONG at the given index as int32_t.
12632a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @param index index into the font data
12732a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @return the ULONG
12832a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @throws IndexOutOfBoundsException if index is outside the FontData's range
129246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  virtual int32_t ReadULongAsInt(int32_t index);
13032a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com
131333edd91cb32d6acfd0307ba2ae8f60baed75ff4arthurhsu@google.com  // Read the ULONG at the given index, little-endian variant
132333edd91cb32d6acfd0307ba2ae8f60baed75ff4arthurhsu@google.com  // @param index index into the font data
133333edd91cb32d6acfd0307ba2ae8f60baed75ff4arthurhsu@google.com  // @return the ULONG
134333edd91cb32d6acfd0307ba2ae8f60baed75ff4arthurhsu@google.com  // @throws IndexOutOfBoundsException if index is outside the FontData's range
135333edd91cb32d6acfd0307ba2ae8f60baed75ff4arthurhsu@google.com  virtual int64_t ReadULongLE(int32_t index);
136333edd91cb32d6acfd0307ba2ae8f60baed75ff4arthurhsu@google.com
13732a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // Read the LONG at the given index.
13832a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @param index index into the font data
13932a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @return the LONG
14032a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @throws IndexOutOfBoundsException if index is outside the FontData's range
141246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  virtual int32_t ReadLong(int32_t index);
14232a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com
14332a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // Read the Fixed at the given index.
14432a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @param index index into the font data
14532a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @return the Fixed
14632a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @throws IndexOutOfBoundsException if index is outside the FontData's range
147246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  virtual int32_t ReadFixed(int32_t index);
14832a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com
14932a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // Read the LONGDATETIME at the given index.
15032a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @param index index into the font data
15132a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @return the LONGDATETIME
15232a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @throws IndexOutOfBoundsException if index is outside the FontData's range
153246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  virtual int64_t ReadDateTimeAsLong(int32_t index);
154246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
15532a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // Read the FWORD at the given index.
15632a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @param index index into the font data
15732a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @return the FWORD
15832a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @throws IndexOutOfBoundsException if index is outside the FontData's range
159246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  virtual int32_t ReadFWord(int32_t index);
16032a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com
16132a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // Read the UFWORD at the given index.
16232a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @param index index into the font data
16332a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @return the UFWORD
16432a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @throws IndexOutOfBoundsException if index is outside the FontData's range
165246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  virtual int32_t ReadFUFWord(int32_t index);
166246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
16732a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // Note: Not ported because they just throw UnsupportedOperationException()
16832a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  //       in Java.
16932a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  /*
17032a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  virtual int32_t ReadFUnit(int32_t index);
17132a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  virtual int64_t ReadF2Dot14(int32_t index);
17232a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  */
17332a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com
17432a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // Copy the FontData to an OutputStream.
17532a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @param os the destination
17632a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @return number of bytes copied
17732a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @throws IOException
178246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  virtual int32_t CopyTo(OutputStream* os);
17932a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com
18032a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // Copy the FontData to a WritableFontData.
18132a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @param wfd the destination
18232a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @return number of bytes copied
18332a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @throws IOException
184246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  virtual int32_t CopyTo(WritableFontData* wfd);
18532a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com
18632a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // Make gcc -Woverloaded-virtual happy.
187246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  virtual int32_t CopyTo(ByteArray* ba);
188464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
189760e6005f25f8d45d413d286ab5a38263ece63addfilimon@google.com  // Search for the key value in the range tables provided.
1906b8e073e978eed96605da6f92d6db740a39864baarthurhsu@google.com  // The search looks through the start-end pairs looking for the key value. It
191760e6005f25f8d45d413d286ab5a38263ece63addfilimon@google.com  // is assumed that the start-end pairs are both represented by UShort values,
192760e6005f25f8d45d413d286ab5a38263ece63addfilimon@google.com  // ranges do not overlap, and are monotonically increasing.
193760e6005f25f8d45d413d286ab5a38263ece63addfilimon@google.com  // @param startIndex the position to read the first start value from
194760e6005f25f8d45d413d286ab5a38263ece63addfilimon@google.com  // @param startOffset the offset between subsequent start values
195760e6005f25f8d45d413d286ab5a38263ece63addfilimon@google.com  // @param endIndex the position to read the first end value from
196760e6005f25f8d45d413d286ab5a38263ece63addfilimon@google.com  // @param endOffset the offset between subsequent end values
197760e6005f25f8d45d413d286ab5a38263ece63addfilimon@google.com  // @param length the number of start-end pairs
198760e6005f25f8d45d413d286ab5a38263ece63addfilimon@google.com  // @param key the value to search for
199760e6005f25f8d45d413d286ab5a38263ece63addfilimon@google.com  // @return the index of the start-end pairs in which the key was found; -1
200760e6005f25f8d45d413d286ab5a38263ece63addfilimon@google.com  //         otherwise
201760e6005f25f8d45d413d286ab5a38263ece63addfilimon@google.com  int32_t SearchUShort(int32_t start_index,
202760e6005f25f8d45d413d286ab5a38263ece63addfilimon@google.com                       int32_t start_offset,
203760e6005f25f8d45d413d286ab5a38263ece63addfilimon@google.com                       int32_t end_index,
204760e6005f25f8d45d413d286ab5a38263ece63addfilimon@google.com                       int32_t end_offset,
205760e6005f25f8d45d413d286ab5a38263ece63addfilimon@google.com                       int32_t length,
206760e6005f25f8d45d413d286ab5a38263ece63addfilimon@google.com                       int32_t key);
207760e6005f25f8d45d413d286ab5a38263ece63addfilimon@google.com
2086b8e073e978eed96605da6f92d6db740a39864baarthurhsu@google.com  // Search for the key value in the table provided.
2096b8e073e978eed96605da6f92d6db740a39864baarthurhsu@google.com  // The search looks through the values looking for the key value. It is
2106b8e073e978eed96605da6f92d6db740a39864baarthurhsu@google.com  // assumed that the are represented by UShort values and are monotonically
2116b8e073e978eed96605da6f92d6db740a39864baarthurhsu@google.com  // increasing.
2126b8e073e978eed96605da6f92d6db740a39864baarthurhsu@google.com  // @param startIndex the position to read the first start value from
2136b8e073e978eed96605da6f92d6db740a39864baarthurhsu@google.com  // @param startOffset the offset between subsequent start values
2146b8e073e978eed96605da6f92d6db740a39864baarthurhsu@google.com  // @param length the number of start-end pairs
2156b8e073e978eed96605da6f92d6db740a39864baarthurhsu@google.com  // @param key the value to search for
2166b8e073e978eed96605da6f92d6db740a39864baarthurhsu@google.com  // @return the index of the start-end pairs in which the key was found; -1
2176b8e073e978eed96605da6f92d6db740a39864baarthurhsu@google.com  //         otherwise
2186b8e073e978eed96605da6f92d6db740a39864baarthurhsu@google.com  int32_t SearchUShort(int32_t start_index,
2196b8e073e978eed96605da6f92d6db740a39864baarthurhsu@google.com                       int32_t start_offset,
2206b8e073e978eed96605da6f92d6db740a39864baarthurhsu@google.com                       int32_t length,
2216b8e073e978eed96605da6f92d6db740a39864baarthurhsu@google.com                       int32_t key);
2226b8e073e978eed96605da6f92d6db740a39864baarthurhsu@google.com
223760e6005f25f8d45d413d286ab5a38263ece63addfilimon@google.com  // Search for the key value in the range tables provided.
224760e6005f25f8d45d413d286ab5a38263ece63addfilimon@google.com  // The search looks through the start-end pairs looking for the key value. It
225760e6005f25f8d45d413d286ab5a38263ece63addfilimon@google.com  // is assumed that the start-end pairs are both represented by ULong values
226760e6005f25f8d45d413d286ab5a38263ece63addfilimon@google.com  // that can be represented within 31 bits, ranges do not overlap, and are
227760e6005f25f8d45d413d286ab5a38263ece63addfilimon@google.com  // monotonically increasing.
228760e6005f25f8d45d413d286ab5a38263ece63addfilimon@google.com  // @param startIndex the position to read the first start value from
229760e6005f25f8d45d413d286ab5a38263ece63addfilimon@google.com  // @param startOffset the offset between subsequent start values
230760e6005f25f8d45d413d286ab5a38263ece63addfilimon@google.com  // @param endIndex the position to read the first end value from
231760e6005f25f8d45d413d286ab5a38263ece63addfilimon@google.com  // @param endOffset the offset between subsequent end values
232760e6005f25f8d45d413d286ab5a38263ece63addfilimon@google.com  // @param length the number of start-end pairs
233760e6005f25f8d45d413d286ab5a38263ece63addfilimon@google.com  // @param key the value to search for
234760e6005f25f8d45d413d286ab5a38263ece63addfilimon@google.com  // @return the index of the start-end pairs in which the key was found; -1
235760e6005f25f8d45d413d286ab5a38263ece63addfilimon@google.com  //         otherwise
236760e6005f25f8d45d413d286ab5a38263ece63addfilimon@google.com  int32_t SearchULong(int32_t start_index,
237760e6005f25f8d45d413d286ab5a38263ece63addfilimon@google.com                      int32_t start_offset,
238760e6005f25f8d45d413d286ab5a38263ece63addfilimon@google.com                      int32_t end_index,
239760e6005f25f8d45d413d286ab5a38263ece63addfilimon@google.com                      int32_t end_offset,
240760e6005f25f8d45d413d286ab5a38263ece63addfilimon@google.com                      int32_t length,
241760e6005f25f8d45d413d286ab5a38263ece63addfilimon@google.com                      int32_t key);
242760e6005f25f8d45d413d286ab5a38263ece63addfilimon@google.com
243760e6005f25f8d45d413d286ab5a38263ece63addfilimon@google.com
244760e6005f25f8d45d413d286ab5a38263ece63addfilimon@google.com  // TODO(arthurhsu): IMPLEMENT
245464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  /*
246760e6005f25f8d45d413d286ab5a38263ece63addfilimon@google.com  virtual int32_t ReadFUnit(int32_t index);
247760e6005f25f8d45d413d286ab5a38263ece63addfilimon@google.com  virtual int64_t ReadF2Dot14(int32_t index);
248760e6005f25f8d45d413d286ab5a38263ece63addfilimon@google.com  virtual int64_t ReadLongDateTime(int32_t index);
249464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  */
250464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
251464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // Makes a slice of this FontData. The returned slice will share the data with
252464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // the original FontData.
253464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // @param offset the start of the slice
254464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // @param length the number of bytes in the slice
255464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // @return a slice of the original FontData
25632a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // Note: C++ polymorphism requires return type to be consistent
257246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  virtual CALLER_ATTACH FontData* Slice(int32_t offset, int32_t length);
258464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
259464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // Makes a bottom bound only slice of this array. The returned slice will
260464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // share the data with the original FontData.
261464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // @param offset the start of the slice
262464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  // @return a slice of the original FontData
26332a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // Note: C++ polymorphism requires return type to be consistent
264246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  virtual CALLER_ATTACH FontData* Slice(int32_t offset);
265246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
26632a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // Not Ported: toString()
26732a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com
268246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com protected:
269246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  // Constructor. Creates a bounded wrapper of another ReadableFontData from the
270246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  // given offset until the end of the original ReadableFontData.
271246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  // @param data data to wrap
272246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  // @param offset the start of this data's view of the original data
273246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  ReadableFontData(ReadableFontData* data, int32_t offset);
274246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
275246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  // Constructor. Creates a bounded wrapper of another ReadableFontData from the
276246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  // given offset until the end of the original ReadableFontData.
277246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  // @param data data to wrap
278246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  // @param offset the start of this data's view of the original data
27932a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @param length the length of the other FontData to use
280246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  ReadableFontData(ReadableFontData* data, int32_t offset, int32_t length);
281464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
282464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com private:
28332a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // Compute the checksum for the font data using any ranges set for the
28432a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // calculation.
285246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  void ComputeChecksum();
28632a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com
28732a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // Do the actual computation of the checksum for a range using the
28832a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // TrueType/OpenType checksum algorithm. The range used is from the low bound
28932a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // to the high bound in steps of four bytes. If any of the bytes within that 4
29032a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // byte segment are not readable then it will considered a zero for
29132a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // calculation.
29232a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // Only called from within a synchronized method so it does not need to be
29332a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // synchronized itself.
29432a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @param lowBound first position to start a 4 byte segment on
29532a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @param highBound last possible position to start a 4 byte segment on
29632a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @return the checksum for the total range
297246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  int64_t ComputeCheckSum(int32_t low_bound, int32_t high_bound);
298246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
29909f3dda615ba59d0b686e8c36f1e8426b9235746arthurhsu@google.com  Lock checksum_lock_;
30009f3dda615ba59d0b686e8c36f1e8426b9235746arthurhsu@google.com  bool checksum_set_;
301464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  int64_t checksum_;
302464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  IntegerList checksum_range_;
303464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com};
304464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.comtypedef Ptr<ReadableFontData> ReadableFontDataPtr;
305464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
306464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com}  // namespace sfntly
307464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
3085af34fd773f8cfee82321393504f558ddf67c628arthurhsu@google.com#endif  // SFNTLY_CPP_SRC_SFNTLY_DATA_READABLE_FONT_DATA_H_
309