home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / d / dvips549.zip / DVIPS / MAKEFONT.C < prev    next >
C/C++ Source or Header  |  1992-10-14  |  6KB  |  254 lines

  1. /*
  2.  *   This software is Copyright 1988 by Radical Eye Software.
  3.  */
  4. #include "structures.h"
  5. extern int quiet ;
  6. extern int filter ;
  7. extern int dontmakefont ;
  8. extern int system() ;
  9. extern Boolean secure ;
  10. extern char *getenv(), *newstring() ;
  11. extern char *mfmode ;
  12. #ifdef MSDOS
  13. extern char *mfjobname ;
  14. extern FILE *mfjobfile ;
  15. extern char *pkpath ;
  16. extern int actualdpi ;
  17. extern int vactualdpi ;
  18. /*
  19.  *  Write mfjob file
  20.  */
  21. void
  22. mfjobout(font,mag)
  23. char *font;
  24. double mag;
  25. {
  26.    if (mfjobfile == (FILE *)NULL) {
  27.       char pkout[128];
  28.       char *p;
  29.       int i;
  30.       for (p=pkpath, i=0; *p && *p!=PATHSEP && i<127; p++) {
  31.          if (*p=='%') {
  32.             p++;
  33.             switch(*p) { /* convert %x codes to mfjob @y codes */
  34.                case 'b':
  35.                   sprintf(pkout+i,"%d",actualdpi);
  36.                   break;
  37.                case 'd':
  38.                   strcpy(pkout+i,"@Rr");
  39.                   break;
  40.                case 'f':
  41.                   strcpy(pkout+i,"@f");
  42.                   break;
  43.                case 'p':
  44.                   strcpy(pkout+i,"pk");
  45.                   break;
  46.                case 'm':
  47.                   strcpy(pkout+i,mfmode);
  48.                   break;
  49.                case '%':
  50.                   strcpy(pkout+i,"%");   
  51.                   break;
  52.                default:
  53.                   sprintf(pkout+i, "%%%c", *p) ;
  54.                   fprintf(stderr,"Unknown option %%%c in pk path\n",*p);
  55.             }
  56.             i += strlen(pkout+i);
  57.          }
  58.          else
  59.            pkout[i++] = *p;   
  60.       }
  61.       *p='\0';
  62.       mfjobfile =  fopen(mfjobname,"w"); 
  63.       if (mfjobfile == (FILE *)NULL)
  64.          return;
  65.       fprintf(mfjobfile,"input[dvidrv];\n{\ndriver=dvips;\n");
  66.       if (actualdpi == vactualdpi)
  67.          fprintf(mfjobfile,"mode=%s[%d];\n",mfmode,actualdpi);
  68.       else
  69.          fprintf(mfjobfile,"mode=%s[%d %d];\n",mfmode,actualdpi,vactualdpi);
  70.       fprintf(mfjobfile,"output=pk[%s];\n",pkout);
  71.    }      
  72.    fprintf(mfjobfile,"{font=%s; mag=%f;}\n",font,mag);
  73. }
  74. #endif
  75. /*
  76.  *   Calculate magstep values.
  77.  */
  78. static int
  79. magstep(n, bdpi)
  80. register int n, bdpi ;
  81. {
  82.    register float t ;
  83.    int neg = 0 ;
  84.  
  85.    if (n < 0) {
  86.       neg = 1 ;
  87.       n = -n ;
  88.    }
  89.    if (n & 1) {
  90.       n &= ~1 ;
  91.       t = 1.095445115 ;
  92.    } else
  93.       t = 1.0 ;
  94.    while (n > 8) {
  95.       n -= 8 ;
  96.       t = t * 2.0736 ;
  97.    }
  98.    while (n > 0) {
  99.       n -= 2 ;
  100.       t = t * 1.2 ;
  101.    }
  102.    if (neg)
  103.       return((int)(0.5 + bdpi / t)) ;
  104.    else
  105.       return((int)(0.5 + bdpi * t)) ;
  106. }
  107. #ifdef MSDOS
  108. static char *defcommand = "command /c MakeTeXP %n %d %b %m" ;
  109. #else
  110. static char *defcommand = "MakeTeXPK %n %d %b %m" ;
  111. #endif
  112. char *command = 0 ;
  113. /*
  114.  *   This routine tries to create a font by executing a command, and
  115.  *   then opening the font again if possible.
  116.  */
  117. static char buf[125] ;
  118. void
  119. makefont(name, dpi, bdpi)
  120.    char *name ;
  121.    int dpi, bdpi ;
  122. {
  123.    register char *p, *q ;
  124.    register int m, n ;
  125. #ifdef MSDOS
  126.    double t;
  127. #endif
  128.  
  129.    if (command == 0)
  130.       if (secure == 0 && (command=getenv("MAKETEXPK")))
  131.          command = newstring(command) ;
  132.       else
  133.          command = defcommand ;
  134.    for (p=command, q=buf; *p; p++)
  135.       if (*p != '%')
  136.          *q++ = *p ;
  137.       else {
  138.          switch (*++p) {
  139. case 'n' : case 'N' :
  140.             (void)strcpy(q, name) ;
  141.             break ;
  142. case 'd' : case 'D' :
  143.             (void)sprintf(q, "%d", dpi) ;
  144.             break ;
  145. case 'b' : case 'B' :
  146.             (void)sprintf(q, "%d", bdpi) ;
  147.             break ;
  148. case 'm' : case 'M' :
  149. /*
  150.  *   Here we want to return a string.  If we can find some integer
  151.  *   m such that floor(0.5 + bdpi * 1.2 ^ (m/2)) = dpi, we write out
  152.  *      magstep(m/2)
  153.  *   where m/2 is a decimal number; else we write out
  154.  *      dpi/bdpi
  155.  *   We do this for the very slight improvement in accuracy that
  156.  *   magstep() gives us over the rounded dpi/bdpi.
  157.  */
  158.             m = 0 ;
  159.             if (dpi < bdpi) {
  160.                while (1) {
  161.                   m-- ;
  162.                   n = magstep(m, bdpi) ;
  163.                   if (n == dpi)
  164.                      break ;
  165.                   if (n < dpi || m < -40) {
  166.                      m = 9999 ;
  167.                      break ;
  168.                   }
  169.                }
  170.             } else if (dpi > bdpi) {
  171.                while (1) {
  172.                   m++ ;
  173.                   n = magstep(m, bdpi) ;
  174.                   if (n == dpi)
  175.                      break ;
  176.                   if (n > dpi || m > 40) {
  177.                      m = 9999 ;
  178.                      break ;
  179.                   }
  180.                }
  181.             }
  182. #ifdef MSDOS
  183. /* write out magnification as decimal number */
  184.             if (m == 9999) {
  185.                (void)sprintf(q, "%f", (double)dpi/bdpi) ;
  186.             } else {
  187.         if (m < 0) 
  188.             n = -m;
  189.         else
  190.             n = m;
  191.         if (n & 1) {
  192.             n &= ~1 ;
  193.             t = 1.095445115 ;
  194.            } else
  195.             t = 1.0 ;
  196.         while (n > 0) {
  197.             n -= 2 ;
  198.             t = t * 1.2 ;
  199.         }
  200.         if (m < 0)
  201.             t = 1 / t ;
  202.         (void)sprintf(q, "%12.9f", t) ;
  203.             }
  204. #else
  205.             if (m == 9999) {
  206.                (void)sprintf(q, "%d+%d/%d", dpi/bdpi, dpi%bdpi, bdpi) ;
  207.             } else if (m >= 0) {
  208.                (void)sprintf(q, "magstep\\(%d.%d\\)", m/2, (m&1)*5) ;
  209.             } else {
  210.                (void)sprintf(q, "magstep\\(-%d.%d\\)", (-m)/2, (m&1)*5) ;
  211.             }
  212. #endif
  213.             break ;
  214. case 0 :    *q = 0 ;
  215.             break ;
  216. default:    *q++ = *p ;
  217.             *q = 0 ;
  218.             break ;
  219.          }
  220.          q += strlen(q) ;
  221.       }
  222.    *q = 0 ;
  223.    if (mfmode) {
  224.       strcpy(q, " ") ;
  225.       strcat(q, mfmode) ;
  226.    }
  227. #ifndef MSDOS
  228.    if (filter)
  229.       (void)strcat(buf, quiet ? " >/dev/null" : " 1>&2") ;
  230. #endif
  231.    if (! quiet)
  232.       (void)fprintf(stderr, "- %s\n", buf) ;
  233.    if (dontmakefont == 0)
  234. #ifdef MSDOS
  235.       if (mfjobname != (char *)NULL)
  236.          mfjobout(name,t);
  237.       else
  238. #endif
  239.       (void)system(buf) ;
  240.    else {
  241.       static FILE *fontlog = 0 ;
  242.  
  243.       if (fontlog == 0) {
  244.          fontlog = fopen("missfont.log", "a") ;
  245.          if (fontlog != 0) {
  246.             (void)fprintf(stderr,
  247.                      "Appending font creation commands to missfont.log\n") ;
  248.          }
  249.       }
  250.       if (fontlog != 0)
  251.          (void)fprintf(fontlog, "%s\n", buf) ;
  252.    }
  253. }
  254.