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

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