38 static const char kSeparator =
'|';
40 static const char kNaturalFlag =
'n';
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;
51 static const int kMeanlineThreshold = 220;
74 const char* UNICHARSET::kCleanupMaps[][2] = {
87 const char* UNICHARSET::null_script =
"NULL";
89 UNICHARSET::UNICHAR_PROPERTIES::UNICHAR_PROPERTIES() {
94 void UNICHARSET::UNICHAR_PROPERTIES::Init() {
99 ispunctuation =
false;
113 void UNICHARSET::UNICHAR_PROPERTIES::SetRangesOpen() {
115 max_bottom = UINT8_MAX;
127 void UNICHARSET::UNICHAR_PROPERTIES::SetRangesEmpty() {
128 min_bottom = UINT8_MAX;
142 bool UNICHARSET::UNICHAR_PROPERTIES::AnyRangeEmpty()
const {
143 return width == 0.0f || advance == 0.0f;
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);
153 if (src.width_sd > width_sd) {
155 width_sd = src.width_sd;
157 if (src.bearing_sd > bearing_sd) {
158 bearing = src.bearing;
159 bearing_sd = src.bearing_sd;
161 if (src.advance_sd > advance_sd) {
162 advance = src.advance;
163 advance_sd = src.advance_sd;
168 void UNICHARSET::UNICHAR_PROPERTIES::CopyFrom(
const UNICHAR_PROPERTIES& src) {
172 fragment = saved_fragment;
180 script_table(nullptr),
181 script_table_size_used(0) {
195 if (unichars_number > size_reserved) {
196 auto* 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);
203 unichars = unichars_new;
204 size_reserved = unichars_number;
211 old_style_included_ ? unichar_repr :
CleanupString(unichar_repr);
212 return ids.
contains(cleaned.data(), cleaned.size())
214 : INVALID_UNICHAR_ID;
221 if (!old_style_included_) cleaned =
CleanupString(unichar_repr, length);
222 return ids.
contains(cleaned.data(), cleaned.size())
224 : INVALID_UNICHAR_ID;
236 if (encoding.
empty() || encoding[0] == INVALID_UNICHAR_ID)
return 0;
244 int *first_bad_position)
const {
246 return encode_string(str,
true, &encoding,
nullptr, first_bad_position);
261 int* encoded_length)
const {
266 int str_length = strlen(str);
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) {
275 if (give_up_on_failure)
break;
276 int step = UNICHAR::utf8_step(str + str_pos);
281 working_encoding = *encoding;
282 working_lengths = best_lengths;
285 if (lengths !=
nullptr) *lengths = best_lengths;
286 if (encoded_length !=
nullptr) *encoded_length = str_pos;
291 if (
id == INVALID_UNICHAR_ID) {
292 return INVALID_UNICHAR;
295 return unichars[id].representation;
299 if (
id == INVALID_UNICHAR_ID) {
300 return INVALID_UNICHAR;
313 return unichars[id].representation;
323 for (
int i = 0; str[i] !=
'\0'; i +=
step) {
324 char hex[
sizeof(
int) * 2 + 1];
325 step = UNICHAR::utf8_step(str + i);
328 sprintf(hex,
"%x", str[i]);
373 unichars[unichar_id].properties.normed_ids.truncate(0);
375 unichars[unichar_id].properties.normed_ids.push_back(
UNICHAR_SPACE);
376 }
else if (!
encode_string(unichars[unichar_id].properties.normed.c_str(),
377 true, &unichars[unichar_id].properties.normed_ids,
379 unichars[unichar_id].properties.normed_ids.truncate(0);
380 unichars[unichar_id].properties.normed_ids.push_back(unichar_id);
390 return (uni >= 0xE000 && uni <= 0xF8FF);
396 for (
int id = 0;
id < size_used; ++id) {
397 unichars[id].properties.SetRangesEmpty();
406 for (
int ch = start_index; ch < size_used; ++ch) {
408 UNICHAR_PROPERTIES properties;
409 if (src.GetStrProperties(utf8, &properties)) {
413 const char* other_case = src.
id_to_unichar(properties.other_case);
417 properties.other_case = ch;
419 const char* mirror_str = src.
id_to_unichar(properties.mirror);
423 properties.mirror = ch;
425 unichars[ch].properties.CopyFrom(properties);
435 for (
int ch = 0; ch < size_used; ++ch) {
437 UNICHAR_PROPERTIES properties;
438 if (src.GetStrProperties(utf8, &properties)) {
440 unichars[ch].properties.ExpandRangesFrom(properties);
449 for (
int ch = 0; ch < src.size_used; ++ch) {
450 const UNICHAR_PROPERTIES& src_props = src.unichars[ch].properties;
453 unichars[ch].properties.ExpandRangesFrom(src_props);
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;
472 unichars[id].properties.ExpandRangesFrom(src_props);
475 unichars[id].properties.SetRangesEmpty();
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);
504 int* best_total_length,
507 if (str_index > *best_total_length) {
509 *best_total_length = str_index;
510 *best_encoding = *encoding;
511 if (best_lengths !=
nullptr)
512 *best_lengths = *lengths;
514 if (str_index == str_length)
return;
515 int encoding_index = encoding->
size();
517 int length = ids.
minmatch(str + str_index);
518 if (length == 0 || str_index + length > str_length)
return;
520 if (ids.
contains(str + str_index, 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)
533 int step = UNICHAR::utf8_step(str + str_index + length);
536 }
while (length <=
UNICHAR_LEN && str_index + length <= str_length);
544 bool UNICHARSET::GetStrProperties(
const char* utf8_str,
545 UNICHAR_PROPERTIES* props)
const {
547 props->SetRangesEmpty();
548 int total_unicodes = 0;
550 if (!
encode_string(utf8_str,
true, &encoding,
nullptr,
nullptr))
552 for (
int i = 0; i < encoding.
size(); ++i) {
553 int id = encoding[i];
554 const UNICHAR_PROPERTIES& src_props = unichars[id].properties;
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;
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;
573 props->advance += src_props.advance;
574 props->advance_sd += src_props.advance_sd;
576 props->width = src_props.width;
577 props->width_sd = src_props.width_sd;
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;
588 props->normed += src_props.normed;
591 if (total_unicodes > 1) {
593 props->width = props->advance - props->bearing;
594 props->width_sd = props->advance_sd + props->bearing_sd;
596 return total_unicodes > 0;
602 unsigned int properties = 0;
604 properties |= ISALPHA_MASK;
606 properties |= ISLOWER_MASK;
608 properties |= ISUPPER_MASK;
610 properties |= ISDIGIT_MASK;
612 properties |= ISPUNCTUATION_MASK;
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();
633 if (!old_style_included_ &&
636 if (size_used == size_reserved) {
645 fprintf(stderr,
"Utf8 buffer too big, size>%d for %s\n",
UNICHAR_LEN,
649 unichars[size_used].representation[index++] = *str++;
650 }
while (*str !=
'\0');
651 unichars[size_used].representation[index] =
'\0';
659 this->unichars[size_used].properties.fragment = frag;
661 this->unichars[size_used].properties.script_id =
664 this->unichars[size_used].properties.enabled =
true;
665 ids.
insert(unichars[size_used].representation, size_used);
672 old_style_included_ ? unichar_repr :
CleanupString(unichar_repr);
673 return ids.
contains(cleaned.data(), cleaned.size());
682 if (!old_style_included_) cleaned =
CleanupString(unichar_repr, length);
683 return ids.
contains(cleaned.data(), cleaned.size());
687 const char*
const unichar_repr)
const {
688 return strcmp(this->
id_to_unichar(unichar_id), unichar_repr) == 0;
692 const int kFileBufSize = 1024;
693 char buffer[kFileBufSize + 1];
694 snprintf(buffer, kFileBufSize,
"%d\n", this->
size());
697 int min_bottom, max_bottom, min_top, max_top;
699 float width, width_sd;
701 float bearing, bearing_sd;
703 float advance, advance_sd;
707 snprintf(buffer, kFileBufSize,
"%s %x %s %d\n",
"NULL", properties,
712 std::ostringstream stream;
713 stream.imbue(std::locale::classic());
714 stream << this->
id_to_unichar(
id) <<
' ' << properties <<
' ' <<
715 min_bottom <<
',' << max_bottom <<
',' <<
716 min_top <<
',' << max_top <<
',' <<
717 width <<
',' << width_sd <<
',' <<
718 bearing <<
',' << bearing_sd <<
',' <<
719 advance <<
',' << advance_sd <<
' ' <<
726 *str += stream.str().
c_str();
736 return ::fgets(dst, size, fp_);
744 using namespace std::placeholders;
745 std::function<
char*(
char*,
int)> fgets_cb =
747 bool success = load_via_fgets(fgets_cb, skip_fragments);
752 using namespace std::placeholders;
753 std::function<
char*(
char*,
int)> fgets_cb =
755 bool success = load_via_fgets(fgets_cb, skip_fragments);
759 bool UNICHARSET::load_via_fgets(std::function<
char*(
char*,
int)> fgets_cb,
760 bool skip_fragments) {
765 if (fgets_cb(buffer,
sizeof(buffer)) ==
nullptr ||
766 sscanf(buffer,
"%d", &unicharset_size) != 1) {
769 this->
reserve(unicharset_size);
770 for (
UNICHAR_ID id = 0;
id < unicharset_size; ++id) {
772 unsigned int properties;
775 strncpy(script, null_script,
sizeof(script) - 1);
777 int max_bottom = UINT8_MAX;
779 int max_top = UINT8_MAX;
781 float width_sd = 0.0f;
782 float bearing = 0.0f;
783 float bearing_sd = 0.0f;
784 float advance = 0.0f;
785 float advance_sd = 0.0f;
791 if (fgets_cb(buffer,
sizeof (buffer)) ==
nullptr) {
796 std::istringstream stream(buffer);
797 stream.imbue(std::locale::classic());
800 stream >> std::setw(255) >> unichar >> std::hex >> properties >> std::dec;
803 fprintf(stderr,
"%s:%u failed\n", __FILE__, __LINE__);
806 auto position = stream.tellg();
807 stream.seekg(position);
808 char c1, c2, c3, c4, c5, c6, c7, c8, c9;
809 stream >> min_bottom >> c1 >> max_bottom >> c2 >> min_top >> c3 >> max_top >> c4 >>
810 width >> c5 >>width_sd >> c6 >> bearing >> c7 >> bearing_sd >> c8 >>
811 advance >> c9 >> advance_sd >> std::setw(63) >> script >>
812 other_case >> direction >> mirror >> std::setw(63) >> normed;
813 if (stream.fail() || c1 !=
',' || c2 !=
',' || c3 !=
',' || c4 !=
',' ||
814 c5 !=
',' || c6 !=
',' || c7 !=
',' || c8 !=
',' || c9 !=
',') {
816 stream.seekg(position);
817 stream >> min_bottom >> c1 >> max_bottom >> c2 >> min_top >> c3 >> max_top >> c4 >>
818 width >> c5 >>width_sd >> c6 >> bearing >> c7 >> bearing_sd >> c8 >>
819 advance >> c9 >> advance_sd >> std::setw(63) >> script >>
820 other_case >> direction >> mirror;
821 if (stream.fail() || c1 !=
',' || c2 !=
',' || c3 !=
',' || c4 !=
',' ||
822 c5 !=
',' || c6 !=
',' || c7 !=
',' || c8 !=
',' || c9 !=
',') {
824 stream.seekg(position);
825 stream >> min_bottom >> c1 >> max_bottom >> c2 >> min_top >> c3 >> max_top >>
826 std::setw(63) >> script >> other_case >> direction >> mirror;
827 if (stream.fail() || c1 !=
',' || c2 !=
',' || c3 !=
',') {
829 stream.seekg(position);
830 stream >> min_bottom >> c1 >> max_bottom >> c2 >> min_top >> c3 >> max_top >>
831 std::setw(63) >> script >> other_case;
832 if (stream.fail() || c1 !=
',' || c2 !=
',' || c3 !=
',') {
834 stream.seekg(position);
835 stream >> std::setw(63) >> script >> other_case;
838 stream.seekg(position);
839 stream >> std::setw(63) >> script;
856 if (strcmp(unichar,
"NULL") == 0)
868 this->unichars[id].properties.enabled =
true;
869 this->
set_top_bottom(
id, min_bottom, max_bottom, min_top, max_top);
873 this->
set_direction(
id, static_cast<UNICHARSET::Direction>(direction));
875 id, (other_case < unicharset_size) ? other_case :
id);
876 this->
set_mirror(
id, (mirror < unicharset_size) ? mirror :
id);
877 this->
set_normed(
id, normed[0] !=
'\0' ? normed : unichar);
889 int net_case_alphas = 0;
890 int x_height_alphas = 0;
891 int cap_height_alphas = 0;
892 top_bottom_set_ =
false;
893 for (
UNICHAR_ID id = 0;
id < size_used; ++id) {
895 int max_bottom = UINT8_MAX;
897 int max_top = UINT8_MAX;
900 top_bottom_set_ =
true;
906 if (min_top < kMeanlineThreshold && max_top < kMeanlineThreshold)
908 else if (min_top > kMeanlineThreshold && max_top > kMeanlineThreshold)
914 script_has_upper_lower_ = net_case_alphas > 0;
915 script_has_xheight_ = script_has_upper_lower_ ||
933 int* script_counts =
new int[script_table_size_used];
934 memset(script_counts, 0,
sizeof(*script_counts) * script_table_size_used);
935 for (
int id = 0;
id < size_used; ++id) {
941 for (
int s = 1; s < script_table_size_used; ++s) {
942 if (script_counts[s] > script_counts[default_sid_] && s != common_sid_)
945 delete [] script_counts;
955 for (
int id = 0;
id < size_used; ++id) {
962 return rtl_count > ltr_count;
970 const char* whitelist,
971 const char* unblacklist) {
972 bool def_enabled = whitelist ==
nullptr || whitelist[0] ==
'\0';
974 for (
int ch = 0; ch < size_used; ++ch)
975 unichars[ch].properties.enabled = def_enabled;
979 encode_string(whitelist,
false, &encoding,
nullptr,
nullptr);
980 for (
int i = 0; i < encoding.
size(); ++i) {
981 if (encoding[i] != INVALID_UNICHAR_ID)
982 unichars[encoding[i]].properties.enabled =
true;
985 if (blacklist !=
nullptr && blacklist[0] !=
'\0') {
988 encode_string(blacklist,
false, &encoding,
nullptr,
nullptr);
989 for (
int i = 0; i < encoding.
size(); ++i) {
990 if (encoding[i] != INVALID_UNICHAR_ID)
991 unichars[encoding[i]].properties.enabled =
false;
994 if (unblacklist !=
nullptr && unblacklist[0] !=
'\0') {
997 encode_string(unblacklist,
false, &encoding,
nullptr,
nullptr);
998 for (
int i = 0; i < encoding.
size(); ++i) {
999 if (encoding[i] != INVALID_UNICHAR_ID)
1000 unichars[encoding[i]].properties.enabled =
true;
1010 for (
int id = start_id;
id < size_used; ++id) {
1013 for (
size_t u = 1; u < unicodes.size(); ++u) {
1014 if (unicodes[u - 1] == unicodes[u])
return true;
1021 for (
int i = 0; i < script_table_size_used; ++i) {
1022 if (strcmp(script, script_table[i]) == 0)
1025 if (script_table_size_reserved == 0) {
1026 script_table_size_reserved = 8;
1027 script_table =
new char*[script_table_size_reserved];
1028 }
else if (script_table_size_used >= script_table_size_reserved) {
1029 assert(script_table_size_used == script_table_size_reserved);
1030 script_table_size_reserved += script_table_size_reserved;
1031 char** new_script_table =
new char*[script_table_size_reserved];
1032 memcpy(new_script_table, script_table,
1033 script_table_size_used *
sizeof(
char*));
1034 delete[] script_table;
1035 script_table = new_script_table;
1037 script_table[script_table_size_used] =
new char[strlen(script) + 1];
1038 strcpy(script_table[script_table_size_used], script);
1039 return script_table_size_used++;
1046 if (total == 1)
return STRING(unichar);
1048 result += kSeparator;
1050 char buffer[kMaxLen];
1051 snprintf(buffer, kMaxLen,
"%c%d%c%d", kSeparator, pos,
1052 natural ? kNaturalFlag : kSeparator, total);
1058 const char *ptr =
string;
1059 int len = strlen(
string);
1060 if (len < kMinLen || *ptr != kSeparator) {
1065 while ((ptr +
step) < (
string + len) && *(ptr +
step) != kSeparator) {
1066 step += UNICHAR::utf8_step(ptr +
step);
1072 strncpy(unichar, ptr,
step);
1073 unichar[
step] =
'\0';
1077 bool natural =
false;
1078 char *end_ptr =
nullptr;
1079 for (
int i = 0; i < 2; i++) {
1080 if (ptr >
string + len || *ptr != kSeparator) {
1081 if (i == 1 && *ptr == kNaturalFlag)
1087 i == 0 ? pos = static_cast<int>(strtol(ptr, &end_ptr, 10))
1088 : total = static_cast<int>(strtol(ptr, &end_ptr, 10));
1091 if (ptr !=
string + len) {
1095 fragment->set_all(unichar, pos, total, natural);
1100 for (
int i = 0; i < script_table_size_used; ++i) {
1101 if (strcmp(script_name, script_table[i]) == 0)
1112 result.reserve(length);
1114 while ((ch = *utf8_str) !=
'\0' && length-- > 0) {
1117 while ((key = kCleanupMaps[key_index][0]) !=
nullptr) {
1119 while (key[match] !=
'\0' && key[match] == utf8_str[match]) ++match;
1120 if (key[match] ==
'\0') {
1126 if (key ==
nullptr) {
1127 result.push_back(ch);
1130 result.append(kCleanupMaps[key_index][1]);