20 #ifndef TESSERACT_CCUTIL_UNICHARSET_H_ 21 #define TESSERACT_CCUTIL_UNICHARSET_H_ 59 inline void set_all(
const char *unichar,
int pos,
int total,
bool natural) {
69 inline void set_pos(
int p) { this->pos = p; }
71 inline const char*
get_unichar()
const {
return this->unichar; }
72 inline int get_pos()
const {
return this->pos; }
73 inline int get_total()
const {
return this->total; }
81 return to_string(unichar, pos, total, natural);
86 inline bool equals(
const char *other_unichar,
87 int other_pos,
int other_total)
const {
88 return (strcmp(this->unichar, other_unichar) == 0 &&
89 this->pos == other_pos && this->total == other_total);
100 return (strcmp(this->unichar, fragment->
get_unichar()) == 0 &&
102 this->pos == fragment->
get_pos() + 1);
109 inline bool is_ending()
const {
return this->pos == this->total-1; }
198 int step(
const char* str)
const;
223 int* encoded_length)
const;
244 static std::string
CleanupString(
const char* utf8_str,
size_t length);
266 if (cleaned != unichar_repr) {
269 int old_size =
size();
271 if (
size() == old_size) {
280 return unichar_id != INVALID_UNICHAR_ID && unichar_id < size_used &&
290 bool eq(
UNICHAR_ID unichar_id,
const char*
const unichar_repr)
const;
294 for (
int i = 0; i < size_used; ++i) {
295 delete unichars[i].properties.fragment;
296 unichars[i].properties.fragment =
nullptr;
302 if (script_table !=
nullptr) {
303 for (
int i = 0; i < script_table_size_used; ++i)
304 delete[] script_table[i];
305 delete[] script_table;
306 script_table =
nullptr;
307 script_table_size_used = 0;
309 if (unichars !=
nullptr) {
314 script_table_size_reserved = 0;
318 top_bottom_set_ =
false;
319 script_has_upper_lower_ =
false;
320 script_has_xheight_ =
false;
321 old_style_included_ =
false;
341 void reserve(
int unichars_number);
346 FILE* file = fopen(filename,
"w+b");
347 if (file ==
nullptr)
return false;
374 bool skip_fragments);
384 FILE* file = fopen(filename,
"rb");
385 if (file ==
nullptr)
return false;
423 const char* unblacklist);
427 unichars[unichar_id].properties.isalpha = value;
432 unichars[unichar_id].properties.islower = value;
437 unichars[unichar_id].properties.isupper = value;
442 unichars[unichar_id].properties.isdigit = value;
447 unichars[unichar_id].properties.ispunctuation = value;
452 unichars[unichar_id].properties.isngram = value;
458 unichars[unichar_id].properties.script_id =
add_script(value);
463 unichars[unichar_id].properties.other_case = other_case;
468 unichars[unichar_id].properties.direction = value;
473 unichars[unichar_id].properties.mirror = mirror;
478 unichars[unichar_id].properties.normed = normed;
479 unichars[unichar_id].properties.normed_ids.truncate(0);
487 if (INVALID_UNICHAR_ID == unichar_id)
return false;
489 return unichars[unichar_id].properties.isalpha;
494 if (INVALID_UNICHAR_ID == unichar_id)
return false;
496 return unichars[unichar_id].properties.islower;
501 if (INVALID_UNICHAR_ID == unichar_id)
return false;
503 return unichars[unichar_id].properties.isupper;
508 if (INVALID_UNICHAR_ID == unichar_id)
return false;
510 return unichars[unichar_id].properties.isdigit;
515 if (INVALID_UNICHAR_ID == unichar_id)
return false;
517 return unichars[unichar_id].properties.ispunctuation;
522 if (INVALID_UNICHAR_ID == unichar_id)
return false;
524 return unichars[unichar_id].properties.isngram;
533 return top_bottom_set_;
564 int* min_bottom,
int* max_bottom,
565 int* min_top,
int* max_top)
const {
566 if (INVALID_UNICHAR_ID == unichar_id) {
567 *min_bottom = *min_top = 0;
568 *max_bottom = *max_top = 256;
572 *min_bottom = unichars[unichar_id].properties.min_bottom;
573 *max_bottom = unichars[unichar_id].properties.max_bottom;
574 *min_top = unichars[unichar_id].properties.min_top;
575 *max_top = unichars[unichar_id].properties.max_top;
578 int min_bottom,
int max_bottom,
579 int min_top,
int max_top) {
580 unichars[unichar_id].properties.min_bottom =
581 ClipToRange<int>(min_bottom, 0, UINT8_MAX);
582 unichars[unichar_id].properties.max_bottom =
583 ClipToRange<int>(max_bottom, 0, UINT8_MAX);
584 unichars[unichar_id].properties.min_top =
585 ClipToRange<int>(min_top, 0, UINT8_MAX);
586 unichars[unichar_id].properties.max_top =
587 ClipToRange<int>(max_top, 0, UINT8_MAX);
592 float* width,
float* width_sd)
const {
593 if (INVALID_UNICHAR_ID == unichar_id) {
599 *width = unichars[unichar_id].properties.width;
600 *width_sd = unichars[unichar_id].properties.width_sd;
603 unichars[unichar_id].properties.width = width;
604 unichars[unichar_id].properties.width_sd = width_sd;
609 float* bearing,
float* bearing_sd)
const {
610 if (INVALID_UNICHAR_ID == unichar_id) {
611 *bearing = *bearing_sd = 0.0f;
615 *bearing = unichars[unichar_id].properties.bearing;
616 *bearing_sd = unichars[unichar_id].properties.bearing_sd;
619 float bearing,
float bearing_sd) {
620 unichars[unichar_id].properties.bearing = bearing;
621 unichars[unichar_id].properties.bearing_sd = bearing_sd;
626 float* advance,
float* advance_sd)
const {
627 if (INVALID_UNICHAR_ID == unichar_id) {
628 *advance = *advance_sd = 0;
632 *advance = unichars[unichar_id].properties.advance;
633 *advance_sd = unichars[unichar_id].properties.advance_sd;
636 float advance,
float advance_sd) {
637 unichars[unichar_id].properties.advance = advance;
638 unichars[unichar_id].properties.advance_sd = advance_sd;
642 return unichars[unichar_id].properties.AnyRangeEmpty();
648 if (INVALID_UNICHAR_ID == unichar_id)
return true;
650 return script_id != han_sid_ && script_id != thai_sid_ &&
651 script_id != hangul_sid_ && script_id != hiragana_sid_ &&
652 script_id != katakana_sid_;
659 if (INVALID_UNICHAR_ID == unichar_id)
return null_sid_;
661 return unichars[unichar_id].properties.script_id;
679 if (INVALID_UNICHAR_ID == unichar_id)
return INVALID_UNICHAR_ID;
681 return unichars[unichar_id].properties.other_case;
688 return unichars[unichar_id].properties.direction;
693 if (INVALID_UNICHAR_ID == unichar_id)
return INVALID_UNICHAR_ID;
695 return unichars[unichar_id].properties.mirror;
700 if (INVALID_UNICHAR_ID == unichar_id)
return INVALID_UNICHAR_ID;
702 if (unichars[unichar_id].properties.islower)
return unichar_id;
703 return unichars[unichar_id].properties.other_case;
708 if (INVALID_UNICHAR_ID == unichar_id)
return INVALID_UNICHAR_ID;
710 if (unichars[unichar_id].properties.isupper)
return unichar_id;
711 return unichars[unichar_id].properties.other_case;
730 if (INVALID_UNICHAR_ID == unichar_id)
return nullptr;
732 return unichars[unichar_id].properties.fragment;
780 if (unichar_repr ==
nullptr || unichar_repr[0] ==
'\0' ||
781 !ids.
contains(unichar_repr,
false)) {
825 return unichars[unichar_id].properties.normed.string();
831 return unichars[unichar_id].properties.normed_ids;
845 return script_table_size_used;
850 if (
id >= script_table_size_used ||
id < 0)
852 return script_table[id];
864 return script == null_script;
874 return unichars[unichar_id].properties.enabled;
892 return script_has_upper_lower_;
899 return script_has_xheight_;
904 struct UNICHAR_PROPERTIES {
905 UNICHAR_PROPERTIES();
910 void SetRangesOpen();
912 void SetRangesEmpty();
915 bool AnyRangeEmpty()
const;
917 void ExpandRangesFrom(
const UNICHAR_PROPERTIES& src);
919 void CopyFrom(
const UNICHAR_PROPERTIES& src);
966 struct UNICHAR_SLOT {
968 UNICHAR_PROPERTIES properties;
982 void encode_string(
const char* str,
int str_index,
int str_length,
985 int* best_total_length,
994 bool GetStrProperties(
const char* utf8_str,
995 UNICHAR_PROPERTIES* props)
const;
1001 bool skip_fragments);
1006 static const char* kCleanupMaps[][2];
1007 static TESS_API const char* null_script;
1009 UNICHAR_SLOT* unichars;
1013 char** script_table;
1014 int script_table_size_used;
1015 int script_table_size_reserved;
1017 bool top_bottom_set_;
1019 bool script_has_upper_lower_;
1022 bool script_has_xheight_;
1024 bool old_style_included_;
1043 #endif // TESSERACT_CCUTIL_UNICHARSET_H_ static TESS_API const char * kSpecialUnicharCodes[SPECIAL_UNICHAR_CODES_COUNT]
void set_isalpha(UNICHAR_ID unichar_id, bool value)
bool is_beginning() const
void PartialSetPropertiesFromOther(int start_index, const UNICHARSET &src)
bool get_islower(UNICHAR_ID unichar_id) const
bool get_islower(const char *const unichar_repr) const
bool get_ispunctuation(UNICHAR_ID unichar_id) const
bool encode_string(const char *str, bool give_up_on_failure, GenericVector< UNICHAR_ID > *encoding, GenericVector< char > *lengths, int *encoded_length) const
bool AnyRepeatedUnicodes() const
void unichar_insert_backwards_compatible(const char *const unichar_repr)
bool script_has_upper_lower() const
bool get_isalpha(const char *const unichar_repr, int length) const
bool IsSpaceDelimited(UNICHAR_ID unichar_id) const
void set_width_stats(UNICHAR_ID unichar_id, float width, float width_sd)
const char * get_normed_unichar(UNICHAR_ID unichar_id) const
void set_unichar(const char *uch)
bool save_to_file(const char *const filename) const
bool get_isdigit(const char *const unichar_repr, int length) const
bool eq(UNICHAR_ID unichar_id, const char *const unichar_repr) const
bool is_continuation_of(const CHAR_FRAGMENT *fragment) const
void CopyFrom(const UNICHARSET &src)
void AppendOtherUnicharset(const UNICHARSET &src)
UNICHAR_ID to_upper(UNICHAR_ID unichar_id) const
bool load_from_inmemory_file(const char *const memory, int mem_size)
const char * get_script_from_script_id(int id) const
bool get_ispunctuation(const char *const unichar_repr, int length) const
char get_chartype(UNICHAR_ID unichar_id) const
static TESS_API const char * kCustomLigatures[][2]
void set_normed_ids(UNICHAR_ID unichar_id)
unsigned int get_properties(UNICHAR_ID unichar_id) const
void set_ispunctuation(UNICHAR_ID unichar_id, bool value)
void set_black_and_whitelist(const char *blacklist, const char *whitelist, const char *unblacklist)
bool get_isupper(const char *const unichar_repr) const
char get_chartype(const char *const unichar_repr) const
bool contains(const char *const unichar_repr, int length) const
UNICHAR_ID unichar_to_id(const char *const unichar_repr) const
const GenericVector< UNICHAR_ID > & normed_ids(UNICHAR_ID unichar_id) const
int direction(EDGEPT *point)
bool Serialize(FILE *fp, const char *data, size_t n)
int get_script_table_size() const
void set_isngram(UNICHAR_ID unichar_id, bool value)
bool script_has_xheight() const
bool get_isalpha(UNICHAR_ID unichar_id) const
int get_script_id_from_name(const char *script_name) const
bool PropertiesIncomplete(UNICHAR_ID unichar_id) const
const char * get_unichar() const
bool is_null_script(const char *script) const
const CHAR_FRAGMENT * get_fragment(const char *const unichar_repr) const
bool get_isdigit(const char *const unichar_repr) const
bool major_right_to_left() const
void unichar_insert(const char *const unichar_repr, OldUncleanUnichars old_style)
void set_all(const char *unichar, int pos, int total, bool natural)
bool equals(const char *other_unichar, int other_pos, int other_total) const
bool get_isngram(UNICHAR_ID unichar_id) const
Direction get_direction(UNICHAR_ID unichar_id) const
void SetPropertiesFromOther(const UNICHARSET &src)
bool get_isdigit(UNICHAR_ID unichar_id) const
int get_script(const char *const unichar_repr) const
void set_top_bottom(UNICHAR_ID unichar_id, int min_bottom, int max_bottom, int min_top, int max_top)
void get_top_bottom(UNICHAR_ID unichar_id, int *min_bottom, int *max_bottom, int *min_top, int *max_top) const
bool contains_unichar(const char *const unichar_repr) const
bool get_isalpha(const char *const unichar_repr) const
int get_script(const char *const unichar_repr, int length) const
STRING debug_str(UNICHAR_ID id) const
bool get_isupper(const char *const unichar_repr, int length) const
bool save_to_file(tesseract::TFile *file) const
bool encodable_string(const char *str, int *first_bad_position) const
void ExpandRangesFromOther(const UNICHARSET &src)
void set_other_case(UNICHAR_ID unichar_id, UNICHAR_ID other_case)
bool get_enabled(UNICHAR_ID unichar_id) const
bool Serialize(const char *data, size_t count=1)
bool load_from_file(FILE *file)
void get_bearing_stats(UNICHAR_ID unichar_id, float *bearing, float *bearing_sd) const
void set_isupper(UNICHAR_ID unichar_id, bool value)
const char * id_to_unichar_ext(UNICHAR_ID id) const
bool contains_unichar_id(UNICHAR_ID unichar_id) const
static std::string CleanupString(const char *utf8_str)
int step(const char *str) const
void get_width_stats(UNICHAR_ID unichar_id, float *width, float *width_sd) const
bool SizesDistinct(UNICHAR_ID id1, UNICHAR_ID id2) const
bool get_isprivate(UNICHAR_ID unichar_id) const
void set_normed(UNICHAR_ID unichar_id, const char *normed)
UNICHAR_ID get_mirror(UNICHAR_ID unichar_id) const
const CHAR_FRAGMENT * get_fragment(UNICHAR_ID unichar_id) const
bool get_islower(const char *const unichar_repr, int length) const
void delete_pointers_in_unichars()
bool load_from_file(const char *const filename)
void set_islower(UNICHAR_ID unichar_id, bool value)
static STRING debug_utf8_str(const char *str)
bool save_to_file(FILE *file) const
const char * id_to_unichar(UNICHAR_ID id) const
unsigned int get_properties(const char *const unichar_repr) const
void set_direction(UNICHAR_ID unichar_id, UNICHARSET::Direction value)
void set_isdigit(UNICHAR_ID unichar_id, bool value)
void reserve(int unichars_number)
void set_bearing_stats(UNICHAR_ID unichar_id, float bearing, float bearing_sd)
bool equals(const CHAR_FRAGMENT *other) const
STRING debug_str(const char *unichar_repr) const
void get_advance_stats(UNICHAR_ID unichar_id, float *advance, float *advance_sd) const
void set_natural(bool value)
bool get_ispunctuation(const char *const unichar_repr) const
UNICHAR_ID get_other_case(UNICHAR_ID unichar_id) const
void set_advance_stats(UNICHAR_ID unichar_id, float advance, float advance_sd)
bool get_isupper(UNICHAR_ID unichar_id) const
bool has_special_codes() const
void set_mirror(UNICHAR_ID unichar_id, UNICHAR_ID mirror)
UNICHAR_ID to_lower(UNICHAR_ID unichar_id) const
bool top_bottom_useful() const
bool load_from_inmemory_file(const char *const memory, int mem_size, bool skip_fragments)
bool load_from_file(const char *const filename, bool skip_fragments)
void set_script(UNICHAR_ID unichar_id, const char *value)
bool save_to_string(STRING *str) const
static const int kMaxChunks
int add_script(const char *script)
int get_script(UNICHAR_ID unichar_id) const
void unichar_insert(const char *const unichar_repr)
static CHAR_FRAGMENT * parse_from_string(const char *str)