| 1 |
#!/usr/bin/perl -w |
| 2 |
# |
| 3 |
# This program maps full PS font names to abreviated TeX style names |
| 4 |
# and puts font files or links to font files in a file hierarchy |
| 5 |
# suitable for texk. |
| 6 |
# |
| 7 |
# Mapping example: Times-Roman.{gsf,pfa,pfb} becomes ptmr.{pfa,pfb} |
| 8 |
# |
| 9 |
|
| 10 |
use File::Path; |
| 11 |
|
| 12 |
# These are the fontname map files to read |
| 13 |
|
| 14 |
# This is for the fontname distribution in teTeX 0.4: |
| 15 |
# @fonts=qw(adobe apple bitstrea dtc linotype monotype skey1250 skey1555 |
| 16 |
# softkey urw); |
| 17 |
|
| 18 |
# This is for fontname 2.2, get it from ftp.tug.org:/tex/fontname-2.2 |
| 19 |
# The linotype map in teTeX 1.0 is terebly corrupted. Removed from the list. |
| 20 |
|
| 21 |
@fonts=qw(adobe apple bitstrea dtc monotype softkey-1250 |
| 22 |
softkey-1555 urw wolfram yandy); |
| 23 |
|
| 24 |
$logname="t1mapper.log"; |
| 25 |
|
| 26 |
# How much to read from font files to figure out the font name |
| 27 |
$fontdatalen=1024*10; |
| 28 |
|
| 29 |
# use $TMPDIR if set by user |
| 30 |
if (defined($ENV{TMPDIR})) { |
| 31 |
$tempdir=$ENV{TMPDIR}; |
| 32 |
} |
| 33 |
else { |
| 34 |
$tempdir="/tmp"; |
| 35 |
} |
| 36 |
|
| 37 |
# HERE BE DRAGONS |
| 38 |
|
| 39 |
usage() if $#ARGV==0; |
| 40 |
|
| 41 |
# By default copy files |
| 42 |
$method=1; |
| 43 |
|
| 44 |
while ($_=shift(@ARGV)) { |
| 45 |
/^-cp$/ && do { $method=1; next; }; |
| 46 |
/^-ln$/ && do { $method=2; next; }; |
| 47 |
/^-lns$/ && do { $method=3; next; }; |
| 48 |
/^-lnlns$/ && do { $method=4; next; }; |
| 49 |
/^-lncp$/ && do { $method=5; next; }; |
| 50 |
/^-gs$/ && do { |
| 51 |
$gsmap = shift(@ARGV) or usage(); |
| 52 |
$gsdir = shift(@ARGV) or usage(); |
| 53 |
next; |
| 54 |
}; |
| 55 |
unshift(@ARGV,$_); |
| 56 |
last; |
| 57 |
} |
| 58 |
|
| 59 |
$texdir = shift(@ARGV) or usage(); |
| 60 |
|
| 61 |
if (! -d $texdir) { |
| 62 |
print "Making directory $texdir\n"; |
| 63 |
mkpath($texdir) or die "Could not make directory: $texdir: $!\n"; |
| 64 |
} |
| 65 |
|
| 66 |
if (!open(LOG,">$logname")) { |
| 67 |
$logname="$tempdir/$logname"; |
| 68 |
open(LOG,">$logname") || |
| 69 |
die "Could not open file $logname for writing: $!\n"; |
| 70 |
} |
| 71 |
|
| 72 |
# What major mode of operation? |
| 73 |
|
| 74 |
# Establish fonts based on ghostscript map? |
| 75 |
if ($gsmap) { |
| 76 |
process_gsfonts(); |
| 77 |
print "Log in $logname\n"; |
| 78 |
exit 0; |
| 79 |
} |
| 80 |
|
| 81 |
# Ok, loop examine fonts to figure out the font name and copy/link to $texdir |
| 82 |
|
| 83 |
process_fontname_maps(); |
| 84 |
process_fonts(); |
| 85 |
print "Log in $logname\n"; |
| 86 |
|
| 87 |
exit 0; |
| 88 |
|
| 89 |
# ############################### MOVE_FONT ################################ |
| 90 |
|
| 91 |
sub move_font { |
| 92 |
# Move font to destination directory, according to specified $method |
| 93 |
my($from)=shift; |
| 94 |
my($to)=shift; |
| 95 |
my($relok)=shift; |
| 96 |
|
| 97 |
my($other); # Same font, other format |
| 98 |
my($type); # Other type |
| 99 |
|
| 100 |
die "I need absolute paths please\n" |
| 101 |
if !$relok && substr($from,0,1) ne '/'; |
| 102 |
|
| 103 |
logmsg("$from -> $to\n"); |
| 104 |
|
| 105 |
# Remove the old font to replace with this |
| 106 |
unlink($to) if -f $to; |
| 107 |
|
| 108 |
# Also remove the old font in the other format: |
| 109 |
$other=$to; |
| 110 |
$type='.pfb' if substr($to,-4) eq '.pfa'; |
| 111 |
$type='.pfa' if !$type && substr($to,-4) eq '.pfb'; |
| 112 |
substr($other,-4)=$type; |
| 113 |
|
| 114 |
unlink($other) if -f $other; |
| 115 |
|
| 116 |
if ($method==1) { |
| 117 |
# cp |
| 118 |
system "cp $from $to 2>/dev/null" and # Shell exit value |
| 119 |
warn "Copying $from to $to failed\n"; |
| 120 |
} elsif ($method==2) { |
| 121 |
# ln |
| 122 |
link($from,$to) or |
| 123 |
die "Linking of $to to $from failed: $!\n"; |
| 124 |
} elsif ($method==3) { |
| 125 |
# lns |
| 126 |
symlink($from,$to) or |
| 127 |
die "Symlinking $to to $from failed: $!\n"; |
| 128 |
} elsif ($method==4) { |
| 129 |
# lnlns |
| 130 |
link($from,$to) || |
| 131 |
symlink($from,$to) || |
| 132 |
die "Both linking and symlinking $to to $from failed: $!\n"; |
| 133 |
} elsif ($method==5) { |
| 134 |
# lncp. Let cp give the error-message |
| 135 |
link($from,$to) || system "cp $from $to"; |
| 136 |
} else { |
| 137 |
die "Internal error: Unknown font copy/link method\n"; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
# ############################# PROCESS_FONTS ############################## |
| 142 |
|
| 143 |
sub process_fonts { |
| 144 |
# Loop over ARGV, examine each (font?) file to see if it is a |
| 145 |
# type1 font, binary or text format, and figure out it's name |
| 146 |
my($type,$font); |
| 147 |
|
| 148 |
foreach $filename (@ARGV) { |
| 149 |
($type,$font)=examine_font($filename); |
| 150 |
|
| 151 |
next if !defined($font); |
| 152 |
|
| 153 |
if (!exists($file{$font})) { |
| 154 |
logmsg("TeX name of $font ($filename) unknown\n"); |
| 155 |
next; |
| 156 |
} |
| 157 |
|
| 158 |
$file=$file{$font}; |
| 159 |
move_font($filename,"$texdir/$file$type",0); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
# ############################## EXAMINE_FONT ############################## |
| 164 |
|
| 165 |
sub examine_font { |
| 166 |
# Examine font to determine name and type |
| 167 |
# |
| 168 |
# The method for finding the font name is based on an awk script |
| 169 |
# written years ago by Kjetil T. Homme and me (mostly him). |
| 170 |
# |
| 171 |
my($filename)=shift; |
| 172 |
my($type); |
| 173 |
|
| 174 |
my($size)=-s $filename; |
| 175 |
|
| 176 |
if ($size==0) { |
| 177 |
logmsg("Zero size file skipped: $filename\n"); |
| 178 |
return (undef,undef); |
| 179 |
} |
| 180 |
|
| 181 |
if (!open(FONT,"< $filename")) { |
| 182 |
logmsg("Unable to open $filename for reading: $!\n"); |
| 183 |
return (undef,undef); |
| 184 |
} |
| 185 |
|
| 186 |
sysread FONT, $fontdata, $fontdatalen, 0; |
| 187 |
|
| 188 |
# Determine type based on magic number |
| 189 |
$type='.pfa' if substr($fontdata,0,16) eq '%!PS-AdobeFont-1'; |
| 190 |
$type='.pfa' if !$type && substr($fontdata,0,13) eq '%!FontType1-1'; |
| 191 |
$type='.pfb' if !$type && substr($fontdata,6,16) eq '%!PS-AdobeFont-1'; |
| 192 |
|
| 193 |
if (!$type) { |
| 194 |
# This is where we end up if the file does not match the magic |
| 195 |
# numbers. The file is most probably not a type1 file. |
| 196 |
|
| 197 |
if ((index($fontdata,'%!PS-AdobeFont-1')==-1) && |
| 198 |
(index($fontdata,'%!FontType1-1')==-1)) { |
| 199 |
# If the string '%!PS-AdobeFont-1.0' or '%!FontType1-1.0' |
| 200 |
# does not appear at all this is NOT a type1 font of any |
| 201 |
# kind. |
| 202 |
logmsg("$filename is not a type1 file\n"); |
| 203 |
return (undef,undef); |
| 204 |
} |
| 205 |
|
| 206 |
logmsg("Odd magic in $filename\n"); |
| 207 |
|
| 208 |
# Try to determine type based on filename extention |
| 209 |
$type='.pfa' if substr($filename,-4) eq '.pfa'; |
| 210 |
$type='.pfb' if !$type && substr($filename,-4) eq '.pfb'; |
| 211 |
|
| 212 |
if (!$type) { |
| 213 |
# No extention? Fall back to perls -T (text) test operator. |
| 214 |
$type='.pfb'; |
| 215 |
$type='.pfa' if -T $filename; |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
# The font type has now been determined. I wonder what the font's |
| 220 |
# name is. |
| 221 |
|
| 222 |
# What we look for is lines like this: |
| 223 |
# |
| 224 |
# %!PS-AdobeFont-1.0: URWGothicL-Demi 001.005 |
| 225 |
# |
| 226 |
# or this: |
| 227 |
# |
| 228 |
# /FontName /URWGothicL-Demi def |
| 229 |
|
| 230 |
$fontname=''; |
| 231 |
|
| 232 |
if ($fontdata =~ m~/FontName /([\S]+) def~) { |
| 233 |
# The /FontName line is the most reliable source |
| 234 |
$fontname = $1; |
| 235 |
} elsif ($fontdata =~ m/%\!PS-AdobeFont-1.0: ([\S]+)/) { |
| 236 |
# The magic line is not always fully formed and can only be |
| 237 |
# used as fallback --- in case the /FontName entry is not in |
| 238 |
# the file. True, this would be a rather dubious font. |
| 239 |
$fontname = $1; |
| 240 |
} elsif ($fontdata =~ m/%\!FontType1-1.0: ([\S]+)/) { |
| 241 |
# The magic line is not always fully (or correctly) formed and |
| 242 |
# can only be used as fallback --- in case the /FontName entry |
| 243 |
# is not in the file. True, this would be a rather dubious |
| 244 |
# font (actually, a lot of type1 versions of tex fonts fall |
| 245 |
# into this category). |
| 246 |
$fontname = $1; |
| 247 |
} |
| 248 |
|
| 249 |
return ($type,$fontname); |
| 250 |
} |
| 251 |
|
| 252 |
# ######################### PROCESS_FONTNAME_MAPS ########################## |
| 253 |
|
| 254 |
sub process_fontname_maps { |
| 255 |
# Read the fontname maps |
| 256 |
|
| 257 |
while ($foundry=shift(@fonts)) { |
| 258 |
$map=findfile($foundry.".map"); |
| 259 |
next unless $map; |
| 260 |
print "Reading (fontname/$foundry) $map\n"; |
| 261 |
open(FILE,"<$map") || die "Could not open $map: $!\n"; |
| 262 |
while (<FILE>) { |
| 263 |
chomp; |
| 264 |
s/@.*//; |
| 265 |
next if $_ eq ''; |
| 266 |
($file,$font,undef)=split(/\s+/,$_,3); |
| 267 |
# Remove known encoding codes: |
| 268 |
$file =~ s/(8a|8r|8t)$//; |
| 269 |
# 8a = StandardEncoding 8a.enc |
| 270 |
# 8r = TeXBase1Encoding 8r.enc |
| 271 |
# 8t = CorkEncoding cork.enc |
| 272 |
# Any filenames with numbers left are 'unknown encodings' |
| 273 |
if ($file =~ /\d/) { |
| 274 |
# print "Unknown encoding: $file - $font\n"; |
| 275 |
next; |
| 276 |
} |
| 277 |
$file{$font}=$file; |
| 278 |
} |
| 279 |
close(FILE); |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
# ########################### PROCESS_GSFONTS ############################# |
| 284 |
|
| 285 |
sub read_gsmap { |
| 286 |
# Read gs map file to obtain Font-Name -> filename mappings, and |
| 287 |
# aliases so we can use GS' Times-Roman replacement as Times-Roman |
| 288 |
# ourselves. Not everyone has oodles of type1 fonts on disk. |
| 289 |
|
| 290 |
my($gsmap)=shift; |
| 291 |
|
| 292 |
open(FILE,"<$gsmap") or die "Could not open $gsmap: $!\n"; |
| 293 |
|
| 294 |
print "Reading (gsmap) $gsmap\n"; |
| 295 |
|
| 296 |
%gsalias=(); |
| 297 |
%gsfile=(); |
| 298 |
|
| 299 |
while (<FILE>) { |
| 300 |
chomp; |
| 301 |
s/%.*//; |
| 302 |
s/\s+$//; |
| 303 |
next if $_ eq ''; |
| 304 |
|
| 305 |
($font,$file,undef)=split(/\s+/,$_,3); |
| 306 |
|
| 307 |
$font =~ s~^/~~; |
| 308 |
|
| 309 |
if (!exists($file{$font})) { |
| 310 |
logmsg("TeX name of $font unknown\n"); |
| 311 |
next; |
| 312 |
} |
| 313 |
|
| 314 |
if ($file =~ m~\((.*)\)~) { |
| 315 |
$file = $1; |
| 316 |
$gsfile{$font}=$file; |
| 317 |
} else { |
| 318 |
$file =~ s~^/~~; |
| 319 |
# The aliases(sp?) cannot be resolved until the whole file is |
| 320 |
# read |
| 321 |
$gsalias{$font}=$file; |
| 322 |
} |
| 323 |
|
| 324 |
} |
| 325 |
|
| 326 |
close(FILE); |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
sub process_gsfonts { |
| 331 |
# Do the Work based on a ghostscript map file. |
| 332 |
|
| 333 |
process_fontname_maps(); |
| 334 |
read_gsmap($gsmap); |
| 335 |
|
| 336 |
chdir($gsdir) or die "Could not chdir to $gsdir: $!\n"; |
| 337 |
|
| 338 |
print "Moving fonts\n"; |
| 339 |
|
| 340 |
foreach $font (keys %gsfile) { |
| 341 |
$gsfile=$gsfile{$font}; |
| 342 |
# GSF files are not propper t1 fonts I understand, so ignore them |
| 343 |
next if $gsfile =~ /\.gsf$/; |
| 344 |
$file=$file{$font}; |
| 345 |
$type=''; |
| 346 |
|
| 347 |
if (-f $gsfile) { |
| 348 |
|
| 349 |
# Crude font type guessing. First based on existing extention: |
| 350 |
$type='pfa' if substr($gsfile,-4) eq '.pfa'; |
| 351 |
$type='pfb' if !$type && substr($gsfile,-4) eq '.pfb'; |
| 352 |
|
| 353 |
# If existing extention fails use perls -T (text) test |
| 354 |
if (!$type) { |
| 355 |
# print "Fallback typing: $gsfile\n"; |
| 356 |
$type='pfb'; |
| 357 |
$type='pfa' if -T $gsfile; |
| 358 |
} |
| 359 |
|
| 360 |
# Use for alias resolving |
| 361 |
$texname{$font}="$file.$type"; |
| 362 |
|
| 363 |
if (substr($gsfile,0,1) eq '/') { |
| 364 |
move_font("$gsfile","$texdir/".$texname{$font},0); |
| 365 |
} else { |
| 366 |
move_font("$gsdir/$gsfile","$texdir/".$texname{$font},0); |
| 367 |
} |
| 368 |
|
| 369 |
} else { |
| 370 |
logmsg("$file not found\n"); |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
chdir($texdir) or die "Could not chdir to $texdir\n"; |
| 375 |
|
| 376 |
foreach $font (keys %gsalias) { |
| 377 |
$alias = $gsalias{$font}; |
| 378 |
$falias = $texname{$alias}; |
| 379 |
next unless $falias; |
| 380 |
|
| 381 |
$type='pfa' if substr($gsfile,-4) eq '.pfa'; |
| 382 |
$type='pfb' if !$type && substr($gsfile,-4) eq '.pfb'; |
| 383 |
|
| 384 |
$file=$file{$font}; |
| 385 |
|
| 386 |
# Symlink the aliases |
| 387 |
logmsg("$falias -> $file.$type\n"); |
| 388 |
$method=3; |
| 389 |
move_font($falias,"$file.$type",1); |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
# ############################ JUNK PROCEDURES ########################### |
| 395 |
|
| 396 |
sub findfile { |
| 397 |
# Find file, use kpsewhich to access texmf database and search rules |
| 398 |
|
| 399 |
my($file)=shift; |
| 400 |
$fileloc=`kpsewhich $file`; |
| 401 |
|
| 402 |
# Check errors |
| 403 |
return undef if ($? != 0); |
| 404 |
chomp($fileloc); |
| 405 |
return undef if $fileloc eq ''; |
| 406 |
|
| 407 |
# It worked |
| 408 |
return $fileloc; |
| 409 |
} |
| 410 |
|
| 411 |
sub usage { |
| 412 |
# Abuse the user |
| 413 |
print |
| 414 |
"Usage: t1mapper [-cp|-ln|-lns|-lnlns|-lncp] \\ |
| 415 |
-gs /path/to/gs/Fontmap /path/to/gs/fonts \\ |
| 416 |
/path/to/target/tex/directory |
| 417 |
|
| 418 |
t1mapper [-cp|-ln|-lns|-lnlns|-lncp] \\ |
| 419 |
/path/to/target/tex/directory \\ |
| 420 |
<font-file-list> |
| 421 |
|
| 422 |
See t1mapper(1) for more documentation |
| 423 |
|
| 424 |
"; |
| 425 |
exit 1; |
| 426 |
} |
| 427 |
|
| 428 |
sub logmsg { |
| 429 |
my($string)=shift; |
| 430 |
|
| 431 |
print STDERR "$string"; |
| 432 |
print LOG "$string"; |
| 433 |
} |