Debian Codegraph @

texk/xdvipsk/virtualfont.c

raw
1 /*
2 * Here's the code to load a VF file into memory.
3 * Any resemblance between this file and loadfont.c is purely uncoincidental.
4 */
5 #ifndef XDVIPSK
6 #include "dvips.h" /* The copyright notice in that file is included too! */
7 #else
8 #include "xdvips.h" /* The copyright notice in that file is included too! */
9 #endif /* XDVIPSK */
10 #ifdef KPATHSEA
11 #include <kpathsea/c-pathmx.h>
12 #include <kpathsea/concatn.h>
13 #endif
14 /*
15 * The external declarations:
16 */
17 #include "protos.h"
18 /*
19 * Now we have some routines to get stuff from the VF file.
20 * Subroutine vfbyte returns the next byte.
21 */
22 static FILE *vffile;
23 static char name[500];
24 static void
25 badvf(const char *s)
26 {
27 char *msg = concatn("! Bad VF file ", name, ": ", s, NULL);
28 error(msg);
29 }
30
31 static shalfword
32 vfbyte(void)
33 {
34 register shalfword i;
35
36 if ((i=getc(vffile))==EOF)
37 badvf("unexpected eof");
38 return(i);
39 }
40
41 static integer
42 vfquad(void)
43 {
44 register integer i;
45
46 i = vfbyte();
47 if (i > 127)
48 i -= 256;
49 i = i * 256 + vfbyte();
50 i = i * 256 + vfbyte();
51 i = i * 256 + vfbyte();
52 return(i);
53 }
54
55 static integer
56 vftrio(void)
57 {
58 register integer i;
59
60 i = vfbyte();
61 i = i * 256 + vfbyte();
62 i = i * 256 + vfbyte();
63 return(i);
64 }
65
66 static int
67 vfopen(register fontdesctype *fd)
68 {
69 register char *n;
70 #ifndef KPATHSEA
71 register char *d;
72 #endif
73
74 n = fd->name;
75 #ifndef KPATHSEA
76 d = fd->area;
77 if (*d==0)
78 d = vfpath;
79 #endif
80 if (strlen(n) + 5 >= sizeof (name)) {
81 /* 5 for vf() + null */
82 error("! VF file name too long in vfopen");
83 }
84 #ifdef MVSXA /* IBM: MVS/XA */
85 sprintf(name, "vf(%s)", n);
86 #else
87 sprintf(name, "%s.vf", n);
88 #endif
89 #ifdef KPATHSEA
90 if (0 != (vffile=search(vfpath, name, READBIN)))
91 #else
92 if (0 != (vffile=search(d, name, READBIN)))
93 #endif
94 return(VF_TEX);
95 if (!noomega)
96 #ifdef KPATHSEA
97 if (0 != (vffile=search(ovfpath, n, READBIN)))
98 #else
99 if (0 != (vffile=search(d, n, READBIN)))
100 #endif
101 return(VF_OMEGA);
102 return(0);
103 }
104
105 /*
106 * The following routine is like fontdef, but for local fontdefs in VF files.
107 */
108 static fontmaptype *
109 vfontdef(integer s, int siz)
110 {
111 register integer i, j, fn;
112 register fontdesctype *fp;
113 register fontmaptype *cfnt;
114 char *nam, *area;
115 integer cksum, scsize, dssize;
116
117 fn = vfbyte();
118 while (siz-- > 1)
119 fn = (fn << 8) + vfbyte();
120 cfnt = (fontmaptype *)mymalloc((integer)sizeof(fontmaptype));
121 cfnt->fontnum = fn;
122 cksum = vfquad();
123 scsize = scalewidth(s, vfquad());
124 dssize = (integer)(alpha * (real)vfquad());
125 i = vfbyte(); j = vfbyte();
126 if (nextstring + i + j > maxstring)
127 error("! out of string space");
128 area = nextstring;
129 for (; i>0; i--)
130 *nextstring++ = vfbyte();
131 *nextstring++ = 0;
132 nam = nextstring;
133 for (; j>0; j--)
134 *nextstring++ = vfbyte();
135 *nextstring++ = 0;
136 fp = matchfont(nam, area, scsize, (char *)0);
137 if (fp) {
138 nextstring = nam;
139 fp->checksum = cksum;
140 } else {
141 fp = newfontdesc(cksum, scsize, dssize, nam, area);
142 fp->next = fonthead;
143 fonthead = fp;
144 }
145 cfnt->desc = fp;
146 return (cfnt);
147 }
148
149 /*
150 * Now our virtualfont routine.
151 */
152 Boolean
153 virtualfont(register fontdesctype *curfnt)
154 {
155 register integer i, j;
156 register shalfword cmd;
157 register integer k;
158 register integer length;
159 register integer cc;
160 register chardesctype *cd;
161 integer scaledsize = curfnt->scaledsize;
162 integer no_of_chars=256;
163 integer maxcc=255;
164 register quarterword *tempr;
165 fontmaptype *fm, *newf;
166 int id, kindfont;
167 #ifdef XDVIPSK
168 static chardesctype ctmp;
169 #endif /* XDVIPSK */
170 kindfont = vfopen(curfnt); /* VF_TEX for TeX and pTeX, VF_OMEGA for Omega */
171 curfnt->kind = kindfont;
172 if (!kindfont)
173 return (0);
174 #ifdef DEBUG
175 if (dd(D_FONTS))
176 fprintf_str(stderr,"Loading virtual font %s at %.1fpt\n",
177 name, (real)scaledsize/(alpha*0x100000));
178 #endif /* DEBUG */
179
180 /*
181 * We clear out some pointers:
182 */
183 if (kindfont==VF_OMEGA) {
184 no_of_chars = VF_MEM_UNIT;
185 curfnt->maxchars=VF_MEM_UNIT;
186 #ifndef XDVIPSK
187 free(curfnt->chardesc);
188 curfnt->chardesc = (chardesctype *)
189 mymalloc(VF_MEM_UNIT * (integer)sizeof(chardesctype));
190 #else
191 delete_all_chardesc(curfnt);
192 curfnt->chardesc_hh = NULL;
193 #endif /* XDVIPSK */
194 }
195 #ifndef XDVIPSK
196 for (i=0; i<no_of_chars; i++) {
197 curfnt->chardesc[i].TFMwidth = 0;
198 curfnt->chardesc[i].packptr = NULL;
199 curfnt->chardesc[i].pixelwidth = 0;
200 curfnt->chardesc[i].flags = 0;
201 curfnt->chardesc[i].flags2 = 0;
202 }
203 #else
204 for (i = 0; i<no_of_chars; i++) {
205 add_chardesc(curfnt, i);
206 }
207 #endif /* XDVIPSK */
208 if (vfbyte()!=247)
209 badvf("expected pre");
210 if (vfbyte()!=202)
211 badvf("wrong id byte");
212 for(i=vfbyte(); i>0; i--)
213 vfbyte();
214 k = vfquad();
215 check_checksum (k, curfnt->checksum, curfnt->name);
216 k = (integer)(alpha * (real)vfquad());
217 if (k > curfnt->designsize + 2 || k < curfnt->designsize - 2) {
218 id = 0;
219 if (!noptex) {
220 tfmopen(curfnt); /* We check if parent is jfm or not. */
221 id = tfm16();
222 fclose(tfmfile);
223 }
224 if (id != 9 && id != 11) {
225 char *msg = concat("Design size mismatch in font ", name);
226 error(msg);
227 free(msg);
228 }
229 }
230 /*
231 * Now we look for font definitions.
232 */
233 fm = NULL;
234 id = 0;
235 while ((cmd=vfbyte())>=243) {
236 if (cmd==248 && (id == 9 || id == 11)) /* parent is jfm and no character definitions */
237 goto close_vf;
238 if (cmd>246)
239 badvf("unexpected command in preamble");
240 newf = vfontdef(scaledsize, cmd-242);
241 if (fm)
242 fm->next = newf;
243 else {
244 curfnt->localfonts = newf;
245 if (!noptex && id==0) {
246 tfmopen(curfnt->localfonts->desc); /* We check if parent is jfm or not. */
247 id = tfm16();
248 if (id == 9 || id == 11)
249 curfnt->kind = VF_PTEX;
250 else if (id == 0 && !noomega) {
251 int font_level;
252 integer ec;
253 font_level = tfm16();
254 tfm32(); tfm32(); tfm32(); /* li, hd, bc */
255 ec = tfm32();
256 if (font_level==1 && ec>=0x2E80) /* We interpret the ofm is for pTeX */
257 curfnt->kind = VF_PTEX;
258 }
259 fclose(tfmfile);
260 }
261 }
262 fm = newf;
263 fm->next = NULL; /* FIFO */
264 }
265 /*
266 * Now we get down to the serious business of reading character definitions.
267 */
268 do {
269 if (cmd==242) {
270 length = vfquad() + 2;
271 if (length<2) badvf("negative length packet");
272 if (length>65535) badvf("packet too long");
273 cc = vfquad();
274 if (cc>=no_of_chars && cc<MAX_VF_CODE) {
275 j = VF_MEM_UNIT * ((integer)(cc/VF_MEM_UNIT) + 1);
276 #ifndef XDVIPSK
277 curfnt->chardesc = (chardesctype *)xrealloc(curfnt->chardesc,
278 sizeof(chardesctype)*j);
279 for (i=no_of_chars; i<j; i++) {
280 curfnt->chardesc[i].TFMwidth = 0;
281 curfnt->chardesc[i].packptr = NULL;
282 curfnt->chardesc[i].pixelwidth = 0;
283 curfnt->chardesc[i].flags = 0;
284 curfnt->chardesc[i].flags2 = 0;
285 }
286 #endif
287 no_of_chars = j;
288 curfnt->maxchars=no_of_chars;
289 #ifdef XDVIPSK
290 for (i = 256; i<no_of_chars; i++) {
291 add_chardesc(curfnt, i);
292 }
293 #endif /* XDVIPSK */
294 }
295 else if (cc<0 || cc>=no_of_chars) badvf("character code out of range");
296 #ifndef XDVIPSK
297 cd = curfnt->chardesc + cc;
298 #else
299 cd = find_chardesc(curfnt, cc);
300 if (!cd)
301 {
302 PRINTF_PR("Error: character %d not found in the font %s\n", cc, curfnt->name);
303 cd = &ctmp;
304 }
305 #endif /* XDVIPSK */
306 cd->TFMwidth = scalewidth(vfquad(), scaledsize);
307 } else {
308 length = cmd + 2;
309 cc = vfbyte();
310 #ifndef XDVIPSK
311 cd = curfnt->chardesc + cc;
312 #else
313 cd = find_chardesc(curfnt, cc);
314 if (!cd)
315 {
316 PRINTF_PR("Error: character %d not found in the font %s\n", cc, curfnt->name);
317 cd = &ctmp;
318 }
319 #endif /* XDVIPSK */
320 cd->TFMwidth = scalewidth(vftrio(), scaledsize);
321 }
322 maxcc = (maxcc<cc) ? cc : maxcc;
323 if (cd->TFMwidth >= 0)
324 cd->pixelwidth = ((integer)(conv*cd->TFMwidth+0.5));
325 else
326 cd->pixelwidth = -((integer)(conv*-cd->TFMwidth+0.5));
327 cd->flags = EXISTS;
328 cd->flags2 = EXISTS;
329 if (bytesleft < length) {
330 #ifdef DEBUG
331 if (dd(D_MEM))
332 fprintf(stderr,
333 #ifdef SHORTINT
334 "Allocating new raster memory (%ld req, %ld left)\n",
335 #else
336 "Allocating new raster memory (%d req, %ld left)\n",
337 #endif
338 length, bytesleft);
339 #endif /* DEBUG */
340 if (length > MINCHUNK) {
341 tempr = (quarterword *)mymalloc((integer)length);
342 bytesleft = 0;
343 } else {
344 raster = (quarterword *)mymalloc((integer)RASTERCHUNK);
345 tempr = raster;
346 bytesleft = RASTERCHUNK - length;
347 raster += length;
348 }
349 } else {
350 tempr = raster;
351 bytesleft -= length;
352 raster += length;
353 }
354 cd->packptr = tempr;
355 length -= 2;
356 *tempr++ = length / 256;
357 *tempr++ = length % 256;
358 for (; length>0; length--)
359 *tempr++ = vfbyte();
360 cmd = vfbyte();
361 } while (cmd < 243);
362 if (cmd != 248)
363 badvf("missing postamble");
364 close_vf:
365 fclose(vffile);
366 curfnt->loaded = 2;
367 if (maxcc+1<no_of_chars) {
368 #ifndef XDVIPSK
369 curfnt->chardesc = (chardesctype *)
370 xrealloc(curfnt->chardesc,
371 (maxcc+1) * (integer)sizeof(chardesctype));
372 #else
373 for (i = maxcc + 1; i<no_of_chars; i++) {
374 delete_chardesc(curfnt, i);
375 }
376 #endif /* XDVIPSK */
377 curfnt->maxchars=maxcc+1;
378 }
379 return (1);
380 }