]>
git.proxmox.com Git - spiceterm.git/blob - genfont.c
3 Copyright (C) 2007-2013 Proxmox Server Solutions GmbH
5 Copyright: spiceterm is under GNU GPL, the GNU General Public License.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; version 2 dated June, 1991.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 Author: Dietmar Maurer <dietmar@proxmox.com>
27 #include <zlib.h> /* read compressed console fonts */
32 /* map unicode to font */
33 static unsigned short vt_fontmap
[65536];
35 /* font glyph storage */
36 static unsigned char *vt_font_data
= NULL
;
37 static int vt_font_size
= 0;
38 static int vt_font_maxsize
= 0;
43 #define PSF_MAGIC1 0x36
44 #define PSF_MAGIC2 0x04
46 #define PSF_MODE256NOSFM 0
47 #define PSF_MODE512NOSFM 1
48 #define PSF_MODE256SFM 2
49 #define PSF_MODE512SFM 3
51 #define PSF_SEPARATOR 0xFFFF
55 unsigned char magic1
, magic2
; /* Magic number */
56 unsigned char mode
; /* PSF font mode */
57 unsigned char charheight
; /* Character size */
60 #define PSF_MAGIC_OK(x) ((x).magic1 == PSF_MAGIC1 && (x).magic2 == PSF_MAGIC2)
61 #define PSF_MODE_VALID(x) ((x) <= PSF_MODE512SFM)
62 #define PSF_MODE_HAS512(x) (((x) == 1) || ((x) == 3))
63 #define PSF_MODE_HASSFM(x) (((x) == 2) || ((x) == 3))
65 typedef unsigned short unicode
;
68 font_add_glyph (const char *data
)
71 if (vt_font_size
>= vt_font_maxsize
) {
72 vt_font_maxsize
+= 256;
73 vt_font_data
= realloc (vt_font_data
, vt_font_maxsize
*16);
76 memcpy (vt_font_data
+ vt_font_size
*16, data
, 16);
80 return vt_font_size
- 1;
84 load_psf_font (const char *filename
, int is_default
)
86 struct psf_header psfhdr
;
88 gzFile f
= gzopen (filename
, "rb");
90 fprintf (stderr
, "unable to read file %s\n", filename
);
95 if (gzread(f
, &psfhdr
, sizeof(struct psf_header
)) != sizeof(struct psf_header
)) {
96 fprintf (stderr
, "unable to read psf font header (%s)\n", filename
);
101 if (!PSF_MAGIC_OK(psfhdr
) || !PSF_MODE_VALID(psfhdr
.mode
) ||
102 !PSF_MODE_HASSFM(psfhdr
.mode
) || (psfhdr
.charheight
!= 16)) {
103 fprintf (stderr
, "no valid 8*16 psf font (%s)\n", filename
);
108 int charcount
= ((PSF_MODE_HAS512(psfhdr
.mode
)) ? 512 : 256);
110 int size
= 16*charcount
;
112 char *chardata
= (char *)malloc (size
);
114 if (size
!= gzread(f
, chardata
, size
)) {
115 fprintf (stderr
, "unable to read font character data (%s)\n", filename
);
123 for (glyph
= 0 ;glyph
< charcount
;glyph
++) {
125 while (gzread (f
, &unichar
, sizeof(unicode
)) == sizeof(unicode
) &&
126 (unichar
!= PSF_SEPARATOR
)) {
127 if (!vt_fontmap
[unichar
]) {
129 fi
= font_add_glyph (chardata
+ glyph
*16);
131 vt_fontmap
[unichar
] = fi
;
135 if (is_default
&& fi
&& glyph
< 256) {
136 vt_fontmap
[0xf000 + glyph
] = fi
;
151 printf ("int vt_font_size = %d;\n\n", vt_font_size
);
153 printf ("unsigned char vt_font_data[] = {\n");
154 for (i
= 0; i
< vt_font_size
; i
++) {
155 printf ("\t/* %d 0x%02x */\n", i
, i
);
156 for (j
= 0; j
< 16; j
++) {
157 unsigned char d
= vt_font_data
[i
*16+j
];
158 printf ("\t0x%02X, /* ", d
);
160 for (k
= 128; k
> 0; k
= k
>>1) {
161 printf ("%c", (d
& k
) ? '1': '0');
169 printf ("unsigned short vt_fontmap[65536] = {\n");
170 for (i
= 0; i
< 0x0ffff; i
++) {
171 printf ("\t/* 0x%04X => */ %d,\n", i
, vt_fontmap
[i
]);
178 main (int argc
, char** argv
)
180 char empty
[] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
183 font_add_glyph (empty
);
185 /* font load order is only important if glyphs are redefined */
186 load_psf_font ("/usr/share/consolefonts/lat1u-16.psf.gz", 0); /* Latin-1 */
187 load_psf_font ("/usr/share/consolefonts/default8x16.psf.gz", 1); /* vga default */
188 load_psf_font ("/usr/share/consolefonts/lat2u-16.psf.gz", 0); /* Latin-2 */
189 load_psf_font ("/usr/share/consolefonts/lat4u-16.psf.gz", 0); /* Baltic */
191 load_psf_font ("/usr/share/consolefonts/iso07.f16.psf.gz", 0); /* Greek */
192 load_psf_font ("/usr/share/consolefonts/Goha-16.psf.gz", 0); /* Ethiopic */
193 load_psf_font ("/usr/share/consolefonts/Arabic-Fixed16.psf.gz", 0); /* Arabic */
195 /* fixme: Japanese letters ? */
197 glob("/usr/share/consolefonts/*", GLOB_ERR
, NULL
, &globbuf
);
200 for (i
= 0; i
< globbuf
.gl_pathc
; i
++) {
201 int pc
= vt_font_size
;
202 load_psf_font (globbuf
.gl_pathv
[i
], 0);
203 if (vt_font_size
> pc
) {
204 //fprintf(stderr, "TEST: %s %d\n", globbuf.gl_pathv[i], vt_font_size - pc);