unibdf2hex: count lines not glyphs
[unifont.git] / src / unibdf2hex.c
index b421f6a7db268af0cecdd2d5cf57d3bc40bf7ce6..bbd4dc1def37ab1848d714d488dfc51b0efb5a4b 100644 (file)
@@ -35,52 +35,56 @@ int
 main()
 {
    int i;
-   int digitsout;  /* how many hex digits we output in a bitmap */
+   int rownum;  /* number of lines we output in bitmap */
    int thispoint;
    char inbuf[MAXBUF];
    int bbxx, bbxy, bbxxoff, bbxyoff;
 
-   int descent=4; /* font descent wrt baseline */
+   int descent=2; /* font descent wrt baseline */
    int startrow;  /* row to start glyph        */
+   int rowlen=4;  /* number of digits per row  */
    unsigned rowout;
 
-   while (fgets (inbuf, MAXBUF - 1, stdin) != NULL) {
-      if (strncmp (inbuf, "ENCODING ", 9) != 0) continue;
+   while (fgets (inbuf, MAXBUF - 1, stdin)) {
+      if (strncmp (inbuf, "ENCODING ", 9)) continue;
       sscanf (&inbuf[9], "%d", &thispoint); /* get code point */
-      /*
-         If we want this code point, get the BBX (bounding box) and
-         BITMAP information.
-      */
-      while (fgets (inbuf, MAXBUF - 1, stdin) != NULL &&
-             strncmp (inbuf, "BBX ", 4) != 0); /* find bounding box */
 
+      /* Read bounding box values from BBX line */
+      while (fgets (inbuf, MAXBUF - 1, stdin)) {
+        if (!strncmp (inbuf, "BBX ", 4)) break;
+      }
       sscanf (&inbuf[4], "%d %d %d %d", &bbxx, &bbxy, &bbxxoff, &bbxyoff);
-      while (fgets (inbuf, MAXBUF - 1, stdin) != NULL &&
-             strncmp (inbuf, "BITMAP", 6) != 0); /* find bitmap start */
+
+      /* Find BITMAP start */
+      while (fgets (inbuf, MAXBUF - 1, stdin)) {
+        if (!strncmp (inbuf, "BITMAP", 6)) break;
+      }
+
       fprintf (stdout, "%04X:", thispoint);
-      digitsout = 0;
+      rownum = 0;
       /* Print initial blank rows */
       startrow = descent + bbxyoff + bbxy;
 
       /* Force everything to 16 pixels wide */
       for (i = 16; i > startrow; i--) {
-         fprintf (stdout,"0000");
-         digitsout += 4;
+         fprintf (stdout, "%0*d", rowlen, 0);
+         rownum++;
       }
-      while (fgets (inbuf, MAXBUF - 1, stdin) != NULL &&
-             strncmp (inbuf, "END", 3) != 0) { /* copy bitmap until END */
+      /* Copy bitmap until END */
+      while (fgets (inbuf, MAXBUF - 1, stdin)) {
+         if (!strncmp (inbuf, "END", 3)) break;
          sscanf (inbuf, "%X", &rowout);
          /* Now force glyph to a 16x16 grid even if they'd fit in 8x16 */
          if (bbxx <= 8) rowout <<= 8;  /* shift left for 16x16 glyph */
          rowout >>= bbxxoff;
-         fprintf (stdout, "%04X", rowout);
-         digitsout += 4;
+         fprintf (stdout, "%0*X", rowlen, rowout);
+         rownum++;
       }
 
-      /* Pad for 16x16 glyph */
-      while (digitsout < 64) {
-         fprintf (stdout,"0000");
-         digitsout += 4;
+      /* Pad empty lines until glyph has sufficient height */
+      while (rownum < 16) {
+         fprintf (stdout, "%0*d", rowlen, 0);
+         rownum++;
       }
       fprintf (stdout,"\n");
    }