5 #define HY_BIT 0200 /* stuff in here only works for 7-bit ascii */
6 /* this value is used (as a literal) in suftab.c */
7 /* to encode possible hyphenation points in suffixes. */
8 /* it could be changed, by widening the tables */
9 /* to be shorts instead of chars. */
17 int hexsize = 0; /* hyphenation exception list size */
18 char *hbufp = NULL; /* base of list */
19 char *nexth = NULL; /* first free slot in list */
22 #define THRESH 160 /* digram goodness threshold */
26 static int alpha(Tchar);
28 void hyphen(Tchar *wp)
41 hyend = wdend = --i - 1;
46 if (wdend - wdstart < 4) /* 4 chars is too short to hyphenate */
52 /* for now, try exceptions first, then tex (if hyphalg is non-zero),
53 then suffix and digram if tex didn't hyphenate it at all.
56 if (!exword() && !texhyphen() && !suffix())
59 /* this appears to sort hyphenation points into increasing order */
64 for (hyp = hyptr + 1; *hyp != 0; hyp++) {
65 if (*(hyp - 1) > *hyp) {
75 static int alpha(Tchar i) /* non-zero if really alphabetic */
79 else if (cbits(i) >= ALPHABET) /* this isn't very elegant, but there's */
80 return 0; /* no good way to make sure i is in range for */
81 else /* the call of isalpha */
82 return isalpha(cbits(i));
95 void caseha(void) /* set hyphenation algorithm */
106 void caseht(void) /* set hyphenation threshold; not in manual! */
117 char *growh(char *where)
122 if ((new = grow(hbufp, hexsize, sizeof(char))) == NULL)
128 diff = where - hbufp;
142 if ((nexth = hbufp = grow(hbufp, NHEX, sizeof(char))) == NULL) {
143 ERROR "No space for exception word list." WARN;
150 if ((j = nexth) >= hbufp + hexsize - 2)
151 if ((j = nexth = growh(j)) == NULL)
154 if (ismot(t = getch()))
157 if (i == ' ' || i == '\n') {
170 *j++ = maplow(i) | k;
172 if (j >= hbufp + hexsize - 2)
173 if ((j = growh(j)) == NULL)
179 ERROR "Cannot grow exception word list." WARN;
192 if (e == NULL || *e == 0)
195 while (*e && w <= hyend && (*e & 0177) == maplow(cbits(*w))) {
200 if (w-1 == hyend || (w == wdend && maplow(cbits(*w)) == 's')) {
202 for (e = save; *e; e++) {
205 if (hyp > hyptr + NHYP - 1)
206 hyp = hyptr + NHYP - 1;
226 extern char *suftab[];
234 if ((s0 = suftab[i-'a']) == 0)
237 if ((i = *s0 & 017) == 0)
241 while (s > s0 && w >= wdstart && (*s & 0177) == maplow(cbits(*w))) {
258 if (*s0 & 0100) /* 0100 used in suftab to encode something too */
297 Tchar *chkvow(Tchar *w)
299 while (--w >= wdstart)
300 if (vowel(cbits(*w)))
310 Tchar *nhyend, *maxw;
312 extern char bxh[26][13], bxxh[26][13], xxh[26][13], xhx[26][13], hxx[26][13];
315 if (!(w = chkvow(hyend + 1)))
318 if (!(w = chkvow(hyend)))
323 while (++w < hyend && w < wdend - 1) {
326 val *= dilook('a', cbits(*w), bxh);
327 else if (w == wdstart + 1)
328 val *= dilook(cbits(*(w-1)), cbits(*w), bxxh);
330 val *= dilook(cbits(*(w-1)), cbits(*w), xxh);
331 val *= dilook(cbits(*w), cbits(*(w+1)), xhx);
332 val *= dilook(cbits(*(w+1)), cbits(*(w+2)), hxx);
345 dilook(int a, int b, char t[26][13])
349 i = t[maplow(a)-'a'][(j = maplow(b)-'a')/2];
356 /* here beginneth the tex hyphenation code, as interpreted freely */
357 /* the main difference is that there is no attempt to squeeze space */
358 /* as tightly at tex does. */
360 static int texit(Tchar *, Tchar *);
361 static int readpats(void);
362 static void install(char *);
363 static void fixup(void);
364 static int trieindex(int, int);
366 static char pats[50000]; /* size ought to be computed dynamically */
367 static char *nextpat = pats;
368 static char *trie[27*27]; /* english-specific sizes */
372 static int loaded = 0; /* -1: couldn't find tex file */
374 if (hyphalg == 0 || loaded == -1) /* non-zero => tex for now */
382 return texit(wdstart, wdend);
385 static int texit(Tchar *start, Tchar *end) /* hyphenate as in tex, return # found */
387 int nw, i, k, equal, cnt[500];
388 char w[500+1], *np, *pp, *wp, *xpp, *xwp;
391 for (nw = 1; start <= end && nw < 500-1; nw++, start++)
392 w[nw] = maplow(tolower(cbits(*start)));
397 * printf("try %s\n", w);
399 for (i = 0; i <= nw; i++)
402 for (wp = w; wp < w + nw; wp++) {
403 for (pp = trie[trieindex(*wp, *(wp+1))]; pp < nextpat; ) {
404 if (pp == 0 /* no trie entry */
405 || *pp != *wp /* no match on 1st letter */
406 || *(pp+1) != *(wp+1)) /* no match on 2nd letter */
407 break; /* so move to next letter of word */
409 for (xpp = pp+2, xwp = wp+2; *xpp; )
410 if (*xpp++ != *xwp++) {
415 np = xpp+1; /* numpat */
416 for (k = wp-w; *np; k++, np++)
420 * printf("match: %s %s\n", pp, xpp+1);
423 pp += *(pp-1); /* skip over pattern and numbers to next */
427 * for (i = 0; i < nw; i++) printf("%c", w[i]);
429 * for (i = 0; i <= nw; i++) printf("%c", cnt[i]);
433 * for (i = 1; i < nw - 1; i++) {
434 * if (i > 2 && i < nw - 3 && cnt[i] % 2)
436 * if (cbits(start[i-1]) != '.')
437 * printf("%c", cbits(start[i-1]));
441 for (i = 1; i < nw -1; i++)
442 if (i > 2 && i < nw - 3 && cnt[i] % 2)
443 *hyp++ = start + i - 1;
444 return hyp - hyptr; /* non-zero if a hyphen was found */
448 This code assumes that hyphen.tex looks like
450 \patterns{ % more comments
451 pat5ter4ns, 1 per line, SORTED, nothing else
454 \hyphenation{ % more comments
455 ex-cep-tions, one per line; i ignore this part for now
458 this code is NOT robust against variations. unfortunately,
459 it looks like every local language version of this file has
460 a different format. i have also made no provision for weird
464 static int readpats(void)
467 char buf[200], buf1[200];
469 if ((fp = fopen(unsharp(TEXHYPHENS), "r")) == NULL
470 && (fp = fopen(unsharp(DWBalthyphens), "r")) == NULL) {
471 ERROR "warning: can't find hyphen.tex" WARN;
475 while (fgets(buf, sizeof buf, fp) != NULL) {
476 sscanf(buf, "%s", buf1);
477 if (strcmp(buf1, "\\patterns{") == 0)
480 while (fgets(buf, sizeof buf, fp) != NULL) {
490 static void install(char *s) /* map ab4c5de to: 12 abcde \0 00405 \0 */
493 char num[500], *onextpat = nextpat;
496 *nextpat++ = ' '; /* fill in with count later */
497 for (npat = lastpat = 0; *s != '\n' && *s != '\0'; s++) {
508 if (nextpat > pats + sizeof(pats)-20) {
509 ERROR "tex hyphenation table overflow, tail end ignored" WARN;
513 strcat(nextpat, num);
514 nextpat += strlen(nextpat) + 1;
517 static void fixup(void) /* build indexes of where . a b c ... start */
522 for (lastc = pats, p = pats+1; p < nextpat; p++)
528 for (p = pats+1; p < nextpat; ) {
529 n = trieindex(p[0], p[1]);
534 /* printf("pats = %d\n", nextpat - pats); */
537 static int trieindex(int d1, int d2)
539 return 27 * (d1 == '.' ? 0 : d1 - 'a' + 1) + (d2 == '.' ? 0 : d2 - 'a' + 1);