home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / PRINTING / DVIPS386.ZIP / DOWNLOAD.C < prev    next >
C/C++ Source or Header  |  1991-11-03  |  8KB  |  305 lines

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