1 /* $Id: roff_term.c,v 1.17 2018/12/16 00:17:02 schwarze Exp $ */
3 * Copyright (c) 2010,2014,2015,2017,2018 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_ft
, /* ft */
46 roff_term_pre_ll
, /* ll */
47 roff_term_pre_mc
, /* mc */
48 roff_term_pre_po
, /* po */
49 roff_term_pre_ce
, /* rj */
50 roff_term_pre_sp
, /* sp */
51 roff_term_pre_ta
, /* ta */
52 roff_term_pre_ti
, /* ti */
57 roff_term_pre(struct termp
*p
, const struct roff_node
*n
)
59 assert(n
->tok
< ROFF_MAX
);
60 (*roff_term_pre_acts
[n
->tok
])(p
, n
);
64 roff_term_pre_br(ROFF_TERM_ARGS
)
67 if (p
->flags
& TERMP_BRIND
) {
68 p
->tcol
->offset
= p
->tcol
->rmargin
;
69 p
->tcol
->rmargin
= p
->maxrmargin
;
70 p
->flags
&= ~(TERMP_NOBREAK
| TERMP_BRIND
);
75 roff_term_pre_ce(ROFF_TERM_ARGS
)
77 const struct roff_node
*nc1
, *nc2
;
80 roff_term_pre_br(p
, n
);
87 if (nc2
->type
== ROFFT_TEXT
) {
90 len
+= term_strlen(p
, nc2
->string
);
93 } while (nc2
!= NULL
&& (nc2
->type
!= ROFFT_TEXT
||
94 (nc2
->flags
& NODE_LINE
) == 0));
95 p
->tcol
->offset
= len
>= p
->tcol
->rmargin
? 0 :
96 lm
+ len
>= p
->tcol
->rmargin
? p
->tcol
->rmargin
- len
:
97 n
->tok
== ROFF_rj
? p
->tcol
->rmargin
- len
:
98 (lm
+ p
->tcol
->rmargin
- len
) / 2;
100 if (nc1
->type
== ROFFT_TEXT
)
101 term_word(p
, nc1
->string
);
103 roff_term_pre(p
, nc1
);
106 p
->flags
|= TERMP_NOSPACE
;
109 p
->tcol
->offset
= lm
;
113 roff_term_pre_ft(ROFF_TERM_ARGS
)
117 cp
= n
->child
->string
;
118 switch (mandoc_font(cp
, (int)strlen(cp
))) {
119 case ESCAPE_FONTBOLD
:
120 term_fontrepl(p
, TERMFONT_BOLD
);
122 case ESCAPE_FONTITALIC
:
123 term_fontrepl(p
, TERMFONT_UNDER
);
126 term_fontrepl(p
, TERMFONT_BI
);
128 case ESCAPE_FONTPREV
:
131 case ESCAPE_FONTROMAN
:
133 term_fontrepl(p
, TERMFONT_NONE
);
141 roff_term_pre_ll(ROFF_TERM_ARGS
)
143 term_setwidth(p
, n
->child
!= NULL
? n
->child
->string
: NULL
);
147 roff_term_pre_mc(ROFF_TERM_ARGS
)
150 p
->flags
|= TERMP_NOBREAK
;
152 p
->flags
&= ~(TERMP_NOBREAK
| TERMP_NOSPACE
);
154 if (n
->child
!= NULL
) {
155 p
->mc
= n
->child
->string
;
156 p
->flags
|= TERMP_NEWMC
;
158 p
->flags
|= TERMP_ENDMC
;
162 roff_term_pre_po(ROFF_TERM_ARGS
)
165 static int po
, polast
;
168 if (n
->child
!= NULL
&&
169 a2roffsu(n
->child
->string
, &su
, SCALE_EM
) != NULL
) {
170 ponew
= term_hen(p
, &su
);
171 if (*n
->child
->string
== '+' ||
172 *n
->child
->string
== '-')
179 ponew
= po
- polast
+ (int)p
->tcol
->offset
;
180 p
->tcol
->offset
= ponew
> 0 ? ponew
: 0;
184 roff_term_pre_sp(ROFF_TERM_ARGS
)
189 if (n
->child
!= NULL
) {
190 if (a2roffsu(n
->child
->string
, &su
, SCALE_VS
) == NULL
)
192 len
= term_vspan(p
, &su
);
202 roff_term_pre_br(p
, n
);
206 roff_term_pre_ta(ROFF_TERM_ARGS
)
208 term_tab_set(p
, NULL
);
209 for (n
= n
->child
; n
!= NULL
; n
= n
->next
)
210 term_tab_set(p
, n
->string
);
214 roff_term_pre_ti(ROFF_TERM_ARGS
)
220 roff_term_pre_br(p
, n
);
222 if (n
->child
== NULL
)
224 cp
= n
->child
->string
;
228 } else if (*cp
== '-') {
234 if (a2roffsu(cp
, &su
, SCALE_EM
) == NULL
)
236 len
= term_hen(p
, &su
);
239 p
->ti
= len
- p
->tcol
->offset
;
240 p
->tcol
->offset
= len
;
241 } else if (sign
== 1) {
243 p
->tcol
->offset
+= len
;
244 } else if ((size_t)len
< p
->tcol
->offset
) {
246 p
->tcol
->offset
-= len
;
248 p
->ti
= -p
->tcol
->offset
;