tesseract  4.0.0-1-g2a2b
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 // Created: Wed May 18 10:39:01 PDT 2011
6 //
7 // (C) Copyright 2011, Google Inc.
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
17 //
19 
20 #include "fontinfo.h"
21 #include "bitvector.h"
22 #include "unicity_table.h"
23 
24 namespace tesseract {
25 
26 // Writes to the given file. Returns false in case of error.
27 bool FontInfo::Serialize(FILE* fp) const {
28  if (!write_info(fp, *this)) return false;
29  if (!write_spacing_info(fp, *this)) return false;
30  return true;
31 }
32 // Reads from the given file. Returns false in case of error.
33 // If swap is true, assumes a big/little-endian swap is needed.
35  if (!read_info(fp, this)) return false;
36  if (!read_spacing_info(fp, this)) return false;
37  return true;
38 }
39 
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.
88  for (int i = 0; i < other->size(); ++i) {
89  GenericVector<FontSpacingInfo*>* spacing_vec = other->get(i).spacing_vec;
90  if (spacing_vec != nullptr) {
91  int target_index = get_index(other->get(i));
92  if (target_index < 0) {
93  // Bit copy the FontInfo and steal all the pointers.
94  push_back(other->get(i));
95  other->get(i).name = nullptr;
96  } else {
97  delete [] get(target_index).spacing_vec;
98  get(target_index).spacing_vec = other->get(i).spacing_vec;
99  }
100  other->get(i).spacing_vec = nullptr;
101  }
102  }
103 }
104 
105 // Moves this to the target unicity table.
107  target->clear();
110  for (int i = 0; i < size(); ++i) {
111  // Bit copy the FontInfo and steal all the pointers.
112  target->push_back(get(i));
113  get(i).name = nullptr;
114  get(i).spacing_vec = nullptr;
115  }
116 }
117 
118 
119 // Compare FontInfo structures.
120 bool CompareFontInfo(const FontInfo& fi1, const FontInfo& fi2) {
121  // The font properties are required to be the same for two font with the same
122  // name, so there is no need to test them.
123  // Consequently, querying the table with only its font name as information is
124  // enough to retrieve its properties.
125  return strcmp(fi1.name, fi2.name) == 0;
126 }
127 // Compare FontSet structures.
128 bool CompareFontSet(const FontSet& fs1, const FontSet& fs2) {
129  if (fs1.size != fs2.size)
130  return false;
131  for (int i = 0; i < fs1.size; ++i) {
132  if (fs1.configs[i] != fs2.configs[i])
133  return false;
134  }
135  return true;
136 }
137 
138 // Callbacks for GenericVector.
140  if (f.spacing_vec != nullptr) {
141  f.spacing_vec->delete_data_pointers();
142  delete f.spacing_vec;
143  }
144  delete[] f.name;
145 }
147  delete[] fs.configs;
148 }
149 
150 /*---------------------------------------------------------------------------*/
151 // Callbacks used by UnicityTable to read/write FontInfo/FontSet structures.
152 bool read_info(TFile* f, FontInfo* fi) {
153  uint32_t size;
154  if (!f->DeSerialize(&size)) return false;
155  char* font_name = new char[size + 1];
156  fi->name = font_name;
157  if (!f->DeSerialize(font_name, size)) return false;
158  font_name[size] = '\0';
159  return f->DeSerialize(&fi->properties);
160 }
161 
162 bool write_info(FILE* f, const FontInfo& fi) {
163  int32_t size = strlen(fi.name);
164  return tesseract::Serialize(f, &size) &&
165  tesseract::Serialize(f, &fi.name[0], size) &&
167 }
168 
170  int32_t vec_size, kern_size;
171  if (!f->DeSerialize(&vec_size)) return false;
172  ASSERT_HOST(vec_size >= 0);
173  if (vec_size == 0) return true;
174  fi->init_spacing(vec_size);
175  for (int i = 0; i < vec_size; ++i) {
176  FontSpacingInfo *fs = new FontSpacingInfo();
177  if (!f->DeSerialize(&fs->x_gap_before) ||
178  !f->DeSerialize(&fs->x_gap_after) ||
179  !f->DeSerialize(&kern_size)) {
180  delete fs;
181  return false;
182  }
183  if (kern_size < 0) { // indication of a nullptr entry in fi->spacing_vec
184  delete fs;
185  continue;
186  }
187  if (kern_size > 0 && (!fs->kerned_unichar_ids.DeSerialize(f) ||
188  !fs->kerned_x_gaps.DeSerialize(f))) {
189  delete fs;
190  return false;
191  }
192  fi->add_spacing(i, fs);
193  }
194  return true;
195 }
196 
197 bool write_spacing_info(FILE* f, const FontInfo& fi) {
198  int32_t vec_size = (fi.spacing_vec == nullptr) ? 0 : fi.spacing_vec->size();
199  if (!tesseract::Serialize(f, &vec_size)) return false;
200  int16_t x_gap_invalid = -1;
201  for (int i = 0; i < vec_size; ++i) {
202  FontSpacingInfo *fs = fi.spacing_vec->get(i);
203  int32_t kern_size = (fs == nullptr) ? -1 : fs->kerned_x_gaps.size();
204  if (fs == nullptr) {
205  // Writing two invalid x-gaps.
206  if (!tesseract::Serialize(f, &x_gap_invalid, 2) ||
207  !tesseract::Serialize(f, &kern_size)) {
208  return false;
209  }
210  } else {
211  if (!tesseract::Serialize(f, &fs->x_gap_before) ||
212  !tesseract::Serialize(f, &fs->x_gap_after) ||
213  !tesseract::Serialize(f, &kern_size)) {
214  return false;
215  }
216  }
217  if (kern_size > 0 && (!fs->kerned_unichar_ids.Serialize(f) ||
218  !fs->kerned_x_gaps.Serialize(f))) {
219  return false;
220  }
221  }
222  return true;
223 }
224 
225 bool read_set(TFile* f, FontSet* fs) {
226  if (!f->DeSerialize(&fs->size)) return false;
227  fs->configs = new int[fs->size];
228  return f->DeSerialize(&fs->configs[0], fs->size);
229 }
230 
231 bool write_set(FILE* f, const FontSet& fs) {
232  return tesseract::Serialize(f, &fs.size) &&
233  tesseract::Serialize(f, &fs.configs[0], fs.size);
234 }
235 
236 } // namespace tesseract.
void MoveTo(UnicityTable< FontInfo > *target)
Definition: fontinfo.cpp:106
bool write_spacing_info(FILE *f, const FontInfo &fi)
Definition: fontinfo.cpp:197
bool write_info(FILE *f, const FontInfo &fi)
Definition: fontinfo.cpp:162
void add_spacing(UNICHAR_ID uch_id, FontSpacingInfo *spacing_info)
Definition: fontinfo.h:80
int size() const
Definition: genericvector.h:71
void MoveSpacingInfoFrom(FontInfoTable *other)
Definition: fontinfo.cpp:85
bool DeSerialize(char *data, size_t count=1)
Definition: serialis.cpp:103
void set_clear_callback(TessCallback1< FontInfo > *cb)
bool DeSerialize(TFile *fp)
Definition: fontinfo.cpp:54
bool SerializeClasses(FILE *fp) const
bool SetContainsMultipleFontProperties(const GenericVector< ScoredFont > &font_set) const
Definition: fontinfo.cpp:72
GenericVector< int16_t > kerned_x_gaps
Definition: fontinfo.h:55
bool DeSerialize(bool swap, FILE *fp)
bool Serialize(FILE *fp, const char *data, size_t n)
Definition: serialis.cpp:59
_ConstTessMemberResultCallback_0_0< false, R, T1 >::base * NewPermanentTessCallback(const T1 *obj, R(T2::*member)() const)
Definition: tesscallback.h:116
bool CompareFontSet(const FontSet &fs1, const FontSet &fs2)
Definition: fontinfo.cpp:128
uint32_t properties
Definition: fontinfo.h:118
bool Serialize(FILE *fp) const
void set_compare_callback(TessResultCallback2< bool, FontInfo const &, FontInfo const & > *cb)
GenericVector< UNICHAR_ID > kerned_unichar_ids
Definition: fontinfo.h:54
int get_index(const FontInfo &object) const
void init_spacing(int unicharset_size)
Definition: fontinfo.h:73
void set_clear_callback(TessCallback1< T > *cb)
void set_compare_callback(TessResultCallback2< bool, T const &, T const &> *cb)
T & get(int index) const
bool CompareFontInfo(const FontInfo &fi1, const FontInfo &fi2)
Definition: fontinfo.cpp:120
bool read_spacing_info(TFile *f, FontInfo *fi)
Definition: fontinfo.cpp:169
bool DeSerialize(TFile *fp)
Definition: fontinfo.cpp:34
bool SetContainsFontProperties(int font_id, const GenericVector< ScoredFont > &font_set) const
Definition: fontinfo.cpp:61
void FontSetDeleteCallback(FontSet fs)
Definition: fontinfo.cpp:146
bool empty() const
Definition: genericvector.h:90
bool read_info(TFile *f, FontInfo *fi)
Definition: fontinfo.cpp:152
int push_back(T object)
Add an element in the table.
int push_back(FontInfo object)
void FontInfoDeleteCallback(FontInfo f)
Definition: fontinfo.cpp:139
GenericVector< FontSpacingInfo * > * spacing_vec
Definition: fontinfo.h:125
bool DeSerializeClasses(bool swap, FILE *fp)
bool Serialize(FILE *fp) const
Definition: fontinfo.cpp:49
bool Serialize(FILE *fp) const
Definition: fontinfo.cpp:27
bool read_set(TFile *f, FontSet *fs)
Definition: fontinfo.cpp:225
bool write_set(FILE *f, const FontSet &fs)
Definition: fontinfo.cpp:231
#define ASSERT_HOST(x)
Definition: errcode.h:84