unifont-6.3.20131215.tar.gz
[unifont.git] / src / unigenwidth.c
index e34eaf86acf1326f97ff503dcca6dae0209b2040..b12fb64912ca213f55053f9e745ac2a399459501 100644 (file)
@@ -27,7 +27,9 @@
 #define MAXSTRING      256
 
 
-int main(int argc, char **argv) {
+int
+main (int argc, char **argv)
+{
 
    int i; /* loop variable */
 
@@ -41,41 +43,41 @@ int main(int argc, char **argv) {
 
    /*
       if (argc != 3) {
-         fprintf(stderr, "\n\nUsage: %s <unifont-file.hex> <combining.txt>\n\n");
-         exit(EXIT_FAILURE);
+         fprintf (stderr, "\n\nUsage: %s <unifont-file.hex> <combining.txt>\n\n");
+         exit (EXIT_FAILURE);
       }
    */
 
    /*
       Read the collection of hex glyphs.
    */
-   if ((infilefp = fopen(argv[1],"r")) == NULL) {
-      fprintf(stderr,"ERROR - hex input file %s not found.\n\n", argv[1]);
-      exit(EXIT_FAILURE);
+   if ((infilefp = fopen (argv[1],"r")) == NULL) {
+      fprintf (stderr,"ERROR - hex input file %s not found.\n\n", argv[1]);
+      exit (EXIT_FAILURE);
    }
 
-   memset(plane0width, 0, 0x10000 * sizeof(char));
+   memset (plane0width, 0, 0x10000 * sizeof (char));
 
    teststring[MAXSTRING] = '\0';
-   while (fgets(teststring, MAXSTRING-1, infilefp) != NULL) {
-      sscanf(teststring, "%X", &loc);
-      gstart = index(teststring,':') + 1;
-      plane0width[loc] = strlen(gstart) <= 34 ? 1 : 2;
+   while (fgets (teststring, MAXSTRING-1, infilefp) != NULL) {
+      sscanf (teststring, "%X", &loc);
+      gstart = index (teststring,':') + 1;
+      plane0width[loc] = strlen (gstart) <= 34 ? 1 : 2;
    }
 
-   fclose(infilefp);
+   fclose (infilefp);
 
    /*
       Now read the combining character code points.  These have width of 0.
    */
-   if ((infilefp = fopen(argv[2],"r")) == NULL) {
-      fprintf(stderr,"ERROR - combining characters file %s not found.\n\n", argv[2]);
-      exit(EXIT_FAILURE);
+   if ((infilefp = fopen (argv[2],"r")) == NULL) {
+      fprintf (stderr,"ERROR - combining characters file %s not found.\n\n", argv[2]);
+      exit (EXIT_FAILURE);
    }
 
-   while (fscanf(infilefp, "%X", &loc) != EOF) plane0width[loc] = 0;
+   while (fscanf (infilefp, "%X", &loc) != EOF) plane0width[loc] = 0;
 
-   fclose(infilefp);
+   fclose (infilefp);
 
    /*
       Code Points with Unusual Properties (Unicode Standard, Chapter 4)
@@ -175,180 +177,180 @@ int main(int argc, char **argv) {
    /*
       Now generate the output file.
    */
-   printf("/*\n");
-   printf("   wcwidth() and wcswidth() functions, as per IEEE 1003.1-2008\n");
-   printf("   System Interfaces, pp. 2241 and 2251.\n\n");
-   printf("   Author: Paul Hardy, 2013\n\n");
-   printf("   Copyright (c) 2013 Paul Hardy\n\n");
-   printf("   LICENSE:\n");
-   printf("\n");
-   printf("      This program is free software: you can redistribute it and/or modify\n");
-   printf("      it under the terms of the GNU General Public License as published by\n");
-   printf("      the Free Software Foundation, either version 2 of the License, or\n");
-   printf("      (at your option) any later version.\n");
-   printf("\n");
-   printf("      This program is distributed in the hope that it will be useful,\n");
-   printf("      but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
-   printf("      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n");
-   printf("      GNU General Public License for more details.\n");
-   printf("\n");
-   printf("      You should have received a copy of the GNU General Public License\n");
-   printf("      along with this program.  If not, see <http://www.gnu.org/licenses/>.\n");
-   printf("*/\n\n");
-
-   printf("#include <wchar.h>\n\n");
-   printf("#define PLANE1_ZEROES 177\n\n");
-   printf("\n\n");
-   printf("/* wcwidth() -- return charcell positions of one code point */\n");
-   printf("inline int wcwidth(wchar_t wc) {\n");
-   printf("   return(wcswidth(&wc, 1));\n");
-   printf("}\n");
-   printf("\n\n");
-   printf("int wcswidth(const wchar_t *pwcs, size_t n) {\n\n");
-   printf("   int i;                    /* loop variable                                      */\n");
-   printf("   unsigned codept;          /* Unicode code point of current character            */\n");
-   printf("   unsigned plane;           /* Unicode plane, 0x00..0x10                          */\n");
-   printf("   unsigned lower16;         /* lower 16 bits of Unicode code point                */\n");
-   printf("   int lowpt, midpt, highpt; /* for binary searching in plane1zeroes[]             */\n");
-   printf("   int found;                /* for binary searching in plane1zeroes[]             */\n");
-   printf("   int totalwidth;           /* total width of string, in charcells (1 or 2/glyph) */\n");
-   printf("   int illegalchar;          /* Whether or not tihs code point is illegal          */\n");
-   putchar('\n');
+   printf ("/*\n");
+   printf ("   wcwidth and wcswidth functions, as per IEEE 1003.1-2008\n");
+   printf ("   System Interfaces, pp. 2241 and 2251.\n\n");
+   printf ("   Author: Paul Hardy, 2013\n\n");
+   printf ("   Copyright (c) 2013 Paul Hardy\n\n");
+   printf ("   LICENSE:\n");
+   printf ("\n");
+   printf ("      This program is free software: you can redistribute it and/or modify\n");
+   printf ("      it under the terms of the GNU General Public License as published by\n");
+   printf ("      the Free Software Foundation, either version 2 of the License, or\n");
+   printf ("      (at your option) any later version.\n");
+   printf ("\n");
+   printf ("      This program is distributed in the hope that it will be useful,\n");
+   printf ("      but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
+   printf ("      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n");
+   printf ("      GNU General Public License for more details.\n");
+   printf ("\n");
+   printf ("      You should have received a copy of the GNU General Public License\n");
+   printf ("      along with this program.  If not, see <http://www.gnu.org/licenses/>.\n");
+   printf ("*/\n\n");
+
+   printf ("#include <wchar.h>\n\n");
+   printf ("#define PLANE1_ZEROES 177\n\n");
+   printf ("\n\n");
+   printf ("/* wcwidth -- return charcell positions of one code point */\n");
+   printf ("inline int\nwcwidth (wchar_t wc)\n{\n");
+   printf ("   return (wcswidth (&wc, 1));\n");
+   printf ("}\n");
+   printf ("\n\n");
+   printf ("int\nwcswidth (const wchar_t *pwcs, size_t n)\n{\n\n");
+   printf ("   int i;                    /* loop variable                                      */\n");
+   printf ("   unsigned codept;          /* Unicode code point of current character            */\n");
+   printf ("   unsigned plane;           /* Unicode plane, 0x00..0x10                          */\n");
+   printf ("   unsigned lower16;         /* lower 16 bits of Unicode code point                */\n");
+   printf ("   int lowpt, midpt, highpt; /* for binary searching in plane1zeroes[]             */\n");
+   printf ("   int found;                /* for binary searching in plane1zeroes[]             */\n");
+   printf ("   int totalwidth;           /* total width of string, in charcells (1 or 2/glyph) */\n");
+   printf ("   int illegalchar;          /* Whether or not tihs code point is illegal          */\n");
+   putchar ('\n');
 
    /*
       Print the plane0width[] array for glyphs widths in the
       Basic Multilingual Plane (Plane 0).
    */
-   printf("   char plane0width[0x10000] = {");
+   printf ("   char plane0width[0x10000] = {");
    for (i = 0; i < 0x10000; i++) {
       if ((i & 0x1F) == 0)
-         printf("\n      /* U+%04X */ ", i);
-      printf("%d,", plane0width[i]);
+         printf ("\n      /* U+%04X */ ", i);
+      printf ("%d,", plane0width[i]);
    }
-   printf("};\n\n");
+   printf ("};\n\n");
 
    /*
       Print wide zero-width glyph code points in the
       Supplemental Lingual Plane (Plane 1).
    */
-   printf("\n");
-   printf("   /*\n");
-   printf("      Zero-width code points in Supplemental Multilingual Plane\n");
-   printf("   */\n");
-   printf("   int plane1zeroes[PLANE1_ZEROES] = {\n");
-   printf("      /* Phaistos Disc */\n");
-   printf("      0x0101FD,\n");
-   printf("      /* Kharoshthi Combining Glyphs */\n");
-   printf("      0x010A01,0x010A02,0x010A03,0x010A05,0x010A06,0x010A0C,0x010A0D,\n");
-   printf("      0x010A0E,0x010A0F,0x010A38,0x010A39,0x010A3A,0x010A3F,\n");
-   printf("      /* Brahmi Combining Glyphs */\n");
-   printf("      0x011000,0x011001,0x011002,0x011038,0x011039,0x01103A,0x01103B,\n");
-   printf("      0x01103C,0x01103D,0x01103E,0x01103F,0x011040,0x011041,0x011042,\n");
-   printf("      0x011043,0x011044,0x011045,0x011046,\n");
-   printf("      /* Kaithi Combining Glyphs */\n");
-   printf("      0x011080,0x011081,0x011082,0x0110B0,0x0110B1,0x0110B2,0x0110B3,\n");
-   printf("      0x0110B4,0x0110B5,0x0110B6,0x0110B7,0x0110B8,0x0110B9,0x0110BA,\n");
-   printf("      /* Chakma Combining Glyphs */\n");
-   printf("      0x011100,0x011101,0x011102,0x011127,0x011128,0x011129,0x01112A,\n");
-   printf("      0x01112B,0x01112C,0x01112D,0x01112E,0x01112F,0x011130,0x011131,\n");
-   printf("      0x011132,0x011133,0x011134,\n");
-   printf("      /* Sharada Combining Glyphs */\n");
-   printf("      0x011180,0x011181,0x011182,0x0111B3,0x0111B4,0x0111B5,0x0111B6,\n");
-   printf("      0x0111B7,0x0111B8,0x0111B9,0x0111BA,0x0111BB,0x0111BC,0x0111BD,\n");
-   printf("      0x0111BE,0x0111BF,0x0111C0,\n");
-   printf("      /* Takri Combining Glyphs */\n");
-   printf("      0x0116AB,0x0116AC,0x0116AD,0x0116AE,0x0116AF,0x0116B0,0x0116B1,\n");
-   printf("      0x0116B2,0x0116B3,0x0116B4,0x0116B5,0x0116B6,0x0116B7,\n");
-   printf("      /* Miao Combining Glyphs */\n");
-   printf("      0x016F51,0x016F52,0x016F53,0x016F54,0x016F55,0x016F56,0x016F57,\n");
-   printf("      0x016F58,0x016F59,0x016F5A,0x016F5B,0x016F5C,0x016F5D,0x016F5E,\n");
-   printf("      0x016F5F,0x016F60,0x016F61,0x016F62,0x016F63,0x016F64,0x016F65,\n");
-   printf("      0x016F66,0x016F67,0x016F68,0x016F69,0x016F6A,0x016F6B,0x016F6C,\n");
-   printf("      0x016F6D,0x016F6E,0x016F6F,0x016F70,0x016F71,0x016F72,0x016F73,\n");
-   printf("      0x016F74,0x016F75,0x016F76,0x016F77,0x016F78,0x016F79,0x016F7A,\n");
-   printf("      0x016F7B,0x016F7C,0x016F7D,0x016F7E,\n");
-   printf("      0x016F8F,0x016F90,0x016F91,0x016F92,\n");
-   printf("      /* Musical Symbols Combining Glyphs */\n");
-   printf("      0x01D159,0x01D165,0x01D166,0x01D167,0x01D168,0x01D169,0x01D16D,\n");
-   printf("      0x01D16E,0x01D16F,0x01D170,0x01D171,0x01D172,0x01D17B,0x01D17C,\n");
-   printf("      0x01D17D,0x01D17E,0x01D17F,0x01D180,0x01D181,0x01D182,0x01D185,\n");
-   printf("      0x01D186,0x01D187,0x01D188,0x01D189,0x01D18A,0x01D18B,0x01D1AA,\n");
-   printf("      0x01D1AB,0x01D1AC,0x01D1AD,\n");
-   printf("      /* Ancient Greek Musical Notation */\n");
-   printf("      0x01D242,0x01D243,0x01D244\n");
-   printf("   };\n\n");
+   printf ("\n");
+   printf ("   /*\n");
+   printf ("      Zero-width code points in Supplemental Multilingual Plane\n");
+   printf ("   */\n");
+   printf ("   int plane1zeroes[PLANE1_ZEROES] = {\n");
+   printf ("      /* Phaistos Disc */\n");
+   printf ("      0x0101FD,\n");
+   printf ("      /* Kharoshthi Combining Glyphs */\n");
+   printf ("      0x010A01,0x010A02,0x010A03,0x010A05,0x010A06,0x010A0C,0x010A0D,\n");
+   printf ("      0x010A0E,0x010A0F,0x010A38,0x010A39,0x010A3A,0x010A3F,\n");
+   printf ("      /* Brahmi Combining Glyphs */\n");
+   printf ("      0x011000,0x011001,0x011002,0x011038,0x011039,0x01103A,0x01103B,\n");
+   printf ("      0x01103C,0x01103D,0x01103E,0x01103F,0x011040,0x011041,0x011042,\n");
+   printf ("      0x011043,0x011044,0x011045,0x011046,\n");
+   printf ("      /* Kaithi Combining Glyphs */\n");
+   printf ("      0x011080,0x011081,0x011082,0x0110B0,0x0110B1,0x0110B2,0x0110B3,\n");
+   printf ("      0x0110B4,0x0110B5,0x0110B6,0x0110B7,0x0110B8,0x0110B9,0x0110BA,\n");
+   printf ("      /* Chakma Combining Glyphs */\n");
+   printf ("      0x011100,0x011101,0x011102,0x011127,0x011128,0x011129,0x01112A,\n");
+   printf ("      0x01112B,0x01112C,0x01112D,0x01112E,0x01112F,0x011130,0x011131,\n");
+   printf ("      0x011132,0x011133,0x011134,\n");
+   printf ("      /* Sharada Combining Glyphs */\n");
+   printf ("      0x011180,0x011181,0x011182,0x0111B3,0x0111B4,0x0111B5,0x0111B6,\n");
+   printf ("      0x0111B7,0x0111B8,0x0111B9,0x0111BA,0x0111BB,0x0111BC,0x0111BD,\n");
+   printf ("      0x0111BE,0x0111BF,0x0111C0,\n");
+   printf ("      /* Takri Combining Glyphs */\n");
+   printf ("      0x0116AB,0x0116AC,0x0116AD,0x0116AE,0x0116AF,0x0116B0,0x0116B1,\n");
+   printf ("      0x0116B2,0x0116B3,0x0116B4,0x0116B5,0x0116B6,0x0116B7,\n");
+   printf ("      /* Miao Combining Glyphs */\n");
+   printf ("      0x016F51,0x016F52,0x016F53,0x016F54,0x016F55,0x016F56,0x016F57,\n");
+   printf ("      0x016F58,0x016F59,0x016F5A,0x016F5B,0x016F5C,0x016F5D,0x016F5E,\n");
+   printf ("      0x016F5F,0x016F60,0x016F61,0x016F62,0x016F63,0x016F64,0x016F65,\n");
+   printf ("      0x016F66,0x016F67,0x016F68,0x016F69,0x016F6A,0x016F6B,0x016F6C,\n");
+   printf ("      0x016F6D,0x016F6E,0x016F6F,0x016F70,0x016F71,0x016F72,0x016F73,\n");
+   printf ("      0x016F74,0x016F75,0x016F76,0x016F77,0x016F78,0x016F79,0x016F7A,\n");
+   printf ("      0x016F7B,0x016F7C,0x016F7D,0x016F7E,\n");
+   printf ("      0x016F8F,0x016F90,0x016F91,0x016F92,\n");
+   printf ("      /* Musical Symbols Combining Glyphs */\n");
+   printf ("      0x01D159,0x01D165,0x01D166,0x01D167,0x01D168,0x01D169,0x01D16D,\n");
+   printf ("      0x01D16E,0x01D16F,0x01D170,0x01D171,0x01D172,0x01D17B,0x01D17C,\n");
+   printf ("      0x01D17D,0x01D17E,0x01D17F,0x01D180,0x01D181,0x01D182,0x01D185,\n");
+   printf ("      0x01D186,0x01D187,0x01D188,0x01D189,0x01D18A,0x01D18B,0x01D1AA,\n");
+   printf ("      0x01D1AB,0x01D1AC,0x01D1AD,\n");
+   printf ("      /* Ancient Greek Musical Notation */\n");
+   printf ("      0x01D242,0x01D243,0x01D244\n");
+   printf ("   };\n\n");
 
    /*
-      Execution part of wcswidth().
+      Execution part of wcswidth.
    */
-   printf("\n");
-   printf("   illegalchar = totalwidth = 0;\n");
-   printf("   for (i = 0; !illegalchar && i < n; i++) {\n");
-   printf("      codept  = pwcs[i];\n");
-   printf("      plane   = codept >> 16;\n");
-   printf("      lower16 = codept & 0xFFFF;\n");
-   printf("      if (plane == 0) { /* the most common case */\n");
-   printf("         if (plane0width[lower16] < 0) illegalchar = 1;\n");
-   printf("         else totalwidth += plane0width[lower16];\n");
-   printf("      }\n");
-   printf("      else { /* a higher plane or beyond Unicode range */\n");
-   printf("         if (lower16 == 0xFFFE || lower16 == 0xFFFF) {\n");
-   printf("            illegalchar = 1;\n");
-   printf("         }\n");
-   printf("         else if (plane > 0x10) {\n");
-   printf("            illegalchar = 1;\n");
-   printf("         }\n");
-   printf("         else if (plane == 1) { /* Supplementary Multilingual Plane */\n");
-   printf("            /*\n");
-   printf("               Perform a binary search in plane1zeroes[] sparse list for\n");
-   printf("               combining code points and other zero-width code points.\n");
-   printf("            */\n");
-   printf("            lowpt  = 0;\n");
-   printf("            highpt = PLANE1_ZEROES - 1;\n");
-   printf("\n");
-   printf("            while (lowpt < highpt) {\n");
-   printf("               midpt  = (lowpt + highpt) >> 1;\n");
-   printf("               if (plane1zeroes[midpt] < codept)\n");
-   printf("                  lowpt = midpt + 1;\n");
-   printf("               else if (plane1zeroes[midpt] > codept)\n");
-   printf("                  highpt = midpt - 1;\n");
-   printf("               else\n");
-   printf("                  lowpt = highpt = midpt; /* found the match */\n");
-   printf("            }\n");
-   printf("\n");
-   printf("            if (lowpt >= 0 && codept == plane1zeroes[lowpt]) {\n");
-   printf("               found = 1;\n");
-   printf("            }\n");
-   printf("            else if (highpt < PLANE1_ZEROES && codept == plane1zeroes[highpt]) {\n");
-   printf("               found = 1;\n");
-   printf("            }\n");
-   printf("            else {\n");
-   printf("               found = 0;\n");
-   printf("            }\n");
-   printf("\n");
-   printf("            if (!found) totalwidth += 2; /* default for SMP glyphs */\n");
-   printf("         }\n");
-   printf("         /* Other non-printing in higher planes; return -1 as per IEEE 1003.1-2008. */\n");
-   printf("         else if (/* language tags */\n");
-   printf("                  codept == 0x0E0001 || (codept >= 0x0E0020 && codept <= 0x0E007F) ||\n");
-   printf("                  /* variation selectors, 0x0E0100..0x0E01EF */\n");
-   printf("                  (codept >= 0x0E0100 && codept <= 0x0E01EF)) {\n");
-   printf("            illegalchar = 1;\n");
-   printf("         }\n");
-   printf("         /*\n");
-   printf("            Unicode plane 0x02..0x10 printing character\n");
-   printf("         */\n");
-   printf("         else {\n");
-   printf("            totalwidth += 2; /* default width of 2 charcells for legal glyphs */\n");
-   printf("         }\n");
-   printf("\n");
-   printf("      }\n");
-   printf("   }\n");
-   printf("   if (illegalchar) totalwidth = -1;\n");
-   printf("\n");
-   printf("   return(totalwidth);\n");
-   printf("\n");
-   printf("}\n");
-
-   exit(EXIT_SUCCESS);
+   printf ("\n");
+   printf ("   illegalchar = totalwidth = 0;\n");
+   printf ("   for (i = 0; !illegalchar && i < n; i++) {\n");
+   printf ("      codept  = pwcs[i];\n");
+   printf ("      plane   = codept >> 16;\n");
+   printf ("      lower16 = codept & 0xFFFF;\n");
+   printf ("      if (plane == 0) { /* the most common case */\n");
+   printf ("         if (plane0width[lower16] < 0) illegalchar = 1;\n");
+   printf ("         else totalwidth += plane0width[lower16];\n");
+   printf ("      }\n");
+   printf ("      else { /* a higher plane or beyond Unicode range */\n");
+   printf ("         if  (lower16 == 0xFFFE || lower16 == 0xFFFF) {\n");
+   printf ("            illegalchar = 1;\n");
+   printf ("         }\n");
+   printf ("         else if (plane > 0x10) {\n");
+   printf ("            illegalchar = 1;\n");
+   printf ("         }\n");
+   printf ("         else if (plane == 1) { /* Supplementary Multilingual Plane */\n");
+   printf ("            /*\n");
+   printf ("               Perform a binary search in plane1zeroes[] sparse list for\n");
+   printf ("               combining code points and other zero-width code points.\n");
+   printf ("            */\n");
+   printf ("            lowpt  = 0;\n");
+   printf ("            highpt = PLANE1_ZEROES - 1;\n");
+   printf ("\n");
+   printf ("            while (lowpt < highpt) {\n");
+   printf ("               midpt  = (lowpt + highpt) >> 1;\n");
+   printf ("               if (plane1zeroes[midpt] < codept)\n");
+   printf ("                  lowpt = midpt + 1;\n");
+   printf ("               else if (plane1zeroes[midpt] > codept)\n");
+   printf ("                  highpt = midpt - 1;\n");
+   printf ("               else\n");
+   printf ("                  lowpt = highpt = midpt; /* found the match */\n");
+   printf ("            }\n");
+   printf ("\n");
+   printf ("            if (lowpt >= 0 && codept == plane1zeroes[lowpt]) {\n");
+   printf ("               found = 1;\n");
+   printf ("            }\n");
+   printf ("            else if (highpt < PLANE1_ZEROES && codept == plane1zeroes[highpt]) {\n");
+   printf ("               found = 1;\n");
+   printf ("            }\n");
+   printf ("            else {\n");
+   printf ("               found = 0;\n");
+   printf ("            }\n");
+   printf ("\n");
+   printf ("            if (!found) totalwidth += 2; /* default for SMP glyphs */\n");
+   printf ("         }\n");
+   printf ("         /* Other non-printing in higher planes; return -1 as per IEEE 1003.1-2008. */\n");
+   printf ("         else if (/* language tags */\n");
+   printf ("                  codept == 0x0E0001 || (codept >= 0x0E0020 && codept <= 0x0E007F) ||\n");
+   printf ("                  /* variation selectors, 0x0E0100..0x0E01EF */\n");
+   printf ("                  (codept >= 0x0E0100 && codept <= 0x0E01EF)) {\n");
+   printf ("            illegalchar = 1;\n");
+   printf ("         }\n");
+   printf ("         /*\n");
+   printf ("            Unicode plane 0x02..0x10 printing character\n");
+   printf ("         */\n");
+   printf ("         else {\n");
+   printf ("            totalwidth += 2; /* default width of 2 charcells for legal glyphs */\n");
+   printf ("         }\n");
+   printf ("\n");
+   printf ("      }\n");
+   printf ("   }\n");
+   printf ("   if (illegalchar) totalwidth = -1;\n");
+   printf ("\n");
+   printf ("   return (totalwidth);\n");
+   printf ("\n");
+   printf ("}\n");
+
+   exit (EXIT_SUCCESS);
 }