Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

illustrate utf8 pitfalls #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions contrib/utf8-special-characters.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/perl -w

use strict;
use warnings;

# use Carp 'verbose'; local $SIG{__DIE__} = sub { Carp::confess(@_) }; # use Data::Dumper;

use PDF::API2;

print qq{Usage: perl $0 ; xdg-open utf8-special-characters.pdf

Prints a sample string using a number of fonts to illustrate some traps of pdf fonts.
Assumes /usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf exists to provide UTF8 characters.
};

my ( $pdf, $page, $text, $font, $ttfont, $font_file, $top, $down );

$pdf = PDF::API2->new();

# Add a blank page
$page = $pdf->page();
$top = 600;
$down = 40;
$text = $page->text();
$font = $pdf->corefont( 'Helvetica-Bold', -encode => 'utf8' )
; # but core fonts don't really have much utf8 !
$text->font( $font, 12 );
$text->translate( 10, $top );
$text->text("In core font: US: a b c; accents: á é í; ES: ñ Ñ ¿;");
$top = $top - .5 * $down;
$text->translate( 10, $top );
$text->text(" DE: ä ö; RU: ѐ Ѡ; GR: α β ; Mono: iI lL zero=0 one=1");

$top = $top - $down;
$font_file = '/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf';
if ( -e $font_file ) {
$ttfont = $pdf->ttfont($font_file);
$text->font( $ttfont, 12 );
$text->translate( 10, $top );
$text->text(
"In true type font Dejavu Sans: US: a b c; accents: á é í; ES: ñ Ñ ¿;"
);
$top = $top - .5 * $down;
$text->translate( 10, $top );
$text->text(
" DE: ä ö; RU: ѐ Ѡ; GR: α β ; Mono: iI lL zero=0 one=1'");
}

$top = $top - $down;
$font_file = '/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf';
if ( -e $font_file ) {
use utf8;
$ttfont = $pdf->ttfont($font_file);
$text->font( $ttfont, 12 );
$text->translate( 10, $top );
$text->text(
"In true type font with 'use utf8;': US: a b c; accents: á é í; ES: ñ Ñ ¿;"
);
$top = $top - .5 * $down;
$text->translate( 10, $top );
$text->text(
" DE: ä ö; RU: ѐ Ѡ; GR: α β ; Mono: iI lL zero=0 one=1'");
}

# Save the PDF
$pdf->saveas('utf8-special-characters.pdf');

exit;