Box

Synopsis

use lib 't';
use PDFTiny;
my $page = PDFTiny.new.add-page;
use PDF::Content;
use PDF::Content::Font::CoreFont;
use PDF::Content::Text::Block;
my PDF::Content::Font::CoreFont $font .= load-font( :family<helvetica>, :weight<bold> );
my $text = "Hello.  Ting, ting-ting. Attention! … ATTENTION! ";
my PDF::Content::Text::Box $text-box .= new( :$text, :$font, :font-size(16) );
my PDF::Content $gfx = $page.gfx;
$gfx.BeginText;
$text-box.render($gfx);
$gfx.EndText;
say $gfx.Str;

Description

Text boxes are used to implement the PDF::Content print and say methods. They usually work "behind the scenes". But can be created as objects and then passed to print and say:

use PDF::Lite;
use PDF::Content;
use PDF::Content::Text::Box;

my PDF::Lite $pdf .= new;

my $font-size = 16;
my $height = 20;
my $text = "Hello.  Ting, ting-ting.";

my PDF::Content::Font::CoreFont $font .= load-font( :family<helvetica>, :weight<bold> );
my PDF::Content::Text::Box $text-box .= new( :$text, :$font, :$font-size, :$height );

say "width:" ~ $text-box.width;
say "height:" ~ $text-box.height;
say "underline-thickness:" ~ $text-box.underline-thickness;
say "underline-position:" ~ $text-box.underline-position;

my $page = $pdf.add-page;

$page.text: {
    .text-position = 10, 20;
    .say: $text-box;
    .text-position = 10, 50;
    .print: $text-box;
}

$pdf.save-as: "test.pdf";

Methods

method text

The text contained in the text box. This is a rw accessor. It can also be used to replace the text contained in a text box.

method width

The constraining width (:$width option), or content-width for the text box.

method height

The constraining height (:$height option), or content-height, for the text box.

method indent

The indentation of the first line (points).

method align

Horizontal alignment left, center, or right.

method valign

Vertical alignment of mutiple-line text boxes: top, center, or bottom.

See also the :baseline option for vertical displacement of the first line of text.

Note that the baseline is implicitely set to the valign option. However the default for valign is top, and the default for baseline is alphabetic.

method baseline

The font baseline to use. This is similar to the HTML Canvas textBaseline property.

method baseline-shift

Vertical displacement, in scaled font units, needed to postions the font to the baseline. 0 for alphabetic baseline.

method lines

An array of PDF::Content::Text::Line objects.

methods margin-left, margin-bottom, margin-right, margin-top

These methods adjust the margin placed around a text box.

They have no direct affect on rendering, except that the PDF::Content print() and say() methods, which returns the bbox around the rendered text. Note that margins can be negative, to trim text boxes.

method offset

A two member array giving the x,y displacement of the text, by default [0, 0]. These can be set to fine-tune the positioning of the text.

method bbox

The text-boxes bounding box, including margin and offset adjustments.

The bbox() method is defined as

[ $tb.offset[0] - $tb.margin-left,   # x0
  $tb.offset[1] - $tb.margin-bottom, # y0
  $tb.offset[0] + $tb.width + $tb.margin-right, #x1
  $tb.offset[1] + $tb.height + $tb.margin-top   #y1
]

for a given paragraph.

method style

method style() returns PDF::Content::Text::Style

Styling delegate for this text box. See PDF::Content::Text::Style

This method also handles method font, font-size, leading, kern, WordSpacing, CharSpacing, HorizScaling, TextRender, TextRise, baseline-shift, space-width, underline-position, underline-thickness, font-height. For example $tb.font-height is equivalent to $tb.style.font-height.

Calculated from the longest line in the text box.

Calculated from the number of lines in the text box.

PDF::Content v0.9.11

PDF content related classes; including text, images, fonts and graphics

Authors

  • David Warring

License

Artistic-2.0

Dependencies

Base64::NativeColorCompress::Zlib::RawFont::AFM:ver<1.24.10+>Native::PackingMethod::AlsoJSON::FastPDF:ver<0.6.8+>PDF::Grammar:ver<0.3.2+>

Test Dependencies

Provides

  • PDF::Content
  • PDF::Content::API
  • PDF::Content::Canvas
  • PDF::Content::Color
  • PDF::Content::Font
  • PDF::Content::Font::CoreFont
  • PDF::Content::Font::Enc::Glyphic
  • PDF::Content::Font::Enc::Type1
  • PDF::Content::Font::Encoder
  • PDF::Content::Font::Encodings
  • PDF::Content::FontObj
  • PDF::Content::Image
  • PDF::Content::Image::GIF
  • PDF::Content::Image::JPEG
  • PDF::Content::Image::PDF
  • PDF::Content::Image::PNG
  • PDF::Content::Interface
  • PDF::Content::Matrix
  • PDF::Content::Ops
  • PDF::Content::Page
  • PDF::Content::PageNode
  • PDF::Content::PageTree
  • PDF::Content::ResourceDict
  • PDF::Content::Resourced
  • PDF::Content::Tag
  • PDF::Content::Text::Block
  • PDF::Content::Text::Box
  • PDF::Content::Text::Line
  • PDF::Content::Text::Style
  • PDF::Content::XObject
  • X::PDF::Content

The Camelia image is copyright 2009 by Larry Wall. "Raku" is a trademark of the Yet Another Society. All rights reserved.

Built with Podlite — the markup and publishing tools behind this site.