Dillo v3.1.1-46-g8a360e32
Loading...
Searching...
No Matches
liang.cc
Go to the documentation of this file.
1/*
2 * File: liang.cc
3 *
4 * Copyright 2012-2016 Sebastian Geerken <sgeerken@dillo.org>
5 * Copyright 2012-2013 Johannes Hofmann <Johannes.Hofmann@gmx.de>
6 * Copyright 2023 Rodrigo Arias Mallo <rodarima@gmail.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22/* Tests the hyphenation of words in different languages with the Liang
23 * algorithm. The hyphenator requires the .pat pattern files which can
24 * be downloaded from CTAN. */
25
26#include <unistd.h>
27#include <stdio.h>
28#include <errno.h>
29
30#include "dw/fltkcore.hh"
31#include "dw/hyphenator.hh"
32
34
35void hyph(dw::Hyphenator *h, const char *word, const char *parts)
36{
37 int p = 0;
38 char buf[1024];
39 int numBreaks;
40 int *breakPos = h->hyphenateWord(platform, word, &numBreaks);
41 memset(buf, 0, 1024);
42 for (int i = 0; i < numBreaks + 1; i++) {
43 int start = (i == 0 ? 0 : breakPos[i - 1]);
44 int end = (i == numBreaks ? strlen (word) : breakPos[i]);
45 if (i != 0)
46 buf[p++] = '-';
47 for (int j = start; j < end; j++)
48 buf[p++] = word[j];
49 }
50
51 if (strcmp(parts, buf) != 0) {
52 fprintf(stderr, "mismatch input=%s output=%s expected=%s\n",
53 word, buf, parts);
54 exit(1);
55 }
56
57 printf("%s\n", buf);
58
59 if (breakPos)
60 free(breakPos);
61}
62
64{
65 if (access(path, F_OK) != 0) {
66 fprintf(stderr, "cannot access %s file: %s", path,
67 strerror(errno));
68 exit(1);
69 }
70
71 return dw::Hyphenator(path, "", 512);
72}
73
75{
76 dw::Hyphenator h = get_hyphenator(CUR_SRC_DIR "/hyph-en-us.pat");
77
78 hyph(&h, "supercalifragilisticexpialidocious", "su-per-cal-ifrag-ilis-tic-ex-pi-ali-do-cious");
79 hyph(&h, "incredible", "in-cred-i-ble");
80 hyph(&h, "hyphenation", "hy-phen-ation");
81 hyph(&h, "...", "...");
82}
83
84void hyph_de()
85{
86 dw::Hyphenator h = get_hyphenator(CUR_SRC_DIR "/hyph-de.pat");
87
88 hyph(&h, "...", "...");
89 hyph(&h, "weiß", "weiß");
90 hyph(&h, "Ackermann", "Acker-mann");
91 hyph(&h, "Grundstücksverkehrsgenehmigungszuständigkeits",
92 "Grund-stücks-ver-kehrs-ge-neh-mi-gungs-zu-stän-dig-keits");
93 hyph(&h, "Donaudampfschifffahrtskapitänsmützenknopf",
94 "Do-nau-dampf-schiff-fahrts-ka-pi-täns-müt-zen-knopf");
95 hyph(&h, "www.dillo.org", "www.dil-lo.org");
96}
97
98int main(void)
99{
101
102 hyph_en_us();
103 hyph_de();
104
105 return 0;
106}
int * hyphenateWord(core::Platform *platform, const char *word, int *numBreaks)
Given a word, returns a list of the possible hyphenation points.
dw::fltk::FltkPlatform * platform
Definition liang.cc:33
dw::Hyphenator get_hyphenator(const char *path)
Definition liang.cc:63
void hyph(dw::Hyphenator *h, const char *word, const char *parts)
Definition liang.cc:35
void hyph_en_us()
Definition liang.cc:74
int main(void)
Definition liang.cc:98
void hyph_de()
Definition liang.cc:84
static void path()
Definition cookies.c:859