forked from tesseract-ocr/tesseract
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfontinfo.cpp
228 lines (211 loc) · 6.99 KB
/
fontinfo.cpp
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
///////////////////////////////////////////////////////////////////////
// File: fontinfo.cpp
// Description: Font information classes abstracted from intproto.h/cpp.
// Author: [email protected] (Ray Smith)
//
// (C) Copyright 2011, Google Inc.
// 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 "fontinfo.h"
#include "bitvector.h"
#include "unicity_table.h"
namespace tesseract {
// Writes to the given file. Returns false in case of error.
bool FontInfo::Serialize(FILE *fp) const {
if (!write_info(fp, *this)) {
return false;
}
if (!write_spacing_info(fp, *this)) {
return false;
}
return true;
}
// Reads from the given file. Returns false in case of error.
// If swap is true, assumes a big/little-endian swap is needed.
bool FontInfo::DeSerialize(TFile *fp) {
if (!read_info(fp, this)) {
return false;
}
if (!read_spacing_info(fp, this)) {
return false;
}
return true;
}
FontInfoTable::FontInfoTable() {
using namespace std::placeholders; // for _1, _2
set_clear_callback(std::bind(FontInfoDeleteCallback, _1));
}
FontInfoTable::~FontInfoTable() = default;
// Writes to the given file. Returns false in case of error.
bool FontInfoTable::Serialize(FILE *fp) const {
return this->SerializeClasses(fp);
}
// Reads from the given file. Returns false in case of error.
// If swap is true, assumes a big/little-endian swap is needed.
bool FontInfoTable::DeSerialize(TFile *fp) {
truncate(0);
return this->DeSerializeClasses(fp);
}
// Returns true if the given set of fonts includes one with the same
// properties as font_id.
bool FontInfoTable::SetContainsFontProperties(int font_id,
const std::vector<ScoredFont> &font_set) const {
uint32_t properties = at(font_id).properties;
for (auto &&f : font_set) {
if (at(f.fontinfo_id).properties == properties) {
return true;
}
}
return false;
}
// Returns true if the given set of fonts includes multiple properties.
bool FontInfoTable::SetContainsMultipleFontProperties(
const std::vector<ScoredFont> &font_set) const {
if (font_set.empty()) {
return false;
}
int first_font = font_set[0].fontinfo_id;
uint32_t properties = at(first_font).properties;
for (unsigned f = 1; f < font_set.size(); ++f) {
if (at(font_set[f].fontinfo_id).properties != properties) {
return true;
}
}
return false;
}
// Moves any non-empty FontSpacingInfo entries from other to this.
void FontInfoTable::MoveSpacingInfoFrom(FontInfoTable *other) {
using namespace std::placeholders; // for _1, _2
set_clear_callback(std::bind(FontInfoDeleteCallback, _1));
for (unsigned i = 0; i < other->size(); ++i) {
std::vector<FontSpacingInfo *> *spacing_vec = other->at(i).spacing_vec;
if (spacing_vec != nullptr) {
int target_index = get_index(other->at(i));
if (target_index < 0) {
// Bit copy the FontInfo and steal all the pointers.
push_back(other->at(i));
other->at(i).name = nullptr;
} else {
delete at(target_index).spacing_vec;
at(target_index).spacing_vec = other->at(i).spacing_vec;
}
other->at(i).spacing_vec = nullptr;
}
}
}
// Moves this to the target unicity table.
void FontInfoTable::MoveTo(UnicityTable<FontInfo> *target) {
target->clear();
using namespace std::placeholders; // for _1, _2
target->set_clear_callback(std::bind(FontInfoDeleteCallback, _1));
for (unsigned i = 0; i < size(); ++i) {
// Bit copy the FontInfo and steal all the pointers.
target->push_back(at(i));
at(i).name = nullptr;
at(i).spacing_vec = nullptr;
}
}
// Callbacks for GenericVector.
void FontInfoDeleteCallback(FontInfo f) {
if (f.spacing_vec != nullptr) {
for (auto data : *f.spacing_vec) {
delete data;
}
delete f.spacing_vec;
f.spacing_vec = nullptr;
}
delete[] f.name;
f.name = nullptr;
}
/*---------------------------------------------------------------------------*/
// Callbacks used by UnicityTable to read/write FontInfo/FontSet structures.
bool read_info(TFile *f, FontInfo *fi) {
uint32_t size;
if (!f->DeSerialize(&size)) {
return false;
}
char *font_name = new char[size + 1];
fi->name = font_name;
if (!f->DeSerialize(font_name, size)) {
return false;
}
font_name[size] = '\0';
return f->DeSerialize(&fi->properties);
}
bool write_info(FILE *f, const FontInfo &fi) {
int32_t size = strlen(fi.name);
return tesseract::Serialize(f, &size) && tesseract::Serialize(f, &fi.name[0], size) &&
tesseract::Serialize(f, &fi.properties);
}
bool read_spacing_info(TFile *f, FontInfo *fi) {
int32_t vec_size, kern_size;
if (!f->DeSerialize(&vec_size)) {
return false;
}
ASSERT_HOST(vec_size >= 0);
if (vec_size == 0) {
return true;
}
fi->init_spacing(vec_size);
for (int i = 0; i < vec_size; ++i) {
auto *fs = new FontSpacingInfo();
if (!f->DeSerialize(&fs->x_gap_before) || !f->DeSerialize(&fs->x_gap_after) ||
!f->DeSerialize(&kern_size)) {
delete fs;
return false;
}
if (kern_size < 0) { // indication of a nullptr entry in fi->spacing_vec
delete fs;
continue;
}
if (kern_size > 0 &&
(!f->DeSerialize(fs->kerned_unichar_ids) || !f->DeSerialize(fs->kerned_x_gaps))) {
delete fs;
return false;
}
fi->add_spacing(i, fs);
}
return true;
}
bool write_spacing_info(FILE *f, const FontInfo &fi) {
int32_t vec_size = (fi.spacing_vec == nullptr) ? 0 : fi.spacing_vec->size();
if (!tesseract::Serialize(f, &vec_size)) {
return false;
}
int16_t x_gap_invalid = -1;
for (int i = 0; i < vec_size; ++i) {
FontSpacingInfo *fs = fi.spacing_vec->at(i);
int32_t kern_size = (fs == nullptr) ? -1 : fs->kerned_x_gaps.size();
if (fs == nullptr) {
// Writing two invalid x-gaps.
if (!tesseract::Serialize(f, &x_gap_invalid, 2) || !tesseract::Serialize(f, &kern_size)) {
return false;
}
} else {
if (!tesseract::Serialize(f, &fs->x_gap_before) ||
!tesseract::Serialize(f, &fs->x_gap_after) || !tesseract::Serialize(f, &kern_size)) {
return false;
}
}
if (kern_size > 0 &&
(!Serialize(f, fs->kerned_unichar_ids) || !Serialize(f, fs->kerned_x_gaps))) {
return false;
}
}
return true;
}
bool write_set(FILE *f, const FontSet &fs) {
int size = fs.size();
return tesseract::Serialize(f, &size) &&
(size > 0 ? tesseract::Serialize(f, &fs[0], size) : true);
}
} // namespace tesseract.