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 #include "sfntly/data/font_input_stream.h"
18
19 #include <algorithm>
20
21 namespace sfntly {
22
FontInputStream(InputStream * is)23 FontInputStream::FontInputStream(InputStream* is)
24 : stream_(is), position_(0), length_(0), bounded_(false) {
25 }
26
FontInputStream(InputStream * is,size_t length)27 FontInputStream::FontInputStream(InputStream* is, size_t length)
28 : stream_(is), position_(0), length_(length), bounded_(true) {
29 }
30
~FontInputStream()31 FontInputStream::~FontInputStream() {
32 // Do not close here, underlying InputStream will close themselves.
33 }
34
Available()35 int32_t FontInputStream::Available() {
36 if (stream_) {
37 return stream_->Available();
38 }
39 return 0;
40 }
41
Close()42 void FontInputStream::Close() {
43 if (stream_) {
44 stream_->Close();
45 }
46 }
47
Mark(int32_t readlimit)48 void FontInputStream::Mark(int32_t readlimit) {
49 if (stream_) {
50 stream_->Mark(readlimit);
51 }
52 }
53
MarkSupported()54 bool FontInputStream::MarkSupported() {
55 if (stream_) {
56 return stream_->MarkSupported();
57 }
58 return false;
59 }
60
Reset()61 void FontInputStream::Reset() {
62 if (stream_) {
63 stream_->Reset();
64 }
65 }
66
Read()67 int32_t FontInputStream::Read() {
68 if (!stream_ || (bounded_ && position_ >= length_)) {
69 return -1;
70 }
71 int32_t b = stream_->Read();
72 if (b >= 0) {
73 position_++;
74 }
75 return b;
76 }
77
Read(ByteVector * b,int32_t offset,int32_t length)78 int32_t FontInputStream::Read(ByteVector* b, int32_t offset, int32_t length) {
79 if (!stream_ || offset < 0 || length < 0 ||
80 (bounded_ && position_ >= length_)) {
81 return -1;
82 }
83 int32_t bytes_to_read =
84 bounded_ ? std::min<int32_t>(length, (int32_t)(length_ - position_)) :
85 length;
86 int32_t bytes_read = stream_->Read(b, offset, bytes_to_read);
87 position_ += bytes_read;
88 return bytes_read;
89 }
90
Read(ByteVector * b)91 int32_t FontInputStream::Read(ByteVector* b) {
92 return Read(b, 0, b->size());
93 }
94
ReadChar()95 int32_t FontInputStream::ReadChar() {
96 return Read();
97 }
98
ReadUShort()99 int32_t FontInputStream::ReadUShort() {
100 return 0xffff & (Read() << 8 | Read());
101 }
102
ReadShort()103 int32_t FontInputStream::ReadShort() {
104 return ((Read() << 8 | Read()) << 16) >> 16;
105 }
106
ReadUInt24()107 int32_t FontInputStream::ReadUInt24() {
108 return 0xffffff & (Read() << 16 | Read() << 8 | Read());
109 }
110
ReadULong()111 int64_t FontInputStream::ReadULong() {
112 return 0xffffffffL & ReadLong();
113 }
114
ReadULongAsInt()115 int32_t FontInputStream::ReadULongAsInt() {
116 int64_t ulong = ReadULong();
117 return ((int32_t)ulong) & ~0x80000000;
118 }
119
ReadLong()120 int32_t FontInputStream::ReadLong() {
121 return Read() << 24 | Read() << 16 | Read() << 8 | Read();
122 }
123
ReadFixed()124 int32_t FontInputStream::ReadFixed() {
125 return ReadLong();
126 }
127
ReadDateTimeAsLong()128 int64_t FontInputStream::ReadDateTimeAsLong() {
129 return (int64_t)ReadULong() << 32 | ReadULong();
130 }
131
Skip(int64_t n)132 int64_t FontInputStream::Skip(int64_t n) {
133 if (stream_) {
134 int64_t skipped = stream_->Skip(n);
135 position_ += skipped;
136 return skipped;
137 }
138 return 0;
139 }
140
141 } // namespace sfntly
142