1 #include <string.h>
2 #include <sys/stat.h>
3 #include <utils/Log.h>
4 
5 #include <cstdio>
6 #include <fstream>
7 #include <vector>
8 
9 #include "minikin/Hyphenator.h"
10 
11 using minikin::HyphenationType;
12 using minikin::Hyphenator;
13 using minikin::HyphenatorCXX;
14 
loadHybFile(const char * fn,int minPrefix,int minSuffix,const char * language)15 Hyphenator* loadHybFile(const char* fn, int minPrefix, int minSuffix, const char* language) {
16     struct stat statbuf;
17     int status = stat(fn, &statbuf);
18     if (status < 0) {
19         fprintf(stderr, "error opening %s\n", fn);
20         return nullptr;
21     }
22     size_t size = statbuf.st_size;
23     FILE* f = fopen(fn, "rb");
24     if (f == NULL) {
25         fprintf(stderr, "error opening %s\n", fn);
26         return nullptr;
27     }
28     uint8_t* buf = new uint8_t[size];
29     size_t read_size = fread(buf, 1, size, f);
30     fclose(f);
31     if (read_size < size) {
32         fprintf(stderr, "error reading %s\n", fn);
33         delete[] buf;
34         return nullptr;
35     }
36     return HyphenatorCXX::loadBinary(buf, size, minPrefix, minSuffix, language);
37 }
38 
main(int argc,char ** argv)39 int main(int argc, char** argv) {
40     Hyphenator* hyph = loadHybFile(argv[1], 2, 3, "fr");  // should also be configurable
41     std::vector<HyphenationType> result;
42     std::vector<uint16_t> word;
43     if (argc < 2) {
44         fprintf(stderr, "usage: hyphtool word\n");
45         return 1;
46     }
47     std::ifstream infile(argv[2]);
48     std::string asciiword;
49     while (std::getline(infile, asciiword)) {
50         word.clear();
51         result.clear();
52         size_t len = asciiword.size();
53         for (size_t i = 0; i < len; i++) {
54             uint32_t c = asciiword[i];
55             if (c == '-') {
56                 c = 0x00AD;
57             }
58             // ASCII (or possibly ISO Latin 1), but kinda painful to do utf conversion :(
59             word.push_back(c);
60         }
61         hyph->hyphenate(word, &result);
62         for (size_t i = 0; i < len; i++) {
63             if (result[i] != HyphenationType::DONT_BREAK) {
64                 printf("-");
65             }
66             printf("%c", word[i]);
67         }
68         printf("\n");
69     }
70     return 0;
71 }
72