Tesseract
3.02
|
00001 00002 // File: fontinfo.cpp 00003 // Description: Font information classes abstracted from intproto.h/cpp. 00004 // Author: rays@google.com (Ray Smith) 00005 // Created: Wed May 18 10:39:01 PDT 2011 00006 // 00007 // (C) Copyright 2011, Google Inc. 00008 // Licensed under the Apache License, Version 2.0 (the "License"); 00009 // you may not use this file except in compliance with the License. 00010 // You may obtain a copy of the License at 00011 // http://www.apache.org/licenses/LICENSE-2.0 00012 // Unless required by applicable law or agreed to in writing, software 00013 // distributed under the License is distributed on an "AS IS" BASIS, 00014 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00015 // See the License for the specific language governing permissions and 00016 // limitations under the License. 00017 // 00019 00020 #include "fontinfo.h" 00021 00022 namespace tesseract { 00023 00024 // Compare FontInfo structures. 00025 bool CompareFontInfo(const FontInfo& fi1, const FontInfo& fi2) { 00026 // The font properties are required to be the same for two font with the same 00027 // name, so there is no need to test them. 00028 // Consequently, querying the table with only its font name as information is 00029 // enough to retrieve its properties. 00030 return strcmp(fi1.name, fi2.name) == 0; 00031 } 00032 // Compare FontSet structures. 00033 bool CompareFontSet(const FontSet& fs1, const FontSet& fs2) { 00034 if (fs1.size != fs2.size) 00035 return false; 00036 for (int i = 0; i < fs1.size; ++i) { 00037 if (fs1.configs[i] != fs2.configs[i]) 00038 return false; 00039 } 00040 return true; 00041 } 00042 00043 // Callbacks for GenericVector. 00044 void FontInfoDeleteCallback(FontInfo f) { 00045 if (f.spacing_vec != NULL) { 00046 f.spacing_vec->delete_data_pointers(); 00047 delete f.spacing_vec; 00048 } 00049 delete[] f.name; 00050 } 00051 void FontSetDeleteCallback(FontSet fs) { 00052 delete[] fs.configs; 00053 } 00054 00055 /*---------------------------------------------------------------------------*/ 00056 // Callbacks used by UnicityTable to read/write FontInfo/FontSet structures. 00057 bool read_info(FILE* f, FontInfo* fi, bool swap) { 00058 inT32 size; 00059 if (fread(&size, sizeof(size), 1, f) != 1) return false; 00060 if (swap) 00061 Reverse32(&size); 00062 char* font_name = new char[size + 1]; 00063 fi->name = font_name; 00064 if (fread(font_name, sizeof(*font_name), size, f) != size) return false; 00065 font_name[size] = '\0'; 00066 if (fread(&fi->properties, sizeof(fi->properties), 1, f) != 1) return false; 00067 if (swap) 00068 Reverse32(&fi->properties); 00069 return true; 00070 } 00071 00072 bool write_info(FILE* f, const FontInfo& fi) { 00073 inT32 size = strlen(fi.name); 00074 if (fwrite(&size, sizeof(size), 1, f) != 1) return false; 00075 if (fwrite(fi.name, sizeof(*fi.name), size, f) != size) return false; 00076 if (fwrite(&fi.properties, sizeof(fi.properties), 1, f) != 1) return false; 00077 return true; 00078 } 00079 00080 bool read_spacing_info(FILE *f, FontInfo* fi, bool swap) { 00081 inT32 vec_size, kern_size; 00082 if (fread(&vec_size, sizeof(vec_size), 1, f) != 1) return false; 00083 if (swap) Reverse32(&vec_size); 00084 ASSERT_HOST(vec_size >= 0); 00085 if (vec_size == 0) return true; 00086 fi->init_spacing(vec_size); 00087 for (int i = 0; i < vec_size; ++i) { 00088 FontSpacingInfo *fs = new FontSpacingInfo(); 00089 if (fread(&fs->x_gap_before, sizeof(fs->x_gap_before), 1, f) != 1 || 00090 fread(&fs->x_gap_after, sizeof(fs->x_gap_after), 1, f) != 1 || 00091 fread(&kern_size, sizeof(kern_size), 1, f) != 1) { 00092 return false; 00093 } 00094 if (swap) { 00095 ReverseN(&(fs->x_gap_before), sizeof(fs->x_gap_before)); 00096 ReverseN(&(fs->x_gap_after), sizeof(fs->x_gap_after)); 00097 Reverse32(&kern_size); 00098 } 00099 if (kern_size < 0) { // indication of a NULL entry in fi->spacing_vec 00100 delete fs; 00101 continue; 00102 } 00103 if (kern_size > 0 && (!fs->kerned_unichar_ids.DeSerialize(swap, f) || 00104 !fs->kerned_x_gaps.DeSerialize(swap, f))) { 00105 return false; 00106 } 00107 fi->add_spacing(i, fs); 00108 } 00109 return true; 00110 } 00111 00112 bool write_spacing_info(FILE* f, const FontInfo& fi) { 00113 inT32 vec_size = (fi.spacing_vec == NULL) ? 0 : fi.spacing_vec->size(); 00114 if (fwrite(&vec_size, sizeof(vec_size), 1, f) != 1) return false; 00115 inT16 x_gap_invalid = -1; 00116 for (int i = 0; i < vec_size; ++i) { 00117 FontSpacingInfo *fs = fi.spacing_vec->get(i); 00118 inT32 kern_size = (fs == NULL) ? -1 : fs->kerned_x_gaps.size(); 00119 if (fs == NULL) { 00120 if (fwrite(&(x_gap_invalid), sizeof(x_gap_invalid), 1, f) != 1 || 00121 fwrite(&(x_gap_invalid), sizeof(x_gap_invalid), 1, f) != 1 || 00122 fwrite(&kern_size, sizeof(kern_size), 1, f) != 1) { 00123 return false; 00124 } 00125 } else { 00126 if (fwrite(&(fs->x_gap_before), sizeof(fs->x_gap_before), 1, f) != 1 || 00127 fwrite(&(fs->x_gap_after), sizeof(fs->x_gap_after), 1, f) != 1 || 00128 fwrite(&kern_size, sizeof(kern_size), 1, f) != 1) { 00129 return false; 00130 } 00131 } 00132 if (kern_size > 0 && (!fs->kerned_unichar_ids.Serialize(f) || 00133 !fs->kerned_x_gaps.Serialize(f))) { 00134 return false; 00135 } 00136 } 00137 return true; 00138 } 00139 00140 bool read_set(FILE* f, FontSet* fs, bool swap) { 00141 if (fread(&fs->size, sizeof(fs->size), 1, f) != 1) return false; 00142 if (swap) 00143 Reverse32(&fs->size); 00144 fs->configs = new int[fs->size]; 00145 for (int i = 0; i < fs->size; ++i) { 00146 if (fread(&fs->configs[i], sizeof(fs->configs[i]), 1, f) != 1) return false; 00147 if (swap) 00148 Reverse32(&fs->configs[i]); 00149 } 00150 return true; 00151 } 00152 00153 bool write_set(FILE* f, const FontSet& fs) { 00154 if (fwrite(&fs.size, sizeof(fs.size), 1, f) != 1) return false; 00155 for (int i = 0; i < fs.size; ++i) { 00156 if (fwrite(&fs.configs[i], sizeof(fs.configs[i]), 1, f) != 1) return false; 00157 } 00158 return true; 00159 } 00160 00161 } // namespace tesseract. 00162