* finished adding hints to Tifinagh in Sans Book, room for improvements.
[dejavu.git] / dejavu-fonts / sfdnormalize.pl
blob11f6574135d97443ea3db20612317df302f37fc9
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 # changes making it incompatible with FF older than (approx.) 20050728:
28 # Ref - renamed to Refer
29 # KernsSLIF
30 # - renamed to KernsSLIFO
32 # !!! Always review changes done by this utility !!!
34 sub process_sfd_file($);
36 sub process_sfd_file($) {
37 my ($sfd_file) = @_;
39 my $out = $sfd_file . '.norm';
41 open (SFD, $sfd_file) || die "Unable to open $sfd_file : $!\n";
42 open (OUT, '>'.$out) || die "Unable to open $out : $!\n";
44 my $curchar = '';
45 my %glyphs = ();
46 my $in_spline_set = 0;
47 my $max_dec_enc = 0;
48 my %pos_glyphs_map = ();
50 while (<SFD>) {
51 next if (/^(WinInfo|DisplaySize|HStem|VStem|ModificationTime|CreationTime|VWidth):/);
52 s,^(NameList:).*$,$1 AGL without afii,;
53 s,^Ref:,Refer:,;
54 s,^KernsSLIF:,KernsSLIFO:,;
55 s,^(Flags:.*?)O(.*)$,$1$2,;
56 s,^(Flags:.*?)H(.*)$,$1$2,;
57 s,^(Flags:.*?)M(.*)$,$1$2,;
58 # remove empty Flags line
59 next if (/^Flags:\s*$/);
60 s,^(Refer:.*?)S(.*)$,$1N$2,;
61 if (/^(Fore|Back|SplineSet|Grid)\s*$/) {
62 $in_spline_set = 1;
63 } elsif (/^EndSplineSet\s*$/) {
64 $in_spline_set = 0;
65 } elsif ($in_spline_set) {
66 s/(\s+)(\S+?)(,\S+\s*)$/$1.($2 & ~4 & ~256).$3/e;
67 s/([\s^])-0(\s)/$1."0".$2/eg
69 if (/^BeginChars:/) {
70 $in_chars = 1;
71 } elsif (/^EndChars\s*$/) {
72 $in_chars = 0;
73 # adding of 1 to max_dec_enc is strange, but works
74 # second parameter is set to 0 to avoid merging conflicts (fontforge ignores anyway)
75 print OUT "BeginChars: ", $max_dec_enc + 1, " 0\n";
76 foreach $glyph (sort { $glyphs{$a}{'dec_enc'} <=> $glyphs{$b}{'dec_enc'} } keys %glyphs) {
77 print OUT "StartChar: ", $glyphs{$glyph}{'name'}, "\n";
78 my $dec_enc = $glyphs{$glyph}{'dec_enc'};
79 my $mapped_enc = $glyphs{$glyph}{'mapped_enc'};
80 print OUT "Encoding: ", $dec_enc, " ", $mapped_enc, " ", $dec_enc, "\n";
81 # recalculate references and kerning pairs
82 foreach $l (@{$glyphs{$glyph}{'lines'}}) {
83 $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;
84 if ($l =~ /^KernsSLIFO:\s*(.*)$/) {
85 my @nums = split(/\s+/, $1);
86 if ((scalar (@nums) % 4) != 0) {
87 warn "Kerning definition in glyph $curchar ($dec_enc) is malformed and won't be remapped\n";
88 } else {
89 for (my $i = 0; $i < scalar (@nums); $i += 4) {
90 my $pos = $nums[$i];
91 if (exists $pos_glyphs_map{$pos}) {
92 $nums[$i] = $pos_glyphs_map{$pos};
93 } else {
94 warn "Glyph $glyph ($dec_enc) has kerning reference to unknown glyph position $pos\n";
98 $l = "KernsSLIFO: " . join(' ', @nums) . "\n";
100 print OUT $l;
102 print OUT "EndChar\n";
103 $pos++;
105 print OUT "EndChars\n";
106 } elsif (/^StartChar:\s*(\S+)\s*$/) {
107 my $name = $1;
108 $curchar = $name;
109 while (exists $glyphs{$curchar}) {
110 $curchar .= '#';
112 $glyphs{$curchar}{'name'} = $name;
113 } elsif (/^Encoding:\s*(\d+)\s*((?:-|\d)+)\s*(\d+)\s*$/) {
114 $dec_enc = $1;
115 $max_dec_enc = $dec_enc if ($dec_enc > $max_dec_enc);
116 $mapped_enc = $2;
117 $pos = $3;
118 $glyphs{$curchar}{'dec_enc'} = $dec_enc;
119 $glyphs{$curchar}{'mapped_enc'} = $mapped_enc;
120 $glyphs{$curchar}{'pos'} = $pos;
121 if (exists $pos_glyphs_map{$pos}) {
122 warn "Glyph $curchar ($dec_enc) has duplicate glyph position $pos - won't be remapped - possible output corruption!\n";
123 } else {
124 $pos_glyphs_map{$pos} = $dec_enc;
126 } elsif (/^EndChar\s*$/) {
127 $curchar = '';
128 } else {
129 if (!$in_chars) {
130 print OUT;
131 } elsif ($curchar eq '') {
132 warn "Malformed input file $sfd_file?";
133 } else {
134 push (@{$glyphs{$curchar}{'lines'}}, $_);
139 close (SFD);
140 close (OUT);
143 if (!@ARGV) {
144 print STDERR "usage: sfd_files+\n";
145 exit 1;
148 @sfd_files = @ARGV;
150 foreach $sfd_file (@sfd_files) {
151 process_sfd_file ($sfd_file);