6 #define R_F(wb) ((wb)->r_f >= 0 ? (wb)->r_f : n_f) /* current font */
7 #define R_S(wb) ((wb)->r_s >= 0 ? (wb)->r_s : n_s) /* current size */
8 #define R_M(wb) ((wb)->r_m >= 0 ? (wb)->r_m : n_m) /* current color */
10 void wb_init(struct wb
*wb
)
12 memset(wb
, 0, sizeof(*wb
));
22 void wb_done(struct wb
*wb
)
27 /* update wb->st and wb->sb */
28 static void wb_stsb(struct wb
*wb
)
30 wb
->st
= MIN(wb
->st
, wb
->v
- SC_HT
);
31 wb
->sb
= MAX(wb
->sb
, wb
->v
);
34 /* append font and size to the buffer if needed */
35 static void wb_font(struct wb
*wb
)
37 if (wb
->f
!= R_F(wb
)) {
38 sbuf_printf(&wb
->sbuf
, "%cf(%02d", c_ec
, R_F(wb
));
41 if (wb
->s
!= R_S(wb
)) {
42 sbuf_printf(&wb
->sbuf
, "%cs(%02d", c_ec
, R_S(wb
));
45 if (!n_cp
&& wb
->m
!= R_M(wb
)) {
46 sbuf_printf(&wb
->sbuf
, "%cm[%s]", c_ec
, clr_str(R_M(wb
)));
52 void wb_hmov(struct wb
*wb
, int n
)
55 sbuf_printf(&wb
->sbuf
, "%ch'%du'", c_ec
, n
);
58 void wb_vmov(struct wb
*wb
, int n
)
61 sbuf_printf(&wb
->sbuf
, "%cv'%du'", c_ec
, n
);
64 void wb_els(struct wb
*wb
, int els
)
66 if (els
> wb
->els_pos
)
68 if (els
< wb
->els_neg
)
70 sbuf_printf(&wb
->sbuf
, "%cx'%du'", c_ec
, els
);
73 void wb_etc(struct wb
*wb
, char *x
)
76 sbuf_printf(&wb
->sbuf
, "%cX\x02%s\x02", c_ec
, x
);
79 /* make sure nothing is appended to wb after the last wb_put() */
80 static void wb_prevcheck(struct wb
*wb
)
82 if (wb
->prev_ll
!= sbuf_len(&wb
->sbuf
))
86 /* mark wb->prev_c[] as valid */
87 static void wb_prevok(struct wb
*wb
)
89 wb
->prev_ll
= sbuf_len(&wb
->sbuf
);
92 /* append c to wb->prev_c[] */
93 static void wb_prevput(struct wb
*wb
, char *c
, int ll
)
95 if (wb
->prev_n
== LEN(wb
->prev_c
))
97 memmove(wb
->prev_l
+ 1, wb
->prev_l
, wb
->prev_n
* sizeof(wb
->prev_l
[0]));
98 memmove(wb
->prev_h
+ 1, wb
->prev_h
, wb
->prev_n
* sizeof(wb
->prev_h
[0]));
99 memmove(wb
->prev_c
+ 1, wb
->prev_c
, wb
->prev_n
* sizeof(wb
->prev_c
[0]));
101 wb
->prev_h
[0] = wb
->h
;
102 strcpy(wb
->prev_c
[0], c
);
107 /* strip the last i characters from wb */
108 static void wb_prevpop(struct wb
*wb
, int i
)
110 int n
= wb
->prev_n
- i
;
111 sbuf_cut(&wb
->sbuf
, wb
->prev_l
[i
- 1]);
112 wb
->h
= wb
->prev_h
[i
- 1];
113 memmove(wb
->prev_l
, wb
->prev_l
+ i
, n
* sizeof(wb
->prev_l
[0]));
114 memmove(wb
->prev_h
, wb
->prev_h
+ i
, n
* sizeof(wb
->prev_h
[0]));
115 memmove(wb
->prev_c
, wb
->prev_c
+ i
, n
* sizeof(wb
->prev_c
[0]));
117 wb
->prev_ll
= sbuf_len(&wb
->sbuf
);
120 /* return the i-th last character inserted via wb_put() */
121 static char *wb_prev(struct wb
*wb
, int i
)
124 return i
< wb
->prev_n
? wb
->prev_c
[i
] : NULL
;
127 void wb_put(struct wb
*wb
, char *c
)
136 wb_hmov(wb
, spacewid(R_F(wb
), R_S(wb
)));
139 if (c
[0] == '\t' || c
[0] == '\x01' ||
140 (c
[0] == c_ni
&& (c
[1] == '\t' || c
[1] == '\x01'))) {
141 sbuf_append(&wb
->sbuf
, c
);
144 g
= dev_glyph(c
, R_F(wb
));
146 wb_prevcheck(wb
); /* make sure wb->prev_c[] is valid */
147 ll
= sbuf_len(&wb
->sbuf
); /* sbuf length before inserting c */
148 if (!c
[1] || c
[0] == c_ec
|| c
[0] == c_ni
||
149 utf8len((unsigned char) c
[0]) == strlen(c
)) {
150 if (c
[0] == c_ni
&& c
[1] == c_ec
)
151 sbuf_printf(&wb
->sbuf
, "%c%c", c_ec
, c_ec
);
153 sbuf_append(&wb
->sbuf
, c
);
156 sbuf_printf(&wb
->sbuf
, "%c(%s", c_ec
, c
);
158 sbuf_printf(&wb
->sbuf
, "%cC'%s'", c_ec
, c
);
160 if (strcmp(c_hc
, c
)) {
161 wb_prevput(wb
, c
, ll
);
162 wb
->h
+= charwid(R_F(wb
), R_S(wb
), g
? g
->wid
: SC_DW
);
163 wb
->ct
|= g
? g
->type
: 0;
168 /* return zero if c formed a ligature with its previous character */
169 int wb_lig(struct wb
*wb
, char *c
)
171 char lig
[GNLEN
] = "";
172 char *cs
[LIGLEN
+ 2];
176 while (wb_prev(wb
, ++i
))
177 cs
[i
+ 1] = wb_prev(wb
, i
);
178 ligpos
= font_lig(dev_font(R_F(wb
)), cs
, i
+ 1);
180 for (i
= 0; i
< ligpos
- 1; i
++)
181 strcat(lig
, wb_prev(wb
, ligpos
- i
- 2));
183 wb_prevpop(wb
, ligpos
- 1);
190 /* return 0 if pairwise kerning was done */
191 int wb_kern(struct wb
*wb
, char *c
)
196 val
= font_kern(dev_font(R_F(wb
)), wb_prev(wb
, 0), c
);
198 wb_hmov(wb
, charwid(R_F(wb
), R_S(wb
), val
));
199 wb_prevok(wb
); /* kerning should not prevent ligatures */
203 int wb_part(struct wb
*wb
)
208 void wb_setpart(struct wb
*wb
)
213 void wb_drawl(struct wb
*wb
, int h
, int v
)
216 sbuf_printf(&wb
->sbuf
, "%cD'l %du %du'", c_ec
, h
, v
);
222 void wb_drawc(struct wb
*wb
, int r
)
225 sbuf_printf(&wb
->sbuf
, "%cD'c %du'", c_ec
, r
);
229 void wb_drawe(struct wb
*wb
, int h
, int v
)
232 sbuf_printf(&wb
->sbuf
, "%cD'e %du %du'", c_ec
, h
, v
);
236 void wb_drawa(struct wb
*wb
, int h1
, int v1
, int h2
, int v2
)
239 sbuf_printf(&wb
->sbuf
, "%cD'a %du %du %du %du'", c_ec
, h1
, v1
, h2
, v2
);
245 void wb_drawxbeg(struct wb
*wb
, int c
)
248 sbuf_printf(&wb
->sbuf
, "%cD'%c", c_ec
, c
);
251 void wb_drawxdot(struct wb
*wb
, int h
, int v
)
253 sbuf_printf(&wb
->sbuf
, " %du %du", h
, v
);
259 void wb_drawxend(struct wb
*wb
)
261 sbuf_printf(&wb
->sbuf
, "'");
264 static void wb_reset(struct wb
*wb
)
270 static void wb_putc(struct wb
*wb
, int t
, char *s
)
284 wb_hmov(wb
, atoi(s
));
287 wb
->r_m
= clr_get(s
);
293 wb_vmov(wb
, atoi(s
));
304 void wb_cat(struct wb
*wb
, struct wb
*src
)
306 char *s
= sbuf_buf(&src
->sbuf
);
309 while ((c
= out_readc(&s
, d
)) >= 0)
319 int wb_wid(struct wb
*wb
)
324 int wb_empty(struct wb
*wb
)
326 return sbuf_empty(&wb
->sbuf
);
329 /* return 1 if wb ends a sentence (.?!) */
330 int wb_eos(struct wb
*wb
)
333 while (wb_prev(wb
, i
) && strchr("'\")]*", wb_prev(wb
, i
)[0]))
335 return wb_prev(wb
, i
) && strchr(".?!", wb_prev(wb
, i
)[0]);
338 void wb_wconf(struct wb
*wb
, int *ct
, int *st
, int *sb
)
345 /* skip troff requests; return 1 if read c_hc */
346 static int skipreqs(char **s
, struct wb
*w1
)
352 while ((c
= out_readc(s
, d
)) > 0) {
356 if (c
< 0 || !strcmp(c_hc
, d
))
362 static char *dashpos(char *s
, int w
, struct wb
*w1
, int any
)
368 while ((c
= out_readc(&s
, d
)) >= 0) {
370 if (wb_wid(w1
) > w
&& (!any
|| r
))
372 if (!c
&& (!strcmp("-", d
) || (!strcmp("em", d
) || !strcmp("hy", d
))))
378 static int wb_dashwid(struct wb
*wb
)
380 struct glyph
*g
= dev_glyph("hy", R_F(wb
));
381 return charwid(R_F(wb
), R_S(wb
), g
? g
->wid
: SC_DW
);
384 static char *indicatorpos(char *s
, int w
, struct wb
*w1
, int flg
)
390 while ((c
= out_readc(&s
, d
)) >= 0) {
392 if (wb_wid(w1
) + wb_dashwid(w1
) > w
&& (!(flg
& HY_ANY
) || r
))
394 if (!c
&& !strcmp(c_hc
, d
))
400 static char *hyphpos(char *s
, int w
, struct wb
*w1
, int flg
)
402 char *map
[ILNLEN
] = {NULL
}; /* mapping from word to s */
403 int fits
[ILNLEN
] = {0}; /* fits[i] if word[0..i]- fits w */
409 char *wp
= word
, *we
= word
+ sizeof(word
);
413 while ((c
= out_readc(&s
, d
)) >= 0 && wp
+ strlen(d
) + 1 < we
) {
417 map
[wp
- word
] = prev_s
;
418 wp
= strchr(wp
, '\0');
419 fits
[wp
- word
] = wb_wid(w1
) + wb_dashwid(w1
) <= w
;
423 if (strlen(word
) < 4)
425 hyphenate(hyph
, word
);
426 beg
= flg
& HY_FIRSTTWO
? 3 : 2;
427 end
= strlen(word
) - (flg
& HY_FINAL
? 2 : 1);
428 for (i
= beg
; i
< end
; i
++)
429 if (map
[i
] && hyph
[i
] && (fits
[i
] || ((flg
& HY_ANY
) && !r
)))
434 static void dohyph(char *s
, char *pos
, int dash
, struct wb
*w1
, struct wb
*w2
)
440 while (s
!= pos
&& (c
= out_readc(&s
, d
)) >= 0)
443 wb_putc(w1
, 0, "hy");
447 while ((c
= out_readc(&s
, d
)) >= 0)
451 /* hyphenate wb into w1 and w2; return zero on success */
452 int wb_hyph(struct wb
*wb
, int w
, struct wb
*w1
, struct wb
*w2
, int flg
)
454 char *s
= sbuf_buf(&wb
->sbuf
);
456 if (skipreqs(&s
, w1
))
458 dp
= dashpos(sbuf_buf(&wb
->sbuf
), w
, w1
, flg
& HY_ANY
);
459 hp
= indicatorpos(sbuf_buf(&wb
->sbuf
), w
, w1
, flg
& HY_ANY
);
460 p
= flg
& HY_ANY
? MIN(dp
, hp
) : MAX(dp
, hp
);
461 if (!p
&& flg
& HY_MASK
)
462 p
= hyphpos(sbuf_buf(&wb
->sbuf
), w
, w1
, flg
& HY_ANY
);
464 dohyph(sbuf_buf(&wb
->sbuf
), p
, p
!= dp
, w1
, w2
);