e333b47d9c5fe0dc4b28c2bbc43306d589a4bfa1
[unifont.git] / src / unibmp2hex.c
1 /*
2    unibmp2hex - program to turn a .bmp or .wbmp glyph matrix into a
3                 GNU Unifont hex glyph set of 256 characters
4
5    Synopsis: unibmp2hex [-iin_file.bmp] [-oout_file.hex] [-phex_page_num] [-w]
6
7
8    Author: Paul Hardy, unifoundry <at> unifoundry.com, December 2007
9    
10    
11    Copyright (C) 2007, 2008, 2013 Paul Hardy
12
13    LICENSE:
14
15       This program is free software: you can redistribute it and/or modify
16       it under the terms of the GNU General Public License as published by
17       the Free Software Foundation, either version 2 of the License, or
18       (at your option) any later version.
19
20       This program is distributed in the hope that it will be useful,
21       but WITHOUT ANY WARRANTY; without even the implied warranty of
22       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23       GNU General Public License for more details.
24
25       You should have received a copy of the GNU General Public License
26       along with this program.  If not, see <http://www.gnu.org/licenses/>.
27 */
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #define MAXBUF 256
34
35
36 unsigned hexdigit[16][4]; /* 32 bit representation of 16x8 0..F bitmap   */
37
38 unsigned uniplane=0;        /* Unicode plane number, 0..0xff ff ff       */
39 unsigned planeset=0;        /* =1: use plane specified with -p parameter */
40 unsigned flip=0;            /* =1 if we're transposing glyph matrix      */
41 unsigned forcewide=0;       /* =1 to set each glyph to 16 pixels wide    */
42
43 /* The six Unicode plane digits, from left-most (0) to right-most (5)    */
44 unsigned unidigit[6][4];
45
46
47
48 int
49 main (int argc, char *argv[])
50 {
51
52    int i, j, k;               /* loop variables                       */
53    unsigned char inchar;       /* temporary input character */
54    char header[MAXBUF];        /* input buffer for bitmap file header */
55    int wbmp=0; /* =0 for Windows Bitmap (.bmp); 1 for Wireless Bitmap (.wbmp) */
56    int fatal; /* =1 if a fatal error occurred */
57    int match; /* =1 if we're still matching a pattern, 0 if no match */
58    int empty1, empty2; /* =1 if bytes tested are all zeroes */
59    unsigned char thischar1[16], thischar2[16]; /* bytes of hex char */
60    int thisrow; /* index to point into thischar1[] and thischar2[] */
61    int tmpsum;  /* temporary sum to see if a character is blank */
62
63    unsigned char bitmap[17*32][18*32/8]; /* final bitmap */
64    char wide[65536]={65536 * 0}; /* 1 = force double width code point */
65
66    char *infile="", *outfile="";  /* names of input and output files */
67    FILE *infp, *outfp;      /* file pointers of input and output files */
68
69    if (argc > 1) {
70       for (i = 1; i < argc; i++) {
71          if (argv[i][0] == '-') {  /* this is an option argument */
72             switch (argv[i][1]) {
73                case 'i':  /* name of input file */
74                   infile = &argv[i][2];
75                   break;
76                case 'o':  /* name of output file */
77                   outfile = &argv[i][2];
78                   break;
79                case 'p':  /* specify a Unicode plane */
80                   sscanf (&argv[i][2], "%x", &uniplane); /* Get Unicode plane */
81                   planeset = 1; /* Use specified range, not what's in bitmap */
82                   break;
83                case 'w': /* force wide (16 pixels) for each glyph */
84                   forcewide = 1;
85                   break;
86                default:   /* if unrecognized option, print list and exit */
87                   fprintf (stderr, "\nSyntax:\n\n");
88                   fprintf (stderr, "   %s -p<Unicode_Page> ", argv[0]);
89                   fprintf (stderr, "-i<Input_File> -o<Output_File> -w\n\n");
90                   fprintf (stderr, "   -w specifies .wbmp output instead of ");
91                   fprintf (stderr, "default Windows .bmp output.\n\n");
92                   fprintf (stderr, "   -p is followed by 1 to 6 ");
93                   fprintf (stderr, "Unicode plane hex digits ");
94                   fprintf (stderr, "(default is Page 0).\n\n");
95                   fprintf (stderr, "\nExample:\n\n");
96                   fprintf (stderr, "   %s -p83 -iunifont.hex -ou83.bmp\n\n\n",
97                          argv[0]);
98                   exit (1);
99             }
100          }
101       }
102    }
103    /*
104       Make sure we can open any I/O files that were specified before
105       doing anything else.
106    */
107    if (strlen (infile) > 0) {
108       if ((infp = fopen (infile, "r")) == NULL) {
109          fprintf (stderr, "Error: can't open %s for input.\n", infile);
110          exit (1);
111       }
112    }
113    else {
114       infp = stdin;
115    }
116    if (strlen (outfile) > 0) {
117       if ((outfp = fopen (outfile, "w")) == NULL) {
118          fprintf (stderr, "Error: can't open %s for output.\n", outfile);
119          exit (1);
120       }
121    }
122    else {
123       outfp = stdout;
124    }
125    /*
126       Initialize selected code points for double width (16x16).
127       Double-width is forced in cases where a glyph (usually a combining
128       glyph) only occupies the left-hand side of a 16x16 grid, but must
129       be rendered as double-width to appear properly with other glyphs
130       in a given script.  If additions were made to a script after
131       Unicode 5.0, the Unicode version is given in parentheses after
132       the script name.
133    */
134    for (i = 0x0700; i <= 0x074F; i++) wide[i] = 1; /* Syriac                 */
135    for (i = 0x0800; i <= 0x083F; i++) wide[i] = 1; /* Samaritan (5.2)        */
136    for (i = 0x0900; i <= 0x0DFF; i++) wide[i] = 1; /* Indic                  */
137    for (i = 0x0F00; i <= 0x0FFF; i++) wide[i] = 1; /* Tibetan                */
138    for (i = 0x1100; i <= 0x11FF; i++) wide[i] = 1; /* Hangul Jamo            */
139    for (i = 0x1800; i <= 0x18AF; i++) wide[i] = 1; /* Mongolian              */
140    for (i = 0x1900; i <= 0x194F; i++) wide[i] = 1; /* Limbu                  */
141    for (i = 0x1980; i <= 0x19DF; i++) wide[i] = 1; /* New Tai Lue            */
142    for (i = 0x1A00; i <= 0x1A1F; i++) wide[i] = 1; /* Buginese               */
143    for (i = 0x1B00; i <= 0x1B7F; i++) wide[i] = 1; /* Balinese               */
144    for (i = 0x1B80; i <= 0x1BBF; i++) wide[i] = 1; /* Sundanese (5.1)        */
145    for (i = 0x1BC0; i <= 0x1BFF; i++) wide[i] = 1; /* Batak (6.0)            */
146    for (i = 0x1C00; i <= 0x1C4F; i++) wide[i] = 1; /* Lepcha (5.1)           */
147    for (i = 0x1CD0; i <= 0x1CFF; i++) wide[i] = 1; /* Vedic Extensions (5.2) */
148    for (i = 0x2E80; i <= 0xA4CF; i++) wide[i] = 1; /* CJK                    */
149    for (i = 0x1A20; i <= 0x1AAF; i++) wide[i] = 1; /* Tai Tham (5.2)         */
150    for (i = 0xA930; i <= 0xA95F; i++) wide[i] = 1; /* Rejang (5.1)           */
151    for (i = 0xA980; i <= 0xA9DF; i++) wide[i] = 1; /* Javanese (5.2)         */
152    for (i = 0xAA00; i <= 0xAA5F; i++) wide[i] = 1; /* Cham (5.1)             */
153    for (i = 0xAAE0; i <= 0xAAFF; i++) wide[i] = 1; /* Meetei Mayek Ext (6.0) */
154    for (i = 0xABC0; i <= 0xABFF; i++) wide[i] = 1; /* Meetei Mayek (5.2)     */
155
156    wide[0x303F] = 0; /* CJK half-space fill */
157
158    /*
159       Determine whether or not the file is a Microsoft Windows Bitmap file.
160       If it starts with 'B', 'M', assume it's a Windows Bitmap file.
161       Otherwise, assume it's a Wireless Bitmap file.
162
163       WARNING: There isn't much in the way of error checking here --
164       if you give it a file that wasn't first created by hex2bmp.c,
165       all bets are off.
166    */
167    fatal = 0;  /* assume everything is okay with reading input file */
168    if ((header[0] = fgetc (infp)) != EOF) {
169       if ((header[1] = fgetc (infp)) != EOF) {
170          if (header[0] == 'B' && header[1] == 'M') {
171             wbmp = 0; /* Not a Wireless Bitmap -- it's a Windows Bitmap */
172          }
173          else {
174             wbmp = 1; /* Assume it's a Wireless Bitmap */
175          }
176       }
177       else
178          fatal = 1;
179    }
180    else
181       fatal = 1;
182
183    if (fatal) {
184       fprintf (stderr, "Fatal error; end of input file.\n\n");
185       exit (1);
186    }
187    /*
188       If this is a Wireless Bitmap (.wbmp) format file,
189       skip the header and point to the start of the bitmap itself.
190    */
191    if (wbmp) {
192       for (i=2; i<6; i++)
193          header[i] = fgetc (infp);
194       /*
195          Now read the bitmap.
196       */
197       for (i=0; i < 32*17; i++) {
198          for (j=0; j < 32*18/8; j++) {
199             inchar = fgetc (infp);
200             bitmap[i][j] = ~inchar;  /* invert bits for proper color */
201          }
202       }
203    }
204    /*
205       Otherwise, this must be a Windows Bitmap file, because we check
206       for that first.  Skip past the header, but save it for possible
207       future use.
208    */
209    else {
210       for (i=2; i<0x3e; i++)
211          header[i] = fgetc (infp);
212       /*
213          Now read the bitmap.
214       */
215       for (i = 32*17-1; i >= 0; i--) {
216          for (j=0; j < 32*18/8; j++) {
217             inchar = fgetc (infp);
218             bitmap[i][j] = ~inchar; /* invert bits for proper color */
219          }
220       }
221    }
222    /*
223       We've read the entire file.  Now close the input file pointer.
224    */
225    fclose (infp);
226   /*
227       We now have the header portion in the header[] array,
228       and have the bitmap portion from top-to-bottom in the bitmap[] array.
229    */
230    /*
231       If no Unicode range (U+nnnnnn00 through U+nnnnnnFF) was specified
232       with a -p parameter, determine the range from the digits in the
233       bitmap itself.
234
235       Store bitmaps for the hex digit patterns that this file uses.
236    */
237    if (!planeset) {  /* If Unicode range not specified with -p parameter */
238       for (i = 0x0; i <= 0xF; i++) {  /* hex digit pattern we're storing */
239          for (j = 0; j < 4; j++) {
240             hexdigit[i][j]   =
241                ((unsigned)bitmap[32 * (i+1) + 4 * j + 8    ][6] << 24 ) |
242                ((unsigned)bitmap[32 * (i+1) + 4 * j + 8 + 1][6] << 16 ) |
243                ((unsigned)bitmap[32 * (i+1) + 4 * j + 8 + 2][6] <<  8 ) |
244                ((unsigned)bitmap[32 * (i+1) + 4 * j + 8 + 3][6]       );
245          }
246       }
247       /*
248          Read the Unicode plane digits into arrays for comparison, to
249          determine the upper four hex digits of the glyph addresses.
250       */
251       for (i = 0; i < 4; i++) {
252          for (j = 0; j < 4; j++) {
253             unidigit[i][j] =
254                ((unsigned)bitmap[32 * 0 + 4 * j + 8 + 1][i + 3] << 24 ) |
255                ((unsigned)bitmap[32 * 0 + 4 * j + 8 + 2][i + 3] << 16 ) |
256                ((unsigned)bitmap[32 * 0 + 4 * j + 8 + 3][i + 3] <<  8 ) |
257                ((unsigned)bitmap[32 * 0 + 4 * j + 8 + 4][i + 3]       );
258          }
259       }
260    
261       tmpsum = 0;
262       for (i = 4; i < 6; i++) {
263          for (j = 0; j < 4; j++) {
264             unidigit[i][j] =
265                ((unsigned)bitmap[32 * 1 + 4 * j + 8    ][i] << 24 ) |
266                ((unsigned)bitmap[32 * 1 + 4 * j + 8 + 1][i] << 16 ) |
267                ((unsigned)bitmap[32 * 1 + 4 * j + 8 + 2][i] <<  8 ) |
268                ((unsigned)bitmap[32 * 1 + 4 * j + 8 + 3][i]       );
269             tmpsum |= unidigit[i][j];
270          }
271       }
272       if (tmpsum == 0) {  /* the glyph matrix is transposed */
273          flip = 1;  /* note transposed order for processing glyphs in matrix */
274          /*
275             Get 5th and 6th hex digits by shifting first column header left by
276             1.5 columns, thereby shifting the hex digit right after the leading
277             "U+nnnn" page number.
278          */
279          for (i = 0x08; i < 0x18; i++) {
280             bitmap[i][7] = (bitmap[i][8] << 4) | ((bitmap[i][ 9] >> 4) & 0xf);
281             bitmap[i][8] = (bitmap[i][9] << 4) | ((bitmap[i][10] >> 4) & 0xf);
282          }
283          for (i = 4; i < 6; i++) {
284             for (j = 0; j < 4; j++) {
285                unidigit[i][j] =
286                   ((unsigned)bitmap[4 * j + 8 + 1][i + 3] << 24 ) |
287                   ((unsigned)bitmap[4 * j + 8 + 2][i + 3] << 16 ) |
288                   ((unsigned)bitmap[4 * j + 8 + 3][i + 3] <<  8 ) |
289                   ((unsigned)bitmap[4 * j + 8 + 4][i + 3]       );
290             }
291          }
292       }
293    
294       /*
295          Now determine the Unicode plane by comparing unidigit[0..5] to
296          the hexdigit[0x0..0xF] array.
297       */
298       uniplane = 0;
299       for (i=0; i<6; i++) { /* go through one bitmap digit at a time */
300          match = 0; /* haven't found pattern yet */
301          for (j = 0x0; !match && j <= 0xF; j++) {
302             if (unidigit[i][0] == hexdigit[j][0] &&
303                 unidigit[i][1] == hexdigit[j][1] &&
304                 unidigit[i][2] == hexdigit[j][2] &&
305                 unidigit[i][3] == hexdigit[j][3]) { /* we found the digit */
306                uniplane |= j;
307                match = 1;
308             }
309          }
310          uniplane <<= 4;
311       }
312       uniplane >>= 4;
313    }
314    /*
315       Now read each glyph and print it as hex.
316    */
317    for (i = 0x0; i <= 0xf; i++) {
318       for (j = 0x0; j <= 0xf; j++) {
319          for (k = 0; k < 16; k++) {
320             if (flip) {  /* transpose glyph matrix */
321                thischar1[k] = bitmap[32*(j+1) + k + 7][4 * (i+2) + 1];
322                thischar2[k] = bitmap[32*(j+1) + k + 7][4 * (i+2) + 2];
323             }
324             else {
325                thischar1[k] = bitmap[32*(i+1) + k + 7][4 * (j+2) + 1];
326                thischar2[k] = bitmap[32*(i+1) + k + 7][4 * (j+2) + 2];
327             }
328          }
329          /*
330             If the second half of the 16*16 character is all zeroes, this
331             character is only 8 bits wide, so print a half-width character.
332          */
333          empty1 = empty2 = 1;
334          for (k=0; (empty1 || empty2) && k < 16; k++) {
335             if (thischar1[k] != 0) empty1 = 0;
336             if (thischar2[k] != 0) empty2 = 0;
337             }
338          /*
339             Only print this glyph if it isn't blank.
340          */
341          if (!empty1 || !empty2) {
342             /*
343                If the second half is empty, this is a half-width character.
344                Only print the first half.
345             */
346             /*
347                Original GNU Unifont format is four hexadecimal digit character
348                code followed by a colon followed by a hex string.  Add support
349                for codes beyond the Basic Multilingual Plane.
350
351                Unicode ranges from U+0000 to U+10FFFF, so print either a
352                4-digit or a 6-digit code point.  Note that this software
353                should support up to an 8-digit code point, extending beyond
354                the normal Unicode range, but this has not been fully tested.
355             */
356             if (uniplane > 0xff)
357                fprintf (outfp, "%04X%X%X:", uniplane, i, j); // 6 digit code pt.
358             else
359                fprintf (outfp, "%02X%X%X:", uniplane, i, j); // 4 digit code pt.
360             for (thisrow=0; thisrow<16; thisrow++) {
361                /*
362                   If second half is empty and we're not forcing this
363                   code point to double width, print as single width
364                */
365                if (!forcewide &&
366                    empty2 && !wide[(uniplane << 8) | (i << 4) | j])
367                   fprintf (outfp,
368                           "%02X",
369                           thischar1[thisrow]);
370                else
371                   fprintf (outfp,
372                           "%02X%02X",
373                           thischar1[thisrow], thischar2[thisrow]);
374             }
375             fprintf (outfp, "\n");
376          }
377       }
378    }
379    exit (0);
380 }