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_data.h"
18 :
19 : #include <algorithm>
20 : #include <functional>
21 : #include <limits>
22 :
23 : #include "sfntly/port/logging.h"
24 :
25 : namespace sfntly {
26 :
27 0 : int32_t FontData::Size() const {
28 0 : return std::min<int32_t>(array_->Size() - bound_offset_, bound_length_);
29 : }
30 :
31 0 : void FontData::Bound(int32_t offset, int32_t length) {
32 : // Inputs should not be negative.
33 0 : CHECK(offset >= 0);
34 0 : CHECK(length >= 0);
35 :
36 : // Check to make sure |bound_offset_| will not overflow.
37 0 : CHECK(bound_offset_ <= std::numeric_limits<int32_t>::max() - offset);
38 0 : const int32_t new_offset = bound_offset_ + offset;
39 :
40 0 : if (length == GROWABLE_SIZE) {
41 : // When |length| has the special value of GROWABLE_SIZE, it means the size
42 : // should not have any artificial limits, thus it is just the underlying
43 : // |array_|'s size. Just make sure |new_offset| is still within bounds.
44 0 : CHECK(new_offset <= array_->Size());
45 : } else {
46 : // When |length| has any other value, |new_offset| + |length| points to the
47 : // end of the array. Make sure that is within bounds, but use subtraction to
48 : // avoid an integer overflow.
49 0 : CHECK(new_offset <= array_->Size() - length);
50 : }
51 :
52 0 : bound_offset_ = new_offset;
53 0 : bound_length_ = length;
54 0 : }
55 :
56 0 : int32_t FontData::Length() const {
57 0 : return std::min<int32_t>(array_->Length() - bound_offset_, bound_length_);
58 : }
59 :
60 0 : FontData::FontData(ByteArray* ba) {
61 0 : Init(ba);
62 0 : }
63 :
64 0 : FontData::FontData(FontData* data, int32_t offset, int32_t length) {
65 0 : Init(data->array_);
66 0 : Bound(data->bound_offset_ + offset, length);
67 0 : }
68 :
69 0 : FontData::FontData(FontData* data, int32_t offset) {
70 0 : Init(data->array_);
71 0 : Bound(data->bound_offset_ + offset,
72 0 : (data->bound_length_ == GROWABLE_SIZE)
73 0 : ? GROWABLE_SIZE : data->bound_length_ - offset);
74 0 : }
75 :
76 0 : FontData::~FontData() {}
77 :
78 0 : void FontData::Init(ByteArray* ba) {
79 0 : array_ = ba;
80 0 : bound_offset_ = 0;
81 0 : bound_length_ = GROWABLE_SIZE;
82 0 : }
83 :
84 0 : int32_t FontData::BoundOffset(int32_t offset) {
85 0 : return offset + bound_offset_;
86 : }
87 :
88 0 : int32_t FontData::BoundLength(int32_t offset, int32_t length) {
89 0 : return std::min<int32_t>(length, bound_length_ - offset);
90 : }
91 :
92 : } // namespace sfntly
|