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