Subject: | string_width wrong for characters > 126 |
The problem can be best demonstrated with the attached script. This script tries to draw some kind of bounding box using the output of string_width (disregard the wrong descender). For characters less and equal than codepoint 126, this looks fine. However, for characters greater and equal than 160, this is most of the time wrong. This is the case for Helvetica and Times-Roman.
Probably this also means that methods like stringc and stringr will produce wrongly aligned output, especially if many characters >= 160 are used.
I think that some values in the tables in PDF::Create::Page::init_widths are wrong. Unfortunately it's not clear how the tables were generated, and what kind of encoding was assumed here.
Subject: | pdfcreate.pl |
#!/usr/bin/perl -w
use strict;
use PDF::Create;
use Encode;
my $pdf = PDF::Create->new('filename' => '/tmp/mypdf.pdf',
'Author' => 'Slaven Rezic',
'Title' => 'Font width test',
'CreationDate' => [ localtime ], );
my $a4 = $pdf->new_page('MediaBox' => $pdf->get_page_size('A4'));
my $page = $a4->new_page;
my $f1 = $pdf->font('BaseFont' => 'Helvetica');
#my $f1 = $pdf->font('BaseFont' => 'Courier');
#my $f1 = $pdf->font('BaseFont' => 'Times-Roman');
{
my $xs = 20; # left margin
my $x = $xs;
my $y = 800; # start y at top margin
my @chs = (32..126, 160..255);
for my $ch (@chs) {
my $ch_s = chr($ch);
#Encode::from_to($ch_s, 'iso-8859-1', 'AdobeStandardEncoding');
my $s = $ch_s . ':' . $ch;
my $w = $page->string_width($f1, $s)*20;
if ($x + 55 > 575) { # dina4 width
$x = $xs;
$y -= 30;
}
$page->string($f1, 20, $x, $y, $s);
my $w_line = $page->string_width($f1, $ch_s)*20;
# draw some kind of bounding box, without taking descender into account
$page->line($x, $y, $x+$w_line, $y);
$page->line($x, $y+20, $x+$w_line, $y+20);
$page->line($x, $y, $x, $y+20);
$page->line($x+$w_line, $y, $x+$w_line, $y+20);
#$x+=$w+10;
$x+=65; # fixed x advance, to get table-like output
}
}
$pdf->close;
__END__