• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
Size() const27 int32_t FontData::Size() const {
28   return std::min<int32_t>(array_->Size() - bound_offset_, bound_length_);
29 }
30 
Bound(int32_t offset,int32_t length)31 void FontData::Bound(int32_t offset, int32_t length) {
32   // Inputs should not be negative.
33   CHECK(offset >= 0);
34   CHECK(length >= 0);
35 
36   // Check to make sure |bound_offset_| will not overflow.
37   CHECK(bound_offset_ <= std::numeric_limits<int32_t>::max() - offset);
38   const int32_t new_offset = bound_offset_ + offset;
39 
40   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     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     CHECK(new_offset <= array_->Size() - length);
50   }
51 
52   bound_offset_ = new_offset;
53   bound_length_ = length;
54 }
55 
Length() const56 int32_t FontData::Length() const {
57   return std::min<int32_t>(array_->Length() - bound_offset_, bound_length_);
58 }
59 
FontData(ByteArray * ba)60 FontData::FontData(ByteArray* ba) {
61   Init(ba);
62 }
63 
FontData(FontData * data,int32_t offset,int32_t length)64 FontData::FontData(FontData* data, int32_t offset, int32_t length) {
65   Init(data->array_);
66   Bound(data->bound_offset_ + offset, length);
67 }
68 
FontData(FontData * data,int32_t offset)69 FontData::FontData(FontData* data, int32_t offset) {
70   Init(data->array_);
71   Bound(data->bound_offset_ + offset,
72         (data->bound_length_ == GROWABLE_SIZE)
73         ? GROWABLE_SIZE : data->bound_length_ - offset);
74 }
75 
~FontData()76 FontData::~FontData() {}
77 
Init(ByteArray * ba)78 void FontData::Init(ByteArray* ba) {
79   array_ = ba;
80   bound_offset_ = 0;
81   bound_length_ = GROWABLE_SIZE;
82 }
83 
BoundOffset(int32_t offset)84 int32_t FontData::BoundOffset(int32_t offset) {
85   return offset + bound_offset_;
86 }
87 
BoundLength(int32_t offset,int32_t length)88 int32_t FontData::BoundLength(int32_t offset, int32_t length) {
89   return std::min<int32_t>(length, bound_length_ - offset);
90 }
91 
92 }  // namespace sfntly
93