]> git.cameronkatri.com Git - mandoc.git/blob - roff_term.c
Move parsing of the .nf and .fi (fill mode) requests from the man(7)
[mandoc.git] / roff_term.c
1 /* $Id: roff_term.c,v 1.18 2018/12/31 07:08:12 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_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 */
55 };
56
57
58 void
59 roff_term_pre(struct termp *p, const struct roff_node *n)
60 {
61 assert(n->tok < ROFF_MAX);
62 (*roff_term_pre_acts[n->tok])(p, n);
63 }
64
65 static void
66 roff_term_pre_br(ROFF_TERM_ARGS)
67 {
68 term_newln(p);
69 if (p->flags & TERMP_BRIND) {
70 p->tcol->offset = p->tcol->rmargin;
71 p->tcol->rmargin = p->maxrmargin;
72 p->trailspace = 0;
73 p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND);
74 p->flags |= TERMP_NOSPACE;
75 }
76 }
77
78 static void
79 roff_term_pre_ce(ROFF_TERM_ARGS)
80 {
81 const struct roff_node *nc1, *nc2;
82 size_t len, lm;
83
84 roff_term_pre_br(p, n);
85 lm = p->tcol->offset;
86 nc1 = n->child->next;
87 while (nc1 != NULL) {
88 nc2 = nc1;
89 len = 0;
90 do {
91 if (nc2->type == ROFFT_TEXT) {
92 if (len)
93 len++;
94 len += term_strlen(p, nc2->string);
95 }
96 nc2 = nc2->next;
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;
103 while (nc1 != nc2) {
104 if (nc1->type == ROFFT_TEXT)
105 term_word(p, nc1->string);
106 else
107 roff_term_pre(p, nc1);
108 nc1 = nc1->next;
109 }
110 p->flags |= TERMP_NOSPACE;
111 term_flushln(p);
112 }
113 p->tcol->offset = lm;
114 }
115
116 static void
117 roff_term_pre_ft(ROFF_TERM_ARGS)
118 {
119 const char *cp;
120
121 cp = n->child->string;
122 switch (mandoc_font(cp, (int)strlen(cp))) {
123 case ESCAPE_FONTBOLD:
124 term_fontrepl(p, TERMFONT_BOLD);
125 break;
126 case ESCAPE_FONTITALIC:
127 term_fontrepl(p, TERMFONT_UNDER);
128 break;
129 case ESCAPE_FONTBI:
130 term_fontrepl(p, TERMFONT_BI);
131 break;
132 case ESCAPE_FONTPREV:
133 term_fontlast(p);
134 break;
135 case ESCAPE_FONTROMAN:
136 case ESCAPE_FONTCW:
137 term_fontrepl(p, TERMFONT_NONE);
138 break;
139 default:
140 break;
141 }
142 }
143
144 static void
145 roff_term_pre_ll(ROFF_TERM_ARGS)
146 {
147 term_setwidth(p, n->child != NULL ? n->child->string : NULL);
148 }
149
150 static void
151 roff_term_pre_mc(ROFF_TERM_ARGS)
152 {
153 if (p->col) {
154 p->flags |= TERMP_NOBREAK;
155 term_flushln(p);
156 p->flags &= ~(TERMP_NOBREAK | TERMP_NOSPACE);
157 }
158 if (n->child != NULL) {
159 p->mc = n->child->string;
160 p->flags |= TERMP_NEWMC;
161 } else
162 p->flags |= TERMP_ENDMC;
163 }
164
165 static void
166 roff_term_pre_po(ROFF_TERM_ARGS)
167 {
168 struct roffsu su;
169 static int po, polast;
170 int ponew;
171
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 == '-')
177 ponew += po;
178 } else
179 ponew = polast;
180 polast = po;
181 po = ponew;
182
183 ponew = po - polast + (int)p->tcol->offset;
184 p->tcol->offset = ponew > 0 ? ponew : 0;
185 }
186
187 static void
188 roff_term_pre_sp(ROFF_TERM_ARGS)
189 {
190 struct roffsu su;
191 int len;
192
193 if (n->child != NULL) {
194 if (a2roffsu(n->child->string, &su, SCALE_VS) == NULL)
195 su.scale = 1.0;
196 len = term_vspan(p, &su);
197 } else
198 len = 1;
199
200 if (len < 0)
201 p->skipvsp -= len;
202 else
203 while (len--)
204 term_vspace(p);
205
206 roff_term_pre_br(p, n);
207 }
208
209 static void
210 roff_term_pre_ta(ROFF_TERM_ARGS)
211 {
212 term_tab_set(p, NULL);
213 for (n = n->child; n != NULL; n = n->next)
214 term_tab_set(p, n->string);
215 }
216
217 static void
218 roff_term_pre_ti(ROFF_TERM_ARGS)
219 {
220 struct roffsu su;
221 const char *cp;
222 int len, sign;
223
224 roff_term_pre_br(p, n);
225
226 if (n->child == NULL)
227 return;
228 cp = n->child->string;
229 if (*cp == '+') {
230 sign = 1;
231 cp++;
232 } else if (*cp == '-') {
233 sign = -1;
234 cp++;
235 } else
236 sign = 0;
237
238 if (a2roffsu(cp, &su, SCALE_EM) == NULL)
239 return;
240 len = term_hen(p, &su);
241
242 if (sign == 0) {
243 p->ti = len - p->tcol->offset;
244 p->tcol->offset = len;
245 } else if (sign == 1) {
246 p->ti = len;
247 p->tcol->offset += len;
248 } else if ((size_t)len < p->tcol->offset) {
249 p->ti = -len;
250 p->tcol->offset -= len;
251 } else {
252 p->ti = -p->tcol->offset;
253 p->tcol->offset = 0;
254 }
255 }