Text::Layout(3pm) | User Contributed Perl Documentation | Text::Layout(3pm) |
Text::Layout - Pango style markup formatting
This module will cooperate with PDF::API2, PDF::Builder, Cairo, and Pango.
Text::Layout provides methods for Pango style text formatting. Where possible the methods have identical names and (near) identical behaviour as their Pango counterparts.
See <https://developer.gnome.org/pango/stable/pango-Layout-Objects.html>.
The package uses Text::Layout::FontConfig (included) to organize fonts by description.
If module HarfBuzz::Shaper is installed, Text::Layout can use it for text shaping.
Example, using PDF::API2 integration:
use PDF::API2; # or PDF::Builder use Text::Layout; # Create a PDF document. my $pdf = PDF::API2->new; # or PDF::Builder->new $pdf->mediabox( 595, 842 ); # A4, PDF units # Set up page and get the text context. my $page = $pdf->page; my $ctx = $page->text; # Create a markup instance. my $layout = Text::Layout->new($pdf); # This example uses PDF corefonts only. Text::Layout::FontConfig->register_corefonts; $layout->set_font_description(Text::Layout::FontConfig->from_string("times 40")); $layout->set_markup( q{The <i><span foreground="red">quick</span> <span size="20"><b>brown</b></span></i> fox} ); # Center text. $layout->set_width(595); # width of A4 page $layout->set_alignment("center"); # Render it. $layout->show( 0, 600, $ctx ); $pdf->saveas("out.pdf");
All PDF::API2 graphic and text methods can still be used, they won't interfere with the layout methods.
PDF::API2 and PDF::Builder render texts using the font baseline as origin.
This module typesets text in an area of possibly limited width and height. The origin is the top left of this area. Currently this area contains only a single line of text. This will change in the future when line breaking and paragraph formatting is implemented.
PDF::API2 and PDF::Builder coordinates have origin bottom left. This module produces information with respect to top left coordinates.
Pango, layered upon Cairo, uses a coordinate system that starts off top left. So for western text the direction is increasing x and increasing y.
PDF::API2 uses the coordinate system as defined in the PDF specification. It starts off bottom left. For western text the direction is increasing x and decreasing y.
Text::Layout can operate in one of two modes: convenience mode (enabled by default), and Pango conformance mode. The desired mode can be selected by calling the method set_pango_scaling().
Pango uses two device coordinates units: Pango units and device units. Pango units are 1024 ("PANGO_SCALE") times the device units.
Several methods have two variants, e.g. get_size() and get_pixel_size(). The pixel-variant uses device units while the other variant uses Pango units.
In convenience mode, this module assumes no scaling. All units are PDF device units (1/72 inch).
Device units are used for font rendering. Pango device units are 96dpi while PDF uses 72dpi.
In convenience mode this is ignored. E.g. a "Times 20" font will be of equal size in the two systems,
In Pango conformance mode you would need to specify a font size of 15360 to get a 20pt font.
Text::Layout recognizes most of the Pango markup as provided by the Pango library version 1.50 or newer. However, not everything is supported.
Note that in Pango conformance mode, the actual font size is 96/72 larger. So "45pt" gives a 60pt font.
In Pango conformance mode, rises the text by NUM units from the baseline. May be negative to lower the text.
Note: In Pango conformance mode,
"rise" does not accumulate.
Use "baseline_shift" instead.
Units are points if postfixed by pt, and a percentage of the current font size if postfixed by %.
em units are equal to the current font size, ex half the font size.
Note: This is not (yet?) part of the Pango markup standard.
Also supported but not part of the official Pango Markup specification.
Equivalent "span" attributes for shortcuts.
use Text::Layout::PDFAPI2; $layout = Text::Layout::PDFAPI2->new($pdf);
For other implementations only the above method can be used.
The argument is the context for text formatting. In the case of PDF::API2 this will be a PDF::API2 object.
The content is copied deeply, the context and fonts are copied by reference.
Note that if you have used set_markup() on this layout before, you may want to call set_attributes() to clear the attributes set on the layout from the markup as this function does not clear all attributes.
Returns undef if no text has been set.
Basically the same as length of get_text().
Returns undef if no text has been set.
The string can contain Pango-compatible markup. See <https://developer.gnome.org/pygtk/stable/pango-markup-language.html>.
Implementation note: Although all markup is parsed, not all is implemented.
$description is a Text::Layout::FontConfig object.
Returns undef if no font has been set yet.
Implementation note: Only alignment is implemented.
Implementation note: Height restrictions are not yet implemented.
Not yet implemented.
Not yet implemented.
Not yet implemented.
Not yet implemented.
A negative value of indent will produce a hanging indentation. That is, the first line will have the full width, and subsequent lines will be indented by the absolute value of indent.
The indent setting is ignored if layout alignment is set to "center".
Not yet implemented.
When placing lines with spacing, things are arranged so that
line2.top = line1.bottom + spacing
Note: By default the line height (as determined by the font) for placing lines is used. The spacing set with this function is only taken into account when the line-height factor is set to zero with set_line_spacing().
Not yet implemented.
If factor is non-zero, lines are placed so that
baseline2 = baseline1 + factor * height2
where height2 is the line height of the second line (as determined by the font(s)). In this case, the spacing set with set_spacing() is ignored.
If factor is zero, spacing is applied as before.
Not yet implemented.
Not yet implemented.
$align must be one of "left", "center", or "right",
Not yet implemented.
Not yet implemented.
Not yet implemented.
Not yet implemented.
Logical extents are usually what you want for positioning things.
Return value in scalar context is a hash ref with 4 values:
"x",
"y",
"width", and
"height" describing the logical
extents of the layout. In list context an array of two hashrefs is
returned. The first reflects the ink extents, the second the logical
extents.
In the extents, "x" will reflect the offset when text is centered or right aligned. It will be zero for left aligned text. For right aligned text, it will be the width of the layout.
"y" will reflect the offset when text is centered vertically or bottom aligned. It will be zero for top aligned text.
See also get_pixel_extents below.
Implementation note: If the PDF::API support layer cannot calculate ink, this function returns two identical extents.
The returned values are suitable for (assuming $pdf_text and $pdf_gfx are the PDF text and graphics contexts):
$layout->render( $x, $y, $pdf_text ); $box = $layout->get_pixel_extents; $pdf_gfx->translate( $x, $y ); $pdf_gfx->rect( @$box{ qw( x y width height ) } ); $pdf_gfx->stroke;
In list context, width and height are returned as an two-element list. In scalar context a hashref with keys "width" and "height" is returned.
Implementation note: This is a dummy, it returns the layout. It is provided so you can write $layout->get_iter()->get_baseline() to be compatible with the official Pango API.
Implementation note: Position is relative to top left, so due to the PDF coordinate system this is a negative number.
Note: The Python API only supports this method on iteration objects. See get_iter().
The following methods are not part of the Pango API.
bb = ( bl, x, y, width, height )
bb[0] = baseline distance from the top.
bb[1] = displacement from the left, nonzero for centered and right aligned text
bb[2] = displacement from the top, usually zero
bb[3] = advancewidth
bb[4] = height
Note that the bounding box will in general be equal to the font bounding box except for the advancewidth.
NOTE: Some fonts do not include accents on capital letters in the ascend.
If an argument is supplied and true, get_bbox() will attempt to calculate the ink extents as well, and add these as another set of 4 elements,
In list context returns the array of values, in scalar context an array ref.
Use this instead of Pango::Cairo::show_layout().
For PDF::API2, $text must be an object created by the $page->text method.
Returns the internal Pango scaling factor if enabled.
Returns the internal Pango scaling factor if conformance mode is enabled, otherwise it returns 1 (one).
Description of the Pango Markup Language: <https://docs.gtk.org/Pango/pango_markup.html#pango-markup>.
Documentation of the Pango Layout class: <https://docs.gtk.org/Pango/class.Layout.html>.
PDF::API2, PDF::Builder, HarfBuzz::Shaper, Font::TTF.
Johan Vromans, "<JV at CPAN dot org>"
Development of this module takes place on GitHub: <https://github.com/sciurius/perl-Text-Layout>.
You can find documentation for this module with the perldoc command.
perldoc Text::Layout
Please report any bugs or feature requests using the issue tracker on GitHub.
Copyright (C) 2019, Johan Vromans
This module is free software. You can redistribute it and/or modify it under the terms of the Artistic License 2.0.
This program is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose.
2024-02-23 | perl v5.38.2 |