53#define PIKTO_START 0x0F0E70
54#define PIKTO_END 0x0F11EF
56#define PIKTO_SIZE (PIKTO_END - PIKTO_START + 1)
67main (
int argc,
char **argv)
76 char glyph_width[0x20000];
82 fprintf (stderr,
"\n\nUsage: %s <unifont.hex> <combining.txt>\n\n", argv[0]);
89 if ((infilefp = fopen (argv[1],
"r")) == NULL) {
90 fprintf (stderr,
"ERROR - hex input file %s not found.\n\n", argv[1]);
95 memset (glyph_width, -1, 0x20000 *
sizeof (
char));
96 memset (pikto_width, -1, (
PIKTO_SIZE) *
sizeof (
char));
99 while (fgets (teststring,
MAXSTRING-1, infilefp) != NULL) {
100 sscanf (teststring,
"%X:%*s", &loc);
102 gstart = strchr (teststring,
':') + 1;
107 glyph_width[loc] = (strlen (gstart) - 1) >> 5;
110 gstart = strchr (teststring,
':') + 1;
111 pikto_width[loc -
PIKTO_START] = strlen (gstart) <= 34 ? 1 : 2;
120 if ((infilefp = fopen (argv[2],
"r")) == NULL) {
121 fprintf (stderr,
"ERROR - combining characters file %s not found.\n\n", argv[2]);
125 while (fgets (teststring,
MAXSTRING-1, infilefp) != NULL) {
126 sscanf (teststring,
"%X:%*s", &loc);
127 if (loc < 0x20000) glyph_width[loc] = 0;
216 for (i = 0xFDD0; i <= 0xFDEF; i++) glyph_width[i] = -1;
217 glyph_width[0xFFFE] = -1;
218 glyph_width[0xFFFF] = -1;
221 for (i = 0xD800; i <= 0xDFFF; i++) glyph_width[i]=-1;
224 for (i = 0x4E00; i <= 0x9FFF; i++)
if (glyph_width[i] < 0) glyph_width[i] = 2;
225 for (i = 0x3400; i <= 0x4DBF; i++)
if (glyph_width[i] < 0) glyph_width[i] = 2;
226 for (i = 0xF900; i <= 0xFAFF; i++)
if (glyph_width[i] < 0) glyph_width[i] = 2;
232 printf (
" wcwidth and wcswidth functions, as per IEEE 1003.1-2008\n");
233 printf (
" System Interfaces, pp. 2241 and 2251.\n\n");
234 printf (
" Author: Paul Hardy, 2013\n\n");
235 printf (
" Copyright (c) 2013 Paul Hardy\n\n");
236 printf (
" LICENSE:\n");
238 printf (
" This program is free software: you can redistribute it and/or modify\n");
239 printf (
" it under the terms of the GNU General Public License as published by\n");
240 printf (
" the Free Software Foundation, either version 2 of the License, or\n");
241 printf (
" (at your option) any later version.\n");
243 printf (
" This program is distributed in the hope that it will be useful,\n");
244 printf (
" but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
245 printf (
" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n");
246 printf (
" GNU General Public License for more details.\n");
248 printf (
" You should have received a copy of the GNU General Public License\n");
249 printf (
" along with this program. If not, see <http://www.gnu.org/licenses/>.\n");
252 printf (
"#include <wchar.h>\n\n");
253 printf (
"/* Definitions for Pikto CSUR Private Use Area glyphs */\n");
254 printf (
"#define PIKTO_START\t0x%06X\n",
PIKTO_START);
255 printf (
"#define PIKTO_END\t0x%06X\n",
PIKTO_END);
256 printf (
"#define PIKTO_SIZE\t(PIKTO_END - PIKTO_START + 1)\n");
258 printf (
"/* wcwidth -- return charcell positions of one code point */\n");
259 printf (
"inline int\nwcwidth (wchar_t wc)\n{\n");
260 printf (
" return (wcswidth (&wc, 1));\n");
263 printf (
"int\nwcswidth (const wchar_t *pwcs, size_t n)\n{\n\n");
264 printf (
" int i; /* loop variable */\n");
265 printf (
" unsigned codept; /* Unicode code point of current character */\n");
266 printf (
" unsigned plane; /* Unicode plane, 0x00..0x10 */\n");
267 printf (
" unsigned lower17; /* lower 17 bits of Unicode code point */\n");
268 printf (
" unsigned lower16; /* lower 16 bits of Unicode code point */\n");
269 printf (
" int lowpt, midpt, highpt; /* for binary searching in plane1zeroes[] */\n");
270 printf (
" int found; /* for binary searching in plane1zeroes[] */\n");
271 printf (
" int totalwidth; /* total width of string, in charcells (1 or 2/glyph) */\n");
272 printf (
" int illegalchar; /* Whether or not this code point is illegal */\n");
279 printf (
" char glyph_width[0x20000] = {");
280 for (i = 0; i < 0x10000; i++) {
282 printf (
"\n /* U+%04X */ ", i);
283 printf (
"%d,", glyph_width[i]);
285 for (i = 0x10000; i < 0x20000; i++) {
287 printf (
"\n /* U+%06X */ ", i);
288 printf (
"%d", glyph_width[i]);
289 if (i < 0x1FFFF) putchar (
',');
291 printf (
"\n };\n\n");
296 printf (
" char pikto_width[PIKTO_SIZE] = {");
300 printf (
"%d", pikto_width[i]);
303 printf (
"\n };\n\n");
309 printf (
" illegalchar = totalwidth = 0;\n");
310 printf (
" for (i = 0; !illegalchar && i < n; i++) {\n");
311 printf (
" codept = pwcs[i];\n");
312 printf (
" plane = codept >> 16;\n");
313 printf (
" lower17 = codept & 0x1FFFF;\n");
314 printf (
" lower16 = codept & 0xFFFF;\n");
315 printf (
" if (plane < 2) { /* the most common case */\n");
316 printf (
" if (glyph_width[lower17] < 0) illegalchar = 1;\n");
317 printf (
" else totalwidth += glyph_width[lower17];\n");
319 printf (
" else { /* a higher plane or beyond Unicode range */\n");
320 printf (
" if ((lower16 == 0xFFFE) || (lower16 == 0xFFFF)) {\n");
321 printf (
" illegalchar = 1;\n");
323 printf (
" else if (plane < 4) { /* Ideographic Plane */\n");
324 printf (
" totalwidth += 2; /* Default ideographic width */\n");
326 printf (
" else if (plane == 0x0F) { /* CSUR Private Use Area */\n");
327 printf (
" if (lower16 <= 0x0E6F) { /* Kinya */\n");
328 printf (
" totalwidth++; /* all Kinya syllables have width 1 */\n");
330 printf (
" else if (lower16 <= (PIKTO_END & 0xFFFF)) { /* Pikto */\n");
331 printf (
" if (pikto_width[lower16 - (PIKTO_START & 0xFFFF)] < 0) illegalchar = 1;\n");
332 printf (
" else totalwidth += pikto_width[lower16 - (PIKTO_START & 0xFFFF)];\n");
335 printf (
" else if (plane > 0x10) {\n");
336 printf (
" illegalchar = 1;\n");
338 printf (
" /* Other non-printing in higher planes; return -1 as per IEEE 1003.1-2008. */\n");
339 printf (
" else if (/* language tags */\n");
340 printf (
" codept == 0x0E0001 || (codept >= 0x0E0020 && codept <= 0x0E007F) ||\n");
341 printf (
" /* variation selectors, 0x0E0100..0x0E01EF */\n");
342 printf (
" (codept >= 0x0E0100 && codept <= 0x0E01EF)) {\n");
343 printf (
" illegalchar = 1;\n");
346 printf (
" Unicode plane 0x02..0x10 printing character\n");
348 printf (
" else {\n");
349 printf (
" illegalchar = 1; /* code is not in font */\n");
354 printf (
" if (illegalchar) totalwidth = -1;\n");
356 printf (
" return (totalwidth);\n");
int main(void)
The main function.
#define MAXSTRING
Maximum input line length - 1.
#define PIKTO_START
Start of Pikto code point range.
#define PIKTO_END
End of Pikto code point range.