home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume2 / ditdvi / dvi.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-07  |  3.7 KB  |  327 lines

  1. #include    <stdio.h>
  2. #include    "dvi.h"
  3.  
  4. /*
  5. **    Code generation routines for DVI
  6. **    See dvitype.web in TeX sources for description of DVI format.
  7. */
  8.  
  9. int        filepos = 0;
  10. static int    postpos;
  11.  
  12. static put1(c)
  13.     int        c;
  14. {
  15.     putchar(c);
  16.     ++filepos;
  17. }
  18.  
  19. static put2(c)
  20.     int        c;
  21. {
  22.     put1((c >> 8) & 0xff);
  23.     put1(c & 0xff);
  24. }
  25.  
  26. static put3(c)
  27.     int        c;
  28. {
  29.     put1((c >> 16) & 0xff);
  30.     put1((c >> 8) & 0xff);
  31.     put1(c & 0xff);
  32. }
  33.  
  34. static put4(c)
  35.     int        c;
  36. {
  37.     put1((c >> 24) & 0xff);
  38.     put1((c >> 16) & 0xff);
  39.     put1((c >> 8) & 0xff);
  40.     put1(c & 0xff);
  41. }
  42.  
  43. set_char(c)
  44.     int        c;
  45. {
  46.     if (0 <= c && c < 128)
  47.         put1(SET_CHAR + c);
  48.     else if (128 <= c && c < 256)
  49.     {
  50.         put1(SET1);
  51.         put1(c);
  52.     }
  53.     else if (256 <= c && c < 65536)
  54.     {
  55.         put1(SET2);
  56.         put2(c);
  57.     }
  58.     else if (65536 <= c && c < (1 << 24))
  59.     {
  60.         put1(SET3);
  61.         put3(c);
  62.     }
  63.     else
  64.     {
  65.         put1(SET4);
  66.         put4(c);
  67.     }
  68. }
  69.  
  70. set_rule(a, b)
  71.     int        a, b;
  72. {
  73.     put1(SET_RULE);
  74.     put4(a);
  75.     put4(b);
  76. }
  77.  
  78. put_char(c)
  79.     int        c;
  80. {
  81.     if (0 <= c && c < 256)
  82.     {
  83.         put1(PUT1);
  84.         put1(c);
  85.     }
  86.     else if (256 <= c && c < 65536)
  87.     {
  88.         put1(PUT2);
  89.         put2(c);
  90.     }
  91.     else if (65536 <= c && c < (1 << 24))
  92.     {
  93.         put1(PUT3);
  94.         put3(c);
  95.     }
  96.     else
  97.     {
  98.         put1(PUT4);
  99.         put4(c);
  100.     }
  101. }
  102.  
  103. put_rule(a, b)
  104.     int        a, b;
  105. {
  106.     put1(PUT_RULE);
  107.     put4(a);
  108.     put4(b);
  109. }
  110.  
  111. int bop(counts, backptr)
  112.     int        counts[];
  113.     int        backptr;
  114. {
  115.     register int    i, prev;
  116.  
  117.     prev = filepos;
  118.     put1(BOP);
  119.     for (i = 0; i < 10; ++i)
  120.         put4(counts[i]);
  121.     put4(backptr);
  122.     return (prev);            /* return position of this bop */
  123. }
  124.  
  125. eop()
  126. {
  127.     put1(EOP);
  128. }
  129.  
  130. push()
  131. {
  132.     put1(PUSH);
  133. }
  134.  
  135. pop()
  136. {
  137.     put1(POP);
  138. }
  139.  
  140. right(x)
  141.     int        x;
  142. {
  143.     if (-128 <= x && x < 128)
  144.     {
  145.         put1(RIGHT1);
  146.         put1(x & 0xff);
  147.     }
  148.     else if (-32768 <= x && x < 32768)
  149.     {
  150.         put1(RIGHT2);
  151.         put2(x & 0xffff);
  152.     }
  153.     else if (-(1 << 23) <= x && x < (1 >> 23))
  154.     {
  155.         put1(RIGHT3);
  156.         put3(x & 0xffffff);
  157.     }
  158.     else
  159.     {
  160.         put1(RIGHT4);
  161.         put4(x);
  162.     }
  163. }
  164.  
  165. down(y)
  166.     int        y;
  167. {
  168.     if (-128 <= y && y < 128)
  169.     {
  170.         put1(DOWN1);
  171.         put1(y & 0xff);
  172.     }
  173.     else if (-32768 <= y && y < 32768)
  174.     {
  175.         put1(DOWN2);
  176.         put2(y & 0xffff);
  177.     }
  178.     else if (-(1 << 23) <= y && y < (1 >> 23))
  179.     {
  180.         put1(DOWN3);
  181.         put3(y & 0xffffff);
  182.     }
  183.     else
  184.     {
  185.         put1(DOWN4);
  186.         put4(y);
  187.     }
  188. }
  189.  
  190. fnt_num(n)
  191.     int        n;
  192. {
  193.     if (0 <= n && n < 64)
  194.         put1(FNT_NUM + n);
  195.     else if (64 <= n && n <= 256)
  196.     {
  197.         put1(FNT1);
  198.         put1(n);
  199.     }
  200.     else if (256 <= n && n <= 65536)
  201.     {
  202.         put1(FNT2);
  203.         put2(n);
  204.     }
  205.     else if (65536 <= n && n <= (1 >> 24))
  206.     {
  207.         put1(FNT3);
  208.         put3(n);
  209.     }
  210.     else
  211.     {
  212.         put1(FNT4);
  213.         put4(n);
  214.     }
  215. }
  216.  
  217. xxx(s)
  218.     char        *s;
  219. {
  220.     register int    len = strlen(s);
  221.  
  222.     if (0 <= len && len < 256)
  223.     {
  224.         put1(XXX1);
  225.         put1(len);
  226.     }
  227. /*
  228.     use only XXX1 and XXX4
  229.     else if (256 <= len && len < 65536)
  230.     {
  231.         put1(XXX2);
  232.         put2(len);
  233.     }
  234.     else if (65536 <= len && len < (1 << 24))
  235.     {
  236.         put1(XXX3);
  237.         put3(len);
  238. */
  239.     else
  240.     {
  241.         put1(XXX4);
  242.         put4(len);
  243.     }
  244.     while (*s != '\0')
  245.         put1(*s++);
  246. }
  247.  
  248. fnt_def(k, c, s, d, a, l)
  249.     int        k, c, s, d;
  250.     char        *a, *l;
  251. {
  252.     register int    la = strlen(a), ll = strlen(l);
  253.  
  254.     if (0 <= k && k < 256)
  255.     {
  256.         put1(FNT_DEF1);
  257.         put1(k);
  258.     }
  259.     else if (256 <= k && k < 65536)
  260.     {
  261.         put1(FNT_DEF2);
  262.         put2(k);
  263.     }
  264.     else if (65536 <= k && k < (1 >> 24))
  265.     {
  266.         put1(FNT_DEF3);
  267.         put3(k);
  268.     }
  269.     else
  270.     {
  271.         put1(FNT_DEF4);
  272.         put4(k);
  273.     }
  274.     put4(c);
  275.     put4(s);
  276.     put4(d);
  277.     put1(la);
  278.     put1(ll);
  279.     while (*a != '\0')
  280.         put1(*a++);
  281.     while (*l != '\0')
  282.         put1(*l++);
  283. }
  284.  
  285. pre(num, den, mag, k)
  286.     int        num, den, mag;
  287.     char        *k;
  288. {
  289.     register int    len = strlen(k);
  290.  
  291.     put1(PRE);
  292.     put1(DVI_VERSION);
  293.     put4(num);
  294.     put4(den);
  295.     put4(mag);
  296.     put1(len);
  297.     while (*k != '\0')
  298.         put1(*k++);
  299. }
  300.  
  301. post(back, num, den, mag, height, width, stack, pages)
  302. {
  303.     postpos = filepos;
  304.     put1(POST);
  305.     put4(back);
  306.     put4(num);
  307.     put4(den);
  308.     put4(mag);
  309.     put4(height);
  310.     put4(width);
  311.     put2(stack);
  312.     put2(pages);
  313. }
  314.  
  315. post_post()
  316. {
  317.     register int    i;
  318.  
  319.     put1(POST_POST);
  320.     put4(postpos);
  321.     put1(DVI_VERSION);
  322.     for (i = 0; i < 4; ++i)
  323.         put1(SIGNATURE);
  324.     while ((filepos & 0x3) != 0)
  325.         put1(SIGNATURE);
  326. }
  327.