pdf-raku / PDF-Font-Loader-raku

Font loader for the PDF tool-chain
Artistic License 2.0
1 stars 3 forks source link

Is $font.height applied with $gfx.say for the resulting text position? #31

Open tbrowder opened 1 year ago

dwarring commented 1 year ago

Also not well documented yet. By default the x position is the baseline of the font. There is a :baseline option on the .print and .say methods and on text-box objects. :baseline<top>, will cause the height to be taken into account.

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

my PDF::Lite $pdf .= new;

my $page = $pdf.add-page;

$page.text: {
    .text-position = 10, 20;
    .print: '[Default]',;
    .print: '[Bottom]', :baseline<bottom>;
    .print: '[Top]', :baseline<top>;
    .print: '[Middle]', :baseline<middle>;
}

$pdf.save-as: "test.pdf";
tbrowder commented 1 year ago

Is the affect supposed to be the same as \:valign\ et al.?

dwarring commented 1 year ago

It's similar, but :baseline is always a constant shift within a line test.pdf . But :valign depends on the number of lines of text, as in the following example, which uses :width` to break the text across multiple lines and shifts the paragraphs as a whole.

use PDF::Lite;

my PDF::Lite $pdf .= new;

my $page = $pdf.add-page;

my $x = 10;
for <center top bottom> -> $valign {
    $page.text: {
        .text-position = $x, 150;
        .print: "A multi line paragraph of $valign aligned text", :$valign, :width(100);
        $x += 150;
    }
}

$pdf.save-as: "test.pdf";
tbrowder commented 1 year ago

Great example, David. Thanks.

dwarring commented 1 year ago

Starting with PDF::Content release 0.70, I've toned down :valign to only affect the text flow of multiple line text boxes. It has no affect now on a single line of text, which is now only be shifted with :baseline.

I made this change after realizing that both :baseline and :valign were "fighting over" the first line of text, allowing it to be moved by up to two lines, so that the text box could be positioned up to a full line above or below the current text position, which seems surprising.