"Fossies" - the Fresh Open Source Software Archive

Member "unifont-12.1.02/src/unipagecount.c" (21 Jun 2018, 7604 Bytes) of package /linux/misc/unifont-12.1.02.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "unipagecount.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 11.0.01_vs_11.0.02.

    1 /*
    2    unipagecount - program to count the number of glyphs defined in each page
    3                   of 256 bytes, and print them in an 8 x 8 grid.  Input is
    4                   from stdin.  Output is to stdout.
    5 
    6    Synopsis: unipagecount < font_file.hex > count.txt
    7              unipagecount -phex_page_num < font_file.hex  -- just 256 points
    8              unipagecount -h < font_file.hex              -- HTML table
    9              unipagecount -P1 -h < font.hex > count.html  -- Plane 1, HTML out
   10              unipagecount -l < font_file.hex              -- linked HTML table
   11 
   12    Author: Paul Hardy, unifoundry <at> unifoundry.com, December 2007
   13    
   14    Copyright (C) 2007, 2008, 2013, 2014 Paul Hardy
   15 
   16    LICENSE:
   17 
   18       This program is free software: you can redistribute it and/or modify
   19       it under the terms of the GNU General Public License as published by
   20       the Free Software Foundation, either version 2 of the License, or
   21       (at your option) any later version.
   22 
   23       This program is distributed in the hope that it will be useful,
   24       but WITHOUT ANY WARRANTY; without even the implied warranty of
   25       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   26       GNU General Public License for more details.
   27 
   28       You should have received a copy of the GNU General Public License
   29       along with this program.  If not, see <http://www.gnu.org/licenses/>.
   30 */
   31 
   32 /*
   33    2018, Paul Hardy: Changed "Private Use" to "Private Use Area" in
   34    output HTML file.
   35 */
   36 
   37 #include <stdio.h>
   38 #include <stdlib.h>
   39 
   40 #define MAXBUF 256 /* maximum input line size */
   41 
   42 int
   43 main (int argc, char *argv[])
   44 {
   45 
   46    char inbuf[MAXBUF]; /* Max 256 characters in an input line */
   47    int i, j;  /* loop variables */
   48    unsigned plane=0; /* Unicode plane number, 0 to 0x16 */
   49    unsigned page;  /* unicode page (256 bytes wide) */
   50    unsigned unichar; /* unicode character */
   51    int pagecount[256] = {256 * 0};
   52    int onepage=0; /* set to one if printing character grid for one page */
   53    int pageno=0; /* page number selected if only examining one page */
   54    int html=0;   /* =0: print plain text; =1: print HTML */
   55    int links=0;  /* =1: print HTML links; =0: don't print links */
   56    void mkftable();  /* make (print) flipped HTML table */
   57 
   58    size_t strlen();
   59 
   60    if (argc > 1 && argv[1][0] == '-') {  /* Parse option */
   61       plane = 0;
   62       for (i = 1; i < argc; i++) {
   63          switch (argv[i][1]) {
   64             case 'p':  /* specified -p<hexpage> -- use given page number */
   65                sscanf (&argv[1][2], "%x", &pageno);
   66                if (pageno >= 0 && pageno <= 255) onepage = 1;
   67                break;
   68             case 'h':  /* print HTML table instead of text table */
   69                html = 1;
   70                break;
   71             case 'l':  /* print hyperlinks in HTML table */
   72                links = 1;
   73                html = 1;
   74                break;
   75             case 'P':  /* Plane number specified */
   76                plane = atoi(&argv[1][2]);
   77                break;
   78          }
   79       }
   80    }
   81    /*
   82       Initialize pagecount to account for noncharacters.
   83    */
   84    if (!onepage && plane==0) {
   85       pagecount[0xfd] = 32;  /* for U+FDD0..U+FDEF */
   86    }
   87    pagecount[0xff] = 2;   /* for U+nnFFFE, U+nnFFFF */
   88    /*
   89       Read one line at a time from input.  The format is:
   90 
   91          <hexpos>:<hexbitmap>
   92 
   93       where <hexpos> is the hexadecimal Unicode character position
   94       in the range 00..FF and <hexbitmap> is the sequence of hexadecimal
   95       digits of the character, laid out in a grid from left to right,
   96       top to bottom.  The character is assumed to be 16 rows of variable
   97       width.
   98    */
   99    while (fgets (inbuf, MAXBUF-1, stdin) != NULL) {
  100       sscanf (inbuf, "%X", &unichar);
  101       page = unichar >> 8;
  102       if (onepage) { /* only increment counter if this is page we want */
  103          if (page == pageno) { /* character is in the page we want */
  104             pagecount[unichar & 0xff]++; /* mark character as covered */
  105          }
  106       }
  107       else { /* counting all characters in all pages */
  108          if (plane == 0) {
  109             /* Don't add in noncharacters (U+FDD0..U+FDEF, U+FFFE, U+FFFF) */
  110             if (unichar < 0xfdd0 || (unichar > 0xfdef && unichar < 0xfffe))
  111                pagecount[page]++;
  112          }
  113          else {
  114             if ((page >> 8) == plane) { /* code point is in desired plane */
  115                pagecount[page & 0xFF]++;
  116             }
  117          }
  118       }
  119    }
  120    if (html) {
  121       mkftable (plane, pagecount, links);
  122    }
  123    else {  /* Otherwise, print plain text table */
  124       if (plane > 0) fprintf (stdout, "  ");
  125       fprintf (stdout,
  126          "   0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F\n");
  127       for (i=0; i<0x10; i++) {
  128          fprintf (stdout,"%02X%X ", plane, i); /* row header */
  129          for (j=0; j<0x10; j++) {
  130             if (onepage) {
  131                if (pagecount[i*16+j])
  132                   fprintf (stdout," *  ");
  133                else
  134                   fprintf (stdout," .  ");
  135             }
  136             else {
  137                fprintf (stdout, "%3X ", pagecount[i*16+j]);
  138             }
  139          }
  140          fprintf (stdout,"\n");
  141       }
  142    
  143    }
  144    exit (0);
  145 }
  146 
  147 
  148 /*
  149    mkftable - function to create an HTML table to show PNG files
  150               in a 16 by 16 grid.
  151 */
  152 
  153 void
  154 mkftable (unsigned plane, int pagecount[256], int links)
  155 {
  156    int i, j;
  157    int count;
  158    unsigned bgcolor;
  159    
  160    printf ("<html>\n");
  161    printf ("<body>\n");
  162    printf ("<table border=\"3\" align=\"center\">\n");
  163    printf ("  <tr><th colspan=\"16\" bgcolor=\"#ffcc80\">");
  164    printf ("GNU Unifont Glyphs<br>with Page Coverage for Plane %d<br>(Green=100%%, Red=0%%)</th></tr>\n", plane);
  165    for (i = 0x0; i <= 0xF; i++) {
  166       printf ("  <tr>\n");
  167       for (j = 0x0; j <= 0xF; j++) {
  168          count = pagecount[ (i << 4) | j ];
  169          
  170          /* print link in cell if links == 1 */
  171          if (plane != 0 || (i < 0xd || (i == 0xd && j < 0x8) || (i == 0xf && j > 0x8))) {
  172             /* background color is light green if completely done */
  173             if (count == 0x100) bgcolor = 0xccffcc;
  174             /* otherwise background is a shade of yellow to orange to red */
  175             else bgcolor = 0xff0000 | (count << 8) | (count >> 1);
  176             printf ("    <td bgcolor=\"#%06X\">", bgcolor);
  177             if (plane == 0)
  178                printf ("<a href=\"png/plane%02X/uni%02X%X%X.png\">%X%X</a>", plane, plane, i, j, i, j);
  179             else
  180                printf ("<a href=\"png/plane%02X/uni%02X%X%X.png\">%02X%X%X</a>", plane, plane, i, j, plane, i, j);
  181             printf ("</td>\n");
  182          }
  183          else if (i == 0xd) {
  184             if (j == 0x8) {
  185                printf ("    <td align=\"center\" colspan=\"8\" bgcolor=\"#cccccc\">");
  186                printf ("<b>Surrogate Pairs</b>");
  187                printf ("</td>\n");
  188             }  /* otherwise don't print anything more columns in this row */
  189          }
  190          else if (i == 0xe) {
  191             if (j == 0x0) {
  192                printf ("    <td align=\"center\" colspan=\"16\" bgcolor=\"#cccccc\">");
  193                printf ("<b>Private Use Area</b>");
  194                printf ("</td>\n");
  195             }  /* otherwise don't print any more columns in this row */
  196          }
  197          else if (i == 0xf) {
  198             if (j == 0x0) {
  199                printf ("    <td align=\"center\" colspan=\"9\" bgcolor=\"#cccccc\">");
  200                printf ("<b>Private Use Area</b>");
  201                printf ("</td>\n");
  202             }
  203          }
  204       }
  205       printf ("  </tr>\n");
  206    }
  207    printf ("</table>\n");
  208    printf ("</body>\n");
  209    printf ("</html>\n");
  210 
  211    return;
  212 }