2 * Copyright 2008 Department of Mathematical Sciences, New Mexico State University
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * DEPARTMENT OF MATHEMATICAL SCIENCES OR NEW MEXICO STATE UNIVERSITY BE
18 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
19 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 #include <X11/Xutil.h>
35 #include <X11/Xatom.h>
36 #endif /* HAVE_XLIB */
40 #include FT_FREETYPE_H
41 #endif /* HAVE_FREETYPE */
47 /**************************************************************************
49 * BDF font options macros and types.
51 **************************************************************************/
53 #define BDF_UNIX_EOL 1 /* Save fonts with Unix LF. */
54 #define BDF_DOS_EOL 2 /* Save fonts with DOS CRLF. */
55 #define BDF_MAC_EOL 3 /* Save fonts with Mac CR. */
57 #define BDF_CORRECT_METRICS 0x01 /* Correct invalid metrics when loading. */
58 #define BDF_KEEP_COMMENTS 0x02 /* Preserve the font comments. */
59 #define BDF_KEEP_UNENCODED 0x04 /* Keep the unencoded glyphs. */
60 #define BDF_PROPORTIONAL 0x08 /* Font has proportional spacing. */
61 #define BDF_MONOWIDTH 0x10 /* Font has mono width. */
62 #define BDF_CHARCELL 0x20 /* Font has charcell spacing. */
64 #define BDF_ALL_SPACING (BDF_PROPORTIONAL|BDF_MONOWIDTH|BDF_CHARCELL)
66 #define BDF_DEFAULT_LOAD_OPTIONS \
67 (BDF_CORRECT_METRICS|BDF_KEEP_COMMENTS|BDF_KEEP_UNENCODED|BDF_PROPORTIONAL)
77 unsigned int resolution_x;
78 unsigned int resolution_y;
86 * Callback function type for unknown configuration options.
88 typedef int (*bdf_options_callback_t)(bdf_options_t *opts,
93 /**************************************************************************
95 * BDF font property macros and types.
97 **************************************************************************/
100 #define BDF_INTEGER 2
101 #define BDF_CARDINAL 3
104 * This structure represents a particular property of a font.
105 * There are a set of defaults and each font has their own.
108 char *name; /* Name of the property. */
109 int format; /* Format of the property. */
110 int builtin; /* A builtin property. */
115 } value; /* Value of the property. */
118 /**************************************************************************
120 * SBIT metrics specific structures.
122 **************************************************************************/
125 * Boolean flags for SBIT metrics files.
127 #define BDF_SBIT_MONO_ADVANCE 0x0001
128 #define BDF_SBIT_ADD_EBLC 0x0002
129 #define BDF_SBIT_APPLE_COMPAT 0x0004
132 * Direction macros (inclusive, can be combined).
134 #define BDF_SBIT_HORIZONTAL 0x0008
135 #define BDF_SBIT_VERTICAL 0x0010
138 * Bitmap storage options (exclusive, cannot be combined).
140 #define BDF_SBIT_STORE_SMALL 0x0020
141 #define BDF_SBIT_STORE_FAST 0x0040
144 short cnum; /* Caret slope numerator. */
145 short cdenom; /* Caret slope denominator. */
146 short coff; /* Caret offset. */
147 short sx; /* Scaled version horizontal PPEM size. */
148 short sy; /* Scaled version vertical PPEM size (optional).*/
149 short flags; /* Booleans and other non-numeric values. */
152 /**************************************************************************
154 * BDF opaque undo information types.
156 **************************************************************************/
158 typedef struct _bdf_undo_t *bdf_undo_t;
160 /**************************************************************************
162 * PSF font flags and Unicode mapping tables. Stored internally in UTF-8.
164 **************************************************************************/
167 * Flags used for exporting PSF fonts and their Unicode maps.
169 #define BDF_PSF_FONT 0x01
170 #define BDF_PSF_UNIMAP 0x02
171 #define BDF_PSF_ALL (BDF_PSF_FONT|BDF_PSF_UNIMAP)
175 unsigned int map_used;
176 unsigned int map_size;
179 /**************************************************************************
181 * BDF font metric and glyph types.
183 **************************************************************************/
186 * A general bitmap type, mostly used when the glyph bitmap is being edited.
191 unsigned short width;
192 unsigned short height;
195 unsigned char *bitmap;
201 unsigned short swidth;
202 unsigned short dwidth;
203 unsigned short width;
204 unsigned short height;
212 unsigned short width;
213 unsigned short height;
221 char *name; /* Glyph name. */
222 int encoding; /* Glyph encoding. */
223 unsigned short swidth; /* Scalable width. */
224 unsigned short dwidth; /* Device width. */
225 bdf_bbx_t bbx; /* Glyph bounding box. */
226 unsigned char *bitmap; /* Glyph bitmap. */
227 unsigned short bytes; /* Number of bytes used for the bitmap. */
228 bdf_psf_unimap_t unicode; /* PSF Unicode mappings. */
232 unsigned short pad; /* Pad to 4-byte boundary. */
233 unsigned short bpp; /* Bits per pixel. */
234 int start; /* Beginning encoding value of glyphs. */
235 int end; /* Ending encoding value of glyphs. */
236 bdf_glyph_t *glyphs; /* Glyphs themselves. */
237 unsigned int glyphs_size; /* Glyph structures allocated. */
238 unsigned int glyphs_used; /* Glyph structures used. */
239 bdf_bbx_t bbx; /* Overall bounding box of glyphs. */
243 char *name; /* Name of the font. */
244 bdf_bbx_t bbx; /* Font bounding box. */
246 int point_size; /* Point size of the font. */
247 unsigned int resolution_x; /* Font horizontal resolution. */
248 unsigned int resolution_y; /* Font vertical resolution. */
250 int hbf; /* Font came from an HBF font. */
252 int spacing; /* Font spacing value. */
254 unsigned short monowidth; /* Logical width for monowidth font. */
256 int default_glyph; /* Encoding of the default glyph. */
258 int font_ascent; /* Font ascent. */
259 int font_descent; /* Font descent. */
261 int glyphs_size; /* Glyph structures allocated. */
262 int glyphs_used; /* Glyph structures used. */
263 bdf_glyph_t *glyphs; /* Glyphs themselves. */
265 int unencoded_size; /* Unencoded glyph structures allocated. */
266 int unencoded_used; /* Unencoded glyph structures used. */
267 bdf_glyph_t *unencoded; /* Unencoded glyphs themselves. */
269 unsigned int props_size; /* Font properties allocated. */
270 unsigned int props_used; /* Font properties used. */
271 bdf_property_t *props; /* Font properties themselves. */
273 char *comments; /* Font comments. */
274 unsigned int comments_len; /* Length of comment string. */
276 char *acmsgs; /* Auto-correction messages. */
277 unsigned int acmsgs_len; /* Length of auto-correction messages. */
279 bdf_glyphlist_t overflow; /* Storage used for glyph insertion. */
281 void *internal; /* Internal data for the font. */
283 unsigned int nmod[2048]; /* Bitmap indicating modified glyphs. */
284 unsigned int umod[2048]; /* Bitmap indicating modified unencoded. */
286 unsigned short modified; /* Boolean indicating font modified. */
287 unsigned short bpp; /* Bits per pixel. */
289 bdf_sbit_t *sbits; /* Associcated SBIT metrics. */
290 unsigned int sbits_used; /* Number of SBIT metrics entries. */
291 unsigned int sbits_size; /* Amount of entries allocated. */
293 bdf_undo_t *undo_stack; /* Record of undoable operations. */
294 unsigned int undo_used; /* Amount of undo stack used. */
295 unsigned int undo_size; /* Amount of undo stack allocated. */
297 bdf_psf_unimap_t unicode; /* PSF Unicode table. */
300 /**************************************************************************
302 * BDF glyph grid structures for editing glyph bitmaps.
304 **************************************************************************/
308 int encoding; /* The glyph encoding. */
309 unsigned short unencoded; /* Whether the glyph was unencoded. */
310 unsigned short bpp; /* Bits per pixel. */
311 int spacing; /* Font spacing. */
312 int resolution_x; /* Horizontal resolution. */
313 int resolution_y; /* Vertical resolution. */
314 unsigned int point_size; /* Font point size. */
315 unsigned short swidth; /* Scalable width. */
316 unsigned short dwidth; /* Device width. */
317 bdf_bbx_t font_bbx; /* Font bounding box. */
318 bdf_bbx_t glyph_bbx; /* Glyph bounding box. */
319 unsigned char *bitmap; /* The grid bitmap. */
320 unsigned short bytes; /* Number of bytes in the grid bitmap. */
321 short grid_width; /* Width of the grid. */
322 short grid_height; /* Height of the grid. */
323 short base_x; /* Baseline X coordinate. */
324 short base_y; /* Baseline Y coordinate. */
325 short glyph_x; /* Top-left X position of glyph. */
326 short glyph_y; /* Top-left Y position of glyph. */
327 unsigned short modified; /* Flag indicating if bitmap modified. */
328 short cap_height; /* Font CAP_HEIGHT if it exists. */
329 short x_height; /* Font X_HEIGHT if it exists. */
330 bdf_bitmap_t sel; /* Selected portion of the glyph bitmap.*/
331 bdf_psf_unimap_t unicode; /* PSF Unicode mappings for this glyph. */
334 /**************************************************************************
336 * Types for load/save callbacks.
338 **************************************************************************/
343 #define BDF_LOAD_START 1
344 #define BDF_LOADING 2
345 #define BDF_SAVE_START 3
347 #define BDF_TRANSLATE_START 5
348 #define BDF_TRANSLATING 6
349 #define BDF_ROTATE_START 7
350 #define BDF_ROTATING 8
351 #define BDF_SHEAR_START 9
352 #define BDF_SHEARING 10
353 #define BDF_GLYPH_NAME_START 11
354 #define BDF_GLYPH_NAME 12
355 #define BDF_EXPORT_START 13
356 #define BDF_EXPORTING 14
357 #define BDF_EMBOLDEN_START 15
358 #define BDF_EMBOLDENING 16
359 #define BDF_WARNING 20
366 #define BDF_MISSING_START -1
367 #define BDF_MISSING_FONTNAME -2
368 #define BDF_MISSING_SIZE -3
369 #define BDF_MISSING_FONTBBX -4
370 #define BDF_MISSING_CHARS -5
371 #define BDF_MISSING_STARTCHAR -6
372 #define BDF_MISSING_ENCODING -7
373 #define BDF_MISSING_BBX -8
375 #define BDF_NOT_CONSOLE_FONT -10
376 #define BDF_NOT_MF_FONT -11
377 #define BDF_NOT_PSF_FONT -12
378 #define BDF_PSF_SHORT_TABLE -13
379 #define BDF_PSF_LONG_TABLE -14
380 #define BDF_PSF_CORRUPT_UTF8 -15
381 #define BDF_PSF_BUFFER_OVRFL -16
382 #define BDF_PSF_UNSUPPORTED -17
383 #define BDF_BAD_RANGE -98
384 #define BDF_EMPTY_FONT -99
385 #define BDF_INVALID_LINE -100
389 unsigned int current;
391 unsigned int errlineno;
392 } bdf_callback_struct_t;
394 typedef void (*bdf_callback_t)(bdf_callback_struct_t *call_data,
397 /**************************************************************************
401 **************************************************************************/
404 * Startup and shutdown functions.
406 extern void bdf_setup(void);
407 extern void bdf_cleanup(void);
410 * Configuration file loading and saving.
412 extern void bdf_load_options(FILE *in, bdf_options_t *opts,
413 bdf_options_callback_t callback,
415 extern void bdf_save_options(FILE *out, bdf_options_t *opts);
418 * Font options functions.
420 extern void bdf_default_options(bdf_options_t *opts);
423 * Font load, create, save and free functions.
425 extern bdf_font_t *bdf_new_font(char *name, int point_size,
426 int resolution_x, int resolution_y,
427 int spacing, int bpp);
428 extern bdf_font_t *bdf_load_font(FILE *in, bdf_options_t *opts,
429 bdf_callback_t callback, void *data);
431 extern bdf_font_t *bdf_load_hbf_font(char *filename, bdf_options_t *opts,
432 bdf_callback_t callback, void *data);
436 extern bdf_font_t *bdf_load_server_font(Display *d, XFontStruct *f,
437 char *name, bdf_options_t *opts,
438 bdf_callback_t callback,
440 #endif /* HAVE_XLIB */
442 extern int bdf_load_console_font(FILE *in, bdf_options_t *opts,
443 bdf_callback_t callback, void *data,
444 bdf_font_t *fonts[3], int *nfonts);
446 extern int bdf_load_mf_font(FILE *in, bdf_options_t *opts,
447 bdf_callback_t callback, void *data,
450 extern void bdf_save_font(FILE *out, bdf_font_t *font, bdf_options_t *opts,
451 bdf_callback_t callback, void *data);
453 extern void bdf_save_sbit_metrics(FILE *out, bdf_font_t *font,
454 bdf_options_t *opts, char *appname);
456 extern void bdf_export_hex(FILE *out, bdf_font_t *font, bdf_options_t *opts,
457 bdf_callback_t callback, void *data);
459 extern int bdf_export_psf(FILE *out, bdf_font_t *font, bdf_options_t *opts,
462 extern void bdf_free_font(bdf_font_t *font);
467 * OpenType related macros and functions.
471 * ID numbers of the strings that can appear in an OpenType font.
473 #define BDFOTF_COPYRIGHT_STRING 0
474 #define BDFOTF_FAMILY_STRING 1
475 #define BDFOTF_SUBFAMILY_STRING 2
476 #define BDFOTF_UNIQUEID_STRING 3
477 #define BDFOTF_FULLNAME_STRING 4
478 #define BDFOTF_VENDOR_STRING 5
479 #define BDFOTF_POSTSCRIPT_STRING 6
480 #define BDFOTF_TRADEMARK_STRING 7
481 #define BDFOTF_FOUNDRY_STRING 8
482 #define BDFOTF_DESIGNER_STRING 9
483 #define BDFOTF_DESCRIPTION_STRING 10
484 #define BDFOTF_VENDORURL_STRING 11
485 #define BDFOTF_DESIGNERURL_STRING 12
486 #define BDFOTF_LICENSE_STRING 13
487 #define BDFOTF_LICENSEURL_STRING 14
488 #define BDFOTF_RESERVED_STRING 15
489 #define BDFOTF_PREFFAMILY_STRING 16
490 #define BDFOTF_PREFSUBFAMILY_STRING 17
491 #define BDFOTF_COMPATIBLEMAC_STRING 18
492 #define BDFOTF_SAMPLETEXT_STRING 19
493 #define BDFOTF_PSCIDFF_STRING 20
495 extern char *bdfotf_platform_name(short pid);
496 extern char *bdfotf_encoding_name(short pid, short eid);
497 extern int bdfotf_get_english_string(FT_Face face, int nameID,
498 int dash_to_space, char *name);
500 extern int bdfotf_load_font(FT_Face face, short pid, short eid,
501 bdf_options_t *opts, bdf_callback_t callback,
502 void *data, bdf_font_t **font);
504 #endif /* HAVE_FREETYPE */
507 * FON/FNT related functions.
511 * String ID numbers for FON/FNT fonts.
513 #define BDFFNT_COPYRIGHT 1
514 #define BDFFNT_TYPEFACE 2
519 typedef struct _bdffnt_font_t *bdffnt_font_t;
521 extern int bdffnt_open_font(char *path, bdffnt_font_t *font);
522 extern void bdffnt_close_font(bdffnt_font_t font);
523 extern int bdffnt_font_count(bdffnt_font_t font);
524 extern int bdffnt_get_copyright(bdffnt_font_t font, unsigned int fontID,
525 unsigned char *string);
526 extern int bdffnt_get_facename(bdffnt_font_t font, unsigned int fontID,
527 int for_xlfd, unsigned char *string);
528 extern int bdffnt_char_count(bdffnt_font_t font, unsigned int fontID);
529 extern int bdffnt_font_pointsize(bdffnt_font_t font, unsigned int fontID);
530 extern int bdffnt_load_font(bdffnt_font_t font, unsigned int fontID,
531 bdf_callback_t callback, void *data,
537 * In PSF fonts, a Unicode table on the end of the font may map a single
538 * glyph to several locations. The BDFPSF_SOURCE_GLYPH marks the glyphs that
539 * are source glyphs and the BDFPSF_PSEUDO_GLYPH flag marks glyphs that are
540 * clones of a source glyph.
542 #define BDFPSF_SOURCE_GLYPH 0x0001
543 #define BDFPSF_PSEUDO_GLYPH 0x0002
545 extern bdf_font_t *bdf_load_psf(FILE *in, unsigned char *magic,
547 bdf_callback_t callback, void *data,
551 * Font property functions.
553 extern void bdf_create_property(char *name, int type);
554 extern bdf_property_t *bdf_get_property(char *name);
555 extern unsigned int bdf_property_list(bdf_property_t **props);
557 extern void bdf_add_font_property(bdf_font_t *font, bdf_property_t *property);
558 extern void bdf_delete_font_property(bdf_font_t *font, char *name);
559 extern bdf_property_t *bdf_get_font_property(bdf_font_t *font, char *name);
560 extern unsigned int bdf_font_property_list(bdf_font_t *font,
561 bdf_property_t **props);
562 extern int bdf_is_xlfd_property(char *name);
565 * Font comment functions.
567 extern int bdf_replace_comments(bdf_font_t *font, char *comments,
568 unsigned int comments_len);
571 * Other miscellaneous functions.
573 extern void bdf_set_default_metrics(bdf_font_t *font);
576 * Font glyph editing functions.
578 extern int bdf_glyph_modified(bdf_font_t *font, int which, int unencoded);
580 extern void bdf_copy_glyphs(bdf_font_t *font, int start, int end,
581 bdf_glyphlist_t *glyphs, int unencoded);
583 extern int bdf_delete_glyphs(bdf_font_t *font, int start, int end,
586 extern int bdf_insert_glyphs(bdf_font_t *font, int start,
587 bdf_glyphlist_t *glyphs);
589 extern int bdf_replace_glyphs(bdf_font_t *font, int start,
590 bdf_glyphlist_t *glyphs, int unencoded);
592 extern int bdf_merge_glyphs(bdf_font_t *font, int start,
593 bdf_glyphlist_t *glyphs, int unencoded);
595 extern int bdf_replace_mappings(bdf_font_t *font, int encoding,
596 bdf_psf_unimap_t *map, int unencoded);
598 /**************************************************************************
600 * Other API functions.
602 **************************************************************************/
604 extern int bdf_set_font_bbx(bdf_font_t *font, bdf_metrics_t *metrics);
606 extern void bdf_set_modified(bdf_font_t *font, int modified);
608 extern int bdf_has_xlfd_name(bdf_font_t *font);
610 extern char *bdf_make_xlfd_name(bdf_font_t *font, char *foundry,
613 extern void bdf_update_name_from_properties(bdf_font_t *font);
615 extern int bdf_update_properties_from_name(bdf_font_t *font);
617 extern int bdf_update_average_width(bdf_font_t *font);
619 extern int bdf_set_unicode_glyph_names(FILE *in, bdf_font_t *font,
620 bdf_callback_t callback);
622 extern int bdf_set_adobe_glyph_names(FILE *in, bdf_font_t *font,
623 bdf_callback_t callback);
625 extern int bdf_set_glyph_code_names(int prefix, bdf_font_t *font,
626 bdf_callback_t callback);
629 * Routine to add Unicode mappings when editing PSF fonts.
631 extern int bdf_psf_add_unicode_mapping(bdf_psf_unimap_t *u,
632 unsigned int *mapping,
633 unsigned int mapping_cnt);
635 /**************************************************************************
639 **************************************************************************/
642 * Glyph grid allocation and deallocation functions.
644 extern bdf_glyph_grid_t *bdf_make_glyph_grid(bdf_font_t *font,
647 extern void bdf_free_glyph_grid(bdf_glyph_grid_t *grid);
650 * Glyph grid information functions.
652 extern void bdf_grid_image(bdf_glyph_grid_t *grid, bdf_bitmap_t *image);
653 extern void bdf_grid_origin(bdf_glyph_grid_t *grid, short *x, short *y);
654 extern bdf_glyph_t *bdf_grid_glyph(bdf_glyph_grid_t *grid);
657 * Glyph grid editing functions.
659 extern int bdf_grid_enlarge(bdf_glyph_grid_t *grid, unsigned short width,
660 unsigned short height);
661 extern int bdf_grid_resize(bdf_glyph_grid_t *grid,
662 bdf_metrics_t *metrics);
663 extern int bdf_grid_crop(bdf_glyph_grid_t *grid, int grid_modified);
665 extern int bdf_grid_set_pixel(bdf_glyph_grid_t *grid, short x, short y,
667 extern int bdf_grid_clear_pixel(bdf_glyph_grid_t *grid, short x, short y);
668 extern int bdf_grid_invert_pixel(bdf_glyph_grid_t *grid,
669 short x, short y, int val);
670 extern int bdf_grid_shift(bdf_glyph_grid_t *grid, short xcount,
672 extern int bdf_grid_flip(bdf_glyph_grid_t *grid, short dir);
673 extern int bdf_grid_rotate(bdf_glyph_grid_t *grid, short degrees,
675 extern int bdf_grid_shear(bdf_glyph_grid_t *grid, short degrees,
677 extern int bdf_grid_embolden(bdf_glyph_grid_t *grid);
680 * Glyph grid selection functions.
682 extern int bdf_has_selection(bdf_glyph_grid_t *grid, short *x, short *y,
683 short *width, short *height);
684 extern void bdf_set_selection(bdf_glyph_grid_t *grid, short x, short y,
685 short width, short height);
686 extern void bdf_lose_selection(bdf_glyph_grid_t *grid);
687 extern void bdf_detach_selection(bdf_glyph_grid_t *grid);
688 extern void bdf_attach_selection(bdf_glyph_grid_t *grid);
689 extern void bdf_delete_selection(bdf_glyph_grid_t *grid);
690 extern int bdf_in_selection(bdf_glyph_grid_t *grid, short x, short y,
692 extern void bdf_add_selection(bdf_glyph_grid_t *grid, bdf_bitmap_t *sel);
695 * Glyph grid misc functions.
697 extern int bdf_grid_color_at(bdf_glyph_grid_t *grid, short x, short y);
700 * Graphical transformation functions.
702 extern int bdf_translate_glyphs(bdf_font_t *font, short dx, short dy,
704 bdf_callback_t callback, void *data,
707 extern int bdf_rotate_glyphs(bdf_font_t *font, short degrees,
709 bdf_callback_t callback, void *data,
712 extern int bdf_shear_glyphs(bdf_font_t *font, short degrees,
714 bdf_callback_t callback, void *data,
717 extern int bdf_embolden_glyphs(bdf_font_t *font, int start, int end,
718 bdf_callback_t callback, void *data,
719 int unencoded, int *resize);
721 extern int bdf_little_endian(void);