33 static const char kSeparator =
'|';
35 static const char kNaturalFlag =
'n';
37 static const int ISALPHA_MASK = 0x1;
38 static const int ISLOWER_MASK = 0x2;
39 static const int ISUPPER_MASK = 0x4;
40 static const int ISDIGIT_MASK = 0x8;
41 static const int ISPUNCTUATION_MASK = 0x10;
46 static const int kMeanlineThreshold = 220;
73 UNICHARSET::UNICHAR_PROPERTIES::UNICHAR_PROPERTIES() {
78 void UNICHARSET::UNICHAR_PROPERTIES::Init() {
83 ispunctuation =
false;
97 void UNICHARSET::UNICHAR_PROPERTIES::SetRangesOpen() {
111 void UNICHARSET::UNICHAR_PROPERTIES::SetRangesEmpty() {
126 bool UNICHARSET::UNICHAR_PROPERTIES::AnyRangeEmpty()
const {
127 return width == 0.0f || advance == 0.0f;
131 void UNICHARSET::UNICHAR_PROPERTIES::ExpandRangesFrom(
132 const UNICHAR_PROPERTIES& src) {
133 UpdateRange(src.min_bottom, &min_bottom, &max_bottom);
134 UpdateRange(src.max_bottom, &min_bottom, &max_bottom);
137 if (src.width_sd > width_sd) {
139 width_sd = src.width_sd;
141 if (src.bearing_sd > bearing_sd) {
142 bearing = src.bearing;
143 bearing_sd = src.bearing_sd;
145 if (src.advance_sd > advance_sd) {
146 advance = src.advance;
147 advance_sd = src.advance_sd;
152 void UNICHARSET::UNICHAR_PROPERTIES::CopyFrom(
const UNICHAR_PROPERTIES& src) {
156 fragment = saved_fragment;
165 script_table_size_used(0),
166 null_script(
"NULL") {
180 if (unichars_number > size_reserved) {
181 UNICHAR_SLOT* unichars_new =
new UNICHAR_SLOT[unichars_number];
182 for (
int i = 0; i < size_used; ++i)
183 unichars_new[i] = unichars[i];
184 for (
int j = size_used; j < unichars_number; ++j) {
185 unichars_new[j].properties.script_id =
add_script(null_script);
188 unichars = unichars_new;
189 size_reserved = unichars_number;
202 return ids.
contains(unichar_repr, length) ?
203 ids.
unichar_to_id(unichar_repr, length) : INVALID_UNICHAR_ID;
215 if (encoding.
empty() || encoding[0] == INVALID_UNICHAR_ID)
return 0;
223 int *first_bad_position)
const {
225 return encode_string(str,
true, &encoding, NULL, first_bad_position);
237 int* encoded_length)
const {
242 int str_length = strlen(str);
245 while (str_pos < str_length) {
246 encode_string(str, str_pos, str_length, &working_encoding, &working_lengths,
247 &str_pos, encoding, &best_lengths);
248 if (str_pos < str_length) {
251 if (give_up_on_failure)
break;
257 working_encoding = *encoding;
258 working_lengths = best_lengths;
261 if (lengths != NULL) *lengths = best_lengths;
262 if (encoded_length != NULL) *encoded_length = str_pos;
267 if (
id == INVALID_UNICHAR_ID) {
268 return INVALID_UNICHAR;
271 return unichars[id].representation;
275 if (
id == INVALID_UNICHAR_ID) {
276 return INVALID_UNICHAR;
289 return unichars[id].representation;
299 for (
int i = 0; str[i] !=
'\0'; i +=
step) {
300 char hex[
sizeof(int) * 2 + 1];
304 sprintf(hex,
"%x", str[i]);
349 unichars[unichar_id].properties.normed_ids.truncate(0);
351 unichars[unichar_id].properties.normed_ids.push_back(
UNICHAR_SPACE);
352 }
else if (!
encode_string(unichars[unichar_id].properties.normed.string(),
353 true, &unichars[unichar_id].properties.normed_ids,
355 unichars[unichar_id].properties.normed_ids.truncate(0);
356 unichars[unichar_id].properties.normed_ids.push_back(unichar_id);
366 return (uni >= 0xE000 && uni <= 0xF8FF);
372 for (
int id = 0;
id < size_used; ++id) {
373 unichars[id].properties.SetRangesEmpty();
382 for (
int ch = start_index; ch < size_used; ++ch) {
384 UNICHAR_PROPERTIES properties;
385 if (src.GetStrProperties(utf8, &properties)) {
389 const char* other_case = src.
id_to_unichar(properties.other_case);
393 properties.other_case = ch;
395 const char* mirror_str = src.
id_to_unichar(properties.mirror);
399 properties.mirror = ch;
401 unichars[ch].properties.CopyFrom(properties);
411 for (
int ch = 0; ch < size_used; ++ch) {
413 UNICHAR_PROPERTIES properties;
414 if (src.GetStrProperties(utf8, &properties)) {
416 unichars[ch].properties.ExpandRangesFrom(properties);
425 for (
int ch = 0; ch < src.size_used; ++ch) {
426 const UNICHAR_PROPERTIES& src_props = src.unichars[ch].properties;
429 unichars[ch].properties.ExpandRangesFrom(src_props);
440 int initial_used = size_used;
441 for (
int ch = 0; ch < src.size_used; ++ch) {
442 const UNICHAR_PROPERTIES& src_props = src.unichars[ch].properties;
446 tprintf(
"Bad properties for index %d, char %s: " 447 "%d,%d %d,%d %g,%g %g,%g %g,%g\n",
448 ch, utf8, src_props.min_bottom, src_props.max_bottom,
449 src_props.min_top, src_props.max_top,
450 src_props.width, src_props.width_sd,
451 src_props.bearing, src_props.bearing_sd,
452 src_props.advance, src_props.advance_sd);
459 unichars[id].properties.ExpandRangesFrom(src_props);
462 unichars[id].properties.SetRangesEmpty();
473 int overlap =
MIN(unichars[id1].properties.max_top,
474 unichars[id2].properties.max_top) -
475 MAX(unichars[id1].properties.min_top,
476 unichars[id2].properties.min_top);
491 int* best_total_length,
494 if (str_index > *best_total_length) {
496 *best_total_length = str_index;
497 *best_encoding = *encoding;
498 if (best_lengths != NULL)
499 *best_lengths = *lengths;
501 if (str_index == str_length)
return;
502 int encoding_index = encoding->
size();
504 int length = ids.
minmatch(str + str_index);
505 if (length == 0 || str_index + length > str_length)
return;
507 if (ids.
contains(str + str_index, length)) {
512 encode_string(str, str_index + length, str_length, encoding, lengths,
513 best_total_length, best_encoding, best_lengths);
514 if (*best_total_length == str_length)
523 }
while (length <=
UNICHAR_LEN && str_index + length <= str_length);
531 bool UNICHARSET::GetStrProperties(
const char* utf8_str,
532 UNICHAR_PROPERTIES* props)
const {
534 props->SetRangesEmpty();
535 int total_unicodes = 0;
539 for (
int i = 0; i < encoding.
size(); ++i) {
540 int id = encoding[i];
541 const UNICHAR_PROPERTIES& src_props = unichars[id].properties;
543 if (src_props.isalpha) props->isalpha =
true;
544 if (src_props.islower) props->islower =
true;
545 if (src_props.isupper) props->isupper =
true;
546 if (src_props.isdigit) props->isdigit =
true;
547 if (src_props.ispunctuation) props->ispunctuation =
true;
548 if (src_props.isngram) props->isngram =
true;
549 if (src_props.enabled) props->enabled =
true;
551 UpdateRange(src_props.min_bottom, &props->min_bottom, &props->max_bottom);
552 UpdateRange(src_props.max_bottom, &props->min_bottom, &props->max_bottom);
553 UpdateRange(src_props.min_top, &props->min_top, &props->max_top);
554 UpdateRange(src_props.max_top, &props->min_top, &props->max_top);
555 float bearing = props->advance + src_props.bearing;
556 if (total_unicodes == 0 || bearing < props->bearing) {
557 props->bearing = bearing;
558 props->bearing_sd = props->advance_sd + src_props.bearing_sd;
560 props->advance += src_props.advance;
561 props->advance_sd += src_props.advance_sd;
563 props->width = src_props.width;
564 props->width_sd = src_props.width_sd;
567 if (total_unicodes == 0) {
568 props->script_id = src_props.script_id;
569 props->other_case = src_props.other_case;
570 props->mirror = src_props.mirror;
571 props->direction = src_props.direction;
575 props->normed += src_props.normed;
578 if (total_unicodes > 1) {
580 props->width = props->advance - props->bearing;
581 props->width_sd = props->advance_sd + props->bearing_sd;
583 return total_unicodes > 0;
589 unsigned int properties = 0;
591 properties |= ISALPHA_MASK;
593 properties |= ISLOWER_MASK;
595 properties |= ISUPPER_MASK;
597 properties |= ISDIGIT_MASK;
599 properties |= ISPUNCTUATION_MASK;
615 fprintf(stderr,
"Utf8 buffer too big, size=%d for %s\n",
616 int(strlen(unichar_repr)), unichar_repr);
619 if (size_used == size_reserved) {
626 strcpy(unichars[size_used].representation, unichar_repr);
633 this->unichars[size_used].properties.fragment = frag;
635 this->unichars[size_used].properties.script_id =
638 this->unichars[size_used].properties.enabled =
true;
639 ids.
insert(unichar_repr, size_used);
653 return ids.
contains(unichar_repr, length);
657 const char*
const unichar_repr)
const {
658 return strcmp(this->
id_to_unichar(unichar_id), unichar_repr) == 0;
662 const int kFileBufSize = 1024;
663 char buffer[kFileBufSize + 1];
664 snprintf(buffer, kFileBufSize,
"%d\n", this->
size());
667 int min_bottom, max_bottom, min_top, max_top;
669 float width, width_sd;
671 float bearing, bearing_sd;
673 float advance, advance_sd;
677 snprintf(buffer, kFileBufSize,
"%s %x %s %d\n",
"NULL", properties,
681 snprintf(buffer, kFileBufSize,
682 "%s %x %d,%d,%d,%d,%g,%g,%g,%g,%g,%g %s %d %d %d %s\t# %s\n",
684 min_bottom, max_bottom, min_top, max_top, width, width_sd,
685 bearing, bearing_sd, advance, advance_sd,
700 : memory_(memory), fgets_ptr_(memory), mem_size_(mem_size) { }
702 char *
fgets(
char *orig_dst,
int size) {
703 const char *src_end = memory_ + mem_size_;
704 char *dst_end = orig_dst + size - 1;
706 return fgets_ptr_ < src_end ? orig_dst : NULL;
709 char *dst = orig_dst;
711 while (fgets_ptr_ < src_end && dst < dst_end && ch !=
'\n') {
712 ch = *dst++ = *fgets_ptr_++;
715 return (dst == orig_dst) ? NULL : orig_dst;
720 const char *fgets_ptr_;
725 const char *memory,
int mem_size,
bool skip_fragments) {
729 bool success = load_via_fgets(fgets_cb, skip_fragments);
738 return ::fgets(dst, size, fp_);
748 bool success = load_via_fgets(fgets_cb, skip_fragments);
756 bool success = load_via_fgets(fgets_cb, skip_fragments);
761 bool UNICHARSET::load_via_fgets(
763 bool skip_fragments) {
768 if (fgets_cb->
Run(buffer,
sizeof(buffer)) == NULL ||
769 sscanf(buffer,
"%d", &unicharset_size) != 1) {
772 this->
reserve(unicharset_size);
773 for (
UNICHAR_ID id = 0;
id < unicharset_size; ++id) {
775 unsigned int properties;
778 strcpy(script, null_script);
784 float width_sd = 0.0f;
785 float bearing = 0.0f;
786 float bearing_sd = 0.0f;
787 float advance = 0.0f;
788 float advance_sd = 0.0f;
796 if (fgets_cb->
Run(buffer, sizeof (buffer)) == NULL ||
798 "%s %x %d,%d,%d,%d,%g,%g,%g,%g,%g,%g %63s %d %d %d %63s",
799 unichar, &properties,
800 &min_bottom, &max_bottom, &min_top, &max_top,
801 &width, &width_sd, &bearing, &bearing_sd,
802 &advance, &advance_sd, script, &other_case,
805 "%s %x %d,%d,%d,%d,%g,%g,%g,%g,%g,%g %63s %d %d %d",
806 unichar, &properties,
807 &min_bottom, &max_bottom, &min_top, &max_top,
808 &width, &width_sd, &bearing, &bearing_sd,
809 &advance, &advance_sd, script, &other_case,
811 (v = sscanf(buffer,
"%s %x %d,%d,%d,%d %63s %d %d %d",
812 unichar, &properties,
813 &min_bottom, &max_bottom, &min_top, &max_top,
814 script, &other_case, &
direction, &mirror)) != 10 &&
815 (v = sscanf(buffer,
"%s %x %d,%d,%d,%d %63s %d", unichar, &properties,
816 &min_bottom, &max_bottom, &min_top, &max_top,
817 script, &other_case)) != 8 &&
818 (v = sscanf(buffer,
"%s %x %63s %d", unichar, &properties,
819 script, &other_case)) != 4 &&
820 (v = sscanf(buffer,
"%s %x %63s",
821 unichar, &properties, script)) != 3 &&
822 (v = sscanf(buffer,
"%s %x", unichar, &properties)) != 2)) {
836 if (strcmp(unichar,
"NULL") == 0)
848 this->unichars[id].properties.enabled =
true;
849 this->
set_top_bottom(
id, min_bottom, max_bottom, min_top, max_top);
858 this->
set_normed(
id, (v>16) ? normed : unichar);
870 int net_case_alphas = 0;
871 int x_height_alphas = 0;
872 int cap_height_alphas = 0;
873 top_bottom_set_ =
false;
874 for (
UNICHAR_ID id = 0;
id < size_used; ++id) {
881 top_bottom_set_ =
true;
887 if (min_top < kMeanlineThreshold && max_top < kMeanlineThreshold)
889 else if (min_top > kMeanlineThreshold && max_top > kMeanlineThreshold)
895 script_has_upper_lower_ = net_case_alphas > 0;
896 script_has_xheight_ = script_has_upper_lower_ ||
912 int* script_counts =
new int[script_table_size_used];
913 memset(script_counts, 0,
sizeof(*script_counts) * script_table_size_used);
914 for (
int id = 0;
id < size_used; ++id) {
920 for (
int s = 1; s < script_table_size_used; ++s) {
921 if (script_counts[s] > script_counts[default_sid_] && s != common_sid_)
924 delete [] script_counts;
934 for (
int id = 0;
id < size_used; ++id) {
941 return rtl_count > ltr_count;
949 const char* whitelist,
950 const char* unblacklist) {
951 bool def_enabled = whitelist == NULL || whitelist[0] ==
'\0';
953 for (
int ch = 0; ch < size_used; ++ch)
954 unichars[ch].properties.enabled = def_enabled;
959 for (
int i = 0; i < encoding.
size(); ++i) {
960 if (encoding[i] != INVALID_UNICHAR_ID)
961 unichars[encoding[i]].properties.enabled =
true;
964 if (blacklist != NULL && blacklist[0] !=
'\0') {
968 for (
int i = 0; i < encoding.
size(); ++i) {
969 if (encoding[i] != INVALID_UNICHAR_ID)
970 unichars[encoding[i]].properties.enabled =
false;
973 if (unblacklist != NULL && unblacklist[0] !=
'\0') {
977 for (
int i = 0; i < encoding.
size(); ++i) {
978 if (encoding[i] != INVALID_UNICHAR_ID)
979 unichars[encoding[i]].properties.enabled =
true;
989 for (
int id = start_id;
id < size_used; ++id) {
993 unicodes.
size() > 1) {
994 for (
int u = 1; u < unicodes.
size(); ++u) {
995 if (unicodes[u - 1] == unicodes[u])
return true;
1003 for (
int i = 0; i < script_table_size_used; ++i) {
1004 if (strcmp(script, script_table[i]) == 0)
1007 if (script_table_size_reserved == 0) {
1008 script_table_size_reserved = 8;
1009 script_table =
new char*[script_table_size_reserved];
1010 }
else if (script_table_size_used >= script_table_size_reserved) {
1011 assert(script_table_size_used == script_table_size_reserved);
1012 script_table_size_reserved += script_table_size_reserved;
1013 char** new_script_table =
new char*[script_table_size_reserved];
1014 memcpy(new_script_table, script_table, script_table_size_used *
sizeof(
char*));
1015 delete[] script_table;
1016 script_table = new_script_table;
1018 script_table[script_table_size_used] =
new char[strlen(script) + 1];
1019 strcpy(script_table[script_table_size_used], script);
1020 return script_table_size_used++;
1027 if (total == 1)
return STRING(unichar);
1029 result += kSeparator;
1032 snprintf(buffer,
kMaxLen,
"%c%d%c%d", kSeparator, pos,
1033 natural ? kNaturalFlag : kSeparator, total);
1039 const char *ptr = string;
1040 int len = strlen(
string);
1041 if (len <
kMinLen || *ptr != kSeparator) {
1046 while ((ptr + step) < (
string + len) && *(ptr + step) != kSeparator) {
1053 strncpy(unichar, ptr, step);
1054 unichar[step] =
'\0';
1058 bool natural =
false;
1059 char *end_ptr = NULL;
1060 for (
int i = 0; i < 2; i++) {
1061 if (ptr >
string + len || *ptr != kSeparator) {
1062 if (i == 1 && *ptr == kNaturalFlag)
1068 i == 0 ? pos =
static_cast<int>(strtol(ptr, &end_ptr, 10))
1069 : total = static_cast<int>(strtol(ptr, &end_ptr, 10));
1072 if (ptr !=
string + len) {
1076 fragment->
set_all(unichar, pos, total, natural);
1081 for (
int i = 0; i < script_table_size_used; ++i) {
1082 if (strcmp(script_name, script_table[i]) == 0)
void set_black_and_whitelist(const char *blacklist, const char *whitelist, const char *unblacklist)
void get_width_stats(UNICHAR_ID unichar_id, float *width, float *width_sd) const
bool eq(UNICHAR_ID unichar_id, const char *const unichar_repr) const
void UpdateRange(const T1 &x, T2 *lower_bound, T2 *upper_bound)
bool get_isalpha(UNICHAR_ID unichar_id) const
int get_script_id_from_name(const char *script_name) const
void AppendOtherUnicharset(const UNICHARSET &src)
UNICHAR_ID unichar_to_id(const char *const unichar_repr) const
bool TESS_API contains_unichar(const char *const unichar_repr) const
void set_islower(UNICHAR_ID unichar_id, bool value)
_ConstTessMemberResultCallback_0_0< false, R, T1 >::base * NewPermanentTessCallback(const T1 *obj, R(T2::*member)() const)
const double kMinXHeightFraction
void reserve(int unichars_number)
InMemoryFilePointer(const char *memory, int mem_size)
void get_top_bottom(UNICHAR_ID unichar_id, int *min_bottom, int *max_bottom, int *min_top, int *max_top) const
void set_normed(UNICHAR_ID unichar_id, const char *normed)
static bool UTF8ToUnicode(const char *utf8_str, GenericVector< int > *unicodes)
UNICHAR_ID get_mirror(UNICHAR_ID unichar_id) const
Direction get_direction(UNICHAR_ID unichar_id) const
const char * get_normed_unichar(UNICHAR_ID unichar_id) const
const char * get_script_from_script_id(int id) const
void set_advance_stats(UNICHAR_ID unichar_id, float advance, float advance_sd)
bool encodable_string(const char *str, int *first_bad_position) const
char get_chartype(UNICHAR_ID unichar_id) const
const char * id_to_unichar_ext(UNICHAR_ID id) const
void set_other_case(UNICHAR_ID unichar_id, UNICHAR_ID other_case)
UNICHAR_ID get_other_case(UNICHAR_ID unichar_id) const
int minmatch(const char *const unichar_repr) const
void PartialSetPropertiesFromOther(int start_index, const UNICHARSET &src)
bool get_ispunctuation(UNICHAR_ID unichar_id) const
bool load_from_inmemory_file(const char *const memory, int mem_size, bool skip_fragments)
bool has_special_codes() 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 set_isdigit(UNICHAR_ID unichar_id, bool value)
char * FGets(char *buffer, int buffer_size)
void get_advance_stats(UNICHAR_ID unichar_id, float *advance, float *advance_sd) const
void set_isupper(UNICHAR_ID unichar_id, bool value)
unsigned int get_properties(UNICHAR_ID unichar_id) const
void set_mirror(UNICHAR_ID unichar_id, UNICHAR_ID mirror)
static const char * kSpecialUnicharCodes[SPECIAL_UNICHAR_CODES_COUNT]
bool major_right_to_left() const
void set_width_stats(UNICHAR_ID unichar_id, float width, float width_sd)
const char * get_unichar() const
bool get_isupper(UNICHAR_ID unichar_id) const
bool TESS_API save_to_string(STRING *str) const
void set_bearing_stats(UNICHAR_ID unichar_id, float bearing, float bearing_sd)
static TESS_API const char * kCustomLigatures[][2]
STRING debug_str(UNICHAR_ID id) const
void set_direction(UNICHAR_ID unichar_id, UNICHARSET::Direction value)
bool contains(const char *const unichar_repr) const
bool load_from_file(const char *const filename, bool skip_fragments)
void get_bearing_stats(UNICHAR_ID unichar_id, float *bearing, float *bearing_sd) const
void TESS_API unichar_insert(const char *const unichar_repr)
static STRING to_string(const char *unichar, int pos, int total, bool natural)
int direction(EDGEPT *point)
int get_script(UNICHAR_ID unichar_id) const
const double kMinCapHeightFraction
UNICHAR_ID TESS_API unichar_to_id(const char *const unichar_repr) const
void set_isalpha(UNICHAR_ID unichar_id, bool value)
void set_normed_ids(UNICHAR_ID unichar_id)
char * fgets(char *orig_dst, int size)
void insert(const char *const unichar_repr, UNICHAR_ID id)
void CopyFrom(const UNICHARSET &src)
static STRING debug_utf8_str(const char *str)
void set_all(const char *unichar, int pos, int total, bool natural)
int add_script(const char *script)
bool get_isdigit(UNICHAR_ID unichar_id) const
bool get_islower(UNICHAR_ID unichar_id) const
LocalFilePointer(FILE *stream)
void set_isngram(UNICHAR_ID unichar_id, bool value)
char * fgets(char *dst, int size)
void ExpandRangesFromOther(const UNICHARSET &src)
const char * id_to_unichar(UNICHAR_ID id) const
void set_ispunctuation(UNICHAR_ID unichar_id, bool value)
static int utf8_step(const char *utf8_str)
const CHAR_FRAGMENT * get_fragment(UNICHAR_ID unichar_id) const
static CHAR_FRAGMENT * parse_from_string(const char *str)
int step(const char *str) const
void set_script(UNICHAR_ID unichar_id, const char *value)
bool SizesDistinct(UNICHAR_ID id1, UNICHAR_ID id2) const
void set_top_bottom(UNICHAR_ID unichar_id, int min_bottom, int max_bottom, int min_top, int max_top)
bool get_isprivate(UNICHAR_ID unichar_id) const