utils/vfl2bdf-2.0.0/vfl2bdf.c
/* [<][>][^][v][top][bottom][index][help] */
FUNCTIONS
This source file includes following functions.
- main
- usage
- GenerateBDF
- DecideFontXLDF
- BDF_Header
- BDF_Trailer
- BDF_PutChar
1 /*
2 * vf2bdf
3 * --- Generate a BDF font file from VFlib fonts.
4 *
5 * Programmed by Hirotsugu KAKUGAWA
6 * E-Mail: h.kakugwa@computer.org
7 *
8 * Edition History
9 * 18 Jan 1996 for VFlib 2
10 * 6 May 1997 for VFlib 3.2
11 * 6 Aug 1997 for VFlib 3.3
12 * 1 Mar 1999 for VFlib 3.5, bug fix.
13 *
14 */
15 /*
16 * Copyright (C) 1996-1998 Hirotsugu Kakugawa.
17 * All rights reserved.
18 *
19 * This file is part of the VFlib Library. This library is free
20 * software; you can redistribute it and/or modify it under the terms of
21 * the GNU Library General Public License as published by the Free
22 * Software Foundation; either version 2 of the License, or (at your
23 * option) any later version. This library is distributed in the hope
24 * that it will be useful, but WITHOUT ANY WARRANTY; without even the
25 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
26 * PURPOSE. See the GNU Library General Public License for more details.
27 * You should have received a copy of the GNU Library General Public
28 * License along with this library; if not, write to the Free Software
29 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 */
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <VFlib-3_6.h>
36
37 #define VFL2BDF_VERSION "2.1"
38
39 #define GLYPH_TYPE_FONT_BBX 0
40 #define GLYPH_TYPE_MIN_BBX 1
41 #define ENCODING_TYPE_FLAT 0
42 #define ENCODING_TYPE_94x94 1
43
44
45 int gtype = GLYPH_TYPE_FONT_BBX;
46 int etype = ENCODING_TYPE_FLAT;
47 int code_from = -1;
48 int code_to = -1;
49 int pixel = -1;
50 int xoff = 0;
51 int yoff = 0;
52 int fixed = 0;
53 int quiet = 0;
54 char font_xlfd[1024];
55
56 int font_bbx_w, font_bbx_h;
57 int font_off_x, font_off_y;
58 int font_mv_x, font_mv_y;
59
60 char *font_creator = "UNKNOWN";
61 char *font_family = "UNKNOWN";
62 char *charset_registry = "UNKNOWN";
63 char *charset_encoding = "0";
64
65
66
67 void usage(void);
68 void DecideFontXLDF(int pixel_size, double point_size);
69 int GenerateBDF(char *font_name, char *bdf_file_name);
70 int BDF_PutChar(FILE *bdf_fp, VF_BITMAP bm,
71 int code_point, int pixel_size);
72 int BDF_Header(FILE *bdf_fp, int fid, int pixel_size, double point_size,
73 int nchars, int default_char,
74 int font_ascent, int font_descent,
75 int font_bbx_w, int font_bbx_h,
76 int font_off_x, int font_off_y);
77 int BDF_Trailer(FILE *bdf_fp, int pixel_size);
78
79
80 int
81 main(argc, argv)
/* [<][>][^][v][top][bottom][index][help] */
82 int argc;
83 char **argv;
84 {
85 int iarg;
86 char *font_name, *bdf_file_name, *vflibcap;
87
88 vflibcap = NULL;
89 font_name = NULL;
90 bdf_file_name = NULL;
91
92 argv++;
93 argc--;
94 iarg = 0;
95 while (argc > 0){
96 if (argv[0][0] != '-'){
97 switch (iarg++){
98 case 0:
99 font_name = *argv;
100 break;
101 case 1:
102 sscanf(*argv, "%i", &code_from);
103 break;
104 case 2:
105 sscanf(*argv, "%i", &code_to);
106 break;
107 default:
108 break;
109 }
110 } else {
111 if (strcmp(argv[0], "-v") == 0){
112 if (--argc == 0)
113 usage();
114 vflibcap = *(++argv);
115 } else if (strcmp(argv[0], "-f") == 0){
116 if (--argc == 0)
117 usage();
118 font_name = *(++argv);
119 } else if (strcmp(argv[0], "-m") == 0){
120 gtype = GLYPH_TYPE_MIN_BBX;
121 } else if (strcmp(argv[0], "-p") == 0){
122 if (--argc == 0)
123 usage();
124 pixel = atoi(*(++argv));
125 if (pixel <= 0){
126 fprintf(stderr, "Illegal pixel size=%d\n", pixel);
127 exit(1);
128 }
129 } else if (strcmp(argv[0], "-xoff") == 0){
130 if (--argc == 0)
131 usage();
132 xoff = atoi(*(++argv));
133 } else if (strcmp(argv[0], "-yoff") == 0){
134 if (--argc == 0)
135 usage();
136 yoff = atoi(*(++argv));
137 } else if (strcmp(argv[0], "-o") == 0){
138 if (--argc == 0)
139 usage();
140 bdf_file_name = *(++argv);
141 } else if (strcmp(argv[0], "-94x94") == 0){
142 etype = ENCODING_TYPE_94x94;
143 } else if (strcmp(argv[0], "-fixed") == 0){
144 fixed = 1;
145 } else if (strcmp(argv[0], "-q") == 0){
146 quiet = 1;
147 } else if (strcmp(argv[0], "-font-creator") == 0){
148 if (--argc == 0)
149 usage();
150 font_creator = *(++argv);
151 } else if (strcmp(argv[0], "-font-family") == 0){
152 if (--argc == 0)
153 usage();
154 font_family = *(++argv);
155 } else if (strcmp(argv[0], "-charset-registry") == 0){
156 if (--argc == 0)
157 usage();
158 charset_registry = *(++argv);
159 } else if (strcmp(argv[0], "-charset-encoding") == 0){
160 if (--argc == 0)
161 usage();
162 charset_encoding = *(++argv);
163 } else {
164 printf("Unknown option: %s\n", *argv);
165 usage();
166 }
167 }
168 argc--;
169 argv++;
170 }
171
172 if ((font_name == NULL) || (code_from < 0) || (code_to < 0))
173 usage();
174
175 if (VF_Init(vflibcap, NULL) < 0){
176 fprintf(stderr, "VFlib initialization error.\n");
177 exit(1);
178 }
179
180 GenerateBDF(font_name, bdf_file_name);
181
182 return 0;
183 }
184
185 void usage(void)
/* [<][>][^][v][top][bottom][index][help] */
186 {
187 printf("Usage: vfl2bdf [Options] FONT START END\n");
188 printf(" - Make a BDF font from VFlib font FONT.\n");
189 printf(" Range of code points is from START to END.\n");
190 printf("Options: \n");
191 printf(" -v FILE : vflibcap file\n");
192 printf(" -p PIXEL : pixel size of BDF font\n");
193 printf(" -o FILE : output file name\n");
194 printf(" -f FONT : font name\n");
195 printf(" -xoff N : shift N pixels to right\n");
196 printf(" -yoff N : shift N pixels to up\n");
197 printf(" -94x94 : assume the font is encoded in 94x94 style\n");
198 printf(" -m : make minimized bounding boxes\n");
199 printf(" -q : quiet mode\n");
200 printf(" -h : print how to use this program\n");
201 printf(" -font-family NAME : set font family name\n");
202 printf(" -charset-registry NAME : set charset registry name\n");
203 printf(" -charset-encoding NAME : set charset encoding name\n");
204 exit(0);
205 }
206
207
208 int
209 GenerateBDF(char *font_name, char *bdf_file_name)
/* [<][>][^][v][top][bottom][index][help] */
210 {
211 FILE *bdf_fp;
212 int fid1, fid2;
213 int code_point, nchars, default_char;
214 int font_ascent, font_descent;
215 int pixel_size;
216 double point_size;
217 VF_BITMAP bm, bm2;
218 char *p;
219
220 font_bbx_w = font_bbx_h = 0;
221 font_off_x = font_off_y = 0;
222 font_mv_x = font_mv_y = 0;
223 font_ascent = font_descent = 0;
224 default_char = code_from;
225
226
227 if (bdf_file_name != NULL){
228 if ((bdf_fp = fopen(bdf_file_name, "w")) == NULL){
229 fprintf(stderr, "Can't open output file: %s\n", bdf_file_name);
230 exit(0);
231 }
232 if (quiet == 0)
233 fprintf(stderr, "Writing to %s\n", bdf_file_name);
234 } else {
235 bdf_fp = stdout;
236 if (quiet == 0)
237 fprintf(stderr, "Writing to %s\n", "standard output");
238 }
239
240
241 /* PASS 1 */
242
243 if (quiet == 0)
244 fprintf(stderr, "Pass 1\n");
245
246 if ((fid1 = VF_OpenFont2(font_name, pixel, 1, 1)) < 0){
247 fprintf(stderr, "Can't open font: %s.\n", font_name);
248 exit(1);
249 }
250
251 nchars = 0;
252 for (code_point = code_from; code_point <= code_to; code_point++){
253 if ((etype == ENCODING_TYPE_94x94)
254 && ( ((code_point % 256) < 0x21) || (0x7e < (code_point % 256))
255 || ((code_point / 256) < 0x21) || (0x7e < (code_point / 256))))
256 continue;
257 if ((bm = VF_GetBitmap2(fid1, code_point, 1, 1)) != NULL){
258 nchars++;
259 if (font_bbx_w < bm->bbx_width)
260 font_bbx_w = bm->bbx_width;
261 if (font_bbx_h < bm->bbx_height)
262 font_bbx_h = bm->bbx_height;
263 if (font_mv_x < bm->mv_x)
264 font_mv_x = bm->mv_x;
265 if (font_mv_y < bm->mv_y)
266 font_mv_y = bm->mv_y;
267 if (font_off_x > bm->off_x)
268 font_off_x = bm->off_x;
269 if (font_off_y > (bm->bbx_height - bm->off_y))
270 font_off_y = (bm->bbx_height - bm->off_y);
271 VF_FreeBitmap(bm);
272 }
273 }
274
275 if ((pixel_size = pixel) < 0){
276 if ((p = VF_GetFontProp(fid1, "PIXEL_SIZE")) != NULL)
277 pixel_size = atoi(p);
278 }
279 if ((p = VF_GetFontProp(fid1, "POINT_SIZE")) != NULL){
280 point_size = (double)atof(p)/10.0;
281 } else {
282 point_size = pixel_size;
283 }
284
285 if (quiet == 0){
286 fprintf(stderr, " Pixel size = %d\n", pixel);
287 fprintf(stderr, " Point size = %.3f\n", point_size);
288 fprintf(stderr, " %d chracters\n", nchars);
289 }
290
291 DecideFontXLDF(pixel_size, point_size);
292
293 /* PASS 2 */
294
295 if (quiet == 0)
296 fprintf(stderr, "Pass 2\n");
297 if (quiet == 0)
298 fprintf(stderr, " XLFD = %s\n", font_xlfd);
299
300
301 if ((fid2 = VF_OpenFont2(font_name, pixel, 1, 1)) < 0){
302 fprintf(stderr, "Can't open font: %s.\n", font_name);
303 exit(1);
304 }
305
306 BDF_Header(bdf_fp, fid2, pixel_size, point_size,
307 nchars, default_char, font_ascent, font_descent,
308 font_bbx_w, font_bbx_h, font_off_x, font_off_y);
309
310 for (code_point = code_from; code_point <= code_to; code_point++){
311 if ((etype == ENCODING_TYPE_94x94)
312 && ( ((code_point % 256) < 0x21) || (0x7e < (code_point % 256))
313 || ((code_point / 256) < 0x21) || (0x7e < (code_point / 256))))
314 continue;
315 if ((bm = VF_GetBitmap2(fid2, code_point, 1, 1)) != NULL){
316 if (gtype == GLYPH_TYPE_FONT_BBX){
317 BDF_PutChar(bdf_fp, bm, code_point, pixel_size);
318 } else {
319 if ((bm2 = VF_MinimizeBitmap(bm)) != NULL){
320 BDF_PutChar(bdf_fp, bm2, code_point, pixel_size);
321 VF_FreeBitmap(bm2);
322 }
323 }
324 VF_FreeBitmap(bm);
325 }
326 }
327
328 BDF_Trailer(bdf_fp, pixel_size);
329
330 fclose(bdf_fp);
331
332 VF_CloseFont(fid1);
333 VF_CloseFont(fid2);
334
335 if (quiet == 0)
336 fprintf(stderr, "done.\n");
337
338 return nchars;
339 }
340
341
342 void
343 DecideFontXLDF(int pixel_size, double point_size)
/* [<][>][^][v][top][bottom][index][help] */
344 {
345 sprintf(font_xlfd, "-%s-%s-%s-%s-%s--%d-%d-75-75-C-140-%s-%s",
346 font_creator, font_family, "medium", "r", "normal",
347 pixel_size, (int)(point_size*10.0),
348 charset_registry, charset_encoding);
349 }
350
351 int
352 BDF_Header(FILE *bdf_fp, int fid, int pixel_size, double point_size,
/* [<][>][^][v][top][bottom][index][help] */
353 int nchars, int default_char,
354 int font_ascent, int font_descent,
355 int font_bbx_w, int font_bbx_h, int font_off_x, int font_off_y)
356 {
357 char *p;
358
359 fprintf(bdf_fp, "STARTFONT 2.1\n");
360 fprintf(bdf_fp, "COMMENT BDF font by VFL2BDF %s\n", VFL2BDF_VERSION);
361 fprintf(bdf_fp, "FONT %s\n", font_xlfd);
362 fprintf(bdf_fp, "SIZE %d 75 75\n", (int)(point_size+0.5));
363 fprintf(bdf_fp, "FONTBOUNDINGBOX %d %d %d %d\n",
364 font_bbx_w, font_bbx_h,
365 font_off_x + xoff, font_off_y + yoff);
366 fprintf(bdf_fp, "STARTPROPERTIES 9\n");
367 fprintf(bdf_fp, "PIXEL_SIZE %d\n", pixel_size);
368 fprintf(bdf_fp, "POINT_SIZE %d\n", (int)(point_size*10.0));
369 fprintf(bdf_fp, "RESOLUTION_X 75\n");
370 fprintf(bdf_fp, "RESOLUTION_Y 75\n");
371 if ((p = VF_GetFontProp(fid, "CHARSET_REGISTRY")) == NULL)
372 p = "UNKNOWN";
373 fprintf(bdf_fp, "CHARSET_REGISTRY \"%s\"\n", p);
374 if ((p = VF_GetFontProp(fid, "CHARSET_ENCODING")) == NULL)
375 p = "";
376 fprintf(bdf_fp, "CHARSET_ENCODING \"%s\"\n", p);
377 fprintf(bdf_fp, "DEFAULT_CHAR %d\n", default_char);
378 fprintf(bdf_fp, "FONT_DESCENT %d\n", font_descent + yoff);
379 fprintf(bdf_fp, "FONT_ASCENT %d\n", font_ascent + yoff);
380 fprintf(bdf_fp, "ENDPROPERTIES\n");
381 fprintf(bdf_fp, "CHARS %d\n", nchars);
382 return 0;
383 }
384
385 int
386 BDF_Trailer(FILE *bdf_fp, int pixel_size)
/* [<][>][^][v][top][bottom][index][help] */
387 {
388 fprintf(bdf_fp, "ENDFONT\n");
389 return 0;
390 }
391
392 int
393 BDF_PutChar(FILE *bdf_fp, VF_BITMAP bm,
/* [<][>][^][v][top][bottom][index][help] */
394 int code_point, int pixel_size)
395 {
396 int x, y;
397 unsigned char *p;
398
399 fprintf(bdf_fp, "STARTCHAR 0x%X\n", code_point);
400 fprintf(bdf_fp, "ENCODING %d\n", code_point);
401 fprintf(bdf_fp, "SWIDTH %d %d\n",
402 (int)(1000.0*(double)bm->mv_x/(double)pixel_size),
403 (int)(1000.0*(double)bm->mv_y/(double)pixel_size));
404 if (fixed == 0){ /* propotional font */
405 fprintf(bdf_fp, "DWIDTH %d %d\n", bm->mv_x, bm->mv_y);
406 fprintf(bdf_fp, "BBX %d %d %d %d\n",
407 bm->bbx_width, bm->bbx_height,
408 bm->off_x + xoff, bm->off_y - bm->bbx_height + yoff);
409 fprintf(bdf_fp, "BITMAP\n");
410 for (y = 0; y < bm->bbx_height; y++){
411 p = &bm->bitmap[y*bm->raster];
412 for (x = 0; x < (bm->bbx_width+7)/8; x++)
413 fprintf(bdf_fp, "%02X", p[x]);
414 fprintf(bdf_fp, "\n");
415 }
416 } else { /* monospace font */
417 fprintf(bdf_fp, "DWIDTH %d %d\n", font_mv_x, font_mv_y);
418 fprintf(bdf_fp, "BBX %d %d %d %d\n",
419 font_bbx_w, font_bbx_h,
420 bm->off_x + xoff, bm->off_y - bm->bbx_height + yoff);
421 fprintf(bdf_fp, "BITMAP\n");
422 for (y = 0; y < font_bbx_h - bm->bbx_height; y++){
423 for (x = 0; x < (bm->bbx_width+7)/8; x++)
424 fprintf(bdf_fp, "00");
425 fprintf(bdf_fp, "\n");
426 }
427 for (y = 0; y < bm->bbx_height; y++){
428 p = &bm->bitmap[y*bm->raster];
429 for (x = 0; x < (bm->bbx_width+7)/8; x++)
430 fprintf(bdf_fp, "%02X", p[x]);
431 for (x = (bm->bbx_width+7)/8; x < (font_bbx_w+7)/8; x++)
432 fprintf(bdf_fp, "00");
433 fprintf(bdf_fp, "\n");
434 }
435 }
436 fprintf(bdf_fp, "ENDCHAR\n");
437 return 0;
438 }
439
440
441 /*EOF*/