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