CXX peace: src/troff/input.cpp
[s-roff.git] / include / font.h
blobbf4a0469d5e6dc4e4f2b89884e644f0536f1a6a5
1 /*
2 * Copyright (c) 2014 - 2017 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
4 * Copyright (C) 1989 - 1992, 2002, 2004, 2006
5 * Free Software Foundation, Inc.
6 * Written by James Clark (jjc@jclark.com)
8 * This is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2, or (at your option) any later
11 * version.
13 * This is distributed in the hope that it will be useful, but WITHOUT ANY
14 * WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with groff; see the file COPYING. If not, write to the Free Software
20 * Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA.
22 #ifndef _FONT_H
23 #define _FONT_H
25 #include "config.h"
27 #include "file_case.h"
29 // A function of this type can be registered to define the semantics of
30 // arbitrary commands in a font DESC file.
31 typedef void (*FONT_COMMAND_HANDLER)(const char *, // command
32 const char *, // arg
33 const char *, // file
34 int); // lineno
36 // A glyph is represented by a font-independent `glyph *' pointer.
37 // The functions name_to_glyph and number_to_glyph return such a pointer.
39 // There are two types of glyphs:
41 // - those with a name, and among these in particular:
42 // `charNNN' denoting a single `char' in the input character set,
43 // `uXXXX' denoting a Unicode character,
45 // - those with a number, referring to the the font-dependent glyph with
46 // the given number.
48 // The statically allocated information about a glyph.
50 // This is an abstract class; only its subclass `charinfo' is instantiated.
51 // `charinfo' exists in two versions: one in roff/troff/input.cpp for troff,
52 // and one in libs/libgroff/nametoindex.cpp for the preprocessors and the
53 // postprocessors.
54 struct glyph {
55 int index; // A font-independent integer value.
56 int number; // Glyph number or -1.
57 friend class character_indexer;
60 #define UNDEFINED_GLYPH ((glyph *) NULL)
62 // The next three functions exist in two versions: one in
63 // roff/troff/input.cpp for troff, and one in
64 // libs/libgroff/nametoindex.cpp for the preprocessors and the
65 // postprocessors.
66 extern glyph *name_to_glyph(const char *); // Convert the glyph with
67 // the given name (arg1) to a `glyph' object. This
68 // has the same semantics as the groff escape sequence
69 // \C'name'. If such a `glyph' object does not yet
70 // exist, a new one is allocated.
71 extern glyph *number_to_glyph(int); // Convert the font-dependent glyph
72 // with the given number (in the font) to a `glyph'
73 // object. This has the same semantics as the groff
74 // escape sequence \N'number'. If such a `glyph'
75 // object does not yet exist, a new one is allocated.
76 extern const char *glyph_to_name(glyph *); // Convert the given glyph
77 // back to its name. Return NULL if the glyph
78 // doesn't have a name.
79 inline int glyph_to_number(glyph *); // Convert the given glyph back to
80 // its number. Return -1 if it does not designate
81 // a numbered character.
82 inline int glyph_to_index(glyph *); // Return the unique index that is
83 // associated with the given glyph. It is >= 0.
85 inline int glyph_to_number(glyph *g)
87 return g->number;
90 inline int glyph_to_index(glyph *g)
92 return g->index;
95 // Types used in non-public members of `class font'.
96 struct font_kern_list;
97 struct font_char_metric;
98 struct font_widths_cache;
100 // A `class font' instance represents the relevant information of a font of
101 // the given device. This includes the set of glyphs represented by the
102 // font, and metrics for each glyph.
103 class font
105 public:
106 enum { // The valid argument values of `has_ligature'.
107 LIG_ff = 1,
108 LIG_fi = 2,
109 LIG_fl = 4,
110 LIG_ffi = 8,
111 LIG_ffl = 16
114 virtual ~font(); // Destructor.
115 int contains(glyph *); // Return 1 if this font contains the given
116 // glyph, 0 otherwise.
117 int is_special(); // Return 1 if this font is special, 0 otherwise.
118 // See section `Special Fonts' in the info file of
119 // groff. Used by make_glyph_node().
120 int get_width(glyph *, int); // A rectangle represents the shape of the
121 // given glyph (arg1) at the given point size
122 // (arg2). Return the horizontal dimension of this
123 // rectangle.
124 int get_height(glyph *, int); // A rectangle represents the shape of the
125 // given glyph (arg1) at the given point size
126 // (arg2). Return the distance between the base
127 // line and the top of this rectangle.
128 // This is often also called the `ascent' of the
129 // glyph. If the top is above the base line, this
130 // value is positive.
131 int get_depth(glyph *, int); // A rectangle represents the shape of the
132 // given glyph (arg1) at the given point size
133 // (arg2). Return the distance between the base
134 // line and the bottom of this rectangle.
135 // This is often also called the `descent' of the
136 // glyph. If the bottom is below the base line,
137 // this value is positive.
138 int get_space_width(int); // Return the normal width of a space at the
139 // given point size.
140 int get_character_type(glyph *); // Return a bit mask describing the
141 // shape of the given glyph. Bit 0 is set if the
142 // character has a descender. Bit 1 is set if the
143 // character has a tall glyph. See groff manual,
144 // description of \w and the `ct' register.
145 int get_kern(glyph *, glyph *, int); // Return the kerning between the
146 // given glyphs (arg1 and arg2), both at the given
147 // point size (arg3).
148 int get_skew(glyph *, int, int); // A rectangle represents the shape
149 // of the given glyph (arg1) at the given point size
150 // (arg2). For slanted fonts like Times-Italic, the
151 // optical vertical axis is naturally slanted. The
152 // natural slant value (measured in degrees;
153 // positive values mean aslant to the right) is
154 // specified in the font's description file (see
155 // member variable SLANT below). In addition to
156 // this, any font can be artificially slanted. This
157 // artificial slant value (arg3, measured in
158 // degrees; positive values mean a slant to the
159 // right) is specified with the \S escape.
161 // Return the skew value which is the horizontal
162 // distance between the upper left corner of the
163 // glyph box and the upper left corner of the glyph
164 // box thought to be slanted by the sum of the
165 // natural and artificial slant. It basically means
166 // how much an accent must be shifted horizontally
167 // to put it on the optical axis of the glyph.
168 int has_ligature(int); // Return a non-zero value if this font has
169 // the given ligature type (one of LIG_ff, LIG_fi,
170 // etc.), 0 otherwise.
171 int get_italic_correction(glyph *, int); // If the given glyph (arg1)
172 // at the given point size (arg2) is followed by an
173 // unslanted glyph, some horizontal white space may
174 // need to be inserted in between. See the groff
175 // manual, description of \/. Return the amount
176 // (width) of this white space.
177 int get_left_italic_correction(glyph *, int); // If the given glyph (arg1)
178 // at the given point size (arg2) is preceded by an
179 // unslanted roman glyph, some horizontal white
180 // space may need to be inserted in between. See
181 // the groff manual, description of \,. Return the
182 // amount (width) of this white space.
183 int get_subscript_correction(glyph *, int); // If the given glyph (arg1)
184 // at the given point size (arg2)is followed by a
185 // subscript glyph, the horizontal position may need
186 // to be advanced by some (possibly negative)
187 // amount. See groff manual, description of \w and
188 // the `ssc' register. Return this amount.
189 void set_zoom(int); // Set the font's zoom factor * 1000. Must be a
190 // non-negative value.
191 int get_zoom(); // Return the font's zoom factor * 1000.
192 int get_code(glyph *); // Return the code point in the physical
193 // font of the given glyph.
194 const char *get_special_device_encoding(glyph *); // Return special
195 // device dependent information about the given
196 // glyph. Return NULL if there is no special
197 // information.
198 const char *get_name(); // Return the name of this font.
199 const char *get_internal_name(); // Return the `internalname'
200 // attribute of this font. Return NULL if it has
201 // none.
202 const char *get_image_generator(); // Return the `image_generator'
203 // attribute of this font. Return NULL if it has
204 // none.
205 static int scan_papersize(const char *, const char **,
206 double *, double *); // Parse the
207 // `papersize' attribute in a DESC file (given in
208 // arg1). Return the name of the size (in arg2),
209 // and the length and width (in arg3 and arg4).
210 // Return 1 in case of success, 0 otherwise.
211 static font *load_font(const char *, int * = 0, int = 0); // Load the
212 // font description file with the given name (arg1)
213 // and return it as a `font' class. If arg2 points
214 // to an integer variable, set it to 1 if the file
215 // is not found, without emitting an error message.
216 // If arg2 is NULL, print an error message if the
217 // file is not found. If arg3 is nonzero, only the
218 // part of the font description file before the
219 // `charset' and `kernpairs' sections is loaded.
220 // Return NULL in case of failure.
221 static void command_line_font_dir(const char *); // Prepend given
222 // path (arg1) to the list of directories in which
223 // to look up fonts.
225 // Open a font file with the given name, searching along the current font
226 // path. Return the opened file or NULL.
227 static file_case * open_file(const char *name,
228 uint32_t flags=file_case::mux_default);
229 static int load_desc(); // Open the DESC file (depending on the
230 // device) and initialize some static variables with
231 // info from there.
232 static FONT_COMMAND_HANDLER
233 set_unknown_desc_command_handler(FONT_COMMAND_HANDLER); // Register
234 // a function which defines the semantics of
235 // arbitrary commands in the font DESC file.
236 // Now the variables from the DESC file, shared by all fonts.
237 static int res; // The `res' attribute given in the DESC file.
238 static int hor; // The `hor' attribute given in the DESC file.
239 static int vert; // The `vert' attribute given in the DESC file.
240 static int unitwidth; // The `unitwidth' attribute given in the DESC file.
241 static int paperwidth; // The `paperwidth' attribute given in the
242 // DESC file, or derived from the `papersize'
243 // attribute given in the DESC file.
244 static int paperlength; // The `paperlength' attribute given in the
245 // DESC file, or derived from the `papersize'
246 // attribute given in the DESC file.
247 static const char *papersize;
248 static int biggestfont; // The `biggestfont' attribute given in the
249 // DESC file.
250 static int spare2;
251 static int sizescale; // The `sizescale' attribute given in the DESC file.
252 static int tcommand; // Nonzero if the DESC file has the `tcommand'
253 // attribute.
254 static int unscaled_charwidths; // Nonzero if the DESC file has the
255 // `unscaled_charwidths' attribute.
256 static int pass_filenames; // Nonzero if the DESC file has the
257 // `pass_filenames' attribute.
258 static int use_charnames_in_special; // Nonzero if the DESC file has the
259 // `use_charnames_in_special' attribute.
260 static int is_unicode; // Nonzero if the DESC file has the `unicode'
261 // attribute.
262 static const char *image_generator; // The `image_generator' attribute
263 // given in the DESC file.
264 static const char **font_name_table; // The `fonts' attribute given in
265 // the DESC file, as a NULL-terminated array of
266 // strings.
267 static const char **style_table; // The `styles' attribute given in
268 // the DESC file, as a NULL-terminated array of
269 // strings.
270 static const char *family; // The `family' attribute given in the DESC
271 // file.
272 static int *sizes; // The `sizes' attribute given in the DESC file, as
273 // an array of intervals of the form { lower1,
274 // upper1, ... lowerN, upperN, 0 }.
276 private:
277 unsigned ligatures; // Bit mask of available ligatures. Used by
278 // has_ligature().
279 font_kern_list **kern_hash_table; // Hash table of kerning pairs.
280 // Used by get_kern().
281 int space_width; // The normal width of a space. Used by
282 // get_space_width().
283 int special; // 1 if this font is special, 0 otherwise. Used by
284 // is_special().
285 char *name; // The name of this font. Used by get_name().
286 char *internalname; // The `internalname' attribute of this font, or
287 // NULL. Used by get_internal_name().
288 double slant; // The natural slant angle (in degrees) of this font.
289 int zoom; // The font's magnification, multiplied by 1000.
290 // Used by scale(). A zero value means `no zoom'.
291 int *ch_index; // Conversion table from font-independent character
292 // indices to indices for this particular font.
293 int nindices;
294 font_char_metric *ch; // Metrics information for every character in this
295 // font (if !is_unicode) or for just some characters
296 // (if is_unicode). The indices of this array are
297 // font-specific, found as values in ch_index[].
298 int ch_used;
299 int ch_size;
300 font_widths_cache *widths_cache; // A cache of scaled character
301 // widths. Used by the get_width() function.
303 static FONT_COMMAND_HANDLER unknown_desc_command_handler; // A
304 // function defining the semantics of arbitrary
305 // commands in the DESC file.
306 enum { KERN_HASH_TABLE_SIZE = 503 }; // Size of the hash table of kerning
307 // pairs.
309 // These methods add new characters to the ch_index[] and ch[] arrays.
310 void add_entry(glyph *, // glyph
311 const font_char_metric &); // metric
312 void copy_entry(glyph *, // new_glyph
313 glyph *); // old_glyph
314 void alloc_ch_index(int); // index
315 void extend_ch();
316 void compact();
318 void add_kern(glyph *, glyph *, int); // Add to the kerning table a
319 // kerning amount (arg3) between two given glyphs
320 // (arg1 and arg2).
321 static int hash_kern(glyph *, glyph *); // Return a hash code for
322 // the pair of glyphs (arg1 and arg2).
324 /* Returns w * pointsize / unitwidth, rounded to the nearest integer. */
325 int scale(int w, int pointsize);
326 static int unit_scale(double *, char); // Convert value in arg1 from the
327 // given unit (arg2; possible values are `i', `c',
328 // `p', and `P' as documented in the info file of
329 // groff, section `Measurements') to inches. Store
330 // the result in arg1 and return 1. If the unit is
331 // invalid, return 0.
332 virtual void handle_unknown_font_command(const char *, // command
333 const char *, // arg
334 const char *, // file
335 int); // lineno
337 protected:
338 font(const char *); // Initialize a font with the given name.
340 int load(int * = 0, int = 0); // Load the font description file with the
341 // given name (in member variable NAME) into this
342 // object. If arg1 points to an integer variable,
343 // set it to 1 if the file is not found, without
344 // emitting an error message. If arg1 is NULL,
345 // print an error message if the file is not found.
346 // If arg2 is nonzero, only the part of the font
347 // description file before the `charset' and
348 // `kernpairs' sections is loaded. Return NULL in
349 // case of failure.
352 #endif // _FONT_H
353 // s-it2-mode