* added U+22C6, U+235F
[dejavu.git] / dejavu-fonts / merge.pl
blobdc0ff3e58c75937aff0bc157e8f3388db550ae44
1 #!/usr/bin/perl -w
3 # $Id$
5 # SFD files merger
6 # (c)2005 Stepan Roh (PUBLIC DOMAIN)
7 # usage: ./merge.pl sfd_from sfd_to sfd_out
8 # will merge sfd_to with sfd_from into the sfd_out (if there are some merges)
9 # will print merged glyph codes on stdout
11 # SFD = ( 'header' => @header_lines, 'footer' => @footer_lines, 'glyphs' => ( glyphenc => @glyph_lines ) )
13 sub load_sfd ($) {
14 my ($sfdfile) = @_;
16 my %sfd = ();
17 open (SFD, $sfdfile) || die "Unable to open $sfdfile : $!\n";
18 # -1 = header, 0 = glyphs, 1 = footer
19 my $section = -1;
20 my $enc = 0;
21 my @cur = ();
22 while (<SFD>) {
23 if (/^StartChar:/) {
24 $section = 0;
25 } elsif (/^Encoding:\s*(\d+)/) {
26 $enc = $1;
28 if ($section == -1) {
29 push (@{$sfd{'header'}}, $_);
30 } elsif ($section == 1) {
31 push (@{$sfd{'footer'}}, $_);
32 } else {
33 push (@cur, $_);
35 if (/^EndChar\s*$/) {
36 $section = 1;
37 @{$sfd{'glyphs'}{$enc}} = @cur;
38 @cur = ();
41 close (SFD);
43 return %sfd;
46 sub save_sfd ($\%) {
47 my ($sfdfile, $sfd_ref) = @_;
49 open (SFD, '>'.$sfdfile) || die "Unable to open $sfdfile : $!\n";
50 print SFD @{$$sfd_ref{'header'}};
51 foreach $enc (sort { $a <=> $b } keys %{$$sfd_ref{'glyphs'}}) {
52 print SFD @{$$sfd_ref{'glyphs'}{$enc}};
54 print SFD @{$$sfd_ref{'footer'}};
55 close (SFD);
58 sub is_dummy_glyph (\@) {
59 my ($glyph_ref) = @_;
61 foreach $l (@$glyph_ref) {
62 if ($l =~ /^Colour:/) {
63 # XXX this is quick'n'dirty hack to detect dummy glyphs
64 return 1;
68 return 0;
71 sub merge_glyphs (\%\%) {
72 my ($sfd_from, $sfd_to) = @_;
74 my $merged = 0;
75 foreach $enc (sort { $a <=> $b } keys %{$$sfd_from{'glyphs'}}) {
76 if (!exists ($$sfd_to{'glyphs'}{$enc}) ||
77 (!is_dummy_glyph(@{$$sfd_from{'glyphs'}{$enc}}) && is_dummy_glyph(@{$$sfd_to{'glyphs'}{$enc}}))) {
78 $$sfd_to{'glyphs'}{$enc} = $$sfd_from{'glyphs'}{$enc};
79 print $enc, "\n";
80 $merged++;
84 return $merged;
87 if (@ARGV < 3) {
88 print STDERR "usage: $0 sfd_from sfd_to sfd_out\n";
89 exit (1);
92 ($sfdfile_from, $sfdfile_to, $sfdfile_out) = @ARGV;
94 %sfd_from = load_sfd ($sfdfile_from);
95 %sfd_to = load_sfd ($sfdfile_to);
96 if (merge_glyphs (%sfd_from, %sfd_to) > -1) {
97 save_sfd ($sfdfile_out, %sfd_to);