Help language development. Donate to The Perl Foundation

PDF::Font::Loader cpan:WARRINGD last updated on 2020-09-10

PDF-Font-Loader-0.3.1/

Build Status

NAME

PDF::Font::Loader

SYNPOSIS

# load a font from a file
use PDF::Font::Loader :load-font;

my $deja = PDF::Font::Loader.load-font: :file<t/fonts/DejaVuSans.ttf>;
my $deja = load-font( :file<t/fonts/DejaVuSans.ttf> );

# find/load system fonts; requires fontconfig
use PDF::Font::Loader :load-font, :find-font;
$deja = load-font( :family<DejaVu>, :slant<italic> );
my Str $file = find-font( :family<DejaVu>, :slant<italic> );
my $deja-vu = load-font: :$file;

# use the font to add text to a PDF
use PDF::Lite;
my PDF::Lite $pdf .= new;
$pdf.add-page.text: {
   .font = $deja;
   .text-position = [10, 600];
   .say: 'Hello, world';
}
$pdf.save-as: "/tmp/example.pdf";

DESCRIPTION

This module provdes font loading and handling for PDF::Lite, PDF::API6 and other PDF modules.

METHODS

load-font

A class level method to create a new font object.

PDF::Font::Loader.load-font(Str :$file);

Loads a font file.

parameters:

PDF::Font::Loader.load-font(Str :$family);

my $vera = PDF::Font::Loader.load-font: :family<vera>;
my $deja = PDF::Font::Loader.load-font: :family<Deja>, :weight<bold>, :width<condensed> :slant<italic>);

Loads a font by a fontconfig name and attributes.

Note: Requires fontconfig to be installed on the system.

parameters:

find-font

use PDF::Font::Loader
    :Weight  # thin|extralight|light|book|regular|medium|semibold|bold|extrabold|black|100..900
    :Stretch # normal|[ultra|extra]?[condensed|expanded]
    :Slant   # normal|oblique|italic
;
find-font(Str :$family,     # e.g. :family<vera>
          Weight  :$weight,
          Stretch :$stretch,
          Slant   :$slant,
          );

Locates a matching font-file. Doesn't actually load it.

my $file = PDF::Font::Loader.find-font(:family<Deja>, :weight<bold>, :width<condensed>, :slant<italic>);
say $file;  # /usr/share/fonts/truetype/dejavu/DejaVuSansCondensed-BoldOblique.ttf
my $font = PDF::Font::Loader.load-font( :$file )';

INSTALL

BUGS AND LIMITATIONS

As a work-around, font subsetting and reduction can be done manually, using the pyftsubset utlity, which is included in the Debian fonttools package.

For example, to create a latin-1 subset of the DejaVuSans font:

`$ pyftsubset t/fonts/DejaVuSans.ttf --output-file=/tmp/dejavu-latin.ttf --layout-features=\* --no-hinting --unicodes="U+0000-00FF"`

Changing the 'Hello, World' example to use this font:

# load a font from a file
use PDF::Font::Loader :load-font;

my $deja = load-font( :file</tmp/dejavu-latin.ttf> );

# use the font to add text to a PDF
use PDF::Lite;
my PDF::Lite $pdf .= new;
$pdf.add-page.text: {
   .font = $deja;
   .text-position = [10, 600];
   .say: 'Hello, world';
}
$pdf.save-as: "/tmp/example.pdf";

Reduces the file size of example.pdf from 373K to 15K.