tesseract  4.1.1
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 
42 }
43 
45 }
46 
47 // Writes to the given file. Returns false in case of error.
48 bool FontInfoTable::Serialize(FILE* fp) const {
49  return this->SerializeClasses(fp);
50 }
51 // Reads from the given file. Returns false in case of error.
52 // If swap is true, assumes a big/little-endian swap is needed.
54  truncate(0);
55  return this->DeSerializeClasses(fp);
56 }
57 
58 // Returns true if the given set of fonts includes one with the same
59 // properties as font_id.
61  int font_id, const GenericVector<ScoredFont>& font_set) const {
62  uint32_t properties = get(font_id).properties;
63  for (int f = 0; f < font_set.size(); ++f) {
64  if (get(font_set[f].fontinfo_id).properties == properties)
65  return true;
66  }
67  return false;
68 }
69 
70 // Returns true if the given set of fonts includes multiple properties.
72  const GenericVector<ScoredFont>& font_set) const {
73  if (font_set.empty()) return false;
74  int first_font = font_set[0].fontinfo_id;
75  uint32_t properties = get(first_font).properties;
76  for (int f = 1; f < font_set.size(); ++f) {
77  if (get(font_set[f].fontinfo_id).properties != properties)
78  return true;
79  }
80  return false;
81 }
82 
83 // Moves any non-empty FontSpacingInfo entries from other to this.
87  for (int i = 0; i < other->size(); ++i) {
88  GenericVector<FontSpacingInfo*>* spacing_vec = other->get(i).spacing_vec;
89  if (spacing_vec != nullptr) {
90  int target_index = get_index(other->get(i));
91  if (target_index < 0) {
92  // Bit copy the FontInfo and steal all the pointers.
93  push_back(other->get(i));
94  other->get(i).name = nullptr;
95  } else {
96  delete get(target_index).spacing_vec;
97  get(target_index).spacing_vec = other->get(i).spacing_vec;
98  }
99  other->get(i).spacing_vec = nullptr;
100  }
101  }
102 }
103 
104 // Moves this to the target unicity table.
106  target->clear();
109  for (int i = 0; i < size(); ++i) {
110  // Bit copy the FontInfo and steal all the pointers.
111  target->push_back(get(i));
112  get(i).name = nullptr;
113  get(i).spacing_vec = nullptr;
114  }
115 }
116 
117 
118 // Compare FontInfo structures.
119 bool CompareFontInfo(const FontInfo& fi1, const FontInfo& fi2) {
120  // The font properties are required to be the same for two font with the same
121  // name, so there is no need to test them.
122  // Consequently, querying the table with only its font name as information is
123  // enough to retrieve its properties.
124  return strcmp(fi1.name, fi2.name) == 0;
125 }
126 // Compare FontSet structures.
127 bool CompareFontSet(const FontSet& fs1, const FontSet& fs2) {
128  if (fs1.size != fs2.size)
129  return false;
130  for (int i = 0; i < fs1.size; ++i) {
131  if (fs1.configs[i] != fs2.configs[i])
132  return false;
133  }
134  return true;
135 }
136 
137 // Callbacks for GenericVector.
139  if (f.spacing_vec != nullptr) {
140  f.spacing_vec->delete_data_pointers();
141  delete f.spacing_vec;
142  f.spacing_vec = nullptr;
143  }
144  delete[] f.name;
145  f.name = nullptr;
146 }
148  delete[] fs.configs;
149 }
150 
151 /*---------------------------------------------------------------------------*/
152 // Callbacks used by UnicityTable to read/write FontInfo/FontSet structures.
153 bool read_info(TFile* f, FontInfo* fi) {
154  uint32_t size;
155  if (!f->DeSerialize(&size)) return false;
156  char* font_name = new char[size + 1];
157  fi->name = font_name;
158  if (!f->DeSerialize(font_name, size)) return false;
159  font_name[size] = '\0';
160  return f->DeSerialize(&fi->properties);
161 }
162 
163 bool write_info(FILE* f, const FontInfo& fi) {
164  int32_t size = strlen(fi.name);
165  return tesseract::Serialize(f, &size) &&
166  tesseract::Serialize(f, &fi.name[0], size) &&
168 }
169 
171  int32_t vec_size, kern_size;
172  if (!f->DeSerialize(&vec_size)) return false;
173  ASSERT_HOST(vec_size >= 0);
174  if (vec_size == 0) return true;
175  fi->init_spacing(vec_size);
176  for (int i = 0; i < vec_size; ++i) {
177  auto *fs = new FontSpacingInfo();
178  if (!f->DeSerialize(&fs->x_gap_before) ||
179  !f->DeSerialize(&fs->x_gap_after) ||
180  !f->DeSerialize(&kern_size)) {
181  delete fs;
182  return false;
183  }
184  if (kern_size < 0) { // indication of a nullptr entry in fi->spacing_vec
185  delete fs;
186  continue;
187  }
188  if (kern_size > 0 && (!fs->kerned_unichar_ids.DeSerialize(f) ||
189  !fs->kerned_x_gaps.DeSerialize(f))) {
190  delete fs;
191  return false;
192  }
193  fi->add_spacing(i, fs);
194  }
195  return true;
196 }
197 
198 bool write_spacing_info(FILE* f, const FontInfo& fi) {
199  int32_t vec_size = (fi.spacing_vec == nullptr) ? 0 : fi.spacing_vec->size();
200  if (!tesseract::Serialize(f, &vec_size)) return false;
201  int16_t x_gap_invalid = -1;
202  for (int i = 0; i < vec_size; ++i) {
203  FontSpacingInfo *fs = fi.spacing_vec->get(i);
204  int32_t kern_size = (fs == nullptr) ? -1 : fs->kerned_x_gaps.size();
205  if (fs == nullptr) {
206  // Writing two invalid x-gaps.
207  if (!tesseract::Serialize(f, &x_gap_invalid, 2) ||
208  !tesseract::Serialize(f, &kern_size)) {
209  return false;
210  }
211  } else {
212  if (!tesseract::Serialize(f, &fs->x_gap_before) ||
213  !tesseract::Serialize(f, &fs->x_gap_after) ||
214  !tesseract::Serialize(f, &kern_size)) {
215  return false;
216  }
217  }
218  if (kern_size > 0 && (!fs->kerned_unichar_ids.Serialize(f) ||
219  !fs->kerned_x_gaps.Serialize(f))) {
220  return false;
221  }
222  }
223  return true;
224 }
225 
226 bool read_set(TFile* f, FontSet* fs) {
227  if (!f->DeSerialize(&fs->size)) return false;
228  fs->configs = new int[fs->size];
229  return f->DeSerialize(&fs->configs[0], fs->size);
230 }
231 
232 bool write_set(FILE* f, const FontSet& fs) {
233  return tesseract::Serialize(f, &fs.size) &&
234  tesseract::Serialize(f, &fs.configs[0], fs.size);
235 }
236 
237 } // namespace tesseract.
tesseract::read_set
bool read_set(TFile *f, FontSet *fs)
Definition: fontinfo.cpp:226
tesseract::write_set
bool write_set(FILE *f, const FontSet &fs)
Definition: fontinfo.cpp:232
tesseract::FontInfo::properties
uint32_t properties
Definition: fontinfo.h:118
tesseract::FontSpacingInfo::kerned_unichar_ids
GenericVector< UNICHAR_ID > kerned_unichar_ids
Definition: fontinfo.h:54
tesseract::FontSpacingInfo::x_gap_after
int16_t x_gap_after
Definition: fontinfo.h:53
tesseract::FontInfo
Definition: fontinfo.h:62
GenericVector< FontInfo >::truncate
void truncate(int size)
Definition: genericvector.h:137
tesseract::TFile::DeSerialize
bool DeSerialize(char *data, size_t count=1)
Definition: serialis.cpp:121
tesseract::write_spacing_info
bool write_spacing_info(FILE *f, const FontInfo &fi)
Definition: fontinfo.cpp:198
tesseract::write_info
bool write_info(FILE *f, const FontInfo &fi)
Definition: fontinfo.cpp:163
UnicityTable::set_clear_callback
void set_clear_callback(TessCallback1< T > *cb)
Definition: unicity_table.h:171
tesseract::FontSpacingInfo::x_gap_before
int16_t x_gap_before
Definition: fontinfo.h:52
tesseract::FontInfo::DeSerialize
bool DeSerialize(TFile *fp)
Definition: fontinfo.cpp:33
tesseract::FontInfo::spacing_vec
GenericVector< FontSpacingInfo * > * spacing_vec
Definition: fontinfo.h:125
tesseract
Definition: altorenderer.cpp:25
tesseract::FontInfo::init_spacing
void init_spacing(int unicharset_size)
Definition: fontinfo.h:73
unicity_table.h
tesseract::FontInfoTable::Serialize
bool Serialize(FILE *fp) const
Definition: fontinfo.cpp:48
GenericVector
Definition: baseapi.h:37
tesseract::read_spacing_info
bool read_spacing_info(TFile *f, FontInfo *fi)
Definition: fontinfo.cpp:170
tesseract::FontSet::configs
int * configs
Definition: fontinfo.h:139
tesseract::FontInfoTable::FontInfoTable
FontInfoTable()
Definition: fontinfo.cpp:39
bitvector.h
ASSERT_HOST
#define ASSERT_HOST(x)
Definition: errcode.h:88
tesseract::FontInfoTable
Definition: fontinfo.h:146
tesseract::FontSetDeleteCallback
void FontSetDeleteCallback(FontSet fs)
Definition: fontinfo.cpp:147
tesseract::FontSpacingInfo
Definition: fontinfo.h:51
tesseract::FontInfoTable::MoveSpacingInfoFrom
void MoveSpacingInfoFrom(FontInfoTable *other)
Definition: fontinfo.cpp:84
tesseract::FontInfoTable::MoveTo
void MoveTo(UnicityTable< FontInfo > *target)
Definition: fontinfo.cpp:105
tesseract::FontInfo::add_spacing
void add_spacing(UNICHAR_ID uch_id, FontSpacingInfo *spacing_info)
Definition: fontinfo.h:80
tesseract::FontInfoTable::~FontInfoTable
~FontInfoTable()
Definition: fontinfo.cpp:44
tesseract::Serialize
bool Serialize(FILE *fp, const char *data, size_t n)
Definition: serialis.cpp:77
UnicityTable
Definition: fontinfo.h:30
tesseract::CompareFontSet
bool CompareFontSet(const FontSet &fs1, const FontSet &fs2)
Definition: fontinfo.cpp:127
GenericVector::Serialize
bool Serialize(FILE *fp) const
Definition: genericvector.h:973
GenericVector< FontInfo >::set_clear_callback
void set_clear_callback(TessCallback1< FontInfo > *cb)
Definition: genericvector.h:145
tesseract::FontSet::size
int size
Definition: fontinfo.h:138
tesseract::FontInfoTable::SetContainsFontProperties
bool SetContainsFontProperties(int font_id, const GenericVector< ScoredFont > &font_set) const
Definition: fontinfo.cpp:60
NewPermanentTessCallback
_ConstTessMemberResultCallback_5_0< false, R, T1, P1, P2, P3, P4, P5 >::base * NewPermanentTessCallback(const T1 *obj, R(T2::*member)(P1, P2, P3, P4, P5) const, typename Identity< P1 >::type p1, typename Identity< P2 >::type p2, typename Identity< P3 >::type p3, typename Identity< P4 >::type p4, typename Identity< P5 >::type p5)
Definition: tesscallback.h:258
tesseract::FontSet
Definition: fontinfo.h:137
UnicityTable::push_back
int push_back(T object)
Add an element in the table.
Definition: unicity_table.h:160
tesseract::FontInfoTable::SetContainsMultipleFontProperties
bool SetContainsMultipleFontProperties(const GenericVector< ScoredFont > &font_set) const
Definition: fontinfo.cpp:71
GenericVector< FontInfo >::DeSerializeClasses
bool DeSerializeClasses(bool swap, FILE *fp)
Definition: genericvector.h:1082
tesseract::FontInfo::Serialize
bool Serialize(FILE *fp) const
Definition: fontinfo.cpp:26
tesseract::CompareFontInfo
bool CompareFontInfo(const FontInfo &fi1, const FontInfo &fi2)
Definition: fontinfo.cpp:119
GenericVector::size
int size() const
Definition: genericvector.h:72
GenericVector< FontInfo >::set_compare_callback
void set_compare_callback(TessResultCallback2< bool, FontInfo const &, FontInfo const & > *cb)
Definition: genericvector.h:151
tesseract::FontSpacingInfo::kerned_x_gaps
GenericVector< int16_t > kerned_x_gaps
Definition: fontinfo.h:55
fontinfo.h
UnicityTable::clear
void clear()
Definition: unicity_table.h:185
tesseract::read_info
bool read_info(TFile *f, FontInfo *fi)
Definition: fontinfo.cpp:153
GenericVector< FontInfo >::SerializeClasses
bool SerializeClasses(FILE *fp) const
Definition: genericvector.h:1052
tesseract::FontInfoTable::DeSerialize
bool DeSerialize(TFile *fp)
Definition: fontinfo.cpp:53
tesseract::FontInfo::name
char * name
Definition: fontinfo.h:117
tesseract::FontInfoDeleteCallback
void FontInfoDeleteCallback(FontInfo f)
Definition: fontinfo.cpp:138
tesseract::TFile
Definition: serialis.h:76
GenericVector::empty
bool empty() const
Definition: genericvector.h:91
UnicityTable::set_compare_callback
void set_compare_callback(TessResultCallback2< bool, T const &, T const & > *cb)
Definition: unicity_table.h:178
GenericVector< FontInfo >::get
FontInfo & get(int index) const
Definition: genericvector.h:754
GenericVector< FontInfo >::get_index
int get_index(const FontInfo &object) const
Definition: genericvector.h:819
GenericVector< FontInfo >::push_back
int push_back(FontInfo object)
Definition: genericvector.h:837