1 /* $Id: roff_term.c,v 1.14 2017/06/24 14:38:33 schwarze Exp $ */
3 * Copyright (c) 2010, 2014, 2015, 2017 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>
27 #define ROFF_TERM_ARGS struct termp *p, const struct roff_node *n
29 typedef void (*roff_term_pre_fp)(ROFF_TERM_ARGS);
31 static void roff_term_pre_br(ROFF_TERM_ARGS);
32 static void roff_term_pre_ce(ROFF_TERM_ARGS);
33 static void roff_term_pre_ft(ROFF_TERM_ARGS);
34 static void roff_term_pre_ll(ROFF_TERM_ARGS);
35 static void roff_term_pre_mc(ROFF_TERM_ARGS);
36 static void roff_term_pre_po(ROFF_TERM_ARGS);
37 static void roff_term_pre_sp(ROFF_TERM_ARGS);
38 static void roff_term_pre_ta(ROFF_TERM_ARGS);
39 static void roff_term_pre_ti(ROFF_TERM_ARGS);
41 static const roff_term_pre_fp roff_term_pre_acts[ROFF_MAX] = {
42 roff_term_pre_br, /* br */
43 roff_term_pre_ce, /* ce */
44 roff_term_pre_ft, /* ft */
45 roff_term_pre_ll, /* ll */
46 roff_term_pre_mc, /* mc */
47 roff_term_pre_po, /* po */
48 roff_term_pre_ce, /* rj */
49 roff_term_pre_sp, /* sp */
50 roff_term_pre_ta, /* ta */
51 roff_term_pre_ti, /* ti */
56 roff_term_pre(struct termp *p, const struct roff_node *n)
58 assert(n->tok < ROFF_MAX);
59 (*roff_term_pre_acts[n->tok])(p, n);
63 roff_term_pre_br(ROFF_TERM_ARGS)
66 if (p->flags & TERMP_BRIND) {
67 p->tcol->offset = p->tcol->rmargin;
68 p->tcol->rmargin = p->maxrmargin;
69 p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND);
74 roff_term_pre_ce(ROFF_TERM_ARGS)
76 const struct roff_node *nc1, *nc2;
79 roff_term_pre_br(p, n);
86 if (nc2->type == ROFFT_TEXT) {
89 len += term_strlen(p, nc2->string);
92 } while (nc2 != NULL && (nc2->type != ROFFT_TEXT ||
93 (nc2->flags & NODE_LINE) == 0));
94 p->tcol->offset = len >= p->tcol->rmargin ? 0 :
95 lm + len >= p->tcol->rmargin ? p->tcol->rmargin - len :
96 n->tok == ROFF_rj ? p->tcol->rmargin - len :
97 (lm + p->tcol->rmargin - len) / 2;
99 if (nc1->type == ROFFT_TEXT)
100 term_word(p, nc1->string);
102 roff_term_pre(p, nc1);
105 p->flags |= TERMP_NOSPACE;
108 p->tcol->offset = lm;
112 roff_term_pre_ft(ROFF_TERM_ARGS)
114 switch (*n->child->string) {
118 term_fontrepl(p, TERMFONT_BOLD);
122 term_fontrepl(p, TERMFONT_UNDER);
130 term_fontrepl(p, TERMFONT_NONE);
138 roff_term_pre_ll(ROFF_TERM_ARGS)
140 term_setwidth(p, n->child != NULL ? n->child->string : NULL);
144 roff_term_pre_mc(ROFF_TERM_ARGS)
147 p->flags |= TERMP_NOBREAK;
149 p->flags &= ~(TERMP_NOBREAK | TERMP_NOSPACE);
151 if (n->child != NULL) {
152 p->mc = n->child->string;
153 p->flags |= TERMP_NEWMC;
155 p->flags |= TERMP_ENDMC;
159 roff_term_pre_po(ROFF_TERM_ARGS)
162 static int po, polast;
165 if (n->child != NULL &&
166 a2roffsu(n->child->string, &su, SCALE_EM) != NULL) {
167 ponew = term_hen(p, &su);
168 if (*n->child->string == '+' ||
169 *n->child->string == '-')
176 ponew = po - polast + (int)p->tcol->offset;
177 p->tcol->offset = ponew > 0 ? ponew : 0;
181 roff_term_pre_sp(ROFF_TERM_ARGS)
186 if (n->child != NULL) {
187 if (a2roffsu(n->child->string, &su, SCALE_VS) == NULL)
189 len = term_vspan(p, &su);
199 roff_term_pre_br(p, n);
203 roff_term_pre_ta(ROFF_TERM_ARGS)
205 term_tab_set(p, NULL);
206 for (n = n->child; n != NULL; n = n->next)
207 term_tab_set(p, n->string);
211 roff_term_pre_ti(ROFF_TERM_ARGS)
217 roff_term_pre_br(p, n);
219 if (n->child == NULL)
221 cp = n->child->string;
225 } else if (*cp == '-') {
231 if (a2roffsu(cp, &su, SCALE_EM) == NULL)
233 len = term_hen(p, &su);
236 p->ti = len - p->tcol->offset;
237 p->tcol->offset = len;
238 } else if (sign == 1) {
240 p->tcol->offset += len;
241 } else if ((size_t)len < p->tcol->offset) {
243 p->tcol->offset -= len;
245 p->ti = -p->tcol->offset;