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