This chapter describes writing a Defoma-configuration script for a package named `example1' which accepts truetype and type1 category fonts. The application reads a font-configuration file which just lists available truetype and type1 fonts. The script generates font.conf file under /var/lib/defoma/example1.d, which is supposed to be symlinked or included from the font-configuration file the application actually reads.
First of all, let's write a header of the script. The script accepts truetype and type1 categories, and the name of the package is `example1', so the header goes like this:
@ACCEPT_CATEGORIES = qw(type1 truetype);
package example1;
Before registraion and unregistraion of fonts is performed, init command is certainly passed. And before quitting, term command is certainly passed. Let the script read the font-configuration file on init command and write it on term command.
package example1;
my @file = ();
my $readflag = 0;
my $writeflag = 0;
sub init {
return 0 if ($readflag);
$readflag = 1;
open(F, '/var/lib/defoma/example1.d/font.conf') || return 0;
while (<F>) {
chomp($_);
push(@file, $_);
}
close F;
return 0;
}
sub term {
return 0 if ($writeflag);
$writeflag = 1;
open(F, '>/var/lib/defoma/example1.d/font.conf') || return 0;
foreach my $i (@file) {
print F $i, "\n" if ($i ne '');
}
close F;
return 0;
}
Let the script add the font to @file on register and erace the font from @file on unregister command.
sub register {
my $font = shift;
push(@file, $font);
return 0;
}
sub unregister {
my $font = shift;
my $i;
for ($i = 0; $i < @file; $i++) {
if ($file[$i] eq $font) {
$file[$i] = '';
return 0;
}
}
return 0;
}
The functions above are not directly called. type1() and truetype() must call them according to the command.
sub type1 {
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(@_);
}
return 0;
}
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(@_);
}
return 0;
}
1;