|
NAMEPDF::Builder::FontManager - Managing the font library for PDF::Builder SYNOPSISThese routines are called from the PDF::Builder class (see get_font(), add_font() methods). # core fonts come pre-loaded
# Add new a new font face and variants
my $rc = $pdf->add_font(
'face' => $unique_face_name, # font family, e.g., Times
'type' => 'core', # note that core fonts preloaded
'style' => 'serif', # also sans-serif, script (cursive),
# and symbol
'width' => 'proportional', # also constant
'settings' => { 'encode' => $encoding },
# note that these are actual core font names rather than file paths
'file' => { 'roman' => 'Times-Roman',
'italic' => 'Times-Italic',
'bold' => 'Times-Bold',
'bold-italic' => 'Times-BoldItalic' },
# for non-core these would be the actual file paths
# prefixed with font search paths
);
$rc = $pdf->add_font(
'face' => 'DejaVuSans', # Deja Vu sans serif family
'type' => 'ttf', # otf uses 'ttf'
'style' => 'sans-serif',
'width' => 'proportional',
'settings' => { 'encode' => 'utf8' },
# the defined font paths will be prepended to find the actual path
'file' => { 'roman' => 'DejaVuSans.ttf',
'italic' => 'DejaVuSans-Oblique.ttf',
'bold' => 'DejaVuSans-Bold.ttf',
'bold-italic' => 'DejaVuSans-BoldOblique.ttf' }
);
Some of the global data, which can be reset via the font_settings() method: * default-face: initialized to Times-Roman (core), used if you start
formatting text without explicitly setting a face
* default-serif: initialized to Times-Roman (core), used if you want
a "generic" serif typeface
* default-sansserif: initialized to Helvetica (core), used if you want
a "generic" sans-serif typeface
* default-constant: initialized to Courier (core), used if you want
a "generic" constant-width typeface
* default-script: NOT initialized (no default), used if you want
a "generic" script (cursive) typeface
* default-symbol initialized to Symbol (core), used if you want
a "generic" symbol typeface
* font-paths: C:/Windows/Fonts for Windows systems for TTF, other types
are in non-standard paths, and for non-Windows, anything goes
Usage of get_font() is to specify the face and variants, and then each time, specify italic and bold to be on or off. If the desired file is not yet opened, it will be, and the $font returned. If the font was already created earlier, the saved $font will be returned. my $font = $pdf->get_font(
'face' => 'Times',
'italic' => 0, # desire Roman (upright)
'bold' => 0, # desire medium weight
);
# if $font is undef, we have a problem...
$text->font($font, $font_size);
$text->... # use this font (medium weight Times-Roman core font)
$font = $pdf->get_font('italic' => 1);
$text->... # switched to italic
$font = $pdf->get_font('italic' => 0);
$text->... # back to Roman (upright) text
METHODSnewPDF::Builder::FontManager->new(%opts) This is called from Builder.pm's
new(). Currently there are no options selectable. It
will set up the font manager system and preload it with the core fonts.
Various defaults will be set for the face (core Times-Roman), serif face (core
Times-Roman), sans-serif face (core Helvetica), constant width (fixed pitch)
face (core Courier), and a symbol font (core Symbol). There is no default for
a script (cursive) font unless you set one using the
font_settings() method.
font_settings @list = $pdf->font_settings() # Get
$pdf->font_settings(%info) # Set
Get or set some information about fonts, particularly the
fonts to be used for "generic" purposes.
"Get" returns a list (array) of the default font face name, the default generic serif face, the default generic sans-serif face, the default generic constant width face, the default generic symbol face, and the default generic script (cursive) face. It is possible for an element to be undefined (e.g., the generic script face is "undef"). "Set" changes one or more default settings: 'font' => face to use for the default font face (initialized to Times)
'serif' => face to use for the generic serif face (initialized to Times)
'sans-serif' => face to use for the generic sans serif face
(initialized to Helvetica)
'constant' => face to use for the generic constant width face
(initialized to Courier)
'script' => face to use for the generic script face (uninitialized)
'symbol' => face to use for the generic symbol face
(initialized to Symbol)
add_font_path $rc = $pdf->add_font_path("a directory path", %opts)
This method adds one search path to the list of paths to
search. In the get_font() method, each defined search
path will be prepended to the "file" entry
(except for core fonts) in turn, until the font file is found. However, if the
"file" entry starts with / or ./ or ../, it
will be used alone. A "file" entry starting
with .../ is a special case, which is turned into ../ before the search path
is prepended. This permits you to give a search path that you expect to move
up one or more directories.
The font path search list always includes the current directory (.), and is initialized in "Builder.pm" as @font_path. For the Windows operating system, "/Windows/Fonts" usually contains a number of TTF fonts that come standard with Windows, so it is added by default. Anything else, including all Linux (and other non-Windows operating systems), will have to be added depending on your particular system. Some common places are: Windows (NOTE: use / and not \\ in Windows paths!). Linux systems may or may not handle spaces in directory names gracefully! /Windows/Fonts
/WinNT/Fonts
/Program Files/MikTex 2.9/fonts/type1/urw/bookman (URW Bookman for MikTex)
/Program Files (x86)/MikTex 2.9/fonts/type1/urw/bookman (older versions)
/Program Files/Adobe/Acrobat DC/Resource/CIDFont (Adobe Reader fonts)
GhostScript may have its own directories
Note that directory names with spaces (e.g., "/Program Files") may not play nice with some Linux systems, so they are not included by default. Linux, etc. /usr/share/fonts (common base)
/usr/local/share/fonts (common base)
/usr/share/fonts/dejavu-sans-fonts (Deja Vu Sans TTF specifically)
/usr/share/fonts/truetype/ttf-dejavu
/usr/share/fonts/truetype/dejavu
/usr/lib/defoma/gs.d/devs/fonts (GhostScript?)
/usr/share/fonts/type1/gsfonts (GhostScript PS)
/usr/share/X11/fonts/urw-fonts (URW PS)
Third-party application installations, such as Adobe's Acrobat Reader, may be a source of installed fonts, too. A return code of 0 means the path was successfully added, while 1 means there was a problem encountered (and a message was issued). No options are currently defined. add_font$rc = add_font(%info) Add a new font entry (by face and variants) to the Font
Manager's list of known fonts.
%info items to be defined:
You can give the entire path of the font's source file, in an absolute path, if you wish. However, it's usually less typing to use add_font_path() to specify a list of font directories to search, and only give the name (and perhaps a subdirectory) for the path here in add_font(). get_font @current = $pdf->get_font() # Get
$font = $pdf->get_font(%info) # Set
If no parameters are given
("@current = $pdf->get_font()"), a list
(array) is returned giving the current font setting: face name,
italic flag 0/1, bold flag 0/1, type ('core', 'ttf',
etc.), a hash reference of settings, such as the encoding
('utf8', etc.), style value, width value, and an array reference
(list) of variants (roman, bold, etc.). If no font has yet been explicitly
set, the current font will be the default font.
If at least one parameter is given (%info hash), the font manager will attempt to discover the appropriate font (from within the font list), load it if not already done, and return the $font value. If undef is returned, there was an error. %info fields:
dump_font_tables$pdf->dump_font_tables() Print (to STDOUT) all the Font Manager font information
on hand.
|