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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <stdio.h> /* tex-file.h needs this */
36 typedef struct tfmpool {
44 static ListHead tfmpool = {NULL, NULL, 0};
45 static DviHashTable tfmhash;
47 #define TFM_HASH_SIZE 31
49 #ifdef WORD_LITTLE_ENDIAN
50 static inline void swap_array(Uint32 *ptr, int n)
56 *ptr++ = ((i & 0xff000000) >> 24)
57 | ((i & 0x00ff0000) >> 8)
58 | ((i & 0x0000ff00) << 8)
59 | ((i & 0x000000ff) << 24);
66 static int __PROTO(ofm_load_file(const char *filename, TFMInfo *info));
68 /* reading of AFM files */
69 /* macro to convert between AFM and TFM units */
70 #define AFM2TFM(x) FROUND((double)(x) * 0x100000 / 1000)
71 int afm_load_file(const char *filename, TFMInfo *info)
73 /* the information we want is:
83 in = fopen(filename, "r");
86 status = afm_parse_file(in, &fi, P_GM);
90 error(_("%s: Error reading AFM data\n"), filename);
95 info->chars = xnalloc(TFMChar, 256);
98 info->design = 0xa00000; /* fake -- 10pt */
99 info->checksum = 0; /* no checksum */
100 info->type = DviFontAFM;
101 xstrncpy(info->coding, fi->gfi->encodingScheme, 63);
102 xstrncpy(info->family, fi->gfi->familyName, 63);
104 /* now get the data */
105 for(cm = fi->cmi; cm < fi->cmi + fi->numOfChars; cm++) {
110 if(code < 0 || code > 255)
111 continue; /* ignore it */
112 ch = &info->chars[code];
118 ch->advance = AFM2TFM(cm->wx);
119 /* this is the `leftSideBearing' */
120 ch->left = AFM2TFM(cm->charBBox.llx);
121 /* this is the height (ascent - descent) -- the sign is to follow
122 * TeX conventions, as opposed to Adobe's ones */
123 ch->depth = -AFM2TFM(cm->charBBox.lly);
124 /* this is the width (rightSideBearing - leftSideBearing) */
125 ch->right = AFM2TFM(cm->charBBox.urx);
126 /* this is the `ascent' */
127 ch->height = AFM2TFM(cm->charBBox.ury);
130 /* we don't need this anymore */
131 afm_free_fontinfo(fi);
133 /* optimize storage */
134 if(info->loc > 0 || info->hic < 256) {
135 memmove(&info->chars[0],
136 &info->chars[info->loc],
137 (info->hic - info->loc + 1) * sizeof(TFMChar));
138 info->chars = xrealloc(info->chars,
139 (info->hic - info->loc + 1) * sizeof(TFMChar));
146 #endif /* WITH_AFM_FILES */
148 int tfm_load_file(const char *filename, TFMInfo *info)
150 int lf, lh, bc, ec, nw, nh, nd, ne;
164 in = fopen(filename, "r");
169 DEBUG((DBG_FONTS, "(mt) reading TFM file `%s'\n",
171 /* We read the entire TFM file into core */
172 if(fstat(fileno(in), &st) < 0)
177 /* allocate a word-aligned buffer to hold the file */
178 size = 4 * ROUND(st.st_size, 4);
179 if(size != st.st_size)
180 warning(_("Warning: TFM file `%s' has suspicious size\n"),
182 tfm = (Uchar *)xmalloc(size);
183 if(fread(tfm, st.st_size, 1, in) != 1)
185 /* we don't need this anymore */
189 /* not a checksum, but serves a similar purpose */
193 /* get the counters */
195 lh = muget2(ptr); checksum += 6 + lh;
197 ec = muget2(ptr); checksum += ec - bc + 1;
198 nw = muget2(ptr); checksum += nw;
199 nh = muget2(ptr); checksum += nh;
200 nd = muget2(ptr); checksum += nd;
201 checksum += muget2(ptr); /* skip italics correction count */
202 checksum += muget2(ptr); /* skip lig/kern table size */
203 checksum += muget2(ptr); /* skip kern table size */
204 ne = muget2(ptr); checksum += ne;
205 checksum += muget2(ptr); /* skip # of font parameters */
208 cb = (Int32 *)tfm; cb += 6 + lh;
209 charinfo = cb; cb += size;
210 widths = cb; cb += nw;
211 heights = cb; cb += nh;
214 if(widths[0] || heights[0] || depths[0] ||
215 checksum != lf || bc - 1 > ec || ec > 255 || ne > 256)
218 /* from this point on, no error checking is done */
220 /* now we're at the header */
221 /* get the checksum */
222 info->checksum = muget4(ptr);
223 /* get the design size */
224 info->design = muget4(ptr);
225 /* get the coding scheme */
227 /* get the coding scheme */
229 if(n < 0 || n > 39) {
230 warning(_("%s: font coding scheme truncated to 40 bytes\n"),
234 memcpy(info->coding, ptr, n);
238 strcpy(info->coding, "FontSpecific");
239 /* get the font family */
244 memcpy(info->family, ptr, i);
247 strcpy(info->family, "unspecified");
250 /* now we don't read from `ptr' anymore */
254 info->type = DviFontTFM;
256 /* allocate characters */
257 info->chars = xnalloc(TFMChar, size);
260 #ifdef WORD_LITTLE_ENDIAN
261 /* byte-swap the three arrays at once (they are consecutive in memory) */
262 swap_array((Uint32 *)widths, nw + nh + nd);
265 /* get the relevant data */
266 ptr = (Uchar *)charinfo;
267 for(i = bc; i <= ec; ptr += 3, i++) {
270 ndx = (int)*ptr; ptr++;
271 info->chars[i-bc].advance = widths[ndx];
272 /* TFM files lack this information */
273 info->chars[i-bc].left = 0;
274 info->chars[i-bc].right = widths[ndx];
275 info->chars[i-bc].present = (ndx != 0);
277 ndx = ((*ptr >> 4) & 0xf);
278 info->chars[i-bc].height = heights[ndx];
280 info->chars[i-bc].depth = depths[ndx];
284 /* free everything */
290 error(_("%s: File corrupted, or not a TFM file\n"), filename);
297 static int ofm1_load_file(FILE *in, TFMInfo *info)
299 int lf, lh, bc, ec, nw, nh, nd;
318 fuget4(in); /* italics */
319 fuget4(in); /* lig-kern */
320 fuget4(in); /* kern */
321 fuget4(in); /* extensible recipe */
322 fuget4(in); /* parameters */
323 fuget4(in); /* direction */
328 /* get the checksum */
329 info->checksum = fuget4(in);
330 /* the design size */
331 info->design = fuget4(in);
332 /* get the coding scheme */
334 /* get the coding scheme */
338 fread(info->coding, 39, 1, in);
341 strcpy(info->coding, "FontSpecific");
342 /* get the font family */
347 fread(info->family, i, 1, in);
350 strcpy(info->family, "unspecified");
354 /* jump to the beginning of the char-info table */
355 fseek(in, 4L*nco, SEEK_SET);
360 info->chars = xnalloc(TFMChar, size);
361 end = info->chars + size;
363 for(tch = info->chars, i = 0; i < ncw; i++) {
367 /* in the characters we store the actual indices */
368 ch.advance = fuget2(in);
369 ch.height = fuget1(in);
370 ch.depth = fuget1(in);
373 /* get # of repeats */
375 /* skip parameters */
376 fseek(in, (long)npc * 2, SEEK_CUR);
377 /* if npc is odd, skip padding */
378 if(npc & 1) fuget2(in);
380 /* now repeat the character */
381 while(nr-- >= 0 && tch < end)
382 memcpy(tch++, &ch, sizeof(TFMChar));
387 /* I wish we were done, but we aren't */
389 /* get the widths, heights and depths */
391 tfm = xnalloc(Int32, size);
392 /* read them in one sweep */
393 if(fread(tfm, 4, size, in) != size) {
398 /* byte-swap things if necessary */
399 #ifdef WORD_LITTLE_ENDIAN
400 swap_array((Uint32 *)tfm, size);
403 heights = widths + nw;
404 depths = heights + nh;
406 if(widths[0] || heights[0] || depths[0])
409 /* now fix the characters */
411 for(tch = info->chars; tch < end; tch++) {
412 tch->present = (tch->advance != 0);
413 tch->advance = widths[tch->advance];
414 tch->height = heights[tch->height];
415 tch->depth = depths[tch->depth];
417 tch->right = tch->advance;
429 /* we don't read OFM files into memory, because they can potentially be large */
430 static int ofm_load_file(const char *filename, TFMInfo *info)
432 int lf, lh, bc, ec, nw, nh, nd;
447 in = fopen(filename, "r");
451 /* not a checksum, but serves a similar purpose */
454 /* get the counters */
461 DEBUG((DBG_FONTS, "(mt) reading Level-1 OFM file `%s'\n",
463 /* we handle level-1 files separately */
464 if(ofm1_load_file(in, info) < 0)
469 DEBUG((DBG_FONTS, "(mt) reading Level-0 OFM file `%s'\n", filename));
471 lf = fuget4(in); checksum = nwords;
472 lh = fuget4(in); checksum += lh;
474 ec = fuget4(in); checksum += 2 * (ec - bc + 1);
475 nw = fuget4(in); checksum += nw;
476 nh = fuget4(in); checksum += nh;
477 nd = fuget4(in); checksum += nd;
478 checksum += fuget4(in); /* skip italics correction count */
479 checksum += 2*fuget4(in); /* skip lig/kern table size */
480 checksum += fuget4(in); /* skip kern table size */
481 checksum += 2*fuget4(in); /* skip extensible recipe count */
482 checksum += fuget4(in); /* skip # of font parameters */
484 /* I have found several .ofm files that seem to have the
485 * font-direction word missing, so we try to detect that here */
486 if(checksum == lf + 1) {
487 DEBUG((DBG_FONTS, "(mt) font direction missing in `%s'\n",
492 /* skip font direction */
496 if(checksum != lf || bc > ec + 1 || ec > 65535)
499 /* now we're at the header */
501 /* get the checksum */
502 info->checksum = fuget4(in);
503 /* get the design size */
504 info->design = fuget4(in);
506 /* get the coding scheme */
508 /* get the coding scheme */
510 if(n < 0 || n > 39) {
511 warning(_("%s: font coding scheme truncated to 40 bytes\n"),
515 fread(info->coding, 39, 1, in);
518 strcpy(info->coding, "FontSpecific");
519 /* get the font family */
524 fread(info->family, i, 1, in);
527 strcpy(info->family, "unspecified");
530 /* now skip anything else in the header */
531 fseek(in, 4L*(nwords + lh), SEEK_SET);
532 /* and read everything at once */
533 size = 2*(ec - bc + 1) + nw + nh + nd;
534 tfm = xnalloc(Int32, size * sizeof(Int32));
535 if(fread(tfm, 4, size, in) != size) {
539 /* byte-swap all the tables at once */
540 #ifdef WORD_LITTLE_ENDIAN
541 swap_array((Uint32 *)tfm, size);
544 charinfo = cb; cb += 2*(ec - bc + 1);
545 widths = cb; cb += nw;
546 heights = cb; cb += nh;
549 if(widths[0] || heights[0] || depths[0]) {
554 /* from this point on, no error checking is done */
556 /* we don't need this anymore */
559 /* now we don't read from `ptr' anymore */
563 info->type = DviFontTFM;
565 /* allocate characters */
566 info->chars = xnalloc(TFMChar, size);
568 /* get the relevant data */
569 ptr = (Uchar *)charinfo;
570 for(i = bc; i <= ec; ptr += 4, i++) {
574 info->chars[i-bc].advance = widths[ndx];
575 /* TFM files lack this information */
576 info->chars[i-bc].left = 0;
577 info->chars[i-bc].right = widths[ndx];
578 info->chars[i-bc].present = (ndx != 0);
580 info->chars[i-bc].height = heights[ndx];
582 info->chars[i-bc].depth = depths[ndx];
589 error(_("%s: File corrupted, or not a TFM file\n"), filename);
594 char *lookup_font_metrics(const char *name, int *type)
599 #ifndef WITH_AFM_FILES
603 file = kpse_find_tfm(name);
607 file = kpse_find_ofm(name);
608 /* we may have gotten a TFM back */
610 const char *ext = file_extension(file);
611 if(ext && STREQ(ext, "tfm"))
616 #ifdef WITH_AFM_FILES
618 file = kpse_find_file(name, kpse_afm_format, 0);
621 file = kpse_find_file(name, kpse_afm_format, 0);
624 file = kpse_find_tfm(name);
637 * The next two functions are just wrappers for the font metric loaders,
638 * and use the pool of TFM data
641 /* this is how we interpret arguments:
642 * - if filename is NULL, we look for files of the given type,
643 * unless type is DviFontAny, in which case we try all the
645 * - if filename is not NULL, we look at `type' to decide
646 * how to read the file. If type is DviFontAny, we just
649 TFMInfo *get_font_metrics(const char *short_name, int type, const char *filename)
656 tfm = (TFMPool *)mdvi_hash_lookup(&tfmhash,
657 MDVI_KEY(short_name));
659 DEBUG((DBG_FONTS, "(mt) reusing metric file `%s' (%d links)\n",
660 short_name, tfm->links));
662 return &tfm->tfminfo;
666 file = filename ? (char *)filename : lookup_font_metrics(short_name, &type);
670 tfm = xalloc(TFMPool);
671 DEBUG((DBG_FONTS, "(mt) loading font metric data from `%s'\n", file, file));
674 status = tfm_load_file(file, &tfm->tfminfo);
677 status = ofm_load_file(file, &tfm->tfminfo);
679 #ifdef WITH_AFM_FILES
681 status = afm_load_file(file, &tfm->tfminfo);
694 tfm->short_name = xstrdup(short_name);
696 /* add it to the pool */
697 if(tfmpool.count == 0)
698 mdvi_hash_create(&tfmhash, TFM_HASH_SIZE);
699 mdvi_hash_add(&tfmhash, MDVI_KEY(tfm->short_name),
700 tfm, MDVI_HASH_UNCHECKED);
701 listh_prepend(&tfmpool, LIST(tfm));
704 return &tfm->tfminfo;
707 void free_font_metrics(TFMInfo *info)
711 if(tfmpool.count == 0)
713 /* get the entry -- can't use the hash table for this, because
714 * we don't have the short name */
715 for(tfm = (TFMPool *)tfmpool.head; tfm; tfm = tfm->next)
716 if(info == &tfm->tfminfo)
720 if(--tfm->links > 0) {
721 DEBUG((DBG_FONTS, "(mt) %s not removed, still in use\n",
725 mdvi_hash_remove_ptr(&tfmhash, MDVI_KEY(tfm->short_name));
727 DEBUG((DBG_FONTS, "(mt) removing unused TFM data for `%s'\n", tfm->short_name));
728 listh_remove(&tfmpool, LIST(tfm));
729 xfree(tfm->short_name);
730 xfree(tfm->tfminfo.chars);
734 void flush_font_metrics(void)
738 for(; (ptr = (TFMPool *)tfmpool.head); ) {
739 tfmpool.head = LIST(ptr->next);
741 xfree(ptr->short_name);
742 xfree(ptr->tfminfo.chars);
745 mdvi_hash_reset(&tfmhash, 0);