ren: disable .char for diverted text
[neatroff.git] / roff.h
bloba65263ae4c31e8d935d5f56bf99af4677af8edab
1 /* converting scales */
2 #define SC_IN (dev_res) /* inch in units */
3 #define SC_PT (SC_IN / 72) /* point in units */
4 #define SC_EM (n_s * SC_IN / 72)
5 #define SC_DW (SC_EM / 3) /* default width */
6 #define SC_HT (n_s * SC_PT) /* character height */
8 /* predefined array limits */
9 #define PATHLEN 1024 /* path length */
10 #define NFILES 16 /* number of input files */
11 #define NFONTS 32 /* number of fonts */
12 #define NGLYPHS 1024 /* glyphs in fonts */
13 #define NLIGS 128 /* number of font ligatures */
14 #define NKERNS 1024 /* number of font pairwise kerning pairs */
15 #define FNLEN 32 /* font name length */
16 #define NMLEN 32 /* macro/register/environment/glyph name length */
17 #define GNLEN NMLEN /* glyph name length */
18 #define RNLEN NMLEN /* register/macro name */
19 #define ILNLEN 1000 /* line limit of input files */
20 #define LNLEN 4000 /* line buffer length (ren.c/out.c) */
21 #define NWORDS 256 /* number of words in line buffer */
22 #define NARGS 16 /* number of macro arguments */
23 #define NPREV 16 /* environment stack depth */
24 #define NTRAPS 1024 /* number of traps per page */
25 #define NIES 128 /* number of nested .ie commands */
26 #define NTABS 16 /* number of tab stops */
27 #define NTR 512 /* number of character translations (.tr) */
28 #define NSSTR 32 /* number of nested sstr_push() calls */
29 #define NFIELDS 32 /* number of fields */
30 #define MAXFRAC 100000 /* maximum value of the fractional part */
31 #define LIGLEN 4 /* length of ligatures */
32 #define NCHDEF 128 /* number of character definitions (.char) */
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 name[GNLEN]; /* name of the glyph */
96 char id[GNLEN]; /* device-dependent glyph identifier */
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 char c[NGLYPHS][GNLEN]; /* character names in charset */
111 struct glyph *g[NGLYPHS]; /* character glyphs in charset */
112 int n; /* number of characters in charset */
113 /* font ligatures */
114 char lig[NLIGS][LIGLEN * GNLEN];
115 int nlig;
116 /* glyph list based on the first character of glyph names */
117 int head[256]; /* glyph list head */
118 int next[NGLYPHS]; /* next item in glyph list */
119 /* kerning pair list per glyph */
120 int knhead[NGLYPHS]; /* kerning pairs of glyphs[] */
121 int knnext[NKERNS]; /* next item in knhead[] list */
122 int knpair[NKERNS]; /* kerning pair 2nd glyphs */
123 int knval[NKERNS]; /* font pairwise kerning value */
124 int knn; /* number of kerning pairs */
127 /* output device functions */
128 int dev_open(char *dir, char *dev);
129 void dev_close(void);
130 int dev_mnt(int pos, char *id, char *name);
131 int dev_pos(char *id);
132 struct font *dev_font(int pos);
133 void dev_setcs(int fn, int cs);
134 int dev_getcs(int fn);
135 void dev_setbd(int fn, int bd);
136 int dev_getbd(int fn);
138 /* font-related functions */
139 struct font *font_open(char *path);
140 void font_close(struct font *fn);
141 struct glyph *font_glyph(struct font *fn, char *id);
142 struct glyph *font_find(struct font *fn, char *name);
143 int font_lig(struct font *fn, char **c, int n);
144 int font_kern(struct font *fn, char *c1, char *c2);
145 int font_islig(struct font *fn, char *s);
147 /* glyph handling functions */
148 struct glyph *dev_glyph(char *c, int fn);
149 struct glyph *dev_glyph_byid(char *id, int fn);
150 int charwid(int fn, int sz, int wid);
151 int spacewid(int fn, int sz);
152 int charwid_base(int fn, int sz, int wid);
154 /* different layers of neatroff */
155 int in_next(void); /* input layer */
156 int cp_next(void); /* copy-mode layer */
157 int tr_next(void); /* troff layer */
159 void in_push(char *s, char **args);
160 void in_pushnl(char *s, char **args);
161 void in_so(char *path); /* .so request */
162 void in_nx(char *path); /* .nx request */
163 void in_ex(void); /* .nx request */
164 void in_lf(char *path, int ln); /* .lf request */
165 void in_queue(char *path); /* .ex request */
166 char *in_arg(int i); /* look up argument */
167 int in_nargs(void); /* number of arguments */
168 void in_back(int c); /* push back input character */
169 int in_top(void); /* the first pushed-back character */
170 char *in_filename(void); /* current filename */
171 int in_lnum(void); /* current line number */
173 void cp_blk(int skip); /* skip or read the next line or block */
174 void cp_wid(int enable); /* control inlining \w requests */
175 #define cp_back in_back /* cp.c is stateless */
176 void tr_first(void); /* read until the first non-command line */
178 /* character translation (.tr) */
179 void tr_add(char *c1, char *c2);
180 char *tr_map(char *c);
181 /* character definition (.char) */
182 char *chdef_map(char *c);
184 /* variable length string buffer */
185 struct sbuf {
186 char *s; /* allocated buffer */
187 int sz; /* buffer size */
188 int n; /* length of the string stored in s */
191 void sbuf_init(struct sbuf *sbuf);
192 void sbuf_done(struct sbuf *sbuf);
193 char *sbuf_buf(struct sbuf *sbuf);
194 void sbuf_add(struct sbuf *sbuf, int c);
195 void sbuf_append(struct sbuf *sbuf, char *s);
196 void sbuf_printf(struct sbuf *sbuf, char *s, ...);
197 void sbuf_putnl(struct sbuf *sbuf);
198 void sbuf_cut(struct sbuf *sbuf, int n);
199 int sbuf_len(struct sbuf *sbuf);
200 int sbuf_empty(struct sbuf *sbuf);
202 /* word buffer */
203 struct wb {
204 struct sbuf sbuf;
205 int f, s, m; /* the last output font and size */
206 int r_f, r_s, r_m; /* current font and size; use n_f and n_s if -1 */
207 int part; /* partial input (\c) */
208 int els_neg, els_pos; /* extra line spacing */
209 int h, v; /* buffer vertical and horizontal positions */
210 int ct, sb, st; /* \w registers */
211 /* saving previous characters added via wb_put() */
212 char prev_c[LIGLEN][GNLEN];
213 int prev_l[LIGLEN]; /* sbuf_len(&wb->sbuf) before wb_put() calls */
214 int prev_h[LIGLEN]; /* wb->h before wb_put() calls */
215 int prev_n; /* number of characters in prev_c[] */
216 int prev_ll; /* sbuf_len(&wb->sbuf) after the last wb_put() */
219 void wb_init(struct wb *wb);
220 void wb_done(struct wb *wb);
221 void wb_hmov(struct wb *wb, int n);
222 void wb_vmov(struct wb *wb, int n);
223 void wb_els(struct wb *wb, int els);
224 void wb_etc(struct wb *wb, char *x);
225 void wb_put(struct wb *wb, char *c);
226 void wb_putexpand(struct wb *wb, char *c);
227 int wb_part(struct wb *wb);
228 void wb_setpart(struct wb *wb);
229 void wb_drawl(struct wb *wb, int c, int h, int v);
230 void wb_drawc(struct wb *wb, int c, int r);
231 void wb_drawe(struct wb *wb, int c, int h, int v);
232 void wb_drawa(struct wb *wb, int c, int h1, int v1, int h2, int v2);
233 void wb_drawxbeg(struct wb *wb, int c);
234 void wb_drawxdot(struct wb *wb, int h, int v);
235 void wb_drawxend(struct wb *wb);
236 void wb_cat(struct wb *wb, struct wb *src);
237 int wb_hyph(struct wb *wb, int w, struct wb *w1, struct wb *w2, int flg);
238 int wb_wid(struct wb *wb);
239 int wb_empty(struct wb *wb);
240 int wb_eos(struct wb *wb);
241 void wb_wconf(struct wb *wb, int *ct, int *st, int *sb);
242 int wb_lig(struct wb *wb, char *c);
243 int wb_kern(struct wb *wb, char *c);
245 /* hyphenation flags */
246 #define HY_MASK 0x0f /* enable hyphenation */
247 #define HY_LAST 0x02 /* do not hyphenate last lines */
248 #define HY_FINAL2 0x04 /* do not hyphenate the final two characters */
249 #define HY_FIRST2 0x08 /* do not hyphenate the first two characters */
250 #define HY_ANY 0x10 /* break at any possible position */
252 void hyphenate(char *hyphs, char *word, int flg);
254 /* adjustment */
255 #define AD_L 0
256 #define AD_B 1
257 #define AD_C 3
258 #define AD_R 5
260 struct adj *adj_alloc(void);
261 void adj_free(struct adj *adj);
262 int adj_fill(struct adj *adj, int ad_b, int fill, int hyph, struct sbuf *dst,
263 int *ll, int *in, int *ti, int *els_neg, int *els_pos);
264 int adj_full(struct adj *adj, int fill);
265 int adj_empty(struct adj *adj, int fill);
266 int adj_wid(struct adj *adj);
267 void adj_swid(struct adj *adj, int swid);
268 void adj_ll(struct adj *adj, int ll);
269 void adj_in(struct adj *adj, int in);
270 void adj_ti(struct adj *adj, int ti);
271 void adj_wb(struct adj *adj, struct wb *wb);
272 void adj_nl(struct adj *adj);
273 void adj_sp(struct adj *adj);
274 void adj_nonl(struct adj *adj);
276 /* rendering */
277 int render(void); /* the main loop */
278 int ren_char(struct wb *wb, int (*next)(void), void (*back)(int));
279 int ren_wid(int (*next)(void), void (*back)(int));
280 void ren_tl(int (*next)(void), void (*back)(int));
281 void ren_hline(struct wb *wb, int l, char *c); /* horizontal line */
282 void ren_hlcmd(struct wb *wb, char *arg); /* \l */
283 void ren_vlcmd(struct wb *wb, char *arg); /* \L */
284 void ren_bcmd(struct wb *wb, char *arg); /* \b */
285 void ren_ocmd(struct wb *wb, char *arg); /* \o */
286 void ren_dcmd(struct wb *wb, char *arg); /* \D */
287 int ren_expand(struct wb *wb, char *c); /* .char expansion */
289 /* out.c */
290 void out_line(char *s); /* output rendered line */
291 void out(char *s, ...); /* output troff cmd */
293 /* troff commands */
294 void tr_ab(char **args);
295 void tr_bp(char **args);
296 void tr_br(char **args);
297 void tr_ce(char **args);
298 void tr_ch(char **args);
299 void tr_cl(char **args);
300 void tr_di(char **args);
301 void tr_divbeg(char **args);
302 void tr_divend(char **args);
303 void tr_dt(char **args);
304 void tr_em(char **args);
305 void tr_ev(char **args);
306 void tr_fc(char **args);
307 void tr_fi(char **args);
308 void tr_fp(char **args);
309 void tr_ft(char **args);
310 void tr_hw(char **args);
311 void tr_in(char **args);
312 void tr_ll(char **args);
313 void tr_mk(char **args);
314 void tr_ne(char **args);
315 void tr_nf(char **args);
316 void tr_ns(char **args);
317 void tr_os(char **args);
318 void tr_pn(char **args);
319 void tr_ps(char **args);
320 void tr_rs(char **args);
321 void tr_rt(char **args);
322 void tr_sp(char **args);
323 void tr_sv(char **args);
324 void tr_ta(char **args);
325 void tr_ti(char **args);
326 void tr_wh(char **args);
327 void tr_eject(char **args);
329 void tr_init(void);
331 /* helpers */
332 void errmsg(char *msg, ...);
333 void errdie(char *msg);
334 int utf8len(int c);
335 int utf8next(char *s, int (*next)(void));
336 int utf8read(char **s, char *d);
337 int utf8one(char *s);
338 int charnext(char *c, int (*next)(void), void (*back)(int));
339 int charread(char **s, char *c);
340 int charnext_delim(char *c, int (*next)(void), void (*back)(int), char *delim);
341 void charnext_str(char *d, char *c);
342 void argnext(char *d, int cmd, int (*next)(void), void (*back)(int));
343 void argread(char **sp, char *d, int cmd);
344 int escread(char **s, char *d);
345 /* string streams; nested next()/back() interface for string buffers */
346 void sstr_push(char *s);
347 char *sstr_pop(void);
348 int sstr_next(void);
349 void sstr_back(int c);
351 /* internal commands */
352 #define TR_DIVBEG "\07<" /* diversion begins */
353 #define TR_DIVEND "\07>" /* diversion ends */
354 #define TR_EJECT "\07P" /* page eject */
356 /* mapping register, macro and environment names to indices */
357 #define NREGS 4096 /* maximum number of mapped names */
358 #define DOTMAP(c2) (c2) /* optimized mapping for ".x" names */
360 int map(char *s); /* map name s to an index */
361 char *map_name(int id); /* return the name mapped to id */
363 /* colors */
364 #define CLR_R(c) (((c) >> 16) & 0xff)
365 #define CLR_G(c) (((c) >> 8) & 0xff)
366 #define CLR_B(c) ((c) & 0xff)
367 #define CLR_RGB(r, g, b) (((r) << 16) | ((g) << 8) | (b))
369 char *clr_str(int c);
370 int clr_get(char *s);
372 /* builtin number registers; n_X for .X register */
373 #define n_a (*nreg(DOTMAP('a')))
374 #define n_cp (*nreg(DOTMAP('C')))
375 #define n_d (*nreg(DOTMAP('d')))
376 #define n_f (*nreg(DOTMAP('f')))
377 #define n_h (*nreg(DOTMAP('h')))
378 #define n_i (*nreg(DOTMAP('i')))
379 #define n_it (*nreg(map(".it"))) /* .it trap macro */
380 #define n_itn (*nreg(map(".itn"))) /* .it lines left */
381 #define n_j (*nreg(DOTMAP('j')))
382 #define n_l (*nreg(DOTMAP('l')))
383 #define n_L (*nreg(DOTMAP('L')))
384 #define n_n (*nreg(DOTMAP('n')))
385 #define n_nI (*nreg(map(".nI"))) /* i for .nm */
386 #define n_nm (*nreg(map(".nm"))) /* .nm enabled */
387 #define n_nM (*nreg(map(".nM"))) /* m for .nm */
388 #define n_nn (*nreg(map(".nn"))) /* remaining .nn */
389 #define n_nS (*nreg(map(".nS"))) /* s for .nm */
390 #define n_m (*nreg(DOTMAP('m')))
391 #define n_mc (*nreg(map(".mc"))) /* .mc enabled */
392 #define n_mcn (*nreg(map(".mcn"))) /* .mc distance */
393 #define n_o (*nreg(DOTMAP('o')))
394 #define n_p (*nreg(DOTMAP('p')))
395 #define n_s (*nreg(DOTMAP('s')))
396 #define n_u (*nreg(DOTMAP('u')))
397 #define n_v (*nreg(DOTMAP('v')))
398 #define n_ct (*nreg(map("ct")))
399 #define n_dl (*nreg(map("dl")))
400 #define n_dn (*nreg(map("dn")))
401 #define n_ln (*nreg(map("ln")))
402 #define n_nl (*nreg(map("nl")))
403 #define n_sb (*nreg(map("sb")))
404 #define n_st (*nreg(map("st")))
405 #define n_pg (*nreg(map("%"))) /* % */
406 #define n_lb (*nreg(map(".b0"))) /* input line beg */
407 #define n_ce (*nreg(map(".ce"))) /* .ce remaining */
408 #define n_f0 (*nreg(map(".f0"))) /* last .f */
409 #define n_lg (*nreg(map(".lg"))) /* .lg mode */
410 #define n_hy (*nreg(map(".hy"))) /* .hy mode */
411 #define n_i0 (*nreg(map(".i0"))) /* last .i */
412 #define n_kn (*nreg(map(".kern"))) /* .kn mode */
413 #define n_l0 (*nreg(map(".l0"))) /* last .l */
414 #define n_L0 (*nreg(map(".L0"))) /* last .L */
415 #define n_m0 (*nreg(map(".m0"))) /* last .m */
416 #define n_mk (*nreg(map(".mk"))) /* .mk internal register */
417 #define n_na (*nreg(map(".na"))) /* .na mode */
418 #define n_ns (*nreg(map(".ns"))) /* .ns mode */
419 #define n_o0 (*nreg(map(".o0"))) /* last .o */
420 #define n_ss (*nreg(map(".ss"))) /* .ss value */
421 #define n_s0 (*nreg(map(".s0"))) /* last .s */
422 #define n_sv (*nreg(map(".sv"))) /* .sv value */
423 #define n_lt (*nreg(map(".lt"))) /* .lt value */
424 #define n_t0 (*nreg(map(".lt0"))) /* previous .lt value */
425 #define n_v0 (*nreg(map(".v0"))) /* last .v */
427 /* functions for implementing read-only registers */
428 int f_nexttrap(void); /* .t */
429 int f_divreg(void); /* .z */
430 int f_hpos(void); /* .k */