Debian Codegraph @

texk/dvipsk/tfmload.c

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