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
)) {
74 sbuf_printf(&wb
->sbuf
, "%cs(%02d", c_ec
, R_S(wb
));
77 if (!n_cp
&& wb
->m
!= R_M(wb
)) {
78 sbuf_printf(&wb
->sbuf
, "%cm[%s]", c_ec
, clr_str(R_M(wb
)));
84 /* apply font and size changes and flush the collected subword */
85 static void wb_flush(struct wb
*wb
)
91 void wb_hmov(struct wb
*wb
, int n
)
95 sbuf_printf(&wb
->sbuf
, "%ch'%du'", c_ec
, n
);
98 void wb_vmov(struct wb
*wb
, int n
)
102 sbuf_printf(&wb
->sbuf
, "%cv'%du'", c_ec
, n
);
105 void wb_els(struct wb
*wb
, int els
)
108 if (els
> wb
->els_pos
)
110 if (els
< wb
->els_neg
)
112 sbuf_printf(&wb
->sbuf
, "%cx'%du'", c_ec
, els
);
115 void wb_etc(struct wb
*wb
, char *x
)
118 sbuf_printf(&wb
->sbuf
, "%cX\x02%s\x02", c_ec
, x
);
121 static void wb_putbuf(struct wb
*wb
, char *c
)
125 if (c
[0] == '\t' || c
[0] == '\x01' ||
126 (c
[0] == c_ni
&& (c
[1] == '\t' || c
[1] == '\x01'))) {
127 sbuf_append(&wb
->sbuf
, c
);
130 g
= dev_glyph(c
, wb
->f
);
131 zerowidth
= c_hymark(c
);
132 if (!g
&& c
[0] == c_ec
&& !zerowidth
) { /* unknown escape */
133 memmove(c
, c
+ 1, strlen(c
));
134 g
= dev_glyph(c
, wb
->f
);
136 if (g
&& !zerowidth
&& wb
->icleft
&& glyph_icleft(g
))
137 wb_hmov(wb
, font_wid(g
->font
, wb
->s
, glyph_icleft(g
)));
139 if (!c
[1] || c
[0] == c_ec
|| c
[0] == c_ni
|| utf8one(c
)) {
140 if (c
[0] == c_ni
&& c
[1] == c_ec
)
141 sbuf_printf(&wb
->sbuf
, "%c%c", c_ec
, c_ec
);
143 sbuf_append(&wb
->sbuf
, c
);
146 sbuf_printf(&wb
->sbuf
, "%c(%s", c_ec
, c
);
148 sbuf_printf(&wb
->sbuf
, "%cC'%s'", c_ec
, c
);
152 wb_bbox(wb
, font_wid(g
->font
, wb
->s
, g
->llx
),
153 font_wid(g
->font
, wb
->s
, g
->lly
),
154 font_wid(g
->font
, wb
->s
, g
->urx
),
155 font_wid(g
->font
, wb
->s
, g
->ury
));
156 wb
->h
+= g
? font_gwid(g
->font
, dev_font(wb
->f
), wb
->s
, g
->wid
) : 0;
157 wb
->ct
|= g
? g
->type
: 0;
162 /* return nonzero if it cannot be hyphenated */
163 static int wb_hyph(char src
[][GNLEN
], int src_n
, char *src_hyph
, int flg
)
165 char word
[WORDLEN
* GNLEN
]; /* word to pass to hyphenate() */
166 char hyph
[WORDLEN
* GNLEN
]; /* hyphenation points of word */
167 int smap
[WORDLEN
]; /* the mapping from src[] to word[] */
172 for (i
= 0; i
< src_n
; i
++) {
181 memset(hyph
, 0, (d
- word
) * sizeof(hyph
[0]));
182 hyphenate(hyph
, word
, flg
);
183 for (i
= 0; i
< src_n
; i
++)
184 src_hyph
[i
] = hyph
[smap
[i
]];
188 static int wb_collect(struct wb
*wb
, int val
)
190 int old
= wb
->sub_collect
;
191 wb
->sub_collect
= val
;
195 /* output the collected characters; only for those present in wb->f font */
196 static void wb_flushsub(struct wb
*wb
)
199 struct glyph
*gsrc
[WORDLEN
];
200 struct glyph
*gdst
[WORDLEN
];
201 int x
[WORDLEN
], y
[WORDLEN
], xadv
[WORDLEN
], yadv
[WORDLEN
];
203 char src_hyph
[WORDLEN
];
206 if (!wb
->sub_n
|| !wb
->sub_collect
)
209 fn
= dev_font(wb
->f
);
210 if (!n_hy
|| wb_hyph(wb
->sub_c
, wb
->sub_n
, src_hyph
, n_hy
))
211 memset(src_hyph
, 0, sizeof(src_hyph
));
212 /* call font_layout() for collected glyphs; skip hyphenation marks */
213 while (sidx
< wb
->sub_n
) {
215 for (; sidx
< wb
->sub_n
&& !c_hymark(wb
->sub_c
[sidx
]); sidx
++)
216 gsrc
[sidx
- beg
] = font_find(fn
, wb
->sub_c
[sidx
]);
217 dst_n
= font_layout(fn
, gsrc
, sidx
- beg
, wb
->s
,
218 gdst
, dmap
, x
, y
, xadv
, yadv
, n_lg
, n_kn
);
219 for (i
= 0; i
< dst_n
; i
++) {
221 wb_hmov(wb
, font_wid(fn
, wb
->s
, x
[i
]));
223 wb_vmov(wb
, font_wid(fn
, wb
->s
, y
[i
]));
224 if (src_hyph
[beg
+ dmap
[i
]])
226 if (gdst
[i
] == gsrc
[dmap
[i
]])
227 wb_putbuf(wb
, wb
->sub_c
[beg
+ dmap
[i
]]);
229 wb_putbuf(wb
, gdst
[i
]->name
);
231 wb_hmov(wb
, font_wid(fn
, wb
->s
, xadv
[i
] - x
[i
]));
233 wb_vmov(wb
, font_wid(fn
, wb
->s
, yadv
[i
] - y
[i
]));
235 for (; sidx
< wb
->sub_n
&& c_hymark(wb
->sub_c
[sidx
]); sidx
++)
236 wb_putbuf(wb
, wb
->sub_c
[sidx
]);
243 void wb_put(struct wb
*wb
, char *c
)
251 wb_hmov(wb
, font_swid(dev_font(R_F(wb
)), R_S(wb
), n_ss
));
254 if (!strcmp(c_nb
, c
)) {
256 sbuf_append(&wb
->sbuf
, c
);
257 wb
->h
+= font_swid(dev_font(R_F(wb
)), R_S(wb
), n_ss
);
260 if (wb_pendingfont(wb
) || wb
->sub_n
== LEN(wb
->sub_c
))
262 if (wb
->sub_collect
) {
263 if (font_find(dev_font(wb
->f
), c
) || c_hymark(c
))
264 strcpy(wb
->sub_c
[wb
->sub_n
++], c
);
272 /* just like wb_put() but disable subword collection */
273 void wb_putraw(struct wb
*wb
, char *c
)
277 collect
= wb_collect(wb
, 0);
279 wb_collect(wb
, collect
);
282 /* just like wb_put(), but call cdef_expand() if c is defined */
283 void wb_putexpand(struct wb
*wb
, char *c
)
285 if (cdef_expand(wb
, c
, R_F(wb
)))
289 int wb_part(struct wb
*wb
)
294 void wb_setpart(struct wb
*wb
)
299 int wb_cost(struct wb
*wb
)
304 void wb_setcost(struct wb
*wb
, int cost
)
309 void wb_drawl(struct wb
*wb
, int c
, int h
, int v
)
312 sbuf_printf(&wb
->sbuf
, "%cD'%c %du %du'", c_ec
, c
, h
, v
);
318 void wb_drawc(struct wb
*wb
, int c
, int r
)
321 sbuf_printf(&wb
->sbuf
, "%cD'%c %du'", c_ec
, c
, r
);
325 void wb_drawe(struct wb
*wb
, int c
, int h
, int v
)
328 sbuf_printf(&wb
->sbuf
, "%cD'%c %du %du'", c_ec
, c
, h
, v
);
332 void wb_drawa(struct wb
*wb
, int c
, int h1
, int v1
, int h2
, int v2
)
335 sbuf_printf(&wb
->sbuf
, "%cD'%c %du %du %du %du'",
336 c_ec
, c
, h1
, v1
, h2
, v2
);
342 void wb_drawxbeg(struct wb
*wb
, int c
)
345 sbuf_printf(&wb
->sbuf
, "%cD'%c", c_ec
, c
);
348 void wb_drawxdot(struct wb
*wb
, int h
, int v
)
350 sbuf_printf(&wb
->sbuf
, " %du %du", h
, v
);
356 void wb_drawxcmd(struct wb
*wb
, char *cmd
)
358 sbuf_printf(&wb
->sbuf
, " %s", cmd
);
361 void wb_drawxend(struct wb
*wb
)
363 sbuf_printf(&wb
->sbuf
, "'");
366 void wb_reset(struct wb
*wb
)
372 char *wb_buf(struct wb
*wb
)
375 return sbuf_buf(&wb
->sbuf
);
378 static void wb_putc(struct wb
*wb
, int t
, char *s
)
394 wb_hmov(wb
, atoi(s
));
397 wb
->r_m
= clr_get(s
);
403 wb_vmov(wb
, atoi(s
));
414 void wb_cat(struct wb
*wb
, struct wb
*src
)
421 collect
= wb_collect(wb
, 0);
422 s
= sbuf_buf(&src
->sbuf
);
423 while ((c
= escread(&s
, &d
)) >= 0)
431 wb_collect(wb
, collect
);
434 int wb_wid(struct wb
*wb
)
440 int wb_hpos(struct wb
*wb
)
446 int wb_vpos(struct wb
*wb
)
452 int wb_empty(struct wb
*wb
)
454 return !wb
->sub_n
&& sbuf_empty(&wb
->sbuf
);
457 /* return 1 if wb ends a sentence (.?!) */
458 int wb_eos(struct wb
*wb
)
460 int i
= wb
->sub_n
- 1;
461 while (i
> 0 && c_eostran(wb
->sub_c
[i
]))
463 return i
>= 0 && c_eossent(wb
->sub_c
[i
]);
466 void wb_wconf(struct wb
*wb
, int *ct
, int *st
, int *sb
,
467 int *llx
, int *lly
, int *urx
, int *ury
)
473 *llx
= wb
->llx
< BBMAX
? wb
->llx
: 0;
474 *lly
= wb
->lly
< BBMAX
? -wb
->lly
: 0;
475 *urx
= wb
->urx
> BBMIN
? wb
->urx
: 0;
476 *ury
= wb
->ury
> BBMIN
? -wb
->ury
: 0;
479 static struct glyph
*wb_prevglyph(struct wb
*wb
)
481 return wb
->sub_n
? dev_glyph(wb
->sub_c
[wb
->sub_n
- 1], wb
->f
) : NULL
;
484 void wb_italiccorrection(struct wb
*wb
)
486 struct glyph
*g
= wb_prevglyph(wb
);
487 if (g
&& glyph_ic(g
))
488 wb_hmov(wb
, font_wid(g
->font
, wb
->s
, glyph_ic(g
)));
491 void wb_italiccorrectionleft(struct wb
*wb
)
497 void wb_fnszget(struct wb
*wb
, int *fn
, int *sz
, int *m
)
505 void wb_fnszset(struct wb
*wb
, int fn
, int sz
, int m
)
512 void wb_catstr(struct wb
*wb
, char *s
, char *end
)
517 collect
= wb_collect(wb
, 0);
518 while (s
< end
&& (c
= escread(&s
, &d
)) >= 0)
520 wb_collect(wb
, collect
);
523 /* return the size of \(hy if appended to wb */
524 int wb_hywid(struct wb
*wb
)
526 struct glyph
*g
= dev_glyph("hy", wb
->f
);
527 return g
? font_gwid(g
->font
, dev_font(wb
->f
), wb
->s
, g
->wid
) : 0;