Debian Codegraph @

texk/dvipsk/makefont.c

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