]> git.cameronkatri.com Git - mandoc.git/blob - term.c
Correct support for `\fX' font modes in -Tascii.
[mandoc.git] / term.c
1 /* $Id: term.c,v 1.122 2009/11/05 08:40:16 kristaps Exp $ */
2 /*
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17 #include <assert.h>
18 #include <ctype.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <time.h>
23
24 #include "chars.h"
25 #include "out.h"
26 #include "term.h"
27 #include "man.h"
28 #include "mdoc.h"
29 #include "main.h"
30
31 /* FIXME: accomodate non-breaking, non-collapsing white-space. */
32 /* FIXME: accomodate non-breaking, collapsing white-space. */
33
34 static struct termp *term_alloc(enum termenc);
35 static void term_free(struct termp *);
36
37 static void do_escaped(struct termp *, const char **);
38 static void do_special(struct termp *,
39 const char *, size_t);
40 static void do_reserved(struct termp *,
41 const char *, size_t);
42 static void buffer(struct termp *, char);
43 static void encode(struct termp *, char);
44
45
46 void *
47 ascii_alloc(void)
48 {
49
50 return(term_alloc(TERMENC_ASCII));
51 }
52
53
54 void
55 terminal_free(void *arg)
56 {
57
58 term_free((struct termp *)arg);
59 }
60
61
62 static void
63 term_free(struct termp *p)
64 {
65
66 if (p->buf)
67 free(p->buf);
68 if (p->symtab)
69 chars_free(p->symtab);
70
71 free(p);
72 }
73
74
75 static struct termp *
76 term_alloc(enum termenc enc)
77 {
78 struct termp *p;
79
80 p = calloc(1, sizeof(struct termp));
81 if (NULL == p) {
82 perror(NULL);
83 exit(EXIT_FAILURE);
84 }
85 p->maxrmargin = 78;
86 p->enc = enc;
87 return(p);
88 }
89
90
91 /*
92 * Flush a line of text. A "line" is loosely defined as being something
93 * that should be followed by a newline, regardless of whether it's
94 * broken apart by newlines getting there. A line can also be a
95 * fragment of a columnar list.
96 *
97 * Specifically, a line is whatever's in p->buf of length p->col, which
98 * is zeroed after this function returns.
99 *
100 * The usage of termp:flags is as follows:
101 *
102 * - TERMP_NOLPAD: when beginning to write the line, don't left-pad the
103 * offset value. This is useful when doing columnar lists where the
104 * prior column has right-padded.
105 *
106 * - TERMP_NOBREAK: this is the most important and is used when making
107 * columns. In short: don't print a newline and instead pad to the
108 * right margin. Used in conjunction with TERMP_NOLPAD.
109 *
110 * - TERMP_TWOSPACE: when padding, make sure there are at least two
111 * space characters of padding. Otherwise, rather break the line.
112 *
113 * - TERMP_DANGLE: don't newline when TERMP_NOBREAK is specified and
114 * the line is overrun, and don't pad-right if it's underrun.
115 *
116 * - TERMP_HANG: like TERMP_DANGLE, but doesn't newline when
117 * overruning, instead save the position and continue at that point
118 * when the next invocation.
119 *
120 * In-line line breaking:
121 *
122 * If TERMP_NOBREAK is specified and the line overruns the right
123 * margin, it will break and pad-right to the right margin after
124 * writing. If maxrmargin is violated, it will break and continue
125 * writing from the right-margin, which will lead to the above scenario
126 * upon exit. Otherwise, the line will break at the right margin.
127 */
128 void
129 term_flushln(struct termp *p)
130 {
131 int i; /* current input position in p->buf */
132 size_t vis; /* current visual position on output */
133 size_t vbl; /* number of blanks to prepend to output */
134 size_t vsz; /* visual characters to write to output */
135 size_t bp; /* visual right border position */
136 int j; /* temporary loop index */
137 size_t maxvis, mmax;
138 static int overstep = 0;
139
140 /*
141 * First, establish the maximum columns of "visible" content.
142 * This is usually the difference between the right-margin and
143 * an indentation, but can be, for tagged lists or columns, a
144 * small set of values.
145 */
146
147 assert(p->offset < p->rmargin);
148
149 maxvis = (int)(p->rmargin - p->offset) - overstep < 0 ?
150 /* LINTED */
151 0 : p->rmargin - p->offset - overstep;
152 mmax = (int)(p->maxrmargin - p->offset) - overstep < 0 ?
153 /* LINTED */
154 0 : p->maxrmargin - p->offset - overstep;
155
156 bp = TERMP_NOBREAK & p->flags ? mmax : maxvis;
157
158 /*
159 * FIXME: if bp is zero, we still output the first word before
160 * breaking the line.
161 */
162
163 vis = 0;
164
165 /*
166 * If in the standard case (left-justified), then begin with our
167 * indentation, otherwise (columns, etc.) just start spitting
168 * out text.
169 */
170
171 if ( ! (p->flags & TERMP_NOLPAD))
172 /* LINTED */
173 for (j = 0; j < (int)p->offset; j++)
174 putchar(' ');
175
176 for (i = 0; i < (int)p->col; i++) {
177 /*
178 * Count up visible word characters. Control sequences
179 * (starting with the CSI) aren't counted. A space
180 * generates a non-printing word, which is valid (the
181 * space is printed according to regular spacing rules).
182 */
183
184 /* LINTED */
185 for (j = i, vsz = 0; j < (int)p->col; j++) {
186 if (j && ' ' == p->buf[j])
187 break;
188 else if (8 == p->buf[j])
189 vsz--;
190 else
191 vsz++;
192 }
193
194 /*
195 * Choose the number of blanks to prepend: no blank at the
196 * beginning of a line, one between words -- but do not
197 * actually write them yet.
198 */
199 vbl = (size_t)(0 == vis ? 0 : 1);
200
201 /*
202 * Find out whether we would exceed the right margin.
203 * If so, break to the next line. (TODO: hyphenate)
204 * Otherwise, write the chosen number of blanks now.
205 */
206 if (vis && vis + vbl + vsz > bp) {
207 putchar('\n');
208 if (TERMP_NOBREAK & p->flags) {
209 for (j = 0; j < (int)p->rmargin; j++)
210 putchar(' ');
211 vis = p->rmargin - p->offset;
212 } else {
213 for (j = 0; j < (int)p->offset; j++)
214 putchar(' ');
215 vis = 0;
216 }
217 /* Remove the overstep width. */
218 bp += (int)/* LINTED */
219 overstep;
220 overstep = 0;
221 } else {
222 for (j = 0; j < (int)vbl; j++)
223 putchar(' ');
224 vis += vbl;
225 }
226
227 /*
228 * Finally, write out the word.
229 */
230 for ( ; i < (int)p->col; i++) {
231 if (' ' == p->buf[i])
232 break;
233
234 /* The unit sep. is a non-breaking space. */
235 if (31 == p->buf[i])
236 putchar(' ');
237 else
238 putchar(p->buf[i]);
239 }
240 vis += vsz;
241 }
242
243 p->col = 0;
244 overstep = 0;
245
246 if ( ! (TERMP_NOBREAK & p->flags)) {
247 putchar('\n');
248 return;
249 }
250
251 if (TERMP_HANG & p->flags) {
252 /* We need one blank after the tag. */
253 overstep = /* LINTED */
254 vis - maxvis + 1;
255
256 /*
257 * Behave exactly the same way as groff:
258 * If we have overstepped the margin, temporarily move
259 * it to the right and flag the rest of the line to be
260 * shorter.
261 * If we landed right at the margin, be happy.
262 * If we are one step before the margin, temporarily
263 * move it one step LEFT and flag the rest of the line
264 * to be longer.
265 */
266 if (overstep >= -1) {
267 assert((int)maxvis + overstep >= 0);
268 /* LINTED */
269 maxvis += overstep;
270 } else
271 overstep = 0;
272
273 } else if (TERMP_DANGLE & p->flags)
274 return;
275
276 /* Right-pad. */
277 if (maxvis > vis + /* LINTED */
278 ((TERMP_TWOSPACE & p->flags) ? 1 : 0))
279 for ( ; vis < maxvis; vis++)
280 putchar(' ');
281 else { /* ...or newline break. */
282 putchar('\n');
283 for (i = 0; i < (int)p->rmargin; i++)
284 putchar(' ');
285 }
286 }
287
288
289 /*
290 * A newline only breaks an existing line; it won't assert vertical
291 * space. All data in the output buffer is flushed prior to the newline
292 * assertion.
293 */
294 void
295 term_newln(struct termp *p)
296 {
297
298 p->flags |= TERMP_NOSPACE;
299 if (0 == p->col) {
300 p->flags &= ~TERMP_NOLPAD;
301 return;
302 }
303 term_flushln(p);
304 p->flags &= ~TERMP_NOLPAD;
305 }
306
307
308 /*
309 * Asserts a vertical space (a full, empty line-break between lines).
310 * Note that if used twice, this will cause two blank spaces and so on.
311 * All data in the output buffer is flushed prior to the newline
312 * assertion.
313 */
314 void
315 term_vspace(struct termp *p)
316 {
317
318 term_newln(p);
319 putchar('\n');
320 }
321
322
323 static void
324 do_special(struct termp *p, const char *word, size_t len)
325 {
326 const char *rhs;
327 size_t sz;
328 int i;
329
330 rhs = chars_a2ascii(p->symtab, word, len, &sz);
331
332 if (NULL == rhs) {
333 #if 0
334 fputs("Unknown special character: ", stderr);
335 for (i = 0; i < (int)len; i++)
336 fputc(word[i], stderr);
337 fputc('\n', stderr);
338 #endif
339 return;
340 }
341 for (i = 0; i < (int)sz; i++)
342 encode(p, rhs[i]);
343 }
344
345
346 static void
347 do_reserved(struct termp *p, const char *word, size_t len)
348 {
349 const char *rhs;
350 size_t sz;
351 int i;
352
353 rhs = chars_a2res(p->symtab, word, len, &sz);
354
355 if (NULL == rhs) {
356 #if 0
357 fputs("Unknown reserved word: ", stderr);
358 for (i = 0; i < (int)len; i++)
359 fputc(word[i], stderr);
360 fputc('\n', stderr);
361 #endif
362 return;
363 }
364 for (i = 0; i < (int)sz; i++)
365 encode(p, rhs[i]);
366 }
367
368
369 /*
370 * Handle an escape sequence: determine its length and pass it to the
371 * escape-symbol look table. Note that we assume mdoc(3) has validated
372 * the escape sequence (we assert upon badly-formed escape sequences).
373 */
374 static void
375 do_escaped(struct termp *p, const char **word)
376 {
377 int j, type, sv;
378 const char *wp;
379
380 wp = *word;
381 type = 1;
382
383 if (0 == *(++wp)) {
384 *word = wp;
385 return;
386 }
387
388 if ('(' == *wp) {
389 wp++;
390 if (0 == *wp || 0 == *(wp + 1)) {
391 *word = 0 == *wp ? wp : wp + 1;
392 return;
393 }
394
395 do_special(p, wp, 2);
396 *word = ++wp;
397 return;
398
399 } else if ('*' == *wp) {
400 if (0 == *(++wp)) {
401 *word = wp;
402 return;
403 }
404
405 switch (*wp) {
406 case ('('):
407 wp++;
408 if (0 == *wp || 0 == *(wp + 1)) {
409 *word = 0 == *wp ? wp : wp + 1;
410 return;
411 }
412
413 do_reserved(p, wp, 2);
414 *word = ++wp;
415 return;
416 case ('['):
417 type = 0;
418 break;
419 default:
420 do_reserved(p, wp, 1);
421 *word = wp;
422 return;
423 }
424
425 } else if ('f' == *wp) {
426 if (0 == *(++wp)) {
427 *word = wp;
428 return;
429 }
430
431 switch (*wp) {
432 case ('3'):
433 /* FALLTHROUGH */
434 case ('B'):
435 p->metamask = p->metafont;
436 p->metafont |= METAF_BOLD;
437 break;
438 case ('2'):
439 /* FALLTHROUGH */
440 case ('I'):
441 p->metamask = p->metafont;
442 p->metafont |= METAF_UNDER;
443 break;
444 case ('P'):
445 sv = p->metamask;
446 p->metamask = p->metafont;
447 p->metafont = sv;
448 break;
449 case ('1'):
450 /* FALLTHROUGH */
451 case ('R'):
452 p->metamask = p->metafont;
453 p->metafont &= ~METAF_UNDER;
454 p->metafont &= ~METAF_BOLD;
455 break;
456 default:
457 break;
458 }
459
460 *word = wp;
461 return;
462
463 } else if ('[' != *wp) {
464 do_special(p, wp, 1);
465 *word = wp;
466 return;
467 }
468
469 wp++;
470 for (j = 0; *wp && ']' != *wp; wp++, j++)
471 /* Loop... */ ;
472
473 if (0 == *wp) {
474 *word = wp;
475 return;
476 }
477
478 if (type)
479 do_special(p, wp - j, (size_t)j);
480 else
481 do_reserved(p, wp - j, (size_t)j);
482 *word = wp;
483 }
484
485
486 /*
487 * Handle pwords, partial words, which may be either a single word or a
488 * phrase that cannot be broken down (such as a literal string). This
489 * handles word styling.
490 */
491 void
492 term_word(struct termp *p, const char *word)
493 {
494 const char *sv;
495
496 sv = word;
497
498 if (word[0] && 0 == word[1])
499 switch (word[0]) {
500 case('.'):
501 /* FALLTHROUGH */
502 case(','):
503 /* FALLTHROUGH */
504 case(';'):
505 /* FALLTHROUGH */
506 case(':'):
507 /* FALLTHROUGH */
508 case('?'):
509 /* FALLTHROUGH */
510 case('!'):
511 /* FALLTHROUGH */
512 case(')'):
513 /* FALLTHROUGH */
514 case(']'):
515 /* FALLTHROUGH */
516 case('}'):
517 if ( ! (TERMP_IGNDELIM & p->flags))
518 p->flags |= TERMP_NOSPACE;
519 break;
520 default:
521 break;
522 }
523
524 if ( ! (TERMP_NOSPACE & p->flags))
525 buffer(p, ' ');
526
527 if ( ! (p->flags & TERMP_NONOSPACE))
528 p->flags &= ~TERMP_NOSPACE;
529
530 for ( ; *word; word++)
531 if ('\\' != *word)
532 encode(p, *word);
533 else
534 do_escaped(p, &word);
535
536 if (sv[0] && 0 == sv[1])
537 switch (sv[0]) {
538 case('('):
539 /* FALLTHROUGH */
540 case('['):
541 /* FALLTHROUGH */
542 case('{'):
543 p->flags |= TERMP_NOSPACE;
544 break;
545 default:
546 break;
547 }
548 }
549
550
551 /*
552 * Insert a single character into the line-buffer. If the buffer's
553 * space is exceeded, then allocate more space by doubling the buffer
554 * size.
555 */
556 static void
557 buffer(struct termp *p, char c)
558 {
559 size_t s;
560
561 if (p->col + 1 >= p->maxcols) {
562 if (0 == p->maxcols)
563 p->maxcols = 256;
564 s = p->maxcols * 2;
565 p->buf = realloc(p->buf, s);
566 if (NULL == p->buf) {
567 perror(NULL);
568 exit(EXIT_FAILURE);
569 }
570 p->maxcols = s;
571 }
572 p->buf[(int)(p->col)++] = c;
573 }
574
575
576 static void
577 encode(struct termp *p, char c)
578 {
579
580 if (isgraph((u_char)c)) {
581 if (p->under || METAF_UNDER & p->metafont) {
582 buffer(p, '_');
583 buffer(p, 8);
584 }
585 if (p->bold || METAF_BOLD & p->metafont) {
586 buffer(p, c);
587 buffer(p, 8);
588 }
589 }
590 buffer(p, c);
591 }
592
593
594 size_t
595 term_vspan(const struct roffsu *su)
596 {
597 double r;
598
599 switch (su->unit) {
600 case (SCALE_CM):
601 r = su->scale * 2;
602 break;
603 case (SCALE_IN):
604 r = su->scale * 6;
605 break;
606 case (SCALE_PC):
607 r = su->scale;
608 break;
609 case (SCALE_PT):
610 r = su->scale / 8;
611 break;
612 case (SCALE_MM):
613 r = su->scale / 1000;
614 break;
615 case (SCALE_VS):
616 r = su->scale;
617 break;
618 default:
619 r = su->scale - 1;
620 break;
621 }
622
623 if (r < 0.0)
624 r = 0.0;
625 return(/* LINTED */(size_t)
626 r);
627 }
628
629
630 size_t
631 term_hspan(const struct roffsu *su)
632 {
633 double r;
634
635 /* XXX: CM, IN, and PT are approximations. */
636
637 switch (su->unit) {
638 case (SCALE_CM):
639 r = 4 * su->scale;
640 break;
641 case (SCALE_IN):
642 /* XXX: this is an approximation. */
643 r = 10 * su->scale;
644 break;
645 case (SCALE_PC):
646 r = (10 * su->scale) / 6;
647 break;
648 case (SCALE_PT):
649 r = (10 * su->scale) / 72;
650 break;
651 case (SCALE_MM):
652 r = su->scale / 1000; /* FIXME: double-check. */
653 break;
654 case (SCALE_VS):
655 r = su->scale * 2 - 1; /* FIXME: double-check. */
656 break;
657 default:
658 r = su->scale;
659 break;
660 }
661
662 if (r < 0.0)
663 r = 0.0;
664 return((size_t)/* LINTED */
665 r);
666 }
667
668