dpost.ps: increase linewidth to match groff
[troff.git] / eqn / text.c
blobb6ae9653a43838f9fad8aeeca067661ce7ddc343
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 */
20 /*OT*/ { 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 0 }, /* OTHER */
21 /*OL*/ { 1, 0, 1, 1, 1, 1, 1, 2, 2, 2, 0 }, /* OLET */
22 /*IL*/ { 1, 1, 0, 1, 1, 1, 1, 3, 2, 1, 0 }, /* ILET */
23 /*DG*/ { 1, 1, 1, 0, 1, 1, 1, 2, 2, 2, 0 }, /* DIG */
24 /*LP*/ { 1, 1, 1, 1, 1, 2, 1, 2, 3, 3, 0 }, /* LPAR */
25 /*RP*/ { 2, 2, 2, 1, 1, 1, 1, 2, 3, 3, 0 }, /* RPAR */
26 /*SL*/ { 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 0 }, /* SLASH */
27 /*PL*/ { 2, 2, 2, 2, 2, 2, 3, 2, 3, 2, 0 }, /* PLUS */
28 /*IF*/ { 3, 3, 1, 2, 2, 3, 2, 3, 0, 1, 1 }, /* ILETF */
29 /*IJ*/ { 1, 1, 1, 1, 1, 1, 1, 2, 2, 0, 0 }, /* ILETJ */
30 /*VB*/ { 4, 4, 4, 4, 4, 4, 4, 4, 5, 4, 1 }, /* VBAR */
34 extern void shim(int, int);
35 extern void roman(int);
36 extern void sadd(char *);
37 extern void cadd(int);
38 extern int trans(int, char *);
40 static int readutf8(int *dst, char *src)
42 int l = 1;
43 char *s = src;
44 if (!*s)
45 return 0;
46 if (~*s & 0xc0) {
47 *dst = *s;
48 return 1;
50 while (l < 6 && *s & (0x40 >> l))
51 l++;
52 *dst = (0x3f >> l) & *s++;
53 while (l--)
54 *dst = (*dst << 6) | (*s++ & 0x3f);
55 return s - src;
58 static int writeutf8(char *dst, int c)
60 char *d = dst;
61 int l;
62 if (c > 0xffff) {
63 *d++ = 0xf0 | (c >> 18);
64 l = 3;
65 } else if (c > 0x7ff) {
66 *d++ = 0xe0 | (c >> 12);
67 l = 2;
68 } else if (c > 0x7f) {
69 *d++ = 0xc0 | (c >> 6);
70 l = 1;
71 } else {
72 *d++ = c > 0 ? c : ' ';
73 l = 0;
75 while (l--)
76 *d++ = 0x80 | ((c >> (l * 6)) & 0x3f);
77 return d - dst;
80 int textc(void) /* read next UTF rune from psp */
82 int r;
83 int w;
85 w = readutf8(&r, psp);
86 if(w == 0){
87 psp++;
88 return 0;
90 if(w < 0){
91 psp += 1;
92 return 0x80; /* Plan 9-ism */
94 psp += w;
95 return r;
98 void text(int t, char *p1) /* convert text string p1 of type t */
100 int c;
101 char *p;
102 tbl *tp;
104 yyval = salloc();
105 ebase[yyval] = 0;
106 eht[yyval] = EM(1.0, ps); /* ht in ems of orig size */
107 lfont[yyval] = rfont[yyval] = ROM;
108 lclass[yyval] = rclass[yyval] = OTHER;
109 if (t == QTEXT) {
110 for (p = p1; *p; p++) /* scan for embedded \f's */
111 if (*p == '\\' && *(p+1) == 'f')
112 break;
113 if (*p) /* if found \f, leave it alone and hope */
114 p = p1;
115 else {
116 sprintf(cs, "\\f%s%s\\fP", ftp->name, p1);
117 p = cs;
119 } else if (t == SPACE)
120 p = "\\ ";
121 else if (t == THIN)
122 p = "\\|";
123 else if (t == TAB)
124 p = "\\t";
125 else if ((tp = lookup(restbl, p1)) != NULL) {
126 p = tp->cval;
127 } else {
128 lf = rf = 0;
129 lastft = 0;
130 nclass = NONE; /* get started with no class == no pad */
131 csp = cs;
132 for (psp = p1; (c = textc()) != '\0'; ) {
133 nextft = ft;
134 pclass = nclass;
135 rf = trans(c, p1);
136 if (lf == 0) {
137 lf = rf; /* left stuff is first found */
138 lclass[yyval] = nclass;
140 if (csp-cs > CSSIZE)
141 ERROR "converted token %.25s... too long", p1 FATAL ;
143 sadd("\\fP");
144 *csp = '\0';
145 p = cs;
146 lfont[yyval] = lf;
147 rfont[yyval] = rf;
148 rclass[yyval] = nclass;
150 dprintf(".\t%dtext: S%d <- %s; b=%g,h=%g,lf=%c,rf=%c,ps=%d\n",
151 t, yyval, p, ebase[yyval], eht[yyval], lfont[yyval], rfont[yyval], ps);
152 printf(".ds %d \"%s\n", yyval, p);
155 int isalpharune(int c)
157 return ('a'<=c && c<='z') || ('A'<=c && c<='Z');
160 int isdigitrune(int c)
162 return ('0'<=c && c<='9');
165 trans(int c, char *p1)
167 int f;
169 if (isalpharune(c) && ft == ITAL && c != 'f' && c != 'j') { /* italic letter */
170 shim(pclass, nclass = ILET);
171 cadd(c);
172 return ITAL;
174 if (isalpharune(c) && ft != ITAL) { /* other letter */
175 shim(pclass, nclass = OLET);
176 cadd(c);
177 return ROM;
179 if (isdigitrune(c)) {
180 shim(pclass, nclass = DIG);
181 roman(c);
182 return ROM; /* this is the right side font of this object */
184 f = ROM;
185 nclass = OTHER;
186 switch (c) {
187 case ':': case ';': case '!': case '%': case '?':
188 shim(pclass, nclass);
189 roman(c);
190 return f;
191 case '(': case '[':
192 shim(pclass, nclass = LPAR);
193 roman(c);
194 return f;
195 case ')': case ']':
196 shim(pclass, nclass = RPAR);
197 roman(c);
198 return f;
199 case ',':
200 shim(pclass, nclass = OTHER);
201 roman(c);
202 return f;
203 case '.':
204 if (rf == ROM)
205 roman(c);
206 else
207 cadd(c);
208 return f;
209 case '|': /* postscript needs help with default width! */
210 shim(pclass, nclass = VBAR);
211 sadd("\\v'.17m'\\z|\\v'-.17m'\\|"); /* and height */
212 return f;
213 case '=':
214 shim(pclass, nclass = PLUS);
215 sadd("\\(eq");
216 return f;
217 case '+':
218 shim(pclass, nclass = PLUS);
219 sadd("\\(pl");
220 return f;
221 case '>':
222 case '<': /* >, >=, >>, <, <-, <=, << */
223 shim(pclass, nclass = PLUS);
224 if (*psp == '=') {
225 sadd(c == '<' ? "\\(<=" : "\\(>=");
226 psp++;
227 } else if (c == '<' && *psp == '-') { /* <- only */
228 sadd("\\(<-");
229 psp++;
230 } else if (*psp == c) { /* << or >> */
231 cadd(c);
232 cadd(c);
233 psp++;
234 } else {
235 cadd(c);
237 return f;
238 case '-':
239 shim(pclass, nclass = PLUS); /* probably too big for ->'s */
240 if (*psp == '>') {
241 sadd("\\(->");
242 psp++;
243 } else {
244 sadd("\\(mi");
246 return f;
247 case '/':
248 shim(pclass, nclass = SLASH);
249 cadd('/');
250 return f;
251 case '~':
252 case ' ':
253 sadd("\\|\\|");
254 return f;
255 case '^':
256 sadd("\\|");
257 return f;
258 case '\\': /* troff - pass only \(xx without comment */
259 shim(pclass, nclass);
260 cadd('\\');
261 cadd(c = *psp++);
262 if (c == '(' && *psp && *(psp+1)) {
263 cadd(*psp++);
264 cadd(*psp++);
265 } else
266 fprintf(stderr, "eqn warning: unquoted troff command \\%c, file %s:%d\n",
267 c, curfile->fname, curfile->lineno);
268 return f;
269 case '\'':
270 shim(pclass, nclass);
271 sadd("\\(fm");
272 return f;
274 case 'f':
275 if (ft == ITAL) {
276 shim(pclass, nclass = ILETF);
277 cadd('f');
278 f = ITAL;
279 } else
280 cadd('f');
281 return f;
282 case 'j':
283 if (ft == ITAL) {
284 shim(pclass, nclass = ILETJ);
285 cadd('j');
286 f = ITAL;
287 } else
288 cadd('j');
289 return f;
290 default:
291 shim(pclass, nclass);
292 cadd(c);
293 return ft==ITAL ? ITAL : ROM;
297 char *pad(int n) /* return the padding as a string */
299 static char buf[20];
301 buf[0] = 0;
302 if (n < 0) {
303 sprintf(buf, "\\h'-%du*\\w'\\^'u'", -n);
304 return buf;
306 for ( ; n > 1; n -= 2)
307 strcat(buf, "\\|");
308 if (n > 0)
309 strcat(buf, "\\^");
310 return buf;
313 void shim(int lc, int rc) /* add padding space suitable to left and right classes */
315 sadd(pad(class[lc][rc]));
318 void roman(int c) /* add char c in "roman" font */
320 nextft = ROM;
321 cadd(c);
324 void sadd(char *s) /* add string s to cs */
326 while (*s)
327 cadd(*s++);
330 void cadd(int c) /* add character c to end of cs */
332 char *p;
333 int w;
335 if (lastft != nextft) {
336 if (lastft != 0) {
337 *csp++ = '\\';
338 *csp++ = 'f';
339 *csp++ = 'P';
341 *csp++ = '\\';
342 *csp++ = 'f';
343 if (ftp == ftstack) { /* bottom level */
344 if (ftp->ft == ITAL) /* usual case */
345 *csp++ = nextft;
346 else /* gfont set, use it */
347 for (p = ftp->name; *csp = *p++; )
348 csp++;
349 } else { /* inside some kind of font ... */
350 for (p = ftp->name; *csp = *p++; )
351 csp++;
353 lastft = nextft;
355 w = writeutf8(csp, c);
356 if(w > 0) /* ignore bad characters */
357 csp += w;