tesseract  4.00.00dev
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 
41  set_compare_callback(NewPermanentTessCallback(CompareFontInfo));
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 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.
86  set_compare_callback(NewPermanentTessCallback(CompareFontInfo));
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(TFile* f, FontInfo* fi) {
153  inT32 size;
154  if (f->FReadEndian(&size, sizeof(size), 1) != 1) return false;
155  char* font_name = new char[size + 1];
156  fi->name = font_name;
157  if (f->FRead(font_name, sizeof(*font_name), size) != size) return false;
158  font_name[size] = '\0';
159  if (f->FReadEndian(&fi->properties, sizeof(fi->properties), 1) != 1)
160  return false;
161  return true;
162 }
163 
164 bool write_info(FILE* f, const FontInfo& fi) {
165  inT32 size = strlen(fi.name);
166  if (fwrite(&size, sizeof(size), 1, f) != 1) return false;
167  if (static_cast<int>(fwrite(fi.name, sizeof(*fi.name), size, f)) != size)
168  return false;
169  if (fwrite(&fi.properties, sizeof(fi.properties), 1, f) != 1) return false;
170  return true;
171 }
172 
174  inT32 vec_size, kern_size;
175  if (f->FReadEndian(&vec_size, sizeof(vec_size), 1) != 1) 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  FontSpacingInfo *fs = new FontSpacingInfo();
181  if (f->FReadEndian(&fs->x_gap_before, sizeof(fs->x_gap_before), 1) != 1 ||
182  f->FReadEndian(&fs->x_gap_after, sizeof(fs->x_gap_after), 1) != 1 ||
183  f->FReadEndian(&kern_size, sizeof(kern_size), 1) != 1) {
184  delete fs;
185  return false;
186  }
187  if (kern_size < 0) { // indication of a NULL 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 vec_size = (fi.spacing_vec == NULL) ? 0 : fi.spacing_vec->size();
203  if (fwrite(&vec_size, sizeof(vec_size), 1, f) != 1) return false;
204  inT16 x_gap_invalid = -1;
205  for (int i = 0; i < vec_size; ++i) {
206  FontSpacingInfo *fs = fi.spacing_vec->get(i);
207  inT32 kern_size = (fs == NULL) ? -1 : fs->kerned_x_gaps.size();
208  if (fs == NULL) {
209  // Valid to have the identical fwrites. Writing invalid x-gaps.
210  if (fwrite(&(x_gap_invalid), sizeof(x_gap_invalid), 1, f) != 1 ||
211  fwrite(&(x_gap_invalid), sizeof(x_gap_invalid), 1, f) != 1 ||
212  fwrite(&kern_size, sizeof(kern_size), 1, f) != 1) {
213  return false;
214  }
215  } else {
216  if (fwrite(&(fs->x_gap_before), sizeof(fs->x_gap_before), 1, f) != 1 ||
217  fwrite(&(fs->x_gap_after), sizeof(fs->x_gap_after), 1, f) != 1 ||
218  fwrite(&kern_size, sizeof(kern_size), 1, f) != 1) {
219  return false;
220  }
221  }
222  if (kern_size > 0 && (!fs->kerned_unichar_ids.Serialize(f) ||
223  !fs->kerned_x_gaps.Serialize(f))) {
224  return false;
225  }
226  }
227  return true;
228 }
229 
230 bool read_set(TFile* f, FontSet* fs) {
231  if (f->FReadEndian(&fs->size, sizeof(fs->size), 1) != 1) return false;
232  fs->configs = new int[fs->size];
233  if (f->FReadEndian(fs->configs, sizeof(fs->configs[0]), fs->size) != fs->size)
234  return false;
235  return true;
236 }
237 
238 bool write_set(FILE* f, const FontSet& fs) {
239  if (fwrite(&fs.size, sizeof(fs.size), 1, f) != 1) return false;
240  for (int i = 0; i < fs.size; ++i) {
241  if (fwrite(&fs.configs[i], sizeof(fs.configs[i]), 1, f) != 1) return false;
242  }
243  return true;
244 }
245 
246 } // namespace tesseract.
247 
void MoveTo(UnicityTable< FontInfo > *target)
Definition: fontinfo.cpp:106
bool DeSerialize(bool swap, FILE *fp)
void add_spacing(UNICHAR_ID uch_id, FontSpacingInfo *spacing_info)
Definition: fontinfo.h:80
bool read_spacing_info(TFile *f, FontInfo *fi)
Definition: fontinfo.cpp:173
int push_back(T object)
Add an element in the table.
bool read_set(TFile *f, FontSet *fs)
Definition: fontinfo.cpp:230
int32_t inT32
Definition: host.h:38
_ConstTessMemberResultCallback_0_0< false, R, T1 >::base * NewPermanentTessCallback(const T1 *obj, R(T2::*member)() const)
Definition: tesscallback.h:116
bool write_set(FILE *f, const FontSet &fs)
Definition: fontinfo.cpp:238
voidpf void uLong size
Definition: ioapi.h:39
GenericVector< FontSpacingInfo * > * spacing_vec
Definition: fontinfo.h:125
GenericVector< UNICHAR_ID > kerned_unichar_ids
Definition: fontinfo.h:54
int FReadEndian(void *buffer, int size, int count)
Definition: serialis.cpp:97
bool CompareFontInfo(const FontInfo &fi1, const FontInfo &fi2)
Definition: fontinfo.cpp:120
bool empty() const
Definition: genericvector.h:90
bool DeSerialize(TFile *fp)
Definition: fontinfo.cpp:34
int size() const
Definition: genericvector.h:72
int16_t inT16
Definition: host.h:36
#define ASSERT_HOST(x)
Definition: errcode.h:84
void init_spacing(int unicharset_size)
Definition: fontinfo.h:73
uint32_t uinT32
Definition: host.h:39
bool write_info(FILE *f, const FontInfo &fi)
Definition: fontinfo.cpp:164
bool read_info(TFile *f, FontInfo *fi)
Definition: fontinfo.cpp:152
T & get(int index) const
void MoveSpacingInfoFrom(FontInfoTable *other)
Definition: fontinfo.cpp:85
GenericVector< inT16 > kerned_x_gaps
Definition: fontinfo.h:55
bool write_spacing_info(FILE *f, const FontInfo &fi)
Definition: fontinfo.cpp:201
bool CompareFontSet(const FontSet &fs1, const FontSet &fs2)
Definition: fontinfo.cpp:128
bool Serialize(FILE *fp) const
bool Serialize(FILE *fp) const
Definition: fontinfo.cpp:27
void FontInfoDeleteCallback(FontInfo f)
Definition: fontinfo.cpp:139
void set_compare_callback(TessResultCallback2< bool, T const &, T const &> *cb)
bool Serialize(FILE *fp) const
Definition: fontinfo.cpp:49
void set_clear_callback(TessCallback1< T > *cb)
bool SetContainsFontProperties(int font_id, const GenericVector< ScoredFont > &font_set) const
Definition: fontinfo.cpp:61
bool SetContainsMultipleFontProperties(const GenericVector< ScoredFont > &font_set) const
Definition: fontinfo.cpp:72
void FontSetDeleteCallback(FontSet fs)
Definition: fontinfo.cpp:146
bool DeSerialize(TFile *fp)
Definition: fontinfo.cpp:54
int FRead(void *buffer, int size, int count)
Definition: serialis.cpp:108