]> git.cameronkatri.com Git - mandoc.git/blob - roff_term.c
Bugfix:
[mandoc.git] / roff_term.c
1 /* $Id: roff_term.c,v 1.17 2018/12/16 00:17:02 schwarze Exp $ */
2 /*
3 * Copyright (c) 2010,2014,2015,2017,2018 Ingo Schwarze <schwarze@openbsd.org>
4 *
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.
8 *
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.
16 */
17 #include <sys/types.h>
18
19 #include <assert.h>
20 #include <stdio.h>
21 #include <string.h>
22
23 #include "mandoc.h"
24 #include "roff.h"
25 #include "out.h"
26 #include "term.h"
27
28 #define ROFF_TERM_ARGS struct termp *p, const struct roff_node *n
29
30 typedef void (*roff_term_pre_fp)(ROFF_TERM_ARGS);
31
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);
41
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 */
53 };
54
55
56 void
57 roff_term_pre(struct termp *p, const struct roff_node *n)
58 {
59 assert(n->tok < ROFF_MAX);
60 (*roff_term_pre_acts[n->tok])(p, n);
61 }
62
63 static void
64 roff_term_pre_br(ROFF_TERM_ARGS)
65 {
66 term_newln(p);
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);
71 }
72 }
73
74 static void
75 roff_term_pre_ce(ROFF_TERM_ARGS)
76 {
77 const struct roff_node *nc1, *nc2;
78 size_t len, lm;
79
80 roff_term_pre_br(p, n);
81 lm = p->tcol->offset;
82 nc1 = n->child->next;
83 while (nc1 != NULL) {
84 nc2 = nc1;
85 len = 0;
86 do {
87 if (nc2->type == ROFFT_TEXT) {
88 if (len)
89 len++;
90 len += term_strlen(p, nc2->string);
91 }
92 nc2 = nc2->next;
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;
99 while (nc1 != nc2) {
100 if (nc1->type == ROFFT_TEXT)
101 term_word(p, nc1->string);
102 else
103 roff_term_pre(p, nc1);
104 nc1 = nc1->next;
105 }
106 p->flags |= TERMP_NOSPACE;
107 term_flushln(p);
108 }
109 p->tcol->offset = lm;
110 }
111
112 static void
113 roff_term_pre_ft(ROFF_TERM_ARGS)
114 {
115 const char *cp;
116
117 cp = n->child->string;
118 switch (mandoc_font(cp, (int)strlen(cp))) {
119 case ESCAPE_FONTBOLD:
120 term_fontrepl(p, TERMFONT_BOLD);
121 break;
122 case ESCAPE_FONTITALIC:
123 term_fontrepl(p, TERMFONT_UNDER);
124 break;
125 case ESCAPE_FONTBI:
126 term_fontrepl(p, TERMFONT_BI);
127 break;
128 case ESCAPE_FONTPREV:
129 term_fontlast(p);
130 break;
131 case ESCAPE_FONTROMAN:
132 case ESCAPE_FONTCW:
133 term_fontrepl(p, TERMFONT_NONE);
134 break;
135 default:
136 break;
137 }
138 }
139
140 static void
141 roff_term_pre_ll(ROFF_TERM_ARGS)
142 {
143 term_setwidth(p, n->child != NULL ? n->child->string : NULL);
144 }
145
146 static void
147 roff_term_pre_mc(ROFF_TERM_ARGS)
148 {
149 if (p->col) {
150 p->flags |= TERMP_NOBREAK;
151 term_flushln(p);
152 p->flags &= ~(TERMP_NOBREAK | TERMP_NOSPACE);
153 }
154 if (n->child != NULL) {
155 p->mc = n->child->string;
156 p->flags |= TERMP_NEWMC;
157 } else
158 p->flags |= TERMP_ENDMC;
159 }
160
161 static void
162 roff_term_pre_po(ROFF_TERM_ARGS)
163 {
164 struct roffsu su;
165 static int po, polast;
166 int ponew;
167
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 == '-')
173 ponew += po;
174 } else
175 ponew = polast;
176 polast = po;
177 po = ponew;
178
179 ponew = po - polast + (int)p->tcol->offset;
180 p->tcol->offset = ponew > 0 ? ponew : 0;
181 }
182
183 static void
184 roff_term_pre_sp(ROFF_TERM_ARGS)
185 {
186 struct roffsu su;
187 int len;
188
189 if (n->child != NULL) {
190 if (a2roffsu(n->child->string, &su, SCALE_VS) == NULL)
191 su.scale = 1.0;
192 len = term_vspan(p, &su);
193 } else
194 len = 1;
195
196 if (len < 0)
197 p->skipvsp -= len;
198 else
199 while (len--)
200 term_vspace(p);
201
202 roff_term_pre_br(p, n);
203 }
204
205 static void
206 roff_term_pre_ta(ROFF_TERM_ARGS)
207 {
208 term_tab_set(p, NULL);
209 for (n = n->child; n != NULL; n = n->next)
210 term_tab_set(p, n->string);
211 }
212
213 static void
214 roff_term_pre_ti(ROFF_TERM_ARGS)
215 {
216 struct roffsu su;
217 const char *cp;
218 int len, sign;
219
220 roff_term_pre_br(p, n);
221
222 if (n->child == NULL)
223 return;
224 cp = n->child->string;
225 if (*cp == '+') {
226 sign = 1;
227 cp++;
228 } else if (*cp == '-') {
229 sign = -1;
230 cp++;
231 } else
232 sign = 0;
233
234 if (a2roffsu(cp, &su, SCALE_EM) == NULL)
235 return;
236 len = term_hen(p, &su);
237
238 if (sign == 0) {
239 p->ti = len - p->tcol->offset;
240 p->tcol->offset = len;
241 } else if (sign == 1) {
242 p->ti = len;
243 p->tcol->offset += len;
244 } else if ((size_t)len < p->tcol->offset) {
245 p->ti = -len;
246 p->tcol->offset -= len;
247 } else {
248 p->ti = -p->tcol->offset;
249 p->tcol->offset = 0;
250 }
251 }