unibdf2hex: preserve line width
[unifont.git] / src / unibdf2hex.c
1 /*
2    unibdf2hex - program to convert a BDF file into a unifont.hex file.
3
4    Author: Paul Hardy, January 2008
5
6    Note: currently this has hard-coded code points for glyphs extracted
7    from Wen Quan Yi to create the Unifont source file "wqy.hex".
8
9    Copyright (C) 2008, 2013 Paul Hardy
10
11    LICENSE:
12
13       This program is free software: you can redistribute it and/or modify
14       it under the terms of the GNU General Public License as published by
15       the Free Software Foundation, either version 2 of the License, or
16       (at your option) any later version.
17    
18       This program is distributed in the hope that it will be useful,
19       but WITHOUT ANY WARRANTY; without even the implied warranty of
20       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
21       GNU General Public License for more details.
22    
23       You should have received a copy of the GNU General Public License
24       along with this program.  If not, see <http://www.gnu.org/licenses/>.
25 */
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #define MAXBUF 256
32
33
34 int
35 main()
36 {
37    int i;
38    int rownum;  /* number of lines we output in bitmap */
39    int thispoint;
40    char inbuf[MAXBUF];
41    int bbxx, bbxy, bbxxoff, bbxyoff;
42
43    int descent=2; /* font descent wrt baseline */
44    int startrow;  /* row to start glyph        */
45    int rowlen;    /* number of digits per row  */
46    unsigned rowout;
47
48    while (fgets (inbuf, MAXBUF - 1, stdin)) {
49       if (strncmp (inbuf, "ENCODING ", 9)) continue;
50       sscanf (&inbuf[9], "%d", &thispoint); /* get code point */
51
52       while (fgets (inbuf, MAXBUF - 1, stdin)) {
53          if (!strncmp (inbuf, "DWIDTH ", 7)) break;
54       }
55       sscanf (&inbuf[7], "%d", &rowlen);
56       rowlen >>= 2;
57
58       /* Read bounding box values from BBX line */
59       while (fgets (inbuf, MAXBUF - 1, stdin)) {
60         if (!strncmp (inbuf, "BBX ", 4)) break;
61       }
62       sscanf (&inbuf[4], "%d %d %d %d", &bbxx, &bbxy, &bbxxoff, &bbxyoff);
63
64       /* Find BITMAP start */
65       while (fgets (inbuf, MAXBUF - 1, stdin)) {
66         if (!strncmp (inbuf, "BITMAP", 6)) break;
67       }
68
69       fprintf (stdout, "%04X:", thispoint);
70       rownum = 0;
71       /* Print initial blank rows */
72       startrow = descent + bbxyoff + bbxy;
73
74       /* Force everything to 16 pixels wide */
75       for (i = 16; i > startrow; i--) {
76          fprintf (stdout, "%0*d", rowlen, 0);
77          rownum++;
78       }
79       /* Copy bitmap until END */
80       while (fgets (inbuf, MAXBUF - 1, stdin)) {
81          if (!strncmp (inbuf, "END", 3)) break;
82          sscanf (inbuf, "%X", &rowout);
83          if (rowlen >= 4 && bbxx <= 8) rowout <<= 8;  /* force 8x16 input to 16x16 grid */
84          rowout >>= bbxxoff;
85          fprintf (stdout, "%0*X", rowlen, rowout);
86          rownum++;
87       }
88
89       /* Pad empty lines until glyph has sufficient height */
90       while (rownum < 16) {
91          fprintf (stdout, "%0*d", rowlen, 0);
92          rownum++;
93       }
94       fprintf (stdout,"\n");
95    }
96    exit (0);
97 }