1 /* pbmfont.h - header file for font routines in libpbm
2 */
3 
4 #include "pbm.h"
5 
6 #ifdef __cplusplus
7 extern "C" {
8 #endif
9 #if 0
10 } /* to fake out automatic code indenters */
11 #endif
12 
13 
14 /* Maximum dimensions for fonts */
15 
16 #define  pbm_maxfontwidth()  65535
17 #define  pbm_maxfontheight() 65535
18     /* These limits are not in the official Adobe BDF definition, but
19        should never be a problem for practical purposes, considering that
20        a 65536 x 65536 glyph occupies 4G pixels.
21 
22        Note that the maximum line length allowed in a BDF file imposes
23        another restriction.
24     */
25 
26 typedef wchar_t PM_WCHAR;
27     /* Precaution to make adjustments, if necessary, for systems with
28        unique wchar_t.
29     */
30 
31 #define PM_FONT_MAXGLYPH 255
32 
33 #define PM_FONT2_MAXGLYPH 65535
34     /* Upper limit of codepoint value.
35 
36        This is large enough to handle Unicode Plane 0 (Basic Multilingual
37        Plane: BMP) which defines the great majority of characters used in
38        modern languages.
39 
40        This can be set to a higher value at some cost to efficiency.
41        As of Unicode v. 11.0.0 planes up to 16 are defined.
42     */
43 
44 enum pbmFontLoad { FIXED_DATA           = 0,
45                    LOAD_PBMSHEET        = 1,
46                    LOAD_BDFFILE         = 2,
47                    CONVERTED_TYPE1_FONT = 9 };
48 
49 static const char * const pbmFontOrigin[10] =
50                  {"Fixed data",                                   /* 0 */
51                   "Loaded from PBM sheet by libnetpbm",           /* 1 */
52                   "Loaded from BDF file by libnetpbm",            /* 2 */
53                   NULL, NULL, NULL, NULL, NULL, NULL,
54                   "Expanded from type 1 font structure by libnetpbm"}; /* 9 */
55 
56 enum pbmFontEncoding { ENCODING_UNKNOWN = 0,
57                        ISO646_1991_IRV = 1,   /* ASCII */
58                        ISO_8859_1 = 1000, ISO_8859_2, ISO_8859_3, ISO_8859_4,
59                        ISO_8859_5,   ISO_8859_6,   ISO_8859_7,   ISO_8859_8,
60                        ISO_8859_9,   ISO_8859_10,  ISO_8859_11,  ISO_8859_12,
61                        ISO_8859_13,  ISO_8859_14,  ISO_8859_15,  ISO_8859_16,
62                        ISO_10646 = 2000 };
63 
64 /* For future use */
65 
66 /* In addition to the above, the following CHARSET_REGISTRY-CHARSET_ENCODING
67    values have been observed in actual BDF files:
68 
69   ADOBE-FONTSPECIFIC, DEC-DECTECH, GOST19768.74-1, IS13194-DEVANAGARI,
70   JISX0201.1976-0, KOI8-C, KOI8-R, MISC-FONTSPECIFIC,
71   MULEARABIC-0, MULEARABIC-1, MULEARABIC-2, MULEIPA-1, MULELAO-1,
72   OMRON_UDC_ZH-0, TIS620.2529-0, TIS620.2529-1, VISCII1-1, VISCII1.1-1,
73   XTIS-0
74  */
75 
76 
77 struct glyph {
78     /* A glyph consists of white borders and the "central glyph" which
79        can be anything, but normally does not have white borders because
80        it's more efficient to represent white borders explicitly.
81     */
82     unsigned int width;
83     unsigned int height;
84         /* The dimensions of the central glyph, i.e. the 'bmap' array */
85     int x;
86         /* Width in pixels of the white left border of this glyph.
87            This can actually be negative to indicate that the central
88            glyph backs up over the previous character in the line.  In
89            that case, if there is no previous character in the line, it
90            is as if 'x' is 0.
91         */
92     int y;
93         /* Height in pixels of the white bottom border of this glyph.
94            Can be negative.
95         */
96     unsigned int xadd;
97         /* Width of glyph -- white left border plus central glyph
98            plus white right border
99         */
100     const char * bmap;
101         /* The raster of the central glyph.  It is an 'width' by
102            'height' array in row major order, with each byte being 1
103            for black; 0 for white.  E.g. if 'width' is 20 pixels and
104            'height' is 40 pixels and it's a rectangle that is black on
105            the top half and white on the bottom, this is an array of
106            800 bytes, with the first 400 having value 0x01 and the
107            last 400 having value 0x00.
108 
109            Do not share bmap objects among glyphs if using
110            pbm_destroybdffont() or pbm_destroybdffont2() to free
111            the font/font2 structure.
112         */
113 };
114 
115 
116 struct font {
117     /* This describes a combination of font and character set.  Given
118        a code point in the range 0..255, this structure describes the
119        glyph for that character.
120     */
121     unsigned int maxwidth, maxheight;
122     int x;
123         /* The minimum value of glyph.x .  The left edge of the glyph
124            in the glyph set which advances furthest to the left. */
125     int y;
126         /* Amount of white space that should be added between lines of
127            this font.  Can be negative.
128         */
129     struct glyph * glyph[256];
130         /* glyph[i] is the glyph for code point i.
131            Glyph objects must be unique for pbm_destroybdffont() to work.
132         */
133     const bit ** oldfont;
134         /* for compatibility with old pbmtext routines */
135         /* oldfont is NULL if the font is BDF derived */
136     int fcols, frows;
137 };
138 
139 
140 
141 struct pm_selector {
142     unsigned int min;     /* smallest index requested */
143     unsigned int max;     /* largest index requested  */
144     unsigned int maxmax;  /* largest index possible  */
145     unsigned int count;   /* number bits set to 1 in 'record' */
146     const unsigned char * record;
147         /* Bit array 0: absent 1: existent size calculated from maxmax
148            This is for reading only; not updating.
149            Might be 'localRecord'; might be fixed object elsewhere.
150         */
151     unsigned char * localRecord;
152         /* Updateable record optionally pointed to by 'record'.
153            Null if none.  Malloc'ed storage we own otherwise.
154         */
155 };
156 
157 
158 
159 struct font2 {
160     /* Font structure for expanded character set.
161        Code points in the range 0...maxmaxglyph may be loaded.
162        Actually loaded code point is in the range 0..maxglyph .
163      */
164 
165     /* 'size' and 'len' are necessary in order to provide forward and
166        backward compatibility between library functions and calling programs
167        as this structure grows.  See struct pam in pam.h.
168      */
169     unsigned int size;
170         /* The storage size of this entire structure, in bytes */
171 
172     unsigned int len;
173         /* The length, in bytes, of the information in this structure.
174            The information starts in the first byte and is contiguous.
175            This cannot be greater than 'size'
176         */
177 
178     int maxwidth, maxheight;
179 
180     int x;
181          /* The minimum value of glyph.x .  The left edge of the glyph in
182             the glyph set which advances furthest to the left.
183          */
184     int y;
185         /* Amount of white space that should be added between lines of this
186            font.  Can be negative.
187         */
188 
189     struct glyph ** glyph;
190         /* glyph[i] is the glyph for code point i
191 
192            Glyph objects must be unique for pbm_destroybdffont2() to work.
193            For example space and non-break-space are often identical at the
194            image data level; they must be loaded into separate memory
195            locations if using pbm_destroybdffont2().
196          */
197 
198     PM_WCHAR maxglyph;
199         /* max code point for glyphs, including vacant slots
200            max value of above i
201         */
202 
203     struct pm_selector * selectorP;
204         /* Structure with bit array indicating which code points to load.
205 
206            When NULL, all available code points up to maxmaxglyph, inclusive
207            are loaded.
208        */
209 
210     PM_WCHAR maxmaxglyph;
211         /* Code points above this value are not loaded, even if they occur
212            in the BDF font file
213         */
214 
215     const bit ** oldfont;
216         /* For compatibility with old pbmtext routines.
217            Valid only when data is in the form of a PBM sheet
218         */
219 
220     unsigned int fcols, frows;
221         /* For compatibility with old pbmtext routines.
222            Valid only when oldfont is non-NULL
223         */
224 
225     unsigned int bit_format;
226         /* PBM_FORMAT:  glyph data: 1 byte per pixel (like P1, but not ASCII)
227            RPBM_FORMAT: glyph data: 1 bit per pixel
228            Currently only PBM_FORMAT is possible
229         */
230 
231     unsigned int total_chars;
232         /* Number of glyphs defined in font file.
233            BDF file: as stated in the CHARS line.
234            PBM sheet font: always 96.
235         */
236 
237     unsigned int chars;
238         /* Number of glyphs actually loaded into structure
239 
240            Maximum: total_chars
241 
242            Less than total_chars when a subset of the file is loaded
243            PBM sheet font: always 96 */
244 
245     enum pbmFontLoad load_fn;
246         /* Description of the function that created the structure and loaded
247            the glyph data
248 
249            Used to choose a string to show in verbose messages.
250 
251            FIXED_DATA (==0) means memory for this structure was not
252            dynamically allocated by a function; all data is hardcoded in
253            source code and resides in static data.  See file pbmfontdata1.c
254         */
255 
256     PM_WCHAR default_char;
257         /* Code index of what to show when there is no glyph for a requested
258            code.  Available in many BDF fonts between STARPROPERTIES -
259            ENDPROPERTIES.
260 
261            Set to value read from BDF font file.
262 
263            Common values are 0, 32, 8481, 32382, 33, 159, 255.
264         */
265 
266     unsigned int default_char_defined;
267         /* boolean
268            TRUE: above field is valid; DEFAULT_CHAR is defined in font file.
269            FALSE: font file has no DEFAULT_CHAR field.
270         */
271 
272     const char * name;
273         /* Name of the font.  Available in BDF fonts.
274            NULL means no name.
275         */
276 
277     enum pbmFontEncoding charset;
278         /* Reserved for future use.
279            Set by analyzing following charset_string.
280         */
281 
282     const char * charset_string;
283         /* Charset registry and encoding.
284            Available in most BDF fonts between STARPROPERTIES - ENDPROPERTIES.
285            NULL means no name.
286         */
287 };
288 
289 
290 /* PBM_FONT2_STRUCT_SIZE(x) tells you how big a struct font2 is up
291    through the member named x.  This is useful in conjunction with the
292    'len' value to determine which fields are present in the structure.
293 */
294 
295 /* Some compilers are really vigilant and recognize it as an error
296    to cast a 64 bit address to a 32 bit type.  Hence the roundabout
297    casting.  See PAM_MEMBER_OFFSET in pam.h .
298 */
299 
300 
301 #define PBM_FONT2_MEMBER_OFFSET(mbrname) \
302   ((size_t)(unsigned long)(char*)&((struct font2 *)0)->mbrname)
303 #define PBM_FONT2_MEMBER_SIZE(mbrname) \
304   sizeof(((struct font2 *)0)->mbrname)
305 #define PBM_FONT2_STRUCT_SIZE(mbrname) \
306   (PBM_FONT2_MEMBER_OFFSET(mbrname) + PBM_FONT2_MEMBER_SIZE(mbrname))
307 
308 
309 struct font *
310 pbm_defaultfont(const char* const which);
311 
312 struct font2 *
313 pbm_defaultfont2(const char* const which);
314 
315 struct font *
316 pbm_dissectfont(const bit ** const font,
317                 unsigned int const frows,
318                 unsigned int const fcols);
319 
320 struct font *
321 pbm_loadfont(const char * const filename);
322 
323 struct font2 *
324 pbm_loadfont2(const    char * const filename,
325               PM_WCHAR        const maxmaxglyph);
326 
327 struct font2 *
328 pbm_loadfont2select(const    char *            const filename,
329                     PM_WCHAR                   const maxmaxglyph,
330                     const struct pm_selector * const selectorP);
331 
332 struct font *
333 pbm_loadpbmfont(const char * const filename);
334 
335 struct font2 *
336 pbm_loadpbmfont2(const char * const filename);
337 
338 struct font *
339 pbm_loadbdffont(const char * const filename);
340 
341 struct font2 *
342 pbm_loadbdffont2(const char * const filename,
343                  PM_WCHAR     const maxmaxglyph);
344 
345 struct font2 *
346 pbm_loadbdffont2select(const char *               const filename,
347                        PM_WCHAR                   const maxmaxglyph,
348                        const struct pm_selector * const selectorP);
349 
350 void
351 pbm_createbdffont2_base(struct font2 ** const font2P,
352                         PM_WCHAR        const maxmaxglyph);
353 
354 void
355 pbm_destroybdffont(struct font * const fontP);
356 
357 void
358 pbm_destroybdffont2_base(struct font2 * const font2P);
359 
360 void
361 pbm_destroybdffont2(struct font2 * const font2P);
362 
363 struct font2 *
364 pbm_expandbdffont(const struct font * const font);
365 
366 void
367 pbm_dumpfont(struct font * const fontP,
368              FILE *        const ofP);
369 
370 /* selector functions */
371 
372 void
373 pm_selector_create(unsigned int          const max,
374                    struct pm_selector ** const selectorPP);
375 
376 void
377 pm_selector_create_fixed(const unsigned char * const record,
378                          unsigned int          const min,
379                          unsigned int          const max,
380                          unsigned int          const count,
381                          struct pm_selector ** const selectorPP);
382 
383 void
384 pm_selector_destroy(struct pm_selector * const selectorP);
385 
386 void
387 pm_selector_copy(unsigned int               const max,
388                  const struct pm_selector * const srcSelectorP,
389                  struct pm_selector **      const destSelectorPP);
390 
391 void
392 pm_selector_mark(struct pm_selector * const selectorP,
393                  unsigned int         const index);
394 
395 int  /* boolean */
396 pm_selector_is_marked(const struct pm_selector * const selectorP,
397                       unsigned int               const index);
398 
399 unsigned int
400 pm_selector_marked_ct(const struct pm_selector * const selectorP);
401 
402 #ifdef __cplusplus
403 }
404 #endif
405