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 <limits.h>
18#include <algorithm>
19#include <functional>
20
21#include "sfntly/data/font_data.h"
22
23namespace sfntly {
24
25int32_t FontData::Size() const {
26  return std::min<int32_t>(array_->Size() - bound_offset_, bound_length_);
27}
28
29bool FontData::Bound(int32_t offset, int32_t length) {
30  if (offset + length > Size() || offset < 0 || length < 0)
31    return false;
32
33  bound_offset_ += offset;
34  bound_length_ = length;
35  return true;
36}
37
38bool FontData::Bound(int32_t offset) {
39if (offset > Size() || offset < 0)
40    return false;
41
42  bound_offset_ += offset;
43  return true;
44}
45
46int32_t FontData::Length() const {
47  return std::min<int32_t>(array_->Length() - bound_offset_, bound_length_);
48}
49
50FontData::FontData(ByteArray* ba) {
51  Init(ba);
52}
53
54FontData::FontData(FontData* data, int32_t offset, int32_t length) {
55  Init(data->array_);
56  Bound(data->bound_offset_ + offset, length);
57}
58
59FontData::FontData(FontData* data, int32_t offset) {
60  Init(data->array_);
61  Bound(data->bound_offset_ + offset,
62        (data->bound_length_ == GROWABLE_SIZE)
63            ? GROWABLE_SIZE : data->bound_length_ - offset);
64}
65
66FontData::~FontData() {}
67
68void FontData::Init(ByteArray* ba) {
69  array_ = ba;
70  bound_offset_ = 0;
71  bound_length_ = GROWABLE_SIZE;
72}
73
74int32_t FontData::BoundOffset(int32_t offset) {
75  return offset + bound_offset_;
76}
77
78int32_t FontData::BoundLength(int32_t offset, int32_t length) {
79  return std::min<int32_t>(length, bound_length_ - offset);
80}
81
82}  // namespace sfntly
83