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