Whole document tree
    

Whole document tree

A Tutorial on Writing Defoma Configuration Script - Using Defoma::Id module.
[ previous ] [ Abstract ] [ Contents ] [ next ]

A Tutorial on Writing Defoma Configuration Script
Chapter 4 Using Defoma::Id module.


The one major problem of the example of previous chapter is that it doesn't consider the situation that two different fonts has the same FontName. It is so-called name-space conflict, and should be avoided if possible. Defoma::Id module can solve this problem.

This chapter describes a package named 'example3' which accepts truetype category fonts. The application accesses fonts put in a certain directory (like X), and reads fonts.dir file put in the directory which lists a fontfile and its real name, and fonts.alias which lists an alias and its real name of a font. The Defoma-configuration script generates the files and symlinks under /var/lib/defoma/example3.d/dirs, which is supposed to be symlinked from the required directory.

In Header of the script, declaration of using Defoma::Id module is needed.

     	  @ACCEPT_CATEGORIES = qw(truetype);
     
     	  package example3;
     	  use Debian::Defoma::Id;
     	  use Debian::Defoma::Common;
     	  import Debian::Defoma::Id;
     	  import Debian::Defoma::Common;
     
     	  my $Id;
     	  my $DIR = '/var/lib/defoma/example3.d/';
     	  my @dir = ();
     	  my @scale = ();

In init command, the script needs to open an id-cache. In the same way as the previous examples, the script reads fonts.dir and fonts.alias files.

     	  sub init {
     	    unless ($Id) {
     	      $Id = defoma_id_open_cache();
     	      if (open(F, $DIR.'fonts.dir')) {
     	        while (<F>) {
     	          chomp($_);
     	          push(@dir, $_);
     	        }
     	        close F;
     	      }
     	      if (open(F, $DIR.'fonts.alias')) {
     	        while (<F>) {
     	          chomp($_);
     	          push(@alias, $_);
     	        }
     	        close F;
     	      }
     	    }
     	    return 0;
     	  }

In register command, the script parses the hints, and obtain the FontName, Alias and Location from them. The script returns error when some required hints are missing, or the font doesn't contain Korean characters.

     	  
     	  sub register {
     	    my $font = shift;
     	    my $h = parse_hints_start(@_);
     
     	    unless (exists($h->{FontName}) && exists($h->{Location})) {
     	      return 1;
     	    }
     
     	    my $fontname = $h->{FontName};
     	    $fontname =~ s/ .*//;
     	    my $priority = $h->{Priority} || 20;
     	    my @locs = split(/ /, $h->{Location});
     	    my @alias = split(/ /, $h->{Alias});
     
     	    return 2 unless(grep($_ eq 'Korean', @locs));

Now the script calls defoma_id_register to register the fontname and the aliases to the id-cache.

     	    defoma_id_register($Id, type => 'real', font => $font,
     	                       id => $fontname, priority => $priority,
     	                       hints => join(' ', @_));
     
     	    foreach my $i (@alias) {
     	      defoma_id_register($Id, type => 'alias', font => $font,
     	                         id => $fontname, priority => $priority,
     	                         origin => $fontname);
     	    }
     
     	    return 0;
     	  }

Process in unregister command becomes a bit easier. The following codes will do.

     	  sub unregister {
     	    my $font = shift;
     	    defoma_id_unregister($Id, type => 'alias', font => $font);
     	    defoma_id_unregister($Id, type => 'real', font => $font);
     
     	    return 0;
     	  }

Codes up to this point do nothing about actual configuration. The script needs to do two things: create a symlink of a fontfile under /var/lib/defoma/example3.d/dirs, and generate fonts.dir and fonts.scale files. Let's perform these on do-install-* and do-remove-* commands.

Both do-install-* commands and do-remove-* commands are invoked by Defoma::Id module. In these commands actual installation and removal are supposed to be performed in Defoma framework. Let's create and remove symlinks in these commands in this example.

In this example, a font may have multiple ids, one real name and some aliases. Creating a symlink of a font is not necessary for each id. It should be done when the real name is installed, because aliases never get installed unless the real name is not installed. That means creating and removing a symlink should be done in do-install-real and do-remove-real commands.

If creating a symlink fails, the script returns non-zero to tell Defoma::Id that the font is not installed, which prevents its aliases from getting installed.

     	  sub do_install_real {
     	    my $font = shift;
     	    my $id = shift;
     	    my $depfont = shift;
     	    my $depid = shift;
     	    my @hints = @_;
     
     	    $font =~ /^(.*)\/(.+)$/;
     	    my $fontfile = $2;
     
     	    symlink($font, $DIR.$fontfile) || return 1;
     
     	    push(@dirs, "/$id ($fontfile) ;");
     	    return 0;
     	  }
     
     	  sub do_remove_real {
     	    my $font = shift;
     	    my $id = shift;
     	    my $depfont = shift;
     	    my $depid = shift;
     	    my @hints = @_;
     
     	    $font =~ /^(.*)\/(.+)$/;
     	    my $fontfile = $2;
     
     	    unlink($DIR.$fontfile);
     
     	    for (my $i = 0; $i < @dirs; $i++) {
     	      if ($dirs[$i] eq "/$id ($fontfile) ;") {
     	        $dirs[$i] = '';
     	      }
     	    }
     	    return 0;
     	  }

Installation and removal for aliases are done in do-install-alias and do-remove-alias commands respectively. This script modifies @alias in these commands. Note that the fourth argument represents the origin of the alias, which is specified in the arguments of defoma_id_register and is actually the real name of the font.

     	  sub do_install_alias {
     	    my $font = shift;
     	    my $id = shift;
     	    my $depfont = shift;
     	    my $depid = shift;
     	    my @hints = @_;
     
     	    push(@alias, "/$id /$depid ;");
     	    return 0;
     	  }
     
     	  sub do_remove_alias {
     	    my $font = shift;
     	    my $id = shift;
     	    my $depfont = shift;
     	    my $depid = shift;
     	    my @hints = @_;
     
     	    for (my $i = 0; $i < @alias; $i++) {
     	      if ($alias[$i] eq "/$id /$depid ;") {
     	        $alias[$i] = '';
     	      }
     	    }
     	    return 0;
     	  }

term command is almost the same as the previous examples, except closing the id-cache.

     	  sub term {
     	    my $i;
     	    if ($Id) {
     	      defoma_id_close_cache($Id);
     	      if (open(F, '>'.$DIR.'fonts.dir')) {
     	        foreach $i (@dir) {
     	          print F $i, "\n"; if ($i ne '');
     	        }
     	        close F;
     	      }
     	      if (open(F, '>'.$DIR.'fonts.alias')) {
     	        foreach $i (@alias) {
     	          print F $i, "\n"; if ($i ne '');
     	        }
     	        close F;
     	      }
     	    }
     	    return 0;
     	  }

The truetype function goes like this:

     	  sub truetype {
     	    my $com = shift;
     	    if ($com eq 'init') {
     	      return init();
     	    } elsif ($com eq 'term') {
     	      return term();
     	    } elsif ($com eq 'register') {
     	      return register(@_);
     	    } elsif ($com eq 'unregister') {
     	      return unregister(@_);
     	    } elsif ($com eq 'do-install-real') {
     	      return do_install_real(@_);
     	    } elsif ($com eq 'do-install-alias') {
     	      return do_install_alias(@_);
     	    } elsif ($com eq 'do-remove-real') {
     	      return do_remove_real(@_);
     	    } elsif ($com eq 'do-remove-alias') {
     	      return do_remove_alias(@_);
     	    }
     	    return 0;
     	  }
     
     	  1;

The generated files go like:

     	  fonts.dir:
     	  /URWGothicL-Bold (gothicb.ttf) ;
     	  /URWSanL-Obli (sano.ttf) ;
     	  fonts.alias:
     	  /Helvetica-Oblique /URWSanL-Obli ;
     	  /AvantGarde-Bold /URWGothicL-Bold ;


[ previous ] [ Abstract ] [ Contents ] [ next ]
A Tutorial on Writing Defoma Configuration Script
Yasuhiro Take