Daniel Stenberg | 22fd075 | 2004-05-21 16:53:06 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * bdf2bmp -- output all glyphs in a bdf-font to a bmp-image-file |
| 3 | * version: 0.6 |
| 4 | * date: Wed Jan 10 23:59:03 2001 |
| 5 | * author: ITOU Hiroki (itouh@lycos.ne.jp) |
| 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * Copyright (c) 2000,2001 ITOU Hiroki |
| 10 | * All rights reserved. |
| 11 | * |
| 12 | * Redistribution and use in source and binary forms, with or without |
| 13 | * modification, are permitted provided that the following conditions |
| 14 | * are met: |
| 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. |
| 20 | * |
| 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 |
| 31 | * SUCH DAMAGE. |
| 32 | */ |
| 33 | |
| 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 |
| 46 | |
| 47 | #define VERBOSE |
| 48 | |
| 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() */ |
| 56 | |
| 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 */ |
| 62 | |
| 63 | #ifdef DEBUG |
| 64 | #define d_printf(message,arg) printf(message,arg) |
| 65 | #else /* not DEBUG */ |
| 66 | #define d_printf(message,arg) |
| 67 | #endif /* DEBUG */ |
| 68 | |
| 69 | #ifdef VERBOSE |
| 70 | #define v_printf(message,arg) printf(message,arg) |
| 71 | #else /* not VERBOSE */ |
| 72 | #define v_printf(message,arg) |
| 73 | #endif /* VERBOSE */ |
| 74 | |
| 75 | /* macro */ |
| 76 | #define STOREBITMAP() if(flagBitmap == ON){\ |
| 77 | memcpy(nextP, sP, length);\ |
| 78 | nextP += length;\ |
| 79 | } |
| 80 | |
| 81 | struct boundingbox{ |
| 82 | int w; /* width (pixel) */ |
| 83 | int h; /* height */ |
| 84 | int offx; /* offset y (pixel) */ |
| 85 | int offy; /* offset y */ |
| 86 | }; |
| 87 | |
| 88 | /* global var */ |
| 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 */ |
| 93 | |
| 94 | /* func prototype */ |
| 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[]); |
| 103 | |
| 104 | |
| 105 | |
| 106 | /* |
| 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 |
| 110 | */ |
| 111 | void checkEndian(void){ |
| 112 | unsigned long ulong = 0x12345678; |
| 113 | unsigned char *ucharP; |
| 114 | |
| 115 | ucharP = (unsigned char *)(&ulong); |
| 116 | if(*ucharP == 0x78){ |
| 117 | d_printf("LSB 0x%x\n", *ucharP); |
| 118 | endian = 1; |
| 119 | }else{ |
| 120 | d_printf("MSB 0x%x\n", *ucharP); |
| 121 | endian = 0; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | |
| 126 | |
| 127 | /* |
| 128 | * write to disk; with arranging LSBfirst(LittleEndian) byte order, |
| 129 | * because BMP-file is defined as LSB-first |
| 130 | */ |
| 131 | void dwrite(const void *varP, int n, FILE *outP){ |
| 132 | const unsigned char *p = varP; |
| 133 | int i; |
| 134 | unsigned char tmp; |
| 135 | |
| 136 | if(endian == 1){ |
| 137 | /* LSB; write without arranging */ |
| 138 | for(i=0; i<n; i++){ |
| 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"); |
| 142 | exit(EXIT_FAILURE); |
| 143 | } |
| 144 | } |
| 145 | }else{ |
| 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"); |
| 151 | exit(EXIT_FAILURE); |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | |
| 158 | |
| 159 | /* |
| 160 | * 3. read bitmapAREA(onMemory) and write bmpFile with adding spacing |
| 161 | * BMP-file: noCompression(BI_RGB), 8bitColor, Windows-Win32 type |
| 162 | */ |
| 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 */ |
| 169 | unsigned long ulong; |
| 170 | unsigned short ushort; |
| 171 | signed long slong; |
| 172 | unsigned char uchar; |
| 173 | int i,x,y,g,tmp; |
| 174 | int rowchar; /* number of row glyphs */ |
| 175 | int bx, by; |
| 176 | |
| 177 | /* bmp-image width */ |
| 178 | bmpw = (font.w+spacing)*colchar + spacing; |
| 179 | |
| 180 | /* bmp-image height */ |
| 181 | rowchar = (chars/colchar) + (chars%colchar!=0); |
| 182 | bmph = (font.h+spacing)*rowchar + spacing; |
| 183 | |
| 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); |
| 188 | |
| 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); |
| 193 | |
| 194 | |
| 195 | /* |
| 196 | * BITMAPFILEHEADER struct |
| 197 | */ |
| 198 | ushort = 0x4d42; /* 4d = 'M', 42 = 'B' */ |
| 199 | dwrite(&ushort, sizeof(ushort), bmpP); |
| 200 | ulong = bmpTotalSize; |
| 201 | dwrite(&ulong, sizeof(ulong), bmpP); |
| 202 | ushort = 0x00; |
| 203 | dwrite(&ushort, sizeof(ushort), bmpP); /* reserved as 0 */ |
| 204 | dwrite(&ushort, sizeof(ushort), bmpP); /* reserved as 0 */ |
| 205 | |
| 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); |
| 209 | |
| 210 | |
| 211 | /* |
| 212 | * BITMAPINFOHEADER struct |
| 213 | */ |
| 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); |
| 236 | |
| 237 | /* |
| 238 | * RGBQUAD[256]: color palette |
| 239 | */ |
| 240 | /* palette[0]: background of glyphs */ |
| 241 | uchar = 0xff; |
| 242 | dwrite(&uchar, sizeof(uchar), bmpP); /* rgbBlue: B */ |
| 243 | dwrite(&uchar, sizeof(uchar), bmpP); /* rgbGreen: G */ |
| 244 | dwrite(&uchar, sizeof(uchar), bmpP); /* rgbRed: R */ |
| 245 | uchar = 0; |
| 246 | dwrite(&uchar, sizeof(uchar), bmpP); /* rgbReserved: must be 0 */ |
| 247 | |
| 248 | /* palette[1]: foreground of glyphs */ |
| 249 | uchar = 0; |
| 250 | for(i=0; i<4; i++) |
| 251 | dwrite(&uchar, sizeof(uchar), bmpP); /* palette[1]: #000000 */ |
| 252 | |
| 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 */ |
| 260 | uchar = 0; |
| 261 | dwrite(&uchar, sizeof(uchar), bmpP); /* must be 0 */ |
| 262 | |
| 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 */ |
| 270 | uchar = 0; |
| 271 | dwrite(&uchar, sizeof(uchar), bmpP); /* must be 0 */ |
| 272 | |
| 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 */ |
| 280 | uchar = 0; |
| 281 | dwrite(&uchar, sizeof(uchar), bmpP); /* must be 0 */ |
| 282 | |
| 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); |
| 290 | } |
| 291 | |
| 292 | /* |
| 293 | * IMAGE DATA array |
| 294 | */ |
| 295 | for(y=bmph-1; y>=0; y--){ |
| 296 | for(x=0; x<bmpw+bmppad; x++){ |
| 297 | if(x>=bmpw){ |
| 298 | /* padding: long(4bytes) aligned */ |
| 299 | uchar = 0; /* must pad with 0 */ |
| 300 | dwrite(&uchar, sizeof(uchar), bmpP); |
| 301 | }else{ |
| 302 | if( (y%(font.h+spacing)<spacing) || (x%(font.w+spacing)<spacing) ){ |
| 303 | /* spacing */ |
| 304 | uchar = 2; /* fill palette[2] */ |
| 305 | dwrite(&uchar, sizeof(uchar), bmpP); |
| 306 | }else{ |
| 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] */ |
| 315 | }else |
| 316 | uchar = *( bitmapP + tmp); |
| 317 | dwrite(&uchar, sizeof(uchar), bmpP); |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | return; |
| 323 | } |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | /* |
| 329 | * 2. transfer bdf-font-file to bitmapAREA(onMemory) one glyph by one |
| 330 | */ |
| 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" |
| 335 | }; |
| 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 */ |
| 340 | char *tmpP; |
| 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; |
| 346 | int i,j,x,y; |
| 347 | |
| 348 | /* |
| 349 | * 2.1) change hexadecimal strings to a bitmap of glyph( called bitA) |
| 350 | */ |
| 351 | tmpP = strstr(glyphP, "\n"); |
| 352 | if(tmpP == NULL){ |
| 353 | /* if there is BITMAP\nENDCHAR in a given bdf-file */ |
| 354 | *glyphP = '0'; |
| 355 | *(glyphP+1) = '0'; |
| 356 | *(glyphP+2) = '\n'; |
| 357 | tmpP = glyphP + 2; |
| 358 | sizeglyphP = 3; |
| 359 | } |
| 360 | hexlen = tmpP - glyphP; |
| 361 | bitAw = hexlen * 4; |
| 362 | bitAh = sizeglyphP / (hexlen+1); |
| 363 | bitAP = malloc(bitAw * bitAh); /* address of bitA */ |
| 364 | if(bitAP == NULL){ |
| 365 | printf("error bitA malloc\n"); |
| 366 | exit(EXIT_FAILURE); |
| 367 | } |
| 368 | for(i=0,x=0,y=0; i<sizeglyphP; i++){ |
| 369 | if(glyphP[i] == '\n'){ |
| 370 | x=0; y++; |
| 371 | }else{ |
| 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"); |
| 378 | exit(EXIT_FAILURE); |
| 379 | }else{ |
| 380 | *(bitAP + y*bitAw + x) = binP[j] - '0'; |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | /* |
| 387 | * 2.2)make another bitmap area(called bitB) |
| 388 | * bitB is sized to FONTBOUNDINGBOX |
| 389 | */ |
| 390 | bitBP = malloc(font.w * font.h); /* address of bitB */ |
| 391 | if(bitBP == NULL){ |
| 392 | printf("error bitB malloc\n"); |
| 393 | exit(EXIT_FAILURE); |
| 394 | } |
| 395 | for(i=0; i<font.h; i++){ |
| 396 | for(j=0; j<font.w; j++){ |
| 397 | if(dwflag == OFF){ |
| 398 | /* all in boundingbox: palette[0] */ |
| 399 | *(bitBP + i*font.w + j) = 0; |
| 400 | }else{ |
| 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; |
| 406 | }else{ |
| 407 | /* under baseline: palette[4] */ |
| 408 | *(bitBP + i*font.w + j) = 4; |
| 409 | } |
| 410 | }else{ |
| 411 | /* in dwidth: palette[0] */ |
| 412 | *(bitBP + i*font.w + j) = 0; |
| 413 | } |
| 414 | } |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | /* |
| 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] |
| 422 | */ |
| 423 | offleft = (-1)*font.offx + glyph.offx; |
| 424 | /* offright = font.w - glyph.w - offleft; */ |
| 425 | |
| 426 | offbottom = (-1)*font.offy + glyph.offy; |
| 427 | offtop = font.h - glyph.h - offbottom; |
| 428 | |
| 429 | for(i=0; i<font.h; i++){ |
| 430 | if( i<offtop || i>=offtop+glyph.h ) |
| 431 | ; /* do nothing */ |
| 432 | else |
| 433 | for(j=0; j<font.w; j++) |
| 434 | if( j<offleft || j>=offleft+glyph.w ) |
| 435 | ; /* do nothing */ |
| 436 | else |
| 437 | *(bitBP + i*font.w + j) = *(bitAP + (i-offtop)*bitAw + (j-offleft)); |
| 438 | } |
| 439 | |
| 440 | /* |
| 441 | * 2.4) copy bitB to bitmapAREA |
| 442 | */ |
| 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); |
| 446 | |
| 447 | nowchar++; |
| 448 | free(bitAP); |
| 449 | free(bitBP); |
| 450 | } |
| 451 | |
| 452 | |
| 453 | /* |
| 454 | * read oneline from textfile |
| 455 | */ |
| 456 | int getline(char* lineP, int max, FILE* inputP){ |
| 457 | if (fgets(lineP, max, inputP) == NULL) |
| 458 | return 0; |
| 459 | else |
| 460 | return strlen(lineP); /* fgets returns strings included '\n' */ |
| 461 | } |
| 462 | |
| 463 | |
| 464 | /* |
| 465 | * 1. read BDF-file and transfer to assignBitmap() |
| 466 | */ |
| 467 | unsigned char *readBdfFile(unsigned char *bitmapP, FILE *readP){ |
| 468 | int i; |
| 469 | int length; |
| 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 */ |
| 479 | |
| 480 | while(1){ |
| 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"); |
| 485 | exit(EXIT_FAILURE); |
| 486 | } |
| 487 | if(length == 0) |
| 488 | break; /* escape from while-loop */ |
| 489 | |
| 490 | /* remove carriage-return(CR) */ |
| 491 | for(i=0; i<length; i++){ |
| 492 | if(sP[i] == '\r'){ |
| 493 | sP[i] = '\n'; |
| 494 | length--; |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | /* classify from the top character of sP */ |
| 499 | switch(sP[0]){ |
| 500 | case 'S': |
| 501 | if(bdfflag == OFF){ |
| 502 | /* top of the bdf-file */ |
| 503 | if(strncmp(sP, "STARTFONT ", 10) == 0){ |
| 504 | bdfflag = ON; |
| 505 | d_printf("startfont exists %d\n", bdfflag); |
| 506 | }else{ |
| 507 | /* given input-file is not a bdf-file */ |
| 508 | printf("error: input-file is not a bdf file\n"); |
| 509 | exit(EXIT_FAILURE); |
| 510 | } |
| 511 | } |
| 512 | break; |
| 513 | case 'F': |
| 514 | if(strncmp(sP, "FONTBOUNDINGBOX ", 16) == 0){ |
| 515 | /* 16 means no comparing '\0' */ |
| 516 | |
| 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 */ |
| 521 | font.w = atoi(tokP); |
| 522 | tokP += (strlen(tokP)+1);/* height in FONTBOUNDINGBOX */ |
| 523 | tokP = strtok(tokP, " "); |
| 524 | font.h = atoi(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); |
| 535 | }else |
| 536 | STOREBITMAP(); |
| 537 | break; |
| 538 | case 'C': |
| 539 | if(strncmp(sP, "CHARS ", 6) == 0){ |
| 540 | /* get chars */ |
| 541 | tokP = strtok(sP, " "); |
| 542 | tokP += (strlen(tokP)+1); |
| 543 | tokP = strtok(tokP, "\n"); |
| 544 | chars = atoi(tokP); |
| 545 | v_printf(" Total glyphs = %d\n",chars); |
| 546 | cnt=0; |
| 547 | |
| 548 | /* allocate bitmapAREA */ |
| 549 | bitmapP = (unsigned char*)malloc(chars * font.h * font.w ); |
| 550 | if(bitmapP == NULL){ |
| 551 | printf("error malloc\n"); |
| 552 | exit(EXIT_FAILURE); |
| 553 | } |
| 554 | }else |
| 555 | STOREBITMAP(); |
| 556 | break; |
| 557 | case 'D': |
| 558 | if(strncmp(sP, "DWIDTH ", 7) == 0){ |
| 559 | /* get dw */ |
| 560 | tokP = strtok(sP, " "); |
| 561 | tokP += (strlen(tokP)+1); |
| 562 | tokP = strtok(tokP, " "); |
| 563 | dw = atoi(tokP); |
| 564 | }else |
| 565 | STOREBITMAP(); |
| 566 | break; |
| 567 | case 'B': |
| 568 | if(strncmp(sP, "BITMAP", 6) == 0){ |
| 569 | /* allocate glyphP */ |
| 570 | glyphP = (char*)malloc(font.w*font.h); /* allocate more room */ |
| 571 | if(glyphP == NULL){ |
| 572 | printf("error malloc bdf\n"); |
| 573 | exit(EXIT_FAILURE); |
| 574 | } |
| 575 | memset(glyphP, 0, font.w*font.h); /* zero clear */ |
| 576 | nextP = glyphP; |
| 577 | flagBitmap = ON; |
| 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); */ |
| 597 | }else |
| 598 | STOREBITMAP(); |
| 599 | break; |
| 600 | case 'E': |
| 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); |
| 605 | flagBitmap = OFF; |
| 606 | free(glyphP); |
| 607 | cnt++; |
| 608 | }else |
| 609 | STOREBITMAP(); |
| 610 | break; |
| 611 | default: |
| 612 | STOREBITMAP(); |
| 613 | break; |
| 614 | }/* switch */ |
| 615 | }/* while */ |
| 616 | /* 'break' goto here */ |
| 617 | return bitmapP; |
| 618 | } |
| 619 | |
| 620 | |
| 621 | /* |
| 622 | * |
| 623 | */ |
| 624 | void printhelp(void){ |
| 625 | printf("bdf2bmp version 0.6\n"); |
| 626 | printf("Usage: bdf2bmp [-option] input-bdf-file output-bmp-file\n"); |
| 627 | printf("Option:\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"); |
| 636 | exit(EXIT_FAILURE); |
| 637 | } |
| 638 | |
| 639 | |
| 640 | |
| 641 | /* |
| 642 | * |
| 643 | */ |
| 644 | int main(int argc, char *argv[]){ |
| 645 | FILE *readP; |
| 646 | FILE *writeP; |
| 647 | char readFilename[FILENAME_CHARMAX] = "input.bdf"; |
| 648 | char writeFilename[FILENAME_CHARMAX] = "output.bmp"; |
| 649 | int i, j, tmp, n, dst, c; |
| 650 | char *sP; |
| 651 | unsigned char *bitmapP = NULL; /* address of bitmapAREA */ |
| 652 | int spacing = 2; /* breadth of spacing (default 2) */ |
| 653 | int flag; |
| 654 | int colchar = 32; /* number of columns(horizontal) (default 32) */ |
| 655 | char paramP[PARAM_MAX][LINE_CHARMAX]; /* parameter strings */ |
| 656 | int iflag = OFF; |
| 657 | struct stat fileinfo; |
| 658 | |
| 659 | /* |
| 660 | * deal with arguments |
| 661 | */ |
| 662 | if(argc < 2){ |
| 663 | /* printf("error: not enough arguments\n"); */ |
| 664 | printhelp(); |
| 665 | } |
| 666 | |
| 667 | /* formatting arguments */ |
| 668 | sP = calloc(LINE_CHARMAX, sizeof(char)); |
| 669 | if(sP == NULL){ |
| 670 | printf("error\n"); |
| 671 | exit(1); |
| 672 | } |
| 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' || |
| 678 | argv[i][j]=='i' || |
| 679 | argv[i][j]=='h' || |
| 680 | argv[i][j]=='v') |
| 681 | { |
| 682 | *(sP+dst) = '-'; dst++; |
| 683 | *(sP+dst) = argv[i][j]; dst++; |
| 684 | *(sP+dst) = '\0'; |
| 685 | strcpy(paramP[n], sP); dst=0; n++; |
| 686 | memset(sP,0,LINE_CHARMAX); |
| 687 | if(n >= PARAM_MAX){ |
| 688 | printf("error: too many arguments\n"); |
| 689 | exit(EXIT_FAILURE); |
| 690 | } |
| 691 | |
| 692 | }else if( (argv[i][j]=='s') || |
| 693 | (argv[i][j]=='c')) |
| 694 | { |
| 695 | *(sP+dst) = '-'; dst++; |
| 696 | *(sP+dst) = argv[i][j]; dst++; |
| 697 | }else if( isdigit(argv[i][j]) == 0 ){ |
| 698 | /* not [0-9] */ |
| 699 | printf("error: invalid option -- '%c'\n", argv[i][j]); |
| 700 | exit(EXIT_FAILURE); |
| 701 | }else if( argv[i][j+1] == '\0' ){ |
| 702 | *(sP+dst) = argv[i][j]; dst++; |
| 703 | *(sP+dst) = '\0'; |
| 704 | strcpy(paramP[n], sP); dst=0; n++; |
| 705 | if(n >= PARAM_MAX){ |
| 706 | printf("error: too many arguments\n"); |
| 707 | exit(EXIT_FAILURE); |
| 708 | } |
| 709 | }else{ |
| 710 | *(sP+dst) = argv[i][j]; dst++; |
| 711 | } |
| 712 | } |
| 713 | }else{ |
| 714 | /* not command-line options */ |
| 715 | for(j=0; j<(int)strlen(argv[i]); j++){ |
| 716 | *(sP+dst) = argv[i][j]; dst++; |
| 717 | } |
| 718 | *(sP+dst) = '\0'; |
| 719 | strcpy(paramP[n], sP); dst=0; n++; |
| 720 | memset(sP,0,LINE_CHARMAX); |
| 721 | if(n >= PARAM_MAX){ |
| 722 | printf("error: too many arguments\n"); |
| 723 | exit(EXIT_FAILURE); |
| 724 | } |
| 725 | } |
| 726 | } |
| 727 | free(sP); |
| 728 | |
| 729 | /* interpretting arguments */ |
| 730 | for(i=0, flag=0; i<n; i++){ |
| 731 | switch( paramP[i][0] ){ |
| 732 | case '-': |
| 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') |
| 738 | dwflag = ON; |
| 739 | else if(paramP[i][1] == 'i') |
| 740 | iflag = ON; |
| 741 | else if( (paramP[i][1]=='v') || (paramP[i][1]=='h')) |
| 742 | printhelp(); |
| 743 | break; |
| 744 | default: |
| 745 | if(flag == 0){ |
| 746 | strcpy(readFilename, paramP[i]); |
| 747 | flag ++; |
| 748 | }else{ |
| 749 | strcpy(writeFilename, paramP[i]); |
| 750 | if(strcmp(readFilename, writeFilename) == 0){ |
| 751 | printf("error: input-filename and output-filename are same\n"); |
| 752 | exit(EXIT_FAILURE); |
| 753 | } |
| 754 | flag ++; |
| 755 | } |
| 756 | break; |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | if(flag < 2){ |
| 761 | printf("error: not enough arguments\n"); |
| 762 | printf("Usage: bdf2bmp [-option] input-bdf-file output-bmp-file\n"); |
| 763 | exit(EXIT_FAILURE); |
| 764 | } |
| 765 | |
| 766 | /* colchar is limited from 1 to 1024 */ |
| 767 | if(colchar < 1) |
| 768 | colchar = 1; |
| 769 | else if(colchar > 1024) |
| 770 | colchar = 1024; |
| 771 | |
| 772 | /* spacing is limited from 0 to 32 */ |
| 773 | if(spacing < 0) |
| 774 | spacing = 0; |
| 775 | else if(spacing > 32) |
| 776 | spacing = 32; |
| 777 | |
| 778 | /* checkEndian */ |
| 779 | checkEndian(); |
| 780 | |
| 781 | /* |
| 782 | * prepare to read&write files |
| 783 | */ |
| 784 | readP = fopen(readFilename, "r"); |
| 785 | if(readP == NULL){ |
| 786 | printf("bdf2bmp: '%s' does not exist\n", readFilename); |
| 787 | exit(EXIT_FAILURE); |
| 788 | } |
| 789 | /* Does writeFilename already exist? */ |
| 790 | if((iflag==ON) && (stat(writeFilename, &fileinfo)==0)){ |
| 791 | fprintf(stderr, "bdf2bmp: overwrite '%s'? ", writeFilename); |
| 792 | c = fgetc(stdin); |
| 793 | if((c=='y') || (c=='Y')) |
| 794 | ; /* go next */ |
| 795 | else |
| 796 | /* printf("not overwrite\n"); */ |
| 797 | exit(EXIT_FAILURE); |
| 798 | } |
| 799 | writeP=fopen(writeFilename, "wb"); |
| 800 | if(writeP == NULL){ |
| 801 | printf("error: cannot write '%s'\n", writeFilename); |
| 802 | exit(EXIT_FAILURE); |
| 803 | } |
| 804 | |
| 805 | |
| 806 | /* read bdf-font-file */ |
| 807 | bitmapP = readBdfFile(bitmapP, readP); |
| 808 | fclose(readP); |
| 809 | |
| 810 | /* write bmp-image-file */ |
| 811 | writeBmpFile(bitmapP, spacing, colchar, writeP); |
| 812 | tmp = fclose(writeP); |
| 813 | if(tmp == EOF){ |
| 814 | printf("error: cannot write '%s'\n", writeFilename); |
| 815 | free(bitmapP); |
| 816 | exit(EXIT_FAILURE); |
| 817 | } |
| 818 | |
| 819 | free(bitmapP); |
| 820 | return EXIT_SUCCESS; |
| 821 | } |