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