GNU Unifont: src/unicoverage.c Source File

GNU Unifont 15.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
52#include <stdio.h>
53#include <stdlib.h>
54#include <string.h>
55
56
57#define MAXBUF 256 ///< Maximum input line length - 1
58
59
60/**
61 @brief The main function.
62
63 @param[in] argc The count of command line arguments.
64 @param[in] argv Pointer to array of command line arguments.
65 @return This program exits with status 0.
66*/
67int
68main (int argc, char *argv[])
69{
70
71 int print_n=0; /* print # of glyphs, not percentage */
72 unsigned i; /* loop variable */
73 unsigned slen; /* string length of coverage file line */
74 char inbuf[256]; /* input buffer */
75 unsigned thischar; /* the current character */
76
77 char *infile="", *outfile=""; /* names of input and output files */
78 FILE *infp, *outfp; /* file pointers of input and output files */
79 FILE *coveragefp; /* file pointer to coverage.dat file */
80 int cstart, cend; /* current coverage start and end code points */
81 char coverstring[MAXBUF]; /* description of current coverage range */
82 int nglyphs; /* number of glyphs in this section */
83 int nextrange(); /* to get next range & name of Unicode glyphs */
84
85 void print_subtotal (FILE *outfp, int print_n, int nglyphs,
86 int cstart, int cend, char *coverstring);
87
88 if ((coveragefp = fopen ("coverage.dat", "r")) == NULL) {
89 fprintf (stderr, "\nError: data file \"coverage.dat\" not found.\n\n");
90 exit (0);
91 }
92
93 if (argc > 1) {
94 for (i = 1; i < argc; i++) {
95 if (argv[i][0] == '-') { /* this is an option argument */
96 switch (argv[i][1]) {
97 case 'i': /* name of input file */
98 infile = &argv[i][2];
99 break;
100 case 'n': /* print number of glyphs instead of percentage */
101 print_n = 1;
102 case 'o': /* name of output file */
103 outfile = &argv[i][2];
104 break;
105 default: /* if unrecognized option, print list and exit */
106 fprintf (stderr, "\nSyntax:\n\n");
107 fprintf (stderr, " %s -p<Unicode_Page> ", argv[0]);
108 fprintf (stderr, "-i<Input_File> -o<Output_File> -w\n\n");
109 exit (1);
110 }
111 }
112 }
113 }
114 /*
115 Make sure we can open any I/O files that were specified before
116 doing anything else.
117 */
118 if (strlen (infile) > 0) {
119 if ((infp = fopen (infile, "r")) == NULL) {
120 fprintf (stderr, "Error: can't open %s for input.\n", infile);
121 exit (1);
122 }
123 }
124 else {
125 infp = stdin;
126 }
127 if (strlen (outfile) > 0) {
128 if ((outfp = fopen (outfile, "w")) == NULL) {
129 fprintf (stderr, "Error: can't open %s for output.\n", outfile);
130 exit (1);
131 }
132 }
133 else {
134 outfp = stdout;
135 }
136
137 /*
138 Print header row.
139 */
140 if (print_n) {
141 fprintf (outfp, "# Glyphs Range Script\n");
142 fprintf (outfp, "-------- ----- ------\n");
143 }
144 else {
145 fprintf (outfp, "Covered Range Script\n");
146 fprintf (outfp, "------- ----- ------\n\n");
147 }
148
149 slen = nextrange (coveragefp, &cstart, &cend, coverstring);
150 nglyphs = 0;
151
152 /*
153 Read in the glyphs in the file
154 */
155 while (slen != 0 && fgets (inbuf, MAXBUF-1, infp) != NULL) {
156 sscanf (inbuf, "%x", &thischar);
157
158 /* Read a character beyond end of current script. */
159 while (cend < thischar && slen != 0) {
160 print_subtotal (outfp, print_n, nglyphs, cstart, cend, coverstring);
161
162 /* start new range total */
163 slen = nextrange (coveragefp, &cstart, &cend, coverstring);
164 nglyphs = 0;
165 }
166 nglyphs++;
167 }
168
169 print_subtotal (outfp, print_n, nglyphs, cstart, cend, coverstring);
170
171 exit (0);
172}
173
174/**
175 @brief Get next Unicode range.
176
177 This function reads the next Unicode script range to count its
178 glyph coverage.
179
180 @param[in] coveragefp File pointer to Unicode script range data file.
181 @param[in] cstart Starting code point in current Unicode script range.
182 @param[in] cend Ending code point in current Unicode script range.
183 @param[out] coverstring String containing <cstart>-<cend> substring.
184 @return Length of the last string read, or 0 for end of file.
185*/
186int
187nextrange (FILE *coveragefp,
188 int *cstart, int *cend,
189 char *coverstring)
190{
191 int i;
192 static char inbuf[MAXBUF];
193 int retval; /* the return value */
194
195 retval = 0;
196
197 do {
198 if (fgets (inbuf, MAXBUF-1, coveragefp) != NULL) {
199 retval = strlen (inbuf);
200 if ((inbuf[0] >= '0' && inbuf[0] <= '9') ||
201 (inbuf[0] >= 'A' && inbuf[0] <= 'F') ||
202 (inbuf[0] >= 'a' && inbuf[0] <= 'f')) {
203 sscanf (inbuf, "%x-%x", cstart, cend);
204 i = 0;
205 while (inbuf[i] != ' ') i++; /* find first blank */
206 while (inbuf[i] == ' ') i++; /* find next non-blank */
207 strncpy (coverstring, &inbuf[i], MAXBUF);
208 }
209 else retval = 0;
210 }
211 else retval = 0;
212 } while (retval == 0 && !feof (coveragefp));
213
214 return (retval);
215}
216
217
218/**
219 @brief Print the subtotal for one Unicode script range.
220
221 @param[in] outfp Pointer to output file.
222 @param[in] print_n 1 = print number of glyphs, 0 = print percentage.
223 @param[in] nglyphs Number of glyphs in current range.
224 @param[in] cstart Starting code point for current range.
225 @param[in] cend Ending code point for current range.
226 @param[in] coverstring Character string of "<cstart>-<cend>".
227*/
228 void print_subtotal (FILE *outfp, int print_n, int nglyphs,
229 int cstart, int cend, char *coverstring) {
230
231 /* print old range total */
232 if (print_n) { /* Print number of glyphs, not percentage */
233 fprintf (outfp, " %6d ", nglyphs);
234 }
235 else {
236 fprintf (outfp, " %5.1f%%", 100.0*nglyphs/(1+cend-cstart));
237 }
238
239 if (cend < 0x10000)
240 fprintf (outfp, " U+%04X..U+%04X %s",
241 cstart, cend, coverstring);
242 else
243 fprintf (outfp, " U+%05X..U+%05X %s",
244 cstart, cend, coverstring);
245
246 return;
247}
main
int main(int argc, char *argv[])
The main function.
Definition: unicoverage.c:68
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:228
MAXBUF
#define MAXBUF
Maximum input line length - 1.
Definition: unicoverage.c:57
nextrange
int nextrange(FILE *coveragefp, int *cstart, int *cend, char *coverstring)
Get next Unicode range.
Definition: unicoverage.c:187