Debian Codegraph @

texk/dvipos/tfm.c

raw
1 /* dvipos-20070107
2
3 Copyright (C) 2003 by Jin-Hwan Cho <[email protected]>
4
5 Copyright (C) 2002 by Jin-Hwan Cho and Shunsaku Hirata,
6 the dvipdfmx project team <[email protected]>
7
8 Copyright (C) 1998, 1999 by Mark A. Wicks <[email protected]>
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
23 */
24
25 #include "utils.h"
26 #include "dvicore.h"
27 #include "tfm.h"
28
29 #include <stdio.h>
30
31 #define tfm_unsigned_byte() get_unsigned_byte(tfm_file)
32 #define tfm_signed_byte() get_signed_byte(tfm_file)
33 #define tfm_unsigned_pair() get_unsigned_pair(tfm_file)
34 #define tfm_signed_pair() get_signed_pair(tfm_file)
35 #define tfm_unsigned_triple() get_unsigned_triple(tfm_file)
36 #define tfm_unsigned_triple_kanji() get_unsigned_triple_kanji(tfm_file)
37 #define tfm_signed_triple() get_signed_triple(tfm_file)
38 #define tfm_unsigned_quad() get_unsigned_quad(tfm_file)
39 #define tfm_signed_quad() get_signed_quad(tfm_file)
40
41 static FILE *tfm_file;
42 static UNSIGNED_QUAD tfm_filesize;
43
44 #define FWBASE ((double) (1<<20))
45 #define JFM_ID 11
46 #define JFMV_ID 9 /* vertical version of JFM */
47
48 typedef signed long fixword;
49
50 /* TFM record structure */
51 struct a_tfm {
52 UNSIGNED_BYTE id; /* id = 11(9) for jfm(vertical) */
53 UNSIGNED_BYTE nt; /* number of charcter types for jfm */
54 UNSIGNED_QUAD wlenfile, wlenheader;
55 UNSIGNED_QUAD bc, ec;
56 UNSIGNED_QUAD nwidths, nheights, ndepths;
57 UNSIGNED_QUAD nitcor, nlig, nkern, nextens;
58 UNSIGNED_QUAD nfonparm;
59 UNSIGNED_QUAD font_direction; /* Used only in OFMs. TFMs don't have this field*/
60 UNSIGNED_QUAD nco, ncw, npc;
61 SIGNED_QUAD *header;
62 UNSIGNED_QUAD *char_info;
63 UNSIGNED_PAIR *width_index;
64 UNSIGNED_BYTE *height_index;
65 UNSIGNED_BYTE *depth_index;
66 SIGNED_QUAD *width;
67 SIGNED_QUAD *height;
68 SIGNED_QUAD *depth;
69 char *tex_name;
70 UNSIGNED_TRIPLE *chartypes;
71 fixword *unpacked_widths;
72 fixword *unpacked_heights;
73 fixword *unpacked_depths;
74 };
75
76 static void
77 a_tfm_init (struct a_tfm *a_tfm)
78 {
79 a_tfm->id = 0;
80 a_tfm->nt = 0;
81 a_tfm->header = NULL;
82 a_tfm->char_info = NULL;
83 a_tfm->width_index = NULL;
84 a_tfm->height_index = NULL;
85 a_tfm->depth_index = NULL;
86 a_tfm->width = NULL;
87 a_tfm->height = NULL;
88 a_tfm->depth = NULL;
89 a_tfm->chartypes = NULL;
90 a_tfm->unpacked_widths = NULL;
91 a_tfm->unpacked_heights = NULL;
92 a_tfm->unpacked_depths = NULL;
93 }
94
95 struct a_tfm *tfm = NULL;
96 static unsigned numtfms = 0, max_tfms = 0; /* numtfms should equal numfonts in dvi.c */
97
98 static UNSIGNED_PAIR sum_of_tfm_sizes (struct a_tfm *a_tfm)
99 {
100 unsigned long result = 6;
101 result += (a_tfm -> ec - a_tfm -> bc + 1);
102 result += a_tfm -> wlenheader;
103 result += a_tfm -> nwidths;
104 result += a_tfm -> nheights;
105 result += a_tfm -> ndepths;
106 result += a_tfm -> nitcor;
107 result += a_tfm -> nlig;
108 result += a_tfm -> nkern;
109 result += a_tfm -> nextens;
110 result += a_tfm -> nfonparm;
111 if (a_tfm->id == JFM_ID || a_tfm->id == JFMV_ID) {
112 result += a_tfm -> nt + 1;
113 }
114 return result;
115 }
116
117 static SIGNED_QUAD sum_of_ofm_sizes (struct a_tfm *a_tfm)
118 {
119 unsigned long result = 14;
120 result += 2*(a_tfm -> ec - a_tfm -> bc + 1);
121 result += a_tfm -> wlenheader;
122 result += a_tfm -> nwidths;
123 result += a_tfm -> nheights;
124 result += a_tfm -> ndepths;
125 result += a_tfm -> nitcor;
126 result += 2*(a_tfm -> nlig);
127 result += a_tfm -> nkern;
128 result += 2*(a_tfm -> nextens);
129 result += a_tfm -> nfonparm;
130 return result;
131 }
132
133
134 static void get_sizes (struct a_tfm *a_tfm)
135 {
136 UNSIGNED_PAIR tfm_id;
137
138 tfm_id = tfm_unsigned_pair();
139 if (tfm_id == JFM_ID || tfm_id == JFMV_ID) { /* is jfm */
140 a_tfm -> id = tfm_id;
141 a_tfm -> nt = tfm_unsigned_pair();
142 a_tfm -> wlenfile = tfm_unsigned_pair();
143 #ifdef DEBUG
144 fprintf (stderr, "size: %ld\n", a_tfm->wlenfile);
145 #endif
146 } else {
147 a_tfm->wlenfile = tfm_id;
148 }
149 a_tfm->wlenheader = tfm_unsigned_pair();
150 a_tfm->bc = tfm_unsigned_pair();
151 a_tfm->ec = tfm_unsigned_pair();
152 if (a_tfm -> ec < a_tfm -> bc) {
153 fprintf(stderr, "TFM file error (ec < bc)\n");
154 exit(1);
155 }
156 a_tfm->nwidths = tfm_unsigned_pair();
157 a_tfm->nheights = tfm_unsigned_pair();
158 a_tfm->ndepths = tfm_unsigned_pair();
159 a_tfm->nitcor = tfm_unsigned_pair();
160 a_tfm->nlig = tfm_unsigned_pair();
161 a_tfm->nkern = tfm_unsigned_pair();
162 a_tfm->nextens = tfm_unsigned_pair();
163 a_tfm->nfonparm = tfm_unsigned_pair();
164 #ifdef DEBUG
165 fprintf(stderr, "\nComputed size (words)%d\n", sum_of_tfm_sizes(a_tfm));
166 fprintf(stderr, "Stated size (words)%ld\n", a_tfm->wlenfile);
167 fprintf(stderr, "Actual size (bytes)%ld\n", tfm_filesize);
168 #endif
169 if (a_tfm->wlenfile != tfm_filesize/4 ||
170 sum_of_tfm_sizes(a_tfm) != a_tfm->wlenfile) {
171 if (tfm_filesize/4 > a_tfm->wlenfile) {
172 fprintf(stderr, "\nHmm. A TFM file is larger than it says it is!");
173 fprintf(stderr, "\nProceeding nervously...\n");
174 } else {
175 fprintf(stderr, "TFM file problem. Table sizes don't agree.\n");
176 exit(1);
177 }
178 }
179 }
180
181 static int ofm_get_sizes (struct a_tfm *a_tfm)
182 {
183 SIGNED_QUAD level;
184 level = tfm_signed_quad();
185 a_tfm -> wlenfile = tfm_signed_quad();
186 a_tfm -> wlenheader = tfm_signed_quad();
187 a_tfm -> bc = tfm_signed_quad();
188 a_tfm -> ec = tfm_signed_quad();
189 if (a_tfm -> ec < a_tfm -> bc) {
190 fprintf(stderr, "OFM file error (ec < bc)\n");
191 exit(1);
192 }
193 a_tfm -> nwidths = tfm_signed_quad();
194 a_tfm -> nheights = tfm_signed_quad();
195 a_tfm -> ndepths = tfm_signed_quad();
196 a_tfm -> nitcor = tfm_signed_quad();
197 a_tfm -> nlig = tfm_signed_quad();
198 a_tfm -> nkern = tfm_signed_quad();
199 a_tfm -> nextens = tfm_signed_quad();
200 a_tfm -> nfonparm = tfm_signed_quad();
201 a_tfm -> font_direction = tfm_signed_quad();
202 if (a_tfm->font_direction) {
203 fprintf (stderr, "Warning: I may be interpreting a font direction incorrectly.\n");
204 }
205 if (level == 0) {
206 if (a_tfm -> wlenfile != tfm_filesize/4 ||
207 sum_of_ofm_sizes (a_tfm) != a_tfm -> wlenfile) {
208 fprintf(stderr, "OFM file problem. Table sizes don't agree.\n");
209 exit(1);
210 }
211 #ifdef DEBUG
212 fprintf (stderr, "Computed size (words)%ld\n", sum_of_ofm_sizes (a_tfm));
213 fprintf (stderr, "Stated size (words)%ld\n", a_tfm -> wlenfile);
214 fprintf (stderr, "Actual size (bytes)%ld\n", tfm_filesize);
215 #endif
216 } else if (level == 1) {
217 a_tfm -> nco = tfm_signed_quad();
218 a_tfm -> ncw = tfm_signed_quad();
219 a_tfm -> npc = tfm_signed_quad();
220 fseek(tfm_file, 4*(a_tfm -> nco), SEEK_SET);
221 } else {
222 fprintf(stderr, "Can't handle OFM files with level > 1");
223 exit(1);
224 }
225 return (int) (level);
226 }
227
228 #if 0
229 /* Not used */
230 static void dump_sizes (struct a_tfm *a_tfm)
231 {
232 fprintf (stderr, "\nwlenfile: %ld, ", a_tfm -> wlenfile);
233 fprintf (stderr, "wlenheader: %ld\n", a_tfm -> wlenheader);
234 fprintf (stderr, "bc: %ld, ", a_tfm -> bc);
235 fprintf (stderr, "ec: %ld, ", a_tfm -> ec);
236 fprintf (stderr, "nwidths: %ld, ", a_tfm -> nwidths);
237 fprintf (stderr, "nheights: %ld, ", a_tfm -> nheights);
238 fprintf (stderr, "ndepths: %ld\n", a_tfm -> ndepths);
239 fprintf (stderr, "nitcor: %ld, ", a_tfm -> nitcor);
240 fprintf (stderr, "nlig: %ld, ", a_tfm -> nlig);
241 fprintf (stderr, "nkern: %ld, ", a_tfm -> nkern);
242 fprintf (stderr, "nextens: %ld, ", a_tfm -> nextens);
243 fprintf (stderr, "nfonparm: %ld\n", a_tfm -> nfonparm);
244 return;
245 }
246 #endif
247
248 static void get_fix_word_array (SIGNED_QUAD *a_word, SIGNED_QUAD length)
249 {
250 register SIGNED_QUAD i;
251 for (i = 0; i < length; i++)
252 a_word[i] = tfm_signed_quad();
253 }
254
255 static void get_unsigned_quad_array (UNSIGNED_QUAD *a_word, SIGNED_QUAD length)
256 {
257 register SIGNED_QUAD i;
258 for (i = 0; i < length; i++)
259 a_word[i] = tfm_unsigned_quad();
260 }
261
262 static void do_fix_word_array (SIGNED_QUAD **a, SIGNED_QUAD len)
263 {
264 if (len != 0) {
265 *a = (SIGNED_QUAD *)calloc(len, sizeof(SIGNED_QUAD));
266 get_fix_word_array(*a, len);
267 } else
268 *a = NULL;
269 }
270
271 static void do_unsigned_quad_array (UNSIGNED_QUAD **a, UNSIGNED_PAIR len)
272 {
273 if (len != 0) {
274 *a = (UNSIGNED_QUAD *)calloc(len, sizeof(UNSIGNED_QUAD));
275 get_unsigned_quad_array(*a, len);
276 } else
277 *a = NULL;
278 }
279
280 static void do_char_type_array(struct a_tfm *a_tfm)
281 {
282 UNSIGNED_PAIR charcode;
283 UNSIGNED_PAIR chartype;
284 register int i;
285
286 a_tfm -> chartypes = (UNSIGNED_TRIPLE *)calloc(1114112, sizeof(UNSIGNED_TRIPLE));
287 for (i = 0; i < 1114112; i++) (a_tfm->chartypes)[i] = 0;
288 for (i = 0; i < a_tfm->nt; i++) {
289 charcode = tfm_unsigned_triple_kanji();
290 chartype = tfm_unsigned_byte();
291 (a_tfm->chartypes)[charcode] = chartype;
292 }
293 }
294
295 static void unpack_widths(struct a_tfm *a_tfm)
296 {
297 UNSIGNED_QUAD charinfo;
298 UNSIGNED_PAIR width_index;
299 register int i;
300
301 a_tfm -> unpacked_widths = (fixword *)calloc(256, sizeof(fixword));
302 for (i = 0; i < 256; i++) (a_tfm ->unpacked_widths)[i] = 0;
303 for (i = (a_tfm->bc); i <= (a_tfm->ec); i++) {
304 charinfo = (a_tfm->char_info)[i-(a_tfm->bc)];
305 width_index = (charinfo / 16777216UL);
306 (a_tfm->unpacked_widths)[i] = (a_tfm->width)[width_index];
307 }
308 }
309
310 static void unpack_heights(struct a_tfm *a_tfm)
311 {
312 UNSIGNED_QUAD charinfo;
313 UNSIGNED_PAIR height_index;
314 register int i;
315
316 a_tfm -> unpacked_heights = (fixword *)calloc(256, sizeof(fixword));
317 for (i = 0; i < 256; i++) (a_tfm ->unpacked_heights)[i] = 0;
318 for (i = (a_tfm->bc); i <= (a_tfm->ec); i++) {
319 charinfo = (a_tfm->char_info)[i-(a_tfm->bc)];
320 height_index = (charinfo / 0x100000UL) & 0x0F;
321 (a_tfm->unpacked_heights)[i] = (a_tfm->height)[height_index];
322 }
323 }
324
325 static void unpack_depths(struct a_tfm *a_tfm)
326 {
327 UNSIGNED_QUAD charinfo;
328 UNSIGNED_PAIR depth_index;
329 register int i;
330
331 a_tfm -> unpacked_depths = (fixword *)calloc(256, sizeof(fixword));
332 for (i = 0; i < 256; i++) (a_tfm ->unpacked_depths)[i] = 0;
333 for (i = (a_tfm->bc); i <= (a_tfm->ec); i++) {
334 charinfo = (a_tfm->char_info)[i-(a_tfm->bc)];
335 depth_index = (charinfo / 0x10000UL) & 0x0F;
336 (a_tfm->unpacked_depths)[i] = (a_tfm->depth)[depth_index];
337 }
338 }
339
340 static void get_arrays (struct a_tfm *a_tfm)
341 {
342 #ifdef DEBUG
343 fprintf (stderr, "Reading %ld word header\n", a_tfm->wlenheader);
344 #endif
345 do_fix_word_array(&(a_tfm->header), a_tfm->wlenheader);
346 if (a_tfm->id == JFM_ID || a_tfm->id == JFMV_ID)
347 do_char_type_array(a_tfm);
348 #ifdef DEBUG
349 fprintf (stderr, "Reading %ld char_infos\n", (a_tfm->ec)-(a_tfm->bc)+1);
350 #endif
351 do_unsigned_quad_array(&(a_tfm->char_info), (a_tfm->ec)-(a_tfm->bc)+1);
352 #ifdef DEBUG
353 fprintf (stderr, "Reading %ld widths\n", a_tfm->nwidths);
354 #endif
355 do_fix_word_array(&(a_tfm->width), a_tfm->nwidths);
356 #ifdef DEBUG
357 fprintf (stderr, "Reading %ld heights\n", a_tfm->nheights);
358 #endif
359 do_fix_word_array(&(a_tfm->height), a_tfm->nheights);
360 #ifdef DEBUG
361 fprintf (stderr, "Reading %ld depths\n", a_tfm->ndepths);
362 #endif
363 do_fix_word_array(&(a_tfm->depth), a_tfm->ndepths);
364 unpack_widths(a_tfm);
365 unpack_heights(a_tfm);
366 unpack_depths(a_tfm);
367 }
368
369 static void do_ofm_zero_char_info (struct a_tfm *a_tfm)
370 {
371 UNSIGNED_QUAD num_chars;
372 register int i;
373
374 num_chars = a_tfm->ec - a_tfm->bc + 1;
375 if (num_chars != 0) {
376 a_tfm->width_index = (UNSIGNED_PAIR *)calloc(num_chars, sizeof(UNSIGNED_PAIR));
377 a_tfm->height_index = (UNSIGNED_BYTE *)calloc(num_chars, sizeof(UNSIGNED_BYTE));
378 a_tfm->depth_index = (UNSIGNED_BYTE *)calloc(num_chars, sizeof(UNSIGNED_BYTE));
379 a_tfm->unpacked_widths = (fixword *)calloc(a_tfm->bc+num_chars, sizeof(fixword));
380 a_tfm->unpacked_heights = (fixword *)calloc(a_tfm->bc+num_chars, sizeof(fixword));
381 a_tfm->unpacked_depths = (fixword *)calloc(a_tfm->bc+num_chars, sizeof(fixword));
382 }
383 for (i = 0; i < num_chars; i++) {
384 (a_tfm->width_index)[i] = tfm_unsigned_pair();
385 (a_tfm->height_index)[i] = tfm_unsigned_byte();
386 (a_tfm->depth_index)[i] = tfm_unsigned_byte();
387 tfm_signed_quad(); /* Ignore remaining quad */
388 }
389 }
390
391 static void do_ofm_one_char_info (struct a_tfm *a_tfm)
392 {
393 UNSIGNED_QUAD num_char_infos, char_infos_read;
394 UNSIGNED_QUAD num_chars;
395 UNSIGNED_QUAD char_info_size;
396 UNSIGNED_QUAD i;
397
398 char_info_size = 3 + (a_tfm->npc/2);
399 num_char_infos = (a_tfm->ncw) / char_info_size;
400 num_chars = (a_tfm->ec - a_tfm->bc) + 1;
401 #ifdef DEBUG
402 fprintf (stderr, "\nReading %ld level 1 chars\n", num_chars);
403 #endif
404 if (num_chars != 0) {
405 a_tfm->width_index = (UNSIGNED_PAIR *)calloc(num_chars, sizeof(UNSIGNED_PAIR));
406 a_tfm->height_index = (UNSIGNED_BYTE *)calloc(num_chars, sizeof(UNSIGNED_BYTE));
407 a_tfm->depth_index = (UNSIGNED_BYTE *)calloc(num_chars, sizeof(UNSIGNED_BYTE));
408 a_tfm->unpacked_widths = (fixword *)calloc(a_tfm->bc+num_chars, sizeof(fixword));
409 a_tfm->unpacked_heights = (fixword *)calloc(a_tfm->bc+num_chars, sizeof(fixword));
410 a_tfm->unpacked_depths = (fixword *)calloc(a_tfm->bc+num_chars, sizeof(fixword));
411 }
412 for (i = 0, char_infos_read = 0; i < num_chars && char_infos_read < num_char_infos; i++) {
413 register int repeats, j;
414 (a_tfm->width_index)[i] = tfm_unsigned_pair();
415 (a_tfm->height_index)[i] = tfm_unsigned_byte();
416 (a_tfm->depth_index)[i] = tfm_unsigned_byte();
417 tfm_signed_quad(); /* Ignore next quad */
418 repeats = tfm_unsigned_pair();
419 /* Skip params */
420 for (j = 0; j < a_tfm->npc; j++) tfm_unsigned_pair();
421 /* Remove word padding if necessary */
422 if ((a_tfm->npc / 2) * 2 == a_tfm->npc) tfm_unsigned_pair();
423 char_infos_read += 1;
424 if (i+repeats > num_chars) {
425 fprintf(stderr, "repeats causes number of characters to be exceeded");
426 exit(1);
427 }
428 for (j = 0; j < repeats; j++) {
429 a_tfm->width_index[i+j+1] = a_tfm->width_index[i];
430 a_tfm->height_index[i+j+1] = a_tfm->height_index[i];
431 a_tfm->depth_index[i+j+1] = a_tfm->depth_index[i];
432 }
433 /* Skip ahead because we have already handled repeats */
434 i += repeats;
435 }
436 #ifdef DEBUG
437 fprintf (stderr, "\npackets read = %ld/%ld\n", char_infos_read, num_char_infos);
438 fprintf (stderr, "\ncharacters defined = %ld/%ld\n", i, num_chars);
439 #endif
440 }
441
442 static void ofm_unpack_arrays (struct a_tfm *a_tfm, UNSIGNED_QUAD num_chars)
443 {
444 register int i;
445 for (i = 0; i < num_chars; i++) {
446 (a_tfm->unpacked_widths)[a_tfm->bc+i] = (a_tfm->width)[(a_tfm->width_index)[i]];
447 (a_tfm->unpacked_heights)[a_tfm->bc+i] = (a_tfm->height)[(a_tfm->height_index)[i]];
448 (a_tfm->unpacked_depths)[a_tfm->bc+i] = (a_tfm->depth)[(a_tfm->depth_index)[i]];
449 }
450 }
451
452 static void ofm_get_arrays (struct a_tfm *a_tfm, int level)
453 {
454 switch (level) {
455 case 0:
456 #ifdef DEBUG
457 fprintf(stderr, "Reading %ld word header\n", a_tfm->wlenheader);
458 #endif
459 do_fix_word_array(&(a_tfm -> header), a_tfm -> wlenheader);
460 #ifdef DEBUG
461 fprintf(stderr, "Reading %ld char_infos\n", (a_tfm->ec)-(a_tfm->bc)+1);
462 #endif
463 do_ofm_zero_char_info(a_tfm);
464 break;
465 case 1:
466 #ifdef DEBUG
467 fprintf (stderr, "Reading %ld char_infos words\n", a_tfm->ncw);
468 #endif
469 do_ofm_one_char_info(a_tfm);
470 break;
471 default:
472 fprintf(stderr, "level != 0 or 1 in ofm_get_arrays()");
473 exit(1);
474 }
475 #ifdef DEBUG
476 fprintf (stderr, "Reading %ld widths\n", a_tfm -> nwidths);
477 #endif
478 do_fix_word_array(&(a_tfm -> width), a_tfm -> nwidths);
479 #ifdef DEBUG
480 fprintf (stderr, "Reading %ld heights\n", a_tfm -> nheights);
481 #endif
482 do_fix_word_array(&(a_tfm -> height), a_tfm -> nheights);
483 #ifdef DEBUG
484 fprintf (stderr, "Reading %ld depths\n", a_tfm -> ndepths);
485 #endif
486 do_fix_word_array(&(a_tfm -> depth), a_tfm -> ndepths);
487 ofm_unpack_arrays(a_tfm, (a_tfm->ec)-(a_tfm->bc)+1);
488 }
489
490 static void get_ofm (struct a_tfm *a_tfm)
491 {
492 register int level;
493 level = ofm_get_sizes(a_tfm);
494 ofm_get_arrays(a_tfm, level);
495 }
496
497 static void get_tfm (struct a_tfm *a_tfm)
498 {
499 get_sizes(a_tfm);
500 get_arrays(a_tfm);
501 }
502
503 /* External Routine */
504 #define TFM_FORMAT 1
505 #define OFM_FORMAT 2
506
507 int tfm_open (const char *tfm_name, int must_exist)
508 {
509 int i, format;
510 char *file_name;
511
512 for (i = 0; i < numtfms; i++) {
513 if (!strcmp(tfm_name, tfm[i].tex_name))
514 return i;
515 }
516
517 /* The procedure to search tfm or ofm files:
518 1. Search tfm file with the given name with the must_exist flag unset.
519 2. Search ofm file with the given name with the must_exist flag unset.
520 3. If not found and must_exist flag is set, try again to search
521 tfm file with the must_exist flag set.
522 4. If not found and must_exist flag is not set, return -1. */
523
524 if ((file_name = kpse_find_file(tfm_name, kpse_tfm_format, 0)))
525 format = TFM_FORMAT;
526 else if ((file_name = kpse_find_file(tfm_name, kpse_ofm_format, 0)))
527 format = OFM_FORMAT;
528 /* In case that must_exist is set, MiKTeX returns always non-NULL value
529 even if the tfm file is not found. */
530 else if (must_exist) {
531 if ((file_name = kpse_find_file(tfm_name, kpse_tfm_format, 1)))
532 format = TFM_FORMAT;
533 else {
534 fprintf(stderr, "\n** Fatal: Unable to find TFM file '%s'.\n", tfm_name);
535 exit(1);
536 }
537 } else
538 return -1;
539
540 if (numtfms >= max_tfms) {
541 max_tfms += MAX_FONTS_STEP;
542 tfm = (struct a_tfm *)realloc(tfm, max_tfms * sizeof(struct a_tfm));
543 }
544
545 a_tfm_init(tfm + numtfms);
546
547 if (!(tfm_file = fopen(file_name, "rb"))) {
548 fprintf(stderr, "\n** Fatal: Specified TFM/OFM file '%s' ", tfm_name);
549 fprintf(stderr, "cannot be opened.\n");
550 exit(1);
551 }
552
553 fseek(tfm_file, 0L, SEEK_END);
554 tfm_filesize = ftell(tfm_file);
555 rewind(tfm_file);
556
557 if (tfm_filesize < 24) {
558 fprintf(stderr, "\n** Fatal: TFM/OFM file too small to be a valid file.\n");
559 exit(1);
560 }
561
562 if (format == OFM_FORMAT) get_ofm(&tfm[numtfms]);
563 else get_tfm(&tfm[numtfms]);
564
565 tfm[numtfms].tex_name = (char *)calloc(strlen(tfm_name)+1, sizeof(char));
566 strcpy(tfm[numtfms].tex_name, tfm_name);
567 fclose(tfm_file);
568
569 return numtfms++;
570 }
571
572 void tfm_close_all(void)
573 {
574 int i;
575 for (i=0; i<numtfms; i++) {
576 if (tfm[i].header)
577 free(tfm[i].header);
578 if (tfm[i].char_info)
579 free(tfm[i].char_info);
580 if (tfm[i].width)
581 free(tfm[i].width);
582 if (tfm[i].height)
583 free(tfm[i].height);
584 if (tfm[i].depth)
585 free(tfm[i].depth);
586 free(tfm[i].tex_name);
587 free(tfm[i].unpacked_widths);
588 free(tfm[i].unpacked_heights);
589 free(tfm[i].unpacked_depths);
590 if (tfm[i].chartypes)
591 free(tfm[i].chartypes);
592 if (tfm[i].width_index)
593 free(tfm[i].width_index);
594 if (tfm[i].height_index)
595 free(tfm[i].height_index);
596 if (tfm[i].depth_index)
597 free(tfm[i].depth_index);
598 }
599 if (tfm)
600 free(tfm);
601 }
602
603 /* tfm_get_width returns the width of the font
604 as a (double) fraction of the design size */
605 double tfm_get_width (int font_id, UNSIGNED_QUAD ch)
606 {
607 if (tfm[font_id].id == JFM_ID || tfm[font_id].id == JFMV_ID )
608 ch = (tfm[font_id].chartypes)[ch];
609 if (tfm[font_id].unpacked_widths && ch <= tfm[font_id].ec)
610 return (double) (tfm[font_id].unpacked_widths)[ch] / FWBASE;
611 else return 0.0;
612 }
613
614 double tfm_get_height (int font_id, UNSIGNED_QUAD ch)
615 {
616 if (tfm[font_id].id == JFM_ID || tfm[font_id].id == JFMV_ID )
617 ch = (tfm[font_id].chartypes)[ch];
618 if (tfm[font_id].unpacked_heights && ch <= tfm[font_id].ec)
619 return (double) (tfm[font_id].unpacked_heights)[ch] / FWBASE;
620 else return 0.0;
621 }
622
623 double tfm_get_depth (int font_id, UNSIGNED_QUAD ch)
624 {
625 if (tfm[font_id].id == JFM_ID || tfm[font_id].id == JFMV_ID )
626 ch = (tfm[font_id].chartypes)[ch];
627 if (tfm[font_id].unpacked_depths && ch <= tfm[font_id].ec)
628 return (tfm[font_id].unpacked_depths)[ch]/FWBASE;
629 else return 0.0;
630 }
631
632 fixword tfm_get_fw_width (int font_id, UNSIGNED_QUAD ch)
633 {
634 if (tfm[font_id].id == JFM_ID || tfm[font_id].id == JFMV_ID )
635 ch = (tfm[font_id].chartypes)[ch];
636 if (tfm[font_id].unpacked_widths && ch <= tfm[font_id].ec) {
637 return (tfm[font_id].unpacked_widths)[ch];
638 }
639 return 0;
640 }
641
642 fixword tfm_get_fw_height (int font_id, UNSIGNED_QUAD ch)
643 {
644 if (tfm[font_id].id == JFM_ID || tfm[font_id].id == JFMV_ID )
645 ch = (tfm[font_id].chartypes)[ch];
646 if (tfm[font_id].unpacked_heights && ch <= tfm[font_id].ec)
647 return (tfm[font_id].unpacked_heights)[ch];
648 return 0;
649 }
650
651 fixword tfm_get_fw_depth (int font_id, UNSIGNED_QUAD ch)
652 {
653 if (tfm[font_id].id == JFM_ID || tfm[font_id].id == JFMV_ID )
654 ch = (tfm[font_id].chartypes)[ch];
655 if (tfm[font_id].unpacked_depths && ch <= tfm[font_id].ec)
656 return (tfm[font_id].unpacked_depths)[ch];
657 return 0;
658 }
659
660 fixword tfm_string_width (int font_id, unsigned char *s, unsigned len)
661 {
662 fixword result = 0;
663 unsigned i;
664 if (tfm[font_id].unpacked_widths)
665 for (i=0; i<len; i++) {
666 if (s[i] <= tfm[font_id].ec)
667 result += tfm[font_id].unpacked_widths[s[i]];
668 }
669 return result;
670 }
671
672 fixword tfm_string_depth (int font_id, unsigned char *s, unsigned len)
673 {
674 fixword result = 0;
675 unsigned i;
676 if (tfm[font_id].unpacked_depths)
677 for (i=0; i<len; i++) {
678 if (s[i] <= tfm[font_id].ec)
679 result = (result > tfm[font_id].unpacked_depths[s[i]] ? result : tfm[font_id].unpacked_depths[s[i]]);
680 }
681 return result;
682 }
683
684 fixword tfm_string_height (int font_id, unsigned char *s, unsigned len)
685 {
686 fixword result = 0;
687 unsigned i;
688 if (tfm[font_id].unpacked_heights)
689 for (i=0; i<len; i++) {
690 if (s[i] <= tfm[font_id].ec)
691 result = (result > tfm[font_id].unpacked_heights[s[i]-tfm[font_id].bc] ? result : tfm[font_id].unpacked_heights[s[i]-tfm[font_id].bc]);
692 }
693 return result;
694 }
695
696 UNSIGNED_PAIR tfm_get_firstchar (int font_id)
697 {
698 return tfm[font_id].bc;
699 }
700
701 UNSIGNED_PAIR tfm_get_lastchar (int font_id)
702 {
703 return tfm[font_id].ec;
704 }
705
706 double tfm_get_design_size (int font_id)
707 {
708 return ((tfm[font_id].header))[1]/FWBASE*(72.0/72.27);
709 }
710
711
712 double tfm_get_max_width (int font_id)
713 {
714 SIGNED_QUAD max = 0;
715 int i;
716 for (i=0; i<tfm[font_id].nwidths; i++) {
717 if ((tfm[font_id].width)[i] > max)
718 max = (tfm[font_id].width)[i];
719 }
720 return (max/FWBASE);
721 }
722
723 int tfm_is_fixed_width (int font_id)
724 {
725 /* We always have two widths since width[0] = 0.
726 A fixed width font will have width[1] = something
727 and not have any other widths */
728 return (tfm[font_id].nwidths == 2);
729 }
730
731 double tfm_get_max_height (int font_id)
732 {
733 SIGNED_QUAD max = 0;
734 int i;
735 for (i=0; i<tfm[font_id].nheights; i++) {
736 if ((tfm[font_id].height)[i] > max)
737 max = (tfm[font_id].height)[i];
738 }
739 return (max/FWBASE);
740 }
741
742 double tfm_get_max_depth (int font_id)
743 {
744 SIGNED_QUAD max = 0;
745 int i;
746 for (i=0; i<tfm[font_id].ndepths; i++) {
747 if ((tfm[font_id].depth)[i] > max)
748 max = (tfm[font_id].depth)[i];
749 }
750 return (max/FWBASE);
751 }
752
753 char is_jfm (int font_id)
754 {
755 return (tfm[font_id].id == JFM_ID || tfm[font_id].id == JFMV_ID) ? 1 : 0;
756 }
757
758 char is_vertical (int font_id) /* Vertical version of JFM */
759 {
760 return (tfm[font_id].id == JFMV_ID) ? 1 : 0;
761 }