Debian Codegraph @

texk/xdvipsk/tfmload.c

raw
1 /*
2 * Loads a tfm file. It marks the characters as undefined.
3 */
4 #ifndef XDVIPSK
5 #include "dvips.h" /* The copyright notice in that file is included too! */
6 #else
7 #include "xdvips.h" /* The copyright notice in that file is included too! */
8 #endif /* XDVIPSK */
9 #ifdef KPATHSEA
10 #include <kpathsea/c-pathmx.h>
11 #include <kpathsea/concatn.h>
12 #endif
13 /*
14 * The external declarations:
15 */
16 #include "protos.h"
17
18 FILE *tfmfile;
19
20 /*
21 * Our static variables:
22 */
23 static char name[500];
24
25 void
26 badtfm(const char *s)
27 {
28 char *msg = concatn ("! Bad TFM file ", name, ": ", s, NULL);
29 error(msg);
30 }
31
32 /*
33 * Tries to open a tfm file in FD. Uses cmr10.tfm if unsuccessful,
34 * and complains loudly about it. No return, sets tfmfile global.
35 */
36 void
37 tfmopen(register fontdesctype *fd)
38 {
39 char *full_name;
40 kpse_file_format_type d;
41 char *stem_name = concat(fd->area, fd->name);
42
43 if (!noomega) { /* search for .ofm first */
44 full_name = concat(stem_name, ".ofm");
45 d = ofmpath;
46 if ((tfmfile = search(d, full_name, READBIN)) != NULL) {
47 free(stem_name);
48 free(full_name);
49 return;
50 }
51 free(full_name);
52 }
53
54 /* try tfm */
55 d = tfmpath;
56 full_name = concat(stem_name, ".tfm");
57 if ((tfmfile = search(d, full_name, READBIN)) != NULL) {
58 free(stem_name);
59 free(full_name);
60 return;
61 }
62 sprintf(errbuf, "Can't open font metric file %.999s", full_name);
63 error(errbuf);
64
65 error("I will use cmr10.tfm instead, so expect bad output.");
66 if ((tfmfile=search(d, "cmr10.tfm", READBIN)) != NULL) {
67 free(stem_name);
68 free(full_name);
69 return;
70 }
71
72 error("! I can't find cmr10.tfm; please reinstall me with proper paths");
73 free(stem_name);
74 }
75
76 static shalfword
77 tfmbyte(void)
78 {
79 return(getc(tfmfile));
80 }
81
82 halfword
83 tfm16(void)
84 {
85 register halfword a;
86 a = tfmbyte ();
87 return ( a * 256 + tfmbyte () );
88 }
89
90 uinteger
91 tfm24_kanji(void)
92 {
93 register uinteger a;
94 a = tfmbyte ();
95 a = a * 256 + tfmbyte ();
96 return ( a + tfmbyte () * 65536 );
97 }
98
99 integer
100 tfm32(void)
101 {
102 register integer a;
103 a = tfm16 ();
104 if (a > 32767) a -= 65536;
105 return ( a * 65536 + tfm16 () );
106 }
107
108 int
109 tfmload(register fontdesctype *curfnt)
110 {
111 register integer i, j;
112 register integer li, cd=0;
113 integer scaledsize;
114 integer id, nt = 0;
115 integer nw, hd;
116 integer bc, ec, direc = -1;
117 integer nco=0, ncw=0, npc=0, no_repeats = 0;
118 uinteger *index = NULL;
119 halfword *chartype = NULL;
120 integer *scaled;
121 integer *chardat;
122 int font_level;
123 integer pretend_no_chars;
124 int charcount = 0;
125 #ifdef XDVIPSK
126 chardesctype *chr;
127 #endif /* XDVIPSK */
128
129 tfmopen(curfnt);
130 /*
131 * Next, we read the font data from the tfm file, and store it in
132 * our own arrays.
133 */
134 curfnt->dir = 0;
135 curfnt->iswide = 0;
136 curfnt->codewidth = 1;
137 curfnt->kind = TFM_TEX;
138 id = tfm16();
139 if (id!=0) {
140 font_level = -1;
141 if (id == 9 || id == 11) {
142 curfnt->kind = JFM_PTEX;
143 if (noptex) badtfm("length");
144 if (id == 9) curfnt->dir = id;
145 nt = tfm16(); li = tfm16();
146 curfnt->iswide = 1;
147 curfnt->maxchars = MAX_CODE;
148 #ifndef XDVIPSK
149 curfnt->chardesc = (chardesctype *)realloc(curfnt->chardesc,
150 sizeof(chardesctype)*(MAX_CODE));
151 for (i=0; i<MAX_CODE; i++) {
152 curfnt->chardesc[i].TFMwidth = 0;
153 curfnt->chardesc[i].packptr = NULL;
154 curfnt->chardesc[i].pixelwidth = 0;
155 curfnt->chardesc[i].flags = 0;
156 }
157 #else
158 delete_all_chardesc(curfnt);
159 curfnt->chardesc_hh = NULL;
160 for (i = 0; i<MAX_CODE; i++) {
161 add_chardesc(curfnt, i);
162 }
163 #endif /* XDVIPSK */
164 }
165 hd = tfm16();
166 bc = tfm16(); ec = tfm16();
167 nw = tfm16();
168 li = tfm32(); li = tfm32(); li = tfm32(); li = tfm16();
169 if ((hd<2 || bc>ec+1 || ec>255 || nw>256) && id != 9 && id != 11)
170 badtfm("header");
171 } else { /* In an .ofm file */
172 if (noomega) badtfm("length");
173 curfnt->kind = OFM_OMEGA;
174 font_level = tfm16();
175 li = tfm32(); hd = tfm32();
176 bc = tfm32(); ec = tfm32();
177 nw = tfm32();
178 for (i=0; i<8; i++) li=tfm32();
179 direc = li;
180 if ((font_level>1 || hd<2 || bc<0 || ec<0 || nw<0
181 || bc>ec+1 || ec>0x10FFFF || nw>0x110000) ||
182 (font_level==0 && (ec>65535 || nw>65536)))
183 badtfm("header");
184 if (font_level==1) {
185 nco = tfm32();
186 ncw = tfm32();
187 npc = tfm32();
188 for (i=0; i<12; i++) li=tfm32();
189 }
190 }
191 li = tfm32();
192 check_checksum (li, curfnt->checksum, curfnt->name);
193 li = (integer)(alpha * (real)tfm32());
194 if (li > curfnt->designsize + fsizetol ||
195 li < curfnt->designsize - fsizetol) {
196 char *msg = concat ("Design size mismatch in font ", curfnt->name);
197 error(msg);
198 }
199 pretend_no_chars=ec+1;
200 if (id == 9 || id == 11) {
201 chardat = (integer *)xmalloc(256*sizeof(integer));
202 for (i=2; i<hd; i++)
203 li = tfm32();
204 index = (uinteger *)malloc(nt * sizeof(uinteger));
205 chartype = (halfword *)malloc(nt * sizeof(halfword));
206 li = tfm16();
207 li = tfm16();
208 for (i=1; i<nt; i++) {
209 /* support new JFM spec by texjporg */
210 index[i] = tfm24_kanji();
211 chartype[i] = tfmbyte();
212 }
213 for (i=0; i<256; i++)
214 chardat[i] = 256;
215
216 for (i=bc; i<=ec; i++) {
217 chardat[i] = tfmbyte();
218 li = tfm16();
219 li |= tfmbyte();
220 if (li || chardat[i])
221 charcount++;
222 }
223 } else {
224 if (pretend_no_chars<=256) pretend_no_chars=256;
225 else {
226 #ifndef XDVIPSK
227 curfnt->chardesc = (chardesctype *)
228 xrealloc(curfnt->chardesc, sizeof(chardesctype)*pretend_no_chars);
229 #else
230 for (i = 256; i<pretend_no_chars; i++) {
231 add_chardesc(curfnt, i);
232 }
233 #endif /* XDVIPSK */
234 curfnt->maxchars = pretend_no_chars;
235 }
236 for (i=2; i<((font_level==1)?nco-29:hd); i++)
237 li = tfm32();
238 chardat = (integer *) xmalloc(pretend_no_chars*sizeof(integer));
239 for (i=0; i<pretend_no_chars; i++)
240 chardat[i] = -1;
241 for (i=bc; i<=ec; i++) {
242 if (no_repeats>0) {
243 no_repeats--;
244 } else if (font_level>=0) {
245 cd = tfm16();
246 li = tfm32();
247 li = tfm16();
248 if (font_level==1) {
249 no_repeats = tfm16();
250 for (j=0; j<(npc|1); j++) tfm16();
251 ncw -= 3 + npc/2;
252 }
253 } else {
254 cd = tfmbyte();
255 li = tfm16();
256 li = tfmbyte();
257 }
258 if (cd>=nw) badtfm("char info");
259 if (cd) {
260 chardat[i] = cd;
261 charcount++;
262 }
263 }
264 if (font_level==1&&ncw!=0) {
265 char *msg = concat ("Table size mismatch in ", curfnt->name);
266 error(msg);
267 }
268 }
269 scaledsize = curfnt->scaledsize;
270 scaled = (integer *) xmalloc(nw*sizeof(integer));
271 for (i=0; i<nw; i++)
272 scaled[i] = scalewidth(tfm32(), scaledsize);
273 fclose(tfmfile);
274
275 if (id == 9 || id == 11) {
276 for (i=0; i<MAX_CODE; i++) {
277 li = scaled[chardat[0]];
278 #ifndef XDVIPSK
279 curfnt->chardesc[i].TFMwidth = li;
280 if (li >= 0)
281 curfnt->chardesc[i].pixelwidth = ((integer)(conv*li+0.5));
282 else
283 curfnt->chardesc[i].pixelwidth = -((integer)(conv*-li+0.5));
284 curfnt->chardesc[i].flags = (curfnt->resfont ? EXISTS : 0);
285 curfnt->chardesc[i].flags2 = EXISTS;
286 #else
287 chr = find_chardesc(curfnt, i);
288 if (!chr)
289 {
290 PRINTF_PR("Error: character %d not found in the font %s\n", i, curfnt->name);
291 }
292 else
293 {
294 chr->TFMwidth = li;
295 if (li >= 0)
296 chr->pixelwidth = ((integer)(conv * li + 0.5));
297 else
298 chr->pixelwidth = -((integer)(conv * (-li) + 0.5));
299 chr->flags = (curfnt->resfont ? EXISTS : 0);
300 chr->flags2 = EXISTS;
301 }
302 #endif /* XDVIPSK */
303 }
304 for (i=1; i<nt; i++) {
305 li = scaled[chardat[chartype[i]]];
306 #ifndef XDVIPSK
307 curfnt->chardesc[index[i]].TFMwidth = li;
308 if (li >= 0)
309 curfnt->chardesc[index[i]].pixelwidth = ((integer)(conv*li+0.5));
310 else
311 curfnt->chardesc[index[i]].pixelwidth = -((integer)(conv*-li+0.5));
312 #else
313 chr = find_chardesc(curfnt, index[i]);
314 if (!chr)
315 {
316 PRINTF_PR("Error: character %d not found in the font %s\n", index[i], curfnt->name);
317 }
318 else
319 {
320 chr->TFMwidth = li;
321 if (li >= 0)
322 chr->pixelwidth = ((integer)(conv * li + 0.5));
323 else
324 chr->pixelwidth = -((integer)(conv * (-li) + 0.5));
325 }
326 #endif /* XDVIPSK */
327 }
328 free(index);
329 free(chartype);
330 } else {
331 for (i=0; i<pretend_no_chars; i++)
332 if (chardat[i]!= -1) {
333 li = scaled[chardat[i]];
334 #ifndef XDVIPSK
335 curfnt->chardesc[i].TFMwidth = li;
336 if (li >= 0)
337 curfnt->chardesc[i].pixelwidth = ((integer)(conv*li+0.5));
338 else
339 curfnt->chardesc[i].pixelwidth = -((integer)(conv*-li+0.5));
340 curfnt->chardesc[i].flags = (curfnt->resfont ? EXISTS : 0);
341 curfnt->chardesc[i].flags2 = EXISTS;
342 if (!noptex && font_level==1 && (i>=0x2E80 && !(0xFB00<=i && i<=0xFB06))) {
343 curfnt->iswide = 1;
344 }
345 #else
346 chr = find_chardesc(curfnt, i);
347 if (!chr)
348 {
349 PRINTF_PR("Error: character %d not found in the font %s\n", i, curfnt->name);
350 }
351 else
352 {
353 chr->TFMwidth = li;
354 if (li >= 0)
355 chr->pixelwidth = ((integer)(conv * li + 0.5));
356 else
357 chr->pixelwidth = -((integer)(conv * (-li) + 0.5));
358 chr->flags = (curfnt->resfont ? EXISTS : 0);
359 chr->flags2 = EXISTS;
360 }
361 #endif /* XDVIPSK */
362 }
363 if (ec>=256) curfnt->codewidth = 2; /* XXX: 2byte-code can have ec<256 */
364 if (!noptex && font_level==1 && curfnt->iswide == 1) {
365 if (direc==5) { /* interpret FONTDIR RT as pTeX vertical writing */
366 curfnt->dir = 9;
367 #ifdef DEBUG
368 if (dd(D_FONTS))
369 fprintf_str(stderr,
370 "We will interpret font (%s.ofm) direction as pTeX vertical writing.\n",
371 curfnt->name);
372 #endif /* DEBUG */
373 }
374 }
375 }
376 free(chardat);
377 free(scaled);
378 curfnt->loaded = 1;
379 return charcount;
380 }