n8.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. #include "tdef.h"
  2. #include "fns.h"
  3. #include "ext.h"
  4. #define HY_BIT 0200 /* stuff in here only works for 7-bit ascii */
  5. /* this value is used (as a literal) in suftab.c */
  6. /* to encode possible hyphenation points in suffixes. */
  7. /* it could be changed, by widening the tables */
  8. /* to be shorts instead of chars. */
  9. /*
  10. * troff8.c
  11. *
  12. * hyphenation
  13. */
  14. int hexsize = 0; /* hyphenation exception list size */
  15. char *hbufp = NULL; /* base of list */
  16. char *nexth = NULL; /* first free slot in list */
  17. Tchar *hyend;
  18. #define THRESH 160 /* digram goodness threshold */
  19. int thresh = THRESH;
  20. int texhyphen(void);
  21. static int alpha(Tchar);
  22. void hyphen(Tchar *wp)
  23. {
  24. int j;
  25. Tchar *i;
  26. i = wp;
  27. while (punct((*i++)))
  28. ;
  29. if (!alpha(*--i))
  30. return;
  31. wdstart = i++;
  32. while (alpha(*i++))
  33. ;
  34. hyend = wdend = --i - 1;
  35. while (punct((*i++)))
  36. ;
  37. if (*--i)
  38. return;
  39. if (wdend - wdstart < 4) /* 4 chars is too short to hyphenate */
  40. return;
  41. hyp = hyptr;
  42. *hyp = 0;
  43. hyoff = 2;
  44. /* for now, try exceptions first, then tex (if hyphalg is non-zero),
  45. then suffix and digram if tex didn't hyphenate it at all.
  46. */
  47. if (!exword() && !texhyphen() && !suffix())
  48. digram();
  49. /* this appears to sort hyphenation points into increasing order */
  50. *hyp++ = 0;
  51. if (*hyptr)
  52. for (j = 1; j; ) {
  53. j = 0;
  54. for (hyp = hyptr + 1; *hyp != 0; hyp++) {
  55. if (*(hyp - 1) > *hyp) {
  56. j++;
  57. i = *hyp;
  58. *hyp = *(hyp - 1);
  59. *(hyp - 1) = i;
  60. }
  61. }
  62. }
  63. }
  64. static alpha(Tchar i) /* non-zero if really alphabetic */
  65. {
  66. if (ismot(i))
  67. return 0;
  68. else if (cbits(i) >= ALPHABET) /* this isn't very elegant, but there's */
  69. return 0; /* no good way to make sure i is in range for */
  70. else /* the call of isalpha */
  71. return isalpha(cbits(i));
  72. }
  73. punct(Tchar i)
  74. {
  75. if (!i || alpha(i))
  76. return(0);
  77. else
  78. return(1);
  79. }
  80. void caseha(void) /* set hyphenation algorithm */
  81. {
  82. hyphalg = HYPHALG;
  83. if (skip())
  84. return;
  85. noscale++;
  86. hyphalg = atoi0();
  87. noscale = 0;
  88. }
  89. void caseht(void) /* set hyphenation threshold; not in manual! */
  90. {
  91. thresh = THRESH;
  92. if (skip())
  93. return;
  94. noscale++;
  95. thresh = atoi0();
  96. noscale = 0;
  97. }
  98. char *growh(char *where)
  99. {
  100. char *new;
  101. hexsize += NHEX;
  102. if ((new = grow(hbufp, hexsize, sizeof(char))) == NULL)
  103. return NULL;
  104. if (new == hbufp) {
  105. return where;
  106. } else {
  107. int diff;
  108. diff = where - hbufp;
  109. hbufp = new;
  110. return new + diff;
  111. }
  112. }
  113. void casehw(void)
  114. {
  115. int i, k;
  116. char *j;
  117. Tchar t;
  118. if (nexth == NULL) {
  119. if ((nexth = hbufp = grow(hbufp, NHEX, sizeof(char))) == NULL) {
  120. ERROR "No space for exception word list." WARN;
  121. return;
  122. }
  123. hexsize = NHEX;
  124. }
  125. k = 0;
  126. while (!skip()) {
  127. if ((j = nexth) >= hbufp + hexsize - 2)
  128. if ((j = nexth = growh(j)) == NULL)
  129. goto full;
  130. for (;;) {
  131. if (ismot(t = getch()))
  132. continue;
  133. i = cbits(t);
  134. if (i == ' ' || i == '\n') {
  135. *j++ = 0;
  136. nexth = j;
  137. *j = 0;
  138. if (i == ' ')
  139. break;
  140. else
  141. return;
  142. }
  143. if (i == '-') {
  144. k = HY_BIT;
  145. continue;
  146. }
  147. *j++ = maplow(i) | k;
  148. k = 0;
  149. if (j >= hbufp + hexsize - 2)
  150. if ((j = growh(j)) == NULL)
  151. goto full;
  152. }
  153. }
  154. return;
  155. full:
  156. ERROR "Cannot grow exception word list." WARN;
  157. *nexth = 0;
  158. }
  159. int exword(void)
  160. {
  161. Tchar *w;
  162. char *e, *save;
  163. e = hbufp;
  164. while (1) {
  165. save = e;
  166. if (e == NULL || *e == 0)
  167. return(0);
  168. w = wdstart;
  169. while (*e && w <= hyend && (*e & 0177) == maplow(cbits(*w))) {
  170. e++;
  171. w++;
  172. }
  173. if (!*e) {
  174. if (w-1 == hyend || (w == wdend && maplow(cbits(*w)) == 's')) {
  175. w = wdstart;
  176. for (e = save; *e; e++) {
  177. if (*e & HY_BIT)
  178. *hyp++ = w;
  179. if (hyp > hyptr + NHYP - 1)
  180. hyp = hyptr + NHYP - 1;
  181. w++;
  182. }
  183. return(1);
  184. } else {
  185. e++;
  186. continue;
  187. }
  188. } else
  189. while (*e++)
  190. ;
  191. }
  192. }
  193. suffix(void)
  194. {
  195. Tchar *w;
  196. char *s, *s0;
  197. Tchar i;
  198. extern char *suftab[];
  199. again:
  200. i = cbits(*hyend);
  201. if (!alpha(i))
  202. return(0);
  203. if (i < 'a')
  204. i -= 'A' - 'a';
  205. if ((s0 = suftab[i-'a']) == 0)
  206. return(0);
  207. for (;;) {
  208. if ((i = *s0 & 017) == 0)
  209. return(0);
  210. s = s0 + i - 1;
  211. w = hyend - 1;
  212. while (s > s0 && w >= wdstart && (*s & 0177) == maplow(cbits(*w))) {
  213. s--;
  214. w--;
  215. }
  216. if (s == s0)
  217. break;
  218. s0 += i;
  219. }
  220. s = s0 + i - 1;
  221. w = hyend;
  222. if (*s0 & HY_BIT)
  223. goto mark;
  224. while (s > s0) {
  225. w--;
  226. if (*s-- & HY_BIT) {
  227. mark:
  228. hyend = w - 1;
  229. if (*s0 & 0100) /* 0100 used in suftab to encode something too */
  230. continue;
  231. if (!chkvow(w))
  232. return(0);
  233. *hyp++ = w;
  234. }
  235. }
  236. if (*s0 & 040)
  237. return(0);
  238. if (exword())
  239. return(1);
  240. goto again;
  241. }
  242. maplow(int i)
  243. {
  244. if (isupper(i))
  245. i = tolower(i);
  246. return(i);
  247. }
  248. vowel(int i)
  249. {
  250. switch (i) {
  251. case 'a': case 'A':
  252. case 'e': case 'E':
  253. case 'i': case 'I':
  254. case 'o': case 'O':
  255. case 'u': case 'U':
  256. case 'y': case 'Y':
  257. return(1);
  258. default:
  259. return(0);
  260. }
  261. }
  262. Tchar *chkvow(Tchar *w)
  263. {
  264. while (--w >= wdstart)
  265. if (vowel(cbits(*w)))
  266. return(w);
  267. return(0);
  268. }
  269. void digram(void)
  270. {
  271. Tchar *w;
  272. int val;
  273. Tchar *nhyend, *maxw;
  274. int maxval;
  275. extern char bxh[26][13], bxxh[26][13], xxh[26][13], xhx[26][13], hxx[26][13];
  276. again:
  277. if (!(w = chkvow(hyend + 1)))
  278. return;
  279. hyend = w;
  280. if (!(w = chkvow(hyend)))
  281. return;
  282. nhyend = w;
  283. maxval = 0;
  284. w--;
  285. while (++w < hyend && w < wdend - 1) {
  286. val = 1;
  287. if (w == wdstart)
  288. val *= dilook('a', cbits(*w), bxh);
  289. else if (w == wdstart + 1)
  290. val *= dilook(cbits(*(w-1)), cbits(*w), bxxh);
  291. else
  292. val *= dilook(cbits(*(w-1)), cbits(*w), xxh);
  293. val *= dilook(cbits(*w), cbits(*(w+1)), xhx);
  294. val *= dilook(cbits(*(w+1)), cbits(*(w+2)), hxx);
  295. if (val > maxval) {
  296. maxval = val;
  297. maxw = w + 1;
  298. }
  299. }
  300. hyend = nhyend;
  301. if (maxval > thresh)
  302. *hyp++ = maxw;
  303. goto again;
  304. }
  305. dilook(int a, int b, char t[26][13])
  306. {
  307. int i, j;
  308. i = t[maplow(a)-'a'][(j = maplow(b)-'a')/2];
  309. if (!(j & 01))
  310. i >>= 4;
  311. return(i & 017);
  312. }
  313. /* here beginneth the tex hyphenation code, as interpreted freely */
  314. /* the main difference is that there is no attempt to squeeze space */
  315. /* as tightly at tex does. */
  316. static int texit(Tchar *, Tchar *);
  317. static int readpats(void);
  318. static void install(char *);
  319. static void fixup(void);
  320. static int trieindex(int, int);
  321. static char pats[50000]; /* size ought to be computed dynamically */
  322. static char *nextpat = pats;
  323. static char *trie[27*27]; /* english-specific sizes */
  324. int texhyphen(void)
  325. {
  326. static int loaded = 0; /* -1: couldn't find tex file */
  327. if (hyphalg == 0 || loaded == -1) /* non-zero => tex for now */
  328. return 0;
  329. if (loaded == 0) {
  330. if (readpats())
  331. loaded = 1;
  332. else
  333. loaded = -1;
  334. }
  335. return texit(wdstart, wdend);
  336. }
  337. static int texit(Tchar *start, Tchar *end) /* hyphenate as in tex, return # found */
  338. {
  339. int nw, i, k, equal, cnt[500];
  340. char w[500+1], *np, *pp, *wp, *xpp, *xwp;
  341. w[0] = '.';
  342. for (nw = 1; start <= end && nw < 500-1; nw++, start++)
  343. w[nw] = maplow(tolower(cbits(*start)));
  344. start -= (nw - 1);
  345. w[nw++] = '.';
  346. w[nw] = 0;
  347. /*
  348. * printf("try %s\n", w);
  349. */
  350. for (i = 0; i <= nw; i++)
  351. cnt[i] = '0';
  352. for (wp = w; wp < w + nw; wp++) {
  353. for (pp = trie[trieindex(*wp, *(wp+1))]; pp < nextpat; ) {
  354. if (pp == 0 /* no trie entry */
  355. || *pp != *wp /* no match on 1st letter */
  356. || *(pp+1) != *(wp+1)) /* no match on 2nd letter */
  357. break; /* so move to next letter of word */
  358. equal = 1;
  359. for (xpp = pp+2, xwp = wp+2; *xpp; )
  360. if (*xpp++ != *xwp++) {
  361. equal = 0;
  362. break;
  363. }
  364. if (equal) {
  365. np = xpp+1; /* numpat */
  366. for (k = wp-w; *np; k++, np++)
  367. if (*np > cnt[k])
  368. cnt[k] = *np;
  369. /*
  370. * printf("match: %s %s\n", pp, xpp+1);
  371. */
  372. }
  373. pp += *(pp-1); /* skip over pattern and numbers to next */
  374. }
  375. }
  376. /*
  377. * for (i = 0; i < nw; i++) printf("%c", w[i]);
  378. * printf(" ");
  379. * for (i = 0; i <= nw; i++) printf("%c", cnt[i]);
  380. * printf("\n");
  381. */
  382. /*
  383. * for (i = 1; i < nw - 1; i++) {
  384. * if (i > 2 && i < nw - 3 && cnt[i] % 2)
  385. * printf("-");
  386. * if (cbits(start[i-1]) != '.')
  387. * printf("%c", cbits(start[i-1]));
  388. * }
  389. * printf("\n");
  390. */
  391. for (i = 1; i < nw -1; i++)
  392. if (i > 2 && i < nw - 3 && cnt[i] % 2)
  393. *hyp++ = start + i - 1;
  394. return hyp - hyptr; /* non-zero if a hyphen was found */
  395. }
  396. /*
  397. This code assumes that hyphen.tex looks like
  398. % some comments
  399. \patterns{ % more comments
  400. pat5ter4ns, 1 per line, SORTED, nothing else
  401. }
  402. more goo
  403. \hyphenation{ % more comments
  404. ex-cep-tions, one per line; i ignore this part for now
  405. }
  406. this code is NOT robust against variations. unfortunately,
  407. it looks like every local language version of this file has
  408. a different format. i have also made no provision for weird
  409. characters. sigh.
  410. */
  411. static int readpats(void)
  412. {
  413. FILE *fp;
  414. char buf[200], buf1[200];
  415. if ((fp = fopen(TEXHYPHENS, "r")) == NULL
  416. && (fp = fopen(DWBalthyphens, "r")) == NULL) {
  417. ERROR "warning: can't find hyphen.tex" WARN;
  418. return 0;
  419. }
  420. while (fgets(buf, sizeof buf, fp) != NULL) {
  421. sscanf(buf, "%s", buf1);
  422. if (strcmp(buf1, "\\patterns{") == 0)
  423. break;
  424. }
  425. while (fgets(buf, sizeof buf, fp) != NULL) {
  426. if (buf[0] == '}')
  427. break;
  428. install(buf);
  429. }
  430. fclose(fp);
  431. fixup();
  432. return 1;
  433. }
  434. static void install(char *s) /* map ab4c5de to: 12 abcde \0 00405 \0 */
  435. {
  436. int npat, lastpat;
  437. char num[500], *onextpat = nextpat;
  438. num[0] = '0';
  439. *nextpat++ = ' '; /* fill in with count later */
  440. for (npat = lastpat = 0; *s != '\n' && *s != '\0'; s++) {
  441. if (isdigit(*s)) {
  442. num[npat] = *s;
  443. lastpat = npat;
  444. } else {
  445. *nextpat++ = *s;
  446. npat++;
  447. num[npat] = '0';
  448. }
  449. }
  450. *nextpat++ = 0;
  451. if (nextpat > pats + sizeof(pats)-20) {
  452. ERROR "tex hyphenation table overflow, tail end ignored" WARN;
  453. nextpat = onextpat;
  454. }
  455. num[lastpat+1] = 0;
  456. strcat(nextpat, num);
  457. nextpat += strlen(nextpat) + 1;
  458. }
  459. static void fixup(void) /* build indexes of where . a b c ... start */
  460. {
  461. char *p, *lastc;
  462. int n;
  463. for (lastc = pats, p = pats+1; p < nextpat; p++)
  464. if (*p == ' ') {
  465. *lastc = p - lastc;
  466. lastc = p;
  467. }
  468. *lastc = p - lastc;
  469. for (p = pats+1; p < nextpat; ) {
  470. n = trieindex(p[0], p[1]);
  471. if (trie[n] == 0)
  472. trie[n] = p;
  473. p += p[-1];
  474. }
  475. /* printf("pats = %d\n", nextpat - pats); */
  476. }
  477. static int trieindex(int d1, int d2)
  478. {
  479. return 27 * (d1 == '.' ? 0 : d1 - 'a' + 1) + (d2 == '.' ? 0 : d2 - 'a' + 1);
  480. }