Skip to content

Commit

Permalink
Added $pdf->standard_fonts() and $pdf->is_standard_font($name)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssimms committed May 18, 2024
1 parent e32d910 commit a541d60
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 18 deletions.
3 changes: 3 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

- Fixed implementation of GH-77.

- Added $pdf->standard_fonts() and $pdf->is_standard_font($name) (initial
patch by Johan Vromans).


2.046 2024-05-14

Expand Down
31 changes: 30 additions & 1 deletion lib/PDF/API2.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2025,7 +2025,7 @@ sub font {
$font = $pdf->synthetic_font($base_font, %options)
Create and return a new synthetic font object. See
Creates and returns a new synthetic font object. See
L<PDF::API2::Resource::Font::SynFont> for details.
=cut
Expand All @@ -2051,6 +2051,35 @@ sub synthetic_font {
return $obj;
}

=head2 standard_fonts
@names = $pdf->standard_fonts()
Returns the names of the 14 standard (built-in) fonts. See
L<PDF::API2::Resource::Font::CoreFont> for details.
=cut

sub standard_fonts {
require PDF::API2::Resource::Font::CoreFont;
return PDF::API2::Resource::Font::CoreFont->names();
}

=head2 is_standard_font
$boolean = PDF::API2->is_standard_font($name);
Returns true if C<$name> is an exact, case-sensitive match for one of the
standard font names.
=cut

sub is_standard_font {
my $name = pop();
require PDF::API2::Resource::Font::CoreFont;
return PDF::API2::Resource::Font::CoreFont->is_standard($name);
}

=head2 font_path
@directories = PDF::API2->font_path()
Expand Down
41 changes: 24 additions & 17 deletions lib/PDF/API2/Resource/Font/CoreFont.pm
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,18 @@ use warnings;
# VERSION

use File::Basename;

use List::Util qw(any);
use PDF::API2::Util;
use PDF::API2::Basic::PDF::Utils;

my @standard_fonts = qw(
Courier Courier-Bold Courier-BoldOblique Courier-Oblique
Helvetica Helvetica-Bold Helvetica-BoldOblique Helvetica-Oblique
Symbol
Times-Bold Times-BoldItalic Times-Italic Times-Roman
ZapfDingbats
);

# Windows fonts with Type1 equivalents
my $alias = {
'arial' => 'helvetica',
Expand Down Expand Up @@ -188,7 +196,7 @@ sub new {
=head2 is_standard
my $boolean = $class->is_standard($name);
my $boolean = PDF::API2::Resource::Font::CoreFont->is_standard($name);
Returns true if C<$name> is an exact, case-sensitive match for one of the
standard font names shown above.
Expand All @@ -197,22 +205,21 @@ standard font names shown above.

sub is_standard {
my $name = pop();
return any { $_ eq $name } @standard_fonts;
}

=head2 names
my @font_names = PDF::API2::Resource::Font::CoreFont->list();
my $array_ref = PDF::API2::Resource::Font::CoreFont->list();
Returns an array or a reference to an array containing the names of the built-in
core (standard) fonts.
=cut

return 1 if $name eq 'Courier';
return 1 if $name eq 'Courier-Bold';
return 1 if $name eq 'Courier-BoldOblique';
return 1 if $name eq 'Courier-Oblique';
return 1 if $name eq 'Helvetica';
return 1 if $name eq 'Helvetica-Bold';
return 1 if $name eq 'Helvetica-BoldOblique';
return 1 if $name eq 'Helvetica-Oblique';
return 1 if $name eq 'Symbol';
return 1 if $name eq 'Times-Bold';
return 1 if $name eq 'Times-BoldItalic';
return 1 if $name eq 'Times-Italic';
return 1 if $name eq 'Times-Roman';
return 1 if $name eq 'ZapfDingbats';
return;
sub names {
return wantarray() ? @standard_fonts : [@standard_fonts];
}

1;
26 changes: 26 additions & 0 deletions t/font-corefont.t
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,30 @@ foreach my $font (qw(bankgothic courier courierbold courierboldoblique
lives_ok(sub { $pdf->corefont($font) }, "Load font $font");
}

ok($pdf->is_standard_font('Helvetica'),
q{Helvetica is a standard font});

ok(!$pdf->is_standard_font('Comic Sans'),
q{Comic Sans is not a standard font});

require PDF::API2::Resource::Font::CoreFont;
my @names = PDF::API2::Resource::Font::CoreFont->names();
is(scalar(@names), 14,
q{names() returns 14 elements in array context});

my $arrayref = PDF::API2::Resource::Font::CoreFont->names();
is(ref($arrayref), 'ARRAY',
q{names() returns an array reference in scalar context});
is(scalar(@$arrayref), 14,
q{The array reference contains 14 elements});

@names = $pdf->standard_fonts();
is(scalar(@names), 14,
q{$pdf->standard_fonts() returns an array with 14 elements});

foreach my $name (@names) {
ok(PDF::API2::Resource::Font::CoreFont->is_standard($name),
qq{$name is a core font});
}

done_testing();

0 comments on commit a541d60

Please sign in to comment.