tesseract  5.0.0-alpha-619-ge9db
fontinfo.cpp
Go to the documentation of this file.
1 // File: fontinfo.cpp
3 // Description: Font information classes abstracted from intproto.h/cpp.
4 // Author: rays@google.com (Ray Smith)
5 //
6 // (C) Copyright 2011, Google Inc.
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
18 
19 #include "fontinfo.h"
20 #include "bitvector.h"
21 #include "unicity_table.h"
22 
23 namespace tesseract {
24 
25 // Writes to the given file. Returns false in case of error.
26 bool FontInfo::Serialize(FILE* fp) const {
27  if (!write_info(fp, *this)) return false;
28  if (!write_spacing_info(fp, *this)) return false;
29  return true;
30 }
31 // Reads from the given file. Returns false in case of error.
32 // If swap is true, assumes a big/little-endian swap is needed.
34  if (!read_info(fp, this)) return false;
35  if (!read_spacing_info(fp, this)) return false;
36  return true;
37 }
38 
40  using namespace std::placeholders; // for _1, _2
41  set_compare_callback(std::bind(CompareFontInfo, _1, _2));
43 }
44 
46 }
47 
48 // Writes to the given file. Returns false in case of error.
49 bool FontInfoTable::Serialize(FILE* fp) const {
50  return this->SerializeClasses(fp);
51 }
52 // Reads from the given file. Returns false in case of error.
53 // If swap is true, assumes a big/little-endian swap is needed.
55  truncate(0);
56  return this->DeSerializeClasses(fp);
57 }
58 
59 // Returns true if the given set of fonts includes one with the same
60 // properties as font_id.
62  int font_id, const GenericVector<ScoredFont>& font_set) const {
63  uint32_t properties = get(font_id).properties;
64  for (int f = 0; f < font_set.size(); ++f) {
65  if (get(font_set[f].fontinfo_id).properties == properties)
66  return true;
67  }
68  return false;
69 }
70 
71 // Returns true if the given set of fonts includes multiple properties.
73  const GenericVector<ScoredFont>& font_set) const {
74  if (font_set.empty()) return false;
75  int first_font = font_set[0].fontinfo_id;
76  uint32_t properties = get(first_font).properties;
77  for (int f = 1; f < font_set.size(); ++f) {
78  if (get(font_set[f].fontinfo_id).properties != properties)
79  return true;
80  }
81  return false;
82 }
83 
84 // Moves any non-empty FontSpacingInfo entries from other to this.
86  using namespace std::placeholders; // for _1, _2
87  set_compare_callback(std::bind(CompareFontInfo, _1, _2));
89  for (int i = 0; i < other->size(); ++i) {
90  GenericVector<FontSpacingInfo*>* spacing_vec = other->get(i).spacing_vec;
91  if (spacing_vec != nullptr) {
92  int target_index = get_index(other->get(i));
93  if (target_index < 0) {
94  // Bit copy the FontInfo and steal all the pointers.
95  push_back(other->get(i));
96  other->get(i).name = nullptr;
97  } else {
98  delete get(target_index).spacing_vec;
99  get(target_index).spacing_vec = other->get(i).spacing_vec;
100  }
101  other->get(i).spacing_vec = nullptr;
102  }
103  }
104 }
105 
106 // Moves this to the target unicity table.
108  target->clear();
109  using namespace std::placeholders; // for _1, _2
110  target->set_compare_callback(std::bind(CompareFontInfo, _1, _2));
111  target->set_clear_callback(std::bind(FontInfoDeleteCallback, _1));
112  for (int i = 0; i < size(); ++i) {
113  // Bit copy the FontInfo and steal all the pointers.
114  target->push_back(get(i));
115  get(i).name = nullptr;
116  get(i).spacing_vec = nullptr;
117  }
118 }
119 
120 
121 // Compare FontInfo structures.
122 bool CompareFontInfo(const FontInfo& fi1, const FontInfo& fi2) {
123  // The font properties are required to be the same for two font with the same
124  // name, so there is no need to test them.
125  // Consequently, querying the table with only its font name as information is
126  // enough to retrieve its properties.
127  return strcmp(fi1.name, fi2.name) == 0;
128 }
129 // Compare FontSet structures.
130 bool CompareFontSet(const FontSet& fs1, const FontSet& fs2) {
131  if (fs1.size != fs2.size)
132  return false;
133  for (int i = 0; i < fs1.size; ++i) {
134  if (fs1.configs[i] != fs2.configs[i])
135  return false;
136  }
137  return true;
138 }
139 
140 // Callbacks for GenericVector.
142  if (f.spacing_vec != nullptr) {
143  f.spacing_vec->delete_data_pointers();
144  delete f.spacing_vec;
145  f.spacing_vec = nullptr;
146  }
147  delete[] f.name;
148  f.name = nullptr;
149 }
151  delete[] fs.configs;
152 }
153 
154 /*---------------------------------------------------------------------------*/
155 // Callbacks used by UnicityTable to read/write FontInfo/FontSet structures.
156 bool read_info(TFile* f, FontInfo* fi) {
157  uint32_t size;
158  if (!f->DeSerialize(&size)) return false;
159  char* font_name = new char[size + 1];
160  fi->name = font_name;
161  if (!f->DeSerialize(font_name, size)) return false;
162  font_name[size] = '\0';
163  return f->DeSerialize(&fi->properties);
164 }
165 
166 bool write_info(FILE* f, const FontInfo& fi) {
167  int32_t size = strlen(fi.name);
168  return tesseract::Serialize(f, &size) &&
169  tesseract::Serialize(f, &fi.name[0], size) &&
171 }
172 
174  int32_t vec_size, kern_size;
175  if (!f->DeSerialize(&vec_size)) return false;
176  ASSERT_HOST(vec_size >= 0);
177  if (vec_size == 0) return true;
178  fi->init_spacing(vec_size);
179  for (int i = 0; i < vec_size; ++i) {
180  auto *fs = new FontSpacingInfo();
181  if (!f->DeSerialize(&fs->x_gap_before) ||
182  !f->DeSerialize(&fs->x_gap_after) ||
183  !f->DeSerialize(&kern_size)) {
184  delete fs;
185  return false;
186  }
187  if (kern_size < 0) { // indication of a nullptr entry in fi->spacing_vec
188  delete fs;
189  continue;
190  }
191  if (kern_size > 0 && (!fs->kerned_unichar_ids.DeSerialize(f) ||
192  !fs->kerned_x_gaps.DeSerialize(f))) {
193  delete fs;
194  return false;
195  }
196  fi->add_spacing(i, fs);
197  }
198  return true;
199 }
200 
201 bool write_spacing_info(FILE* f, const FontInfo& fi) {
202  int32_t vec_size = (fi.spacing_vec == nullptr) ? 0 : fi.spacing_vec->size();
203  if (!tesseract::Serialize(f, &vec_size)) return false;
204  int16_t x_gap_invalid = -1;
205  for (int i = 0; i < vec_size; ++i) {
206  FontSpacingInfo *fs = fi.spacing_vec->get(i);
207  int32_t kern_size = (fs == nullptr) ? -1 : fs->kerned_x_gaps.size();
208  if (fs == nullptr) {
209  // Writing two invalid x-gaps.
210  if (!tesseract::Serialize(f, &x_gap_invalid, 2) ||
211  !tesseract::Serialize(f, &kern_size)) {
212  return false;
213  }
214  } else {
215  if (!tesseract::Serialize(f, &fs->x_gap_before) ||
216  !tesseract::Serialize(f, &fs->x_gap_after) ||
217  !tesseract::Serialize(f, &kern_size)) {
218  return false;
219  }
220  }
221  if (kern_size > 0 && (!fs->kerned_unichar_ids.Serialize(f) ||
222  !fs->kerned_x_gaps.Serialize(f))) {
223  return false;
224  }
225  }
226  return true;
227 }
228 
229 bool read_set(TFile* f, FontSet* fs) {
230  if (!f->DeSerialize(&fs->size)) return false;
231  fs->configs = new int[fs->size];
232  return f->DeSerialize(&fs->configs[0], fs->size);
233 }
234 
235 bool write_set(FILE* f, const FontSet& fs) {
236  return tesseract::Serialize(f, &fs.size) &&
237  tesseract::Serialize(f, &fs.configs[0], fs.size);
238 }
239 
240 } // namespace tesseract.
tesseract::FontInfoDeleteCallback
void FontInfoDeleteCallback(FontInfo f)
Definition: fontinfo.cpp:141
tesseract::FontInfo::add_spacing
void add_spacing(UNICHAR_ID uch_id, FontSpacingInfo *spacing_info)
Definition: fontinfo.h:80
tesseract::FontSpacingInfo::x_gap_after
int16_t x_gap_after
Definition: fontinfo.h:53
unicity_table.h
tesseract::FontSpacingInfo::kerned_unichar_ids
GenericVector< UNICHAR_ID > kerned_unichar_ids
Definition: fontinfo.h:54
ASSERT_HOST
#define ASSERT_HOST(x)
Definition: errcode.h:87
tesseract::write_set
bool write_set(FILE *f, const FontSet &fs)
Definition: fontinfo.cpp:235
tesseract::FontSpacingInfo::x_gap_before
int16_t x_gap_before
Definition: fontinfo.h:52
tesseract::FontInfoTable::FontInfoTable
FontInfoTable()
Definition: fontinfo.cpp:39
tesseract::write_info
bool write_info(FILE *f, const FontInfo &fi)
Definition: fontinfo.cpp:166
tesseract::FontSpacingInfo
Definition: fontinfo.h:51
GenericVector::Serialize
bool Serialize(FILE *fp) const
Definition: genericvector.h:929
tesseract::FontInfoTable::Serialize
bool Serialize(FILE *fp) const
Definition: fontinfo.cpp:49
tesseract::read_spacing_info
bool read_spacing_info(TFile *f, FontInfo *fi)
Definition: fontinfo.cpp:173
GenericVector< FontInfo >::set_clear_callback
void set_clear_callback(std::function< void(FontInfo)> cb)
Definition: genericvector.h:140
tesseract::write_spacing_info
bool write_spacing_info(FILE *f, const FontInfo &fi)
Definition: fontinfo.cpp:201
tesseract::FontInfoTable
Definition: fontinfo.h:146
UnicityTable::push_back
int push_back(T object)
Add an element in the table.
Definition: unicity_table.h:168
tesseract::FontInfo::spacing_vec
GenericVector< FontSpacingInfo * > * spacing_vec
Definition: fontinfo.h:125
GenericVector< FontInfo >::DeSerializeClasses
bool DeSerializeClasses(bool swap, FILE *fp)
Definition: genericvector.h:1038
tesseract::FontInfoTable::SetContainsMultipleFontProperties
bool SetContainsMultipleFontProperties(const GenericVector< ScoredFont > &font_set) const
Definition: fontinfo.cpp:72
GenericVector< FontInfo >::push_back
int push_back(FontInfo object)
Definition: genericvector.h:799
GenericVector< FontInfo >::SerializeClasses
bool SerializeClasses(FILE *fp) const
Definition: genericvector.h:1008
UnicityTable::set_compare_callback
void set_compare_callback(std::function< bool(const T &, const T &)> cb)
Definition: unicity_table.h:74
GenericVector< FontInfo >::set_compare_callback
void set_compare_callback(std::function< bool(const FontInfo &, const FontInfo &)> cb)
Definition: genericvector.h:146
tesseract::TFile::DeSerialize
bool DeSerialize(char *data, size_t count=1)
Definition: serialis.cpp:117
tesseract::FontInfoTable::~FontInfoTable
~FontInfoTable()
Definition: fontinfo.cpp:45
tesseract::FontSpacingInfo::kerned_x_gaps
GenericVector< int16_t > kerned_x_gaps
Definition: fontinfo.h:55
tesseract::FontSetDeleteCallback
void FontSetDeleteCallback(FontSet fs)
Definition: fontinfo.cpp:150
tesseract::FontInfo::properties
uint32_t properties
Definition: fontinfo.h:118
tesseract::TFile
Definition: serialis.h:75
tesseract::FontInfoTable::MoveTo
void MoveTo(UnicityTable< FontInfo > *target)
Definition: fontinfo.cpp:107
GenericVector::empty
bool empty() const
Definition: genericvector.h:86
GenericVector< FontInfo >::get_index
int get_index(const FontInfo &object) const
Definition: genericvector.h:781
tesseract
Definition: baseapi.h:65
fontinfo.h
tesseract::FontInfoTable::SetContainsFontProperties
bool SetContainsFontProperties(int font_id, const GenericVector< ScoredFont > &font_set) const
Definition: fontinfo.cpp:61
tesseract::CompareFontSet
bool CompareFontSet(const FontSet &fs1, const FontSet &fs2)
Definition: fontinfo.cpp:130
tesseract::FontInfo
Definition: fontinfo.h:62
bitvector.h
tesseract::FontInfo::init_spacing
void init_spacing(int unicharset_size)
Definition: fontinfo.h:73
GenericVector
Definition: baseapi.h:40
tesseract::FontInfo::name
char * name
Definition: fontinfo.h:117
UnicityTable
Definition: fontinfo.h:30
UnicityTable::clear
void clear()
Definition: unicity_table.h:178
tesseract::FontInfoTable::DeSerialize
bool DeSerialize(TFile *fp)
Definition: fontinfo.cpp:54
tesseract::read_info
bool read_info(TFile *f, FontInfo *fi)
Definition: fontinfo.cpp:156
tesseract::FontInfo::DeSerialize
bool DeSerialize(TFile *fp)
Definition: fontinfo.cpp:33
UnicityTable::set_clear_callback
void set_clear_callback(std::function< void(T)> cb)
Definition: unicity_table.h:68
GenericVector< FontInfo >::truncate
void truncate(int size)
Definition: genericvector.h:132
tesseract::FontSet::size
int size
Definition: fontinfo.h:138
GenericVector< FontInfo >::get
FontInfo & get(int index) const
Definition: genericvector.h:716
tesseract::CompareFontInfo
bool CompareFontInfo(const FontInfo &fi1, const FontInfo &fi2)
Definition: fontinfo.cpp:122
tesseract::FontInfoTable::MoveSpacingInfoFrom
void MoveSpacingInfoFrom(FontInfoTable *other)
Definition: fontinfo.cpp:85
tesseract::FontSet::configs
int * configs
Definition: fontinfo.h:139
tesseract::FontInfo::Serialize
bool Serialize(FILE *fp) const
Definition: fontinfo.cpp:26
GenericVector::size
int size() const
Definition: genericvector.h:71
tesseract::Serialize
bool Serialize(FILE *fp, const char *data, size_t n=1)
Definition: serialis.cpp:73
tesseract::FontSet
Definition: fontinfo.h:137
tesseract::read_set
bool read_set(TFile *f, FontSet *fs)
Definition: fontinfo.cpp:229