xref: /openbsd-src/usr.bin/mandoc/term.h (revision 18bbf166f13c7fc2b27723eab6a98003cffb9162)
1*18bbf166Sschwarze /* $OpenBSD: term.h,v 1.78 2022/08/16 17:44:53 schwarze Exp $ */
2f73abda9Skristaps /*
3*18bbf166Sschwarze  * Copyright (c) 2011-2015,2017,2019,2022 Ingo Schwarze <schwarze@openbsd.org>
42791bd1cSschwarze  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
5f73abda9Skristaps  *
6f73abda9Skristaps  * Permission to use, copy, modify, and distribute this software for any
7a6464425Sschwarze  * purpose with or without fee is hereby granted, provided that the above
8a6464425Sschwarze  * copyright notice and this permission notice appear in all copies.
9f73abda9Skristaps  *
102a238f45Sschwarze  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
11a6464425Sschwarze  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
122a238f45Sschwarze  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
13a6464425Sschwarze  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14a6464425Sschwarze  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15a6464425Sschwarze  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16a6464425Sschwarze  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17f73abda9Skristaps  */
18f95d589eSschwarze 
19f73abda9Skristaps enum	termenc {
20a5e11edeSschwarze 	TERMENC_ASCII,
21a5e11edeSschwarze 	TERMENC_LOCALE,
22a5e11edeSschwarze 	TERMENC_UTF8
23f73abda9Skristaps };
24f73abda9Skristaps 
25f95d589eSschwarze enum	termtype {
26f95d589eSschwarze 	TERMTYPE_CHAR,
27ddce0b0cSschwarze 	TERMTYPE_PS,
28ddce0b0cSschwarze 	TERMTYPE_PDF
29f95d589eSschwarze };
30f95d589eSschwarze 
31fa70b73eSschwarze enum	termfont {
32fa70b73eSschwarze 	TERMFONT_NONE = 0,
33fa70b73eSschwarze 	TERMFONT_BOLD,
3406f7b709Sschwarze 	TERMFONT_UNDER,
3568941ea9Sschwarze 	TERMFONT_BI,
3606f7b709Sschwarze 	TERMFONT__MAX
37fa70b73eSschwarze };
38fa70b73eSschwarze 
39bf9acac6Sschwarze struct	eqn_box;
402a238f45Sschwarze struct	roff_meta;
4196a5de47Sschwarze struct	roff_node;
4296a5de47Sschwarze struct	tbl_span;
43dd617d76Sschwarze struct	termp;
44dd617d76Sschwarze 
452a238f45Sschwarze typedef void	(*term_margin)(struct termp *, const struct roff_meta *);
46f95d589eSschwarze 
472791bd1cSschwarze struct	termp_tbl {
482791bd1cSschwarze 	int		  width;	/* width in fixed chars */
492791bd1cSschwarze 	int		  decimal;	/* decimal point position */
502791bd1cSschwarze };
512791bd1cSschwarze 
52e93ea447Sschwarze struct	termp_col {
53e93ea447Sschwarze 	int		 *buf;		/* Output buffer. */
54e93ea447Sschwarze 	size_t		  maxcols;	/* Allocated bytes in buf. */
558afa4451Sschwarze 	size_t		  lastcol;	/* Last byte in buf. */
568f9c9bf2Sschwarze 	size_t		  col;		/* Byte in buf to be written. */
57e93ea447Sschwarze 	size_t		  rmargin;	/* Current right margin. */
58e93ea447Sschwarze 	size_t		  offset;	/* Current left margin. */
59dd2df837Sschwarze 	size_t		  taboff;	/* Offset for literal tabs. */
60e93ea447Sschwarze };
61e93ea447Sschwarze 
62f73abda9Skristaps struct	termp {
63e93ea447Sschwarze 	struct rofftbl	  tbl;		/* Table configuration. */
64e93ea447Sschwarze 	struct termp_col *tcols;	/* Array of table columns. */
65e93ea447Sschwarze 	struct termp_col *tcol;		/* Current table column. */
66e93ea447Sschwarze 	size_t		  maxtcol;	/* Allocated table columns. */
678f9c9bf2Sschwarze 	size_t		  lasttcol;	/* Last column currently used. */
68c0a657b3Sschwarze 	size_t		  line;		/* Current output line number. */
69e12fe158Sschwarze 	size_t		  defindent;	/* Default indent for text. */
702791bd1cSschwarze 	size_t		  defrmargin;	/* Right margin of the device. */
715281506aSschwarze 	size_t		  lastrmargin;	/* Right margin before the last ll. */
72f73abda9Skristaps 	size_t		  maxrmargin;	/* Max right margin. */
73108ab9ecSschwarze 	size_t		  col;		/* Byte position in buf. */
7414fc54faSschwarze 	size_t		  viscol;	/* Chars on current line. */
758f9c9bf2Sschwarze 	size_t		  trailspace;	/* See term_flushln(). */
76771c54bcSschwarze 	size_t		  minbl;	/* Minimum blanks before next field. */
77e93ea447Sschwarze 	int		  synopsisonly; /* Print the synopsis only. */
78e93ea447Sschwarze 	int		  mdocstyle;	/* Imitate mdoc(7) output. */
7911d70615Sschwarze 	int		  ti;		/* Temporary indent for one line. */
80ef8ef3ffSschwarze 	int		  skipvsp;	/* Vertical space to skip. */
81f73abda9Skristaps 	int		  flags;
82f2e60d14Sschwarze #define	TERMP_SENTENCE	 (1 << 0)	/* Space before a sentence. */
83f2e60d14Sschwarze #define	TERMP_NOSPACE	 (1 << 1)	/* No space before words. */
84f2e60d14Sschwarze #define	TERMP_NONOSPACE	 (1 << 2)	/* No space (no autounset). */
85f2e60d14Sschwarze #define	TERMP_NBRWORD	 (1 << 3)	/* Make next word nonbreaking. */
86f2e60d14Sschwarze #define	TERMP_KEEP	 (1 << 4)	/* Keep words together. */
87f2e60d14Sschwarze #define	TERMP_PREKEEP	 (1 << 5)	/* ...starting with the next one. */
88f2e60d14Sschwarze #define	TERMP_BACKAFTER	 (1 << 6)	/* Back up after next character. */
89f2e60d14Sschwarze #define	TERMP_BACKBEFORE (1 << 7)	/* Back up before next character. */
90072a1c5dSschwarze #define	TERMP_NOBREAK	 (1 << 8)	/* See term_flushln(). */
91b8d4b727Sschwarze #define	TERMP_BRTRSP	 (1 << 9)	/* See term_flushln(). */
92b8d4b727Sschwarze #define	TERMP_BRIND	 (1 << 10)	/* See term_flushln(). */
93771c54bcSschwarze #define	TERMP_HANG	 (1 << 11)	/* See term_flushln(). */
94771c54bcSschwarze #define	TERMP_NOPAD	 (1 << 12)	/* See term_flushln(). */
95b8d4b727Sschwarze #define	TERMP_NOSPLIT	 (1 << 13)	/* Do not break line before .An. */
96b8d4b727Sschwarze #define	TERMP_SPLIT	 (1 << 14)	/* Break line before .An. */
97b8d4b727Sschwarze #define	TERMP_NONEWLINE	 (1 << 15)	/* No line break in nofill mode. */
9824f1eaadSschwarze #define	TERMP_BRNEVER	 (1 << 16)	/* Don't even break at maxrmargin. */
9924f1eaadSschwarze #define	TERMP_NOBUF	 (1 << 17)	/* Bypass output buffer. */
10024f1eaadSschwarze #define	TERMP_NEWMC	 (1 << 18)	/* No .mc printed yet. */
10124f1eaadSschwarze #define	TERMP_ENDMC	 (1 << 19)	/* Next break ends .mc mode. */
1028afa4451Sschwarze #define	TERMP_MULTICOL	 (1 << 20)	/* Multiple column mode. */
103b306689cSschwarze #define	TERMP_CENTER	 (1 << 21)	/* Center output lines. */
104b306689cSschwarze #define	TERMP_RIGHT	 (1 << 22)	/* Adjust to the right margin. */
105e93ea447Sschwarze 	enum termtype	  type;		/* Terminal, PS, or PDF. */
106f73abda9Skristaps 	enum termenc	  enc;		/* Type of encoding. */
107fa70b73eSschwarze 	enum termfont	  fontl;	/* Last font set. */
108c48a0735Sschwarze 	enum termfont	 *fontq;	/* Symmetric fonts. */
109c48a0735Sschwarze 	int		  fontsz;	/* Allocated size of font stack */
110fa70b73eSschwarze 	int		  fonti;	/* Index of font stack. */
111f95d589eSschwarze 	term_margin	  headf;	/* invoked to print head */
112f95d589eSschwarze 	term_margin	  footf;	/* invoked to print foot */
113a5e11edeSschwarze 	void		(*letter)(struct termp *, int);
114f95d589eSschwarze 	void		(*begin)(struct termp *);
115f95d589eSschwarze 	void		(*end)(struct termp *);
116f95d589eSschwarze 	void		(*endline)(struct termp *);
117f95d589eSschwarze 	void		(*advance)(struct termp *, size_t);
11813a35416Sschwarze 	void		(*setwidth)(struct termp *, int, int);
119a5e11edeSschwarze 	size_t		(*width)(const struct termp *, int);
12013a35416Sschwarze 	int		(*hspan)(const struct termp *,
121769ee804Sschwarze 				const struct roffsu *);
122f95d589eSschwarze 	const void	 *argf;		/* arg for headf/footf */
12324f1eaadSschwarze 	const char	 *mc;		/* Margin character. */
124a5e11edeSschwarze 	struct termp_ps	 *ps;
125f73abda9Skristaps };
126f73abda9Skristaps 
127dd617d76Sschwarze 
12855f79d48Sschwarze const char	 *ascii_uc2str(int);
12955f79d48Sschwarze 
13096a5de47Sschwarze void		  roff_term_pre(struct termp *, const struct roff_node *);
13196a5de47Sschwarze 
132bf9acac6Sschwarze void		  term_eqn(struct termp *, const struct eqn_box *);
1332791bd1cSschwarze void		  term_tbl(struct termp *, const struct tbl_span *);
134f95d589eSschwarze void		  term_free(struct termp *);
1358afa4451Sschwarze void		  term_setcol(struct termp *, size_t);
136f73abda9Skristaps void		  term_newln(struct termp *);
137f73abda9Skristaps void		  term_vspace(struct termp *);
138f73abda9Skristaps void		  term_word(struct termp *, const char *);
139f73abda9Skristaps void		  term_flushln(struct termp *);
140f95d589eSschwarze void		  term_begin(struct termp *, term_margin,
1412a238f45Sschwarze 			term_margin, const struct roff_meta *);
142f95d589eSschwarze void		  term_end(struct termp *);
143f73abda9Skristaps 
14410111bf6Sschwarze void		  term_setwidth(struct termp *, const char *);
145bf8e53c9Sschwarze int		  term_hspan(const struct termp *, const struct roffsu *);
146f4692b45Sschwarze int		  term_hen(const struct termp *, const struct roffsu *);
147bf8e53c9Sschwarze int		  term_vspan(const struct termp *, const struct roffsu *);
1483ebeb861Sschwarze size_t		  term_strlen(const struct termp *, const char *);
1493ebeb861Sschwarze size_t		  term_len(const struct termp *, size_t);
1504175bdabSschwarze 
151f7242c43Sschwarze void		  term_tab_set(const struct termp *, const char *);
152c18aa4a9Sschwarze void		  term_tab_iset(size_t);
153*18bbf166Sschwarze void		  term_tab_ref(struct termp *);
154f7242c43Sschwarze size_t		  term_tab_next(size_t);
155888caeecSschwarze void		  term_tab_free(void);
156f7242c43Sschwarze 
157fa70b73eSschwarze void		  term_fontpush(struct termp *, enum termfont);
158fa70b73eSschwarze void		  term_fontpop(struct termp *);
159f29a1da1Sschwarze void		  term_fontpopq(struct termp *, int);
160fa70b73eSschwarze void		  term_fontrepl(struct termp *, enum termfont);
161fa70b73eSschwarze void		  term_fontlast(struct termp *);
162