ssimms / pdfapi2

Create, modify, and examine PDF files in Perl
Other
15 stars 20 forks source link

Puzzled by text position #44

Closed sciurius closed 2 years ago

sciurius commented 2 years ago

On a new page

$text->position(72, 720);
( $x, $y ) = $text->position;
$text->text("Hello, World! @ $x,$y");

The docs say that position moves to the start of the current line of text, offset by $x and $y. The text "Hello World! @ 72,720" is placed at position 72,720 on the page. So apparently the beginning of the current line is initially at 0,0. After setting the position, the current line now starts at 0,720.

Then

$text->position(0, -100);
( $x, $y ) = $text->position;
$text->text("Hello, World! @ $x,$y");

The text "Hello World! @ 0,620" is placed at position 72,620 on the page.

I don't understand. I would have expected "Hello World! @ 0,620" to be placed at position 0,620. Or "Hello World! @ 72,620" placed at position 72,620. What am I missing?

ssimms commented 2 years ago

After setting the position, the current line now starts at 0,720.

If I'm understanding the spec correctly, the current line should be starting at 72,720 after the first position call, which is consistent with what you're seeing in the PDF. However, the "start of current line" coordinates don't appear to be stored in the text object, or at least not correctly. The position call (and the deprecated distance call on which it's based) store X as an absolute value rather than a relative value. It looks like this only matters after the second call to set the position.

That seems to be the case at least back as far as 2013, so I'm guessing that people weren't using the old method (textpos) for reading the position.

I've just committed a change that should resolve the problem. Let me know if that works for you, and/or if it breaks anything else.