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