File: Type1Utils.pm

package info (click to toggle)
dtm 0.4
  • links: PTS
  • area: main
  • in suites: slink
  • size: 512 kB
  • ctags: 67
  • sloc: perl: 1,348; sh: 61; makefile: 41
file content (495 lines) | stat: -rw-r--r-- 13,436 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
# -*- Mode: Perl -*- 
#
# Type1Utils.pm 
# Copyright (C) 1997 Federico Di Gregorio.
#
# This module is part of the Debian Type Manager package.  
#
# Some (small?) parts of it are derived from type1inst 
# Copyright (C) 1996-1997 James Macnicol. 
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.

=head1 NAME

DTM::Type1Utils - Some small usefull functions to analyze type1 fonts 

=cut

package DTM::Type1Utils;

require 5.004;
use DTM::Utils;
use DTM::Snippet;
use strict;

require Exporter;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
$VERSION     = 0.1;
@ISA         = qw(Exporter);
@EXPORT      = qw(&build_type1snippet);
%EXPORT_TAGS = ();
@EXPORT_OK   = qw();


# The build_type1snippet method need some information like the mappings 
# for font foundries, styles, weights et al.

my @foundries = ("Adobe:adobe:Adobe",
		 "Publishers' Paradise:paradise:Publishers' Paradise",
		 "PUBLISHERS' PARADISE:paradise:Publishers' Paradise",
		 "Bigelow & Holmes:b&h:Bigelow & Holmes",
	         "Bitstream:bitstream:Bitstream",
		 "International Typeface Corporation:itc:ITC",
	         "IBM:ibm:IBM",
	         "LETRASET:letraset:LETRASET",
		 "Monotype Corporation:monotype:Monotype Corporation",
		 "SoftMaker:softmaker:SoftMaker",
		 "URW:urw:URW",
		 "Jonathan Brecher:brecher:J.Brecher",
		 "Brendel Informatik:brendel:Brendel Informatik",
		 "A. Carr:carr:A.Carr",
		 "FontBank:fontbank:FontBank",
		 "Hershey:hershey:Hershey",
		 "A.S.Meit:meit:A.S.Meit",
		 "Andrew s. Meit:meit:A.S.Meit",
		 "S.G. Moye:moye:S.G.Moye",
		 "S. G. Moye:moye:S.G.Moye",
		 "D. Rakowski:rakowski:D.Rakowski",
		 "David Rakowski:rakowski:D.Rakowski",
		 "Reasonable Solutions:reasonable:Reasonable Solutions",
		 "Southern Software:southern:Southern Software",
		 "Title Wave:titlewave:Title Wave",
		 "ZSoft:zsoft:ZSoft",
		 "Henry Churchyard:churchyard:H.Churchyard");

my @weights = ("book:book",
	       "extrabold:extrabold",
	       "boldface:bold",
	       "bold:bold",
	       "heavyface:heavy",
	       "heavy:heavy",
	       "ultrablack:ultra",
	       "extrablack:ultra",
	       "ultra:ultra",
	       "black:black",
	       "extralight:extralight",
	       "super:super",
	       "thin:hairline",
	       "hairline:hairline",
	       "light:light",
	       "demi:demibold",
	       "semi:semibold",
	       "demibold:demibold",
	       "semibold:semibold",
	       "medium:medium",
	       "normal:medium",
	       "regular:regular",
	       "roman:regular");

my @widths = ("extracondensed:extracondensed",
	      "ultracondensed:ultracondensed",   
	      "condensed:condensed",
	      "cond:condensed",
	      "cn:condensed",
	      "extracompressed:extracompressed",
	      "ultracompressed:extracompressed",
	      "compressed:compressed",
	      "comp:compressed",
	      "narrow:narrow",
	      "regular:regular",
	      "normal:regular",
	      "extended:extended",
	      "expanded:expanded",
	      "wide:wide",
	      "extraextended:wide");

my @variants = ("sans:sans",
		"sansserifs:sans",
		"alt:alternate",
		"beginning:beginning",
		"display:display",
		"dfr:dfr",
		"ending:ending",
		"ep:expert",
		"exp:expert",
		"ornaments:ornaments",
		"osf:oldstylefigures",
		"outline:outline",
		"smallcaps:smallcaps",
		"sc:smallcaps",
		"shaded:shaded",
		"shadowed:shadowed",
		"stencil:stencil",
		"swash:swash",
		"sw:swash",
		"stone:stone",
		"one:one",
		"two:two",
		"three:three",
		"four:four", 
		"alternate:alternate");

my @slants = ("roman:regular:r",
	      "regular:regular:r",
	      "upright:regular:r",
	      "italic:italic:i",
	      "cursive:italic:i",
	      "kursiv:italic:i",
	      "oblique:slanted:o",
	      "obl:slanted:o",
	      "slanted:slanted:o",
	      "inclined:slanted:o");


# build_type1snippet() analyzes a type1 font and returns
# a valid snippet (in perl format, not textual one)

sub build_type1snippet {
    my ($realpath, $realfile) = (@_);
    my ($fontfile, $tmpfile);
    my ($gsfont, $mm, $notice, $encoding, $axis, $psname);
    my ($fullname, $strippedfullname, $name, $foundry, $family);
    my ($weight, $slant, $width, $var, $allstyle);
    my ($fixedpitch);
    my @fields;

    # gets real path and name of the font and
    # convert it to .pfa if it is a .pfb
    if ($realfile =~ /\.pfb\s*$/) {
	$tmpfile = `tempfile`;
	safe_system("pfb2pfa $realpath/$realfile $tmpfile");
	$fontfile = $tmpfile;
    }
    else {
	$fontfile = "$realpath/$realfile";
    }
    
    # check to see if this is a ghostscript font
    $gsfont = $fontfile =~ /\.gsf\s*$/ ? 1 : 0;
    
    # default is not MultipleMaster
    $mm = 0;

    open(IN, $fontfile) or do {
	warning("can't open `$fontfile' for reading");
	return ();
    };
 
    $name = "NONAME";
    $family = "NOFAMILY";
    $foundry = "unknown";

    while(<IN>) {
      
	# fixed pitch? (only X11 needs that)
	if (/\/isFixedPitch\s+(.+)\s+def\s*/) {
	    if ($1 =~ /true/) {
		$fixedpitch = 'm';
	    } else {
		$fixedpitch = 'p';
	    }
	}

	# First we get the full name of the font (that has to be there)
	# and we save it. /FullName might contain useful information 
	# in determining the properties of a font.
	if (/\/FullName\s*\((.+)\)\s+readonly\s+def\s*/) {
	    $fullname = $strippedfullname = $1;
	    # some names got extra numerical information at the start
	    # let's remove it
	    $fullname =~ s/\*//g;
	    $fullname =~ s/^\d*\s*(.+)/$1/;
	    # removes dashes (replaces with spaces) from the fullname
	    $fullname =~ s/-/ /g;
	}

	# gets the font name and separate it from the style
	# the /FontName *has* to be here, for sure
	if (/\/FontName\s*\/(.+)\s+def\s*/) {
	    $name = $1;
	    # remove spaces (necessary?) and saves away a copy
	    # of the real postscript name
	    $psname = $name;
	    $name =~ s/\s//g;

	    # split the name into fontname and fontstyle instead 
	    # and handle them separate.
	    ($name, $allstyle) = split(/-/, $name);
	    # remove "-", makes styles lower case
	    $name =~ s/-//g;
	    $allstyle =~ s/-//g;
	    $allstyle = lc $allstyle;
	}

	# extract the family name if possible, else will use $fontname
	if (/\/FamilyName\s*\((.+)\)\s+readonly\s+def\s*/) {
	    $family = $1;
	}

	# extract weight
	if (/\/Weight\s*\((.+)\)\s+readonly\s+def\s*/) {
	    $weight = $1;
	    # coverts and removes white space
	    $weight = lc $weight;
	    $weight =~ s/\s*//g;

	    # remember if it's a MultipleMaster font
	    $mm = 1 if ($weight =~ /^all$/);
	  
	    # removes style info in the weight field...
	    foreach my $i (@variants, @slants) {
		@fields = split(/:/, $i);
		$weight =~ s/$fields[0]//;
	    }
	}

	# extract the encoding 
	if (/^\/Encoding\s+(\S+)\s*/) {
	    if ($1 =~ /StandardEncoding/) {
		$encoding = "standard";
	    } else {
		$encoding = ($encoding =~ /\d+/ ?
			     'fontspecific' : $1);
	    }
	}

	# extract foundry name from notice
	if (/^\s*\/Notice\s*(.*)$/) {
	    $notice = $1;
	    $notice =~ s/readonly def//g;

	    foreach my $i (@foundries) {
		@fields = split(/:/, $i);
		if ($notice =~ /$fields[0]/) {
		    $foundry = $fields[2];
		}
	    }
	}

	# multipleMaster fonts have this field.
	if (/\/BlendAxisTypes\s+\[([^\]]+)\]\s*def/) {
	    $axis = $1;
	    # remove axises we don't need
	    $axis =~ s/\/Weight\s+//;
	    $axis =~ s/\/Width\s+//;
	    # are there still some axises left?
	    if ($axis =~ /\//) {
		# remove trailing spaces
		$axis =~ s/^(.*?)\s*$/$1/;
		$axis =~ s/\/\S+/0/g;
		$axis= "[$axis]";
	    }
	}

	# break out of loop if we've passed the interesting stuff
	# we should have gathered all the intersting stuff by now
	# this is for .pfa and .pfb fonts
	if ((! $gsfont) && (/currentfile\s+eexec/)) {
	    last;
	} 
	# this is for ghostscript .gsf fonts
	elsif (($gsfont) && (/currentdict\s+end/)) {
	    last;
	}
    }
    close(IN);  # *END OF THE FILE SCAN*

    # here we do some little tricks with the family name,
    # first we set it to the initial part of fontname if not
    # yet set.
    if ($family =~ /NOFAMILY/) {
	($family, undef) = split(/-/, $name);
	$family =~ s/-//g;
    }

    # creates an alternate style, by stripping the family name
    # from the full name of the font. then look for other attributes
    # like width, weight, etc...
    if ($strippedfullname =~ s/^$family\s*(\d\d+)?\s*(.*)/$2/) {
	# removes whitespace and convert to lowercase
	$strippedfullname =~ s/\s+//g;
	$strippedfullname = lc $strippedfullname;
    } else {
	$strippedfullname = "";
    }	

    # check for weight modifiers in the name...
    my $name_weight = "medium";
    foreach my $i (@weights) {
	my @fields = split(/:/, $i);
	# we try first with the fullname...
	if ($strippedfullname =~ /$fields[0]/) {
	    $name_weight = $fields[1];
	    # we put it in weight
	    if (! $weight) {
		$weight = $name_weight; 
	    }
	} 
	# remove matched word
	$strippedfullname =~ s/$fields[0]//;

	# ...then with the style from the name
	if ($allstyle =~ /$fields[0]/) {
	    $name_weight = $fields[1];
	    # we put it in weight
	    if (! $weight) {
		$weight = $name_weight; 
	    }
	} 
	# remove matched word from the font's name
	$allstyle =~ s/$fields[0]//;
    }
    # puts the medium one if none was found
    $weight = $name_weight unless $weight;
    
    # check for slantness (italic, roman, oblique)
    $slant = "regular";
    foreach my $i (@slants) {
	my @fields = split(/:/, $i);
	if ($strippedfullname =~ /$fields[0]/) {
	    $slant = $fields[1];
	}
	# remove matched word from the font's name
	$strippedfullname =~ s/$fields[0]//;

	if ($allstyle =~ /$fields[0]/) {
	    $slant = $fields[1];
	}
	# remove matched word from the font's name
	$allstyle =~ s/$fields[0]//;
    }
    
    # check for width (condensed, normal, wide, ...)
    $width = "regular";
    foreach my $i (@widths) {
	@fields = split(/:/, $i);
	if ($strippedfullname =~ /$fields[0]/) {
	    $width = $fields[1];
	}
	# remove matched word from the font's name
	$strippedfullname =~ s/$fields[0]//;

	if ($allstyle =~ /$fields[0]/) {
	    $width = $fields[1];
	}
	# remove matched word from the font's name
	$allstyle =~ s/$fields[0]//;
    }
    
    # check for additional style variants (alternate, smallcaps):
    # what's left of fullname is probably additional style information.
    # Some fontnames have some strange numerical information here too.
    # If it's just one number, it usually refers to some variant of the
    # fontfamily, otherwise, just get rid of it.
    $strippedfullname = "" if ($strippedfullname =~ /^\d\d+$/);
    $var = 'none';
    foreach my $i (@variants) {
	@fields = split(/:/, $i);
	if ($strippedfullname =~ /$fields[0]/) {
	    $var = $fields[1];
	}
	if ($allstyle =~ /$fields[0]/) {
	    $var = $fields[1];
	}
    }   
    
    # couldn't get family name right
    if ($family =~ /NONAME/) {
	warning("couldn't get font name from " .
		 "file `$fontfile' (is this a Type1 font?)");
	return ();
    }

    # builds the x line for the font, here we should replace some of the
    # information retrieved with more X11-ish one.

    # FIX: only iso8859-1 supported at now, all others
    # are `adobe-fontspecific'
    $encoding = 'fontspecific' if $encoding =~ /\d+/;
    my $xencoding;
    if ($encoding =~ /standard/) {
	$xencoding = 'iso8859-1';
    }
    else
    {
	$xencoding = 'fontspecific';
    }

    # a better X11 foundry name
    my $xfoundry;
    foreach my $i (@foundries) {
	@fields = split(/:/, $i);
	if ($foundry =~ /$fields[2]/) {
	    $xfoundry = $fields[1];
	}
    }

    # slantness
    my $xslant;
    foreach my $i (@slants) {
	@fields = split(/:/, $i);
	if ($slant =~ /$fields[1]/) {
	    $xslant = $fields[2];
	}
    }

    # makes a lowercase copy of familyname (X11 again) and eats 
    # dashes (sometimes in fullname or familyname)
    my $xfamily = lc $family;
    $xfamily =~ s/-//g;
    
    my $xweight = $weight;
    my $xfixedpitch = $fixedpitch;
    my $xaddstyle = ($var =~ /none/ ? '' : $var);;

    # X manpage shows 'normal' and not 'regular'
    my $xstyle = ($width =~ /regular/ ? 'normal' : $width);

    # we are dealing with a MultipleMaster font...
    if ($mm) {
	$xweight = "0";
	$xstyle = "0";
	$xaddstyle .= $axis;
    }
	
    my $xline = "-$xfoundry-$xfamily-$xweight-$xslant-$xstyle-$xaddstyle-0-0-0-0-$xfixedpitch-0-$xencoding";

    # builds the ID
    my $id = "type1$foundry$family$weight$slant$xstyle$xaddstyle";
    $id =~ s/\s//g;
    $id = lc $id;

    # deletes the temporary file if it exists
    safe_system("rm $tmpfile") if $tmpfile;

    #and finally creates the catalog snippet
    my $snippet = new DTM::Snippet;
    $snippet->set_attrs(undef,  # this was the class
			ID => $id,
			Foundry => $foundry,
			Typeface => $family,
			Weight => $weight,
			Slantness => $slant,
			Width => $width,
			Variant => $var,
			Encoding => $encoding,
			Name => $fullname,
			FontFile => $realfile,
			FontPath => $realpath);
    $snippet->set_attr('Name', $xline, 'x11specific');
    $snippet->set_attr('Name', $psname, 'psspecific');
    return $snippet;
}


# return true
1