unifont-7.0.06.tar.gz
[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 UNISTART 0x3400
32 #define UNISTOP 0x4DBF
33
34 #define MAXBUF 256
35
36
37 int
38 main()
39 {
40    int i;
41    int digitsout;  /* how many hex digits we output in a bitmap */
42    int thispoint;
43    char inbuf[MAXBUF];
44    int bbxx, bbxy, bbxxoff, bbxyoff;
45
46    int descent=4; /* font descent wrt baseline */
47    int startrow;  /* row to start glyph        */
48    unsigned rowout;
49
50    while (fgets (inbuf, MAXBUF - 1, stdin) != NULL) {
51       if (strncmp (inbuf, "ENCODING ", 9) == 0) {
52          sscanf (&inbuf[9], "%d", &thispoint); /* get code point */
53          /*
54             If we want this code point, get the BBX (bounding box) and
55             BITMAP information.
56          */
57          if ((thispoint >= 0x2E80 && thispoint <= 0x2EFF) || // CJK Radicals Supplement
58              (thispoint >= 0x2F00 && thispoint <= 0x2FDF) || // Kangxi Radicals
59              (thispoint >= 0x2FF0 && thispoint <= 0x2FFF) || // Ideographic Description Characters
60              (thispoint >= 0x3001 && thispoint <= 0x303F) || // CJK Symbols and Punctuation (U+3000 is a space)
61              (thispoint >= 0x3100 && thispoint <= 0x312F) || // Bopomofo
62              (thispoint >= 0x31A0 && thispoint <= 0x31BF) || // Bopomofo extend
63              (thispoint >= 0x31C0 && thispoint <= 0x31EF) || // CJK Strokes
64              (thispoint >= 0x3400 && thispoint <= 0x4DBF) || // CJK Unified Ideographs Extension A
65              (thispoint >= 0x4E00 && thispoint <= 0x9FCF) || // CJK Unified Ideographs
66              (thispoint >= 0xF900 && thispoint <= 0xFAFF))   // CJK Compatibility Ideographs
67             {
68             while (fgets (inbuf, MAXBUF - 1, stdin) != NULL &&
69                    strncmp (inbuf, "BBX ", 4) != 0); /* find bounding box */
70
71             sscanf (&inbuf[4], "%d %d %d %d", &bbxx, &bbxy, &bbxxoff, &bbxyoff);
72             while (fgets (inbuf, MAXBUF - 1, stdin) != NULL &&
73                    strncmp (inbuf, "BITMAP", 6) != 0); /* find bitmap start */
74             fprintf (stdout, "%04X:", thispoint);
75             digitsout = 0;
76             /* Print initial blank rows */
77             startrow = descent + bbxyoff + bbxy;
78
79             /* Force everything to 16 pixels wide */
80             for (i = 16; i > startrow; i--) {
81                fprintf (stdout,"0000");
82                digitsout += 4;
83             }
84             while (fgets (inbuf, MAXBUF - 1, stdin) != NULL &&
85                    strncmp (inbuf, "END", 3) != 0) { /* copy bitmap until END */
86                sscanf (inbuf, "%X", &rowout);
87                /* Now force glyph to a 16x16 grid even if they'd fit in 8x16 */
88                if (bbxx <= 8) rowout <<= 8;  /* shift left for 16x16 glyph */
89                rowout >>= bbxxoff;
90                fprintf (stdout, "%04X", rowout);
91                digitsout += 4;
92             }
93
94             /* Pad for 16x16 glyph */
95             while (digitsout < 64) {
96                fprintf (stdout,"0000");
97                digitsout += 4;
98             }
99             fprintf (stdout,"\n");
100          }
101       }
102    }
103    exit (0);
104 }