home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume3 / bdftools / part01 / dumpgf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-07  |  8.8 KB  |  496 lines

  1. #include    <stdio.h>
  2. #include    <sys/types.h>
  3. #include    "bdf.h"
  4. #include    "gf.h"
  5.  
  6. #define        NCHARS        256
  7.  
  8. static charinfo    ci[NCHARS];
  9.  
  10. static int    filepos = 0;        /* position in gf output */
  11.  
  12. static FILE    *plfile    = NULL;        /* property list output */
  13.  
  14. /*
  15. **    The parameters to these routines follow the order
  16. **    they are described in gftopk.web.
  17. */
  18.  
  19. static out(c)
  20.     int        c;
  21. {
  22.     putchar(c);
  23.     filepos++;
  24. }
  25.  
  26. static out2(c)
  27.     int        c;
  28. {
  29.     out((c >> 8) & 0xff);
  30.     out(c & 0xff);
  31. }
  32.  
  33. static out3(c)
  34.     int        c;
  35. {
  36.     out((c >> 16) & 0xff);
  37.     out((c >> 8) & 0xff);
  38.     out(c & 0xff);
  39. }
  40.  
  41. static out4(c)
  42.     int        c;
  43. {
  44.     out((c >> 24) & 0xff);
  45.     out((c >> 16) & 0xff);
  46.     out((c >> 8) & 0xff);
  47.     out(c & 0xff);
  48. }
  49.  
  50. static paint(n)
  51.     register int    n;
  52. {
  53.     if (0 <= n && n < 64)
  54.         out(PAINT0 + n);
  55.     else if (64 <= n && n < 256)
  56.     {
  57.         out(PAINT1);
  58.         out(n);
  59.     }
  60.     else if (256 <= n && n < 65536)
  61.     {
  62.         out(PAINT2);
  63.         out2(n);
  64.     }
  65.     else if (65536 <= n && n < (1 << 24))
  66.     {
  67.         out(PAINT3);
  68.         out3(n);
  69.     }
  70.     else
  71.         fatal("Impossibly large value for paint(n)");
  72. }
  73.  
  74. static boc(c, p, minm, maxm, minn, maxn)
  75.     int        c, p, minm, maxm, minn, maxn;
  76. {
  77.     if (p == -1 &&
  78.         0 <= maxm && maxm < 256 &&
  79.         (maxm - minm) < 256 &&
  80.         0 <= maxn && maxn < 256 &&
  81.         (maxn - minn) < 256)
  82.     {
  83.         out(BOC1);
  84.         out(c);
  85.         out(maxm-minm);
  86.         out(maxm);
  87.         out(maxn-minn);
  88.         out(maxn);
  89.     }
  90.     else
  91.     {
  92.         out(BOC);
  93.         out4(c);
  94.         out4(p);
  95.         out4(minm);
  96.         out4(maxm);
  97.         out4(minn);
  98.         out4(maxn);
  99.     }
  100. }
  101.  
  102. static eoc()
  103. {
  104.     out(EOC);
  105. }
  106.  
  107. static skip(n)
  108.     int        n;
  109. {
  110.     if (n <= 0)
  111.         out(SKIP0);
  112.     else if (1 <= n && n <= 256)
  113.     {
  114.         out(SKIP1);
  115.         out(n-1);
  116.     }
  117.     else if (256 < n && n < 65536)
  118.     {
  119.         out(SKIP2);
  120.         out2(n);
  121.     }
  122.     else if (65536 <= n && n < (1 << 24))
  123.     {
  124.         out(SKIP3);
  125.         out3(n);
  126.     }
  127.     else
  128.         fatal("Impossibly large value for skip(n)");
  129. }
  130.  
  131. static newrow(m)
  132.     int        m;
  133. {
  134.     if (m <= 0)
  135.         out(NEWROW0);
  136.     else if (1 <= m && m <= 164)
  137.         out(NEWROW1+m-1);
  138.     else
  139.         fatal("Too large a value for newrow(m)");
  140. }
  141.  
  142. static xxx(k, x)
  143.     int        k;
  144.     char        *x;
  145. {
  146.     if (0 <= k && k < 256)
  147.     {
  148.         out(XXX1);
  149.         out(k);
  150.     }
  151.     else if (256 <= k && k < 65536)
  152.     {
  153.         out(XXX2);
  154.         out2(k);
  155.     }
  156.     else if (65536 <= k && k < (1 << 24))
  157.     {
  158.         out(XXX3);
  159.         out3(k);
  160.     }
  161.     else
  162.     {
  163.         out(XXX4);
  164.         out4(k);
  165.     }
  166.     while (k-- > 0)
  167.         out(*x++);
  168. }
  169.  
  170. static yyy(n)
  171.     int        n;
  172. {
  173.     out(YYY);
  174.     out4(n);
  175. }
  176.  
  177. static noop()
  178. {
  179.     out(NOOP);
  180. }
  181.  
  182. static charloc(c, dx, dy, w, p)
  183.     int        c, dx, dy, w, p;
  184. {
  185.     if (dy == 0 && (dx & 0xffff) == 0 && (dx >> 16) < 256)
  186.     {
  187.         out(CHARLOC0);
  188.         out(c);
  189.         out(dx >> 16);
  190.         out4(w);
  191.         out4(p);
  192.     }
  193.     else
  194.     {
  195.         out(CHARLOC);
  196.         out(c);
  197.         out4(dx);
  198.         out4(dy);
  199.         out4(w);
  200.         out4(p);
  201.     }
  202. }
  203.  
  204. static pre(i, k, x)
  205.     int        i, k;
  206.     char        *x;
  207. {
  208.     out(PRE);
  209.     out(i);
  210.     out(k);
  211.     while (k-- > 0)
  212.         out(*x++);
  213. }
  214.  
  215. static post(p)
  216.     int        p;
  217. {
  218.     out(POST);
  219.     out4(p);
  220.     out4(pointsize * FIX);
  221.     out4(0);            /* checksum */
  222.     out4((xres * (1 << 16))/72);    /* hppp */
  223.     out4((yres * (1 << 16))/72);    /* vppp */
  224.     out4(offx);            /* global */
  225.     out4(bbx + offx);
  226.     out4(offy);
  227.     out4(bby + offy - 1);
  228. }
  229.  
  230. static postpost(q, i)
  231.     int        q, i;
  232. {
  233.     register int    n;
  234.  
  235.     out(POSTPOST);
  236.     out4(q);
  237.     out(i);
  238.     for (n = 0; n < 4; ++n)
  239.         out(223);
  240.     while (filepos & 0x3)        /* round off to 4 bytes */
  241.         out(223);
  242. }
  243.  
  244. initialize(argc, argv)
  245.     int        argc;
  246.     char        *argv[];
  247. {
  248.     register int    i;
  249.  
  250.     if (argc < 2)
  251.         fatal("Usage: bdf2gf gf [pl]");
  252.  
  253.     if (freopen(argv[1], "w", stdout) == NULL)
  254.         fatal("Cannot open gf file");
  255.  
  256.     if (argc >= 3)
  257.     {
  258.         if ((plfile = fopen(argv[2], "w")) == NULL)
  259.             fatal("Cannot open pl file");
  260.     }
  261.  
  262.     for (i = 0; i < NCHARS; ++i)
  263.         ci[i].p = -1;
  264. }
  265.  
  266. storeglyph(n, bbx, bby, offx, offy, dx, dy)
  267.     int        n, bbx, bby, offx, offy;
  268.     float        dx, dy;
  269. {
  270.     register charinfo    *cp;
  271.  
  272.     n %= NCHARS;
  273.     cp = &ci[n];
  274.     if (cp->p != -1)
  275.         (void)fprintf(stderr, "Warning, redefinition of char %d\n", n);
  276.     cp->bbx = bbx;
  277.     cp->bby = bby;
  278.     cp->offx = offx;
  279.     cp->offy = offy;
  280.     cp->w = dx * (float)FIX / 1000.0;
  281. /*
  282. **    Conversion from BDF swidths to pixels:
  283. **    pixels = swidth * (pointsize / 72) * resolution / 1000;
  284. **    i.e. swidths are dimensionless and 1000x the true width.
  285. **    Swidths normally range in the high hundreds, as you would expect.
  286. **
  287. **    To convert to GF escapements we round and multiply by 2^16.
  288. */
  289.     dx *= (float)(pointsize * xres) / 72000.0;
  290.     cp->dx = (int)(dx + 0.5) * (1 << 16);
  291.     dy *= (float)(pointsize * yres) / 72000.0;
  292.     cp->dy = (int)(dy + 0.5) * (1 << 16);
  293. }
  294.  
  295. /*
  296. **    We can save white_pixels until a black_pixel forces us
  297. **    to output them.
  298. **    If end_of_row == 0, then white_pixels are row trailing counts,
  299. **    otherwise they are row leading counts.
  300. */
  301. static int    white_pixels    = 0;
  302. static int    end_of_row     = 0;
  303.  
  304. static run(color, length)
  305.     int        color, length;
  306. {
  307.     register int    leading_white;
  308.  
  309.     if (length <= 0)
  310.         return;
  311.     if (color == WHITE_PIXEL)
  312.         white_pixels += length;
  313.     else            /* BLACK_PIXEL */
  314.     {
  315.         if (end_of_row <= 0)
  316.             paint(white_pixels);
  317.         else
  318.         {
  319. /*
  320. **    This is how we dispose of most of the blank rows.
  321. */
  322.             if (end_of_row > 1)
  323.                 skip(end_of_row - 1);
  324.             if ((leading_white = white_pixels) > 164)
  325.                 leading_white = 164;
  326.             newrow(leading_white);
  327. /*
  328. **    It is very unlikely that we will have more than 164
  329. **    leading columns of white but in case, we output a paint(0)
  330. **    followed by as many more white pixels as we need.
  331. */
  332.             if ((white_pixels - leading_white) > 0)
  333.             {
  334.                 paint(0);
  335.                 paint(white_pixels);
  336.             }
  337.         }
  338.         white_pixels = end_of_row = 0;    /* reset counts */
  339.         paint(length);        /* and automatically reverse color */
  340.     }
  341. }
  342.  
  343. static endrow()
  344. {
  345. /*
  346. **    If we have white pixels here they are trailing pixels and
  347. **    we can just throw them away because we will output
  348. **    a newrow command next.
  349. */
  350.     white_pixels = 0;
  351.     ++end_of_row;
  352. }
  353.  
  354. static dump1map(n, bitmap)
  355.     int        n;
  356.     u_char        *bitmap;
  357. {
  358.     register int    i, j, save, paintswitch, bitpos;
  359.     register u_char    *p;
  360.     register int    bytewidth;
  361.     register charinfo    *cp;
  362.  
  363.     if (bitmap == 0)    /* no bitmap? */
  364.         return;
  365.     cp = &ci[n];
  366.     i = filepos;        /* save position */
  367.     /* any xxx's come here */
  368.     boc(n, cp->p, cp->offx, cp->bbx + cp->offx,
  369.         cp->offy, cp->bby + cp->offy - 1);
  370.     cp->p = i;        /* set backpointer */
  371.     /* dump bitmap here */
  372.     bytewidth = (cp->bbx + 7) / 8;
  373.     white_pixels = end_of_row = 0;
  374.     for (i = 0; i < cp->bby; ++i)
  375.     {
  376.         p = &bitmap[i * bytewidth];
  377.         bitpos = 7;
  378.         paintswitch = WHITE_PIXEL;
  379.         for (save = j = 0; j < cp->bbx; ++j)
  380.         {
  381.             if (((*p >> bitpos) & 0x1) != paintswitch)
  382.             {
  383.                 run(paintswitch, j - save);
  384.                 save = j;
  385.                 paintswitch ^= 0x1;
  386.             }
  387.             if (--bitpos < 0)
  388.             {
  389.                 ++p;
  390.                 bitpos = 7;
  391.             }
  392.         }
  393.         run(paintswitch, j - save);
  394.         endrow();
  395.     }
  396.     eoc();
  397. }
  398.  
  399. static dump1loc(n, bitmap)
  400.     int        n;
  401.     u_char        *bitmap;
  402. {
  403.     register charinfo    *cp;
  404.  
  405.     if (bitmap == 0)
  406.         return;
  407.     cp = &ci[n];
  408.     charloc(n, cp->dx, cp->dy, cp->w, cp->p);
  409. }
  410.  
  411. static fontdimens(scale, bitmaps)
  412.     float        scale;
  413.     u_char        *bitmaps[NCHARS];
  414. {
  415.     register int    ch;
  416.     register charinfo    *cp;
  417.  
  418.     (void)fprintf(plfile, "(FONTDIMEN\n");
  419.     ch = bitmaps[' '] != 0 ? ' ' : 'x';
  420.     if (bitmaps[ch] != 0)
  421.     {
  422.         cp = &ci[ch];
  423.         (void)fprintf(plfile, " (SPACE D %d)\n", cp->bbx);
  424.         (void)fprintf(plfile, " (STRETCH R %.9f)\n", (float)cp->bbx * 0.5);
  425.         (void)fprintf(plfile, " (SHRINK R %.9f)\n", (float)cp->bbx * 0.3);
  426.         (void)fprintf(plfile, " (EXTRASPACE R %.9f)\n", (float)cp->bbx * 0.3);
  427.         (void)fprintf(plfile, " (QUAD D %d)\n", cp->bbx * 2);
  428.         (void)fprintf(plfile, " (XHEIGHT D %d)\n", cp->bby);
  429.     }
  430.     else
  431.     {
  432.         (void)fprintf(plfile, " (SPACE R %.9f)\n", scale * 0.75);
  433.         (void)fprintf(plfile, " (STRETCH R %.9f)\n", scale * 0.375);
  434.         (void)fprintf(plfile, " (SHRINK R %.9f)\n", scale * 0.225);
  435.         (void)fprintf(plfile, " (EXTRASPACE R %.9f)\n", scale * 0.225);
  436.         (void)fprintf(plfile, " (QUAD R %.9f)\n", scale * 1.5);
  437.         (void)fprintf(plfile, " (XHEIGHT R %.9f)\n", scale * 0.75);
  438.     }
  439.     (void)fprintf(plfile, " )\n");
  440. }
  441.  
  442. static dump1pl(n, bitmap)
  443.     int        n;
  444.     u_char        *bitmap;
  445. {
  446.     register charinfo    *cp;
  447.  
  448.     if (bitmap == 0)
  449.         return;
  450.     cp = &ci[n];
  451.     if (' ' < n && n <= '~' && n != '(' && n != ')')
  452.         (void)fprintf(plfile, "(CHARACTER C %c\n", n);
  453.     else
  454.         (void)fprintf(plfile, "(CHARACTER D %d\n", n);
  455.     (void)fprintf(plfile, " (CHARWD D %d)\n", cp->bbx);
  456.     (void)fprintf(plfile, " (CHARHT D %d)\n", cp->bby + cp->offy);
  457.     (void)fprintf(plfile, " (CHARDP D %d)\n", -cp->offy);
  458.     (void)fprintf(plfile, " )\n");
  459. }
  460.  
  461. static dumppl(bitmaps)
  462.     u_char    *bitmaps[NCHARS];
  463. {
  464.     register int    i;
  465.     register float    du;
  466.  
  467.     (void)fprintf(plfile, "(DESIGNSIZE D %d)\n", pointsize);
  468.     du = (float)xres * (float)pointsize / 72.0;
  469.     (void)fprintf(plfile, "(DESIGNUNITS R %.9f)\n", du);
  470.     (void)fprintf(plfile, "(COMMENT Units are in pixels)\n");
  471.     fontdimens(du, bitmaps);
  472.     for (i = 0; i < NCHARS; ++i)
  473.         dump1pl(i, bitmaps[i]);
  474.     (void)fclose(plfile);
  475. }
  476.  
  477. dumpfont(bitmaps)
  478.     u_char    *bitmaps[NCHARS];
  479. {
  480.     register int    i, lastboc, pos;
  481.  
  482.     pre(131, 0, "");
  483.     for (i = 0; i < NCHARS; ++i)
  484.         dump1map(i, bitmaps[i]);
  485.     lastboc = filepos;    /* save position of lastboc */
  486.     /* xxx's for the postamble go here */
  487.     pos = filepos;        /* save position of post */
  488.     /* beginning of postamble */
  489.     post(lastboc);
  490.     for (i = 0; i < NCHARS; ++i)
  491.         dump1loc(i, bitmaps[i]);
  492.     postpost(pos, 131);
  493.     if (plfile != NULL)
  494.         dumppl(bitmaps);
  495. }
  496.