* updated sfdnormalize.pl to new SFD format with empty lines.
[dejavu.git] / dejavu-fonts / scripts / sfdnormalize.pl
blobad91661085d84e868e2ff0ea3349669c329dfa63
1 #!/usr/bin/perl -w
3 # $Id$
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
10 # changes done:
11 # WinInfo - discarded
12 # DisplaySize
13 # - discarded
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)
19 # HStem, VStem
20 # - discarded (those are Type1 stems = PS hints)
21 # recalculate number of characters and positional encoding
22 # NameList
23 # - discarded (temporary)
24 # ModificationTime - discarded
25 # CreationTime - discarded
26 # VWidth - discarded (for now)
27 # TeXData - discarded
28 # TeX - discarded
29 # changes making it incompatible with FF older than (approx.) 20050728:
30 # Ref - renamed to Refer
31 # KernsSLIF
32 # - renamed to KernsSLIFO
34 # !!! Always review changes done by this utility !!!
36 sub process_sfd_file($);
38 sub process_sfd_file($) {
39 my ($sfd_file) = @_;
41 my $out = $sfd_file . '.norm';
43 open (SFD, $sfd_file) || die "Unable to open $sfd_file : $!\n";
44 open (OUT, '>'.$out) || die "Unable to open $out : $!\n";
46 my $curchar = '';
47 my %glyphs = ();
48 my $in_spline_set = 0;
49 my $max_dec_enc = 0;
50 my %pos_glyphs_map = ();
52 while (<SFD>) {
53 next if (/^(WinInfo|DisplaySize|HStem|VStem|ModificationTime|CreationTime|VWidth|TeX|TeXData):/);
54 next if (/^$/);
55 s,^(NameList:).*$,$1 AGL without afii,;
56 s,^Ref:,Refer:,;
57 s,^KernsSLIF:,KernsSLIFO:,;
58 s,^(Flags:.*?)O(.*)$,$1$2,;
59 s,^(Flags:.*?)H(.*)$,$1$2,;
60 s,^(Flags:.*?)M(.*)$,$1$2,;
61 # remove empty Flags line
62 next if (/^Flags:\s*$/);
63 s,^(Refer:.*?)S(.*)$,$1N$2,;
64 if (/^(Fore|Back|SplineSet|Grid)\s*$/) {
65 $in_spline_set = 1;
66 } elsif (/^EndSplineSet\s*$/) {
67 $in_spline_set = 0;
68 } elsif ($in_spline_set) {
69 s/(\s+)(\S+?)(,\S+\s*)$/$1.($2 & ~4 & ~256).$3/e;
70 s/([\s^])-0(\s)/$1."0".$2/eg
72 if (/^BeginChars:/) {
73 $in_chars = 1;
74 } elsif (/^EndChars\s*$/) {
75 $in_chars = 0;
76 # adding of 1 to max_dec_enc is strange, but works
77 # second parameter is set to 0 to avoid merging conflicts (fontforge ignores anyway)
78 print OUT "BeginChars: ", $max_dec_enc + 1, " 0\n";
79 foreach $glyph (sort { $glyphs{$a}{'dec_enc'} <=> $glyphs{$b}{'dec_enc'} } keys %glyphs) {
80 print OUT "StartChar: ", $glyphs{$glyph}{'name'}, "\n";
81 my $dec_enc = $glyphs{$glyph}{'dec_enc'};
82 my $mapped_enc = $glyphs{$glyph}{'mapped_enc'};
83 print OUT "Encoding: ", $dec_enc, " ", $mapped_enc, " ", $dec_enc, "\n";
84 # recalculate references and kerning pairs
85 foreach $l (@{$glyphs{$glyph}{'lines'}}) {
86 $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;
87 if ($l =~ /^KernsSLIFO:\s*(.*)$/) {
88 my @nums = split(/\s+/, $1);
89 if ((scalar (@nums) % 4) != 0) {
90 warn "Kerning definition in glyph $curchar ($dec_enc) is malformed and won't be remapped\n";
91 } else {
92 for (my $i = 0; $i < scalar (@nums); $i += 4) {
93 my $pos = $nums[$i];
94 if (exists $pos_glyphs_map{$pos}) {
95 $nums[$i] = $pos_glyphs_map{$pos};
96 } else {
97 warn "Glyph $glyph ($dec_enc) has kerning reference to unknown glyph position $pos\n";
101 $l = "KernsSLIFO: " . join(' ', @nums) . "\n";
103 print OUT $l;
105 print OUT "EndChar\n";
106 $pos++;
108 print OUT "EndChars\n";
109 } elsif (/^StartChar:\s*(\S+)\s*$/) {
110 my $name = $1;
111 $curchar = $name;
112 while (exists $glyphs{$curchar}) {
113 $curchar .= '#';
115 $glyphs{$curchar}{'name'} = $name;
116 } elsif (/^Encoding:\s*(\d+)\s*((?:-|\d)+)\s*(\d+)\s*$/) {
117 $dec_enc = $1;
118 $max_dec_enc = $dec_enc if ($dec_enc > $max_dec_enc);
119 $mapped_enc = $2;
120 $pos = $3;
121 $glyphs{$curchar}{'dec_enc'} = $dec_enc;
122 $glyphs{$curchar}{'mapped_enc'} = $mapped_enc;
123 $glyphs{$curchar}{'pos'} = $pos;
124 if (exists $pos_glyphs_map{$pos}) {
125 warn "Glyph $curchar ($dec_enc) has duplicate glyph position $pos - won't be remapped - possible output corruption!\n";
126 } else {
127 $pos_glyphs_map{$pos} = $dec_enc;
129 } elsif (/^EndChar\s*$/) {
130 $curchar = '';
131 } else {
132 if (!$in_chars) {
133 print OUT;
134 } elsif ($curchar eq '') {
135 warn "Malformed input file $sfd_file?";
136 } else {
137 push (@{$glyphs{$curchar}{'lines'}}, $_);
142 close (SFD);
143 close (OUT);
146 if (!@ARGV) {
147 print STDERR "usage: sfd_files+\n";
148 exit 1;
151 @sfd_files = @ARGV;
153 foreach $sfd_file (@sfd_files) {
154 process_sfd_file ($sfd_file);