1 /*
2     psftools: Manipulate console fonts in the .PSF format
3     Copyright (C) 2005  John Elliott
4 
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9 
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14 
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 
20 /* This is a mini-library for loading and saving CPI files, the better to
21  * manipulate them. */
22 typedef unsigned char cpi_byte;
23 
24 /* This corresponds to a single font. For FONT and FONT.NT files, the font
25  * bitmap will be stored in this object. For DRFONT files, these just have
26  * the font size; the font bitmaps live in the CP_DRFONT structure below. */
27 typedef struct cp_font
28 {
29 	unsigned pr_type;	/* Printer CPI: Printer type */
30 	unsigned pr_seqsize;	/* Printer CPI: Escape sequence length */
31 	unsigned fontsize;	/* Size of mallocced data */
32 	unsigned height;	/* Character height */
33 	unsigned width;		/* Character width */
34 	unsigned nchars;	/* Count of characters */
35 	unsigned xaspect;	/* X aspect */
36 	unsigned yaspect;	/* Y aspect */
37 	cpi_byte *data;		/* Character bitmaps */
38 	long self_offset;	/* Offset of this structure in the file */
39 	struct cp_font *next;	/* Linked list of fonts in codepage */
40 } CP_FONT;
41 
42 /* This is an entry for a codepage; it may have one or more fonts attached. */
43 typedef struct cp_head
44 {
45 	unsigned headsize;		/* Size (in file) of the header */
46 	unsigned dev_type;		/* Device type 1=Screen 2=Printer */
47 	char  dev_name[9];		/* Device name */
48 	unsigned short codepage;	/* Codepage ID */
49 	unsigned font_count;		/* Number of fonts */
50 	long  next_offset;		/* File offset of next header */
51 	long  cpih_offset;		/* File offset of CP_FONT */
52 	long  self_offset;		/* File offset of this */
53 	long  font_len;			/* Length of all font data */
54 	unsigned short *dr_lookup;	/* DRFONT lookup table */
55 	struct cp_font *fonts;		/* Child fonts */
56 	struct cp_head *next;		/* Linked list of these in CPI file */
57 } CP_HEAD;
58 
59 /* This contains the font bitmaps used by DRFONT fonts. */
60 typedef struct cp_drfont
61 {
62 	unsigned char_len;
63 	long offset;
64 	unsigned bitmap_len;
65 	cpi_byte *bitmap;
66 } CP_DRFONT;
67 
68 /* And this represents an entire file */
69 typedef struct cpi_file
70 {
71 	cpi_byte magic0;	/* 0x7F for DRFONT, 0xFF for FONT */
72 	char format[8];		/* DRFONT / FONT as string */
73 	CP_HEAD *firstpage;	/* Linked list of codepages */
74 	unsigned drcount;	/* Number of size records (DRFONT) */
75 	CP_DRFONT *drfonts;	/* The DRFONT-specific entries */
76 	unsigned comment_len;	/* Size of comment (usually (c) message) */
77 	cpi_byte *comment;	/* Comment data */
78 } CPI_FILE;
79 
80 /* The three CPI formats supported: */
81 typedef enum
82 {
83 	CPI_FONT, 	/* MS-DOS / PC-DOS screen or printer font */
84 	CPI_FONTNT, 	/* Windows NT font with relative addresses */
85 	CPI_DRFONT,	/* DR-DOS compressed screen font */
86 	CPI_NAKED	/* Linux .CP file */
87 }
88 cpi_format;
89 
90 /* Initialise a CPI object */
91 int cpi_new   (CPI_FILE *f, cpi_format format);
92 /* Free a CPI object and all its memory */
93 int cpi_delete(CPI_FILE *f);
94 /* Load a CPI file */
95 int cpi_load  (CPI_FILE *f, const char *filename);
96 int cpi_loadfile(CPI_FILE *f, FILE *fp);
97 /* Save a CPI file. Interleaved is 0 to save the headers followed by the
98  * data; 1 to save each header followed immediately by its associated data. */
99 int cpi_save  (CPI_FILE *f, const char *filename, int interleaved);
100 int cpi_savefile(CPI_FILE *f, FILE *fp, int interleaved);
101 
102 /* Debugging: Dump a CPI structure as text */
103 void cpi_dump (CPI_FILE *f);
104 
105 /* See if CPI file has the given codepage */
106 CP_HEAD *cpi_lookup(CPI_FILE *f, unsigned number);
107 /* Force CPI file to have the given codepage */
108 CP_HEAD *cpi_add_page(CPI_FILE *f, unsigned number);
109 /* Add a font to a codepage */
110 CP_FONT *cph_add_font(CP_HEAD *h);
111 /* Convert the internal format of a CPI file to DRFONT */
112 int cpi_crush(CPI_FILE *f);
113 /* Convert the internal format of a CPI file from DRFONT */
114 int cpi_bloat(CPI_FILE *f);
115 /* Get the number of characters in a codepage */
116 int cpi_count_chars(CP_HEAD *h);
117 /* Find the bitmap for a glyph */
118 cpi_byte *cpi_find_glyph(CPI_FILE *f, int codepage, int height,
119 		int width, int glyph);
120 
121 /* Error numbers have been chosen to match the ones in psflib.h */
122 #define CPI_ERR_OK     (0)	/* No error */
123 #define CPI_ERR_NOMEM  (-1)	/* malloc() failed */
124 #define CPI_ERR_BADFMT (-3)	/* CPI file not in correct format */
125 #define CPI_ERR_ERRNO  (-4)	/* Error in stdio function, use perror() */
126 #define CPI_ERR_EMPTY  (-5)	/* File is empty */
127