tesseract  4.0.0-1-g2a2b
unicharset.cpp
Go to the documentation of this file.
1 // File: unicharset.cpp
3 // Description: Unicode character/ligature set class.
4 // Author: Thomas Kielbus
5 // Created: Wed Jun 28 17:05:01 PDT 2006
6 //
7 // (C) Copyright 2006, 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 "unicharset.h"
21 
22 #include <algorithm>
23 #include <cassert>
24 #include <cstdio>
25 #include <cstring>
26 
27 #include "params.h"
28 #include "serialis.h"
29 #include "tesscallback.h"
30 #include "tprintf.h"
31 #include "unichar.h"
32 
33 // TODO(rays) Move UNICHARSET to tesseract namespace.
34 using tesseract::char32;
35 using tesseract::UNICHAR;
36 
37 // Special character used in representing character fragments.
38 static const char kSeparator = '|';
39 // Special character used in representing 'natural' character fragments.
40 static const char kNaturalFlag = 'n';
41 
42 static const int ISALPHA_MASK = 0x1;
43 static const int ISLOWER_MASK = 0x2;
44 static const int ISUPPER_MASK = 0x4;
45 static const int ISDIGIT_MASK = 0x8;
46 static const int ISPUNCTUATION_MASK = 0x10;
47 
48 // Y coordinate threshold for determining cap-height vs x-height.
49 // TODO(rays) Bring the global definition down to the ccutil library level,
50 // so this constant is relative to some other constants.
51 static const int kMeanlineThreshold = 220;
52 // Let C be the number of alpha chars for which all tops exceed
53 // kMeanlineThreshold, and X the number of alpha chars for which all
54 // tops are below kMeanlineThreshold, then if X > C *
55 // kMinXHeightFraction and C > X * kMinCapHeightFraction or more than
56 // half the alpha characters have upper or lower case, then the
57 // unicharset "has x-height".
58 const double kMinXHeightFraction = 0.25;
59 const double kMinCapHeightFraction = 0.05;
60 
61 /*static */
62 const char* UNICHARSET::kCustomLigatures[][2] = {
63  {"ct", "\uE003"}, // c + t -> U+E003
64  {"ſh", "\uE006"}, // long-s + h -> U+E006
65  {"ſi", "\uE007"}, // long-s + i -> U+E007
66  {"ſl", "\uE008"}, // long-s + l -> U+E008
67  {"ſſ", "\uE009"}, // long-s + long-s -> U+E009
68  {nullptr, nullptr}
69 };
70 
71 // List of mappings to make when ingesting strings from the outside.
72 // The substitutions clean up text that should exist for rendering of
73 // synthetic data, but not in the recognition set.
74 const char* UNICHARSET::kCleanupMaps[][2] = {
75  {"\u0640", ""}, // TATWEEL is deleted.
76  {"\ufb01", "fi"}, // fi ligature->fi pair.
77  {"\ufb02", "fl"}, // fl ligature->fl pair.
78  {nullptr, nullptr}};
79 
80 // List of strings for the SpecialUnicharCodes. Keep in sync with the enum.
82  " ",
83  "Joined",
84  "|Broken|0|1"
85 };
86 
87 const char* UNICHARSET::null_script = "NULL";
88 
89 UNICHARSET::UNICHAR_PROPERTIES::UNICHAR_PROPERTIES() {
90  Init();
91 }
92 
93 // Initialize all properties to sensible default values.
94 void UNICHARSET::UNICHAR_PROPERTIES::Init() {
95  isalpha = false;
96  islower = false;
97  isupper = false;
98  isdigit = false;
99  ispunctuation = false;
100  isngram = false;
101  enabled = false;
102  SetRangesOpen();
103  script_id = 0;
104  other_case = 0;
105  mirror = 0;
106  normed = "";
108  fragment = nullptr;
109 }
110 
111 // Sets all ranges wide open. Initialization default in case there are
112 // no useful values available.
113 void UNICHARSET::UNICHAR_PROPERTIES::SetRangesOpen() {
114  min_bottom = 0;
115  max_bottom = UINT8_MAX;
116  min_top = 0;
117  max_top = UINT8_MAX;
118  width = 0.0f;
119  width_sd = 0.0f;
120  bearing = 0.0f;
121  bearing_sd = 0.0f;
122  advance = 0.0f;
123  advance_sd = 0.0f;
124 }
125 
126 // Sets all ranges to empty. Used before expanding with font-based data.
127 void UNICHARSET::UNICHAR_PROPERTIES::SetRangesEmpty() {
128  min_bottom = UINT8_MAX;
129  max_bottom = 0;
130  min_top = UINT8_MAX;
131  max_top = 0;
132  width = 0.0f;
133  width_sd = 0.0f;
134  bearing = 0.0f;
135  bearing_sd = 0.0f;
136  advance = 0.0f;
137  advance_sd = 0.0f;
138 }
139 
140 // Returns true if any of the top/bottom/width/bearing/advance ranges/stats
141 // is empty.
142 bool UNICHARSET::UNICHAR_PROPERTIES::AnyRangeEmpty() const {
143  return width == 0.0f || advance == 0.0f;
144 }
145 
146 // Expands the ranges with the ranges from the src properties.
147 void UNICHARSET::UNICHAR_PROPERTIES::ExpandRangesFrom(
148  const UNICHAR_PROPERTIES& src) {
149  UpdateRange(src.min_bottom, &min_bottom, &max_bottom);
150  UpdateRange(src.max_bottom, &min_bottom, &max_bottom);
151  UpdateRange(src.min_top, &min_top, &max_top);
152  UpdateRange(src.max_top, &min_top, &max_top);
153  if (src.width_sd > width_sd) {
154  width = src.width;
155  width_sd = src.width_sd;
156  }
157  if (src.bearing_sd > bearing_sd) {
158  bearing = src.bearing;
159  bearing_sd = src.bearing_sd;
160  }
161  if (src.advance_sd > advance_sd) {
162  advance = src.advance;
163  advance_sd = src.advance_sd;
164  }
165 }
166 
167 // Copies the properties from src into this.
168 void UNICHARSET::UNICHAR_PROPERTIES::CopyFrom(const UNICHAR_PROPERTIES& src) {
169  // Apart from the fragment, everything else can be done with a default copy.
170  CHAR_FRAGMENT* saved_fragment = fragment;
171  *this = src; // Bitwise copy.
172  fragment = saved_fragment;
173 }
174 
176  unichars(nullptr),
177  ids(),
178  size_used(0),
179  size_reserved(0),
180  script_table(nullptr),
181  script_table_size_used(0) {
182  clear();
183  for (int i = 0; i < SPECIAL_UNICHAR_CODES_COUNT; ++i) {
185  if (i == UNICHAR_JOINED)
186  set_isngram(i, true);
187  }
188 }
189 
191  clear();
192 }
193 
194 void UNICHARSET::reserve(int unichars_number) {
195  if (unichars_number > size_reserved) {
196  UNICHAR_SLOT* unichars_new = new UNICHAR_SLOT[unichars_number];
197  for (int i = 0; i < size_used; ++i)
198  unichars_new[i] = unichars[i];
199  for (int j = size_used; j < unichars_number; ++j) {
200  unichars_new[j].properties.script_id = add_script(null_script);
201  }
202  delete[] unichars;
203  unichars = unichars_new;
204  size_reserved = unichars_number;
205  }
206 }
207 
209 UNICHARSET::unichar_to_id(const char* const unichar_repr) const {
210  std::string cleaned =
211  old_style_included_ ? unichar_repr : CleanupString(unichar_repr);
212  return ids.contains(cleaned.data(), cleaned.size())
213  ? ids.unichar_to_id(cleaned.data(), cleaned.size())
214  : INVALID_UNICHAR_ID;
215 }
216 
217 UNICHAR_ID UNICHARSET::unichar_to_id(const char* const unichar_repr,
218  int length) const {
219  assert(length > 0 && length <= UNICHAR_LEN);
220  std::string cleaned(unichar_repr, length);
221  if (!old_style_included_) cleaned = CleanupString(unichar_repr, length);
222  return ids.contains(cleaned.data(), cleaned.size())
223  ? ids.unichar_to_id(cleaned.data(), cleaned.size())
224  : INVALID_UNICHAR_ID;
225 }
226 
227 // Return the minimum number of bytes that matches a legal UNICHAR_ID,
228 // while leaving the rest of the string encodable. Returns 0 if the
229 // beginning of the string is not encodable.
230 // WARNING: this function now encodes the whole string for precision.
231 // Use encode_string in preference to repeatedly calling step.
232 int UNICHARSET::step(const char* str) const {
233  GenericVector<UNICHAR_ID> encoding;
234  GenericVector<char> lengths;
235  encode_string(str, true, &encoding, &lengths, nullptr);
236  if (encoding.empty() || encoding[0] == INVALID_UNICHAR_ID) return 0;
237  return lengths[0];
238 }
239 
240 // Return whether the given UTF-8 string is encodable with this UNICHARSET.
241 // If not encodable, write the first byte offset which cannot be converted
242 // into the second (return) argument.
243 bool UNICHARSET::encodable_string(const char *str,
244  int *first_bad_position) const {
245  GenericVector<UNICHAR_ID> encoding;
246  return encode_string(str, true, &encoding, nullptr, first_bad_position);
247 }
248 
249 // Encodes the given UTF-8 string with this UNICHARSET.
250 // Returns true if the encoding succeeds completely, false if there is at
251 // least one INVALID_UNICHAR_ID in the returned encoding, but in this case
252 // the rest of the string is still encoded.
253 // If lengths is not nullptr, then it is filled with the corresponding
254 // byte length of each encoded UNICHAR_ID.
255 // WARNING: Caller must guarantee that str has already been cleaned of codes
256 // that do not belong in the unicharset, or encoding may fail.
257 // Use CleanupString to perform the cleaning.
258 bool UNICHARSET::encode_string(const char* str, bool give_up_on_failure,
259  GenericVector<UNICHAR_ID>* encoding,
260  GenericVector<char>* lengths,
261  int* encoded_length) const {
262  GenericVector<UNICHAR_ID> working_encoding;
263  GenericVector<char> working_lengths;
264  GenericVector<char> best_lengths;
265  encoding->truncate(0); // Just in case str is empty.
266  int str_length = strlen(str);
267  int str_pos = 0;
268  bool perfect = true;
269  while (str_pos < str_length) {
270  encode_string(str, str_pos, str_length, &working_encoding, &working_lengths,
271  &str_pos, encoding, &best_lengths);
272  if (str_pos < str_length) {
273  // This is a non-match. Skip one utf-8 character.
274  perfect = false;
275  if (give_up_on_failure) break;
276  int step = UNICHAR::utf8_step(str + str_pos);
277  if (step == 0) step = 1;
278  encoding->push_back(INVALID_UNICHAR_ID);
279  best_lengths.push_back(step);
280  str_pos += step;
281  working_encoding = *encoding;
282  working_lengths = best_lengths;
283  }
284  }
285  if (lengths != nullptr) *lengths = best_lengths;
286  if (encoded_length != nullptr) *encoded_length = str_pos;
287  return perfect;
288 }
289 
290 const char* UNICHARSET::id_to_unichar(UNICHAR_ID id) const {
291  if (id == INVALID_UNICHAR_ID) {
292  return INVALID_UNICHAR;
293  }
294  ASSERT_HOST(id < this->size());
295  return unichars[id].representation;
296 }
297 
299  if (id == INVALID_UNICHAR_ID) {
300  return INVALID_UNICHAR;
301  }
302  ASSERT_HOST(id < this->size());
303  // Resolve from the kCustomLigatures table if this is a private encoding.
304  if (get_isprivate(id)) {
305  const char* ch = id_to_unichar(id);
306  for (int i = 0; kCustomLigatures[i][0] != nullptr; ++i) {
307  if (!strcmp(ch, kCustomLigatures[i][1])) {
308  return kCustomLigatures[i][0];
309  }
310  }
311  }
312  // Otherwise return the stored representation.
313  return unichars[id].representation;
314 }
315 
316 // Return a STRING that reformats the utf8 str into the str followed
317 // by its hex unicodes.
319  STRING result = str;
320  result += " [";
321  int step = 1;
322  // Chop into unicodes and code each as hex.
323  for (int i = 0; str[i] != '\0'; i += step) {
324  char hex[sizeof(int) * 2 + 1];
325  step = UNICHAR::utf8_step(str + i);
326  if (step == 0) {
327  step = 1;
328  sprintf(hex, "%x", str[i]);
329  } else {
330  UNICHAR ch(str + i, step);
331  sprintf(hex, "%x", ch.first_uni());
332  }
333  result += hex;
334  result += " ";
335  }
336  result += "]";
337  return result;
338 }
339 
340 // Return a STRING containing debug information on the unichar, including
341 // the id_to_unichar, its hex unicodes and the properties.
343  if (id == INVALID_UNICHAR_ID) return STRING(id_to_unichar(id));
344  const CHAR_FRAGMENT *fragment = this->get_fragment(id);
345  if (fragment) {
346  return fragment->to_string();
347  }
348  const char* str = id_to_unichar(id);
349  STRING result = debug_utf8_str(str);
350  // Append a for lower alpha, A for upper alpha, and x if alpha but neither.
351  if (get_isalpha(id)) {
352  if (get_islower(id))
353  result += "a";
354  else if (get_isupper(id))
355  result += "A";
356  else
357  result += "x";
358  }
359  // Append 0 if a digit.
360  if (get_isdigit(id)) {
361  result += "0";
362  }
363  // Append p is a punctuation symbol.
364  if (get_ispunctuation(id)) {
365  result += "p";
366  }
367  return result;
368 }
369 
370 // Sets the normed_ids vector from the normed string. normed_ids is not
371 // stored in the file, and needs to be set when the UNICHARSET is loaded.
373  unichars[unichar_id].properties.normed_ids.truncate(0);
374  if (unichar_id == UNICHAR_SPACE && id_to_unichar(unichar_id)[0] == ' ') {
375  unichars[unichar_id].properties.normed_ids.push_back(UNICHAR_SPACE);
376  } else if (!encode_string(unichars[unichar_id].properties.normed.string(),
377  true, &unichars[unichar_id].properties.normed_ids,
378  nullptr, nullptr)) {
379  unichars[unichar_id].properties.normed_ids.truncate(0);
380  unichars[unichar_id].properties.normed_ids.push_back(unichar_id);
381  }
382 }
383 
384 // Returns whether the unichar id represents a unicode value in the private use
385 // area. We use this range only internally to represent uncommon ligatures
386 // (eg. 'ct') that do not have regular unicode values.
387 bool UNICHARSET::get_isprivate(UNICHAR_ID unichar_id) const {
388  UNICHAR uc(id_to_unichar(unichar_id), -1);
389  int uni = uc.first_uni();
390  return (uni >= 0xE000 && uni <= 0xF8FF);
391 }
392 
393 
394 // Sets all ranges to empty, so they can be expanded to set the values.
396  for (int id = 0; id < size_used; ++id) {
397  unichars[id].properties.SetRangesEmpty();
398  }
399 }
400 
401 // Sets all the properties for this unicharset given a src unicharset with
402 // everything set. The unicharsets don't have to be the same, and graphemes
403 // are correctly accounted for.
405  const UNICHARSET& src) {
406  for (int ch = start_index; ch < size_used; ++ch) {
407  const char* utf8 = id_to_unichar(ch);
408  UNICHAR_PROPERTIES properties;
409  if (src.GetStrProperties(utf8, &properties)) {
410  // Setup the script_id, other_case, and mirror properly.
411  const char* script = src.get_script_from_script_id(properties.script_id);
412  properties.script_id = add_script(script);
413  const char* other_case = src.id_to_unichar(properties.other_case);
414  if (contains_unichar(other_case)) {
415  properties.other_case = unichar_to_id(other_case);
416  } else {
417  properties.other_case = ch;
418  }
419  const char* mirror_str = src.id_to_unichar(properties.mirror);
420  if (contains_unichar(mirror_str)) {
421  properties.mirror = unichar_to_id(mirror_str);
422  } else {
423  properties.mirror = ch;
424  }
425  unichars[ch].properties.CopyFrom(properties);
426  set_normed_ids(ch);
427  }
428  }
429 }
430 
431 // Expands the tops and bottoms and widths for this unicharset given a
432 // src unicharset with ranges in it. The unicharsets don't have to be the
433 // same, and graphemes are correctly accounted for.
435  for (int ch = 0; ch < size_used; ++ch) {
436  const char* utf8 = id_to_unichar(ch);
437  UNICHAR_PROPERTIES properties;
438  if (src.GetStrProperties(utf8, &properties)) {
439  // Expand just the ranges from properties.
440  unichars[ch].properties.ExpandRangesFrom(properties);
441  }
442  }
443 }
444 
445 // Makes this a copy of src. Clears this completely first, so the automatic
446 // ids will not be present in this if not in src. Does NOT reorder the set!
448  clear();
449  for (int ch = 0; ch < src.size_used; ++ch) {
450  const UNICHAR_PROPERTIES& src_props = src.unichars[ch].properties;
451  const char* utf8 = src.id_to_unichar(ch);
453  unichars[ch].properties.ExpandRangesFrom(src_props);
454  }
455  // Set properties, including mirror and other_case, WITHOUT reordering
456  // the unicharset.
458 }
459 
460 // For each id in src, if it does not occur in this, add it, as in
461 // SetPropertiesFromOther, otherwise expand the ranges, as in
462 // ExpandRangesFromOther.
464  int initial_used = size_used;
465  for (int ch = 0; ch < src.size_used; ++ch) {
466  const UNICHAR_PROPERTIES& src_props = src.unichars[ch].properties;
467  const char* utf8 = src.id_to_unichar(ch);
468  int id = size_used;
469  if (contains_unichar(utf8)) {
470  id = unichar_to_id(utf8);
471  // Just expand current ranges.
472  unichars[id].properties.ExpandRangesFrom(src_props);
473  } else {
475  unichars[id].properties.SetRangesEmpty();
476  }
477  }
478  // Set properties, including mirror and other_case, WITHOUT reordering
479  // the unicharset.
480  PartialSetPropertiesFromOther(initial_used, src);
481 }
482 
483 // Returns true if the acceptable ranges of the tops of the characters do
484 // not overlap, making their x-height calculations distinct.
486  int overlap = std::min(unichars[id1].properties.max_top,
487  unichars[id2].properties.max_top) -
488  std::max(unichars[id1].properties.min_top,
489  unichars[id2].properties.min_top);
490  return overlap <= 0;
491 }
492 
493 // Internal recursive version of encode_string above.
494 // Seeks to encode the given string as a sequence of UNICHAR_IDs such that
495 // each UNICHAR_ID uses the least possible part of the utf8 str.
496 // It does this by depth-first tail recursion on increasing length matches
497 // to the UNICHARSET, saving the first encountered result that encodes the
498 // maximum total length of str. It stops on a failure to encode to make
499 // the overall process of encoding a partially failed string more efficient.
500 // See unicharset.h for definition of the args.
501 void UNICHARSET::encode_string(const char* str, int str_index, int str_length,
502  GenericVector<UNICHAR_ID>* encoding,
503  GenericVector<char>* lengths,
504  int* best_total_length,
505  GenericVector<UNICHAR_ID>* best_encoding,
506  GenericVector<char>* best_lengths) const {
507  if (str_index > *best_total_length) {
508  // This is the best result so far.
509  *best_total_length = str_index;
510  *best_encoding = *encoding;
511  if (best_lengths != nullptr)
512  *best_lengths = *lengths;
513  }
514  if (str_index == str_length) return;
515  int encoding_index = encoding->size();
516  // Find the length of the first matching unicharset member.
517  int length = ids.minmatch(str + str_index);
518  if (length == 0 || str_index + length > str_length) return;
519  do {
520  if (ids.contains(str + str_index, length)) {
521  // Successful encoding so far.
522  UNICHAR_ID id = ids.unichar_to_id(str + str_index, length);
523  encoding->push_back(id);
524  lengths->push_back(length);
525  encode_string(str, str_index + length, str_length, encoding, lengths,
526  best_total_length, best_encoding, best_lengths);
527  if (*best_total_length == str_length)
528  return; // Tail recursion success!
529  // Failed with that length, truncate back and try again.
530  encoding->truncate(encoding_index);
531  lengths->truncate(encoding_index);
532  }
533  int step = UNICHAR::utf8_step(str + str_index + length);
534  if (step == 0) step = 1;
535  length += step;
536  } while (length <= UNICHAR_LEN && str_index + length <= str_length);
537 }
538 
539 // Gets the properties for a grapheme string, combining properties for
540 // multiple characters in a meaningful way where possible.
541 // Returns false if no valid match was found in the unicharset.
542 // NOTE that script_id, mirror, and other_case refer to this unicharset on
543 // return and will need translation if the target unicharset is different.
544 bool UNICHARSET::GetStrProperties(const char* utf8_str,
545  UNICHAR_PROPERTIES* props) const {
546  props->Init();
547  props->SetRangesEmpty();
548  int total_unicodes = 0;
549  GenericVector<UNICHAR_ID> encoding;
550  if (!encode_string(utf8_str, true, &encoding, nullptr, nullptr))
551  return false; // Some part was invalid.
552  for (int i = 0; i < encoding.size(); ++i) {
553  int id = encoding[i];
554  const UNICHAR_PROPERTIES& src_props = unichars[id].properties;
555  // Logical OR all the bools.
556  if (src_props.isalpha) props->isalpha = true;
557  if (src_props.islower) props->islower = true;
558  if (src_props.isupper) props->isupper = true;
559  if (src_props.isdigit) props->isdigit = true;
560  if (src_props.ispunctuation) props->ispunctuation = true;
561  if (src_props.isngram) props->isngram = true;
562  if (src_props.enabled) props->enabled = true;
563  // Min/max the tops/bottoms.
564  UpdateRange(src_props.min_bottom, &props->min_bottom, &props->max_bottom);
565  UpdateRange(src_props.max_bottom, &props->min_bottom, &props->max_bottom);
566  UpdateRange(src_props.min_top, &props->min_top, &props->max_top);
567  UpdateRange(src_props.max_top, &props->min_top, &props->max_top);
568  float bearing = props->advance + src_props.bearing;
569  if (total_unicodes == 0 || bearing < props->bearing) {
570  props->bearing = bearing;
571  props->bearing_sd = props->advance_sd + src_props.bearing_sd;
572  }
573  props->advance += src_props.advance;
574  props->advance_sd += src_props.advance_sd;
575  // With a single width, just use the widths stored in the unicharset.
576  props->width = src_props.width;
577  props->width_sd = src_props.width_sd;
578  // Use the first script id, other_case, mirror, direction.
579  // Note that these will need translation, except direction.
580  if (total_unicodes == 0) {
581  props->script_id = src_props.script_id;
582  props->other_case = src_props.other_case;
583  props->mirror = src_props.mirror;
584  props->direction = src_props.direction;
585  }
586  // The normed string for the compound character is the concatenation of
587  // the normed versions of the individual characters.
588  props->normed += src_props.normed;
589  ++total_unicodes;
590  }
591  if (total_unicodes > 1) {
592  // Estimate the total widths from the advance - bearing.
593  props->width = props->advance - props->bearing;
594  props->width_sd = props->advance_sd + props->bearing_sd;
595  }
596  return total_unicodes > 0;
597 }
598 
599 // TODO(rays) clean-up the order of functions to match unicharset.h.
600 
601 unsigned int UNICHARSET::get_properties(UNICHAR_ID id) const {
602  unsigned int properties = 0;
603  if (this->get_isalpha(id))
604  properties |= ISALPHA_MASK;
605  if (this->get_islower(id))
606  properties |= ISLOWER_MASK;
607  if (this->get_isupper(id))
608  properties |= ISUPPER_MASK;
609  if (this->get_isdigit(id))
610  properties |= ISDIGIT_MASK;
611  if (this->get_ispunctuation(id))
612  properties |= ISPUNCTUATION_MASK;
613  return properties;
614 }
615 
617  if (this->get_isupper(id)) return 'A';
618  if (this->get_islower(id)) return 'a';
619  if (this->get_isalpha(id)) return 'x';
620  if (this->get_isdigit(id)) return '0';
621  if (this->get_ispunctuation(id)) return 'p';
622  return 0;
623 }
624 
625 void UNICHARSET::unichar_insert(const char* const unichar_repr,
626  OldUncleanUnichars old_style) {
627  if (old_style == OldUncleanUnichars::kTrue) old_style_included_ = true;
628  std::string cleaned =
629  old_style_included_ ? unichar_repr : CleanupString(unichar_repr);
630  if (!cleaned.empty() && !ids.contains(cleaned.data(), cleaned.size())) {
631  const char* str = cleaned.c_str();
632  GenericVector<int> encoding;
633  if (!old_style_included_ &&
634  encode_string(str, true, &encoding, nullptr, nullptr))
635  return;
636  if (size_used == size_reserved) {
637  if (size_used == 0)
638  reserve(8);
639  else
640  reserve(2 * size_used);
641  }
642  int index = 0;
643  do {
644  if (index >= UNICHAR_LEN) {
645  fprintf(stderr, "Utf8 buffer too big, size>%d for %s\n", UNICHAR_LEN,
646  unichar_repr);
647  return;
648  }
649  unichars[size_used].representation[index++] = *str++;
650  } while (*str != '\0');
651  unichars[size_used].representation[index] = '\0';
652  this->set_script(size_used, null_script);
653  // If the given unichar_repr represents a fragmented character, set
654  // fragment property to a pointer to CHAR_FRAGMENT class instance with
655  // information parsed from the unichar representation. Use the script
656  // of the base unichar for the fragmented character if possible.
657  CHAR_FRAGMENT* frag =
658  CHAR_FRAGMENT::parse_from_string(unichars[size_used].representation);
659  this->unichars[size_used].properties.fragment = frag;
660  if (frag != nullptr && this->contains_unichar(frag->get_unichar())) {
661  this->unichars[size_used].properties.script_id =
662  this->get_script(frag->get_unichar());
663  }
664  this->unichars[size_used].properties.enabled = true;
665  ids.insert(unichars[size_used].representation, size_used);
666  ++size_used;
667  }
668 }
669 
670 bool UNICHARSET::contains_unichar(const char* const unichar_repr) const {
671  std::string cleaned =
672  old_style_included_ ? unichar_repr : CleanupString(unichar_repr);
673  return ids.contains(cleaned.data(), cleaned.size());
674 }
675 
676 bool UNICHARSET::contains_unichar(const char* const unichar_repr,
677  int length) const {
678  if (length == 0) {
679  return false;
680  }
681  std::string cleaned(unichar_repr, length);
682  if (!old_style_included_) cleaned = CleanupString(unichar_repr, length);
683  return ids.contains(cleaned.data(), cleaned.size());
684 }
685 
686 bool UNICHARSET::eq(UNICHAR_ID unichar_id,
687  const char* const unichar_repr) const {
688  return strcmp(this->id_to_unichar(unichar_id), unichar_repr) == 0;
689 }
690 
692  const int kFileBufSize = 1024;
693  char buffer[kFileBufSize + 1];
694  snprintf(buffer, kFileBufSize, "%d\n", this->size());
695  *str = buffer;
696  for (UNICHAR_ID id = 0; id < this->size(); ++id) {
697  int min_bottom, max_bottom, min_top, max_top;
698  get_top_bottom(id, &min_bottom, &max_bottom, &min_top, &max_top);
699  float width, width_sd;
700  get_width_stats(id, &width, &width_sd);
701  float bearing, bearing_sd;
702  get_bearing_stats(id, &bearing, &bearing_sd);
703  float advance, advance_sd;
704  get_advance_stats(id, &advance, &advance_sd);
705  unsigned int properties = this->get_properties(id);
706  if (strcmp(this->id_to_unichar(id), " ") == 0) {
707  snprintf(buffer, kFileBufSize, "%s %x %s %d\n", "NULL", properties,
708  this->get_script_from_script_id(this->get_script(id)),
709  this->get_other_case(id));
710  } else {
711  snprintf(buffer, kFileBufSize,
712  "%s %x %d,%d,%d,%d,%g,%g,%g,%g,%g,%g %s %d %d %d %s\t# %s\n",
713  this->id_to_unichar(id), properties,
714  min_bottom, max_bottom, min_top, max_top, width, width_sd,
715  bearing, bearing_sd, advance, advance_sd,
716  this->get_script_from_script_id(this->get_script(id)),
717  this->get_other_case(id), this->get_direction(id),
718  this->get_mirror(id), this->get_normed_unichar(id),
719  this->debug_str(id).string());
720  }
721  *str += buffer;
722  }
723  return true;
724 }
725 
726 // TODO(rays) Replace with TFile everywhere.
728  public:
729  InMemoryFilePointer(const char *memory, int mem_size)
730  : memory_(memory), fgets_ptr_(memory), mem_size_(mem_size) { }
731 
732  char *fgets(char *orig_dst, int size) {
733  const char *src_end = memory_ + mem_size_;
734  char *dst_end = orig_dst + size - 1;
735  if (size < 1) {
736  return fgets_ptr_ < src_end ? orig_dst : nullptr;
737  }
738 
739  char *dst = orig_dst;
740  char ch = '^';
741  while (fgets_ptr_ < src_end && dst < dst_end && ch != '\n') {
742  ch = *dst++ = *fgets_ptr_++;
743  }
744  *dst = 0;
745  return (dst == orig_dst) ? nullptr : orig_dst;
746  }
747 
748  private:
749  const char *memory_;
750  const char *fgets_ptr_;
751  const int mem_size_;
752 };
753 
755  const char *memory, int mem_size, bool skip_fragments) {
756  InMemoryFilePointer mem_fp(memory, mem_size);
759  bool success = load_via_fgets(fgets_cb, skip_fragments);
760  delete fgets_cb;
761  return success;
762 }
763 
765  public:
766  LocalFilePointer(FILE *stream) : fp_(stream) {}
767  char *fgets(char *dst, int size) {
768  return ::fgets(dst, size, fp_);
769  }
770  private:
771  FILE *fp_;
772 };
773 
774 bool UNICHARSET::load_from_file(FILE *file, bool skip_fragments) {
775  LocalFilePointer lfp(file);
778  bool success = load_via_fgets(fgets_cb, skip_fragments);
779  delete fgets_cb;
780  return success;
781 }
782 
783 bool UNICHARSET::load_from_file(tesseract::TFile *file, bool skip_fragments) {
786  bool success = load_via_fgets(fgets_cb, skip_fragments);
787  delete fgets_cb;
788  return success;
789 }
790 
791 bool UNICHARSET::load_via_fgets(
793  bool skip_fragments) {
794  int unicharset_size;
795  char buffer[256];
796 
797  this->clear();
798  if (fgets_cb->Run(buffer, sizeof(buffer)) == nullptr ||
799  sscanf(buffer, "%d", &unicharset_size) != 1) {
800  return false;
801  }
802  this->reserve(unicharset_size);
803  for (UNICHAR_ID id = 0; id < unicharset_size; ++id) {
804  char unichar[256];
805  unsigned int properties;
806  char script[64];
807 
808  strncpy(script, null_script, sizeof(script));
809  int min_bottom = 0;
810  int max_bottom = UINT8_MAX;
811  int min_top = 0;
812  int max_top = UINT8_MAX;
813  float width = 0.0f;
814  float width_sd = 0.0f;
815  float bearing = 0.0f;
816  float bearing_sd = 0.0f;
817  float advance = 0.0f;
818  float advance_sd = 0.0f;
819  // TODO(eger): check that this default it ok
820  // after enabling BiDi iterator for Arabic+Cube.
822  UNICHAR_ID other_case = id;
823  UNICHAR_ID mirror = id;
824  char normed[64];
825  int v = -1;
826  if (fgets_cb->Run(buffer, sizeof (buffer)) == nullptr ||
827  ((v = sscanf(buffer,
828  "%s %x %d,%d,%d,%d,%g,%g,%g,%g,%g,%g %63s %d %d %d %63s",
829  unichar, &properties,
830  &min_bottom, &max_bottom, &min_top, &max_top,
831  &width, &width_sd, &bearing, &bearing_sd,
832  &advance, &advance_sd, script, &other_case,
833  &direction, &mirror, normed)) != 17 &&
834  (v = sscanf(buffer,
835  "%s %x %d,%d,%d,%d,%g,%g,%g,%g,%g,%g %63s %d %d %d",
836  unichar, &properties,
837  &min_bottom, &max_bottom, &min_top, &max_top,
838  &width, &width_sd, &bearing, &bearing_sd,
839  &advance, &advance_sd, script, &other_case,
840  &direction, &mirror)) != 16 &&
841  (v = sscanf(buffer, "%s %x %d,%d,%d,%d %63s %d %d %d",
842  unichar, &properties,
843  &min_bottom, &max_bottom, &min_top, &max_top,
844  script, &other_case, &direction, &mirror)) != 10 &&
845  (v = sscanf(buffer, "%s %x %d,%d,%d,%d %63s %d", unichar, &properties,
846  &min_bottom, &max_bottom, &min_top, &max_top,
847  script, &other_case)) != 8 &&
848  (v = sscanf(buffer, "%s %x %63s %d", unichar, &properties,
849  script, &other_case)) != 4 &&
850  (v = sscanf(buffer, "%s %x %63s",
851  unichar, &properties, script)) != 3 &&
852  (v = sscanf(buffer, "%s %x", unichar, &properties)) != 2)) {
853  return false;
854  }
855 
856  // Skip fragments if needed.
857  CHAR_FRAGMENT *frag = nullptr;
858  if (skip_fragments && (frag = CHAR_FRAGMENT::parse_from_string(unichar))) {
859  int num_pieces = frag->get_total();
860  delete frag;
861  // Skip multi-element fragments, but keep singles like UNICHAR_BROKEN in.
862  if (num_pieces > 1)
863  continue;
864  }
865  // Insert unichar into unicharset and set its properties.
866  if (strcmp(unichar, "NULL") == 0)
867  this->unichar_insert(" ");
868  else
870 
871  this->set_isalpha(id, properties & ISALPHA_MASK);
872  this->set_islower(id, properties & ISLOWER_MASK);
873  this->set_isupper(id, properties & ISUPPER_MASK);
874  this->set_isdigit(id, properties & ISDIGIT_MASK);
875  this->set_ispunctuation(id, properties & ISPUNCTUATION_MASK);
876  this->set_isngram(id, false);
877  this->set_script(id, script);
878  this->unichars[id].properties.enabled = true;
879  this->set_top_bottom(id, min_bottom, max_bottom, min_top, max_top);
880  this->set_width_stats(id, width, width_sd);
881  this->set_bearing_stats(id, bearing, bearing_sd);
882  this->set_advance_stats(id, advance, advance_sd);
883  this->set_direction(id, static_cast<UNICHARSET::Direction>(direction));
884  this->set_other_case(
885  id, (v > 3 && other_case < unicharset_size) ? other_case : id);
886  this->set_mirror(id, (v > 8 && mirror < unicharset_size) ? mirror : id);
887  this->set_normed(id, (v>16) ? normed : unichar);
888  }
889  post_load_setup();
890  return true;
891 }
892 
893 // Sets up internal data after loading the file, based on the char
894 // properties. Called from load_from_file, but also needs to be run
895 // during set_unicharset_properties.
897  // Number of alpha chars with the case property minus those without,
898  // in order to determine that half the alpha chars have case.
899  int net_case_alphas = 0;
900  int x_height_alphas = 0;
901  int cap_height_alphas = 0;
902  top_bottom_set_ = false;
903  for (UNICHAR_ID id = 0; id < size_used; ++id) {
904  int min_bottom = 0;
905  int max_bottom = UINT8_MAX;
906  int min_top = 0;
907  int max_top = UINT8_MAX;
908  get_top_bottom(id, &min_bottom, &max_bottom, &min_top, &max_top);
909  if (min_top > 0)
910  top_bottom_set_ = true;
911  if (get_isalpha(id)) {
912  if (get_islower(id) || get_isupper(id))
913  ++net_case_alphas;
914  else
915  --net_case_alphas;
916  if (min_top < kMeanlineThreshold && max_top < kMeanlineThreshold)
917  ++x_height_alphas;
918  else if (min_top > kMeanlineThreshold && max_top > kMeanlineThreshold)
919  ++cap_height_alphas;
920  }
921  set_normed_ids(id);
922  }
923 
924  script_has_upper_lower_ = net_case_alphas > 0;
925  script_has_xheight_ = script_has_upper_lower_ ||
926  (x_height_alphas > cap_height_alphas * kMinXHeightFraction &&
927  cap_height_alphas > x_height_alphas * kMinCapHeightFraction);
928 
929  null_sid_ = get_script_id_from_name(null_script);
930  ASSERT_HOST(null_sid_ == 0);
931  common_sid_ = get_script_id_from_name("Common");
932  latin_sid_ = get_script_id_from_name("Latin");
933  cyrillic_sid_ = get_script_id_from_name("Cyrillic");
934  greek_sid_ = get_script_id_from_name("Greek");
935  han_sid_ = get_script_id_from_name("Han");
936  hiragana_sid_ = get_script_id_from_name("Hiragana");
937  katakana_sid_ = get_script_id_from_name("Katakana");
938  thai_sid_ = get_script_id_from_name("Thai");
939  hangul_sid_ = get_script_id_from_name("Hangul");
940 
941  // Compute default script. Use the highest-counting alpha script, that is
942  // not the common script, as that still contains some "alphas".
943  int* script_counts = new int[script_table_size_used];
944  memset(script_counts, 0, sizeof(*script_counts) * script_table_size_used);
945  for (int id = 0; id < size_used; ++id) {
946  if (get_isalpha(id)) {
947  ++script_counts[get_script(id)];
948  }
949  }
950  default_sid_ = 0;
951  for (int s = 1; s < script_table_size_used; ++s) {
952  if (script_counts[s] > script_counts[default_sid_] && s != common_sid_)
953  default_sid_ = s;
954  }
955  delete [] script_counts;
956 }
957 
958 // Returns true if right_to_left scripts are significant in the unicharset,
959 // but without being so sensitive that "universal" unicharsets containing
960 // characters from many scripts, like orientation and script detection,
961 // look like they are right_to_left.
963  int ltr_count = 0;
964  int rtl_count = 0;
965  for (int id = 0; id < size_used; ++id) {
966  int dir = get_direction(id);
967  if (dir == UNICHARSET::U_LEFT_TO_RIGHT) ltr_count++;
968  if (dir == UNICHARSET::U_RIGHT_TO_LEFT ||
970  dir == UNICHARSET::U_ARABIC_NUMBER) rtl_count++;
971  }
972  return rtl_count > ltr_count;
973 }
974 
975 // Set a whitelist and/or blacklist of characters to recognize.
976 // An empty or nullptr whitelist enables everything (minus any blacklist).
977 // An empty or nullptr blacklist disables nothing.
978 // An empty or nullptr blacklist has no effect.
979 void UNICHARSET::set_black_and_whitelist(const char* blacklist,
980  const char* whitelist,
981  const char* unblacklist) {
982  bool def_enabled = whitelist == nullptr || whitelist[0] == '\0';
983  // Set everything to default
984  for (int ch = 0; ch < size_used; ++ch)
985  unichars[ch].properties.enabled = def_enabled;
986  if (!def_enabled) {
987  // Enable the whitelist.
988  GenericVector<UNICHAR_ID> encoding;
989  encode_string(whitelist, false, &encoding, nullptr, nullptr);
990  for (int i = 0; i < encoding.size(); ++i) {
991  if (encoding[i] != INVALID_UNICHAR_ID)
992  unichars[encoding[i]].properties.enabled = true;
993  }
994  }
995  if (blacklist != nullptr && blacklist[0] != '\0') {
996  // Disable the blacklist.
997  GenericVector<UNICHAR_ID> encoding;
998  encode_string(blacklist, false, &encoding, nullptr, nullptr);
999  for (int i = 0; i < encoding.size(); ++i) {
1000  if (encoding[i] != INVALID_UNICHAR_ID)
1001  unichars[encoding[i]].properties.enabled = false;
1002  }
1003  }
1004  if (unblacklist != nullptr && unblacklist[0] != '\0') {
1005  // Re-enable the unblacklist.
1006  GenericVector<UNICHAR_ID> encoding;
1007  encode_string(unblacklist, false, &encoding, nullptr, nullptr);
1008  for (int i = 0; i < encoding.size(); ++i) {
1009  if (encoding[i] != INVALID_UNICHAR_ID)
1010  unichars[encoding[i]].properties.enabled = true;
1011  }
1012  }
1013 }
1014 
1015 // Returns true if there are any repeated unicodes in the normalized
1016 // text of any unichar-id in the unicharset.
1018  int start_id = 0;
1020  for (int id = start_id; id < size_used; ++id) {
1021  // Convert to unicodes.
1022  std::vector<char32> unicodes = UNICHAR::UTF8ToUTF32(get_normed_unichar(id));
1023  for (int u = 1; u < unicodes.size(); ++u) {
1024  if (unicodes[u - 1] == unicodes[u]) return true;
1025  }
1026  }
1027  return false;
1028 }
1029 
1030 int UNICHARSET::add_script(const char* script) {
1031  for (int i = 0; i < script_table_size_used; ++i) {
1032  if (strcmp(script, script_table[i]) == 0)
1033  return i;
1034  }
1035  if (script_table_size_reserved == 0) {
1036  script_table_size_reserved = 8;
1037  script_table = new char*[script_table_size_reserved];
1038  } else if (script_table_size_used >= script_table_size_reserved) {
1039  assert(script_table_size_used == script_table_size_reserved);
1040  script_table_size_reserved += script_table_size_reserved;
1041  char** new_script_table = new char*[script_table_size_reserved];
1042  memcpy(new_script_table, script_table,
1043  script_table_size_used * sizeof(char*));
1044  delete[] script_table;
1045  script_table = new_script_table;
1046  }
1047  script_table[script_table_size_used] = new char[strlen(script) + 1];
1048  strcpy(script_table[script_table_size_used], script);
1049  return script_table_size_used++;
1050 }
1051 
1052 // Returns the string that represents a fragment
1053 // with the given unichar, pos and total.
1054 STRING CHAR_FRAGMENT::to_string(const char *unichar, int pos, int total,
1055  bool natural) {
1056  if (total == 1) return STRING(unichar);
1057  STRING result = "";
1058  result += kSeparator;
1059  result += unichar;
1060  char buffer[kMaxLen];
1061  snprintf(buffer, kMaxLen, "%c%d%c%d", kSeparator, pos,
1062  natural ? kNaturalFlag : kSeparator, total);
1063  result += buffer;
1064  return result;
1065 }
1066 
1068  const char *ptr = string;
1069  int len = strlen(string);
1070  if (len < kMinLen || *ptr != kSeparator) {
1071  return nullptr; // this string can not represent a fragment
1072  }
1073  ptr++; // move to the next character
1074  int step = 0;
1075  while ((ptr + step) < (string + len) && *(ptr + step) != kSeparator) {
1076  step += UNICHAR::utf8_step(ptr + step);
1077  }
1078  if (step == 0 || step > UNICHAR_LEN) {
1079  return nullptr; // no character for unichar or the character is too long
1080  }
1081  char unichar[UNICHAR_LEN + 1];
1082  strncpy(unichar, ptr, step);
1083  unichar[step] = '\0'; // null terminate unichar
1084  ptr += step; // move to the next fragment separator
1085  int pos = 0;
1086  int total = 0;
1087  bool natural = false;
1088  char *end_ptr = nullptr;
1089  for (int i = 0; i < 2; i++) {
1090  if (ptr > string + len || *ptr != kSeparator) {
1091  if (i == 1 && *ptr == kNaturalFlag)
1092  natural = true;
1093  else
1094  return nullptr; // Failed to parse fragment representation.
1095  }
1096  ptr++; // move to the next character
1097  i == 0 ? pos = static_cast<int>(strtol(ptr, &end_ptr, 10))
1098  : total = static_cast<int>(strtol(ptr, &end_ptr, 10));
1099  ptr = end_ptr;
1100  }
1101  if (ptr != string + len) {
1102  return nullptr; // malformed fragment representation
1103  }
1104  CHAR_FRAGMENT *fragment = new CHAR_FRAGMENT();
1105  fragment->set_all(unichar, pos, total, natural);
1106  return fragment;
1107 }
1108 
1109 int UNICHARSET::get_script_id_from_name(const char* script_name) const {
1110  for (int i = 0; i < script_table_size_used; ++i) {
1111  if (strcmp(script_name, script_table[i]) == 0)
1112  return i;
1113  }
1114  return 0; // 0 is always the null_script
1115 }
1116 
1117 // Removes/replaces content that belongs in rendered text, but not in the
1118 // unicharset.
1119 /* static */
1120 std::string UNICHARSET::CleanupString(const char* utf8_str, size_t length) {
1121  std::string result;
1122  result.reserve(length);
1123  char ch;
1124  while ((ch = *utf8_str) != '\0' && length-- > 0) {
1125  int key_index = 0;
1126  const char* key;
1127  while ((key = kCleanupMaps[key_index][0]) != nullptr) {
1128  int match = 0;
1129  while (key[match] != '\0' && key[match] == utf8_str[match]) ++match;
1130  if (key[match] == '\0') {
1131  utf8_str += match;
1132  break;
1133  }
1134  ++key_index;
1135  }
1136  if (key == nullptr) {
1137  result.push_back(ch);
1138  ++utf8_str;
1139  } else {
1140  result.append(kCleanupMaps[key_index][1]);
1141  }
1142  }
1143  return result;
1144 }
static TESS_API const char * kSpecialUnicharCodes[SPECIAL_UNICHAR_CODES_COUNT]
Definition: unicharset.h:154
void set_isalpha(UNICHAR_ID unichar_id, bool value)
Definition: unicharset.h:426
void PartialSetPropertiesFromOther(int start_index, const UNICHARSET &src)
Definition: unicharset.cpp:404
bool get_islower(UNICHAR_ID unichar_id) const
Definition: unicharset.h:493
int UNICHAR_ID
Definition: unichar.h:35
int size() const
Definition: genericvector.h:71
static const int kMaxLen
Definition: unicharset.h:54
bool get_ispunctuation(UNICHAR_ID unichar_id) const
Definition: unicharset.h:514
bool encode_string(const char *str, bool give_up_on_failure, GenericVector< UNICHAR_ID > *encoding, GenericVector< char > *lengths, int *encoded_length) const
Definition: unicharset.cpp:258
bool AnyRepeatedUnicodes() const
int first_uni() const
Definition: unichar.cpp:99
void unichar_insert_backwards_compatible(const char *const unichar_repr)
Definition: unicharset.h:264
void set_width_stats(UNICHAR_ID unichar_id, float width, float width_sd)
Definition: unicharset.h:602
const char * get_normed_unichar(UNICHAR_ID unichar_id) const
Definition: unicharset.h:823
bool eq(UNICHAR_ID unichar_id, const char *const unichar_repr) const
Definition: unicharset.cpp:686
void CopyFrom(const UNICHARSET &src)
Definition: unicharset.cpp:447
void AppendOtherUnicharset(const UNICHARSET &src)
Definition: unicharset.cpp:463
const char * get_script_from_script_id(int id) const
Definition: unicharset.h:849
char get_chartype(UNICHAR_ID unichar_id) const
Definition: unicharset.cpp:616
signed int char32
Definition: unichar.h:52
static TESS_API const char * kCustomLigatures[][2]
Definition: unicharset.h:151
void set_normed_ids(UNICHAR_ID unichar_id)
Definition: unicharset.cpp:372
unsigned int get_properties(UNICHAR_ID unichar_id) const
Definition: unicharset.cpp:601
void set_ranges_empty()
Definition: unicharset.cpp:395
UNICHAR_ID unichar_to_id(const char *const unichar_repr, int length) const
Definition: unicharmap.cpp:36
void set_ispunctuation(UNICHAR_ID unichar_id, bool value)
Definition: unicharset.h:446
void set_black_and_whitelist(const char *blacklist, const char *whitelist, const char *unblacklist)
Definition: unicharset.cpp:979
char * FGets(char *buffer, int buffer_size)
Definition: serialis.cpp:248
bool contains(const char *const unichar_repr, int length) const
Definition: unicharmap.cpp:81
UNICHAR_ID unichar_to_id(const char *const unichar_repr) const
Definition: unicharset.cpp:209
static STRING to_string(const char *unichar, int pos, int total, bool natural)
int direction(EDGEPT *point)
Definition: vecfuncs.cpp:43
_ConstTessMemberResultCallback_0_0< false, R, T1 >::base * NewPermanentTessCallback(const T1 *obj, R(T2::*member)() const)
Definition: tesscallback.h:116
void set_isngram(UNICHAR_ID unichar_id, bool value)
Definition: unicharset.h:451
int get_total() const
Definition: unicharset.h:73
char * fgets(char *orig_dst, int size)
Definition: unicharset.cpp:732
#define UNICHAR_LEN
Definition: unichar.h:31
bool get_isalpha(UNICHAR_ID unichar_id) const
Definition: unicharset.h:486
int get_script_id_from_name(const char *script_name) const
const char * get_unichar() const
Definition: unicharset.h:71
LocalFilePointer(FILE *stream)
Definition: unicharset.cpp:766
int size() const
Definition: unicharset.h:336
bool major_right_to_left() const
Definition: unicharset.cpp:962
void unichar_insert(const char *const unichar_repr, OldUncleanUnichars old_style)
Definition: unicharset.cpp:625
void set_all(const char *unichar, int pos, int total, bool natural)
Definition: unicharset.h:59
Direction get_direction(UNICHAR_ID unichar_id) const
Definition: unicharset.h:685
static const int kMinLen
Definition: unicharset.h:52
bool get_isdigit(UNICHAR_ID unichar_id) const
Definition: unicharset.h:507
void set_top_bottom(UNICHAR_ID unichar_id, int min_bottom, int max_bottom, int min_top, int max_top)
Definition: unicharset.h:577
const double kMinXHeightFraction
Definition: unicharset.cpp:58
STRING to_string() const
Definition: unicharset.h:80
void get_top_bottom(UNICHAR_ID unichar_id, int *min_bottom, int *max_bottom, int *min_top, int *max_top) const
Definition: unicharset.h:563
bool contains_unichar(const char *const unichar_repr) const
Definition: unicharset.cpp:670
const double kMinCapHeightFraction
Definition: unicharset.cpp:59
virtual R Run(A1, A2)=0
STRING debug_str(UNICHAR_ID id) const
Definition: unicharset.cpp:342
bool encodable_string(const char *str, int *first_bad_position) const
Definition: unicharset.cpp:243
void ExpandRangesFromOther(const UNICHARSET &src)
Definition: unicharset.cpp:434
void set_other_case(UNICHAR_ID unichar_id, UNICHAR_ID other_case)
Definition: unicharset.h:462
bool empty() const
Definition: genericvector.h:90
void get_bearing_stats(UNICHAR_ID unichar_id, float *bearing, float *bearing_sd) const
Definition: unicharset.h:608
void set_isupper(UNICHAR_ID unichar_id, bool value)
Definition: unicharset.h:436
void insert(const char *const unichar_repr, UNICHAR_ID id)
Definition: unicharmap.cpp:58
const char * id_to_unichar_ext(UNICHAR_ID id) const
Definition: unicharset.cpp:298
static std::string CleanupString(const char *utf8_str)
Definition: unicharset.h:241
int step(const char *str) const
Definition: unicharset.cpp:232
int push_back(T object)
void get_width_stats(UNICHAR_ID unichar_id, float *width, float *width_sd) const
Definition: unicharset.h:591
bool SizesDistinct(UNICHAR_ID id1, UNICHAR_ID id2) const
Definition: unicharset.cpp:485
bool get_isprivate(UNICHAR_ID unichar_id) const
Definition: unicharset.cpp:387
void set_normed(UNICHAR_ID unichar_id, const char *normed)
Definition: unicharset.h:477
UNICHAR_ID get_mirror(UNICHAR_ID unichar_id) const
Definition: unicharset.h:692
const CHAR_FRAGMENT * get_fragment(UNICHAR_ID unichar_id) const
Definition: unicharset.h:729
Definition: strngs.h:45
void set_islower(UNICHAR_ID unichar_id, bool value)
Definition: unicharset.h:431
InMemoryFilePointer(const char *memory, int mem_size)
Definition: unicharset.cpp:729
static STRING debug_utf8_str(const char *str)
Definition: unicharset.cpp:318
const char * id_to_unichar(UNICHAR_ID id) const
Definition: unicharset.cpp:290
void clear()
Definition: unicharset.h:301
void set_direction(UNICHAR_ID unichar_id, UNICHARSET::Direction value)
Definition: unicharset.h:467
OldUncleanUnichars
Definition: unicharset.h:44
void set_isdigit(UNICHAR_ID unichar_id, bool value)
Definition: unicharset.h:441
void reserve(int unichars_number)
Definition: unicharset.cpp:194
void set_bearing_stats(UNICHAR_ID unichar_id, float bearing, float bearing_sd)
Definition: unicharset.h:618
void truncate(int size)
void get_advance_stats(UNICHAR_ID unichar_id, float *advance, float *advance_sd) const
Definition: unicharset.h:625
UNICHAR_ID get_other_case(UNICHAR_ID unichar_id) const
Definition: unicharset.h:678
void set_advance_stats(UNICHAR_ID unichar_id, float advance, float advance_sd)
Definition: unicharset.h:635
bool get_isupper(UNICHAR_ID unichar_id) const
Definition: unicharset.h:500
char * fgets(char *dst, int size)
Definition: unicharset.cpp:767
bool has_special_codes() const
Definition: unicharset.h:717
void set_mirror(UNICHAR_ID unichar_id, UNICHAR_ID mirror)
Definition: unicharset.h:472
int minmatch(const char *const unichar_repr) const
Definition: unicharmap.cpp:102
bool load_from_inmemory_file(const char *const memory, int mem_size, bool skip_fragments)
Definition: unicharset.cpp:754
bool load_from_file(const char *const filename, bool skip_fragments)
Definition: unicharset.h:383
void set_script(UNICHAR_ID unichar_id, const char *value)
Definition: unicharset.h:457
bool save_to_string(STRING *str) const
Definition: unicharset.cpp:691
int add_script(const char *script)
int get_script(UNICHAR_ID unichar_id) const
Definition: unicharset.h:658
void post_load_setup()
Definition: unicharset.cpp:896
static CHAR_FRAGMENT * parse_from_string(const char *str)
void UpdateRange(const T1 &x, T2 *lower_bound, T2 *upper_bound)
Definition: helpers.h:121
#define ASSERT_HOST(x)
Definition: errcode.h:84