8 /* the current font, size and color */
9 #define R_F(wb) ((wb)->r_f >= 0 ? (wb)->r_f : n_f) /* current font */
10 #define R_S(wb) ((wb)->r_s >= 0 ? (wb)->r_s : n_s) /* current size */
11 #define R_M(wb) ((wb)->r_m >= 0 ? (wb)->r_m : n_m) /* current color */
12 /* italic correction */
13 #define glyph_ic(g) (MAX(0, (g)->urx - (g)->wid))
14 #define glyph_icleft(g) (MAX(0, -(g)->llx))
15 /* the maximum and minimum values of bounding box coordinates */
16 #define BBMAX (1 << 29)
19 static void wb_flushsub(struct wb
*wb
);
21 void wb_init(struct wb
*wb
)
23 memset(wb
, 0, sizeof(*wb
));
38 void wb_done(struct wb
*wb
)
43 /* update wb->st and wb->sb */
44 static void wb_stsb(struct wb
*wb
)
46 wb
->st
= MIN(wb
->st
, wb
->v
- (wb
->s
* SC_IN
/ 72));
47 wb
->sb
= MAX(wb
->sb
, wb
->v
);
50 /* update bounding box */
51 static void wb_bbox(struct wb
*wb
, int llx
, int lly
, int urx
, int ury
)
53 wb
->llx
= MIN(wb
->llx
, wb
->h
+ llx
);
54 wb
->lly
= MIN(wb
->lly
, -wb
->v
+ lly
);
55 wb
->urx
= MAX(wb
->urx
, wb
->h
+ urx
);
56 wb
->ury
= MAX(wb
->ury
, -wb
->v
+ ury
);
59 /* pending font, size or color changes */
60 static int wb_pendingfont(struct wb
*wb
)
62 return wb
->f
!= R_F(wb
) || wb
->s
!= R_S(wb
) ||
63 (!n_cp
&& wb
->m
!= R_M(wb
));
66 /* append font and size to the buffer if needed */
67 static void wb_flushfont(struct wb
*wb
)
69 if (wb
->f
!= R_F(wb
)) {
70 sbuf_printf(&wb
->sbuf
, "%cf(%02d", c_ec
, R_F(wb
));
73 if (wb
->s
!= R_S(wb
)) {
75 sbuf_printf(&wb
->sbuf
, "%cs(%02d", c_ec
, R_S(wb
));
77 sbuf_printf(&wb
->sbuf
, "%cs[%d]", c_ec
, R_S(wb
));
80 if (!n_cp
&& wb
->m
!= R_M(wb
)) {
81 sbuf_printf(&wb
->sbuf
, "%cm[%s]", c_ec
, clr_str(R_M(wb
)));
87 /* apply font and size changes and flush the collected subword */
88 static void wb_flush(struct wb
*wb
)
94 void wb_hmov(struct wb
*wb
, int n
)
98 sbuf_printf(&wb
->sbuf
, "%ch'%du'", c_ec
, n
);
101 void wb_vmov(struct wb
*wb
, int n
)
105 sbuf_printf(&wb
->sbuf
, "%cv'%du'", c_ec
, n
);
108 void wb_els(struct wb
*wb
, int els
)
111 if (els
> wb
->els_pos
)
113 if (els
< wb
->els_neg
)
115 sbuf_printf(&wb
->sbuf
, "%cx'%du'", c_ec
, els
);
118 void wb_etc(struct wb
*wb
, char *x
)
121 sbuf_printf(&wb
->sbuf
, "%cX\x02%s\x02", c_ec
, x
);
124 static void wb_putbuf(struct wb
*wb
, char *c
)
128 if (c
[0] == '\t' || c
[0] == '\x01' ||
129 (c
[0] == c_ni
&& (c
[1] == '\t' || c
[1] == '\x01'))) {
130 sbuf_append(&wb
->sbuf
, c
);
133 g
= dev_glyph(c
, wb
->f
);
134 zerowidth
= c_hymark(c
);
135 if (!g
&& c
[0] == c_ec
&& !zerowidth
) { /* unknown escape */
136 memmove(c
, c
+ 1, strlen(c
));
137 g
= dev_glyph(c
, wb
->f
);
139 if (g
&& !zerowidth
&& wb
->icleft
&& glyph_icleft(g
))
140 wb_hmov(wb
, font_wid(g
->font
, wb
->s
, glyph_icleft(g
)));
142 if (!c
[1] || c
[0] == c_ec
|| c
[0] == c_ni
|| utf8one(c
)) {
143 if (c
[0] == c_ni
&& c
[1] == c_ec
)
144 sbuf_printf(&wb
->sbuf
, "%c%c", c_ec
, c_ec
);
146 sbuf_append(&wb
->sbuf
, c
);
149 sbuf_printf(&wb
->sbuf
, "%c(%s", c_ec
, c
);
151 sbuf_printf(&wb
->sbuf
, "%cC'%s'", c_ec
, c
);
155 wb_bbox(wb
, font_wid(g
->font
, wb
->s
, g
->llx
),
156 font_wid(g
->font
, wb
->s
, g
->lly
),
157 font_wid(g
->font
, wb
->s
, g
->urx
),
158 font_wid(g
->font
, wb
->s
, g
->ury
));
159 wb
->h
+= g
? font_gwid(g
->font
, dev_font(wb
->f
), wb
->s
, g
->wid
) : 0;
160 wb
->ct
|= g
? g
->type
: 0;
165 /* return nonzero if it cannot be hyphenated */
166 static int wb_hyph(char src
[][GNLEN
], int src_n
, char *src_hyph
, int flg
)
168 char word
[WORDLEN
* GNLEN
]; /* word to pass to hyphenate() */
169 char hyph
[WORDLEN
* GNLEN
]; /* hyphenation points of word */
170 int smap
[WORDLEN
]; /* the mapping from src[] to word[] */
175 for (i
= 0; i
< src_n
; i
++) {
184 memset(hyph
, 0, (d
- word
) * sizeof(hyph
[0]));
185 hyphenate(hyph
, word
, flg
);
186 for (i
= 0; i
< src_n
; i
++)
187 src_hyph
[i
] = hyph
[smap
[i
]];
191 static int wb_collect(struct wb
*wb
, int val
)
193 int old
= wb
->sub_collect
;
194 wb
->sub_collect
= val
;
198 /* output the collected characters; only for those present in wb->f font */
199 static void wb_flushsub(struct wb
*wb
)
202 struct glyph
*gsrc
[WORDLEN
];
203 struct glyph
*gdst
[WORDLEN
];
204 int x
[WORDLEN
], y
[WORDLEN
], xadv
[WORDLEN
], yadv
[WORDLEN
];
206 char src_hyph
[WORDLEN
];
209 if (!wb
->sub_n
|| !wb
->sub_collect
)
212 fn
= dev_font(wb
->f
);
213 if (!n_hy
|| wb_hyph(wb
->sub_c
, wb
->sub_n
, src_hyph
, n_hy
))
214 memset(src_hyph
, 0, sizeof(src_hyph
));
215 /* call font_layout() for collected glyphs; skip hyphenation marks */
216 while (sidx
< wb
->sub_n
) {
218 for (; sidx
< wb
->sub_n
&& !c_hymark(wb
->sub_c
[sidx
]); sidx
++)
219 gsrc
[sidx
- beg
] = font_find(fn
, wb
->sub_c
[sidx
]);
220 dst_n
= font_layout(fn
, gsrc
, sidx
- beg
, wb
->s
,
221 gdst
, dmap
, x
, y
, xadv
, yadv
, n_lg
, n_kn
);
222 for (i
= 0; i
< dst_n
; i
++) {
224 wb_hmov(wb
, font_wid(fn
, wb
->s
, x
[i
]));
226 wb_vmov(wb
, font_wid(fn
, wb
->s
, y
[i
]));
227 if (src_hyph
[beg
+ dmap
[i
]])
229 if (gdst
[i
] == gsrc
[dmap
[i
]])
230 wb_putbuf(wb
, wb
->sub_c
[beg
+ dmap
[i
]]);
232 wb_putbuf(wb
, gdst
[i
]->name
);
234 wb_hmov(wb
, font_wid(fn
, wb
->s
, xadv
[i
] - x
[i
]));
236 wb_vmov(wb
, font_wid(fn
, wb
->s
, yadv
[i
] - y
[i
]));
238 for (; sidx
< wb
->sub_n
&& c_hymark(wb
->sub_c
[sidx
]); sidx
++)
239 wb_putbuf(wb
, wb
->sub_c
[sidx
]);
246 void wb_put(struct wb
*wb
, char *c
)
254 wb_hmov(wb
, font_swid(dev_font(R_F(wb
)), R_S(wb
), n_ss
));
257 if (!strcmp(c_nb
, c
)) {
259 sbuf_append(&wb
->sbuf
, c
);
260 wb
->h
+= font_swid(dev_font(R_F(wb
)), R_S(wb
), n_ss
);
263 if (wb_pendingfont(wb
) || wb
->sub_n
== LEN(wb
->sub_c
))
265 if (wb
->sub_collect
) {
266 if (font_find(dev_font(wb
->f
), c
) || c_hymark(c
))
267 strcpy(wb
->sub_c
[wb
->sub_n
++], c
);
275 /* just like wb_put() but disable subword collection */
276 void wb_putraw(struct wb
*wb
, char *c
)
280 collect
= wb_collect(wb
, 0);
282 wb_collect(wb
, collect
);
285 /* just like wb_put(), but call cdef_expand() if c is defined */
286 void wb_putexpand(struct wb
*wb
, char *c
)
288 if (cdef_expand(wb
, c
, R_F(wb
)))
292 int wb_part(struct wb
*wb
)
297 void wb_setpart(struct wb
*wb
)
302 int wb_cost(struct wb
*wb
)
307 void wb_setcost(struct wb
*wb
, int cost
)
312 void wb_drawl(struct wb
*wb
, int c
, int h
, int v
)
315 sbuf_printf(&wb
->sbuf
, "%cD'%c %du %du'", c_ec
, c
, h
, v
);
321 void wb_drawc(struct wb
*wb
, int c
, int r
)
324 sbuf_printf(&wb
->sbuf
, "%cD'%c %du'", c_ec
, c
, r
);
328 void wb_drawe(struct wb
*wb
, int c
, int h
, int v
)
331 sbuf_printf(&wb
->sbuf
, "%cD'%c %du %du'", c_ec
, c
, h
, v
);
335 void wb_drawa(struct wb
*wb
, int c
, int h1
, int v1
, int h2
, int v2
)
338 sbuf_printf(&wb
->sbuf
, "%cD'%c %du %du %du %du'",
339 c_ec
, c
, h1
, v1
, h2
, v2
);
345 void wb_drawxbeg(struct wb
*wb
, int c
)
348 sbuf_printf(&wb
->sbuf
, "%cD'%c", c_ec
, c
);
351 void wb_drawxdot(struct wb
*wb
, int h
, int v
)
353 sbuf_printf(&wb
->sbuf
, " %du %du", h
, v
);
359 void wb_drawxcmd(struct wb
*wb
, char *cmd
)
361 sbuf_printf(&wb
->sbuf
, " %s", cmd
);
364 void wb_drawxend(struct wb
*wb
)
366 sbuf_printf(&wb
->sbuf
, "'");
369 void wb_reset(struct wb
*wb
)
375 char *wb_buf(struct wb
*wb
)
378 return sbuf_buf(&wb
->sbuf
);
381 static void wb_putc(struct wb
*wb
, int t
, char *s
)
397 wb_hmov(wb
, atoi(s
));
400 wb
->r_m
= clr_get(s
);
406 wb_vmov(wb
, atoi(s
));
417 void wb_cat(struct wb
*wb
, struct wb
*src
)
424 collect
= wb_collect(wb
, 0);
425 s
= sbuf_buf(&src
->sbuf
);
426 while ((c
= escread(&s
, &d
)) >= 0)
434 wb_collect(wb
, collect
);
437 int wb_wid(struct wb
*wb
)
443 int wb_hpos(struct wb
*wb
)
449 int wb_vpos(struct wb
*wb
)
455 int wb_empty(struct wb
*wb
)
457 return !wb
->sub_n
&& sbuf_empty(&wb
->sbuf
);
460 /* return 1 if wb ends a sentence (.?!) */
461 int wb_eos(struct wb
*wb
)
463 int i
= wb
->sub_n
- 1;
464 while (i
> 0 && c_eostran(wb
->sub_c
[i
]))
466 return i
>= 0 && c_eossent(wb
->sub_c
[i
]);
469 void wb_wconf(struct wb
*wb
, int *ct
, int *st
, int *sb
,
470 int *llx
, int *lly
, int *urx
, int *ury
)
476 *llx
= wb
->llx
< BBMAX
? wb
->llx
: 0;
477 *lly
= wb
->lly
< BBMAX
? -wb
->lly
: 0;
478 *urx
= wb
->urx
> BBMIN
? wb
->urx
: 0;
479 *ury
= wb
->ury
> BBMIN
? -wb
->ury
: 0;
482 static struct glyph
*wb_prevglyph(struct wb
*wb
)
484 return wb
->sub_n
? dev_glyph(wb
->sub_c
[wb
->sub_n
- 1], wb
->f
) : NULL
;
487 void wb_italiccorrection(struct wb
*wb
)
489 struct glyph
*g
= wb_prevglyph(wb
);
490 if (g
&& glyph_ic(g
))
491 wb_hmov(wb
, font_wid(g
->font
, wb
->s
, glyph_ic(g
)));
494 void wb_italiccorrectionleft(struct wb
*wb
)
500 void wb_fnszget(struct wb
*wb
, int *fn
, int *sz
, int *m
)
508 void wb_fnszset(struct wb
*wb
, int fn
, int sz
, int m
)
515 void wb_catstr(struct wb
*wb
, char *s
, char *end
)
520 collect
= wb_collect(wb
, 0);
521 while (s
< end
&& (c
= escread(&s
, &d
)) >= 0)
523 wb_collect(wb
, collect
);
526 /* return the size of \(hy if appended to wb */
527 int wb_hywid(struct wb
*wb
)
529 struct glyph
*g
= dev_glyph("hy", wb
->f
);
530 return g
? font_gwid(g
->font
, dev_font(R_F(wb
)), R_S(wb
), g
->wid
) : 0;
533 /* return the size of space if appended to wb */
534 int wb_swid(struct wb
*wb
)
536 return font_swid(dev_font(R_F(wb
)), R_S(wb
), n_ss
);