1 /* tfmfile.c -- readers for TFM, AFM, OTFM-0 and OTFM-1 files */
3 * Copyright (C) 2000, Matias Atria
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include <stdio.h> /* tex-file.h needs this */
37 typedef struct tfmpool {
45 static ListHead tfmpool = {NULL, NULL, 0};
46 static DviHashTable tfmhash;
48 #define TFM_HASH_SIZE 31
50 #ifdef WORD_LITTLE_ENDIAN
51 static inline void swap_array(Uint32 *ptr, int n)
57 *ptr++ = ((i & 0xff000000) >> 24)
58 | ((i & 0x00ff0000) >> 8)
59 | ((i & 0x0000ff00) << 8)
60 | ((i & 0x000000ff) << 24);
67 static int __PROTO(ofm_load_file(const char *filename, TFMInfo *info));
69 /* reading of AFM files */
70 /* macro to convert between AFM and TFM units */
71 #define AFM2TFM(x) FROUND((double)(x) * 0x100000 / 1000)
72 int afm_load_file(const char *filename, TFMInfo *info)
74 /* the information we want is:
84 in = fopen(filename, "rb");
87 status = afm_parse_file(in, &fi, P_GM);
91 mdvi_error(_("%s: Error reading AFM data\n"), filename);
96 info->chars = xnalloc(TFMChar, 256);
99 info->design = 0xa00000; /* fake -- 10pt */
100 info->checksum = 0; /* no checksum */
101 info->type = DviFontAFM;
102 mdvi_strncpy(info->coding, fi->gfi->encodingScheme, 63);
103 mdvi_strncpy(info->family, fi->gfi->familyName, 63);
105 /* now get the data */
106 for(cm = fi->cmi; cm < fi->cmi + fi->numOfChars; cm++) {
111 if(code < 0 || code > 255)
112 continue; /* ignore it */
113 ch = &info->chars[code];
119 ch->advance = AFM2TFM(cm->wx);
120 /* this is the `leftSideBearing' */
121 ch->left = AFM2TFM(cm->charBBox.llx);
122 /* this is the height (ascent - descent) -- the sign is to follow
123 * TeX conventions, as opposed to Adobe's ones */
124 ch->depth = -AFM2TFM(cm->charBBox.lly);
125 /* this is the width (rightSideBearing - leftSideBearing) */
126 ch->right = AFM2TFM(cm->charBBox.urx);
127 /* this is the `ascent' */
128 ch->height = AFM2TFM(cm->charBBox.ury);
131 /* we don't need this anymore */
132 afm_free_fontinfo(fi);
134 /* optimize storage */
135 if(info->loc > 0 || info->hic < 256) {
136 memmove(&info->chars[0],
137 &info->chars[info->loc],
138 (info->hic - info->loc + 1) * sizeof(TFMChar));
139 info->chars = mdvi_realloc(info->chars,
140 (info->hic - info->loc + 1) * sizeof(TFMChar));
147 #endif /* WITH_AFM_FILES */
149 int tfm_load_file(const char *filename, TFMInfo *info)
151 int lf, lh, bc, ec, nw, nh, nd, ne;
165 in = fopen(filename, "rb");
170 DEBUG((DBG_FONTS, "(mt) reading TFM file `%s'\n",
172 /* We read the entire TFM file into core */
173 if(fstat(fileno(in), &st) < 0)
178 /* allocate a word-aligned buffer to hold the file */
179 size = 4 * ROUND(st.st_size, 4);
180 if(size != st.st_size)
181 mdvi_warning(_("Warning: TFM file `%s' has suspicious size\n"),
183 tfm = (Uchar *)mdvi_malloc(size);
184 if(fread(tfm, st.st_size, 1, in) != 1)
186 /* we don't need this anymore */
190 /* not a checksum, but serves a similar purpose */
194 /* get the counters */
196 lh = muget2(ptr); checksum += 6 + lh;
198 ec = muget2(ptr); checksum += ec - bc + 1;
199 nw = muget2(ptr); checksum += nw;
200 nh = muget2(ptr); checksum += nh;
201 nd = muget2(ptr); checksum += nd;
202 checksum += muget2(ptr); /* skip italics correction count */
203 checksum += muget2(ptr); /* skip lig/kern table size */
204 checksum += muget2(ptr); /* skip kern table size */
205 ne = muget2(ptr); checksum += ne;
206 checksum += muget2(ptr); /* skip # of font parameters */
209 cb = (Int32 *)tfm; cb += 6 + lh;
210 charinfo = cb; cb += size;
211 widths = cb; cb += nw;
212 heights = cb; cb += nh;
215 if(widths[0] || heights[0] || depths[0] ||
216 checksum != lf || bc - 1 > ec || ec > 255 || ne > 256)
219 /* from this point on, no error checking is done */
221 /* now we're at the header */
222 /* get the checksum */
223 info->checksum = muget4(ptr);
224 /* get the design size */
225 info->design = muget4(ptr);
226 /* get the coding scheme */
228 /* get the coding scheme */
230 if(n < 0 || n > 39) {
231 mdvi_warning(_("%s: font coding scheme truncated to 40 bytes\n"),
235 memcpy(info->coding, ptr, n);
239 strcpy(info->coding, "FontSpecific");
240 /* get the font family */
245 memcpy(info->family, ptr, i);
248 strcpy(info->family, "unspecified");
251 /* now we don't read from `ptr' anymore */
255 info->type = DviFontTFM;
257 /* allocate characters */
258 info->chars = xnalloc(TFMChar, size);
261 #ifdef WORD_LITTLE_ENDIAN
262 /* byte-swap the three arrays at once (they are consecutive in memory) */
263 swap_array((Uint32 *)widths, nw + nh + nd);
266 /* get the relevant data */
267 ptr = (Uchar *)charinfo;
268 for(i = bc; i <= ec; ptr += 3, i++) {
271 ndx = (int)*ptr; ptr++;
272 info->chars[i-bc].advance = widths[ndx];
273 /* TFM files lack this information */
274 info->chars[i-bc].left = 0;
275 info->chars[i-bc].right = widths[ndx];
276 info->chars[i-bc].present = (ndx != 0);
278 ndx = ((*ptr >> 4) & 0xf);
279 info->chars[i-bc].height = heights[ndx];
281 info->chars[i-bc].depth = depths[ndx];
285 /* free everything */
291 mdvi_error(_("%s: File corrupted, or not a TFM file\n"), filename);
293 if(tfm) mdvi_free(tfm);
298 static int ofm1_load_file(FILE *in, TFMInfo *info)
300 int lf, lh, bc, ec, nw, nh, nd;
319 fuget4(in); /* italics */
320 fuget4(in); /* lig-kern */
321 fuget4(in); /* kern */
322 fuget4(in); /* extensible recipe */
323 fuget4(in); /* parameters */
324 fuget4(in); /* direction */
329 /* get the checksum */
330 info->checksum = fuget4(in);
331 /* the design size */
332 info->design = fuget4(in);
333 /* get the coding scheme */
335 /* get the coding scheme */
339 fread(info->coding, 39, 1, in);
342 strcpy(info->coding, "FontSpecific");
343 /* get the font family */
348 fread(info->family, i, 1, in);
351 strcpy(info->family, "unspecified");
355 /* jump to the beginning of the char-info table */
356 fseek(in, 4L*nco, SEEK_SET);
361 info->chars = xnalloc(TFMChar, size);
362 end = info->chars + size;
364 for(tch = info->chars, i = 0; i < ncw; i++) {
368 /* in the characters we store the actual indices */
369 ch.advance = fuget2(in);
370 ch.height = fuget1(in);
371 ch.depth = fuget1(in);
374 /* get # of repeats */
376 /* skip parameters */
377 fseek(in, (long)npc * 2, SEEK_CUR);
378 /* if npc is odd, skip padding */
379 if(npc & 1) fuget2(in);
381 /* now repeat the character */
382 while(nr-- >= 0 && tch < end)
383 memcpy(tch++, &ch, sizeof(TFMChar));
388 /* I wish we were done, but we aren't */
390 /* get the widths, heights and depths */
392 tfm = xnalloc(Int32, size);
393 /* read them in one sweep */
394 if(fread(tfm, 4, size, in) != size) {
399 /* byte-swap things if necessary */
400 #ifdef WORD_LITTLE_ENDIAN
401 swap_array((Uint32 *)tfm, size);
404 heights = widths + nw;
405 depths = heights + nh;
407 if(widths[0] || heights[0] || depths[0])
410 /* now fix the characters */
412 for(tch = info->chars; tch < end; tch++) {
413 tch->present = (tch->advance != 0);
414 tch->advance = widths[tch->advance];
415 tch->height = heights[tch->height];
416 tch->depth = depths[tch->depth];
418 tch->right = tch->advance;
426 if(tfm) mdvi_free(tfm);
430 /* we don't read OFM files into memory, because they can potentially be large */
431 static int ofm_load_file(const char *filename, TFMInfo *info)
433 int lf, lh, bc, ec, nw, nh, nd;
448 in = fopen(filename, "rb");
452 /* not a checksum, but serves a similar purpose */
455 /* get the counters */
462 DEBUG((DBG_FONTS, "(mt) reading Level-1 OFM file `%s'\n",
464 /* we handle level-1 files separately */
465 if(ofm1_load_file(in, info) < 0)
470 DEBUG((DBG_FONTS, "(mt) reading Level-0 OFM file `%s'\n", filename));
472 lf = fuget4(in); checksum = nwords;
473 lh = fuget4(in); checksum += lh;
475 ec = fuget4(in); checksum += 2 * (ec - bc + 1);
476 nw = fuget4(in); checksum += nw;
477 nh = fuget4(in); checksum += nh;
478 nd = fuget4(in); checksum += nd;
479 checksum += fuget4(in); /* skip italics correction count */
480 checksum += 2*fuget4(in); /* skip lig/kern table size */
481 checksum += fuget4(in); /* skip kern table size */
482 checksum += 2*fuget4(in); /* skip extensible recipe count */
483 checksum += fuget4(in); /* skip # of font parameters */
485 /* I have found several .ofm files that seem to have the
486 * font-direction word missing, so we try to detect that here */
487 if(checksum == lf + 1) {
488 DEBUG((DBG_FONTS, "(mt) font direction missing in `%s'\n",
493 /* skip font direction */
497 if(checksum != lf || bc > ec + 1 || ec > 65535)
500 /* now we're at the header */
502 /* get the checksum */
503 info->checksum = fuget4(in);
504 /* get the design size */
505 info->design = fuget4(in);
507 /* get the coding scheme */
509 /* get the coding scheme */
511 if(n < 0 || n > 39) {
512 mdvi_warning(_("%s: font coding scheme truncated to 40 bytes\n"),
516 fread(info->coding, 39, 1, in);
519 strcpy(info->coding, "FontSpecific");
520 /* get the font family */
525 fread(info->family, i, 1, in);
528 strcpy(info->family, "unspecified");
531 /* now skip anything else in the header */
532 fseek(in, 4L*(nwords + lh), SEEK_SET);
533 /* and read everything at once */
534 size = 2*(ec - bc + 1) + nw + nh + nd;
535 tfm = xnalloc(Int32, size * sizeof(Int32));
536 if(fread(tfm, 4, size, in) != size) {
540 /* byte-swap all the tables at once */
541 #ifdef WORD_LITTLE_ENDIAN
542 swap_array((Uint32 *)tfm, size);
545 charinfo = cb; cb += 2*(ec - bc + 1);
546 widths = cb; cb += nw;
547 heights = cb; cb += nh;
550 if(widths[0] || heights[0] || depths[0]) {
555 /* from this point on, no error checking is done */
557 /* we don't need this anymore */
560 /* now we don't read from `ptr' anymore */
564 info->type = DviFontTFM;
566 /* allocate characters */
567 info->chars = xnalloc(TFMChar, size);
569 /* get the relevant data */
570 ptr = (Uchar *)charinfo;
571 for(i = bc; i <= ec; ptr += 4, i++) {
575 info->chars[i-bc].advance = widths[ndx];
576 /* TFM files lack this information */
577 info->chars[i-bc].left = 0;
578 info->chars[i-bc].right = widths[ndx];
579 info->chars[i-bc].present = (ndx != 0);
581 info->chars[i-bc].height = heights[ndx];
583 info->chars[i-bc].depth = depths[ndx];
590 mdvi_error(_("%s: File corrupted, or not a TFM file\n"), filename);
595 char *lookup_font_metrics(const char *name, int *type)
600 #ifndef WITH_AFM_FILES
604 file = kpse_find_tfm(name);
608 file = kpse_find_ofm(name);
609 /* we may have gotten a TFM back */
611 const char *ext = file_extension(file);
612 if(ext && STREQ(ext, "tfm"))
617 #ifdef WITH_AFM_FILES
619 file = kpse_find_file(name, kpse_afm_format, 0);
622 file = kpse_find_file(name, kpse_afm_format, 0);
625 file = kpse_find_tfm(name);
638 * The next two functions are just wrappers for the font metric loaders,
639 * and use the pool of TFM data
642 /* this is how we interpret arguments:
643 * - if filename is NULL, we look for files of the given type,
644 * unless type is DviFontAny, in which case we try all the
646 * - if filename is not NULL, we look at `type' to decide
647 * how to read the file. If type is DviFontAny, we just
650 TFMInfo *get_font_metrics(const char *short_name, int type, const char *filename)
657 tfm = (TFMPool *)mdvi_hash_lookup(&tfmhash,
658 MDVI_KEY(short_name));
660 DEBUG((DBG_FONTS, "(mt) reusing metric file `%s' (%d links)\n",
661 short_name, tfm->links));
663 return &tfm->tfminfo;
667 file = filename ? (char *)filename : lookup_font_metrics(short_name, &type);
671 tfm = xalloc(TFMPool);
672 DEBUG((DBG_FONTS, "(mt) loading font metric data from `%s'\n", file, file));
675 status = tfm_load_file(file, &tfm->tfminfo);
678 status = ofm_load_file(file, &tfm->tfminfo);
680 #ifdef WITH_AFM_FILES
682 status = afm_load_file(file, &tfm->tfminfo);
695 tfm->short_name = mdvi_strdup(short_name);
697 /* add it to the pool */
698 if(tfmpool.count == 0)
699 mdvi_hash_create(&tfmhash, TFM_HASH_SIZE);
700 mdvi_hash_add(&tfmhash, MDVI_KEY(tfm->short_name),
701 tfm, MDVI_HASH_UNCHECKED);
702 listh_prepend(&tfmpool, LIST(tfm));
705 return &tfm->tfminfo;
708 void free_font_metrics(TFMInfo *info)
712 if(tfmpool.count == 0)
714 /* get the entry -- can't use the hash table for this, because
715 * we don't have the short name */
716 for(tfm = (TFMPool *)tfmpool.head; tfm; tfm = tfm->next)
717 if(info == &tfm->tfminfo)
721 if(--tfm->links > 0) {
722 DEBUG((DBG_FONTS, "(mt) %s not removed, still in use\n",
726 mdvi_hash_remove_ptr(&tfmhash, MDVI_KEY(tfm->short_name));
728 DEBUG((DBG_FONTS, "(mt) removing unused TFM data for `%s'\n", tfm->short_name));
729 listh_remove(&tfmpool, LIST(tfm));
730 mdvi_free(tfm->short_name);
731 mdvi_free(tfm->tfminfo.chars);
735 void flush_font_metrics(void)
739 for(; (ptr = (TFMPool *)tfmpool.head); ) {
740 tfmpool.head = LIST(ptr->next);
742 mdvi_free(ptr->short_name);
743 mdvi_free(ptr->tfminfo.chars);
746 mdvi_hash_reset(&tfmhash, 0);