GNU Unifont 16.0.02 Pan-Unicode font with complete Unicode Plane 0 coverage and partial coverage of higher planes

unipagecount.c
Go to the documentation of this file.
1/**
2 @file unipagecount.c
3
4 @brief unipagecount - Count the number of glyphs defined in each page
5 of 256 code points
6
7 @author Paul Hardy, unifoundry <at> unifoundry.com, December 2007
8
9 @copyright Copyright (C) 2007, 2008, 2013, 2014 Paul Hardy
10
11 This program counts the number of glyphs that are defined in each
12 "page" of 256 code points, and prints the counts in an 8 x 8 grid.
13 Input is from stdin. Output is to stdout.
14
15 The background color of each cell in a 16-by-16 grid of 256 code points
16 is shaded to indicate percentage coverage. Red indicates 0% coverage,
17 green represents 100% coverage, and colors in between pure red and pure
18 green indicate partial coverage on a scale.
19
20 Each code point range number can be a hyperlink to a PNG file for
21 that 256-code point range's corresponding bitmap glyph image.
22
23 Synopsis:
24
25 unipagecount < font_file.hex > count.txt
26 unipagecount -phex_page_num < font_file.hex -- just 256 points
27 unipagecount -h < font_file.hex -- HTML table
28 unipagecount -P1 -h < font.hex > count.html -- Plane 1, HTML out
29 unipagecount -l < font_file.hex -- linked HTML table
30*/
31/*
32 LICENSE:
33
34 This program is free software: you can redistribute it and/or modify
35 it under the terms of the GNU General Public License as published by
36 the Free Software Foundation, either version 2 of the License, or
37 (at your option) any later version.
38
39 This program is distributed in the hope that it will be useful,
40 but WITHOUT ANY WARRANTY; without even the implied warranty of
41 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
42 GNU General Public License for more details.
43
44 You should have received a copy of the GNU General Public License
45 along with this program. If not, see <http://www.gnu.org/licenses/>.
46*/
47
48/*
49 2018, Paul Hardy: Changed "Private Use" to "Private Use Area" in
50 output HTML file.
51
52 21 October 2023 [Paul Hardy]:
53 - Added full prototype for mkftable function in main function.
54*/
55
56#include <stdio.h>
57#include <stdlib.h>
58
59#define MAXBUF 256 ///< Maximum input line size - 1.
60
61
62/**
63 @brief The main function.
64
65 @param[in] argc The count of command line arguments.
66 @param[in] argv Pointer to array of command line arguments.
67 @return This program exits with status 0.
68*/
69 int
70 main (int argc, char *argv[])
71{
72
73 char inbuf[MAXBUF]; /* Max 256 characters in an input line */
74 int i, j; /* loop variables */
75 unsigned plane=0; /* Unicode plane number, 0 to 0x16 */
76 unsigned page; /* unicode page (256 bytes wide) */
77 unsigned unichar; /* unicode character */
78 int pagecount[256] = {256 * 0};
79 int onepage=0; /* set to one if printing character grid for one page */
80 int pageno=0; /* page number selected if only examining one page */
81 int html=0; /* =0: print plain text; =1: print HTML */
82 int links=0; /* =1: print HTML links; =0: don't print links */
83
84 /* make (print) flipped HTML table */
85 void mkftable (unsigned plane, int pagecount[256], int links);
86
87 size_t strlen();
88
89 if (argc > 1 && argv[1][0] == '-') { /* Parse option */
90 plane = 0;
91 for (i = 1; i < argc; i++) {
92 switch (argv[i][1]) {
93 case 'p': /* specified -p<hexpage> -- use given page number */
94 sscanf (&argv[1][2], "%x", &pageno);
95 if (pageno >= 0 && pageno <= 255) onepage = 1;
96 break;
97 case 'h': /* print HTML table instead of text table */
98 html = 1;
99 break;
100 case 'l': /* print hyperlinks in HTML table */
101 links = 1;
102 html = 1;
103 break;
104 case 'P': /* Plane number specified */
105 plane = atoi(&argv[1][2]);
106 break;
107 }
108 }
109 }
110 /*
111 Initialize pagecount to account for noncharacters.
112 */
113 if (!onepage && plane==0) {
114 pagecount[0xfd] = 32; /* for U+FDD0..U+FDEF */
115 }
116 pagecount[0xff] = 2; /* for U+nnFFFE, U+nnFFFF */
117 /*
118 Read one line at a time from input. The format is:
119
120 <hexpos>:<hexbitmap>
121
122 where <hexpos> is the hexadecimal Unicode character position
123 in the range 00..FF and <hexbitmap> is the sequence of hexadecimal
124 digits of the character, laid out in a grid from left to right,
125 top to bottom. The character is assumed to be 16 rows of variable
126 width.
127 */
128 while (fgets (inbuf, MAXBUF-1, stdin) != NULL) {
129 sscanf (inbuf, "%X", &unichar);
130 page = unichar >> 8;
131 if (onepage) { /* only increment counter if this is page we want */
132 if (page == pageno) { /* character is in the page we want */
133 pagecount[unichar & 0xff]++; /* mark character as covered */
134 }
135 }
136 else { /* counting all characters in all pages */
137 if (plane == 0) {
138 /* Don't add in noncharacters (U+FDD0..U+FDEF, U+FFFE, U+FFFF) */
139 if (unichar < 0xfdd0 || (unichar > 0xfdef && unichar < 0xfffe))
140 pagecount[page]++;
141 }
142 else {
143 if ((page >> 8) == plane) { /* code point is in desired plane */
144 pagecount[page & 0xFF]++;
145 }
146 }
147 }
148 }
149 if (html) {
150 mkftable (plane, pagecount, links);
151 }
152 else { /* Otherwise, print plain text table */
153 if (plane > 0) fprintf (stdout, " ");
154 fprintf (stdout,
155 " 0 1 2 3 4 5 6 7 8 9 A B C D E F\n");
156 for (i=0; i<0x10; i++) {
157 fprintf (stdout,"%02X%X ", plane, i); /* row header */
158 for (j=0; j<0x10; j++) {
159 if (onepage) {
160 if (pagecount[i*16+j])
161 fprintf (stdout," * ");
162 else
163 fprintf (stdout," . ");
164 }
165 else {
166 fprintf (stdout, "%3X ", pagecount[i*16+j]);
167 }
168 }
169 fprintf (stdout,"\n");
170 }
171
172 }
173 exit (0);
174}
175
176
177/**
178 @brief Create an HTML table linked to PNG images.
179
180 This function creates an HTML table to show PNG files
181 in a 16 by 16 grid. The background color of each "page"
182 of 256 code points is shaded from red (for 0% coverage)
183 to green (for 100% coverage).
184
185 @param[in] plane The Unicode plane, 0..17.
186 @param[in] pagecount Array with count of glyphs in each 256 code point range.
187 @param[in] links 1 = generate hyperlinks, 0 = do not generate hyperlinks.
188*/
189 void
190 mkftable (unsigned plane, int pagecount[256], int links)
191{
192 int i, j;
193 int count;
194 unsigned bgcolor;
195
196 printf ("<html>\n");
197 printf ("<body>\n");
198 printf ("<table border=\"3\" align=\"center\">\n");
199 printf (" <tr><th colspan=\"16\" bgcolor=\"#ffcc80\">");
200 printf ("GNU Unifont Glyphs<br>with Page Coverage for Plane %d<br>(Green=100%%, Red=0%%)</th></tr>\n", plane);
201 for (i = 0x0; i <= 0xF; i++) {
202 printf (" <tr>\n");
203 for (j = 0x0; j <= 0xF; j++) {
204 count = pagecount[ (i << 4) | j ];
205
206 /* print link in cell if links == 1 */
207 if (plane != 0 || (i < 0xd || (i == 0xd && j < 0x8) || (i == 0xf && j > 0x8))) {
208 /* background color is light green if completely done */
209 if (count == 0x100) bgcolor = 0xccffcc;
210 /* otherwise background is a shade of yellow to orange to red */
211 else bgcolor = 0xff0000 | (count << 8) | (count >> 1);
212 printf (" <td bgcolor=\"#%06X\">", bgcolor);
213 if (plane == 0)
214 printf ("<a href=\"png/plane%02X/uni%02X%X%X.png\">%X%X</a>", plane, plane, i, j, i, j);
215 else
216 printf ("<a href=\"png/plane%02X/uni%02X%X%X.png\">%02X%X%X</a>", plane, plane, i, j, plane, i, j);
217 printf ("</td>\n");
218 }
219 else if (i == 0xd) {
220 if (j == 0x8) {
221 printf (" <td align=\"center\" colspan=\"8\" bgcolor=\"#cccccc\">");
222 printf ("<b>Surrogate Pairs</b>");
223 printf ("</td>\n");
224 } /* otherwise don't print anything more columns in this row */
225 }
226 else if (i == 0xe) {
227 if (j == 0x0) {
228 printf (" <td align=\"center\" colspan=\"16\" bgcolor=\"#cccccc\">");
229 printf ("<b>Private Use Area</b>");
230 printf ("</td>\n");
231 } /* otherwise don't print any more columns in this row */
232 }
233 else if (i == 0xf) {
234 if (j == 0x0) {
235 printf (" <td align=\"center\" colspan=\"9\" bgcolor=\"#cccccc\">");
236 printf ("<b>Private Use Area</b>");
237 printf ("</td>\n");
238 }
239 }
240 }
241 printf (" </tr>\n");
242 }
243 printf ("</table>\n");
244 printf ("</body>\n");
245 printf ("</html>\n");
246
247 return;
248}
main
int main(int argc, char *argv[])
The main function.
Definition: unipagecount.c:70
mkftable
void mkftable(unsigned plane, int pagecount[256], int links)
Create an HTML table linked to PNG images.
Definition: unipagecount.c:190
MAXBUF
#define MAXBUF
Maximum input line size - 1.
Definition: unipagecount.c:59