1464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com/*
2464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com * Copyright 2011 Google Inc. All Rights Reserved.
3464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com *
4464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com * Licensed under the Apache License, Version 2.0 (the "License");
5464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com * you may not use this file except in compliance with the License.
6464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com * You may obtain a copy of the License at
7464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com *
8464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com *      http://www.apache.org/licenses/LICENSE-2.0
9464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com *
10464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com * Unless required by applicable law or agreed to in writing, software
11464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com * distributed under the License is distributed on an "AS IS" BASIS,
12464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com * See the License for the specific language governing permissions and
14464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com * limitations under the License.
15464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com */
16464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
17464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com#include <limits.h>
18464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com#include <algorithm>
19464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com#include <functional>
20464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
21464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com#include "sfntly/data/font_data.h"
22464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
23464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.comnamespace sfntly {
24464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
25246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.comint32_t FontData::Size() const {
26246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  return std::min<int32_t>(array_->Size() - bound_offset_, bound_length_);
27464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com}
28464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
29246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.combool FontData::Bound(int32_t offset, int32_t length) {
30246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  if (offset + length > Size() || offset < 0 || length < 0)
31464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    return false;
32464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
33464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  bound_offset_ += offset;
34464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  bound_length_ = length;
35464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  return true;
36464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com}
37464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
38246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.combool FontData::Bound(int32_t offset) {
39246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.comif (offset > Size() || offset < 0)
40464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    return false;
41464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
42464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  bound_offset_ += offset;
43464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  return true;
44464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com}
45464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
46246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.comint32_t FontData::Length() const {
47246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  return std::min<int32_t>(array_->Length() - bound_offset_, bound_length_);
48464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com}
49464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
50246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.comFontData::FontData(ByteArray* ba) {
51246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  Init(ba);
52246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com}
53246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
54246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.comFontData::FontData(FontData* data, int32_t offset, int32_t length) {
55246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  Init(data->array_);
56246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  Bound(data->bound_offset_ + offset, length);
57246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com}
58246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
59246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.comFontData::FontData(FontData* data, int32_t offset) {
60246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  Init(data->array_);
61333edd91cb32d6acfd0307ba2ae8f60baed75ff4arthurhsu@google.com  Bound(data->bound_offset_ + offset,
62333edd91cb32d6acfd0307ba2ae8f60baed75ff4arthurhsu@google.com        (data->bound_length_ == GROWABLE_SIZE)
63333edd91cb32d6acfd0307ba2ae8f60baed75ff4arthurhsu@google.com            ? GROWABLE_SIZE : data->bound_length_ - offset);
64246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com}
65246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
66246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.comFontData::~FontData() {}
67246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
68246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.comvoid FontData::Init(ByteArray* ba) {
69246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  array_ = ba;
70246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  bound_offset_ = 0;
71333edd91cb32d6acfd0307ba2ae8f60baed75ff4arthurhsu@google.com  bound_length_ = GROWABLE_SIZE;
72464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com}
73464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
74246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.comint32_t FontData::BoundOffset(int32_t offset) {
75464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  return offset + bound_offset_;
76464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com}
77464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
78246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.comint32_t FontData::BoundLength(int32_t offset, int32_t length) {
79464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  return std::min<int32_t>(length, bound_length_ - offset);
80464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com}
81464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
82464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com}  // namespace sfntly
83