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
= !strcmp(c_hc
, c
) || !strcmp(c_bp
, 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 int c_isdash(char *c
)
164 return !strcmp("-", c
) || !strcmp("em", c
) || !strcmp("hy", c
);
167 /* return nonzero if it cannot be hyphenated */
168 static int wb_hyph(char src
[][GNLEN
], int src_n
, char *src_hyph
, int flg
)
170 char word
[WORDLEN
* GNLEN
]; /* word to pass to hyphenate() */
171 char hyph
[WORDLEN
* GNLEN
]; /* hyphenation points of word */
172 int smap
[WORDLEN
]; /* the mapping from src[] to word[] */
177 for (i
= 0; i
< src_n
; i
++) {
180 if (c_isdash(s
) || !strcmp(c_hc
, s
))
182 if (!strcmp(c_bp
, s
))
184 if (!utf8one(s
) || (!s
[1] && !isalpha((unsigned char) s
[0])))
190 memset(hyph
, 0, (d
- word
) * sizeof(hyph
[0]));
191 hyphenate(hyph
, word
, flg
);
192 for (i
= 0; i
< src_n
; i
++)
193 src_hyph
[i
] = hyph
[smap
[i
]];
197 static int wb_collect(struct wb
*wb
, int val
)
199 int old
= wb
->sub_collect
;
200 wb
->sub_collect
= val
;
204 static void wb_flushsub(struct wb
*wb
)
207 struct glyph
*gsrc
[WORDLEN
];
208 struct glyph
*gdst
[WORDLEN
];
209 int x
[WORDLEN
], y
[WORDLEN
], xadv
[WORDLEN
], yadv
[WORDLEN
];
211 char src_hyph
[WORDLEN
];
213 if (!wb
->sub_n
|| !wb
->sub_collect
)
216 fn
= dev_font(wb
->f
);
217 if (!n_hy
|| wb_hyph(wb
->sub_c
, wb
->sub_n
, src_hyph
, n_hy
))
218 memset(src_hyph
, 0, sizeof(src_hyph
));
219 for (i
= 0; i
< wb
->sub_n
; i
++)
220 gsrc
[i
] = font_find(fn
, wb
->sub_c
[i
]);
221 dst_n
= font_layout(fn
, gsrc
, wb
->sub_n
, wb
->s
,
222 gdst
, dmap
, x
, y
, xadv
, yadv
, n_lg
, n_kn
);
223 for (i
= 0; i
< dst_n
; i
++) {
225 wb_hmov(wb
, font_wid(fn
, wb
->s
, x
[i
]));
227 wb_vmov(wb
, font_wid(fn
, wb
->s
, y
[i
]));
228 if (src_hyph
[dmap
[i
]])
230 if (gdst
[i
] == gsrc
[dmap
[i
]])
231 wb_putbuf(wb
, wb
->sub_c
[dmap
[i
]]);
233 wb_putbuf(wb
, gdst
[i
]->name
);
235 wb_hmov(wb
, font_wid(fn
, wb
->s
, xadv
[i
] - x
[i
]));
237 wb_vmov(wb
, font_wid(fn
, wb
->s
, yadv
[i
] - y
[i
]));
244 void wb_put(struct wb
*wb
, char *c
)
252 wb_hmov(wb
, font_swid(dev_font(R_F(wb
)), R_S(wb
), n_ss
));
255 if (wb_pendingfont(wb
) || wb
->sub_n
== LEN(wb
->sub_c
))
257 if (wb
->sub_collect
) {
258 if (font_find(dev_font(wb
->f
), c
))
259 strcpy(wb
->sub_c
[wb
->sub_n
++], c
);
267 /* just like wb_put() but disable subword collection */
268 void wb_putraw(struct wb
*wb
, char *c
)
272 collect
= wb_collect(wb
, 0);
274 wb_collect(wb
, collect
);
277 /* just like wb_put(), but call cdef_expand() if c is defined */
278 void wb_putexpand(struct wb
*wb
, char *c
)
280 if (cdef_expand(wb
, c
, R_F(wb
)))
284 int wb_part(struct wb
*wb
)
289 void wb_setpart(struct wb
*wb
)
294 void wb_drawl(struct wb
*wb
, int c
, int h
, int v
)
297 sbuf_printf(&wb
->sbuf
, "%cD'%c %du %du'", c_ec
, c
, h
, v
);
303 void wb_drawc(struct wb
*wb
, int c
, int r
)
306 sbuf_printf(&wb
->sbuf
, "%cD'%c %du'", c_ec
, c
, r
);
310 void wb_drawe(struct wb
*wb
, int c
, int h
, int v
)
313 sbuf_printf(&wb
->sbuf
, "%cD'%c %du %du'", c_ec
, c
, h
, v
);
317 void wb_drawa(struct wb
*wb
, int c
, int h1
, int v1
, int h2
, int v2
)
320 sbuf_printf(&wb
->sbuf
, "%cD'%c %du %du %du %du'",
321 c_ec
, c
, h1
, v1
, h2
, v2
);
327 void wb_drawxbeg(struct wb
*wb
, int c
)
330 sbuf_printf(&wb
->sbuf
, "%cD'%c", c_ec
, c
);
333 void wb_drawxdot(struct wb
*wb
, int h
, int v
)
335 sbuf_printf(&wb
->sbuf
, " %du %du", h
, v
);
341 void wb_drawxend(struct wb
*wb
)
343 sbuf_printf(&wb
->sbuf
, "'");
346 void wb_reset(struct wb
*wb
)
352 char *wb_buf(struct wb
*wb
)
355 return sbuf_buf(&wb
->sbuf
);
358 static void wb_putc(struct wb
*wb
, int t
, char *s
)
374 wb_hmov(wb
, atoi(s
));
377 wb
->r_m
= clr_get(s
);
383 wb_vmov(wb
, atoi(s
));
394 void wb_cat(struct wb
*wb
, struct wb
*src
)
402 collect
= wb_collect(wb
, 0);
403 s
= sbuf_buf(&src
->sbuf
);
404 while ((c
= escread(&s
, d
)) >= 0)
412 wb_collect(wb
, collect
);
415 int wb_wid(struct wb
*wb
)
421 int wb_hpos(struct wb
*wb
)
427 int wb_vpos(struct wb
*wb
)
433 int wb_empty(struct wb
*wb
)
435 return !wb
->sub_n
&& sbuf_empty(&wb
->sbuf
);
438 /* return 1 if wb ends a sentence (.?!) */
439 int wb_eos(struct wb
*wb
)
441 int i
= wb
->sub_n
- 1;
442 while (i
> 0 && strchr("'\")]*", wb
->sub_c
[i
][0]))
444 return i
>= 0 && strchr(".?!", wb
->sub_c
[i
][0]);
447 void wb_wconf(struct wb
*wb
, int *ct
, int *st
, int *sb
,
448 int *llx
, int *lly
, int *urx
, int *ury
)
454 *llx
= wb
->llx
< BBMAX
? wb
->llx
: 0;
455 *lly
= wb
->lly
< BBMAX
? -wb
->lly
: 0;
456 *urx
= wb
->urx
> BBMIN
? wb
->urx
: 0;
457 *ury
= wb
->ury
> BBMIN
? -wb
->ury
: 0;
460 static struct glyph
*wb_prevglyph(struct wb
*wb
)
462 return wb
->sub_n
? dev_glyph(wb
->sub_c
[wb
->sub_n
- 1], wb
->f
) : NULL
;
465 void wb_italiccorrection(struct wb
*wb
)
467 struct glyph
*g
= wb_prevglyph(wb
);
468 if (g
&& glyph_ic(g
))
469 wb_hmov(wb
, font_wid(g
->font
, wb
->s
, glyph_ic(g
)));
472 void wb_italiccorrectionleft(struct wb
*wb
)
478 void wb_fnszget(struct wb
*wb
, int *fn
, int *sz
, int *m
)
486 void wb_fnszset(struct wb
*wb
, int fn
, int sz
, int m
)
493 void wb_catstr(struct wb
*wb
, char *s
, char *end
)
498 collect
= wb_collect(wb
, 0);
499 while (s
< end
&& (c
= escread(&s
, d
)) >= 0)
501 wb_collect(wb
, collect
);
504 /* return the size of \(hy if appended to wb */
505 int wb_dashwid(struct wb
*wb
)
507 struct glyph
*g
= dev_glyph("hy", wb
->f
);
508 return g
? font_gwid(g
->font
, dev_font(wb
->f
), wb
->s
, g
->wid
) : 0;