tr: add .fmap to map character names to glyphs
[neatroff.git] / roff.h
blob3dcae60dbf886c227f4e9cca0db54df0d04c0634
1 /* predefined array limits */
2 #define PATHLEN 1024 /* path length */
3 #define NFILES 16 /* number of input files */
4 #define NFONTS 32 /* number of fonts */
5 #define NGLYPHS 1024 /* glyphs in fonts */
6 #define NLIGS 128 /* number of font ligatures */
7 #define NKERNS 1024 /* number of font kerning pairs */
8 #define FNLEN 32 /* font name length */
9 #define NMLEN 32 /* macro/register/environment/glyph name length */
10 #define GNLEN NMLEN /* glyph name length */
11 #define RNLEN NMLEN /* register/macro name */
12 #define ILNLEN 1000 /* line limit of input files */
13 #define LNLEN 4000 /* line buffer length (ren.c/out.c) */
14 #define NWORDS 256 /* number of words in line buffer */
15 #define NARGS 16 /* number of macro arguments */
16 #define NPREV 16 /* environment stack depth */
17 #define NTRAPS 1024 /* number of traps per page */
18 #define NIES 128 /* number of nested .ie commands */
19 #define NTABS 16 /* number of tab stops */
20 #define NCMAPS 512 /* number of character translations (.tr) */
21 #define NSSTR 32 /* number of nested sstr_push() calls */
22 #define NFIELDS 32 /* number of fields */
23 #define MAXFRAC 100000 /* maximum value of the fractional part */
24 #define LIGLEN 4 /* length of ligatures */
25 #define NCDEFS 128 /* number of character definitions (.char) */
27 /* converting scales */
28 #define SC_IN (dev_res) /* inch in units */
29 #define SC_PT (SC_IN / 72) /* point in units */
30 #define SC_EM (n_s * SC_IN / 72)
31 #define SC_DW (SC_EM / 3) /* default width */
32 #define SC_HT (n_s * SC_PT) /* character height */
34 /* escape sequences */
35 #define ESC_Q "bCDhHlLNoSvwxX" /* \X'ccc' quoted escape sequences */
36 #define ESC_P "*fgkmns" /* \Xc \X(cc \X[ccc] escape sequences */
38 #define MIN(a, b) ((a) < (b) ? (a) : (b))
39 #define MAX(a, b) ((a) < (b) ? (b) : (a))
40 #define LEN(a) (sizeof(a) / sizeof((a)[0]))
42 /* special characters */
43 extern int c_ec; /* escape character (\) */
44 extern int c_cc; /* basic control character (.) */
45 extern int c_c2; /* no-break control character (') */
46 #define c_ni 4 /* non-interpreted copy mode escape */
47 #define c_hc env_hc()/* hyphenation character */
48 #define c_mc env_mc()/* margin character (.mc) */
49 #define c_tc env_tc()
50 #define c_lc env_lc()
52 /* number registers */
53 int num_get(int id, int inc);
54 void num_set(int id, int val);
55 void num_inc(int id, int val);
56 void num_del(int id);
57 char *num_str(int id);
58 char *num_getfmt(int id);
59 void num_setfmt(int id, char *fmt);
60 int *nreg(int id);
61 int eval(char *s, int unit);
62 int eval_up(char **s, int unit);
63 int eval_re(char *s, int orig, int unit);
65 /* string registers */
66 void str_set(int id, char *s);
67 void str_dset(int id, void *d);
68 char *str_get(int id);
69 void *str_dget(int id);
70 void str_rm(int id);
71 void str_rn(int src, int dst);
73 /* saving and restoring registers before and after printing diverted lines */
74 void odiv_beg(void);
75 void odiv_end(void);
77 /* enviroments */
78 void env_init(void);
79 void env_done(void);
80 struct adj *env_adj(void);
81 char *env_hc(void);
82 char *env_mc(void);
83 char *env_tc(void);
84 char *env_lc(void);
85 int tab_next(int pos);
86 int tab_type(int pos);
88 /* device related variables */
89 extern int dev_res;
90 extern int dev_uwid;
91 extern int dev_hor;
92 extern int dev_ver;
94 struct glyph {
95 char id[GNLEN]; /* device-dependent glyph identifier */
96 char name[GNLEN]; /* the first character mapped to this glyph */
97 struct font *font; /* glyph font */
98 int wid; /* character width */
99 int type; /* character type; ascender/descender */
102 struct font {
103 char name[FNLEN];
104 char fontname[FNLEN];
105 struct glyph glyphs[NGLYPHS];
106 int nglyphs;
107 int spacewid;
108 int special;
109 int cs, bd; /* for .cs and .bd requests */
110 /* charset section characters */
111 char c[NGLYPHS][GNLEN]; /* character names in charset */
112 struct glyph *g[NGLYPHS]; /* character glyphs in charset */
113 struct glyph *g_map[NGLYPHS]; /* character remapped via font_map() */
114 int n; /* number of characters in charset */
115 /* glyph table based on the first character of their id fields in glyphs[] */
116 int ghead[256]; /* glyph list heads */
117 int gnext[NGLYPHS]; /* next item in glyph lists */
118 /* character table based on the first character of glyph names in c[] */
119 int chead[256]; /* character list heads */
120 int cnext[NGLYPHS]; /* next item in character lists */
121 /* font ligatures (lg*) */
122 char lg[NLIGS][LIGLEN * GNLEN]; /* ligatures */
123 int lgn; /* number of ligatures in lg[] */
124 /* kerning pair table per glyph (kn*) */
125 int knhead[NGLYPHS]; /* kerning pairs of glyphs[] */
126 int knnext[NKERNS]; /* next item in knhead[] list */
127 int knpair[NKERNS]; /* kerning pair 2nd glyphs */
128 int knval[NKERNS]; /* font pairwise kerning value */
129 int knn; /* number of kerning pairs */
132 /* output device functions */
133 int dev_open(char *dir, char *dev);
134 void dev_close(void);
135 int dev_mnt(int pos, char *id, char *name);
136 int dev_pos(char *id);
137 struct font *dev_font(int pos);
138 int dev_fontpos(struct font *fn);
139 void dev_setcs(int fn, int cs);
140 int dev_getcs(int fn);
141 void dev_setbd(int fn, int bd);
142 int dev_getbd(int fn);
144 /* font-related functions */
145 struct font *font_open(char *path);
146 void font_close(struct font *fn);
147 struct glyph *font_glyph(struct font *fn, char *id);
148 struct glyph *font_find(struct font *fn, char *name);
149 int font_lig(struct font *fn, char **c, int n);
150 int font_kern(struct font *fn, char *c1, char *c2);
151 int font_islig(struct font *fn, char *s);
152 int font_map(struct font *fn, char *name, struct glyph *gl);
153 int font_mapped(struct font *fn, char *name);
155 /* glyph handling functions */
156 struct glyph *dev_glyph(char *c, int fn);
157 struct glyph *dev_glyph_byid(char *id, int fn);
158 int charwid(int fn, int sz, int wid);
159 int spacewid(int fn, int sz);
160 int charwid_base(int fn, int sz, int wid);
162 /* different layers of neatroff */
163 int in_next(void); /* input layer */
164 int cp_next(void); /* copy-mode layer */
165 int tr_next(void); /* troff layer */
167 void in_push(char *s, char **args);
168 void in_pushnl(char *s, char **args);
169 void in_so(char *path); /* .so request */
170 void in_nx(char *path); /* .nx request */
171 void in_ex(void); /* .nx request */
172 void in_lf(char *path, int ln); /* .lf request */
173 void in_queue(char *path); /* .ex request */
174 char *in_arg(int i); /* look up argument */
175 int in_nargs(void); /* number of arguments */
176 void in_back(int c); /* push back input character */
177 int in_top(void); /* the first pushed-back character */
178 char *in_filename(void); /* current filename */
179 int in_lnum(void); /* current line number */
181 void cp_blk(int skip); /* skip or read the next line or block */
182 void cp_wid(int enable); /* control inlining \w requests */
183 #define cp_back in_back /* cp.c is stateless */
184 void tr_first(void); /* read until the first non-command line */
186 /* variable length string buffer */
187 struct sbuf {
188 char *s; /* allocated buffer */
189 int sz; /* buffer size */
190 int n; /* length of the string stored in s */
193 void sbuf_init(struct sbuf *sbuf);
194 void sbuf_done(struct sbuf *sbuf);
195 char *sbuf_buf(struct sbuf *sbuf);
196 void sbuf_add(struct sbuf *sbuf, int c);
197 void sbuf_append(struct sbuf *sbuf, char *s);
198 void sbuf_printf(struct sbuf *sbuf, char *s, ...);
199 void sbuf_putnl(struct sbuf *sbuf);
200 void sbuf_cut(struct sbuf *sbuf, int n);
201 int sbuf_len(struct sbuf *sbuf);
202 int sbuf_empty(struct sbuf *sbuf);
204 /* word buffer */
205 struct wb {
206 struct sbuf sbuf;
207 int f, s, m; /* the last output font and size */
208 int r_f, r_s, r_m; /* current font and size; use n_f and n_s if -1 */
209 int part; /* partial input (\c) */
210 int els_neg, els_pos; /* extra line spacing */
211 int h, v; /* buffer vertical and horizontal positions */
212 int ct, sb, st; /* \w registers */
213 /* saving previous characters added via wb_put() */
214 char prev_c[LIGLEN][GNLEN];
215 int prev_l[LIGLEN]; /* sbuf_len(&wb->sbuf) before wb_put() calls */
216 int prev_h[LIGLEN]; /* wb->h before wb_put() calls */
217 int prev_n; /* number of characters in prev_c[] */
218 int prev_ll; /* sbuf_len(&wb->sbuf) after the last wb_put() */
221 void wb_init(struct wb *wb);
222 void wb_done(struct wb *wb);
223 void wb_hmov(struct wb *wb, int n);
224 void wb_vmov(struct wb *wb, int n);
225 void wb_els(struct wb *wb, int els);
226 void wb_etc(struct wb *wb, char *x);
227 void wb_put(struct wb *wb, char *c);
228 void wb_putexpand(struct wb *wb, char *c);
229 int wb_part(struct wb *wb);
230 void wb_setpart(struct wb *wb);
231 void wb_drawl(struct wb *wb, int c, int h, int v);
232 void wb_drawc(struct wb *wb, int c, int r);
233 void wb_drawe(struct wb *wb, int c, int h, int v);
234 void wb_drawa(struct wb *wb, int c, int h1, int v1, int h2, int v2);
235 void wb_drawxbeg(struct wb *wb, int c);
236 void wb_drawxdot(struct wb *wb, int h, int v);
237 void wb_drawxend(struct wb *wb);
238 void wb_cat(struct wb *wb, struct wb *src);
239 int wb_hyph(struct wb *wb, int w, struct wb *w1, struct wb *w2, int flg);
240 int wb_wid(struct wb *wb);
241 int wb_empty(struct wb *wb);
242 int wb_eos(struct wb *wb);
243 void wb_wconf(struct wb *wb, int *ct, int *st, int *sb);
244 int wb_lig(struct wb *wb, char *c);
245 int wb_kern(struct wb *wb, char *c);
247 /* character translation (.tr) */
248 void cmap_add(char *c1, char *c2);
249 char *cmap_map(char *c);
250 /* character definition (.char) */
251 char *cdef_map(char *c, int fn);
252 int cdef_expand(struct wb *wb, char *c, int fn);
254 /* hyphenation flags */
255 #define HY_MASK 0x0f /* enable hyphenation */
256 #define HY_LAST 0x02 /* do not hyphenate last lines */
257 #define HY_FINAL2 0x04 /* do not hyphenate the final two characters */
258 #define HY_FIRST2 0x08 /* do not hyphenate the first two characters */
259 #define HY_ANY 0x10 /* break at any possible position */
261 void hyphenate(char *hyphs, char *word, int flg);
263 /* adjustment */
264 #define AD_L 0
265 #define AD_B 1
266 #define AD_C 3
267 #define AD_R 5
269 struct adj *adj_alloc(void);
270 void adj_free(struct adj *adj);
271 int adj_fill(struct adj *adj, int ad_b, int fill, int hyph, struct sbuf *dst,
272 int *ll, int *in, int *ti, int *els_neg, int *els_pos);
273 int adj_full(struct adj *adj, int fill);
274 int adj_empty(struct adj *adj, int fill);
275 int adj_wid(struct adj *adj);
276 void adj_swid(struct adj *adj, int swid);
277 void adj_ll(struct adj *adj, int ll);
278 void adj_in(struct adj *adj, int in);
279 void adj_ti(struct adj *adj, int ti);
280 void adj_wb(struct adj *adj, struct wb *wb);
281 void adj_nl(struct adj *adj);
282 void adj_sp(struct adj *adj);
283 void adj_nonl(struct adj *adj);
285 /* rendering */
286 int render(void); /* the main loop */
287 int ren_parse(struct wb *wb, char *c);
288 int ren_char(struct wb *wb, int (*next)(void), void (*back)(int));
289 int ren_wid(int (*next)(void), void (*back)(int));
290 void ren_tl(int (*next)(void), void (*back)(int));
291 void ren_hline(struct wb *wb, int l, char *c); /* horizontal line */
292 void ren_hlcmd(struct wb *wb, char *arg); /* \l */
293 void ren_vlcmd(struct wb *wb, char *arg); /* \L */
294 void ren_bcmd(struct wb *wb, char *arg); /* \b */
295 void ren_ocmd(struct wb *wb, char *arg); /* \o */
296 void ren_dcmd(struct wb *wb, char *arg); /* \D */
298 /* out.c */
299 void out_line(char *s); /* output rendered line */
300 void out(char *s, ...); /* output troff cmd */
302 /* troff commands */
303 void tr_ab(char **args);
304 void tr_bp(char **args);
305 void tr_br(char **args);
306 void tr_ce(char **args);
307 void tr_ch(char **args);
308 void tr_cl(char **args);
309 void tr_di(char **args);
310 void tr_divbeg(char **args);
311 void tr_divend(char **args);
312 void tr_dt(char **args);
313 void tr_em(char **args);
314 void tr_ev(char **args);
315 void tr_fc(char **args);
316 void tr_fi(char **args);
317 void tr_fp(char **args);
318 void tr_fspecial(char **args);
319 void tr_ft(char **args);
320 void tr_hw(char **args);
321 void tr_in(char **args);
322 void tr_ll(char **args);
323 void tr_mk(char **args);
324 void tr_ne(char **args);
325 void tr_nf(char **args);
326 void tr_ns(char **args);
327 void tr_os(char **args);
328 void tr_pn(char **args);
329 void tr_ps(char **args);
330 void tr_rs(char **args);
331 void tr_rt(char **args);
332 void tr_sp(char **args);
333 void tr_sv(char **args);
334 void tr_ta(char **args);
335 void tr_ti(char **args);
336 void tr_wh(char **args);
337 void tr_eject(char **args);
339 void tr_init(void);
341 /* helpers */
342 void errmsg(char *msg, ...);
343 void errdie(char *msg);
344 int utf8len(int c);
345 int utf8next(char *s, int (*next)(void));
346 int utf8read(char **s, char *d);
347 int utf8one(char *s);
348 int charnext(char *c, int (*next)(void), void (*back)(int));
349 int charread(char **s, char *c);
350 int charnext_delim(char *c, int (*next)(void), void (*back)(int), char *delim);
351 void charnext_str(char *d, char *c);
352 void argnext(char *d, int cmd, int (*next)(void), void (*back)(int));
353 void argread(char **sp, char *d, int cmd);
354 int escread(char **s, char *d);
355 /* string streams; nested next()/back() interface for string buffers */
356 void sstr_push(char *s);
357 char *sstr_pop(void);
358 int sstr_next(void);
359 void sstr_back(int c);
361 /* internal commands */
362 #define TR_DIVBEG "\07<" /* diversion begins */
363 #define TR_DIVEND "\07>" /* diversion ends */
364 #define TR_EJECT "\07P" /* page eject */
366 /* mapping register, macro and environment names to indices */
367 #define NREGS 4096 /* maximum number of mapped names */
368 #define DOTMAP(c2) (c2) /* optimized mapping for ".x" names */
370 int map(char *s); /* map name s to an index */
371 char *map_name(int id); /* return the name mapped to id */
373 /* colors */
374 #define CLR_R(c) (((c) >> 16) & 0xff)
375 #define CLR_G(c) (((c) >> 8) & 0xff)
376 #define CLR_B(c) ((c) & 0xff)
377 #define CLR_RGB(r, g, b) (((r) << 16) | ((g) << 8) | (b))
379 char *clr_str(int c);
380 int clr_get(char *s);
382 /* builtin number registers; n_X for .X register */
383 #define n_a (*nreg(DOTMAP('a')))
384 #define n_cp (*nreg(DOTMAP('C')))
385 #define n_d (*nreg(DOTMAP('d')))
386 #define n_f (*nreg(DOTMAP('f')))
387 #define n_h (*nreg(DOTMAP('h')))
388 #define n_i (*nreg(DOTMAP('i')))
389 #define n_it (*nreg(map(".it"))) /* .it trap macro */
390 #define n_itn (*nreg(map(".itn"))) /* .it lines left */
391 #define n_j (*nreg(DOTMAP('j')))
392 #define n_l (*nreg(DOTMAP('l')))
393 #define n_L (*nreg(DOTMAP('L')))
394 #define n_n (*nreg(DOTMAP('n')))
395 #define n_nI (*nreg(map(".nI"))) /* i for .nm */
396 #define n_nm (*nreg(map(".nm"))) /* .nm enabled */
397 #define n_nM (*nreg(map(".nM"))) /* m for .nm */
398 #define n_nn (*nreg(map(".nn"))) /* remaining .nn */
399 #define n_nS (*nreg(map(".nS"))) /* s for .nm */
400 #define n_m (*nreg(DOTMAP('m')))
401 #define n_mc (*nreg(map(".mc"))) /* .mc enabled */
402 #define n_mcn (*nreg(map(".mcn"))) /* .mc distance */
403 #define n_o (*nreg(DOTMAP('o')))
404 #define n_p (*nreg(DOTMAP('p')))
405 #define n_s (*nreg(DOTMAP('s')))
406 #define n_u (*nreg(DOTMAP('u')))
407 #define n_v (*nreg(DOTMAP('v')))
408 #define n_ct (*nreg(map("ct")))
409 #define n_dl (*nreg(map("dl")))
410 #define n_dn (*nreg(map("dn")))
411 #define n_ln (*nreg(map("ln")))
412 #define n_nl (*nreg(map("nl")))
413 #define n_sb (*nreg(map("sb")))
414 #define n_st (*nreg(map("st")))
415 #define n_pg (*nreg(map("%"))) /* % */
416 #define n_lb (*nreg(map(".b0"))) /* input line beg */
417 #define n_ce (*nreg(map(".ce"))) /* .ce remaining */
418 #define n_f0 (*nreg(map(".f0"))) /* last .f */
419 #define n_lg (*nreg(map(".lg"))) /* .lg mode */
420 #define n_hy (*nreg(map(".hy"))) /* .hy mode */
421 #define n_i0 (*nreg(map(".i0"))) /* last .i */
422 #define n_kn (*nreg(map(".kern"))) /* .kn mode */
423 #define n_l0 (*nreg(map(".l0"))) /* last .l */
424 #define n_L0 (*nreg(map(".L0"))) /* last .L */
425 #define n_m0 (*nreg(map(".m0"))) /* last .m */
426 #define n_mk (*nreg(map(".mk"))) /* .mk internal register */
427 #define n_na (*nreg(map(".na"))) /* .na mode */
428 #define n_ns (*nreg(map(".ns"))) /* .ns mode */
429 #define n_o0 (*nreg(map(".o0"))) /* last .o */
430 #define n_ss (*nreg(map(".ss"))) /* .ss value */
431 #define n_s0 (*nreg(map(".s0"))) /* last .s */
432 #define n_sv (*nreg(map(".sv"))) /* .sv value */
433 #define n_lt (*nreg(map(".lt"))) /* .lt value */
434 #define n_t0 (*nreg(map(".lt0"))) /* previous .lt value */
435 #define n_v0 (*nreg(map(".v0"))) /* last .v */
437 /* functions for implementing read-only registers */
438 int f_nexttrap(void); /* .t */
439 int f_divreg(void); /* .z */
440 int f_hpos(void); /* .k */