1 /* $Id: roff_term.c,v 1.18 2018/12/31 07:08:12 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_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
;
84 roff_term_pre_br(p
, n
);
91 if (nc2
->type
== ROFFT_TEXT
) {
94 len
+= term_strlen(p
, nc2
->string
);
97 } while (nc2
!= NULL
&& (nc2
->type
!= ROFFT_TEXT
||
98 (nc2
->flags
& NODE_LINE
) == 0));
99 p
->tcol
->offset
= len
>= p
->tcol
->rmargin
? 0 :
100 lm
+ len
>= p
->tcol
->rmargin
? p
->tcol
->rmargin
- len
:
101 n
->tok
== ROFF_rj
? p
->tcol
->rmargin
- len
:
102 (lm
+ p
->tcol
->rmargin
- len
) / 2;
104 if (nc1
->type
== ROFFT_TEXT
)
105 term_word(p
, nc1
->string
);
107 roff_term_pre(p
, nc1
);
110 p
->flags
|= TERMP_NOSPACE
;
113 p
->tcol
->offset
= lm
;
117 roff_term_pre_ft(ROFF_TERM_ARGS
)
121 cp
= n
->child
->string
;
122 switch (mandoc_font(cp
, (int)strlen(cp
))) {
123 case ESCAPE_FONTBOLD
:
124 term_fontrepl(p
, TERMFONT_BOLD
);
126 case ESCAPE_FONTITALIC
:
127 term_fontrepl(p
, TERMFONT_UNDER
);
130 term_fontrepl(p
, TERMFONT_BI
);
132 case ESCAPE_FONTPREV
:
135 case ESCAPE_FONTROMAN
:
137 term_fontrepl(p
, TERMFONT_NONE
);
145 roff_term_pre_ll(ROFF_TERM_ARGS
)
147 term_setwidth(p
, n
->child
!= NULL
? n
->child
->string
: NULL
);
151 roff_term_pre_mc(ROFF_TERM_ARGS
)
154 p
->flags
|= TERMP_NOBREAK
;
156 p
->flags
&= ~(TERMP_NOBREAK
| TERMP_NOSPACE
);
158 if (n
->child
!= NULL
) {
159 p
->mc
= n
->child
->string
;
160 p
->flags
|= TERMP_NEWMC
;
162 p
->flags
|= TERMP_ENDMC
;
166 roff_term_pre_po(ROFF_TERM_ARGS
)
169 static int po
, polast
;
172 if (n
->child
!= NULL
&&
173 a2roffsu(n
->child
->string
, &su
, SCALE_EM
) != NULL
) {
174 ponew
= term_hen(p
, &su
);
175 if (*n
->child
->string
== '+' ||
176 *n
->child
->string
== '-')
183 ponew
= po
- polast
+ (int)p
->tcol
->offset
;
184 p
->tcol
->offset
= ponew
> 0 ? ponew
: 0;
188 roff_term_pre_sp(ROFF_TERM_ARGS
)
193 if (n
->child
!= NULL
) {
194 if (a2roffsu(n
->child
->string
, &su
, SCALE_VS
) == NULL
)
196 len
= term_vspan(p
, &su
);
206 roff_term_pre_br(p
, n
);
210 roff_term_pre_ta(ROFF_TERM_ARGS
)
212 term_tab_set(p
, NULL
);
213 for (n
= n
->child
; n
!= NULL
; n
= n
->next
)
214 term_tab_set(p
, n
->string
);
218 roff_term_pre_ti(ROFF_TERM_ARGS
)
224 roff_term_pre_br(p
, n
);
226 if (n
->child
== NULL
)
228 cp
= n
->child
->string
;
232 } else if (*cp
== '-') {
238 if (a2roffsu(cp
, &su
, SCALE_EM
) == NULL
)
240 len
= term_hen(p
, &su
);
243 p
->ti
= len
- p
->tcol
->offset
;
244 p
->tcol
->offset
= len
;
245 } else if (sign
== 1) {
247 p
->tcol
->offset
+= len
;
248 } else if ((size_t)len
< p
->tcol
->offset
) {
250 p
->tcol
->offset
-= len
;
252 p
->ti
= -p
->tcol
->offset
;