Post without Account — your post will be reviewed, and if appropriate, posted under Anonymous. You can also use this link to report any problems registering or logging in.

CTS 31 - How to find limits of textlabel?

  • 3 Replies
  • 68 Views
*

Offline stueber

  • Newbie
  • *
  • 2
    • View Profile
    • biolib
CTS 31 - How to find limits of textlabel?
« July 10, 2020, 07:37:17 AM »
Hello,

sorry for the beginners question:

When I use the textlabel command:

$text->textlabel(50, 100, $font, 10, "Hello World", -hscale=>80 );

How can I find out where on the paper the right end of this text will be?
I would like to know, so that other elements of the page do not overlap or
collide with the text.

greetings
Kurt
« Last Edit: July 10, 2020, 01:10:04 PM by Phil »

*

Offline Phil

  • Global Moderator
  • Hero Member
  • *****
  • 697
    • View Profile
Re: CTS 31 - How to find limits of textlabel?
« Reply #1: July 10, 2020, 01:09:27 PM »
The textlabel() call does not appear to have been intended for anything more than one-off labels. I will take a look at it to see if I can do anything about its leaving the "next write" position defined (@where = $text->textpos();). In the meantime, you can either

  • use the text width returned by textlabel() and add it to your "x" coordinate
  • use $text->text("Hello World"); instead, which leaves the "next write" position defined

An example of both:
Code: [Select]
use warnings;
use strict;
use PDF::Builder;

my $pdf = PDF::Builder->new();
my $page = $pdf->page();
my $text = $page->text();
my $font = $pdf->corefont("times-roman");

# output via textlabel()
my $w = $text->textlabel(50,100, $font,10, "Hello World (textlabel)", -hscale=>80);
$text->textlabel(50+$w,100, $font,10, "Next write here", -hscale=>80);

# output via text() or text_left()
$text->font($font, 10);
$text->translate(50,200);
$text->hscale(80);
$text->text("Hello World (text)");
$text->text("Next write here");

$pdf->saveas("where.pdf");

There is also $text->advancewidth("text") to tell you the width of a slug of text, in the current font and size.

I will move this to the bugs section soon, as either the code or the documentation should be updated.

*

Offline stueber

  • Newbie
  • *
  • 2
    • View Profile
    • biolib
Re: CTS 31 - How to find limits of textlabel?
« Reply #2: July 12, 2020, 04:33:32 PM »
Dear Phil,

Thanks a lot for your elaborate answer. I tested the program snippet you
provided and it works fine for me  :)

Greetings
Kurt

*

Offline Phil

  • Global Moderator
  • Hero Member
  • *****
  • 697
    • View Profile
Re: CTS 31 - How to find limits of textlabel?
« Reply #3: July 16, 2020, 10:53:19 AM »
I don't see a clean way to change the behavior of textlabel() to make it play nicely with other text operations, so I have updated the documentation to clarify that it is a standalone operation.

Closing.