Debian Codegraph @

texk/dvipdfm-x/tfm.c

raw
1 /* This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks.
2
3 Copyright (C) 2002-2021 by Jin-Hwan Cho and Shunsaku Hirata,
4 the dvipdfmx project team.
5
6 Copyright (C) 1998, 1999 by Mark A. Wicks <[email protected]>
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <string.h>
28
29 #include "system.h"
30 #include "mem.h"
31 #include "mfileio.h"
32 #include "error.h"
33
34 #include "numbers.h"
35 #include "dpxconf.h"
36 #include "dpxutil.h"
37
38 #include "tfm.h"
39 #include "dvi.h"
40
41 #define TFM_FORMAT 1
42 #define OFM_FORMAT 2
43
44 #define FWBASE ((double) (1<<20))
45
46 #ifndef WITHOUT_ASCII_PTEX
47 /*
48 * ID is 9 for vertical JFM file.
49 */
50 #define JFM_ID 11
51 #define JFMV_ID 9
52 #define IS_JFM(i) ((i) == JFM_ID || (i) == JFMV_ID)
53
54 #define CHARACTER_INDEX(i) ((i > UCS_LASTCHAR ? UCS_LASTCHAR+1 : i))
55 #else
56 #define CHARACTER_INDEX(i) ((i))
57 #endif
58
59 #define IS_WIDE_CHAR(i) (((i)>=0x2E80 && !(0xFB00<=(i) && (i)<=0xFB06)))
60
61 /*
62 * TFM Record structure:
63 * Multiple TFM's may be read in at once.
64 */
65
66 struct tfm_font
67 {
68 #ifndef WITHOUT_ASCII_PTEX
69 int id;
70 int nt;
71 #endif /* !WITHOUT_ASCII_PTEX */
72 #ifndef WITHOUT_OMEGA
73 int32_t level;
74 #endif /* !WITHOUT_OMEGA */
75 uint32_t wlenfile;
76 uint32_t wlenheader;
77 uint32_t bc, ec;
78 uint32_t nwidths, nheights, ndepths;
79 uint32_t nitcor, nlig, nkern, nextens;
80 uint32_t nfonparm;
81 #ifndef WITHOUT_OMEGA
82 uint32_t fontdir, iswide;
83 uint32_t nco, ncw, npc;
84 #endif /* !WITHOUT_OMEGA */
85 fixword *header;
86 #ifndef WITHOUT_ASCII_PTEX
87 unsigned int *chartypes;
88 #endif /* !WITHOUT_ASCII_PTEX */
89 uint32_t *char_info;
90 unsigned short *width_index;
91 unsigned char *height_index;
92 unsigned char *depth_index;
93 fixword *width;
94 fixword *height;
95 fixword *depth;
96 };
97
98 static void
99 tfm_font_init (struct tfm_font *tfm)
100 {
101 tfm->header = NULL;
102 #ifndef WITHOUT_ASCII_PTEX
103 tfm->id = 0;
104 tfm->nt = 0;
105 tfm->chartypes = NULL;
106 #endif /* !WITHOUT_ASCII_PTEX */
107 #ifndef WITHOUT_OMEGA
108 tfm->level = 0;
109 tfm->fontdir = 0;
110 tfm->iswide = 0;
111 tfm->nco = tfm->ncw = tfm->npc = 0;
112 #endif
113 tfm->char_info = NULL;
114 tfm->width_index = NULL;
115 tfm->height_index = NULL;
116 tfm->depth_index = NULL;
117 tfm->width = tfm->height = tfm->depth = NULL;
118 }
119
120 static void
121 tfm_font_clear (struct tfm_font *tfm)
122 {
123 if (tfm) {
124 if (tfm->header) {
125 RELEASE(tfm->header);
126 tfm->header = NULL;
127 }
128 if (tfm->char_info) {
129 RELEASE(tfm->char_info);
130 tfm->char_info = NULL;
131 }
132 if (tfm->width) {
133 RELEASE(tfm->width);
134 tfm->width = NULL;
135 }
136 if (tfm->height) {
137 RELEASE(tfm->height);
138 tfm->height = NULL;
139 }
140 if (tfm->depth) {
141 RELEASE(tfm->depth);
142 tfm->depth = NULL;
143 }
144 #ifndef WITHOUT_ASCII_PTEX
145 if (tfm->chartypes) {
146 RELEASE(tfm->chartypes);
147 tfm->chartypes = NULL;
148 }
149 #endif /* !WITHOUT_ASCII_PTEX */
150 if (tfm->width_index) {
151 RELEASE(tfm->width_index);
152 tfm->width_index = NULL;
153 }
154 if (tfm->height_index) {
155 RELEASE(tfm->height_index);
156 tfm->height_index = NULL;
157 }
158 if (tfm->depth_index) {
159 RELEASE(tfm->depth_index);
160 tfm->depth_index = NULL;
161 }
162 }
163 }
164
165
166 struct coverage
167 {
168 int first_char;
169 int num_chars;
170 #ifndef WITHOUT_ASCII_PTEX
171 int last_char;
172 #endif
173 };
174
175 /*
176 * All characters in the same range have same metrics.
177 */
178
179 struct range_map {
180 unsigned short num_coverages;
181 struct coverage *coverages;
182 unsigned short *indices;
183 };
184
185 /* Special case of num_coverages = 1 */
186 struct char_map
187 {
188 struct coverage coverage;
189 #ifndef WITHOUT_ASCII_PTEX
190 unsigned int *indices;
191 #else
192 unsigned short *indices;
193 #endif
194 };
195
196 static void
197 release_char_map (struct char_map *map)
198 {
199 if (map->indices)
200 RELEASE(map->indices);
201 map->indices = NULL;
202 RELEASE(map);
203 }
204
205 static void
206 release_range_map (struct range_map *map)
207 {
208 if (map->coverages)
209 RELEASE(map->coverages);
210 if (map->indices)
211 RELEASE(map->indices);
212 map->coverages = NULL;
213 map->indices = NULL;
214 RELEASE(map);
215 }
216
217 static int
218 lookup_char (const struct char_map *map, int charcode)
219 {
220 if (charcode >= map->coverage.first_char &&
221 charcode <= map->coverage.first_char + map->coverage.num_chars)
222 return map->indices[CHARACTER_INDEX(charcode - map->coverage.first_char)];
223
224 #ifndef WITHOUT_ASCII_PTEX
225 if (charcode <= map->coverage.last_char)
226 return map->indices[0];
227 #endif
228
229 return -1;
230 }
231
232 static int
233 lookup_range (const struct range_map *map, int charcode)
234 {
235 int idx;
236
237 for (idx = map->num_coverages - 1; idx >= 0 &&
238 charcode >= map->coverages[idx].first_char; idx--) {
239 if (charcode <=
240 map->coverages[idx].first_char + map->coverages[idx].num_chars)
241 return map->indices[CHARACTER_INDEX(idx)];
242 }
243 #ifndef WITHOUT_ASCII_PTEX
244 if (charcode <= JFM_LASTCHAR)
245 return map->indices[0];
246 #endif
247
248 return -1;
249 }
250
251 #define SOURCE_TYPE_TFM 0
252 #define SOURCE_TYPE_JFM 1
253 #define SOURCE_TYPE_OFM 2
254
255 #define MAPTYPE_NONE 0
256 #define MAPTYPE_CHAR 1
257 #define MAPTYPE_RANGE 2
258
259 #define FONT_DIR_HORIZ 0
260 #define FONT_DIR_VERT 1
261 #define FONT_DIR_RT 5
262
263 struct font_metric
264 {
265 char *tex_name;
266 fixword designsize;
267 char *codingscheme;
268
269 int level, fontdir, iswide;
270 int firstchar, lastchar;
271
272 fixword *widths;
273 fixword *heights;
274 fixword *depths;
275
276 struct {
277 int type;
278 void *data;
279 } charmap;
280
281 int source;
282 };
283
284 static void
285 fm_init (struct font_metric *fm)
286 {
287 fm->tex_name = NULL;
288 fm->firstchar = 0;
289 fm->lastchar = 0;
290 fm->fontdir = FONT_DIR_HORIZ;
291 fm->codingscheme = NULL;
292 fm->designsize = 0;
293
294 fm->widths = NULL;
295 fm->heights = NULL;
296 fm->depths = NULL;
297
298 fm->charmap.type = MAPTYPE_NONE;
299 fm->charmap.data = NULL;
300
301 fm->source = SOURCE_TYPE_TFM;
302 }
303
304 static void
305 fm_clear (struct font_metric *fm)
306 {
307 if (fm) {
308 if (fm->tex_name)
309 RELEASE(fm->tex_name);
310 if (fm->widths)
311 RELEASE(fm->widths);
312 if (fm->heights)
313 RELEASE(fm->heights);
314 if (fm->depths)
315 RELEASE(fm->depths);
316 if (fm->codingscheme)
317 RELEASE(fm->codingscheme);
318
319 switch (fm->charmap.type) {
320 case MAPTYPE_CHAR:
321 release_char_map(fm->charmap.data);
322 break;
323 case MAPTYPE_RANGE:
324 release_range_map(fm->charmap.data);
325 break;
326 }
327 }
328 }
329
330 #ifndef MAX_FONTS
331 #define MAX_FONTS 16
332 #endif
333
334 struct font_metric *fms = NULL;
335 static unsigned numfms = 0, max_fms = 0;
336
337 static void
338 fms_need (unsigned n)
339 {
340 if (n > max_fms) {
341 max_fms = MAX(max_fms + MAX_FONTS, n);
342 fms = RENEW(fms, max_fms, struct font_metric);
343 }
344 }
345
346 static int
347 fread_fwords (fixword *words, int32_t nmemb, FILE *fp)
348 {
349 int i;
350
351 for (i = 0; i < nmemb; i++)
352 words[i] = get_signed_quad(fp);
353
354 return nmemb*4;
355 }
356
357 static int
358 fread_uquads (uint32_t *quads, int32_t nmemb, FILE *fp)
359 {
360 int i;
361
362 for (i = 0; i < nmemb; i++) {
363 quads[i] = get_unsigned_quad(fp);
364 }
365
366 return nmemb*4;
367 }
368
369 /*
370 * TFM and JFM
371 */
372 static void
373 tfm_check_size (struct tfm_font *tfm, off_t tfm_file_size)
374 {
375 uint32_t expected_size = 6;
376
377 /* Removed the warning message caused by EC TFM metric files.
378 *
379 if (tfm->wlenfile != tfm_file_size / 4) {
380 WARN("TFM file size is %ld bytes but it says it is %ld bytes!",
381 tfm_file_size, tfm->wlenfile * 4);
382 if (tfm_file_size > tfm->wlenfile * 4) {
383 WARN("Proceeding nervously...");
384 } else {
385 ERROR("Can't proceed...");
386 }
387 }
388 */
389 if ((int64_t)tfm_file_size < (int64_t)tfm->wlenfile * 4) {
390 ERROR("Can't proceed...");
391 }
392
393 expected_size += (tfm->ec - tfm->bc + 1);
394 expected_size += tfm->wlenheader;
395 expected_size += tfm->nwidths;
396 expected_size += tfm->nheights;
397 expected_size += tfm->ndepths;
398 expected_size += tfm->nitcor;
399 expected_size += tfm->nlig;
400 expected_size += tfm->nkern;
401 expected_size += tfm->nextens;
402 expected_size += tfm->nfonparm;
403 #ifndef WITHOUT_ASCII_PTEX
404 if (IS_JFM(tfm->id)) {
405 expected_size += tfm->nt + 1;
406 }
407 #endif /* !WITHOUT_ASCII_PTEX */
408 if (expected_size != tfm->wlenfile) {
409 WARN("TFM file size is expected to be %" PRId64 " bytes but it says it is %" PRId64 "bytes!",
410 (int64_t)expected_size * 4, (int64_t)tfm->wlenfile * 4);
411 if ((int64_t)tfm_file_size > (int64_t)expected_size *4) {
412 WARN("Proceeding nervously...");
413 } else {
414 ERROR("Can't proceed...");
415 }
416 }
417 }
418
419 static void
420 tfm_get_sizes (FILE *tfm_file, off_t tfm_file_size, struct tfm_font *tfm)
421 {
422 #ifndef WITHOUT_ASCII_PTEX
423 {
424 unsigned short first_hword;
425
426 /*
427 * The first half word of TFM/JFM is TFM ID for JFM or size of
428 * TFM file in word for TFM. TFM with 9*4 or 11*4 bytes is not
429 * expected to be a valid TFM. So, we always assume that TFMs
430 * starting with 00 09 or 00 0B is JFM.
431 */
432 first_hword = get_unsigned_pair(tfm_file);
433 if (IS_JFM(first_hword)) {
434 tfm->id = first_hword;
435 tfm->nt = get_unsigned_pair(tfm_file);
436 tfm->wlenfile = get_unsigned_pair(tfm_file);
437 } else {
438 tfm->wlenfile = first_hword;
439 }
440 }
441 #else /* WITHOUT_ASCII_PTEX */
442 tfm->wlenfile = get_unsigned_pair(tfm_file);
443 #endif /* !WITHOUT_ASCII_PTEX */
444
445 tfm->wlenheader = get_unsigned_pair(tfm_file);
446 tfm->bc = get_unsigned_pair(tfm_file);
447 tfm->ec = get_unsigned_pair(tfm_file);
448 if (tfm->ec < tfm->bc) {
449 ERROR("TFM file error: ec(%u) < bc(%u) ???", tfm->ec, tfm->bc);
450 }
451 tfm->nwidths = get_unsigned_pair(tfm_file);
452 tfm->nheights = get_unsigned_pair(tfm_file);
453 tfm->ndepths = get_unsigned_pair(tfm_file);
454 tfm->nitcor = get_unsigned_pair(tfm_file);
455 tfm->nlig = get_unsigned_pair(tfm_file);
456 tfm->nkern = get_unsigned_pair(tfm_file);
457 tfm->nextens = get_unsigned_pair(tfm_file);
458 tfm->nfonparm = get_unsigned_pair(tfm_file);
459
460 tfm_check_size(tfm, tfm_file_size);
461
462 return;
463 }
464
465 #ifndef WITHOUT_ASCII_PTEX
466 static unsigned int
467 get_unsigned_triple_kanji(FILE *file)
468 {
469 unsigned int triple = get_unsigned_byte(file);
470 triple = (triple << 8) | get_unsigned_byte(file);
471 triple = triple | (get_unsigned_byte(file)<<16);
472 return triple;
473 }
474
475 static void
476 jfm_do_char_type_array (FILE *tfm_file, struct tfm_font *tfm)
477 {
478 unsigned int charcode;
479 unsigned short chartype;
480 unsigned int i;
481
482 tfm->chartypes = NEW(UCS_LASTCHAR + 1, unsigned int);
483 for (i = 0; i < (UCS_LASTCHAR + 1); i++) {
484 tfm->chartypes[i] = 0;
485 }
486 for (i = 0; i < tfm->nt; i++) {
487 /* support new JFM spec by texjporg */
488 charcode = get_unsigned_triple_kanji(tfm_file);
489 chartype = get_unsigned_byte(tfm_file);
490 if (charcode < (UCS_LASTCHAR + 1))
491 tfm->chartypes[charcode] = chartype;
492 else {
493 /* Invalid charcode */
494 }
495 }
496 }
497
498 static void
499 jfm_make_charmap (struct font_metric *fm, struct tfm_font *tfm)
500 {
501 if (tfm->nt > 1) {
502 struct char_map *map;
503 int code;
504
505 fm->charmap.type = MAPTYPE_CHAR;
506 fm->charmap.data = map = NEW(1, struct char_map);
507 map->coverage.first_char = 0;
508 #ifndef WITHOUT_ASCII_PTEX
509 map->coverage.num_chars = UCS_LASTCHAR;
510 map->coverage.last_char = JFM_LASTCHAR;
511 map->indices = NEW(UCS_LASTCHAR + 2, unsigned int);
512 map->indices[UCS_LASTCHAR + 1] = tfm->chartypes[0];
513 for (code = 0; code <= UCS_LASTCHAR; code++) {
514 #else
515 map->coverage.num_chars = 0xFFFFL;
516 map->indices = NEW(0x10000L, unsigned short);
517 for (code = 0; code <= 0xFFFFU; code++) {
518 #endif
519 map->indices[code] = tfm->chartypes[code];
520 }
521 } else {
522 struct range_map *map;
523
524 fm->charmap.type = MAPTYPE_RANGE;
525 fm->charmap.data = map = NEW(1, struct range_map);
526 map->num_coverages = 1;
527 map->coverages = NEW(map->num_coverages, struct coverage);
528 map->coverages[0].first_char = 0;
529 #ifndef WITHOUT_ASCII_PTEX
530 map->coverages[0].num_chars = UCS_LASTCHAR;
531 map->coverages[0].last_char = JFM_LASTCHAR;
532 #else
533 map->coverages[0].num_chars = 0xFFFFL;
534 #endif
535 map->indices = NEW(1, unsigned short);
536 map->indices[0] = 0; /* Only default type used. */
537 }
538 }
539 #endif /* !WITHOUT_ASCII_PTEX */
540
541 static void
542 tfm_unpack_arrays (struct font_metric *fm, struct tfm_font *tfm)
543 {
544 uint32_t charinfo;
545 unsigned short width_index;
546 unsigned char height_index, depth_index;
547 int i;
548
549 fm->widths = NEW(256, fixword);
550 fm->heights = NEW(256, fixword);
551 fm->depths = NEW(256, fixword);
552 for (i = 0; i < 256; i++) {
553 fm->widths [i] = 0;
554 fm->heights[i] = 0;
555 fm->depths [i] = 0;
556 }
557
558 for (i = tfm->bc; i <= tfm->ec; i++ ) {
559 charinfo = tfm->char_info[i - tfm->bc];
560 width_index = (charinfo >> 24);
561 height_index = (charinfo >> 20) & 0xf;
562 depth_index = (charinfo >> 16) & 0xf;
563 fm->widths [i] = tfm->width [width_index];
564 fm->heights[i] = tfm->height[height_index];
565 fm->depths [i] = tfm->depth [depth_index];
566 }
567
568 return;
569 }
570
571 static int
572 sput_bigendian (char *s, int32_t v, int n)
573 {
574 int i;
575
576 for (i = n-1; i >= 0; i--) {
577 s[i] = (char) (v & 0xff);
578 v >>= 8;
579 }
580
581 return n;
582 }
583
584 static void
585 tfm_unpack_header (struct font_metric *fm, struct tfm_font *tfm)
586 {
587 if (tfm->wlenheader < 12) {
588 fm->codingscheme = NULL;
589 } else {
590 int i, len;
591 char *p;
592
593 len = (tfm->header[2] >> 24);
594 if (len < 0 || len > 39)
595 ERROR("Invalid TFM header.");
596 if (len > 0) {
597 fm->codingscheme = NEW(40, char);
598 p = fm->codingscheme;
599 p += sput_bigendian(p, tfm->header[2], 3);
600 for (i = 1; i <= len / 4; i++) {
601 p += sput_bigendian(p, tfm->header[2+i], 4);
602 }
603 fm->codingscheme[len] = '\0';
604 } else {
605 fm->codingscheme = NULL;
606 }
607 }
608
609 fm->designsize = tfm->header[1];
610 }
611
612 #ifndef WITHOUT_OMEGA
613
614 static void
615 ofm_check_size_one (struct tfm_font *tfm, off_t ofm_file_size)
616 {
617 uint32_t ofm_size = 14;
618
619 ofm_size += 2*(tfm->ec - tfm->bc + 1);
620 ofm_size += tfm->wlenheader;
621 ofm_size += tfm->nwidths;
622 ofm_size += tfm->nheights;
623 ofm_size += tfm->ndepths;
624 ofm_size += tfm->nitcor;
625 ofm_size += 2*(tfm->nlig);
626 ofm_size += tfm->nkern;
627 ofm_size += 2*(tfm->nextens);
628 ofm_size += tfm->nfonparm;
629 if (tfm->wlenfile != ofm_file_size / 4 ||
630 tfm->wlenfile != ofm_size) {
631 ERROR("OFM file problem. Table sizes don't agree.");
632 }
633 }
634
635 static void
636 ofm_get_sizes (FILE *ofm_file, off_t ofm_file_size, struct tfm_font *tfm)
637 {
638 tfm->level = get_signed_quad(ofm_file);
639
640 tfm->wlenfile = get_positive_quad(ofm_file, "OFM", "wlenfile");
641 tfm->wlenheader = get_positive_quad(ofm_file, "OFM", "wlenheader");
642 tfm->bc = get_positive_quad(ofm_file, "OFM", "bc");
643 tfm->ec = get_positive_quad(ofm_file, "OFM", "ec");
644 if (tfm->ec < tfm->bc) {
645 ERROR("OFM file error: ec(%u) < bc(%u) ???", tfm->ec, tfm->bc);
646 }
647 tfm->nwidths = get_positive_quad(ofm_file, "OFM", "nwidths");
648 tfm->nheights = get_positive_quad(ofm_file, "OFM", "nheights");
649 tfm->ndepths = get_positive_quad(ofm_file, "OFM", "ndepths");
650 tfm->nitcor = get_positive_quad(ofm_file, "OFM", "nitcor");
651 tfm->nlig = get_positive_quad(ofm_file, "OFM", "nlig");
652 tfm->nkern = get_positive_quad(ofm_file, "OFM", "nkern");
653 tfm->nextens = get_positive_quad(ofm_file, "OFM", "nextens");
654 tfm->nfonparm = get_positive_quad(ofm_file, "OFM", "nfonparm");
655 tfm->fontdir = get_positive_quad(ofm_file, "OFM", "fontdir");
656 if (tfm->fontdir) {
657 #ifndef WITHOUT_ASCII_PTEX
658 if (dvi_ptex_with_vert && tfm->fontdir==FONT_DIR_RT && tfm->level==1 && tfm->ec>=0x2E00) {
659 /* interpret FONTDIR RT as pTeX vertical writing */
660 if (dpx_conf.verbose_level > 0)
661 WARN("I will interpret a font direction as pTeX vertical writing.");
662 } else
663 #endif /* !WITHOUT_ASCII_PTEX */
664 {
665 WARN("I may be interpreting a font direction incorrectly.");
666 }
667 }
668 if (tfm->level == 0) {
669 ofm_check_size_one(tfm, ofm_file_size);
670 } else if (tfm->level == 1) {
671 tfm->nco = get_positive_quad(ofm_file, "OFM", "nco");
672 tfm->ncw = get_positive_quad(ofm_file, "OFM", "nco");
673 tfm->npc = get_positive_quad(ofm_file, "OFM", "npc");
674 xseek_absolute(ofm_file, 4*(off_t)(tfm->nco - tfm->wlenheader), "OFM");
675 } else {
676 ERROR("Can't handle OFM files with level > 1");
677 }
678
679 return;
680 }
681
682 static void
683 ofm_do_char_info_zero (FILE *tfm_file, struct tfm_font *tfm)
684 {
685 uint32_t num_chars;
686
687 num_chars = tfm->ec - tfm->bc + 1;
688 if (num_chars != 0) {
689 uint32_t i;
690
691 tfm->width_index = NEW(num_chars, unsigned short);
692 tfm->height_index = NEW(num_chars, unsigned char);
693 tfm->depth_index = NEW(num_chars, unsigned char);
694 for (i = 0; i < num_chars; i++) {
695 tfm->width_index [i] = get_unsigned_pair(tfm_file);
696 tfm->height_index[i] = get_unsigned_byte(tfm_file);
697 tfm->depth_index [i] = get_unsigned_byte(tfm_file);
698 /* Ignore remaining quad */
699 skip_bytes(4, tfm_file);
700 }
701 }
702 }
703
704 static void
705 ofm_do_char_info_one (FILE *tfm_file, struct tfm_font *tfm)
706 {
707 uint32_t num_char_infos;
708 uint32_t num_chars;
709
710 num_char_infos = tfm->ncw / (3 + (tfm->npc / 2));
711 num_chars = tfm->ec - tfm ->bc + 1;
712
713 if (num_chars != 0) {
714 uint32_t i;
715 uint32_t char_infos_read;
716
717 tfm->width_index = NEW(num_chars, unsigned short);
718 tfm->height_index = NEW(num_chars, unsigned char);
719 tfm->depth_index = NEW(num_chars, unsigned char);
720 char_infos_read = 0;
721 for (i = 0; i < num_chars &&
722 char_infos_read < num_char_infos; i++) {
723 int repeats, j;
724
725 tfm->width_index [i] = get_unsigned_pair(tfm_file);
726 tfm->height_index[i] = get_unsigned_byte(tfm_file);
727 tfm->depth_index [i] = get_unsigned_byte(tfm_file);
728 /* Ignore next quad */
729 skip_bytes(4, tfm_file);
730 repeats = get_unsigned_pair(tfm_file);
731 /* Skip params */
732 for (j = 0; j < tfm->npc; j++) {
733 get_unsigned_pair(tfm_file);
734 }
735 /* Remove word padding if necessary */
736 if (ISEVEN(tfm->npc)){
737 get_unsigned_pair(tfm_file);
738 }
739 char_infos_read++;
740 if (i + repeats > num_chars) {
741 ERROR("Repeats causes number of characters to be exceeded.");
742 }
743 for (j = 0; j < repeats; j++) {
744 tfm->width_index [i+j+1] = tfm->width_index [i];
745 tfm->height_index[i+j+1] = tfm->height_index[i];
746 tfm->depth_index [i+j+1] = tfm->depth_index [i];
747 }
748 if (IS_WIDE_CHAR(i) || IS_WIDE_CHAR(i+repeats)) {
749 tfm->iswide = 1;
750 }
751 /* Skip ahead because we have already handled repeats */
752 i += repeats;
753 }
754 }
755 }
756
757 static void
758 ofm_unpack_arrays (struct font_metric *fm,
759 struct tfm_font *tfm, uint32_t num_chars)
760 {
761 int i;
762
763 fm->widths = NEW(tfm->bc + num_chars, fixword);
764 fm->heights = NEW(tfm->bc + num_chars, fixword);
765 fm->depths = NEW(tfm->bc + num_chars, fixword);
766 for (i = 0; i < num_chars; i++) {
767 fm->widths [tfm->bc + i] = tfm->width [ tfm->width_index [i] ];
768 fm->heights[tfm->bc + i] = tfm->height[ tfm->height_index[i] ];
769 fm->depths [tfm->bc + i] = tfm->depth [ tfm->depth_index [i] ];
770 }
771 }
772
773 static void
774 read_ofm (struct font_metric *fm, FILE *ofm_file, off_t ofm_file_size)
775 {
776 struct tfm_font tfm;
777
778 tfm_font_init(&tfm);
779
780 ofm_get_sizes(ofm_file, ofm_file_size, &tfm);
781
782 if (tfm.level < 0 || tfm.level > 1)
783 ERROR ("OFM level %d not supported.", tfm.level);
784
785 if (tfm.wlenheader > 0) {
786 tfm.header = NEW(tfm.wlenheader, fixword);
787 fread_fwords(tfm.header, tfm.wlenheader, ofm_file);
788 }
789 if (tfm.level == 0) {
790 ofm_do_char_info_zero(ofm_file, &tfm);
791 } else if (tfm.level == 1) {
792 ofm_do_char_info_one(ofm_file, &tfm);
793 }
794 if (tfm.nwidths > 0) {
795 tfm.width = NEW(tfm.nwidths, fixword);
796 fread_fwords(tfm.width, tfm.nwidths, ofm_file);
797 }
798 if (tfm.nheights > 0) {
799 tfm.height = NEW(tfm.nheights, fixword);
800 fread_fwords(tfm.height, tfm.nheights, ofm_file);
801 }
802 if (tfm.ndepths > 0) {
803 tfm.depth = NEW(tfm.ndepths, fixword);
804 fread_fwords(tfm.depth, tfm.ndepths, ofm_file);
805 }
806
807 ofm_unpack_arrays(fm, &tfm, tfm.ec - tfm.bc + 1);
808 tfm_unpack_header(fm, &tfm);
809 fm->firstchar = tfm.bc;
810 fm->lastchar = tfm.ec;
811 fm->level = tfm.level;
812 fm->source = SOURCE_TYPE_OFM;
813 fm->iswide = tfm.iswide;
814
815 tfm_font_clear(&tfm);
816
817 return;
818 }
819 #endif /* !WITHOUT_OMEGA */
820
821 static void
822 read_tfm (struct font_metric *fm, FILE *tfm_file, off_t tfm_file_size)
823 {
824 struct tfm_font tfm;
825
826 tfm_font_init(&tfm);
827
828 tfm_get_sizes(tfm_file, tfm_file_size, &tfm);
829 fm->firstchar = tfm.bc;
830 fm->lastchar = tfm.ec;
831 if (tfm.wlenheader > 0) {
832 tfm.header = NEW(tfm.wlenheader, fixword);
833 fread_fwords(tfm.header, tfm.wlenheader, tfm_file);
834 }
835 #ifndef WITHOUT_ASCII_PTEX
836 if (IS_JFM(tfm.id)) {
837 jfm_do_char_type_array(tfm_file, &tfm);
838 jfm_make_charmap(fm, &tfm);
839 fm->firstchar = 0;
840 fm->lastchar = JFM_LASTCHAR;
841 fm->fontdir = (tfm.id == JFMV_ID) ? FONT_DIR_VERT : FONT_DIR_HORIZ;
842 fm->source = SOURCE_TYPE_JFM;
843 }
844 #endif /* !WITHOUT_ASCII_PTEX */
845 if (tfm.ec - tfm.bc + 1 > 0) {
846 tfm.char_info = NEW(tfm.ec - tfm.bc + 1, uint32_t);
847 fread_uquads(tfm.char_info, tfm.ec - tfm.bc + 1, tfm_file);
848 }
849 if (tfm.nwidths > 0) {
850 tfm.width = NEW(tfm.nwidths, fixword);
851 fread_fwords(tfm.width, tfm.nwidths, tfm_file);
852 }
853 if (tfm.nheights > 0) {
854 tfm.height = NEW(tfm.nheights, fixword);
855 fread_fwords(tfm.height, tfm.nheights, tfm_file);
856 }
857 if (tfm.ndepths > 0) {
858 tfm.depth = NEW(tfm.ndepths, fixword);
859 fread_fwords(tfm.depth, tfm.ndepths, tfm_file);
860 }
861 tfm_unpack_arrays(fm, &tfm);
862 tfm_unpack_header(fm, &tfm);
863
864 tfm_font_clear(&tfm);
865
866 return;
867 }
868
869 int
870 tfm_open (const char *tfm_name, int must_exist)
871 {
872 FILE *tfm_file;
873 int i, format = TFM_FORMAT;
874 off_t tfm_file_size;
875 char *file_name = NULL;
876
877 for (i = 0; i < numfms; i++) {
878 if (!strcmp(tfm_name, fms[i].tex_name))
879 return i;
880 }
881
882 /*
883 * The procedure to search tfm or ofm files:
884 * 1. Search tfm file with the given name with the must_exist flag unset.
885 * 2. Search ofm file with the given name with the must_exist flag unset.
886 * 3. If not found and must_exist flag is set, try again to search
887 * tfm file with the must_exist flag set.
888 * 4. If not found and must_exist flag is not set, return -1.
889 */
890
891
892 /*
893 * We first look for OFM and then TFM.
894 * The reason for this change is incompatibility introduced when dvipdfmx
895 * started to write correct glyph metrics to output PDF for CID fonts.
896 * I'll not explain this in detail... This change is mostly specific to
897 * Japanese support.
898 */
899 #if 0
900 if ((file_name = kpse_find_file(tfm_name, kpse_tfm_format, 0))) {
901 format = TFM_FORMAT;
902 } else if ((file_name = kpse_find_file(tfm_name, kpse_ofm_format, 0))) {
903 format = OFM_FORMAT;
904 }
905 #endif
906 {
907 char *ofm_name, *suffix;
908
909 suffix = strrchr(tfm_name, '.');
910 if (!suffix || (strcasecmp(suffix, ".tfm") != 0 &&
911 strcasecmp(suffix, ".ofm") != 0)) {
912 ofm_name = NEW(strlen(tfm_name) + strlen(".ofm") + 1, char);
913 strcpy(ofm_name, tfm_name);
914 strcat(ofm_name, ".ofm");
915 } else {
916 ofm_name = NULL;
917 }
918 if (ofm_name &&
919 (file_name = kpse_find_file(ofm_name, kpse_ofm_format, 0)) != NULL) {
920 format = OFM_FORMAT;
921 } else if ((file_name =
922 kpse_find_file(tfm_name, kpse_tfm_format, 0)) != NULL) {
923 format = TFM_FORMAT;
924 } else if ((file_name =
925 kpse_find_file(tfm_name, kpse_ofm_format, 0)) != NULL) {
926 suffix = strrchr(file_name, '.');
927 if (suffix && strcasecmp(suffix, ".ofm") == 0)
928 format = OFM_FORMAT;
929 else
930 format = TFM_FORMAT;
931 }
932 if (ofm_name)
933 RELEASE(ofm_name);
934 }
935
936 /*
937 * In case that must_exist is set, MiKTeX returns always non-NULL value
938 * even if the tfm file is not found.
939 */
940 if (file_name == NULL) {
941 if (must_exist) {
942 if ((file_name = kpse_find_file(tfm_name, kpse_tfm_format, 1)) != NULL)
943 format = TFM_FORMAT;
944 else {
945 ERROR("Unable to find TFM file \"%s\".", tfm_name);
946 }
947 } else {
948 return -1;
949 }
950 }
951
952 tfm_file = MFOPEN(file_name, FOPEN_RBIN_MODE);
953 if (!tfm_file) {
954 ERROR("Could not open specified TFM/OFM file \"%s\".", tfm_name);
955 }
956
957 if (dpx_conf.verbose_level > 0) {
958 if (format == TFM_FORMAT)
959 MESG("(TFM:%s", tfm_name);
960 else if (format == OFM_FORMAT)
961 MESG("(OFM:%s", tfm_name);
962 if (dpx_conf.verbose_level > 1)
963 MESG("[%s]", file_name);
964 }
965
966 RELEASE(file_name);
967
968 tfm_file_size = xfile_size (tfm_file, "TFM/OFM");
969 if (tfm_file_size > 0x1ffffffff)
970 ERROR("TFM/OFM file size exceeds 33-bit");
971 if (tfm_file_size < 24) {
972 ERROR("TFM/OFM file too small to be a valid file.");
973 }
974
975 fms_need(numfms + 1);
976 fm_init(fms + numfms);
977
978 #ifndef WITHOUT_OMEGA
979 if (format == OFM_FORMAT)
980 read_ofm(&fms[numfms], tfm_file, tfm_file_size);
981 else
982 #endif /* !WITHOUT_OMEGA */
983 {
984 read_tfm(&fms[numfms], tfm_file, tfm_file_size);
985 }
986
987 MFCLOSE(tfm_file);
988
989 fms[numfms].tex_name = NEW(strlen(tfm_name)+1, char);
990 strcpy(fms[numfms].tex_name, tfm_name);
991
992 if (dpx_conf.verbose_level > 0)
993 MESG(")");
994
995 return numfms++;
996 }
997
998 void
999 tfm_close_all (void)
1000 {
1001 int i;
1002
1003 if (fms) {
1004 for (i = 0; i < numfms; i++) {
1005 fm_clear(&(fms[i]));
1006 }
1007 RELEASE(fms);
1008 }
1009 }
1010
1011 #define CHECK_ID(n) do {\
1012 if ((n) < 0 || (n) >= numfms)\
1013 ERROR("TFM: Invalid TFM ID: %d", (n));\
1014 } while (0)
1015
1016 fixword
1017 tfm_get_fw_width (int font_id, int32_t ch)
1018 {
1019 struct font_metric *fm;
1020 int idx = 0;
1021
1022 CHECK_ID(font_id);
1023
1024 fm = &(fms[font_id]);
1025 if (ch >= fm->firstchar && ch <= fm->lastchar) {
1026 switch (fm->charmap.type) {
1027 case MAPTYPE_CHAR:
1028 idx = lookup_char(fm->charmap.data, ch);
1029 if (idx < 0)
1030 ERROR("Invalid char: %ld\n", ch);
1031 break;
1032 case MAPTYPE_RANGE:
1033 idx = lookup_range(fm->charmap.data, ch);
1034 if (idx < 0)
1035 ERROR("Invalid char: %ld\n", ch);
1036 break;
1037 default:
1038 idx = ch;
1039 }
1040 #ifndef WITHOUT_ASCII_PTEX
1041 } else if (ch > fm->lastchar && ch <= JFM_LASTCHAR) {
1042 idx = 0;
1043 #endif /* !WITHOUT_ASCII_PTEX */
1044 } else {
1045 ERROR("Invalid char: %ld\n", ch);
1046 }
1047
1048 return fm->widths[idx];
1049 }
1050
1051 fixword
1052 tfm_get_fw_height (int font_id, int32_t ch)
1053 {
1054 struct font_metric *fm;
1055 int idx = 0;
1056
1057 CHECK_ID(font_id);
1058
1059 fm = &(fms[font_id]);
1060 if (ch >= fm->firstchar && ch <= fm->lastchar) {
1061 switch (fm->charmap.type) {
1062 case MAPTYPE_CHAR:
1063 idx = lookup_char(fm->charmap.data, ch);
1064 if (idx < 0)
1065 ERROR("Invalid char: %ld\n", ch);
1066 break;
1067 case MAPTYPE_RANGE:
1068 idx = lookup_range(fm->charmap.data, ch);
1069 if (idx < 0)
1070 ERROR("Invalid char: %ld\n", ch);
1071 break;
1072 default:
1073 idx = ch;
1074 }
1075 #ifndef WITHOUT_ASCII_PTEX
1076 } else if (ch > fm->lastchar && ch <= JFM_LASTCHAR) {
1077 idx = 0;
1078 #endif /* !WITHOUT_ASCII_PTEX */
1079 } else {
1080 ERROR("Invalid char: %ld\n", ch);
1081 }
1082
1083 return fm->heights[idx];
1084 }
1085
1086 fixword
1087 tfm_get_fw_depth (int font_id, int32_t ch)
1088 {
1089 struct font_metric *fm;
1090 int idx = 0;
1091
1092 CHECK_ID(font_id);
1093
1094 fm = &(fms[font_id]);
1095 if (ch >= fm->firstchar && ch <= fm->lastchar) {
1096 switch (fm->charmap.type) {
1097 case MAPTYPE_CHAR:
1098 idx = lookup_char(fm->charmap.data, ch);
1099 if (idx < 0)
1100 ERROR("Invalid char: %ld\n", ch);
1101 break;
1102 case MAPTYPE_RANGE:
1103 idx = lookup_range(fm->charmap.data, ch);
1104 if (idx < 0)
1105 ERROR("Invalid char: %ld\n", ch);
1106 break;
1107 default:
1108 idx = ch;
1109 }
1110 #ifndef WITHOUT_ASCII_PTEX
1111 } else if (ch > fm->lastchar && ch <= JFM_LASTCHAR) {
1112 idx = 0;
1113 #endif /* !WITHOUT_ASCII_PTEX */
1114 } else {
1115 ERROR("Invalid char: %ld\n", ch);
1116 }
1117
1118 return fm->depths[idx];
1119 }
1120
1121
1122 /*
1123 * tfm_get_width returns the width of the font
1124 * as a (double) fraction of the design size.
1125 */
1126 double
1127 tfm_get_width (int font_id, int32_t ch)
1128 {
1129 return ((double) tfm_get_fw_width(font_id, ch)/FWBASE);
1130 }
1131
1132 #if 0
1133 double
1134 tfm_get_height (int font_id, int32_t ch)
1135 {
1136 return ((double) tfm_get_fw_height(font_id, ch)/FWBASE);
1137 }
1138
1139 double
1140 tfm_get_depth (int font_id, Sint32_t ch)
1141 {
1142 return ((double) tfm_get_fw_depth(font_id, ch)/FWBASE);
1143 }
1144 #endif
1145
1146 /* tfm_string_xxx() do not work for OFM... */
1147 fixword
1148 tfm_string_width (int font_id, const unsigned char *s, unsigned len)
1149 {
1150 fixword result = 0;
1151 struct font_metric *fm;
1152 unsigned i;
1153
1154 CHECK_ID(font_id);
1155
1156 fm = &(fms[font_id]);
1157 #ifndef WITHOUT_ASCII_PTEX
1158 if (fm->source == SOURCE_TYPE_JFM) {
1159 for (i = 0; i < len/2; i++) {
1160 int32_t ch;
1161
1162 ch = (s[2*i] << 8)|s[2*i+1];
1163 result += tfm_get_fw_width(font_id, ch);
1164 }
1165 } else
1166 #endif
1167 for (i = 0; i < len; i++) {
1168 result += tfm_get_fw_width(font_id, s[i]);
1169 }
1170
1171 return result;
1172 }
1173
1174 #if 0
1175 fixword
1176 tfm_string_depth (int font_id, const unsigned char *s, unsigned len)
1177 {
1178 fixword result = 0;
1179 struct font_metric *fm;
1180 unsigned i;
1181
1182 CHECK_ID(font_id);
1183
1184 fm = &(fms[font_id]);
1185 #ifndef WITHOUT_ASCII_PTEX
1186 if (fm->source == SOURCE_TYPE_JFM) {
1187 for (i = 0; i < len/2; i++) {
1188 int32_t ch;
1189
1190 ch = (s[2*i] << 8)|s[2*i+1];
1191 result += tfm_get_fw_depth(font_id, ch);
1192 }
1193 } else
1194 #endif
1195 for (i = 0; i < len; i++) {
1196 result = MAX(result, tfm_get_fw_depth(font_id, s[i]));
1197 }
1198
1199 return result;
1200 }
1201
1202 fixword
1203 tfm_string_height (int font_id, const unsigned char *s, unsigned len)
1204 {
1205 fixword result = 0;
1206 struct font_metric *fm;
1207 unsigned i;
1208
1209 CHECK_ID(font_id);
1210
1211 fm = &(fms[font_id]);
1212 #ifndef WITHOUT_ASCII_PTEX
1213 if (fm->source == SOURCE_TYPE_JFM) {
1214 for (i = 0; i < len/2; i++) {
1215 int32_t ch;
1216
1217 ch = (s[2*i] << 8)|s[2*i+1];
1218 result += tfm_get_fw_height(font_id, ch);
1219 }
1220 } else
1221 #endif
1222 for (i = 0; i < len; i++) {
1223 result = MAX(result, tfm_get_fw_height(font_id, s[i]));
1224 }
1225
1226 return result;
1227 }
1228 #endif
1229
1230 double
1231 tfm_get_design_size (int font_id)
1232 {
1233 CHECK_ID(font_id);
1234
1235 return (double) (fms[font_id].designsize)/FWBASE*(72.0/72.27);
1236 }
1237
1238 #if 0
1239 char *
1240 tfm_get_codingscheme (int font_id)
1241 {
1242 CHECK_ID(font_id);
1243
1244 return fms[font_id].codingscheme;
1245 }
1246
1247 #ifndef WITHOUT_ASCII_PTEX
1248 /* Vertical version of JFM */
1249 int
1250 tfm_is_vert (int font_id)
1251 {
1252 CHECK_ID(font_id);
1253
1254 return (fms[font_id].fontdir == FONT_DIR_VERT) ? 1 : 0;
1255 }
1256 #else /* WITHOUT_ASCII_PTEX */
1257 int
1258 tfm_is_vert (int font_id)
1259 {
1260 return 0;
1261 }
1262 #endif /* !WITHOUT_ASCII_PTEX */
1263 #endif
1264
1265 #ifndef WITHOUT_ASCII_PTEX
1266 int
1267 tfm_is_jfm (int font_id)
1268 {
1269 int is_jfm = 0;
1270
1271 CHECK_ID(font_id);
1272
1273 if (fms[font_id].source == SOURCE_TYPE_JFM) is_jfm = 1;
1274 #ifndef WITHOUT_OMEGA
1275 if (fms[font_id].source == SOURCE_TYPE_OFM
1276 && fms[font_id].level == 1 && fms[font_id].iswide == 1) is_jfm = 2;
1277 #endif
1278
1279 return is_jfm;
1280 }
1281 #else /* WITHOUT_ASCII_PTEX */
1282 int
1283 tfm_is_jfm (int font_id)
1284 {
1285 return 0;
1286 }
1287 #endif /* !WITHOUT_ASCII_PTEX */
1288
1289 int
1290 tfm_exists (const char *tfm_name)
1291 {
1292 char *fullname;
1293
1294 fullname = kpse_find_file(tfm_name, kpse_ofm_format, 0);
1295 if (fullname) {
1296 RELEASE(fullname);
1297 return 1;
1298 }
1299 fullname = kpse_find_file(tfm_name, kpse_tfm_format, 0);
1300 if (fullname) {
1301 RELEASE(fullname);
1302 return 1;
1303 }
1304
1305 return 0;
1306 }