| 1 |
% Copyright 2008-2009 Taco Hoekwater. |
| 2 |
% |
| 3 |
% This program is free software: you can redistribute it and/or modify |
| 4 |
% it under the terms of the GNU Lesser General Public License as published by |
| 5 |
% the Free Software Foundation, either version 3 of the License, or |
| 6 |
% (at your option) any later version. |
| 7 |
% |
| 8 |
% This program is distributed in the hope that it will be useful, |
| 9 |
% but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
% GNU Lesser General Public License for more details. |
| 12 |
% |
| 13 |
% You should have received a copy of the GNU Lesser General Public License |
| 14 |
% along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
% |
| 16 |
% TeX is a trademark of the American Mathematical Society. |
| 17 |
% METAFONT is a trademark of Addison-Wesley Publishing Company. |
| 18 |
% PostScript is a trademark of Adobe Systems Incorporated. |
| 19 |
|
| 20 |
% Here is TeX material that gets inserted after \input webmac |
| 21 |
|
| 22 |
\font\tenlogo=logo10 % font used for the METAFONT logo |
| 23 |
\font\logos=logosl10 |
| 24 |
\def\MF{{\tenlogo META}\-{\tenlogo FONT}} |
| 25 |
\def\MP{{\tenlogo META}\-{\tenlogo POST}} |
| 26 |
|
| 27 |
\def\title{Reading TEX metrics files} |
| 28 |
\pdfoutput=1 |
| 29 |
|
| 30 |
@ Introduction. |
| 31 |
|
| 32 |
@ Needed headers and macros |
| 33 |
|
| 34 |
@d qi(A) (quarterword)(A) /* to store eight bits in a quarterword */ |
| 35 |
@d null_font 0 /* the |font_number| for an empty font */ |
| 36 |
@d False 0 |
| 37 |
@d True 1 |
| 38 |
@d hlp1(A) mp->help_line[0]=A; } |
| 39 |
@d hlp2(A,B) mp->help_line[1]=A; hlp1(B) |
| 40 |
@d hlp3(A,B,C) mp->help_line[2]=A; hlp2(B,C) |
| 41 |
@d help3 { mp->help_ptr=3; hlp3 /* use this with three help lines */ |
| 42 |
|
| 43 |
@c |
| 44 |
#include "mpconfig.h" |
| 45 |
#include <w2c/config.h> |
| 46 |
#include <stdio.h> |
| 47 |
#include <stdlib.h> |
| 48 |
#include <string.h> |
| 49 |
#include "mplib.h" |
| 50 |
#include "mpmp.h" /* internal header */ |
| 51 |
#include "mpmath.h" /* internal header */ |
| 52 |
#include "mpstrings.h" /* internal header */ |
| 53 |
@<Declarations@> |
| 54 |
@h |
| 55 |
|
| 56 |
@ The |font_ps_name| for a built-in font should be what PostScript expects. |
| 57 |
A preliminary name is obtained here from the \.{TFM} name as given in the |
| 58 |
|fname| argument. This gets updated later from an external table if necessary. |
| 59 |
|
| 60 |
@<Declarations@>= |
| 61 |
font_number mp_read_font_info (MP mp, char *fname); |
| 62 |
|
| 63 |
@ @c |
| 64 |
font_number mp_read_font_info (MP mp, char *fname) { |
| 65 |
boolean file_opened; /* has |tfm_infile| been opened? */ |
| 66 |
font_number n; /* the number to return */ |
| 67 |
halfword lf,tfm_lh,bc,ec,nw,nh,nd; /* subfile size parameters */ |
| 68 |
size_t whd_size; /* words needed for heights, widths, and depths */ |
| 69 |
int i,ii; /* |font_info| indices */ |
| 70 |
int jj; /* counts bytes to be ignored */ |
| 71 |
int z; /* used to compute the design size */ |
| 72 |
int d; /* height, width, or depth as a fraction of design size times $2^{-8}$ */ |
| 73 |
int h_and_d; /* height and depth indices being unpacked */ |
| 74 |
int tfbyte = 0; /* a byte read from the file */ |
| 75 |
n=null_font; |
| 76 |
@<Open |tfm_infile| for input@>; |
| 77 |
@<Read data from |tfm_infile|; if there is no room, say so and |goto done|; |
| 78 |
otherwise |goto bad_tfm| or |goto done| as appropriate@>; |
| 79 |
BAD_TFM: |
| 80 |
@<Complain that the \.{TFM} file is bad@>; |
| 81 |
DONE: |
| 82 |
if ( file_opened ) (mp->close_file)(mp,mp->tfm_infile); |
| 83 |
if ( n!=null_font ) { |
| 84 |
mp->font_ps_name[n]=mp_xstrdup(mp,fname); |
| 85 |
mp->font_name[n]=mp_xstrdup(mp,fname); |
| 86 |
} |
| 87 |
return n; |
| 88 |
} |
| 89 |
|
| 90 |
@ \MP\ doesn't bother to check the entire \.{TFM} file for errors or explain |
| 91 |
precisely what is wrong if it does find a problem. Programs called \.{TFtoPL} |
| 92 |
@.TFtoPL@> @.PLtoTF@> |
| 93 |
and \.{PLtoTF} can be used to debug \.{TFM} files. |
| 94 |
|
| 95 |
@<Complain that the \.{TFM} file is bad@>= |
| 96 |
{ |
| 97 |
char msg[256]; |
| 98 |
const char *hlp[] = { |
| 99 |
"I wasn't able to read the size data for this font so this", |
| 100 |
"`infont' operation won't produce anything. If the font name", |
| 101 |
"is right, you might ask an expert to make a TFM file", |
| 102 |
NULL }; |
| 103 |
if ( file_opened ) |
| 104 |
hlp[2]="is right, try asking an expert to fix the TFM file"; |
| 105 |
mp_snprintf(msg, 256, "Font %s not usable: TFM file %s", fname, |
| 106 |
( file_opened ? "is bad" : "not found")); |
| 107 |
mp_error(mp, msg, hlp, True); |
| 108 |
} |
| 109 |
|
| 110 |
@ @<Read data from |tfm_infile|; if there is no room, say so...@>= |
| 111 |
@<Read the \.{TFM} size fields@> |
| 112 |
@<Use the size fields to allocate space in |font_info|@> |
| 113 |
@<Read the \.{TFM} header@> |
| 114 |
@<Read the character data and the width, height, and depth tables and |
| 115 |
|goto done|@> |
| 116 |
|
| 117 |
@ A bad \.{TFM} file can be shorter than it claims to be. The code given here |
| 118 |
might try to read past the end of the file if this happens. Changes will be |
| 119 |
needed if it causes a system error to refer to |tfm_infile^| or call |
| 120 |
|get_tfm_infile| when |eof(tfm_infile)| is true. For example, the definition |
| 121 |
@^system dependencies@> |
| 122 |
of |tfget| could be changed to |
| 123 |
``|begin get(tfm_infile); if eof(tfm_infile) then goto bad_tfm; end|.'' |
| 124 |
|
| 125 |
@d tfget do { |
| 126 |
size_t wanted=1; |
| 127 |
unsigned char abyte=0; |
| 128 |
void *tfbyte_ptr = &abyte; |
| 129 |
(mp->read_binary_file)(mp,mp->tfm_infile, &tfbyte_ptr,&wanted); |
| 130 |
if (wanted==0) goto BAD_TFM; |
| 131 |
tfbyte = (int)abyte; |
| 132 |
} while (0) |
| 133 |
@d read_two(A) { (A)=tfbyte; |
| 134 |
if ( (A)>127 ) goto BAD_TFM; |
| 135 |
tfget; (A)=(A)*0400+tfbyte; |
| 136 |
} |
| 137 |
@d tf_ignore(A) { for (jj=(A);jj>=1;jj--) tfget; } |
| 138 |
|
| 139 |
@<Read the \.{TFM} size fields@>= |
| 140 |
tfget; read_two(lf); |
| 141 |
tfget; read_two(tfm_lh); |
| 142 |
tfget; read_two(bc); |
| 143 |
tfget; read_two(ec); |
| 144 |
if ( (bc>1+ec)||(ec>255) ) goto BAD_TFM; |
| 145 |
tfget; read_two(nw); |
| 146 |
tfget; read_two(nh); |
| 147 |
tfget; read_two(nd); |
| 148 |
whd_size=(size_t)((ec+1-bc)+nw+nh+nd); |
| 149 |
if ( lf<(int)(6+(size_t)tfm_lh+whd_size) ) goto BAD_TFM; |
| 150 |
tf_ignore(10) |
| 151 |
|
| 152 |
@ Offsets are added to |char_base[n]| and |width_base[n]| so that is not |
| 153 |
necessary to apply the |so| and |qo| macros when looking up the width of a |
| 154 |
character in the string pool. In order to ensure nonnegative |char_base| |
| 155 |
values when |bc>0|, it may be necessary to reserve a few unused |font_info| |
| 156 |
elements. |
| 157 |
|
| 158 |
@<Use the size fields to allocate space in |font_info|@>= |
| 159 |
if ( mp->next_fmem<(size_t)bc) |
| 160 |
mp->next_fmem=(size_t)bc; /* ensure nonnegative |char_base| */ |
| 161 |
if (mp->last_fnum==mp->font_max) |
| 162 |
mp_reallocate_fonts(mp,(mp->font_max+(mp->font_max/4))); |
| 163 |
while (mp->next_fmem+whd_size>=mp->font_mem_size) { |
| 164 |
size_t l = mp->font_mem_size+(mp->font_mem_size/4); |
| 165 |
font_data *font_info; |
| 166 |
font_info = mp_xmalloc (mp,(l+1),sizeof(font_data)); |
| 167 |
memset (font_info,0,sizeof(font_data)*(l+1)); |
| 168 |
memcpy (font_info,mp->font_info,sizeof(font_data)*(mp->font_mem_size+1)); |
| 169 |
mp_xfree(mp->font_info); |
| 170 |
mp->font_info = font_info; |
| 171 |
mp->font_mem_size = l; |
| 172 |
} |
| 173 |
mp->last_fnum++; |
| 174 |
n=mp->last_fnum; |
| 175 |
mp->font_bc[n]=(eight_bits)bc; |
| 176 |
mp->font_ec[n]=(eight_bits)ec; |
| 177 |
mp->char_base[n]=(int)(mp->next_fmem-(size_t)bc); |
| 178 |
mp->width_base[n]=(int)(mp->next_fmem+(size_t)(ec-bc)+1); |
| 179 |
mp->height_base[n]=mp->width_base[n]+nw; |
| 180 |
mp->depth_base[n]=mp->height_base[n]+nh; |
| 181 |
mp->next_fmem=mp->next_fmem+whd_size; |
| 182 |
|
| 183 |
|
| 184 |
@ This macro is a bit odd, but it works. |
| 185 |
|
| 186 |
@d integer_as_fraction(A) (int)(A) |
| 187 |
|
| 188 |
@<Read the \.{TFM} header@>= |
| 189 |
if ( tfm_lh<2 ) goto BAD_TFM; |
| 190 |
tf_ignore(4); |
| 191 |
tfget; read_two(z); |
| 192 |
tfget; z=z*0400+tfbyte; |
| 193 |
tfget; z=z*0400+tfbyte; /* now |z| is 16 times the design size */ |
| 194 |
mp->font_dsize[n]=mp_take_fraction(mp, z,integer_as_fraction(267432584)); |
| 195 |
/* times ${72\over72.27}2^{28}$ to convert from \TeX\ points */ |
| 196 |
tf_ignore(4*(tfm_lh-2)) |
| 197 |
|
| 198 |
@ @<Read the character data and the width, height, and depth tables...@>= |
| 199 |
ii=mp->width_base[n]; |
| 200 |
i=mp->char_base[n]+bc; |
| 201 |
while ( i<ii ) { |
| 202 |
tfget; mp->font_info[i].qqqq.b0=qi(tfbyte); |
| 203 |
tfget; h_and_d=tfbyte; |
| 204 |
mp->font_info[i].qqqq.b1=qi(h_and_d / 16); |
| 205 |
mp->font_info[i].qqqq.b2=qi(h_and_d % 16); |
| 206 |
tfget; tfget; |
| 207 |
i++; |
| 208 |
} |
| 209 |
while ( i<(int)mp->next_fmem ) { |
| 210 |
@<Read a four byte dimension, scale it by the design size, store it in |
| 211 |
|font_info[i]|, and increment |i|@>; |
| 212 |
} |
| 213 |
goto DONE |
| 214 |
|
| 215 |
@ The raw dimension read into |d| should have magnitude at most $2^{24}$ when |
| 216 |
interpreted as an integer, and this includes a scale factor of $2^{20}$. Thus |
| 217 |
we can multiply it by sixteen and think of it as a |fraction| that has been |
| 218 |
divided by sixteen. This cancels the extra scale factor contained in |
| 219 |
|font_dsize[n|. |
| 220 |
|
| 221 |
@<Read a four byte dimension, scale it by the design size, store it in...@>= |
| 222 |
{ |
| 223 |
tfget; d=tfbyte; |
| 224 |
if ( d>=0200 ) d=d-0400; |
| 225 |
tfget; d=d*0400+tfbyte; |
| 226 |
tfget; d=d*0400+tfbyte; |
| 227 |
tfget; d=d*0400+tfbyte; |
| 228 |
mp->font_info[i].sc=mp_take_fraction(mp, d*16,integer_as_fraction(mp->font_dsize[n])); |
| 229 |
i++; |
| 230 |
} |
| 231 |
|
| 232 |
@ This function does no longer use the file name parser, because |fname| is |
| 233 |
a C string already. |
| 234 |
|
| 235 |
@<Open |tfm_infile| for input@>= |
| 236 |
file_opened=False; |
| 237 |
mp_ptr_scan_file(mp, fname); |
| 238 |
if ( strlen(mp->cur_area)==0 ) { mp_xfree(mp->cur_area); mp->cur_area=NULL; } |
| 239 |
if ( strlen(mp->cur_ext)==0 ) { |
| 240 |
mp_xfree(mp->cur_ext); |
| 241 |
mp->cur_ext=mp_xstrdup(mp,".tfm"); |
| 242 |
} |
| 243 |
mp_pack_file_name(mp, mp->cur_name,mp->cur_area,mp->cur_ext); |
| 244 |
mp->tfm_infile = (mp->open_file)(mp, mp->name_of_file, "r",mp_filetype_metrics); |
| 245 |
if ( !mp->tfm_infile ) goto BAD_TFM; |
| 246 |
file_opened=True |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|