Debian Codegraph @

texk/xdvipsk/makefont.c

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