home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 3 / goldfish_volume_3.bin / files / text / tex / pastex / source / dvips / download.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-08  |  8.3 KB  |  338 lines

  1. /*
  2.  *   Code to download a font definition at the beginning of a section.
  3.  */
  4. #include "dvips.h" /* The copyright notice in that file is included too! */
  5. /*
  6.  *   These are the external routines we call.
  7.  */
  8. #ifdef AMIGA
  9. #include "download_protos.h"
  10. #include "output_protos.h"
  11. #include "unpack_protos.h"
  12. #include "repack_protos.h"
  13. #include "dvips_protos.h"
  14. #else
  15. extern void numout() ;
  16. extern void mhexout() ;
  17. extern void cmdout() ;
  18. extern long unpack() ;
  19. extern void flip() ;
  20. extern void specialout() ;
  21. extern long getlong() ;
  22. extern void error() ;
  23. #endif
  24. extern char *nextstring ;
  25. /*
  26.  *   These are the external variables we access.
  27.  */
  28. extern FILE *bitfile ;
  29. extern fontdesctype *curfnt ;
  30. extern long bytesleft ;
  31. extern long mbytesleft ;
  32. extern quarterword *raster ;
  33. extern quarterword *mraster ;
  34. extern Boolean compressed ;
  35. extern integer mag ;
  36. extern int actualdpi ;
  37. static unsigned char dummyend[8] = { 252 } ;
  38. /*
  39.  *   We have a routine that downloads an individual character.
  40.  */
  41. static int lastccout ;
  42. void downchar(c, cc)
  43. chardesctype *c ;
  44. shalfword cc ;
  45. {
  46.    register long i, j ;
  47.    register halfword cheight, cwidth ;
  48.    register long k ;
  49.    register quarterword *p ;
  50.    register halfword cmd ;
  51.    register shalfword xoff, yoff ;
  52.    halfword wwidth = 0 ;
  53.    register long len ;
  54.    int smallchar ;
  55.  
  56.    p = c->packptr ;
  57.    cmd = *p++ ;
  58.    if (cmd & 4) {
  59.       if ((cmd & 7) == 7) {
  60.          cwidth = getlong(p) ;
  61.          cheight = getlong(p + 4) ;
  62.          xoff = getlong(p + 8) ;
  63.          yoff = getlong(p + 12) ;
  64.          p += 16 ;
  65.       } else {
  66.          cwidth = p[0] * 256 + p[1] ;
  67.          cheight = p[2] * 256 + p[3] ;
  68.          xoff = p[4] * 256 + p[5] ; /* N.B.: xoff, yoff are signed halfwords */
  69.          yoff = p[6] * 256 + p[7] ;
  70.          p += 8 ;
  71.       }
  72.    } else {
  73.       cwidth = *p++ ;
  74.       cheight = *p++ ;
  75.       xoff = *p++ ;
  76.       yoff = *p++ ;
  77.       if (xoff > 127)
  78.          xoff -= 256 ;
  79.       if (yoff > 127)
  80.          yoff -= 256 ;
  81.    }
  82.    if (c->flags & BIGCHAR)
  83.       smallchar = 0 ;
  84.    else
  85.       smallchar = 5 ;
  86.    if (compressed) {
  87.       len = getlong(p) ;
  88.       p += 4 ;
  89.    } else {
  90.       wwidth = (cwidth + 15) / 16 ;
  91.       i = 2 * cheight * (long)wwidth ;
  92.       if (i <= 0)
  93.          i = 2 ;
  94.       i += smallchar ;
  95.       if (mbytesleft < i) {
  96.          if (mbytesleft >= RASTERCHUNK)
  97.             (void) free((char *) mraster) ;
  98.          if (RASTERCHUNK > i) {
  99.             mraster = (quarterword *)mymalloc((integer)RASTERCHUNK) ;
  100.             mbytesleft = RASTERCHUNK ;
  101.          } else {
  102.             k = i + i / 4 ;
  103.             mraster = (quarterword *)mymalloc((integer)k) ;
  104.             mbytesleft = k ;
  105.          }
  106.       }
  107.       k = i;
  108.       while (k > 0)
  109.          mraster[--k] = 0 ;
  110.       unpack(p, (halfword *)mraster, cwidth, cheight, cmd) ;
  111.       p = mraster ;
  112.       len = i - smallchar ;
  113.    }
  114.    if (cheight == 0 || cwidth == 0 || len == 0) {
  115.       cwidth = 1 ;
  116.       cheight = 1 ;
  117.       wwidth = 1 ;
  118.       len = 2 ;
  119.       if (compressed)
  120.          p = dummyend ;  /* CMD(END); see repack.c */
  121.       else
  122.          mraster[0] = 0 ;
  123.    }
  124.    if (smallchar) {
  125.       p[len] = cwidth ;
  126.       p[len + 1] = cheight ;
  127.       p[len + 2] = xoff + 128 ;
  128.       p[len + 3] = yoff + 128 ;
  129.       p[len + 4] = c->pixelwidth ;
  130.    } else
  131. /*
  132.  *   Now we actually send out the data.
  133.  */
  134.       specialout('[') ;
  135.    if (compressed) {
  136.       specialout('<') ;
  137.       mhexout(p, len + smallchar) ;
  138.       specialout('>') ;
  139.    } else {
  140.       i = (cwidth + 7) / 8 ;
  141.       if (i * cheight > 65520) {
  142.          long bc = 0 ;
  143.          specialout('<') ;
  144.          for (j=0; j<cheight; j++) {
  145.             if (bc + i > 65520) {
  146.                specialout('>') ;
  147.                specialout('<') ;
  148.                bc = 0 ;
  149.             }
  150.             mhexout(p, i) ;
  151.             bc += i ;
  152.             p += 2*wwidth ;
  153.          }
  154.          specialout('>') ;
  155.       } else {
  156.          specialout('<') ;
  157.          if (2 * wwidth == i)
  158.             mhexout(p, ((long)cheight) * i + smallchar) ;
  159.          else {
  160.             for (j=0; j<cheight; j++) {
  161.                mhexout(p, i) ;
  162.                p += 2*wwidth ;
  163.             }
  164.             if (smallchar)
  165.                mhexout(p, (long)smallchar) ;
  166.          }
  167.          specialout('>') ;
  168.       }
  169.    }
  170.    if (smallchar == 0) {
  171.       numout((integer)cwidth) ;
  172.       numout((integer)cheight) ;
  173.       numout((integer)xoff + 128) ; /* not all these casts needed. */
  174.       numout((integer)yoff + 128) ;
  175.       numout((integer)(c->pixelwidth)) ;
  176.    }
  177.    if (lastccout + 1 == cc) {
  178.       cmdout("I") ;
  179.    } else {
  180.       numout((integer)cc) ;
  181.       cmdout("D") ;
  182.    }
  183.    lastccout = cc ;
  184. }
  185. /*
  186.  * Output the literal name of the font change command with PostScript index n
  187.  */
  188. static char goodnames[] =
  189.    "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" ;
  190. void
  191. makepsname(s, n)
  192. register char *s ;
  193. register int n ;
  194. {
  195.    n-- ;
  196.    *s++ = 'F' + n / sizeof(goodnames) ;
  197.    *s++ = goodnames[n % sizeof(goodnames)] ;
  198.    *s++ = 0 ;
  199. }
  200. void
  201. lfontout(n)
  202. int n ;
  203. {
  204.     char buf[10];
  205.         char *b = buf ;
  206.         *b++ = '/' ;
  207.         makepsname(b, n) ;
  208.     cmdout(buf);
  209. }
  210. /*
  211.  *   And the download procedure.
  212.  */
  213. void download(p, psfont)
  214. charusetype *p ;
  215. int psfont ;
  216. {
  217.    register int b, i ;
  218.    register halfword bit ;
  219.    register chardesctype *c ;
  220.    int cc, maxcc = -1, numcc ;
  221.    float fontscale ;
  222.    char name[10] ;
  223.  
  224.    lastccout = -5 ;
  225.    name[0] = '/' ;
  226.    makepsname(name + 1, psfont) ;
  227.    curfnt = p->fd ;
  228.    curfnt->psname = psfont ;
  229.    if (curfnt->resfont) {
  230.       struct resfont *rf = curfnt->resfont ;
  231.  
  232.       cmdout(name) ;
  233. /* following code re-arranged - Rob Hutchings 1992Apr02 */
  234.       c = curfnt->chardesc ;
  235.  
  236.       c = curfnt->chardesc + 255 ;
  237.       cc = 255 ;
  238.       numcc = 0 ;
  239.       i = 0 ;
  240.       for (b=15; b>=0; b--) {
  241.          for (bit=1; bit; bit<<=1) {
  242.             if (p->bitmap[b] & bit) {
  243.                if (i > 0) {
  244.                   numout((integer)i) ;
  245.                   specialout('[') ;
  246.                   i = 0 ;
  247.                }
  248.                numout((integer)c->pixelwidth) ;
  249.                c->flags |= EXISTS ;
  250.                numcc++ ;
  251.             } else {
  252.                i++ ;
  253.                c->flags &= ~EXISTS ;
  254.             }
  255.             c-- ;
  256.             cc-- ;
  257.          }
  258.       }
  259.       if (i > 0) {
  260.          numout((integer)i) ;
  261.          specialout('[') ;
  262.       }
  263.       specialout ('{') ;
  264.       if (rf->specialinstructions)
  265.          cmdout(rf->specialinstructions) ;
  266.       specialout ('}') ;
  267. /* 
  268.  * Perhaps there is a good reason to avoid float in this code; but in general
  269.  * arithmetic should be done in the driver rather than the interpreter - Rob 
  270.  */
  271.       numout((integer)numcc) ;
  272.       fontscale = ((float)(curfnt->scaledsize)) / 655360.0 ;
  273.       fontscale *= (((float)mag)/7200.0) ;
  274.       fontscale *= actualdpi ;
  275.       (void)sprintf(nextstring, "%f", fontscale) ;
  276.       cmdout(nextstring) ;
  277.       (void)strcpy(nextstring, "/") ;
  278.       (void)strcat(nextstring, rf->PSname) ;
  279.       cmdout(nextstring) ;
  280. /* end of changed section - Rob */
  281.       cmdout("rf") ;
  282.       rf->sent = 1 ;
  283.       return ;
  284.    }
  285. /*
  286.  *   Here we calculate the largest character actually used, and
  287.  *   send it as a parameter to df.
  288.  */
  289.    cc = 0 ;
  290.    numcc = 0 ;
  291.    for (b=0; b<16; b++) {
  292.       for (bit=32768; bit!=0; bit>>=1) {
  293.          if (p->bitmap[b] & bit) {
  294.             maxcc = cc ;
  295.             numcc++ ;
  296.          }
  297.          cc++ ;
  298.       }
  299.    }
  300.    if (numcc <= 0)
  301.       return ;
  302.    cmdout(name) ;
  303.    numout((integer)numcc) ;
  304.    numout((integer)maxcc + 1) ;
  305. /*
  306.  *   If we need to scale the font, we say so by using dfs
  307.  *   instead of df, and we give it a scale factor.  We also
  308.  *   scale the character widths; this is ugly, but scaling
  309.  *   fonts is ugly, and this is the best we can probably do.
  310.  */
  311.    if (curfnt->dpi != curfnt->loadeddpi) {
  312.       numout((integer)curfnt->dpi) ;
  313.       numout((integer)curfnt->loadeddpi) ;
  314.       if (curfnt->alreadyscaled == 0) {
  315.          for (b=0, c=curfnt->chardesc; b<256; b++, c++)
  316.             c->pixelwidth = (c->pixelwidth * 
  317.       (long)curfnt->dpi * 2 + curfnt->loadeddpi) / (2 * curfnt->loadeddpi) ;
  318.          curfnt->alreadyscaled = 1 ;
  319.       }
  320.       cmdout("dfs") ;
  321.    } else
  322.       cmdout("df") ;
  323.    c = curfnt->chardesc ;
  324.    cc = 0 ;
  325.    for (b=0; b<16; b++) {
  326.       for (bit=32768; bit; bit>>=1) {
  327.          if (p->bitmap[b] & bit) {
  328.             downchar(c, cc) ;
  329.             c->flags |= EXISTS ;
  330.          } else
  331.             c->flags &= ~EXISTS ;
  332.          c++ ;
  333.          cc++ ;
  334.       }
  335.    }
  336.    cmdout("E") ;
  337. }
  338.