Bug 8375: (follow-up) adjust StrWidth to account for TTF fonts
authorGalen Charlton <gmc@esilibrary.com>
Tue, 6 May 2014 18:52:12 +0000 (18:52 +0000)
committerGalen Charlton <gmc@esilibrary.com>
Tue, 6 May 2014 18:52:12 +0000 (18:52 +0000)
This patch fixes an issue caught by the test case where StrWidth()
based its calculations on the internal Adobe font rather than a
TrueType font in use.

Signed-off-by: Galen Charlton <gmc@esilibrary.com>

C4/Creators/PDF.pm
t/Creators.t

index 14bc9c4..738c34f 100644 (file)
@@ -308,6 +308,18 @@ sub SinglePage {
 sub StrWidth {
     my $self = shift;
     my ($string, $font, $fontSize) = @_;
+
+    # replace font code with path to TTF font file if need be
+    my $ttf = C4::Context->config('ttf');
+    if ( $ttf ) {
+        my $ttf_path = first { $_->{type} eq $font } @{ $ttf->{font} };
+        if ( -e $ttf_path->{content} ) {
+            $font = $ttf_path->{content};
+        } else {
+            warn "ERROR in koha-conf.xml -- missing <font type=\"$font\">/path/to/font.ttf</font>";
+        }
+    }
+
     return prStrWidth($string, $font, $fontSize);
 }
 
index 85701c2..899ff33 100755 (executable)
@@ -34,11 +34,11 @@ $pdf_creator->FontSize(); # Reset font size before testing text width etc below
 
 ok($pdf_creator->Page(), "testing Page() works");
 
-is($pdf_creator->StrWidth("test", "H", 12), '19.344', "testing StrWidth() returns correct point width");
+is($pdf_creator->StrWidth("test", "H", 12), '23.044921875', "testing StrWidth() returns correct point width");
 
 @result = $pdf_creator->Text(10, 10, "test");
 is($result[0], '10', "testing Text() writes from a given x-value");
-is($result[1], '29.344', "testing Text() writes to the correct x-value");
+is($result[1], '33.044921875', "testing Text() writes to the correct x-value");
 
 open(my $fh, '>', 'test.pdf');
 select $fh;