All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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.
34 bool FontInfo::DeSerialize(bool swap, FILE* fp) {
35  if (!read_info(fp, this, swap)) return false;
36  if (!read_spacing_info(fp, this, swap)) 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.
54 bool FontInfoTable::DeSerialize(bool swap, FILE* fp) {
55  truncate(0);
56  return this->DeSerializeClasses(swap, 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 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 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 != NULL) {
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 = NULL;
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 = NULL;
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 = NULL;
114  get(i).spacing_vec = NULL;
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 != NULL) {
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(FILE* f, FontInfo* fi, bool swap) {
153  inT32 size;
154  if (fread(&size, sizeof(size), 1, f) != 1) return false;
155  if (swap)
156  Reverse32(&size);
157  char* font_name = new char[size + 1];
158  fi->name = font_name;
159  if (static_cast<int>(fread(font_name, sizeof(*font_name), size, f)) != size)
160  return false;
161  font_name[size] = '\0';
162  if (fread(&fi->properties, sizeof(fi->properties), 1, f) != 1) return false;
163  if (swap)
164  Reverse32(&fi->properties);
165  return true;
166 }
167 
168 bool write_info(FILE* f, const FontInfo& fi) {
169  inT32 size = strlen(fi.name);
170  if (fwrite(&size, sizeof(size), 1, f) != 1) return false;
171  if (static_cast<int>(fwrite(fi.name, sizeof(*fi.name), size, f)) != size)
172  return false;
173  if (fwrite(&fi.properties, sizeof(fi.properties), 1, f) != 1) return false;
174  return true;
175 }
176 
177 bool read_spacing_info(FILE *f, FontInfo* fi, bool swap) {
178  inT32 vec_size, kern_size;
179  if (fread(&vec_size, sizeof(vec_size), 1, f) != 1) return false;
180  if (swap) Reverse32(&vec_size);
181  ASSERT_HOST(vec_size >= 0);
182  if (vec_size == 0) return true;
183  fi->init_spacing(vec_size);
184  for (int i = 0; i < vec_size; ++i) {
185  FontSpacingInfo *fs = new FontSpacingInfo();
186  if (fread(&fs->x_gap_before, sizeof(fs->x_gap_before), 1, f) != 1 ||
187  fread(&fs->x_gap_after, sizeof(fs->x_gap_after), 1, f) != 1 ||
188  fread(&kern_size, sizeof(kern_size), 1, f) != 1) {
189  delete fs;
190  return false;
191  }
192  if (swap) {
193  ReverseN(&(fs->x_gap_before), sizeof(fs->x_gap_before));
194  ReverseN(&(fs->x_gap_after), sizeof(fs->x_gap_after));
195  Reverse32(&kern_size);
196  }
197  if (kern_size < 0) { // indication of a NULL entry in fi->spacing_vec
198  delete fs;
199  continue;
200  }
201  if (kern_size > 0 && (!fs->kerned_unichar_ids.DeSerialize(swap, f) ||
202  !fs->kerned_x_gaps.DeSerialize(swap, f))) {
203  delete fs;
204  return false;
205  }
206  fi->add_spacing(i, fs);
207  }
208  return true;
209 }
210 
211 bool write_spacing_info(FILE* f, const FontInfo& fi) {
212  inT32 vec_size = (fi.spacing_vec == NULL) ? 0 : fi.spacing_vec->size();
213  if (fwrite(&vec_size, sizeof(vec_size), 1, f) != 1) return false;
214  inT16 x_gap_invalid = -1;
215  for (int i = 0; i < vec_size; ++i) {
216  FontSpacingInfo *fs = fi.spacing_vec->get(i);
217  inT32 kern_size = (fs == NULL) ? -1 : fs->kerned_x_gaps.size();
218  if (fs == NULL) {
219  // Valid to have the identical fwrites. Writing invalid x-gaps.
220  if (fwrite(&(x_gap_invalid), sizeof(x_gap_invalid), 1, f) != 1 ||
221  fwrite(&(x_gap_invalid), sizeof(x_gap_invalid), 1, f) != 1 ||
222  fwrite(&kern_size, sizeof(kern_size), 1, f) != 1) {
223  return false;
224  }
225  } else {
226  if (fwrite(&(fs->x_gap_before), sizeof(fs->x_gap_before), 1, f) != 1 ||
227  fwrite(&(fs->x_gap_after), sizeof(fs->x_gap_after), 1, f) != 1 ||
228  fwrite(&kern_size, sizeof(kern_size), 1, f) != 1) {
229  return false;
230  }
231  }
232  if (kern_size > 0 && (!fs->kerned_unichar_ids.Serialize(f) ||
233  !fs->kerned_x_gaps.Serialize(f))) {
234  return false;
235  }
236  }
237  return true;
238 }
239 
240 bool read_set(FILE* f, FontSet* fs, bool swap) {
241  if (fread(&fs->size, sizeof(fs->size), 1, f) != 1) return false;
242  if (swap)
243  Reverse32(&fs->size);
244  fs->configs = new int[fs->size];
245  for (int i = 0; i < fs->size; ++i) {
246  if (fread(&fs->configs[i], sizeof(fs->configs[i]), 1, f) != 1) return false;
247  if (swap)
248  Reverse32(&fs->configs[i]);
249  }
250  return true;
251 }
252 
253 bool write_set(FILE* f, const FontSet& fs) {
254  if (fwrite(&fs.size, sizeof(fs.size), 1, f) != 1) return false;
255  for (int i = 0; i < fs.size; ++i) {
256  if (fwrite(&fs.configs[i], sizeof(fs.configs[i]), 1, f) != 1) return false;
257  }
258  return true;
259 }
260 
261 } // namespace tesseract.
262 
void set_compare_callback(TessResultCallback2< bool, T const &, T const & > *cb)
int size() const
Definition: genericvector.h:72
bool DeSerializeClasses(bool swap, FILE *fp)
bool write_info(FILE *f, const FontInfo &fi)
Definition: fontinfo.cpp:168
bool read_info(FILE *f, FontInfo *fi, bool swap)
Definition: fontinfo.cpp:152
int push_back(FontInfoobject)
void Reverse32(void *ptr)
Definition: helpers.h:193
void set_compare_callback(TessResultCallback2< bool, FontInfoconst &, FontInfoconst & > *cb)
bool read_set(FILE *f, FontSet *fs, bool swap)
Definition: fontinfo.cpp:240
bool Serialize(FILE *fp) const
bool CompareFontInfo(const FontInfo &fi1, const FontInfo &fi2)
Definition: fontinfo.cpp:120
bool CompareFontSet(const FontSet &fs1, const FontSet &fs2)
Definition: fontinfo.cpp:128
#define ASSERT_HOST(x)
Definition: errcode.h:84
bool DeSerialize(bool swap, FILE *fp)
Definition: fontinfo.cpp:34
bool write_spacing_info(FILE *f, const FontInfo &fi)
Definition: fontinfo.cpp:211
bool SerializeClasses(FILE *fp) const
GenericVector< UNICHAR_ID > kerned_unichar_ids
Definition: fontinfo.h:54
bool SetContainsMultipleFontProperties(const GenericVector< ScoredFont > &font_set) const
Definition: fontinfo.cpp:72
unsigned int uinT32
Definition: host.h:103
name_table name
void FontSetDeleteCallback(FontSet fs)
Definition: fontinfo.cpp:146
bool DeSerialize(bool swap, FILE *fp)
int push_back(T object)
Add an element in the table.
_ConstTessMemberResultCallback_0_0< false, R, T1 >::base * NewPermanentTessCallback(const T1 *obj, R(T2::*member)() const)
Definition: tesscallback.h:116
void add_spacing(UNICHAR_ID uch_id, FontSpacingInfo *spacing_info)
Definition: fontinfo.h:80
void FontInfoDeleteCallback(FontInfo f)
Definition: fontinfo.cpp:139
void MoveTo(UnicityTable< FontInfo > *target)
Definition: fontinfo.cpp:106
bool empty() const
Definition: genericvector.h:84
bool Serialize(FILE *fp) const
Definition: fontinfo.cpp:49
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:177
bool DeSerialize(bool swap, FILE *fp)
Definition: fontinfo.cpp:54
GenericVector< FontSpacingInfo * > * spacing_vec
Definition: fontinfo.h:125
void set_clear_callback(TessCallback1< T > *cb)
void init_spacing(int unicharset_size)
Definition: fontinfo.h:73
#define NULL
Definition: host.h:144
void MoveSpacingInfoFrom(FontInfoTable *other)
Definition: fontinfo.cpp:85
bool SetContainsFontProperties(int font_id, const GenericVector< ScoredFont > &font_set) const
Definition: fontinfo.cpp:61
bool read_spacing_info(FILE *f, FontInfo *fi, bool swap)
Definition: fontinfo.cpp:177
int get_index(FontInfoobject) const
bool Serialize(FILE *fp) const
Definition: fontinfo.cpp:27
T & get(int index) const
void set_clear_callback(TessCallback1< FontInfo > *cb)
bool write_set(FILE *f, const FontSet &fs)
Definition: fontinfo.cpp:253
short inT16
Definition: host.h:100
int inT32
Definition: host.h:102
GenericVector< inT16 > kerned_x_gaps
Definition: fontinfo.h:55