home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 3.3J / os33j.iso / NextLibrary / TeX / tex / src / dvips / loadfont.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-19  |  13.0 KB  |  445 lines

  1. /*
  2.  *   Here's the code to load a PK file into memory.
  3.  *   Individual bitmaps won't be unpacked until they prove to be needed.
  4.  */
  5. #include "dvips.h" /* The copyright notice in that file is included too! */
  6. /*
  7.  *   These are the external routines we use.
  8.  */
  9. extern void makefont() ;
  10. extern void error() ;
  11. extern integer scalewidth() ;
  12. extern int tfmload() ;
  13. extern FILE *pksearch() ;
  14. /*
  15.  *   These are the external variables we use.
  16.  */
  17. #ifdef DEBUG
  18. extern integer debug_flag;
  19. #endif  /* DEBUG */
  20. extern long bytesleft ;
  21. extern quarterword *raster ;
  22. extern real conv ;
  23. extern int actualdpi, vactualdpi ;
  24. extern real alpha ;
  25. extern char *pkpath ;
  26. char errbuf[200] ;
  27. int lastresortsizes[40] ;
  28. extern integer fsizetol ;
  29. extern Boolean nosmallchars ;
  30. extern Boolean compressed ;
  31. extern Boolean dopprescan ;
  32. #ifdef FONTLIB
  33. extern Boolean flib ;
  34. extern FILE *flisearch() ;
  35. #endif
  36. /*
  37.  *   Now we have some routines to get stuff from the PK file.
  38.  *   Subroutine pkbyte returns the next byte.
  39.  */
  40. FILE *pkfile ;
  41. char name[50] ;
  42. void
  43. badpk(s)
  44.    char *s ;
  45. {
  46.    (void)sprintf(errbuf,"! Bad PK file %s: %s",name,s) ;
  47.    error(errbuf);
  48. }
  49.  
  50. shalfword
  51. pkbyte()
  52. {
  53.    register shalfword i ;
  54.  
  55.    if ((i=getc(pkfile))==EOF)
  56.       badpk("unexpected eof") ;
  57.    return(i) ;
  58. }
  59.  
  60. integer
  61. pkquad()
  62. {
  63.    register integer i ;
  64.  
  65.    i = pkbyte() ;
  66.    if (i > 127)
  67.       i -= 256 ;
  68.    i = i * 256 + pkbyte() ;
  69.    i = i * 256 + pkbyte() ;
  70.    i = i * 256 + pkbyte() ;
  71.    return(i) ;
  72. }
  73.  
  74. integer
  75. pktrio()
  76. {
  77.    register integer i ;
  78.  
  79.    i = pkbyte() ;
  80.    i = i * 256 + pkbyte() ;
  81.    i = i * 256 + pkbyte() ;
  82.    return(i) ;
  83. }
  84.  
  85.  
  86. /*
  87.  *   pkopen opens the pk file.  This is system dependent.  We work really
  88.  *   hard to open some sort of PK file.
  89.  */
  90. #ifdef VMCMS /* IBM: VM/CMS - we let DVIPS EXEC handle this after
  91.                              the DVIPS MODULE has finished to avoid
  92.                              complications with system calls. */
  93. int dontmakefont = 0 ;
  94. #else
  95. #ifdef MVSXA /* IBM: MVS/XA - we let system administrator handle this on
  96.                             MVS/XA since some printers can't get to user
  97.                              fonts anyway */
  98. int dontmakefont = 1 ;
  99. #else
  100. int dontmakefont = 0 ; /* if makefont fails once we won't try again */
  101. #endif  /* IBM: VM/CMS */
  102. #endif
  103.  
  104. void
  105. lectureuser() {
  106.    static int userwarned = 0 ;
  107.  
  108.    if (! userwarned) {
  109.       error("Such scaling will generate extremely poor output.") ;
  110.       userwarned = 1 ;
  111.    }
  112. }
  113. Boolean
  114. pkopen(fd)
  115.         register fontdesctype *fd ;
  116. {
  117.    register char *d, *n ;
  118.    int vdpi ;
  119.  
  120.    d = fd->area ;
  121.    n = fd->name ;
  122.    if (*d==0)
  123.       d = pkpath ;
  124. #ifdef FONTLIB
  125.    if (*(fd->area) == 0) {
  126.       int del ;
  127.       for (del=0; del<=RES_TOLERANCE(fd->dpi); del=del>0?-del:-del+1) {
  128.         if ((pkfile=flisearch(n, fd->dpi + del)) != (FILE *)NULL )
  129.           return(1);
  130.       }
  131.    }
  132. #endif
  133.    {
  134.       int del ;
  135.       for (del=0; del<=RES_TOLERANCE(fd->dpi); del=del>0?-del:-del+1) {
  136.          if (actualdpi == vactualdpi) {
  137.             vdpi = 0 ;
  138.          } else {
  139.             vdpi = (2 * ((long)vactualdpi) * (fd->dpi + del) + actualdpi)
  140.                                                        / (2 * actualdpi) ;
  141.          }
  142.          (void)sprintf(name, "%s.%dpk", n, fd->dpi + del) ;
  143.          if (pkfile=pksearch(d, name, READBIN, n, fd->dpi + del, vdpi))
  144.             return(1) ;
  145.       }
  146.    }
  147.    if (d == pkpath) {
  148.       if (actualdpi == vactualdpi) {
  149.          vdpi = 0 ;
  150.       } else {
  151.          vdpi = (2 * ((long)vactualdpi) * fd->dpi + actualdpi)
  152.                                                     / (2 * actualdpi) ;
  153.       }
  154.       (void)sprintf(name, "%s.%dpk", n, fd->dpi) ;
  155.       makefont(n, (int)fd->dpi, DPI) ;
  156.       if (dontmakefont == 0 &&
  157.           (pkfile = pksearch(d, name, READBIN, n, fd->dpi, vdpi)))
  158.                return(1) ;
  159. #ifndef MSDOS
  160.       dontmakefont = 1 ;
  161. #endif
  162.    }
  163. /*
  164.  *   If nothing above worked, then we get desparate.  We attempt to
  165.  *   open the stupid font at one of a small set of predefined sizes,
  166.  *   and then use PostScript scaling to generate the correct size.
  167.  *
  168.  *   We much prefer scaling up to scaling down, since scaling down
  169.  *   can omit character features, so we try the larger sizes first,
  170.  *   and then work down.
  171.  */
  172.    {
  173.       int i, j ;
  174.  
  175.       if (lastresortsizes[0] && fd->dpi < 30000) {
  176.          for (i=0; lastresortsizes[i] < fd->dpi; i++) ;
  177.          for (j = i-1; j >= 0; j--) {
  178.             if (actualdpi == vactualdpi) {
  179.                vdpi = 0 ;
  180.             } else {
  181.                vdpi = (2 * ((long)vactualdpi) * lastresortsizes[j] + actualdpi)
  182.                                                        / (2 * actualdpi) ;
  183.             }
  184.             (void)sprintf(name, "%s.%dpk", n, lastresortsizes[j]) ;
  185. #ifdef FONTLIB
  186.             if ((pkfile=flisearch(n,(halfword)lastresortsizes[j]))
  187.              || (pkfile=pksearch(d, name, READBIN, n,
  188.                          (halfword)lastresortsizes[j], vdpi))) {
  189. #else
  190.             if (pkfile=pksearch(d, name, READBIN, n,
  191.                          (halfword)lastresortsizes[j], vdpi)) {
  192. #endif
  193.                fd->loadeddpi = lastresortsizes[j] ;
  194.                fd->alreadyscaled = 0 ;
  195.                (void)sprintf(errbuf,
  196.                        "Font %s at %d not found; scaling %d instead.",
  197.                                          name, fd->dpi, lastresortsizes[j]) ;
  198.                error(errbuf) ;
  199.                lectureuser() ;
  200.                return 1 ;
  201.             }
  202.          }
  203.          for (j = i; lastresortsizes[j] < 30000; j++) {
  204.             if (actualdpi == vactualdpi) {
  205.                vdpi = 0 ;
  206.             } else {
  207.                vdpi = (2 * ((long)vactualdpi) * lastresortsizes[j] + actualdpi)
  208.                                                        / (2 * actualdpi) ;
  209.             }
  210.             (void)sprintf(name, "%s.%dpk", n, lastresortsizes[j]) ;
  211. #ifdef FONTLIB
  212.             if ((pkfile=flisearch(n, (halfword)lastresortsizes[j]))
  213.                 || (pkfile=pksearch(d, name, READBIN, n,
  214.                       (halfword)lastresortsizes[j], vdpi))) {
  215. #else
  216.             if (pkfile=pksearch(d, name, READBIN, n,
  217.                             (halfword)lastresortsizes[j], vdpi)) {
  218. #endif
  219.                fd->loadeddpi = lastresortsizes[j] ;
  220.                fd->alreadyscaled = 0 ;
  221.                (void)sprintf(errbuf,
  222.                        "Font %s at %d not found; scaling %d instead.",
  223.                                          name, fd->dpi, lastresortsizes[j]) ;
  224.                error(errbuf) ;
  225.                lectureuser() ;
  226.                return 1 ;
  227.             }
  228.          }
  229.       }
  230.    }
  231.    (void)sprintf(name, "%s.%dpk", n, fd->dpi) ;
  232.    (void)sprintf(errbuf,
  233.       "Font %s%s not found, characters will be left blank.",
  234.       fd->area, name) ;
  235.    error(errbuf) ;
  236.    return(0) ;
  237. }
  238.  
  239. /*
  240.  *   Now our loadfont routine.  We return an integer indicating the
  241.  *   highest character code in the font, so we know how much space
  242.  *   to reserve for the character.  (It's returned in the font
  243.  *   structure, along with everything else.)
  244.  */
  245. void
  246. loadfont(curfnt)
  247.         register fontdesctype *curfnt ;
  248. {
  249.    register shalfword i ;
  250.    register shalfword cmd ;
  251.    register integer k ;
  252.    register integer length = 0 ;
  253.    register shalfword cc = 0 ;
  254.    register integer scaledsize = curfnt->scaledsize ;
  255.    register quarterword *tempr ;
  256.    register chardesctype *cd = 0 ;
  257.    int maxcc = 0 ;
  258.    int munged = 0 ;
  259. /*
  260.  *   We clear out some pointers:
  261.  */
  262.    if (curfnt->loaded == 3) {
  263.       for (i=0; i<256; i++) {
  264.          curfnt->chardesc[i].TFMwidth = 0 ;
  265.          curfnt->chardesc[i].packptr = NULL ;
  266.          curfnt->chardesc[i].pixelwidth = 0 ;
  267.          curfnt->chardesc[i].flags &= EXISTS ;
  268.       }
  269.    } else {
  270.       for (i=0; i<256; i++) {
  271.          curfnt->chardesc[i].TFMwidth = 0 ;
  272.          curfnt->chardesc[i].packptr = NULL ;
  273.          curfnt->chardesc[i].pixelwidth = 0 ;
  274.          curfnt->chardesc[i].flags = 0 ;
  275.       }
  276.    }
  277.    curfnt->maxchars = 256 ; /* just in case we return before the end */
  278.    if (!pkopen(curfnt)) {
  279.       tfmload(curfnt) ;
  280.       return ;
  281.    }
  282. #ifdef DEBUG
  283.    if (dd(D_FONTS))
  284.       (void)fprintf(stderr,"Loading pk font %s at %.1fpt\n",
  285.          name, (real)scaledsize/(alpha*0x100000)) ;
  286. #endif /* DEBUG */
  287.    if (pkbyte()!=247)
  288.       badpk("expected pre") ;
  289.    if (pkbyte()!=89)
  290.       badpk("wrong id byte") ;
  291.    for(i=pkbyte(); i>0; i--)
  292.       (void)pkbyte() ;
  293.    k = (integer)(alpha * (real)pkquad()) ;
  294.    if (k > curfnt->designsize + fsizetol ||
  295.        k < curfnt->designsize - fsizetol) {
  296.       (void)sprintf(errbuf,"Design size mismatch in font %s", name) ;
  297.       error(errbuf) ;
  298.    }
  299.    k = pkquad() ;
  300.    if (k && curfnt->checksum)
  301.       if (k!=curfnt->checksum) {
  302.          (void)sprintf(errbuf,"Checksum mismatch in font %s", name) ;
  303.          error(errbuf) ;
  304.        }
  305.    k = pkquad() ; /* assume that hppp is correct in the PK file */
  306.    k = pkquad() ; /* assume that vppp is correct in the PK file */
  307. /*
  308.  *   Now we get down to the serious business of reading character definitions.
  309.  */
  310.    while ((cmd=pkbyte())!=245) {
  311.       if (cmd < 240) {
  312.          switch (cmd & 7) {
  313. case 0: case 1: case 2: case 3:
  314.             length = (cmd & 7) * 256 + pkbyte() - 3 ;
  315.             cc = pkbyte() ;
  316.             cd = curfnt->chardesc+cc ;
  317.             if (nosmallchars || curfnt->dpi != curfnt->loadeddpi)
  318.                cd->flags |= BIGCHAR ;
  319.             cd->TFMwidth = scalewidth(pktrio(), scaledsize) ;
  320.             cd->pixelwidth = pkbyte() ;
  321.             break ;
  322. case 4: case 5: case 6:
  323.             length = (cmd & 3) * 65536L + pkbyte() * 256L ;
  324.             length = length + pkbyte() - 4L ;
  325.             cc = pkbyte() ;
  326.             cd = curfnt->chardesc+cc ;
  327.             cd->TFMwidth = scalewidth(pktrio(), scaledsize) ;
  328.             cd->flags |= BIGCHAR ;
  329.             i = pkbyte() ;
  330.             cd->pixelwidth = i * 256 + pkbyte() ;
  331.             break ;
  332. case 7:
  333.             length = pkquad() - 11 ;
  334.             cc = pkquad() ;
  335.             if (cc<0 || cc>255) badpk("character code out of range") ;
  336.             cd = curfnt->chardesc + cc ;
  337.             cd->flags |= BIGCHAR ;
  338.             cd->TFMwidth = scalewidth(pkquad(), scaledsize) ;
  339.             cd->pixelwidth = (pkquad() + 32768) >> 16 ;
  340.             k = pkquad() ;
  341.          }
  342.          if (cd->pixelwidth == 0 && cd->TFMwidth != 0) {
  343.             if (cd->TFMwidth > 0)
  344.                k = (integer)(cd->TFMwidth * conv + 0.5) ;
  345.             else
  346.                k = -(integer)(-cd->TFMwidth * conv + 0.5) ;
  347.             if (k != 0) {
  348.                cd->pixelwidth = k ;
  349.                munged++ ;
  350.             }
  351.          }
  352.          if (length <= 0)
  353.             badpk("packet length too small") ;
  354.          if (dopprescan && ((cd->flags & EXISTS) == 0)) {
  355.             for (length--; length>0; length--)
  356.                (void)pkbyte() ;
  357.          } else {
  358.             if (cc > maxcc)
  359.                maxcc = cc ;
  360.             if (bytesleft < length || (length > MINCHUNK && compressed)) {
  361. #ifdef DEBUG
  362.                 if (dd(D_FONTS))
  363.                    (void)fprintf(stderr,
  364. #ifdef SHORTINT
  365.                       "Allocating new raster memory (%ld req, %ld left)\n",
  366.                                    length, bytesleft) ;
  367. #else
  368.                       "Allocating new raster memory (%d req, %d left)\n",
  369.                                    (int)length, (int)bytesleft) ;
  370. #endif
  371. #endif /* DEBUG */
  372.                 if (length > MINCHUNK) {
  373.                    tempr = (quarterword *)mymalloc((integer)length) ;
  374.                 } else {
  375.                    raster = (quarterword *)mymalloc((integer)RASTERCHUNK) ;
  376.                    tempr = raster ;
  377.                    bytesleft = RASTERCHUNK - length ;
  378.                    raster += length ;
  379.                }
  380.             } else {
  381.                tempr = raster ;
  382.                bytesleft -= length ;
  383.                raster += length ;
  384.             }
  385.             cd->packptr = tempr ;
  386.             *tempr++ = cmd ;
  387.             for (length--; length>0; length--)
  388.                *tempr++ = pkbyte() ;
  389.          }
  390.       } else {
  391.          k = 0 ;
  392.          switch (cmd) {
  393. case 243:
  394.             k = pkbyte() ;
  395.             if (k > 127)
  396.                k -= 256 ;
  397. case 242:
  398.             k = k * 256 + pkbyte() ;
  399. case 241:
  400.             k = k * 256 + pkbyte() ;
  401. case 240:
  402.             k = k * 256 + pkbyte() ;
  403.             while (k-- > 0)
  404.                i = pkbyte() ;
  405.             break ;
  406. case 244:
  407.             k = pkquad() ;
  408.             break ;
  409. case 246:
  410.             break ;
  411. default:
  412.             badpk("! unexpected command") ;
  413.          }
  414.       }
  415.    }
  416. #ifdef FONTLIB
  417.    if (flib)
  418.       flib = 0 ;
  419.    else
  420. #endif
  421.    (void)fclose(pkfile) ;
  422.    curfnt->loaded = 1 ;
  423.    curfnt->maxchars = maxcc + 1 ;
  424.    if (munged > 0) {
  425.       static int seen = 0 ;
  426.       sprintf(errbuf,
  427.           "Font %s at %d dpi has most likely been made improperly;",
  428.            curfnt->name, curfnt->dpi) ;
  429.       error(errbuf) ;
  430.       if (seen)
  431.          return ;
  432.       seen = 1 ;
  433.       sprintf(errbuf,
  434.      "%d characters have 0 escapements but non-trivial TFM widths.", munged) ;
  435.       error(errbuf) ;
  436.       error(
  437.           "I'm stumbling along as best I can, but I recommend regenerating") ;
  438.       error(
  439.           "these fonts; the problem is probably that they are non-CM fonts") ;
  440.       error(
  441.           "(such as circle10 or line10) created with a MF with the CM base") ;
  442.       error("preloaded .") ;
  443.    }
  444. }
  445.