Debian Codegraph @

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