1 /* $Id: roff_term.c,v 1.19 2019/01/04 03:24:33 schwarze Exp $ */
3 * Copyright (c) 2010,2014,2015,2017-2019 Ingo Schwarze <schwarze@openbsd.org>
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.
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.
17 #include <sys/types.h>
28 #define ROFF_TERM_ARGS struct termp *p, const struct roff_node *n
30 typedef void (*roff_term_pre_fp)(ROFF_TERM_ARGS);
32 static void roff_term_pre_br(ROFF_TERM_ARGS);
33 static void roff_term_pre_ce(ROFF_TERM_ARGS);
34 static void roff_term_pre_ft(ROFF_TERM_ARGS);
35 static void roff_term_pre_ll(ROFF_TERM_ARGS);
36 static void roff_term_pre_mc(ROFF_TERM_ARGS);
37 static void roff_term_pre_po(ROFF_TERM_ARGS);
38 static void roff_term_pre_sp(ROFF_TERM_ARGS);
39 static void roff_term_pre_ta(ROFF_TERM_ARGS);
40 static void roff_term_pre_ti(ROFF_TERM_ARGS);
42 static const roff_term_pre_fp roff_term_pre_acts[ROFF_MAX] = {
43 roff_term_pre_br, /* br */
44 roff_term_pre_ce, /* ce */
45 roff_term_pre_br, /* fi */
46 roff_term_pre_ft, /* ft */
47 roff_term_pre_ll, /* ll */
48 roff_term_pre_mc, /* mc */
49 roff_term_pre_br, /* nf */
50 roff_term_pre_po, /* po */
51 roff_term_pre_ce, /* rj */
52 roff_term_pre_sp, /* sp */
53 roff_term_pre_ta, /* ta */
54 roff_term_pre_ti, /* ti */
59 roff_term_pre(struct termp *p, const struct roff_node *n)
61 assert(n->tok < ROFF_MAX);
62 (*roff_term_pre_acts[n->tok])(p, n);
66 roff_term_pre_br(ROFF_TERM_ARGS)
69 if (p->flags & TERMP_BRIND) {
70 p->tcol->offset = p->tcol->rmargin;
71 p->tcol->rmargin = p->maxrmargin;
73 p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND);
74 p->flags |= TERMP_NOSPACE;
79 roff_term_pre_ce(ROFF_TERM_ARGS)
81 const struct roff_node *nc1, *nc2;
83 roff_term_pre_br(p, n);
84 p->flags |= n->tok == ROFF_ce ? TERMP_CENTER : TERMP_RIGHT;
90 } while (nc2 != NULL && (nc2->type != ROFFT_TEXT ||
91 (nc2->flags & NODE_LINE) == 0));
93 if (nc1->type == ROFFT_TEXT)
94 term_word(p, nc1->string);
96 roff_term_pre(p, nc1);
99 p->flags |= TERMP_NOSPACE;
102 p->flags &= ~(TERMP_CENTER | TERMP_RIGHT);
106 roff_term_pre_ft(ROFF_TERM_ARGS)
110 cp = n->child->string;
111 switch (mandoc_font(cp, (int)strlen(cp))) {
112 case ESCAPE_FONTBOLD:
113 term_fontrepl(p, TERMFONT_BOLD);
115 case ESCAPE_FONTITALIC:
116 term_fontrepl(p, TERMFONT_UNDER);
119 term_fontrepl(p, TERMFONT_BI);
121 case ESCAPE_FONTPREV:
124 case ESCAPE_FONTROMAN:
126 term_fontrepl(p, TERMFONT_NONE);
134 roff_term_pre_ll(ROFF_TERM_ARGS)
136 term_setwidth(p, n->child != NULL ? n->child->string : NULL);
140 roff_term_pre_mc(ROFF_TERM_ARGS)
143 p->flags |= TERMP_NOBREAK;
145 p->flags &= ~(TERMP_NOBREAK | TERMP_NOSPACE);
147 if (n->child != NULL) {
148 p->mc = n->child->string;
149 p->flags |= TERMP_NEWMC;
151 p->flags |= TERMP_ENDMC;
155 roff_term_pre_po(ROFF_TERM_ARGS)
158 static int po, polast;
161 if (n->child != NULL &&
162 a2roffsu(n->child->string, &su, SCALE_EM) != NULL) {
163 ponew = term_hen(p, &su);
164 if (*n->child->string == '+' ||
165 *n->child->string == '-')
172 ponew = po - polast + (int)p->tcol->offset;
173 p->tcol->offset = ponew > 0 ? ponew : 0;
177 roff_term_pre_sp(ROFF_TERM_ARGS)
182 if (n->child != NULL) {
183 if (a2roffsu(n->child->string, &su, SCALE_VS) == NULL)
185 len = term_vspan(p, &su);
195 roff_term_pre_br(p, n);
199 roff_term_pre_ta(ROFF_TERM_ARGS)
201 term_tab_set(p, NULL);
202 for (n = n->child; n != NULL; n = n->next)
203 term_tab_set(p, n->string);
207 roff_term_pre_ti(ROFF_TERM_ARGS)
213 roff_term_pre_br(p, n);
215 if (n->child == NULL)
217 cp = n->child->string;
221 } else if (*cp == '-') {
227 if (a2roffsu(cp, &su, SCALE_EM) == NULL)
229 len = term_hen(p, &su);
232 p->ti = len - p->tcol->offset;
233 p->tcol->offset = len;
234 } else if (sign == 1) {
236 p->tcol->offset += len;
237 } else if ((size_t)len < p->tcol->offset) {
239 p->tcol->offset -= len;
241 p->ti = -p->tcol->offset;