Tesseract  3.02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
fontinfo.cpp
Go to the documentation of this file.
1 
2 // 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 
22 namespace tesseract {
23 
24 // Compare FontInfo structures.
25 bool CompareFontInfo(const FontInfo& fi1, const FontInfo& fi2) {
26  // The font properties are required to be the same for two font with the same
27  // name, so there is no need to test them.
28  // Consequently, querying the table with only its font name as information is
29  // enough to retrieve its properties.
30  return strcmp(fi1.name, fi2.name) == 0;
31 }
32 // Compare FontSet structures.
33 bool CompareFontSet(const FontSet& fs1, const FontSet& fs2) {
34  if (fs1.size != fs2.size)
35  return false;
36  for (int i = 0; i < fs1.size; ++i) {
37  if (fs1.configs[i] != fs2.configs[i])
38  return false;
39  }
40  return true;
41 }
42 
43 // Callbacks for GenericVector.
45  if (f.spacing_vec != NULL) {
47  delete f.spacing_vec;
48  }
49  delete[] f.name;
50 }
52  delete[] fs.configs;
53 }
54 
55 /*---------------------------------------------------------------------------*/
56 // Callbacks used by UnicityTable to read/write FontInfo/FontSet structures.
57 bool read_info(FILE* f, FontInfo* fi, bool swap) {
58  inT32 size;
59  if (fread(&size, sizeof(size), 1, f) != 1) return false;
60  if (swap)
61  Reverse32(&size);
62  char* font_name = new char[size + 1];
63  fi->name = font_name;
64  if (fread(font_name, sizeof(*font_name), size, f) != size) return false;
65  font_name[size] = '\0';
66  if (fread(&fi->properties, sizeof(fi->properties), 1, f) != 1) return false;
67  if (swap)
68  Reverse32(&fi->properties);
69  return true;
70 }
71 
72 bool write_info(FILE* f, const FontInfo& fi) {
73  inT32 size = strlen(fi.name);
74  if (fwrite(&size, sizeof(size), 1, f) != 1) return false;
75  if (fwrite(fi.name, sizeof(*fi.name), size, f) != size) return false;
76  if (fwrite(&fi.properties, sizeof(fi.properties), 1, f) != 1) return false;
77  return true;
78 }
79 
80 bool read_spacing_info(FILE *f, FontInfo* fi, bool swap) {
81  inT32 vec_size, kern_size;
82  if (fread(&vec_size, sizeof(vec_size), 1, f) != 1) return false;
83  if (swap) Reverse32(&vec_size);
84  ASSERT_HOST(vec_size >= 0);
85  if (vec_size == 0) return true;
86  fi->init_spacing(vec_size);
87  for (int i = 0; i < vec_size; ++i) {
88  FontSpacingInfo *fs = new FontSpacingInfo();
89  if (fread(&fs->x_gap_before, sizeof(fs->x_gap_before), 1, f) != 1 ||
90  fread(&fs->x_gap_after, sizeof(fs->x_gap_after), 1, f) != 1 ||
91  fread(&kern_size, sizeof(kern_size), 1, f) != 1) {
92  return false;
93  }
94  if (swap) {
95  ReverseN(&(fs->x_gap_before), sizeof(fs->x_gap_before));
96  ReverseN(&(fs->x_gap_after), sizeof(fs->x_gap_after));
97  Reverse32(&kern_size);
98  }
99  if (kern_size < 0) { // indication of a NULL entry in fi->spacing_vec
100  delete fs;
101  continue;
102  }
103  if (kern_size > 0 && (!fs->kerned_unichar_ids.DeSerialize(swap, f) ||
104  !fs->kerned_x_gaps.DeSerialize(swap, f))) {
105  return false;
106  }
107  fi->add_spacing(i, fs);
108  }
109  return true;
110 }
111 
112 bool write_spacing_info(FILE* f, const FontInfo& fi) {
113  inT32 vec_size = (fi.spacing_vec == NULL) ? 0 : fi.spacing_vec->size();
114  if (fwrite(&vec_size, sizeof(vec_size), 1, f) != 1) return false;
115  inT16 x_gap_invalid = -1;
116  for (int i = 0; i < vec_size; ++i) {
117  FontSpacingInfo *fs = fi.spacing_vec->get(i);
118  inT32 kern_size = (fs == NULL) ? -1 : fs->kerned_x_gaps.size();
119  if (fs == NULL) {
120  if (fwrite(&(x_gap_invalid), sizeof(x_gap_invalid), 1, f) != 1 ||
121  fwrite(&(x_gap_invalid), sizeof(x_gap_invalid), 1, f) != 1 ||
122  fwrite(&kern_size, sizeof(kern_size), 1, f) != 1) {
123  return false;
124  }
125  } else {
126  if (fwrite(&(fs->x_gap_before), sizeof(fs->x_gap_before), 1, f) != 1 ||
127  fwrite(&(fs->x_gap_after), sizeof(fs->x_gap_after), 1, f) != 1 ||
128  fwrite(&kern_size, sizeof(kern_size), 1, f) != 1) {
129  return false;
130  }
131  }
132  if (kern_size > 0 && (!fs->kerned_unichar_ids.Serialize(f) ||
133  !fs->kerned_x_gaps.Serialize(f))) {
134  return false;
135  }
136  }
137  return true;
138 }
139 
140 bool read_set(FILE* f, FontSet* fs, bool swap) {
141  if (fread(&fs->size, sizeof(fs->size), 1, f) != 1) return false;
142  if (swap)
143  Reverse32(&fs->size);
144  fs->configs = new int[fs->size];
145  for (int i = 0; i < fs->size; ++i) {
146  if (fread(&fs->configs[i], sizeof(fs->configs[i]), 1, f) != 1) return false;
147  if (swap)
148  Reverse32(&fs->configs[i]);
149  }
150  return true;
151 }
152 
153 bool write_set(FILE* f, const FontSet& fs) {
154  if (fwrite(&fs.size, sizeof(fs.size), 1, f) != 1) return false;
155  for (int i = 0; i < fs.size; ++i) {
156  if (fwrite(&fs.configs[i], sizeof(fs.configs[i]), 1, f) != 1) return false;
157  }
158  return true;
159 }
160 
161 } // namespace tesseract.
162