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