xref: /freebsd/contrib/mandoc/term.h (revision 06410c1b51637e5e1f392d553b5008948af58014)
1 /* $Id: term.h,v 1.138 2025/07/27 15:27:28 schwarze Exp $ */
2 /*
3  * Copyright (c) 2011-2015, 2017, 2019, 2021, 2022, 2025
4  *               Ingo Schwarze <schwarze@openbsd.org>
5  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 enum	termenc {
21 	TERMENC_ASCII,
22 	TERMENC_LOCALE,
23 	TERMENC_UTF8
24 };
25 
26 enum	termtype {
27 	TERMTYPE_CHAR,
28 	TERMTYPE_PS,
29 	TERMTYPE_PDF
30 };
31 
32 enum	termfont {
33 	TERMFONT_NONE = 0,
34 	TERMFONT_BOLD,
35 	TERMFONT_UNDER,
36 	TERMFONT_BI,
37 	TERMFONT__MAX
38 };
39 
40 struct	eqn_box;
41 struct	roff_meta;
42 struct	roff_node;
43 struct	tbl_span;
44 struct	termp;
45 
46 typedef void	(*term_margin)(struct termp *, const struct roff_meta *);
47 
48 struct	termp_col {
49 	int		 *buf;		/* Output buffer. */
50 	size_t		  maxcols;	/* Allocated bytes in buf. */
51 	size_t		  lastcol;	/* Last byte in buf. */
52 	size_t		  col;		/* Byte in buf to be written. */
53 	size_t		  rmargin;	/* Current right margin [BU]. */
54 	size_t		  offset;	/* Current left margin [BU]. */
55 	size_t		  taboff;	/* Offset for literal tabs [BU]. */
56 };
57 
58 struct	termp {
59 	struct rofftbl	  tbl;		/* Table configuration. */
60 	struct termp_col *tcols;	/* Array of table columns. */
61 	struct termp_col *tcol;		/* Current table column. */
62 	size_t		  maxtcol;	/* Allocated table columns. */
63 	size_t		  lasttcol;	/* Last column currently used. */
64 	size_t		  line;		/* Current output line number. */
65 	size_t		  defindent;	/* Default indent for text [EN]. */
66 	size_t		  defrmargin;	/* Right margin of the device [BU]. */
67 	size_t		  lastrmargin;	/* Right margin before last ll [BU]. */
68 	size_t		  maxrmargin;	/* Maximum right margin [BU]. */
69 	size_t		  col;		/* Byte position in buf. */
70 	size_t		  viscol;	/* Width of the current line [BU]. */
71 	size_t		  trailspace;	/* Whitespace after field [EN]. */
72 	size_t		  minbl;	/* Whitespace before field [EN]. */
73 	int		  synopsisonly; /* Print the synopsis only. */
74 	int		  ti;		/* Temporary indent for line [BU]. */
75 	int		  skipvsp;	/* Vertical space to skip. */
76 	int		  flags;
77 #define	TERMP_SENTENCE	 (1 << 0)	/* Space before a sentence. */
78 #define	TERMP_NOSPACE	 (1 << 1)	/* No space before words. */
79 #define	TERMP_NONOSPACE	 (1 << 2)	/* No space (no autounset). */
80 #define	TERMP_NBRWORD	 (1 << 3)	/* Make next word nonbreaking. */
81 #define	TERMP_KEEP	 (1 << 4)	/* Keep words together. */
82 #define	TERMP_PREKEEP	 (1 << 5)	/* ...starting with the next one. */
83 #define	TERMP_BACKAFTER	 (1 << 6)	/* Back up after next character. */
84 #define	TERMP_BACKBEFORE (1 << 7)	/* Back up before next character. */
85 #define	TERMP_NOBREAK	 (1 << 8)	/* See term_flushln(). */
86 #define	TERMP_BRTRSP	 (1 << 9)	/* See term_flushln(). */
87 #define	TERMP_BRIND	 (1 << 10)	/* See term_flushln(). */
88 #define	TERMP_HANG	 (1 << 11)	/* See term_flushln(). */
89 #define	TERMP_NOPAD	 (1 << 12)	/* See term_flushln(). */
90 #define	TERMP_NOSPLIT	 (1 << 13)	/* Do not break line before .An. */
91 #define	TERMP_SPLIT	 (1 << 14)	/* Break line before .An. */
92 #define	TERMP_NONEWLINE	 (1 << 15)	/* No line break in nofill mode. */
93 #define	TERMP_BRNEVER	 (1 << 16)	/* Don't even break at maxrmargin. */
94 #define	TERMP_NOBUF	 (1 << 17)	/* Bypass output buffer. */
95 #define	TERMP_NEWMC	 (1 << 18)	/* No .mc printed yet. */
96 #define	TERMP_ENDMC	 (1 << 19)	/* Next break ends .mc mode. */
97 #define	TERMP_MULTICOL	 (1 << 20)	/* Multiple column mode. */
98 #define	TERMP_CENTER	 (1 << 21)	/* Center output lines. */
99 #define	TERMP_RIGHT	 (1 << 22)	/* Adjust to the right margin. */
100 	enum termtype	  type;		/* Terminal, PS, or PDF. */
101 	enum termenc	  enc;		/* Type of encoding. */
102 	enum termfont	  fontl;	/* Last font set. */
103 	enum termfont	 *fontq;	/* Symmetric fonts. */
104 	int		  fontsz;	/* Allocated size of font stack */
105 	int		  fonti;	/* Index of font stack. */
106 	int		  fontibi;	/* Map font I to BI. */
107 	term_margin	  headf;	/* invoked to print head */
108 	term_margin	  footf;	/* invoked to print foot */
109 	void		(*letter)(struct termp *, int);
110 	void		(*begin)(struct termp *);
111 	void		(*end)(struct termp *);
112 	void		(*endline)(struct termp *);
113 	void		(*advance)(struct termp *, size_t);
114 	void		(*setwidth)(struct termp *, int, int);
115 	size_t		(*getwidth)(const struct termp *, int);
116 	int		(*hspan)(const struct termp *,
117 				const struct roffsu *);
118 	const void	 *argf;		/* arg for headf/footf */
119 	const char	 *mc;		/* Margin character. */
120 	struct termp_ps	 *ps;
121 };
122 
123 
124 const char	 *ascii_uc2str(int);
125 
126 void		  roff_term_pre(struct termp *, const struct roff_node *);
127 
128 void		  term_eqn(struct termp *, const struct eqn_box *);
129 void		  term_tbl(struct termp *, const struct tbl_span *);
130 void		  term_free(struct termp *);
131 void		  term_setcol(struct termp *, size_t);
132 void		  term_newln(struct termp *);
133 void		  term_vspace(struct termp *);
134 void		  term_word(struct termp *, const char *);
135 void		  term_flushln(struct termp *);
136 void		  term_begin(struct termp *, term_margin,
137 			term_margin, const struct roff_meta *);
138 void		  term_end(struct termp *);
139 
140 void		  term_setwidth(struct termp *, const char *);
141 int		  term_hspan(const struct termp *, const struct roffsu *);
142 int		  term_vspan(const struct termp *, const struct roffsu *);
143 size_t		  term_strlen(const struct termp *, const char *);
144 size_t		  term_len(const struct termp *, size_t);
145 
146 void		  term_tab_set(const struct termp *, const char *);
147 void		  term_tab_ref(struct termp *);
148 size_t		  term_tab_next(size_t);
149 void		  term_tab_free(void);
150 
151 void		  term_fontpush(struct termp *, enum termfont);
152 void		  term_fontpop(struct termp *);
153 void		  term_fontpopq(struct termp *, int);
154 void		  term_fontrepl(struct termp *, enum termfont);
155 void		  term_fontlast(struct termp *);
156