2 * bdf2bmp -- output all glyphs in a bdf-font to a bmp-image-file
4 * date: Wed Jan 10 23:59:03 2001
5 * author: ITOU Hiroki (itouh@lycos.ne.jp)
9 * Copyright (c) 2000,2001 ITOU Hiroki
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 /* modify if you want; color of spacing */
35 #define COLOR_SPACING_RED 0x9a
36 #define COLOR_SPACING_GREEN 0x9a
37 #define COLOR_SPACING_BLUE 0xbd
38 /* modify if you want; out-of-dwidth over baseline */
39 #define COLOR_OVERBL_RED 0xca
40 #define COLOR_OVERBL_GREEN 0xca
41 #define COLOR_OVERBL_BLUE 0xd8
42 /* modify if you want; out-of-dwidth under baseline */
43 #define COLOR_UNDERBL_RED 0xde
44 #define COLOR_UNDERBL_GREEN 0xde
45 #define COLOR_UNDERBL_BLUE 0xe7
49 #include <stdio.h> /* printf(), fopen(), fwrite() */
50 #include <stdlib.h> /* malloc(), EXIT_SUCCESS, strtol(), exit() */
51 #include <string.h> /* strcmp(), strcpy() */
52 #include <limits.h> /* strtol() */
53 #include <sys/stat.h> /* stat() */
54 #include <sys/types.h> /* stat ? */
55 #include <ctype.h> /* isdigit() */
57 #define LINE_CHARMAX 1000 /* number of max characters in bdf-font-file; number is without reason */
58 #define FILENAME_CHARMAX 256 /* number of max characters in filenames; number is without reason */
59 #define ON 1 /* number is without reason; only needs the difference to OFF */
60 #define OFF 0 /* number is without reason; only needs the difference to ON */
61 #define PARAM_MAX 10 /* max number of parameters */
64 #define d_printf(message,arg) printf(message,arg)
66 #define d_printf(message,arg)
70 #define v_printf(message,arg) printf(message,arg)
71 #else /* not VERBOSE */
72 #define v_printf(message,arg)
76 #define STOREBITMAP() if(flagBitmap == ON){\
77 memcpy(nextP, sP, length);\
82 int w
; /* width (pixel) */
84 int offx
; /* offset y (pixel) */
85 int offy
; /* offset y */
89 struct boundingbox font
; /* global boundingbox */
90 static int chars
; /* total number of glyphs in a bdf file */
91 static int dwflag
= ON
; /* device width; only used for proportional-fonts */
92 static int endian
; /* 0 = MSB, 1 = LSB */
95 void checkEndian(void);
96 void dwrite(const void *ptrP
, int n
, FILE *outP
);
97 void writeBmpFile(unsigned char *bitmapP
, int spacing
, int colchar
, FILE *bmpP
);
98 void assignBitmap(unsigned char *bitmapP
, char *glyphP
, int sizeglyphP
, struct boundingbox glyph
, int dw
);
99 int getline(char* lineP
, int max
, FILE* inputP
);
100 unsigned char *readBdfFile(unsigned char *bitmapP
, FILE *readP
);
101 void printhelp(void);
102 int main(int argc
, char *argv
[]);
107 * Is your-CPU-byte-order MSB or LSB?
108 * MSB .. Most Significant Byte first (BigEndian) e.g. PowerPC, SPARC
109 * LSB .. Least Significant Byte first (LittleEndian) e.g. Intel Pentium
111 void checkEndian(void){
112 unsigned long ulong
= 0x12345678;
113 unsigned char *ucharP
;
115 ucharP
= (unsigned char *)(&ulong
);
117 d_printf("LSB 0x%x\n", *ucharP
);
120 d_printf("MSB 0x%x\n", *ucharP
);
128 * write to disk; with arranging LSBfirst(LittleEndian) byte order,
129 * because BMP-file is defined as LSB-first
131 void dwrite(const void *varP
, int n
, FILE *outP
){
132 const unsigned char *p
= varP
;
137 /* LSB; write without arranging */
139 tmp
= fwrite(p
+i
, 1, sizeof(unsigned char), outP
);
140 if(tmp
!= sizeof(unsigned char)){
141 printf("error: cannot write an output-file\n");
146 /* MSB; write with arranging */
147 for(i
=n
-1; i
>=0; i
--){
148 tmp
= fwrite(p
+i
, 1, sizeof(unsigned char), outP
);
149 if(tmp
!= sizeof(unsigned char)){
150 printf("error: cannot write an output-file\n");
160 * 3. read bitmapAREA(onMemory) and write bmpFile with adding spacing
161 * BMP-file: noCompression(BI_RGB), 8bitColor, Windows-Win32 type
163 void writeBmpFile(unsigned char *bitmapP
, int spacing
, int colchar
, FILE *bmpP
){
164 long bmpw
; /* bmp-image width (pixel) */
165 long bmph
; /* bmp-image height (pixel) */
166 int bmppad
; /* number of padding pixels */
167 unsigned long bmpTotalSize
; /* bmp filesize (byte) */
168 /* bmp-lines needs to be long alined and padded with 0 */
170 unsigned short ushort
;
174 int rowchar
; /* number of row glyphs */
177 /* bmp-image width */
178 bmpw
= (font
.w
+spacing
)*colchar
+ spacing
;
180 /* bmp-image height */
181 rowchar
= (chars
/colchar
) + (chars
%colchar
!=0);
182 bmph
= (font
.h
+spacing
)*rowchar
+ spacing
;
184 v_printf(" BMP width = %d pixels\n", (int)bmpw
);
185 v_printf(" BMP height = %d pixels\n", (int)bmph
);
186 d_printf(" number of glyphs column=%d ", colchar
);
187 d_printf("row=%d\n", rowchar
);
189 bmppad
= ((bmpw
+ 3) / 4 * 4) - bmpw
;
190 bmpTotalSize
= (bmpw
+ bmppad
) * bmph
191 + sizeof(long)*11 + sizeof(short)*5 + sizeof(char)*4*256;
192 v_printf(" BMP filesize = %d bytes\n", (int)bmpTotalSize
);
196 * BITMAPFILEHEADER struct
198 ushort
= 0x4d42; /* 4d = 'M', 42 = 'B' */
199 dwrite(&ushort
, sizeof(ushort
), bmpP
);
200 ulong
= bmpTotalSize
;
201 dwrite(&ulong
, sizeof(ulong
), bmpP
);
203 dwrite(&ushort
, sizeof(ushort
), bmpP
); /* reserved as 0 */
204 dwrite(&ushort
, sizeof(ushort
), bmpP
); /* reserved as 0 */
206 /* bfOffBits: offset to image-data array */
207 ulong
= sizeof(long)*11 + sizeof(short)*5 + sizeof(char)*4*256;
208 dwrite(&ulong
, sizeof(ulong
), bmpP
);
212 * BITMAPINFOHEADER struct
214 ulong
= 40; /* when Windows-BMP, this is 40 */
215 dwrite(&ulong
, sizeof(ulong
), bmpP
);
216 slong
= bmpw
; /* biWidth */
217 dwrite(&slong
, sizeof(slong
), bmpP
);
218 slong
= bmph
; /* biHeight: down-top */
219 dwrite(&slong
, sizeof(slong
), bmpP
);
220 ushort
= 1; /* biPlanes: must be 1 */
221 dwrite(&ushort
, sizeof(ushort
), bmpP
);
222 ushort
= 8; /* biBitCount: 8bitColor */
223 dwrite(&ushort
, sizeof(ushort
), bmpP
);
224 ulong
= 0; /* biCompression: noCompression(BI_RGB) */
225 dwrite(&ulong
, sizeof(ulong
), bmpP
);
226 ulong
= 0; /* biSizeImage: when noComprsn, 0 is ok */
227 dwrite(&ulong
, sizeof(ulong
), bmpP
);
228 slong
= 0; /* biXPelsPerMeter: resolution x; 0 ok */
229 dwrite(&slong
, sizeof(slong
), bmpP
);
230 slong
= 0; /* biYPelsPerMeter: res y; 0 is ok */
231 dwrite(&slong
, sizeof(slong
), bmpP
);
232 ulong
= 0; /* biClrUsed: optimized color palette; not used */
233 dwrite(&ulong
, sizeof(ulong
), bmpP
);
234 ulong
= 0; /* biClrImportant: 0 is ok */
235 dwrite(&ulong
, sizeof(ulong
), bmpP
);
238 * RGBQUAD[256]: color palette
240 /* palette[0]: background of glyphs */
242 dwrite(&uchar
, sizeof(uchar
), bmpP
); /* rgbBlue: B */
243 dwrite(&uchar
, sizeof(uchar
), bmpP
); /* rgbGreen: G */
244 dwrite(&uchar
, sizeof(uchar
), bmpP
); /* rgbRed: R */
246 dwrite(&uchar
, sizeof(uchar
), bmpP
); /* rgbReserved: must be 0 */
248 /* palette[1]: foreground of glyphs */
251 dwrite(&uchar
, sizeof(uchar
), bmpP
); /* palette[1]: #000000 */
253 /* palette[2]: spacing */
254 uchar
= COLOR_SPACING_BLUE
;
255 dwrite(&uchar
, sizeof(uchar
), bmpP
); /* B */
256 uchar
= COLOR_SPACING_GREEN
;
257 dwrite(&uchar
, sizeof(uchar
), bmpP
); /* G */
258 uchar
= COLOR_SPACING_RED
;
259 dwrite(&uchar
, sizeof(uchar
), bmpP
); /* R */
261 dwrite(&uchar
, sizeof(uchar
), bmpP
); /* must be 0 */
263 /* palette[3]: out of dwidth over baseline */
264 uchar
= COLOR_OVERBL_BLUE
;
265 dwrite(&uchar
, sizeof(uchar
), bmpP
); /* B */
266 uchar
= COLOR_OVERBL_GREEN
;
267 dwrite(&uchar
, sizeof(uchar
), bmpP
); /* G */
268 uchar
= COLOR_OVERBL_RED
;
269 dwrite(&uchar
, sizeof(uchar
), bmpP
); /* R */
271 dwrite(&uchar
, sizeof(uchar
), bmpP
); /* must be 0 */
273 /* palette[4]: out of dwidth; under baseline */
274 uchar
= COLOR_UNDERBL_BLUE
;
275 dwrite(&uchar
, sizeof(uchar
), bmpP
); /* B */
276 uchar
= COLOR_UNDERBL_GREEN
;
277 dwrite(&uchar
, sizeof(uchar
), bmpP
); /* G */
278 uchar
= COLOR_UNDERBL_RED
;
279 dwrite(&uchar
, sizeof(uchar
), bmpP
); /* R */
281 dwrite(&uchar
, sizeof(uchar
), bmpP
); /* must be 0 */
283 /* palette[5] to palette[255]: not used */
284 for(i
=5; i
<256; i
++){
285 uchar
= 0x00; /* palette[5to255]: #000000 */
286 dwrite(&uchar
, sizeof(uchar
), bmpP
);
287 dwrite(&uchar
, sizeof(uchar
), bmpP
);
288 dwrite(&uchar
, sizeof(uchar
), bmpP
);
289 dwrite(&uchar
, sizeof(uchar
), bmpP
);
295 for(y
=bmph
-1; y
>=0; y
--){
296 for(x
=0; x
<bmpw
+bmppad
; x
++){
298 /* padding: long(4bytes) aligned */
299 uchar
= 0; /* must pad with 0 */
300 dwrite(&uchar
, sizeof(uchar
), bmpP
);
302 if( (y
%(font
.h
+spacing
)<spacing
) || (x
%(font
.w
+spacing
)<spacing
) ){
304 uchar
= 2; /* fill palette[2] */
305 dwrite(&uchar
, sizeof(uchar
), bmpP
);
307 /* read bitmapAREA & write bmpFile */
308 g
= (x
/(font
.w
+spacing
)) + (y
/(font
.h
+spacing
)*colchar
);
309 bx
= x
- (spacing
*(g
%colchar
)) - spacing
;
310 by
= y
- (spacing
*(g
/colchar
)) - spacing
;
311 tmp
= g
*(font
.h
*font
.w
) + (by
%font
.h
)*font
.w
+ (bx
%font
.w
);
312 if(tmp
>= chars
*font
.h
*font
.w
){
313 /* spacing over the last glyph */
314 uchar
= 2; /* fill palette[2] */
316 uchar
= *( bitmapP
+ tmp
);
317 dwrite(&uchar
, sizeof(uchar
), bmpP
);
329 * 2. transfer bdf-font-file to bitmapAREA(onMemory) one glyph by one
331 void assignBitmap(unsigned char *bitmapP
, char *glyphP
, int sizeglyphP
, struct boundingbox glyph
, int dw
){
332 static char *hex2binP
[]= {
333 "0000","0001","0010","0011","0100","0101","0110","0111",
334 "1000","1001","1010","1011","1100","1101","1110","1111"
336 int d
; /* decimal number translated from hexNumber */
337 int hexlen
; /* a line length(without newline code) */
338 char binP
[LINE_CHARMAX
]; /* binary strings translated from decimal number */
339 static int nowchar
= 0; /* number of glyphs handlled until now */
341 char tmpsP
[LINE_CHARMAX
];
342 int bitAh
, bitAw
; /* bitA width, height */
343 int offtop
, offbottom
, offleft
; /* glyph offset */
344 unsigned char *bitAP
;
345 unsigned char *bitBP
;
349 * 2.1) change hexadecimal strings to a bitmap of glyph( called bitA)
351 tmpP
= strstr(glyphP
, "\n");
353 /* if there is BITMAP\nENDCHAR in a given bdf-file */
360 hexlen
= tmpP
- glyphP
;
362 bitAh
= sizeglyphP
/ (hexlen
+1);
363 bitAP
= malloc(bitAw
* bitAh
); /* address of bitA */
365 printf("error bitA malloc\n");
368 for(i
=0,x
=0,y
=0; i
<sizeglyphP
; i
++){
369 if(glyphP
[i
] == '\n'){
372 sprintf(tmpsP
, "0x%c", glyphP
[i
]); /* get one character from hexadecimal strings */
373 d
= (int)strtol(tmpsP
,(char **)NULL
, 16);
374 strcpy(binP
, hex2binP
[d
]); /* change hexa strings to bin strings */
375 for(j
=0; j
<4; j
++,x
++){
376 if( bitAP
+y
*bitAw
+x
> bitAP
+bitAw
*bitAh
){
377 printf("error: bitA pointer\n");
380 *(bitAP
+ y
*bitAw
+ x
) = binP
[j
] - '0';
387 * 2.2)make another bitmap area(called bitB)
388 * bitB is sized to FONTBOUNDINGBOX
390 bitBP
= malloc(font
.w
* font
.h
); /* address of bitB */
392 printf("error bitB malloc\n");
395 for(i
=0; i
<font
.h
; i
++){
396 for(j
=0; j
<font
.w
; j
++){
398 /* all in boundingbox: palette[0] */
399 *(bitBP
+ i
*font
.w
+ j
) = 0;
401 /* show the baselines and widths of glyphs */
402 if( (j
< (-1)*font
.offx
) || (j
>= (-1)*font
.offx
+dw
)){
403 if(i
< font
.h
- (-1)*font
.offy
){
404 /* over baseline: palette[3] */
405 *(bitBP
+ i
*font
.w
+ j
) = 3;
407 /* under baseline: palette[4] */
408 *(bitBP
+ i
*font
.w
+ j
) = 4;
411 /* in dwidth: palette[0] */
412 *(bitBP
+ i
*font
.w
+ j
) = 0;
419 * 2.3) copy bitA inside BBX (smaller) to bitB (bigger)
420 * with offset-shifting;
421 * a scope beyond bitA is already filled with palette[0or3]
423 offleft
= (-1)*font
.offx
+ glyph
.offx
;
424 /* offright = font.w - glyph.w - offleft; */
426 offbottom
= (-1)*font
.offy
+ glyph
.offy
;
427 offtop
= font
.h
- glyph
.h
- offbottom
;
429 for(i
=0; i
<font
.h
; i
++){
430 if( i
<offtop
|| i
>=offtop
+glyph
.h
)
433 for(j
=0; j
<font
.w
; j
++)
434 if( j
<offleft
|| j
>=offleft
+glyph
.w
)
437 *(bitBP
+ i
*font
.w
+ j
) = *(bitAP
+ (i
-offtop
)*bitAw
+ (j
-offleft
));
441 * 2.4) copy bitB to bitmapAREA
443 for(i
=0; i
<font
.h
; i
++)
444 for(j
=0; j
<font
.w
; j
++)
445 *(bitmapP
+ (nowchar
*font
.w
*font
.h
) + (i
*font
.w
) + j
) = *(bitBP
+ i
*font
.w
+ j
);
454 * read oneline from textfile
456 int getline(char* lineP
, int max
, FILE* inputP
){
457 if (fgets(lineP
, max
, inputP
) == NULL
)
460 return strlen(lineP
); /* fgets returns strings included '\n' */
465 * 1. read BDF-file and transfer to assignBitmap()
467 unsigned char *readBdfFile(unsigned char *bitmapP
, FILE *readP
){
470 char sP
[LINE_CHARMAX
]; /* one line(strings) from bdf-font-file */
471 static int cnt
; /* only used in debugging: counter of appeared glyphs */
472 struct boundingbox glyph
; /* an indivisual glyph width, height,offset x,y */
473 int flagBitmap
= OFF
; /* this line is bitmap-data?(ON) or no?(OFF) */
474 char *tokP
; /* top address of a token from strings */
475 char *glyphP
= NULL
; /* bitmap-data(hexadecimal strings) */
476 char* nextP
= NULL
; /* address of writing next in glyphP */
477 int dw
= 0; /* dwidth */
478 static int bdfflag
= OFF
; /* the given bdf-file is valid or not */
481 length
= getline(sP
, LINE_CHARMAX
, readP
);
482 if((bdfflag
== OFF
) && (length
== 0)){
483 /* given input-file is not a bdf-file */
484 printf("error: input-file is not a bdf file\n");
488 break; /* escape from while-loop */
490 /* remove carriage-return(CR) */
491 for(i
=0; i
<length
; i
++){
498 /* classify from the top character of sP */
502 /* top of the bdf-file */
503 if(strncmp(sP
, "STARTFONT ", 10) == 0){
505 d_printf("startfont exists %d\n", bdfflag
);
507 /* given input-file is not a bdf-file */
508 printf("error: input-file is not a bdf file\n");
514 if(strncmp(sP
, "FONTBOUNDINGBOX ", 16) == 0){
515 /* 16 means no comparing '\0' */
517 /* get font.w, font.h, font.offx, and font.offy */
518 tokP
= strtok(sP
, " ");/* tokP addresses next space of FONTBOUNDINGBOX */
519 tokP
+= (strlen(tokP
)+1);/* tokP addresses top character of width in FONTBOUNDINGBOX */
520 tokP
= strtok(tokP
, " ");/* set NUL on space after width */
522 tokP
+= (strlen(tokP
)+1);/* height in FONTBOUNDINGBOX */
523 tokP
= strtok(tokP
, " ");
525 tokP
+= (strlen(tokP
)+1);
526 tokP
= strtok(tokP
, " ");
527 font
.offx
= atoi(tokP
);
528 tokP
+= (strlen(tokP
)+1);
529 tokP
= strtok(tokP
, "\n");
530 font
.offy
= atoi(tokP
);
531 d_printf("global glyph width=%dpixels ",font
.w
);
532 d_printf("height=%dpixels\n",font
.h
);
533 d_printf("global glyph offset x=%dpixels ",font
.offx
);
534 d_printf("y=%dpixels\n",font
.offy
);
539 if(strncmp(sP
, "CHARS ", 6) == 0){
541 tokP
= strtok(sP
, " ");
542 tokP
+= (strlen(tokP
)+1);
543 tokP
= strtok(tokP
, "\n");
545 v_printf(" Total glyphs = %d\n",chars
);
548 /* allocate bitmapAREA */
549 bitmapP
= (unsigned char*)malloc(chars
* font
.h
* font
.w
);
551 printf("error malloc\n");
558 if(strncmp(sP
, "DWIDTH ", 7) == 0){
560 tokP
= strtok(sP
, " ");
561 tokP
+= (strlen(tokP
)+1);
562 tokP
= strtok(tokP
, " ");
568 if(strncmp(sP
, "BITMAP", 6) == 0){
569 /* allocate glyphP */
570 glyphP
= (char*)malloc(font
.w
*font
.h
); /* allocate more room */
572 printf("error malloc bdf\n");
575 memset(glyphP
, 0, font
.w
*font
.h
); /* zero clear */
578 }else if(strncmp(sP
, "BBX ", 4) == 0){
579 /* get glyph.offx, glyph.offy, glyph.w, and glyph.h */
580 tokP
= strtok(sP
, " ");/* space after 'BBX' */
581 tokP
+= (strlen(tokP
)+1);/* top of width */
582 tokP
= strtok(tokP
, " ");/* set NUL on space after width */
583 glyph
.w
= atoi(tokP
);
584 tokP
+= (strlen(tokP
)+1);/* height */
585 tokP
= strtok(tokP
, " ");
586 glyph
.h
= atoi(tokP
);
587 tokP
+= (strlen(tokP
)+1);/* offx */
588 tokP
= strtok(tokP
, " ");
589 glyph
.offx
= atoi(tokP
);
590 tokP
+= (strlen(tokP
)+1);/* offy */
591 tokP
= strtok(tokP
, "\n");
592 glyph
.offy
= atoi(tokP
);
593 /* d_printf("glyph width=%dpixels ",glyph.w); */
594 /* d_printf("height=%dpixels\n",glyph.h); */
595 /* d_printf("glyph offset x=%dpixels ",glyph.offx); */
596 /* d_printf("y=%dpixels\n",glyph.offy); */
601 if(strncmp(sP
, "ENDCHAR", 7) == 0){
602 d_printf("\nglyph %d\n", cnt
);
603 d_printf("%s\n",glyphP
);
604 assignBitmap(bitmapP
, glyphP
, nextP
- glyphP
, glyph
, dw
);
616 /* 'break' goto here */
624 void printhelp(void){
625 printf("bdf2bmp version 0.6\n");
626 printf("Usage: bdf2bmp [-option] input-bdf-file output-bmp-file\n");
628 printf(" -sN spacing N pixels (default N=2)\n");
629 printf(" N value can range from 0 to 32\n");
630 printf(" -cN specifying N colomns in grid (default N=32)\n");
631 printf(" N value can range from 1 to 1024\n");
632 printf(" -w showing the baseline and the widths of glyphs\n");
633 printf(" with gray colors\n");
634 printf(" -i prompting whether to overwrite an existing file\n");
635 printf(" -h print help\n");
644 int main(int argc
, char *argv
[]){
647 char readFilename
[FILENAME_CHARMAX
] = "input.bdf";
648 char writeFilename
[FILENAME_CHARMAX
] = "output.bmp";
649 int i
, j
, tmp
, n
, dst
, c
;
651 unsigned char *bitmapP
= NULL
; /* address of bitmapAREA */
652 int spacing
= 2; /* breadth of spacing (default 2) */
654 int colchar
= 32; /* number of columns(horizontal) (default 32) */
655 char paramP
[PARAM_MAX
][LINE_CHARMAX
]; /* parameter strings */
657 struct stat fileinfo
;
660 * deal with arguments
663 /* printf("error: not enough arguments\n"); */
667 /* formatting arguments */
668 sP
= calloc(LINE_CHARMAX
, sizeof(char));
673 for(i
=1,dst
=0,n
=0; i
<argc
; i
++){
674 if(argv
[i
][0] == '-'){
675 /* command-line options */
676 for(j
=1; j
<(int)strlen(argv
[i
]); j
++){
677 if(argv
[i
][j
]=='w' ||
682 *(sP
+dst
) = '-'; dst
++;
683 *(sP
+dst
) = argv
[i
][j
]; dst
++;
685 strcpy(paramP
[n
], sP
); dst
=0; n
++;
686 memset(sP
,0,LINE_CHARMAX
);
688 printf("error: too many arguments\n");
692 }else if( (argv
[i
][j
]=='s') ||
695 *(sP
+dst
) = '-'; dst
++;
696 *(sP
+dst
) = argv
[i
][j
]; dst
++;
697 }else if( isdigit(argv
[i
][j
]) == 0 ){
699 printf("error: invalid option -- '%c'\n", argv
[i
][j
]);
701 }else if( argv
[i
][j
+1] == '\0' ){
702 *(sP
+dst
) = argv
[i
][j
]; dst
++;
704 strcpy(paramP
[n
], sP
); dst
=0; n
++;
706 printf("error: too many arguments\n");
710 *(sP
+dst
) = argv
[i
][j
]; dst
++;
714 /* not command-line options */
715 for(j
=0; j
<(int)strlen(argv
[i
]); j
++){
716 *(sP
+dst
) = argv
[i
][j
]; dst
++;
719 strcpy(paramP
[n
], sP
); dst
=0; n
++;
720 memset(sP
,0,LINE_CHARMAX
);
722 printf("error: too many arguments\n");
729 /* interpretting arguments */
730 for(i
=0, flag
=0; i
<n
; i
++){
731 switch( paramP
[i
][0] ){
733 if(paramP
[i
][1] == 's')
734 spacing
= atoi(¶mP
[i
][2]);
735 else if(paramP
[i
][1] == 'c')
736 colchar
= atoi(¶mP
[i
][2]);
737 else if(paramP
[i
][1] == 'w')
739 else if(paramP
[i
][1] == 'i')
741 else if( (paramP
[i
][1]=='v') || (paramP
[i
][1]=='h'))
746 strcpy(readFilename
, paramP
[i
]);
749 strcpy(writeFilename
, paramP
[i
]);
750 if(strcmp(readFilename
, writeFilename
) == 0){
751 printf("error: input-filename and output-filename are same\n");
761 printf("error: not enough arguments\n");
762 printf("Usage: bdf2bmp [-option] input-bdf-file output-bmp-file\n");
766 /* colchar is limited from 1 to 1024 */
769 else if(colchar
> 1024)
772 /* spacing is limited from 0 to 32 */
775 else if(spacing
> 32)
782 * prepare to read&write files
784 readP
= fopen(readFilename
, "r");
786 printf("bdf2bmp: '%s' does not exist\n", readFilename
);
789 /* Does writeFilename already exist? */
790 if((iflag
==ON
) && (stat(writeFilename
, &fileinfo
)==0)){
791 fprintf(stderr
, "bdf2bmp: overwrite '%s'? ", writeFilename
);
793 if((c
=='y') || (c
=='Y'))
796 /* printf("not overwrite\n"); */
799 writeP
=fopen(writeFilename
, "wb");
801 printf("error: cannot write '%s'\n", writeFilename
);
806 /* read bdf-font-file */
807 bitmapP
= readBdfFile(bitmapP
, readP
);
810 /* write bmp-image-file */
811 writeBmpFile(bitmapP
, spacing
, colchar
, writeP
);
812 tmp
= fclose(writeP
);
814 printf("error: cannot write '%s'\n", writeFilename
);