Whole document tree
    

Whole document tree

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

A Tutorial on Writing Defoma Configuration Script
Chapter 3 Using hints.


register and unregister commands are called with hints of the font. Let's use hints in font configuration. This chapter describes a package named 'example2' which accepts truetype category fonts. The Defoma-configuration script generates default.conf, ja.conf, ko.conf, zh_cn.conf and zh_tw.conf under /var/lib/defoma/example2.d/dirs directory, which is supposed to be symlinked from the required place. These files list the font (path) and its font name taken from hints. Location hint decides which file to write.

Header, init and term goes the same way as the previous chapter's example, so the detail is not described here. Only one difference is that this script declares to use Debian::Defoma::Common module because this script calls parse_hints_start() function.

     	  @ACCEPT_CATEGORIES = qw(truetype);
     
     	  package example2;
     	  use Debian::Defoma::Common;
     	  import Debian::Defoma::Common;
     	  
     	  my @default = ();
     	  my @ja = ();
     	  my @ko = ();
     	  my @zh_cn = ();
     	  my @zh_tw = ();
     
     	  sub init {
     	    if (open(F, '/var/lib/defoma/example2.d/dirs/default.conf')) {
     	      while (<F>) {
     	        chomp($_);
     	        push(@default, $_);
     	      }
     	      close F;
     	    }
     	    if (open(F, '/var/lib/defoma/example2.d/dirs/ja.conf')) {
     	      while (<F>) {
     	        chomp($_);
     	        push(@ja, $_);
     	      }
     	      close F;
     	    }
     	    if (open(F, '/var/lib/defoma/example2.d/dirs/ko.conf')) {
     	      while (<F>) {
     	        chomp($_);
     	        push(@ko, $_);
     	      }
     	      close F;
     	    }
     	    if (open(F, '/var/lib/defoma/example2.d/dirs/zh_cn.conf')) {
     	      while (<F>) {
     	        chomp($_);
     	        push(@zh_cn, $_);
     	      }
     	      close F;
     	    }
     	    if (open(F, '/var/lib/defoma/example2.d/dirs/zh_tw.conf')) {
     	      while (<F>) {
     	        chomp($_);
     	        push(@zh_tw, $_);
     	      }
     	      close F;
     	    }
     	    return 0;
     	  }
     
     	  sub term {
     	    my $i;
     	    if (open(F, '>/var/lib/defoma/example2.d/dirs/default.conf')) {
     	      foreach $i (@default) {
     	        print F $i, "\n" if ($i ne '');
     	      }
     	      close F;
     	    }
     	    if (open(F, '>/var/lib/defoma/example2.d/dirs/ja.conf')) {
     	      foreach $i (@ja) {
     	        print F $i, "\n" if ($i ne '');
     	      }
     	      close F;
     	    }
     	    if (open(F, '>/var/lib/defoma/example2.d/dirs/ko.conf')) {
     	      foreach $i (@ko) {
     	        print F $i, "\n" if ($i ne '');
     	      }
     	      close F;
     	    }
     	    if (open(F, '>/var/lib/defoma/example2.d/dirs/zh_cn.conf')) {
     	      foreach $i (@zh_cn) {
     	        print F $i, "\n" if ($i ne '');
     	      }
     	      close F;
     	    }
     	    if (open(F, '>/var/lib/defoma/example2.d/dirs/zh_tw.conf')) {
     	      foreach $i (@zh_tw) {
     	        print F $i, "\n" if ($i ne '');
     	      }
     	      close F;
     	    }
     	    return 0;
     	  }

On register command, the hints must be parsed to pick out Location and FontName hints. parse_hints_start() is used for this purpose. This function converts hints stored in an array to a hash. For more detail, please refer the manpage of Defoma::Common.

If these necessary HintTypes, Location and FontName are not found, the script returns non-zero to tell defoma that the script failed to register the font. Unregister command goes the similar way except that the script does not check if Location and FontName exist, because defoma does not pass the registration-failed fonts to the script on unregister command.

     	  sub register {
     	    my $font = shift;
     	    my $h = parse_hints_start(@_);
     
     	    unless (exists($h->{Location}) || exists($h->{FontName})) {
     	      return 1;
     	    }
     
     	    my @locs = split(/ /, $h->{Location});
     	    my $fontname = $h->{FontName};
     	    # If there're more than one fontnames, only the first one
     	    # is used and the others are removed.
     	    $fontname=~ s/ .*//;
     
     	    if (grep($_ eq 'Japanese', @locs)) {
     	      push(@ja, $fontname.' '.$font);
     	    } elsif (grep($_ eq 'Korean', @locs)) {
     	      push(@ko, $fontname.' '.$font);
     	    } elsif (grep($_ eq 'Chinese-China', @locs)) {
     	      push(@zh_cn, $fontname.' '.$font);
     	    } elsif (grep($_ eq 'Chinese-Taiwan', @locs)) {
     	      push(@zh_tw, $fontname.' '.$font);
     	    } else {
     	      push(@default, $fontname.' '.$font);
     	    }
     	    return 0;
     	  }
     
     	  sub unregister {
     	    my $font = shift;
     	    my $h = parse_hints_start(@_);
     
     	    my $fontname = $h->{FontName};
     	    $fontname =~ s/ .*//;
     
     	    my $i;
     	    for ($i = 0; $i < @default; $i++) {
     	      $default[$i] = '' if ($default[$i] eq $fontname.' '.$font);
     	    }
     	    for ($i = 0; $i < @ja; $i++) {
     	      $ja[$i] = '' if ($ja[$i] eq $fontname.' '.$font);
     	    }
     	    for ($i = 0; $i < @ko; $i++) {
     	      $ko[$i] = '' if ($ko[$i] eq $fontname.' '.$font);
     	    }
     	    for ($i = 0; $i < @zh_cn; $i++) {
     	      $zh_cn[$i] = '' if ($zh_cn[$i] eq $fontname.' '.$font);
     	    }
     	    for ($i = 0; $i < @zh_tw; $i++) {
     	      $zh_tw[$i] = '' if ($zh_tw[$i] eq $fontname.' '.$font);
     	    }
     	    return 0;
     	  }

truetype() function is the same as the previous chapter's, so please refer it. The generated file would go like:

     	  Helvetica /usr/share/fonts/truetype/helvetic.ttf
     	  Courier /usr/share/fonts/truetype/courier.ttf
     	  Times-Roman /usr/share/fonts/truetype/times.ttf


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