|
22 | 22 | #include <cassert>
|
23 | 23 | #include <cstdio>
|
24 | 24 | #include <cstring>
|
| 25 | +#include <iomanip> // for std::setw |
| 26 | +#include <locale> // for std::locale::classic |
| 27 | +#include <sstream> // for std::istringstream |
25 | 28 |
|
26 | 29 | #include "params.h"
|
27 | 30 | #include "serialis.h"
|
@@ -706,6 +709,7 @@ bool UNICHARSET::save_to_string(STRING *str) const {
|
706 | 709 | this->get_script_from_script_id(this->get_script(id)),
|
707 | 710 | this->get_other_case(id));
|
708 | 711 | } else {
|
| 712 | + // FIXME |
709 | 713 | snprintf(buffer, kFileBufSize,
|
710 | 714 | "%s %x %d,%d,%d,%d,%g,%g,%g,%g,%g,%g %s %d %d %d %s\t# %s\n",
|
711 | 715 | this->id_to_unichar(id), properties,
|
@@ -815,41 +819,64 @@ bool UNICHARSET::load_via_fgets(
|
815 | 819 | float advance = 0.0f;
|
816 | 820 | float advance_sd = 0.0f;
|
817 | 821 | // TODO(eger): check that this default it ok
|
818 |
| - // after enabling BiDi iterator for Arabic+Cube. |
| 822 | + // after enabling BiDi iterator for Arabic. |
819 | 823 | int direction = UNICHARSET::U_LEFT_TO_RIGHT;
|
820 |
| - UNICHAR_ID other_case = id; |
821 |
| - UNICHAR_ID mirror = id; |
| 824 | + UNICHAR_ID other_case = unicharset_size; |
| 825 | + UNICHAR_ID mirror = unicharset_size; |
| 826 | + if (fgets_cb->Run(buffer, sizeof (buffer)) == nullptr) { |
| 827 | + return false; |
| 828 | + } |
822 | 829 | char normed[64];
|
823 |
| - int v = -1; |
824 |
| - if (fgets_cb->Run(buffer, sizeof (buffer)) == nullptr || |
825 |
| - ((v = sscanf(buffer, |
826 |
| - "%s %x %d,%d,%d,%d,%g,%g,%g,%g,%g,%g %63s %d %d %d %63s", |
827 |
| - unichar, &properties, |
828 |
| - &min_bottom, &max_bottom, &min_top, &max_top, |
829 |
| - &width, &width_sd, &bearing, &bearing_sd, |
830 |
| - &advance, &advance_sd, script, &other_case, |
831 |
| - &direction, &mirror, normed)) != 17 && |
832 |
| - (v = sscanf(buffer, |
833 |
| - "%s %x %d,%d,%d,%d,%g,%g,%g,%g,%g,%g %63s %d %d %d", |
834 |
| - unichar, &properties, |
835 |
| - &min_bottom, &max_bottom, &min_top, &max_top, |
836 |
| - &width, &width_sd, &bearing, &bearing_sd, |
837 |
| - &advance, &advance_sd, script, &other_case, |
838 |
| - &direction, &mirror)) != 16 && |
839 |
| - (v = sscanf(buffer, "%s %x %d,%d,%d,%d %63s %d %d %d", |
840 |
| - unichar, &properties, |
841 |
| - &min_bottom, &max_bottom, &min_top, &max_top, |
842 |
| - script, &other_case, &direction, &mirror)) != 10 && |
843 |
| - (v = sscanf(buffer, "%s %x %d,%d,%d,%d %63s %d", unichar, &properties, |
844 |
| - &min_bottom, &max_bottom, &min_top, &max_top, |
845 |
| - script, &other_case)) != 8 && |
846 |
| - (v = sscanf(buffer, "%s %x %63s %d", unichar, &properties, |
847 |
| - script, &other_case)) != 4 && |
848 |
| - (v = sscanf(buffer, "%s %x %63s", |
849 |
| - unichar, &properties, script)) != 3 && |
850 |
| - (v = sscanf(buffer, "%s %x", unichar, &properties)) != 2)) { |
| 830 | + normed[0] = '\0'; |
| 831 | + std::istringstream stream(buffer); |
| 832 | + stream.imbue(std::locale::classic()); |
| 833 | + // 标 1 0,255,0,255,0,0,0,0,0,0 Han 68 0 68 标 # 标 [6807 ]x |
| 834 | + //stream.flags(std::ios::hex); |
| 835 | + stream >> std::setw(255) >> unichar >> std::hex >> properties >> std::dec; |
| 836 | + //stream.flags(std::ios::dec); |
| 837 | + if (stream.fail()) { |
| 838 | + fprintf(stderr, "%s:%u failed\n", __FILE__, __LINE__); |
851 | 839 | return false;
|
852 | 840 | }
|
| 841 | + auto position = stream.tellg(); |
| 842 | + stream.seekg(position); |
| 843 | + char c1, c2, c3, c4, c5, c6, c7, c8, c9; |
| 844 | + stream >> min_bottom >> c1 >> max_bottom >> c2 >> min_top >> c3 >> max_top >> c4 >> |
| 845 | + width >> c5 >>width_sd >> c6 >> bearing >> c7 >> bearing_sd >> c8 >> |
| 846 | + advance >> c9 >> advance_sd >> std::setw(63) >> script >> |
| 847 | + other_case >> direction >> mirror >> std::setw(63) >> normed; |
| 848 | + if (stream.fail() || c1 != ',' || c2 != ',' || c3 != ',' || c4 != ',' || |
| 849 | + c5 != ',' || c6 != ',' || c7 != ',' || c8 != ',' || c9 != ',') { |
| 850 | + stream.clear(); |
| 851 | + stream.seekg(position); |
| 852 | + stream >> min_bottom >> c1 >> max_bottom >> c2 >> min_top >> c3 >> max_top >> c4 >> |
| 853 | + width >> c5 >>width_sd >> c6 >> bearing >> c7 >> bearing_sd >> c8 >> |
| 854 | + advance >> c9 >> advance_sd >> std::setw(63) >> script >> |
| 855 | + other_case >> direction >> mirror; |
| 856 | + if (stream.fail() || c1 != ',' || c2 != ',' || c3 != ',' || c4 != ',' || |
| 857 | + c5 != ',' || c6 != ',' || c7 != ',' || c8 != ',' || c9 != ',') { |
| 858 | + stream.clear(); |
| 859 | + stream.seekg(position); |
| 860 | + stream >> min_bottom >> c1 >> max_bottom >> c2 >> min_top >> c3 >> max_top >> |
| 861 | + std::setw(63) >> script >> other_case >> direction >> mirror; |
| 862 | + if (stream.fail() || c1 != ',' || c2 != ',' || c3 != ',') { |
| 863 | + stream.clear(); |
| 864 | + stream.seekg(position); |
| 865 | + stream >> min_bottom >> c1 >> max_bottom >> c2 >> min_top >> c3 >> max_top >> |
| 866 | + std::setw(63) >> script >> other_case; |
| 867 | + if (stream.fail() || c1 != ',' || c2 != ',' || c3 != ',') { |
| 868 | + stream.clear(); |
| 869 | + stream.seekg(position); |
| 870 | + stream >> std::setw(63) >> script >> other_case; |
| 871 | + if (stream.fail()) { |
| 872 | + stream.clear(); |
| 873 | + stream.seekg(position); |
| 874 | + stream >> std::setw(63) >> script; |
| 875 | + } |
| 876 | + } |
| 877 | + } |
| 878 | + } |
| 879 | + } |
853 | 880 |
|
854 | 881 | // Skip fragments if needed.
|
855 | 882 | CHAR_FRAGMENT *frag = nullptr;
|
@@ -880,9 +907,9 @@ bool UNICHARSET::load_via_fgets(
|
880 | 907 | this->set_advance_stats(id, advance, advance_sd);
|
881 | 908 | this->set_direction(id, static_cast<UNICHARSET::Direction>(direction));
|
882 | 909 | this->set_other_case(
|
883 |
| - id, (v > 3 && other_case < unicharset_size) ? other_case : id); |
884 |
| - this->set_mirror(id, (v > 8 && mirror < unicharset_size) ? mirror : id); |
885 |
| - this->set_normed(id, (v>16) ? normed : unichar); |
| 910 | + id, (other_case < unicharset_size) ? other_case : id); |
| 911 | + this->set_mirror(id, (mirror < unicharset_size) ? mirror : id); |
| 912 | + this->set_normed(id, normed[0] != '\0' ? normed : unichar); |
886 | 913 | }
|
887 | 914 | post_load_setup();
|
888 | 915 | return true;
|
|
0 commit comments