On Wed, Sep 12, 2007 at 08:59:22PM +1000, Brendan O'Dea wrote:
>I'm not very familiar with the po4a code, but have attached a simple
>proof of concept script which could perhaps be of help in adding this
>feature to po4a.
Bugger. Really attaching this script this time...
--bod
#!/usr/bin/perl
# proof of concept converter to exchange utf8 strings for groff escapes
use strict;
use warnings;
package Groff::Font::utf8;
use Carp;
use IO::File;
sub new
{
my $class = shift;
my $self = {};
my ($datadir) = grep -d, '/usr/share/groff',
'/usr/local/share/groff' # etc
or croak "can't find groff data directory: $!";
my $fontdir = "$datadir/current/font";
unless (-d $fontdir)
{
# MacOS has no "current" symlink... punt
($fontdir) = grep -d, reverse glob "$datadir/[0-9]*/font";
}
my $font = IO::File->new("$fontdir/devutf8/R")
or croak "can't open groff utf8 roman font: $!";
while (<$font>)
{
/^(\S+) \s+ \d+ \s+ \d+ \s+ 0x([\da-f]+)/xi or next;
my ($esc, $code) = ($1, hex $2);
# skip unrepresentable chars and ascii range
next if $esc eq '---' or $code < 0x80;
my $len = length $esc;
$self->{$code} = $len == 1 ? "\\$esc" :
$len == 2 ? "\\($esc" :
"\\[$esc]";
}
bless $self, $class;
}
# convert utf8 string
sub convert
{
my $self = shift;
my @s = @_;
s/([^\x00-\x7f])/$self->{ord $1} || $1/ge for @s;
wantarray ? @s : join '', @s;
}
# convert raw bytes
sub convert_raw
{
my $self = shift;
require Encode;
$self->convert(map Encode::decode_utf8($_), @_);
}
package main;
my $f = Groff::Font::utf8->new;
my $utf8 = <<EOT;
Det h\x{e4}r \x{e4}r fri programvara, licenserad under villkoren f\x{f6}r GNU
General
Public License. Det finns INGEN garanti; inte ens f\x{f6}r S\x{c4}LJBARHET
eller
L\x{c4}MPLIGHET F\x{d6}R N\x{c5}GOT SPECIELLT \x{c4}NDAM\x{c5}L.
EOT
print "utf8 string:\n", '-' x 60, "\n", $f->convert($utf8);
my $bytes = do { local $/; <DATA> };
print "\nraw bytes:\n", '-' x 60, "\n", $f->convert_raw($bytes);
__END__
Det här är fri programvara, licenserad under villkoren för GNU General
Public License. Det finns INGEN garanti; inte ens för SÄLJBARHET eller
LÄMPLIGHET FÖR NÅGOT SPECIELLT ÄNDAMÅL.