2 unigenwidth - IEEE 1003.1-2008 setup to calculate wchar_t string widths.
4 Author: Paul Hardy, 2013
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
31 main (int argc, char **argv)
34 int i; /* loop variable */
36 char teststring[MAXSTRING];
40 char plane0width[0x10000];
46 fprintf (stderr, "\n\nUsage: %s <unifont-file.hex> <combining.txt>\n\n");
52 Read the collection of hex glyphs.
54 if ((infilefp = fopen (argv[1],"r")) == NULL) {
55 fprintf (stderr,"ERROR - hex input file %s not found.\n\n", argv[1]);
59 memset (plane0width, 0, 0x10000 * sizeof (char));
61 teststring[MAXSTRING-1] = '\0';
62 while (fgets (teststring, MAXSTRING-1, infilefp) != NULL) {
63 sscanf (teststring, "%X", &loc);
64 gstart = index (teststring,':') + 1;
65 plane0width[loc] = strlen (gstart) <= 34 ? 1 : 2;
71 Now read the combining character code points. These have width of 0.
73 if ((infilefp = fopen (argv[2],"r")) == NULL) {
74 fprintf (stderr,"ERROR - combining characters file %s not found.\n\n", argv[2]);
78 while (fscanf (infilefp, "%X", &loc) != EOF) plane0width[loc] = 0;
83 Code Points with Unusual Properties (Unicode Standard, Chapter 4)
85 plane0width[0]=0; /* NULL character */
86 for (i = 0x0001; i <= 0x001F; i++) plane0width[i]=-1; /* Control Characters */
87 for (i = 0x007F; i <= 0x009F; i++) plane0width[i]=-1; /* Control Characters */
89 plane0width[0x034F]=0; /* combining grapheme joiner */
90 plane0width[0x180B]=0; /* Mongolian free variation selector one */
91 plane0width[0x180C]=0; /* Mongolian free variation selector two */
92 plane0width[0x180D]=0; /* Mongolian free variation selector three */
93 plane0width[0x180E]=0; /* Mongolian vowel separator */
94 plane0width[0x200B]=0; /* zero width space */
95 plane0width[0x200C]=0; /* zero width non-joiner */
96 plane0width[0x200D]=0; /* zero width joiner */
97 plane0width[0x200E]=0; /* left-to-right mark */
98 plane0width[0x200F]=0; /* right-to-left mark */
99 plane0width[0x202A]=0; /* left-to-right embedding */
100 plane0width[0x202B]=0; /* right-to-left embedding */
101 plane0width[0x202C]=0; /* pop directional formatting */
102 plane0width[0x202D]=0; /* left-to-right override */
103 plane0width[0x202E]=0; /* right-to-left override */
104 plane0width[0x2060]=0; /* word joiner */
105 plane0width[0x2061]=0; /* function application */
106 plane0width[0x2062]=0; /* invisible times */
107 plane0width[0x2063]=0; /* invisible separator */
108 plane0width[0x2064]=0; /* invisible plus */
109 plane0width[0x206A]=0; /* inhibit symmetric swapping */
110 plane0width[0x206B]=0; /* activate symmetric swapping */
111 plane0width[0x206C]=0; /* inhibit arabic form shaping */
112 plane0width[0x206D]=0; /* activate arabic form shaping */
113 plane0width[0x206E]=0; /* national digit shapes */
114 plane0width[0x206F]=0; /* nominal digit shapes */
117 Ideographic Description Character Left to Right..
118 Ideographic Description Character Overlaid
120 for (i = 0x2FF0; i <= 0x2FFB; i++) plane0width[i] = 0;
122 plane0width[0x303E] = 0; /* ideographic variation indicator */
124 /* Variation Selector-1 to Variation Selector-16 */
125 for (i = 0xFE00; i <= 0xFE0F; i++) plane0width[i] = 0;
127 plane0width[0xFEFF]=0; /* zero width no-break space */
128 plane0width[0xFFF9]=0; /* interlinear annotation anchor */
129 plane0width[0xFFFA]=0; /* interlinear annotation separator */
130 plane0width[0xFFFB]=0; /* interlinear annotation terminator */
132 Let glyph widths represent 0xFFFC (object replacement character)
133 and 0xFFFD (replacement character).
139 Leading Consonant (Choseong): leave spacing as is.
141 Hangul Choseong Filler (U+115F): set width to 2.
143 Hangul Jungseong Filler, Hangul Vowel (Jungseong), and
144 Final Consonant (Jongseong): set width to 0, because these
145 combine with the leading consonant as one composite syllabic
146 glyph. As of Unicode 5.2, the Hangul Jamo block (U+1100..U+11FF)
147 is completely filled.
149 for (i = 0x1160; i <= 0x11FF; i++) plane0width[i]=0; /* Vowels & Final Consonants */
152 Private Use Area -- the width is undefined, but likely
153 to be 2 charcells wide either from a graphic glyph or
154 from a four-digit hexadecimal glyph representing the
155 code point. Therefore if any PUA glyph does not have
156 a non-zero width yet, assign it a default width of 2.
157 The Unicode Standard allows giving PUA characters
158 default property values; see for example The Unicode
159 Standard Version 5.0, p. 91. This same default is
160 used for higher plane PUA code points below.
162 for (i = 0xE000; i <= 0xF8FF; i++) {
163 if (plane0width[i] == 0) plane0width[i]=2;
169 for (i = 0xFDD0; i <= 0xFDEF; i++) plane0width[i] = -1;
170 plane0width[0xFFFE] = -1; /* Byte Order Mark */
171 plane0width[0xFFFF] = -1; /* Byte Order Mark */
173 /* Surrogate Code Points */
174 for (i = 0xD800; i <= 0xDFFF; i++) plane0width[i]=-1;
178 Now generate the output file.
181 printf (" wcwidth and wcswidth functions, as per IEEE 1003.1-2008\n");
182 printf (" System Interfaces, pp. 2241 and 2251.\n\n");
183 printf (" Author: Paul Hardy, 2013\n\n");
184 printf (" Copyright (c) 2013 Paul Hardy\n\n");
185 printf (" LICENSE:\n");
187 printf (" This program is free software: you can redistribute it and/or modify\n");
188 printf (" it under the terms of the GNU General Public License as published by\n");
189 printf (" the Free Software Foundation, either version 2 of the License, or\n");
190 printf (" (at your option) any later version.\n");
192 printf (" This program is distributed in the hope that it will be useful,\n");
193 printf (" but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
194 printf (" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n");
195 printf (" GNU General Public License for more details.\n");
197 printf (" You should have received a copy of the GNU General Public License\n");
198 printf (" along with this program. If not, see <http://www.gnu.org/licenses/>.\n");
201 printf ("#include <wchar.h>\n\n");
202 printf ("#define PLANE1_ZEROES 177\n\n");
204 printf ("/* wcwidth -- return charcell positions of one code point */\n");
205 printf ("inline int\nwcwidth (wchar_t wc)\n{\n");
206 printf (" return (wcswidth (&wc, 1));\n");
209 printf ("int\nwcswidth (const wchar_t *pwcs, size_t n)\n{\n\n");
210 printf (" int i; /* loop variable */\n");
211 printf (" unsigned codept; /* Unicode code point of current character */\n");
212 printf (" unsigned plane; /* Unicode plane, 0x00..0x10 */\n");
213 printf (" unsigned lower16; /* lower 16 bits of Unicode code point */\n");
214 printf (" int lowpt, midpt, highpt; /* for binary searching in plane1zeroes[] */\n");
215 printf (" int found; /* for binary searching in plane1zeroes[] */\n");
216 printf (" int totalwidth; /* total width of string, in charcells (1 or 2/glyph) */\n");
217 printf (" int illegalchar; /* Whether or not tihs code point is illegal */\n");
221 Print the plane0width[] array for glyphs widths in the
222 Basic Multilingual Plane (Plane 0).
224 printf (" char plane0width[0x10000] = {");
225 for (i = 0; i < 0x10000; i++) {
227 printf ("\n /* U+%04X */ ", i);
228 printf ("%d,", plane0width[i]);
233 Print wide zero-width glyph code points in the
234 Supplemental Lingual Plane (Plane 1).
238 printf (" Zero-width code points in Supplemental Multilingual Plane\n");
240 printf (" int plane1zeroes[PLANE1_ZEROES] = {\n");
241 printf (" /* Phaistos Disc */\n");
242 printf (" 0x0101FD,\n");
243 printf (" /* Kharoshthi Combining Glyphs */\n");
244 printf (" 0x010A01,0x010A02,0x010A03,0x010A05,0x010A06,0x010A0C,0x010A0D,\n");
245 printf (" 0x010A0E,0x010A0F,0x010A38,0x010A39,0x010A3A,0x010A3F,\n");
246 printf (" /* Brahmi Combining Glyphs */\n");
247 printf (" 0x011000,0x011001,0x011002,0x011038,0x011039,0x01103A,0x01103B,\n");
248 printf (" 0x01103C,0x01103D,0x01103E,0x01103F,0x011040,0x011041,0x011042,\n");
249 printf (" 0x011043,0x011044,0x011045,0x011046,\n");
250 printf (" /* Kaithi Combining Glyphs */\n");
251 printf (" 0x011080,0x011081,0x011082,0x0110B0,0x0110B1,0x0110B2,0x0110B3,\n");
252 printf (" 0x0110B4,0x0110B5,0x0110B6,0x0110B7,0x0110B8,0x0110B9,0x0110BA,\n");
253 printf (" /* Chakma Combining Glyphs */\n");
254 printf (" 0x011100,0x011101,0x011102,0x011127,0x011128,0x011129,0x01112A,\n");
255 printf (" 0x01112B,0x01112C,0x01112D,0x01112E,0x01112F,0x011130,0x011131,\n");
256 printf (" 0x011132,0x011133,0x011134,\n");
257 printf (" /* Sharada Combining Glyphs */\n");
258 printf (" 0x011180,0x011181,0x011182,0x0111B3,0x0111B4,0x0111B5,0x0111B6,\n");
259 printf (" 0x0111B7,0x0111B8,0x0111B9,0x0111BA,0x0111BB,0x0111BC,0x0111BD,\n");
260 printf (" 0x0111BE,0x0111BF,0x0111C0,\n");
261 printf (" /* Takri Combining Glyphs */\n");
262 printf (" 0x0116AB,0x0116AC,0x0116AD,0x0116AE,0x0116AF,0x0116B0,0x0116B1,\n");
263 printf (" 0x0116B2,0x0116B3,0x0116B4,0x0116B5,0x0116B6,0x0116B7,\n");
264 printf (" /* Miao Combining Glyphs */\n");
265 printf (" 0x016F51,0x016F52,0x016F53,0x016F54,0x016F55,0x016F56,0x016F57,\n");
266 printf (" 0x016F58,0x016F59,0x016F5A,0x016F5B,0x016F5C,0x016F5D,0x016F5E,\n");
267 printf (" 0x016F5F,0x016F60,0x016F61,0x016F62,0x016F63,0x016F64,0x016F65,\n");
268 printf (" 0x016F66,0x016F67,0x016F68,0x016F69,0x016F6A,0x016F6B,0x016F6C,\n");
269 printf (" 0x016F6D,0x016F6E,0x016F6F,0x016F70,0x016F71,0x016F72,0x016F73,\n");
270 printf (" 0x016F74,0x016F75,0x016F76,0x016F77,0x016F78,0x016F79,0x016F7A,\n");
271 printf (" 0x016F7B,0x016F7C,0x016F7D,0x016F7E,\n");
272 printf (" 0x016F8F,0x016F90,0x016F91,0x016F92,\n");
273 printf (" /* Musical Symbols Combining Glyphs */\n");
274 printf (" 0x01D159,0x01D165,0x01D166,0x01D167,0x01D168,0x01D169,0x01D16D,\n");
275 printf (" 0x01D16E,0x01D16F,0x01D170,0x01D171,0x01D172,0x01D17B,0x01D17C,\n");
276 printf (" 0x01D17D,0x01D17E,0x01D17F,0x01D180,0x01D181,0x01D182,0x01D185,\n");
277 printf (" 0x01D186,0x01D187,0x01D188,0x01D189,0x01D18A,0x01D18B,0x01D1AA,\n");
278 printf (" 0x01D1AB,0x01D1AC,0x01D1AD,\n");
279 printf (" /* Ancient Greek Musical Notation */\n");
280 printf (" 0x01D242,0x01D243,0x01D244\n");
284 Execution part of wcswidth.
287 printf (" illegalchar = totalwidth = 0;\n");
288 printf (" for (i = 0; !illegalchar && i < n; i++) {\n");
289 printf (" codept = pwcs[i];\n");
290 printf (" plane = codept >> 16;\n");
291 printf (" lower16 = codept & 0xFFFF;\n");
292 printf (" if (plane == 0) { /* the most common case */\n");
293 printf (" if (plane0width[lower16] < 0) illegalchar = 1;\n");
294 printf (" else totalwidth += plane0width[lower16];\n");
296 printf (" else { /* a higher plane or beyond Unicode range */\n");
297 printf (" if (lower16 == 0xFFFE || lower16 == 0xFFFF) {\n");
298 printf (" illegalchar = 1;\n");
300 printf (" else if (plane > 0x10) {\n");
301 printf (" illegalchar = 1;\n");
303 printf (" else if (plane == 1) { /* Supplementary Multilingual Plane */\n");
305 printf (" Perform a binary search in plane1zeroes[] sparse list for\n");
306 printf (" combining code points and other zero-width code points.\n");
308 printf (" lowpt = 0;\n");
309 printf (" highpt = PLANE1_ZEROES - 1;\n");
311 printf (" while (lowpt < highpt) {\n");
312 printf (" midpt = (lowpt + highpt) >> 1;\n");
313 printf (" if (plane1zeroes[midpt] < codept)\n");
314 printf (" lowpt = midpt + 1;\n");
315 printf (" else if (plane1zeroes[midpt] > codept)\n");
316 printf (" highpt = midpt - 1;\n");
318 printf (" lowpt = highpt = midpt; /* found the match */\n");
321 printf (" if (lowpt >= 0 && codept == plane1zeroes[lowpt]) {\n");
322 printf (" found = 1;\n");
324 printf (" else if (highpt < PLANE1_ZEROES && codept == plane1zeroes[highpt]) {\n");
325 printf (" found = 1;\n");
327 printf (" else {\n");
328 printf (" found = 0;\n");
331 printf (" if (!found) totalwidth += 2; /* default for SMP glyphs */\n");
333 printf (" /* Other non-printing in higher planes; return -1 as per IEEE 1003.1-2008. */\n");
334 printf (" else if (/* language tags */\n");
335 printf (" codept == 0x0E0001 || (codept >= 0x0E0020 && codept <= 0x0E007F) ||\n");
336 printf (" /* variation selectors, 0x0E0100..0x0E01EF */\n");
337 printf (" (codept >= 0x0E0100 && codept <= 0x0E01EF)) {\n");
338 printf (" illegalchar = 1;\n");
341 printf (" Unicode plane 0x02..0x10 printing character\n");
343 printf (" else {\n");
344 printf (" totalwidth += 2; /* default width of 2 charcells for legal glyphs */\n");
349 printf (" if (illegalchar) totalwidth = -1;\n");
351 printf (" return (totalwidth);\n");