eqn: adjust the padding space around commas
[troff.git] / eqn / text.c
bloba2e75ac83bc1f0479ec0c02c41bdf5212f11b870
1 #include "e.h"
2 #include "y.tab.h"
3 #include <ctype.h>
5 #define CSSIZE 1000
6 char cs[CSSIZE+20]; /* text string converted into this */
7 char *csp; /* next spot in cs[] */
8 char *psp; /* next character in input token */
10 int lf, rf; /* temporary spots for left and right fonts */
11 int lastft; /* last \f added */
12 int nextft; /* next \f to be added */
14 int pclass; /* class of previous character */
15 int nclass; /* class of next character */
17 int class[LAST][LAST] ={ /* guesswork, tuned to times roman postscript */
19 /*OT OL IL DG LP RP SL PL IF IJ VB CM */
20 /*OT*/ { 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 0, 0 }, /* OTHER */
21 /*OL*/ { 1, 0, 1, 1, 1, 1, 1, 2, 2, 2, 0, 0 }, /* OLET */
22 /*IL*/ { 1, 1, 0, 1, 1, 1, 1, 3, 2, 1, 0, 0 }, /* ILET */
23 /*DG*/ { 1, 1, 1, 0, 1, 1, 1, 2, 2, 2, 0, 0 }, /* DIG */
24 /*LP*/ { 1, 1, 1, 1, 1, 2, 1, 2, 3, 3, 0, 0 }, /* LPAR */
25 /*RP*/ { 2, 2, 2, 1, 1, 1, 1, 2, 3, 3, 0, 0 }, /* RPAR */
26 /*SL*/ { 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 0 }, /* SLASH */
27 /*PL*/ { 2, 2, 2, 2, 2, 2, 3, 2, 3, 2, 0, 0 }, /* PLUS */
28 /*IF*/ { 3, 3, 1, 2, 2, 3, 2, 3, 0, 1, 1, 0 }, /* ILETF */
29 /*IJ*/ { 1, 1, 1, 1, 1, 1, 1, 2, 2, 0, 0, 0 }, /* ILETJ */
30 /*VB*/ { 1, 1, 0, 1, 1, 1, 1, 1, 2, 1, 1, 1 }, /* VBAR */
31 /*CM*/ { 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 }, /* COMMA */
35 extern void shim(int, int);
36 extern void roman(int);
37 extern void sadd(char *);
38 extern void cadd(int);
39 extern int trans(int, char *);
41 static int readutf8(int *dst, char *src)
43 int l = 1;
44 char *s = src;
45 if (!*s)
46 return 0;
47 if (~*s & 0xc0) {
48 *dst = *s;
49 return 1;
51 while (l < 6 && *s & (0x40 >> l))
52 l++;
53 *dst = (0x3f >> l) & *s++;
54 while (l--)
55 *dst = (*dst << 6) | (*s++ & 0x3f);
56 return s - src;
59 static int writeutf8(char *dst, int c)
61 char *d = dst;
62 int l;
63 if (c > 0xffff) {
64 *d++ = 0xf0 | (c >> 18);
65 l = 3;
66 } else if (c > 0x7ff) {
67 *d++ = 0xe0 | (c >> 12);
68 l = 2;
69 } else if (c > 0x7f) {
70 *d++ = 0xc0 | (c >> 6);
71 l = 1;
72 } else {
73 *d++ = c > 0 ? c : ' ';
74 l = 0;
76 while (l--)
77 *d++ = 0x80 | ((c >> (l * 6)) & 0x3f);
78 return d - dst;
81 int textc(void) /* read next UTF rune from psp */
83 int r;
84 int w;
86 w = readutf8(&r, psp);
87 if(w == 0){
88 psp++;
89 return 0;
91 if(w < 0){
92 psp += 1;
93 return 0x80; /* Plan 9-ism */
95 psp += w;
96 return r;
99 void text(int t, char *p1) /* convert text string p1 of type t */
101 int c;
102 char *p;
103 tbl *tp;
105 yyval = salloc();
106 ebase[yyval] = 0;
107 eht[yyval] = EM(1.0, ps); /* ht in ems of orig size */
108 lfont[yyval] = rfont[yyval] = ROM;
109 lclass[yyval] = rclass[yyval] = OTHER;
110 if (t == QTEXT) {
111 for (p = p1; *p; p++) /* scan for embedded \f's */
112 if (*p == '\\' && *(p+1) == 'f')
113 break;
114 if (*p) /* if found \f, leave it alone and hope */
115 p = p1;
116 else {
117 sprintf(cs, "\\f%s%s\\fP", ftp->name, p1);
118 p = cs;
120 } else if (t == SPACE)
121 p = "\\ ";
122 else if (t == THIN)
123 p = "\\|";
124 else if (t == TAB)
125 p = "\\t";
126 else if ((tp = lookup(restbl, p1)) != NULL) {
127 p = tp->cval;
128 } else {
129 lf = rf = 0;
130 lastft = 0;
131 nclass = NONE; /* get started with no class == no pad */
132 csp = cs;
133 for (psp = p1; (c = textc()) != '\0'; ) {
134 nextft = ft;
135 pclass = nclass;
136 rf = trans(c, p1);
137 if (lf == 0) {
138 lf = rf; /* left stuff is first found */
139 lclass[yyval] = nclass;
141 if (csp-cs > CSSIZE)
142 ERROR "converted token %.25s... too long", p1 FATAL ;
144 sadd("\\fP");
145 *csp = '\0';
146 p = cs;
147 lfont[yyval] = lf;
148 rfont[yyval] = rf;
149 rclass[yyval] = nclass;
151 dprintf(".\t%dtext: S%d <- %s; b=%g,h=%g,lf=%c,rf=%c,ps=%d\n",
152 t, yyval, p, ebase[yyval], eht[yyval], lfont[yyval], rfont[yyval], ps);
153 printf(".ds %d \"%s\n", yyval, p);
156 int isalpharune(int c)
158 return ('a'<=c && c<='z') || ('A'<=c && c<='Z');
161 int isdigitrune(int c)
163 return ('0'<=c && c<='9');
166 trans(int c, char *p1)
168 int f;
170 if (isalpharune(c) && ft == ITAL && c != 'f' && c != 'j') { /* italic letter */
171 shim(pclass, nclass = ILET);
172 cadd(c);
173 return ITAL;
175 if (isalpharune(c) && ft != ITAL) { /* other letter */
176 shim(pclass, nclass = OLET);
177 cadd(c);
178 return ROM;
180 if (isdigitrune(c)) {
181 shim(pclass, nclass = DIG);
182 roman(c);
183 return ROM; /* this is the right side font of this object */
185 f = ROM;
186 nclass = OTHER;
187 switch (c) {
188 case ':': case ';': case '!': case '%': case '?':
189 shim(pclass, nclass);
190 roman(c);
191 return f;
192 case '(': case '[':
193 shim(pclass, nclass = LPAR);
194 roman(c);
195 return f;
196 case ')': case ']':
197 shim(pclass, nclass = RPAR);
198 roman(c);
199 return f;
200 case ',':
201 shim(pclass, nclass = COMMA);
202 roman(c);
203 return f;
204 case '.':
205 if (rf == ROM)
206 roman(c);
207 else
208 cadd(c);
209 return f;
210 case '|': /* postscript needs help with default width! */
211 shim(pclass, nclass = VBAR);
212 sadd("|"); /* and height */
213 return f;
214 case '=':
215 shim(pclass, nclass = PLUS);
216 sadd("\\(eq");
217 return f;
218 case '+':
219 shim(pclass, nclass = PLUS);
220 sadd("\\(pl");
221 return f;
222 case '>':
223 case '<': /* >, >=, >>, <, <-, <=, << */
224 shim(pclass, nclass = PLUS);
225 if (*psp == '=') {
226 sadd(c == '<' ? "\\(<=" : "\\(>=");
227 psp++;
228 } else if (c == '<' && *psp == '-') { /* <- only */
229 sadd("\\(<-");
230 psp++;
231 } else if (*psp == c) { /* << or >> */
232 cadd(c);
233 cadd(c);
234 psp++;
235 } else {
236 cadd(c);
238 return f;
239 case '-':
240 shim(pclass, nclass = PLUS); /* probably too big for ->'s */
241 if (*psp == '>') {
242 sadd("\\(->");
243 psp++;
244 } else {
245 sadd("\\(mi");
247 return f;
248 case '/':
249 shim(pclass, nclass = SLASH);
250 cadd('/');
251 return f;
252 case '~':
253 case ' ':
254 sadd("\\|\\|");
255 return f;
256 case '^':
257 sadd("\\|");
258 return f;
259 case '\\': /* troff - pass only \(xx without comment */
260 shim(pclass, nclass);
261 cadd('\\');
262 cadd(c = *psp++);
263 if (c == '(' && *psp && *(psp+1)) {
264 cadd(*psp++);
265 cadd(*psp++);
266 } else
267 fprintf(stderr, "eqn warning: unquoted troff command \\%c, file %s:%d\n",
268 c, curfile->fname, curfile->lineno);
269 return f;
270 case '\'':
271 shim(pclass, nclass);
272 sadd("\\(fm");
273 return f;
275 case 'f':
276 if (ft == ITAL) {
277 shim(pclass, nclass = ILETF);
278 cadd('f');
279 f = ITAL;
280 } else
281 cadd('f');
282 return f;
283 case 'j':
284 if (ft == ITAL) {
285 shim(pclass, nclass = ILETJ);
286 cadd('j');
287 f = ITAL;
288 } else
289 cadd('j');
290 return f;
291 default:
292 shim(pclass, nclass);
293 cadd(c);
294 return ft==ITAL ? ITAL : ROM;
298 char *pad(int n) /* return the padding as a string */
300 static char buf[20];
302 buf[0] = 0;
303 if (n < 0) {
304 sprintf(buf, "\\h'-%du*\\w'\\^'u'", -n);
305 return buf;
307 for ( ; n > 1; n -= 2)
308 strcat(buf, "\\|");
309 if (n > 0)
310 strcat(buf, "\\^");
311 return buf;
314 void shim(int lc, int rc) /* add padding space suitable to left and right classes */
316 sadd(pad(class[lc][rc]));
319 void roman(int c) /* add char c in "roman" font */
321 nextft = ROM;
322 cadd(c);
325 void sadd(char *s) /* add string s to cs */
327 while (*s)
328 cadd(*s++);
331 void cadd(int c) /* add character c to end of cs */
333 char *p;
334 int w;
336 if (lastft != nextft) {
337 if (lastft != 0) {
338 *csp++ = '\\';
339 *csp++ = 'f';
340 *csp++ = 'P';
342 *csp++ = '\\';
343 *csp++ = 'f';
344 if (ftp == ftstack) { /* bottom level */
345 if (ftp->ft == ITAL) /* usual case */
346 *csp++ = nextft;
347 else /* gfont set, use it */
348 for (p = ftp->name; *csp = *p++; )
349 csp++;
350 } else { /* inside some kind of font ... */
351 for (p = ftp->name; *csp = *p++; )
352 csp++;
354 lastft = nextft;
356 w = writeutf8(csp, c);
357 if(w > 0) /* ignore bad characters */
358 csp += w;