LibPDF: Mark text rendering matrix as dirty after TJ numbers

Mostly because I audited all places that assigned to `m_text_matrix`
after #22760.

This one is very difficult to trigger in practice.

`show_text()` marks the text rendering matrix dirty already,
so this only has an effect if the `TJ` array starts with a
number, and the matrix isn't marked dirty going in.

`Tm` caches the text rendering matrix, so I changed text.pdf
to contain:

```
1 0 0 1 45 130 Tm
[ 200 (Hello) -2000 (World) ] TJ T*
```

This first sets an x offset of 5 (on top of the normal 40), and
then undoes it (`200` is multiplied by font size (25) / -1000,
and `200 * 25 / -1000` is -5). Before this change, the topmost
"Hello World" ended up slightly indented.

Likely no behavior change in practice, but makes the code easier
to understand, and maybe it helps in the wild somewhere.
This commit is contained in:
Nico Weber 2024-01-14 21:36:50 -05:00 committed by Sam Atkins
parent f23f5dcd62
commit 9a93f677f4
2 changed files with 16 additions and 40 deletions

View file

@ -2,77 +2,51 @@
%µ¶ %µ¶
1 0 obj 1 0 obj
<< <</Type/Catalog/Pages 2 0 R>>
/Type /Catalog
/Pages 2 0 R
>>
endobj endobj
2 0 obj 2 0 obj
<< <</Type/Pages/Kids[3 0 R]/Count 1>>
/Type /Pages
/Kids [ 3 0 R ]
/Count 1
>>
endobj endobj
3 0 obj 3 0 obj
<< <</Type/Page/Parent 2 0 R/MediaBox[0 0 525 250]/Contents 4 0 R/Resources<</Font<</F1 5 0 R>>>>>>
/Type /Page
/Parent 2 0 R
/MediaBox [ 0 0 525 250 ]
/Contents 4 0 R
/Resources <<
/Font <<
/F1 5 0 R
>>
>>
>>
endobj endobj
4 0 obj 4 0 obj
<< <</Length 291>>
/Length 236
>>
stream stream
BT BT
/F1 24 Tf /F1 25 Tf
-30 TL -30 TL
40 40 Td 40 40 Td
[ (Hello) -2000 (World) ] TJ T* [ (Hello) -2000 (World) ] TJ T*
[ (Hello) -1000 -1000 (World) ] TJ T* [ (Hello) -1000 -1000 (World) ] TJ T*
[ (Hello) -1000 ] TJ [ -1000 ] TJ [ (World) ] TJ T* [ (Hello) -1000 ] TJ [ -1000 ] TJ [ (World) ] TJ T*
1 0 0 1 45 130 Tm [ 200 (Hello) -2000 (World) ] TJ T*
(should be the same on all lines:) ' (should be the same on all lines:) '
(The distance between "Hello" and "World") ' (The distance between "Hello" and "World") '
ET ET
endstream endstream
endobj endobj
5 0 obj 5 0 obj
<< <</Type/Font/Subtype/Type1/Name/F1/BaseFont/Helvetica/Encoding/MacRomanEncoding>>
/Type /Font
/Subtype /Type1
/Name /F1
/BaseFont /Helvetica
/Encoding /MacRomanEncoding
>>
endobj endobj
xref xref
0 6 0 6
0000000000 65536 f 0000000000 65536 f
0000000016 00000 n 0000000016 00000 n
0000000070 00000 n 0000000062 00000 n
0000000136 00000 n 0000000114 00000 n
0000000291 00000 n 0000000227 00000 n
0000000581 00000 n 0000000568 00000 n
trailer trailer
<< <</Size 6/Root 1 0 R>>
/Size 6
/Root 1 0 R
>>
startxref startxref
700 666
%%EOF %%EOF

View file

@ -572,9 +572,11 @@ RENDERER_HANDLER(text_show_string_array)
if (element.has<int>()) { if (element.has<int>()) {
float shift = (float)element.get<int>() / 1000.0f; float shift = (float)element.get<int>() / 1000.0f;
m_text_matrix.translate(-shift * text_state().font_size * text_state().horizontal_scaling, 0.0f); m_text_matrix.translate(-shift * text_state().font_size * text_state().horizontal_scaling, 0.0f);
m_text_rendering_matrix_is_dirty = true;
} else if (element.has<float>()) { } else if (element.has<float>()) {
float shift = element.get<float>() / 1000.0f; float shift = element.get<float>() / 1000.0f;
m_text_matrix.translate(-shift * text_state().font_size * text_state().horizontal_scaling, 0.0f); m_text_matrix.translate(-shift * text_state().font_size * text_state().horizontal_scaling, 0.0f);
m_text_rendering_matrix_is_dirty = true;
} else { } else {
auto str = element.get<NonnullRefPtr<Object>>()->cast<StringObject>()->string(); auto str = element.get<NonnullRefPtr<Object>>()->cast<StringObject>()->string();
TRY(show_text(str)); TRY(show_text(str));