PDF::Builder::FontManager(3pm) | User Contributed Perl Documentation | PDF::Builder::FontManager(3pm) |
PDF::Builder::FontManager - Managing the font library for PDF::Builder
These 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
"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)
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.
%info items to be defined:
On Windows systems, there are an additional 14 core fonts which are normally loaded. These are Georgia, Verdana, Trebuchet, Wingdings, and Webdings faces. Use caution if making use of these additional core fonts, as non-Windows systems may not include them without explicit manual installation of these fonts. These fonts may be safe to use if you know that all your PDF readers will be running on Windows systems.
It is assumed that the font metrics file (.afm or .pfm) has the same base file name as the glyph file (.pfa or .pfb), is found in the same directory, and either can work with either. If you have need for a different directory, a different base name, or a specific metrics file to go with a specific glyph file, let us know, so we can add such functionality. Otherwise, you will need to directly use the "psfont()" method in order to specify such different paths.
Currently, all fonts may use any single byte encoding you desire (the default is CP-1252). Only TTF type fonts (which includes OTF and CJK fonts) may currently specify a multibyte encoding such as utf8. Needless to say, the text data that you pass to text routines must conform to the given encoding. You are not forced to use the same encoding for all defined fonts, but if you wish to mix-and-match encodings, it is up to you to define your text that uses the encoding specified for the particular font used!
Note in particular when you use entities that (if numeric) they are given in the Unicode number. When out of the single byte range (x00-xFF), results are unpredictable if you give an entity that does not fall within the encoding's range! Also check results for Unicode points within the range x80-xFF if you are using utf8 encoding.
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()".
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:
If you give the "face" entry, the other settings ("italic", "bold", etc.) are not reset, unless it is impossible to use the existing setting. If you do not give the "face" entry, the current entry will be updated (bold, italic switched on/off, etc.). You may always explicitly give current to make it clear in your code that you don't want to change the face.
2023-01-24 | perl v5.36.0 |