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

unicoverage.c
Go to the documentation of this file.
1/**
2 @file unicoverage.c
3
4 @brief unicoverage - Show the coverage of Unicode plane scripts
5 for a GNU Unifont hex glyph file
6
7 @author Paul Hardy, unifoundry <at> unifoundry.com, 6 January 2008
8
9 @copyright Copyright (C) 2008, 2013 Paul Hardy
10
11 Synopsis: unicoverage [-ifont_file.hex] [-ocoverage_file.txt]
12
13 This program requires the file "coverage.dat" to be present
14 in the directory from which it is run.
15*/
16/*
17 LICENSE:
18
19 This program is free software: you can redistribute it and/or modify
20 it under the terms of the GNU General Public License as published by
21 the Free Software Foundation, either version 2 of the License, or
22 (at your option) any later version.
23
24 This program is distributed in the hope that it will be useful,
25 but WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 GNU General Public License for more details.
28
29 You should have received a copy of the GNU General Public License
30 along with this program. If not, see <http://www.gnu.org/licenses/>.
31*/
32
33/*
34 2016 (Paul Hardy): Modified in Unifont 9.0.01 release to remove non-existent
35 "-p" option and empty example from help printout.
36
37 2018 (Paul Hardy): Modified to cover entire Unicode range, not just Plane 0.
38
39 11 May 2019: [Paul Hardy] changed strcpy function call to strlcpy
40 for better error handling.
41
42 31 May 2019: [Paul Hardy] replaced strlcpy call with strncpy
43 for compilation on more systems.
44
45 4 June 2022: [Paul Hardy] Adjusted column spacing for better alignment
46 of Unicode Plane 1-15 scripts. Added "-n" option to print number of
47 glyphs in each range instead of percent coverage.
48
49 18 September 2022: [Paul Hardy] in nextrange function, initialize retval.
50
51 21 October 2023: [Paul Hardy]
52 Added full function prototype for nextrange function in main function.
53*/
54
55#include <stdio.h>
56#include <stdlib.h>
57#include <string.h>
58
59
60#define MAXBUF 256 ///< Maximum input line length - 1
61
62
63/**
64 @brief The main function.
65
66 @param[in] argc The count of command line arguments.
67 @param[in] argv Pointer to array of command line arguments.
68 @return This program exits with status 0.
69*/
70 int
71 main (int argc, char *argv[])
72{
73
74 int print_n=0; /* print # of glyphs, not percentage */
75 unsigned i; /* loop variable */
76 unsigned slen; /* string length of coverage file line */
77 char inbuf[256]; /* input buffer */
78 unsigned thischar; /* the current character */
79
80 char *infile="", *outfile=""; /* names of input and output files */
81 FILE *infp, *outfp; /* file pointers of input and output files */
82 FILE *coveragefp; /* file pointer to coverage.dat file */
83 int cstart, cend; /* current coverage start and end code points */
84 char coverstring[MAXBUF]; /* description of current coverage range */
85 int nglyphs; /* number of glyphs in this section */
86
87 /* to get next range & name of Unicode glyphs */
88 int nextrange (FILE *coveragefp, int *cstart, int *cend, char *coverstring);
89
90 void print_subtotal (FILE *outfp, int print_n, int nglyphs,
91 int cstart, int cend, char *coverstring);
92
93 if ((coveragefp = fopen ("coverage.dat", "r")) == NULL) {
94 fprintf (stderr, "\nError: data file \"coverage.dat\" not found.\n\n");
95 exit (0);
96 }
97
98 if (argc > 1) {
99 for (i = 1; i < argc; i++) {
100 if (argv[i][0] == '-') { /* this is an option argument */
101 switch (argv[i][1]) {
102 case 'i': /* name of input file */
103 infile = &argv[i][2];
104 break;
105 case 'n': /* print number of glyphs instead of percentage */
106 print_n = 1;
107 case 'o': /* name of output file */
108 outfile = &argv[i][2];
109 break;
110 default: /* if unrecognized option, print list and exit */
111 fprintf (stderr, "\nSyntax:\n\n");
112 fprintf (stderr, " %s -p<Unicode_Page> ", argv[0]);
113 fprintf (stderr, "-i<Input_File> -o<Output_File> -w\n\n");
114 exit (1);
115 }
116 }
117 }
118 }
119 /*
120 Make sure we can open any I/O files that were specified before
121 doing anything else.
122 */
123 if (strlen (infile) > 0) {
124 if ((infp = fopen (infile, "r")) == NULL) {
125 fprintf (stderr, "Error: can't open %s for input.\n", infile);
126 exit (1);
127 }
128 }
129 else {
130 infp = stdin;
131 }
132 if (strlen (outfile) > 0) {
133 if ((outfp = fopen (outfile, "w")) == NULL) {
134 fprintf (stderr, "Error: can't open %s for output.\n", outfile);
135 exit (1);
136 }
137 }
138 else {
139 outfp = stdout;
140 }
141
142 /*
143 Print header row.
144 */
145 if (print_n) {
146 fprintf (outfp, "# Glyphs Range Script\n");
147 fprintf (outfp, "-------- ----- ------\n");
148 }
149 else {
150 fprintf (outfp, "Covered Range Script\n");
151 fprintf (outfp, "------- ----- ------\n\n");
152 }
153
154 slen = nextrange (coveragefp, &cstart, &cend, coverstring);
155 nglyphs = 0;
156
157 /*
158 Read in the glyphs in the file
159 */
160 while (slen != 0 && fgets (inbuf, MAXBUF-1, infp) != NULL) {
161 sscanf (inbuf, "%x", &thischar);
162
163 /* Read a character beyond end of current script. */
164 while (cend < thischar && slen != 0) {
165 print_subtotal (outfp, print_n, nglyphs, cstart, cend, coverstring);
166
167 /* start new range total */
168 slen = nextrange (coveragefp, &cstart, &cend, coverstring);
169 nglyphs = 0;
170 }
171 nglyphs++;
172 }
173
174 print_subtotal (outfp, print_n, nglyphs, cstart, cend, coverstring);
175
176 exit (0);
177}
178
179/**
180 @brief Get next Unicode range.
181
182 This function reads the next Unicode script range to count its
183 glyph coverage.
184
185 @param[in] coveragefp File pointer to Unicode script range data file.
186 @param[in] cstart Starting code point in current Unicode script range.
187 @param[in] cend Ending code point in current Unicode script range.
188 @param[out] coverstring String containing <cstart>-<cend> substring.
189 @return Length of the last string read, or 0 for end of file.
190*/
191 int
192 nextrange (FILE *coveragefp,
193 int *cstart, int *cend,
194 char *coverstring)
195{
196 int i;
197 static char inbuf[MAXBUF];
198 int retval; /* the return value */
199
200 retval = 0;
201
202 do {
203 if (fgets (inbuf, MAXBUF-1, coveragefp) != NULL) {
204 retval = strlen (inbuf);
205 if ((inbuf[0] >= '0' && inbuf[0] <= '9') ||
206 (inbuf[0] >= 'A' && inbuf[0] <= 'F') ||
207 (inbuf[0] >= 'a' && inbuf[0] <= 'f')) {
208 sscanf (inbuf, "%x-%x", cstart, cend);
209 i = 0;
210 while (inbuf[i] != ' ') i++; /* find first blank */
211 while (inbuf[i] == ' ') i++; /* find next non-blank */
212 strncpy (coverstring, &inbuf[i], MAXBUF);
213 }
214 else retval = 0;
215 }
216 else retval = 0;
217 } while (retval == 0 && !feof (coveragefp));
218
219 return (retval);
220}
221
222
223/**
224 @brief Print the subtotal for one Unicode script range.
225
226 @param[in] outfp Pointer to output file.
227 @param[in] print_n 1 = print number of glyphs, 0 = print percentage.
228 @param[in] nglyphs Number of glyphs in current range.
229 @param[in] cstart Starting code point for current range.
230 @param[in] cend Ending code point for current range.
231 @param[in] coverstring Character string of "<cstart>-<cend>".
232*/
233 void print_subtotal (FILE *outfp, int print_n, int nglyphs,
234 int cstart, int cend, char *coverstring) {
235
236 /* print old range total */
237 if (print_n) { /* Print number of glyphs, not percentage */
238 fprintf (outfp, " %6d ", nglyphs);
239 }
240 else {
241 fprintf (outfp, " %5.1f%%", 100.0*nglyphs/(1+cend-cstart));
242 }
243
244 if (cend < 0x10000)
245 fprintf (outfp, " U+%04X..U+%04X %s",
246 cstart, cend, coverstring);
247 else
248 fprintf (outfp, " U+%05X..U+%05X %s",
249 cstart, cend, coverstring);
250
251 return;
252}
main
int main(int argc, char *argv[])
The main function.
Definition: unicoverage.c:71
print_subtotal
void print_subtotal(FILE *outfp, int print_n, int nglyphs, int cstart, int cend, char *coverstring)
Print the subtotal for one Unicode script range.
Definition: unicoverage.c:233
MAXBUF
#define MAXBUF
Maximum input line length - 1.
Definition: unicoverage.c:60
nextrange
int nextrange(FILE *coveragefp, int *cstart, int *cend, char *coverstring)
Get next Unicode range.
Definition: unicoverage.c:192