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