5 # SFD normalizer (discards GUI information from SFD files)
6 # (c)2004,2005 Stepan Roh (PUBLIC DOMAIN)
7 # usage: ./sfdnormalize.pl sfd_file(s)
8 # will create files with suffix .norm
14 # Flags - discarded O (open), H (changed since last hinting - useless for TTF), M (manual hinting - useless for TTF)
15 # Refer - changed S (selected) to N (not selected)
16 # Fore, Back, SplineSet, Grid
17 # - all points have 4 masked out from flags (selected)
18 # and 256 (do not interpolate)
20 # - discarded (those are Type1 stems = PS hints)
21 # recalculate number of characters and positional encoding
23 # - discarded (temporary)
24 # ModificationTime - discarded
25 # CreationTime - discarded
26 # VWidth - discarded (for now)
29 # Validated - discarded
30 # changes making it incompatible with FF older than (approx.) 20050728:
31 # Ref - renamed to Refer
33 # - renamed to KernsSLIFO
35 # !!! Always review changes done by this utility !!!
37 sub process_sfd_file
($);
39 sub process_sfd_file
($) {
42 my $out = $sfd_file . '.norm';
44 open (SFD
, $sfd_file) || die "Unable to open $sfd_file : $!\n";
45 open (OUT
, '>'.$out) || die "Unable to open $out : $!\n";
49 my $in_spline_set = 0;
51 my %pos_glyphs_map = ();
54 next if (/^(WinInfo|DisplaySize|HStem|VStem|ModificationTime|CreationTime|VWidth|TeX|TeXData|Validated):/);
56 s
,^(NameList
:).*$,$1 AGL without afii
,;
58 s
,^KernsSLIF
:,KernsSLIFO
:,;
59 s
,^(Flags
:.*?
)O
(.*)$,$1$2,;
60 s
,^(Flags
:.*?
)H
(.*)$,$1$2,;
61 s
,^(Flags
:.*?
)M
(.*)$,$1$2,;
62 # remove empty Flags line
63 next if (/^Flags:\s*$/);
64 s
,^(Refer
:.*?
)S
(.*)$,$1N$2,;
65 if (/^(Fore|Back|SplineSet|Grid)\s*$/) {
67 } elsif (/^EndSplineSet\s*$/) {
69 } elsif ($in_spline_set) {
70 s/(\s+)(\S+?)(,\S+\s*)$/$1.($2 & ~4 & ~256).$3/e;
71 s/([\s^])-0(\s)/$1."0".$2/eg
75 } elsif (/^EndChars\s*$/) {
77 # adding of 1 to max_dec_enc is strange, but works
78 # second parameter is set to 0 to avoid merging conflicts (fontforge ignores anyway)
79 print OUT
"BeginChars: ", $max_dec_enc + 1, " 0\n";
80 foreach $glyph (sort { $glyphs{$a}{'dec_enc'} <=> $glyphs{$b}{'dec_enc'} } keys %glyphs) {
81 print OUT
"StartChar: ", $glyphs{$glyph}{'name'}, "\n";
82 my $dec_enc = $glyphs{$glyph}{'dec_enc'};
83 my $mapped_enc = $glyphs{$glyph}{'mapped_enc'};
84 print OUT
"Encoding: ", $dec_enc, " ", $mapped_enc, " ", $dec_enc, "\n";
85 # recalculate references and kerning pairs
86 foreach $l (@
{$glyphs{$glyph}{'lines'}}) {
87 $l =~ s/^(Refer:\s*)(\S+)/$1.(exists $pos_glyphs_map{$2} ? $pos_glyphs_map{$2} : (warn "Glyph $glyph ($dec_enc) has reference to unknown glyph position $2\n", $2))/e;
88 if ($l =~ /^KernsSLIFO:\s*(.*)$/) {
89 my @nums = split(/\s+/, $1);
90 if ((scalar (@nums) % 4) != 0) {
91 warn "Kerning definition in glyph $curchar ($dec_enc) is malformed and won't be remapped\n";
93 for (my $i = 0; $i < scalar (@nums); $i += 4) {
95 if (exists $pos_glyphs_map{$pos}) {
96 $nums[$i] = $pos_glyphs_map{$pos};
98 warn "Glyph $glyph ($dec_enc) has kerning reference to unknown glyph position $pos\n";
102 $l = "KernsSLIFO: " . join(' ', @nums) . "\n";
106 print OUT
"EndChar\n";
109 print OUT
"EndChars\n";
110 } elsif (/^StartChar:\s*(\S+)\s*$/) {
113 while (exists $glyphs{$curchar}) {
116 $glyphs{$curchar}{'name'} = $name;
117 } elsif (/^Encoding:\s*(\d+)\s*((?:-|\d)+)\s*(\d+)\s*$/) {
119 $max_dec_enc = $dec_enc if ($dec_enc > $max_dec_enc);
122 $glyphs{$curchar}{'dec_enc'} = $dec_enc;
123 $glyphs{$curchar}{'mapped_enc'} = $mapped_enc;
124 $glyphs{$curchar}{'pos'} = $pos;
125 if (exists $pos_glyphs_map{$pos}) {
126 warn "Glyph $curchar ($dec_enc) has duplicate glyph position $pos - won't be remapped - possible output corruption!\n";
128 $pos_glyphs_map{$pos} = $dec_enc;
130 } elsif (/^EndChar\s*$/) {
135 } elsif ($curchar eq '') {
136 warn "Malformed input file $sfd_file?";
138 push (@
{$glyphs{$curchar}{'lines'}}, $_);
148 print STDERR
"usage: sfd_files+\n";
154 foreach $sfd_file (@sfd_files) {
155 process_sfd_file
($sfd_file);