aboutsummaryrefslogtreecommitdiff
path: root/cpp/src/sfntly/data/font_data.cc
blob: 8cda562d154d6473a8ab5a37fd90bb208ef2bc9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
 * Copyright 2011 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <limits.h>
#include <algorithm>
#include <functional>

#include "sfntly/data/font_data.h"

namespace sfntly {

const int32_t DataSize::kBYTE = 1;
const int32_t DataSize::kCHAR = 1;
const int32_t DataSize::kUSHORT = 2;
const int32_t DataSize::kSHORT = 2;
const int32_t DataSize::kUINT24 = 3;
const int32_t DataSize::kULONG = 4;
const int32_t DataSize::kLONG = 4;
const int32_t DataSize::kFixed = 4;
const int32_t DataSize::kFUNIT = 4;
const int32_t DataSize::kFWORD = 2;
const int32_t DataSize::kUFWORD = 2;
const int32_t DataSize::kF2DOT14 = 2;
const int32_t DataSize::kLONGDATETIME = 8;
const int32_t DataSize::kTag = 4;
const int32_t DataSize::kGlyphID = 2;
const int32_t DataSize::kOffset = 2;

FontData::~FontData() {}

void FontData::init(ByteArray* ba) {
  array_ = ba;
  bound_offset_ = 0;
  bound_length_ = INT_MAX;
}

FontData::FontData(ByteArray* ba) {
  init(ba);
}

FontData::FontData(FontData* data, int32_t offset, int32_t length) {
  init(data->array_);
  bound(data->bound_offset_ + offset, length);
}

FontData::FontData(FontData* data, int32_t offset) {
  init(data->array_);
  bound(offset);
}

bool FontData::bound(int32_t offset, int32_t length) {
  if (offset + length > size() || offset < 0 || length < 0)
    return false;

  bound_offset_ += offset;
  bound_length_ = length;
  return true;
}

bool FontData::bound(int32_t offset) {
if (offset > size() || offset < 0)
    return false;

  bound_offset_ += offset;
  return true;
}

int32_t FontData::length() const {
  return std::min<int32_t>(array_->length() - bound_offset_, bound_length_);
}

int32_t FontData::size() const {
  return std::min<int32_t>(array_->size() - bound_offset_, bound_length_);
}

int32_t FontData::boundOffset(int32_t offset) {
  return offset + bound_offset_;
}

int32_t FontData::boundLength(int32_t offset, int32_t length) {
  return std::min<int32_t>(length, bound_length_ - offset);
}

}  // namespace sfntly