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