| 1 |
/* $Id: tfm.c,v 2.0 1996/05/12 22:25:04 neumann Exp $ */ |
| 2 |
/* Read tfm widths resident font support in dvilj. Originally |
| 3 |
written by [email protected] in early 1994. Public domain. */ |
| 4 |
|
| 5 |
#ifdef KPATHSEA |
| 6 |
#include <kpathsea/config.h> |
| 7 |
#include <kpathsea/c-fopen.h> |
| 8 |
#include <kpathsea/lib.h> |
| 9 |
#include <kpathsea/tex-file.h> |
| 10 |
#include <c-auto.h> |
| 11 |
#else |
| 12 |
#include <stdio.h> |
| 13 |
#include <stdlib.h> |
| 14 |
#endif |
| 15 |
|
| 16 |
#include "config.h" /* for STRSIZE and tfm_info_type, at least */ |
| 17 |
/* and for IO-functions (since RISC OS) */ |
| 18 |
|
| 19 |
#include "tfm.h" |
| 20 |
|
| 21 |
#ifdef vms |
| 22 |
#include <ssdef.h> |
| 23 |
#include <stsdef.h> |
| 24 |
#define getenv vms_getenv |
| 25 |
#endif |
| 26 |
|
| 27 |
#define TFM_GET_TWO() NoSignExtend (tfm_fp, 2) |
| 28 |
#define TFM_GET_FOUR() NoSignExtend (tfm_fp, 4) |
| 29 |
|
| 30 |
/* Read N words (N * 4 bytes) from TFM_FP and return it in *OUTBUF, unless |
| 31 |
OUTBUF==NULL, in which case throw them away. */ |
| 32 |
|
| 33 |
static void |
| 34 |
tfm_get_n (FILEPTR tfm_fp, unsigned nwords, unsigned char **outbuf) |
| 35 |
{ |
| 36 |
unsigned n = nwords * 4; |
| 37 |
void *buf = (void *) malloc (n); |
| 38 |
|
| 39 |
if (buf == NULL) {BCLOSE(tfm_fp); Fatal("(tfm): out of memory error!\n");} |
| 40 |
read_multi (buf, 1, n, tfm_fp); |
| 41 |
if (FEOF(tfm_fp)) { |
| 42 |
BCLOSE(tfm_fp); Fatal("dvilj(tfm): Could not read %u bytes from TFM file.\n", n); |
| 43 |
exit (1); |
| 44 |
} |
| 45 |
|
| 46 |
/* If OUTBUF is null, free what we just read, else return it. */ |
| 47 |
if (outbuf) { |
| 48 |
*outbuf = buf; |
| 49 |
} else { |
| 50 |
free (buf); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
/* Read a string in BCPL format from DATA into STR, and terminate with a |
| 56 |
null byte. First byte of DATA says how many characters follow. |
| 57 |
Assume STR is long enough. */ |
| 58 |
|
| 59 |
static void |
| 60 |
get_bcpl (unsigned char *data, unsigned char *str) |
| 61 |
{ |
| 62 |
unsigned length; |
| 63 |
|
| 64 |
for (length = *(data ++); length; length --) { |
| 65 |
*(str ++) = *(data ++); |
| 66 |
} |
| 67 |
*str = 0; |
| 68 |
} |
| 69 |
|
| 70 |
/* Header word 18: |
| 71 |
2 bytes: "KN" for Karl and Norm---this identifies our extensions |
| 72 |
1 byte : 1 if proportional, 0 otherwise |
| 73 |
1 byte : reserved (to extend the style, if necessary) |
| 74 |
Header word 19: |
| 75 |
2 bytes: PCL style selection number |
| 76 |
1 byte : reserved (to extend weight, if necessary) |
| 77 |
1 byte : weight (signed, 2's complement, valid -7 to +7) |
| 78 |
Header word 20: |
| 79 |
2 bytes: reserved (to extend typeface id, if necessary) |
| 80 |
2 bytes: PCL typeface selection number |
| 81 |
|
| 82 |
The first (BigEndian) byte of header word #18 is DATA[0]. |
| 83 |
Assume DATA is long enough for everything we might try to read. */ |
| 84 |
|
| 85 |
static bool |
| 86 |
get_pcl_info (unsigned char *data, unsigned *spacing, unsigned *style, |
| 87 |
int *weight, unsigned *typeface_id) |
| 88 |
{ |
| 89 |
/* No magic number for our extensions => forget it. */ |
| 90 |
if (data[0] != 'K' && data[1] != 'N') |
| 91 |
return _FALSE; |
| 92 |
|
| 93 |
*spacing = data[(0* 4) + 2]; /* Third byte of first word. */ |
| 94 |
|
| 95 |
/* First two bytes of second word. */ |
| 96 |
*style = (data[(1 * 4)] << 8) + data[(1 * 4) + 1]; |
| 97 |
|
| 98 |
/* Last byte of second word, signed two-complement. */ |
| 99 |
*weight = data[(1 * 4) + 3]; |
| 100 |
if (*weight >= 128) *weight -= 256; |
| 101 |
|
| 102 |
/* Effectively all four bytes of third word. */ |
| 103 |
*typeface_id = (data[(2 * 4) + 0] << 24) + (data[(2 * 4) + 1] << 16) |
| 104 |
+ (data[(2 * 4) + 2] << 8) + (data[(2 * 4) + 3]); |
| 105 |
|
| 106 |
return _TRUE; |
| 107 |
} |
| 108 |
|
| 109 |
/* If the TFM file NAME exists, set the elements of RET and return true. |
| 110 |
Otherwise, return false. */ |
| 111 |
|
| 112 |
bool |
| 113 |
tfm_read_info (char *name, tfm_info_type *ret) |
| 114 |
{ |
| 115 |
/* Don't use a structure for this, since then it might occupy more |
| 116 |
than the exactly four bytes that we need. */ |
| 117 |
unsigned char *char_info; |
| 118 |
FILEPTR tfm_fp; |
| 119 |
unsigned char *header_data; |
| 120 |
unsigned char *width_raw; /* array of 1-byte data */ |
| 121 |
unsigned long4 *width_table; /* array of 4-byte fixes */ |
| 122 |
unsigned i; |
| 123 |
unsigned lh, bc, ec, nw, nh, nd, ni, nl, nk, ne, np; |
| 124 |
#ifdef KPATHSEA |
| 125 |
char *full_name = kpse_find_tfm (name); |
| 126 |
|
| 127 |
if (full_name == NULL) { |
| 128 |
return _FALSE; |
| 129 |
} |
| 130 |
tfm_fp = xfopen (full_name, FOPEN_RBIN_MODE); |
| 131 |
#else /* not KPATHSEA */ |
| 132 |
char full_name[STRSIZE]; |
| 133 |
if (findfile(TFMpath, name, 0, full_name, _TRUE, 0)) { |
| 134 |
|
| 135 |
/* fprintf(ERR_STREAM,"full_name=<%s>\n", full_name);*/ |
| 136 |
tfm_fp = BINOPEN(full_name); |
| 137 |
if (tfm_fp == FPNULL) { |
| 138 |
/* this can happen, if the calculation for max number of open |
| 139 |
* files has to be corrected |
| 140 |
*/ |
| 141 |
fprintf(ERR_STREAM,"Error: file <%s> could not be opened\n", full_name); |
| 142 |
return _FALSE; |
| 143 |
} |
| 144 |
} else { |
| 145 |
Warning("tfm file %s.tfm not found on path <%s>\n", name, TFMpath); |
| 146 |
return _FALSE; |
| 147 |
} |
| 148 |
#endif /* not KPATHSEA */ |
| 149 |
|
| 150 |
(void) TFM_GET_TWO (); /* word length of file */ |
| 151 |
lh = TFM_GET_TWO (); /* words of header data */ |
| 152 |
bc = TFM_GET_TWO (); /* smallest character code */ |
| 153 |
ec = TFM_GET_TWO (); /* largest character code */ |
| 154 |
nw = TFM_GET_TWO (); /* words in width table */ |
| 155 |
nh = TFM_GET_TWO (); /* words in height table */ |
| 156 |
nd = TFM_GET_TWO (); /* words in depth table */ |
| 157 |
ni = TFM_GET_TWO (); /* words in italic correction table */ |
| 158 |
nl = TFM_GET_TWO (); /* words in lig/kern table */ |
| 159 |
nk = TFM_GET_TWO (); /* words in kern table */ |
| 160 |
ne = TFM_GET_TWO (); /* words in extensible char table */ |
| 161 |
np = TFM_GET_TWO (); /* words of font parameter data */ |
| 162 |
|
| 163 |
tfm_get_n (tfm_fp, lh, &header_data); |
| 164 |
/* Only two headerbyte words are required by the TFM format, so don't |
| 165 |
insist on all this extra stuff. */ |
| 166 |
if (lh > 2) { |
| 167 |
get_bcpl (header_data + (2 * 4), ret->coding_scheme); |
| 168 |
} else { |
| 169 |
ret->coding_scheme[0] = 0; |
| 170 |
} |
| 171 |
|
| 172 |
if (lh > 12) { |
| 173 |
get_bcpl (header_data + (12 * 4), ret->family); |
| 174 |
} else { |
| 175 |
ret->family[0] = 0; |
| 176 |
} |
| 177 |
|
| 178 |
/* Sorry for the convoluted logic. The idea is that if the family |
| 179 |
is HPAUTOTFM, we better have our extensions -- so if we don't |
| 180 |
have enough header words, or if we don't find what we need in |
| 181 |
the header words, something's seriously wrong, and we shouldn't |
| 182 |
claim to have succeeded at reading a good TFM file. */ |
| 183 |
|
| 184 |
if (strcmp ((char *)ret->family, "HPAUTOTFM") == 0 |
| 185 |
&& (lh < 20 |
| 186 |
|| !get_pcl_info (&(header_data[18 * 4]), |
| 187 |
&ret->spacing, &ret->style, &ret->weight, |
| 188 |
&ret->typeface_id))) { |
| 189 |
BCLOSE (tfm_fp); |
| 190 |
return _FALSE; |
| 191 |
} |
| 192 |
|
| 193 |
/* Initialize our returned array of widths to zero, since the TFM file |
| 194 |
need not contain info for all character codes. */ |
| 195 |
for (i = 0; i < bc; i++) { |
| 196 |
ret->widths[i] = 0; |
| 197 |
} |
| 198 |
for (i = ec + 1; i < 256; i++) { |
| 199 |
ret->widths[i] = 0; |
| 200 |
} |
| 201 |
|
| 202 |
/* The char_info is one word (four bytes) for each character in the font. */ |
| 203 |
tfm_get_n (tfm_fp, ec - bc + 1, &char_info); |
| 204 |
|
| 205 |
/* The width table is just nw words. */ |
| 206 |
tfm_get_n (tfm_fp, nw, &width_raw); |
| 207 |
width_table = (unsigned long4 *) malloc (nw * 4); |
| 208 |
if (width_table == NULL) {BCLOSE(tfm_fp); Fatal("dvilj(tfm): out of memory!\n");} |
| 209 |
|
| 210 |
/* But the width table contains four-byte numbers, so have to convert |
| 211 |
from BigEndian to host order. */ |
| 212 |
for (i = 0; i < nw; i++) { |
| 213 |
unsigned byte_offset = i * 4; |
| 214 |
unsigned b1 = width_raw[byte_offset]; |
| 215 |
unsigned b2 = width_raw[byte_offset + 1]; |
| 216 |
unsigned b3 = width_raw[byte_offset + 2]; |
| 217 |
unsigned b4 = width_raw[byte_offset + 3]; |
| 218 |
width_table[i] = (b1 << 24) + (b2 << 16) + (b3 << 8) + b4; |
| 219 |
} |
| 220 |
|
| 221 |
/* For each character, retrieve and store the TFM width. */ |
| 222 |
for (i = bc; i <= ec; i++) { |
| 223 |
unsigned char w_i = char_info[(i - bc) * 4]; |
| 224 |
ret->widths[i] = width_table[w_i]; |
| 225 |
} |
| 226 |
|
| 227 |
/* Throw away everything up to the second font parameter. (Could just |
| 228 |
seek, but I don't want to pull in the include files, etc.) */ |
| 229 |
if (np >= 2) { |
| 230 |
tfm_get_n (tfm_fp, nh + nd + ni + nl + nk + ne + 1, NULL); |
| 231 |
ret->interword = TFM_GET_FOUR (); |
| 232 |
} else { |
| 233 |
ret->interword = 0; |
| 234 |
} |
| 235 |
|
| 236 |
free (header_data); |
| 237 |
free (char_info); |
| 238 |
free (width_raw); |
| 239 |
free (width_table); |
| 240 |
|
| 241 |
BCLOSE (tfm_fp); |
| 242 |
return _TRUE; |
| 243 |
} |