Bug 8375: Use TrueType fonts in PDF::Reuse

Since built-in PDF fonts suport just Latin-1 encoding, we have
to switch to TrueType fonts to correctly encode all UTF-8 characters
(which we should be getting from database anyway).

This approach also nicely sidesteps our encoding cludges, but
requires paths to TrueType fonts which are included in koha-conf.xml
under new <ttf> section. Without this directive in kona-conf.xml
code will still use Latin-1 built-in pdf fonts.

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Chris Nighswonger <cnighswonger@foundations.edu>
Signed-off-by: Galen Charlton <gmc@esilibrary.com>
This commit is contained in:
Dobrica Pavlinusic 2012-07-19 23:57:28 +02:00 committed by Galen Charlton
parent a97926a999
commit 8e71489c33
3 changed files with 32 additions and 6 deletions

View File

@ -22,6 +22,7 @@ use warnings;
use PDF::Reuse;
use PDF::Reuse::Barcode;
use File::Temp;
use List::Util qw/first/;
BEGIN {
use version; our $VERSION = qv('3.07.00.049');
@ -55,8 +56,7 @@ sub new {
sub End {
my $self = shift;
# if the pdf stream is utf8, explicitly set it to utf8; this avoids at lease some wide character errors -chris_n
utf8::encode($PDF::Reuse::stream) if utf8::is_utf8($PDF::Reuse::stream);
prEnd();
# slurp temporary filename and print it out for plack to pick up
@ -112,6 +112,17 @@ sub Field {
sub Font {
my $self = shift;
my $fontName = shift;
my $ttf = C4::Context->config('ttf');
if ( $ttf ) {
my $ttf_path = first { $_->{type} eq $fontName } @{ $ttf->{font} };
if ( -e $ttf_path->{content} ) {
return prTTFont($ttf_path->{content});
} else {
warn "ERROR in koha-conf.xml -- missing <font type=\"$fontName\">/path/to/font.ttf</font>";
}
}
return prFont($fontName);
}

View File

@ -113,5 +113,21 @@ __PAZPAR2_TOGGLE_XML_POST__
<zebra_auth_index_mode>__AUTH_INDEX_MODE__</zebra_auth_index_mode>
<zebra_lockdir>__ZEBRA_LOCK_DIR__</zebra_lockdir>
<queryparser_config>__KOHA_CONF_DIR__/searchengine/queryparser.yaml</queryparser_config>
<!-- true type font mapping accoding to type from $font_types in C4/Creators/Lib.pm -->
<ttf>
<font type="TR" >/usr/share/fonts/truetype/ttf-dejavu/DejaVuSerif.ttf</font>
<font type="TB" >/usr/share/fonts/truetype/ttf-dejavu/DejaVuSerif-Bold.ttf</font>
<font type="TI" >/usr/share/fonts/truetype/ttf-dejavu/DejaVuSerif-Italic.ttf</font>
<font type="TBI">/usr/share/fonts/truetype/ttf-dejavu/DejaVuSerif-BoldItalic.ttf</font>
<font type="C" >/usr/share/fonts/truetype/ttf-dejavu/DejaVuSansMono.ttf</font>
<font type="CB" >/usr/share/fonts/truetype/ttf-dejavu/DejaVuSansMono-Bold.ttf</font>
<font type="CO" >/usr/share/fonts/truetype/ttf-dejavu/DejaVuSansMono-Oblique.ttf</font>
<font type="CBO">/usr/share/fonts/truetype/ttf-dejavu/DejaVuSansMono-BoldOblique.ttf</font>
<font type="H" >/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf</font>
<font type="HB" >/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans-Bold.ttf</font>
<font type="HBO">/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans-BoldOblique.ttf</font>
</ttf>
</config>
</yazgfs>

View File

@ -88,10 +88,9 @@ sub _calc_next_label_pos {
sub _print_text {
my $label_text = shift;
foreach my $text_line (@$label_text) {
my $pdf_font = $pdf->Font($text_line->{'font'});
my $line = "BT /$pdf_font $text_line->{'font_size'} Tf $text_line->{'text_llx'} $text_line->{'text_lly'} Td ($text_line->{'line'}) Tj ET";
utf8::decode($line);
$pdf->Add($line);
$pdf->Font($text_line->{'font'});
$pdf->FontSize( $text_line->{'font_size'} );
$pdf->Text( $text_line->{'text_llx'}, $text_line->{'text_lly'}, $text_line->{'line'} );
}
}