]> git.cameronkatri.com Git - mandoc.git/blob - term.c
bfedefb0ff31e718b8f5f80d5ce3ddd619853414
[mandoc.git] / term.c
1 /* $Id: term.c,v 1.192 2011/05/17 11:55:08 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_UNICODE):
455 encode(p, "?", 1);
456 break;
457 case (ESCAPE_NUMBERED):
458 if ('\0' != (c = mchars_num2char(seq, sz)))
459 encode(p, &c, 1);
460 break;
461 case (ESCAPE_PREDEF):
462 cp = mchars_res2str(p->symtab, seq, sz, &ssz);
463 if (NULL != cp)
464 encode(p, cp, ssz);
465 break;
466 case (ESCAPE_SPECIAL):
467 cp = mchars_spec2str(p->symtab, seq, sz, &ssz);
468 if (NULL != cp)
469 encode(p, cp, ssz);
470 else if (1 == ssz)
471 encode(p, seq, sz);
472 break;
473 case (ESCAPE_FONTBOLD):
474 term_fontrepl(p, TERMFONT_BOLD);
475 break;
476 case (ESCAPE_FONTITALIC):
477 term_fontrepl(p, TERMFONT_UNDER);
478 break;
479 case (ESCAPE_FONTROMAN):
480 term_fontrepl(p, TERMFONT_NONE);
481 break;
482 case (ESCAPE_FONTPREV):
483 term_fontlast(p);
484 break;
485 case (ESCAPE_NOSPACE):
486 if ('\0' == *word)
487 p->flags |= TERMP_NOSPACE;
488 break;
489 default:
490 break;
491 }
492 }
493 }
494
495 static void
496 adjbuf(struct termp *p, int sz)
497 {
498
499 if (0 == p->maxcols)
500 p->maxcols = 1024;
501 while (sz >= p->maxcols)
502 p->maxcols <<= 2;
503
504 p->buf = mandoc_realloc
505 (p->buf, sizeof(int) * (size_t)p->maxcols);
506 }
507
508 static void
509 bufferc(struct termp *p, char c)
510 {
511
512 if (p->col + 1 >= p->maxcols)
513 adjbuf(p, p->col + 1);
514
515 p->buf[p->col++] = c;
516 }
517
518 static void
519 encode(struct termp *p, const char *word, size_t sz)
520 {
521 enum termfont f;
522 int i, len;
523
524 /* LINTED */
525 len = sz;
526
527 /*
528 * Encode and buffer a string of characters. If the current
529 * font mode is unset, buffer directly, else encode then buffer
530 * character by character.
531 */
532
533 if (TERMFONT_NONE == (f = term_fonttop(p))) {
534 if (p->col + len >= p->maxcols)
535 adjbuf(p, p->col + len);
536 for (i = 0; i < len; i++)
537 p->buf[p->col++] = word[i];
538 return;
539 }
540
541 /* Pre-buffer, assuming worst-case. */
542
543 if (p->col + 1 + (len * 3) >= p->maxcols)
544 adjbuf(p, p->col + 1 + (len * 3));
545
546 for (i = 0; i < len; i++) {
547 if ( ! isgraph((unsigned char)word[i])) {
548 p->buf[p->col++] = word[i];
549 continue;
550 }
551
552 if (TERMFONT_UNDER == f)
553 p->buf[p->col++] = '_';
554 else
555 p->buf[p->col++] = word[i];
556
557 p->buf[p->col++] = 8;
558 p->buf[p->col++] = word[i];
559 }
560 }
561
562 size_t
563 term_len(const struct termp *p, size_t sz)
564 {
565
566 return((*p->width)(p, ' ') * sz);
567 }
568
569
570 size_t
571 term_strlen(const struct termp *p, const char *cp)
572 {
573 size_t sz, rsz, i;
574 int ssz, c;
575 const char *seq, *rhs;
576 static const char rej[] = { '\\', ASCII_HYPH, ASCII_NBRSP, '\0' };
577
578 /*
579 * Account for escaped sequences within string length
580 * calculations. This follows the logic in term_word() as we
581 * must calculate the width of produced strings.
582 */
583
584 sz = 0;
585 while ('\0' != *cp) {
586 rsz = strcspn(cp, rej);
587 for (i = 0; i < rsz; i++)
588 sz += (*p->width)(p, *cp++);
589
590 c = 0;
591 switch (*cp) {
592 case ('\\'):
593 cp++;
594 rhs = NULL;
595 switch (mandoc_escape(&cp, &seq, &ssz)) {
596 case (ESCAPE_ERROR):
597 return(sz);
598 case (ESCAPE_UNICODE):
599 c = '?';
600 /* FALLTHROUGH */
601 case (ESCAPE_NUMBERED):
602 if ('\0' != c)
603 c = mchars_num2char(seq, ssz);
604 if ('\0' != c)
605 sz += (*p->width)(p, c);
606 break;
607 case (ESCAPE_PREDEF):
608 rhs = mchars_res2str
609 (p->symtab, seq, ssz, &rsz);
610 break;
611 case (ESCAPE_SPECIAL):
612 rhs = mchars_spec2str
613 (p->symtab, seq, ssz, &rsz);
614
615 if (ssz != 1 || rhs)
616 break;
617
618 rhs = seq;
619 rsz = ssz;
620 break;
621 default:
622 break;
623 }
624
625 if (NULL == rhs)
626 break;
627
628 for (i = 0; i < rsz; i++)
629 sz += (*p->width)(p, *rhs++);
630 break;
631 case (ASCII_NBRSP):
632 sz += (*p->width)(p, ' ');
633 cp++;
634 break;
635 case (ASCII_HYPH):
636 sz += (*p->width)(p, '-');
637 cp++;
638 break;
639 default:
640 break;
641 }
642 }
643
644 return(sz);
645 }
646
647 /* ARGSUSED */
648 size_t
649 term_vspan(const struct termp *p, const struct roffsu *su)
650 {
651 double r;
652
653 switch (su->unit) {
654 case (SCALE_CM):
655 r = su->scale * 2;
656 break;
657 case (SCALE_IN):
658 r = su->scale * 6;
659 break;
660 case (SCALE_PC):
661 r = su->scale;
662 break;
663 case (SCALE_PT):
664 r = su->scale / 8;
665 break;
666 case (SCALE_MM):
667 r = su->scale / 1000;
668 break;
669 case (SCALE_VS):
670 r = su->scale;
671 break;
672 default:
673 r = su->scale - 1;
674 break;
675 }
676
677 if (r < 0.0)
678 r = 0.0;
679 return(/* LINTED */(size_t)
680 r);
681 }
682
683 size_t
684 term_hspan(const struct termp *p, const struct roffsu *su)
685 {
686 double v;
687
688 v = ((*p->hspan)(p, su));
689 if (v < 0.0)
690 v = 0.0;
691 return((size_t) /* LINTED */
692 v);
693 }