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