PDF glyph spacing and TJ operator
Asked Answered
V

2

1

I am new to PDF, and i want to manipulate the space between the characters in a file. I have read the PDFReference and understood some of it. Now, the problem I have is how to calculate the spaces for the text rendering.

I have for example:

1 0 0 1 0 188.28799438 cm
BT 
/F2 11.04 Tf
1 0 0 -1 0 9.38000011 Tm 
(Some)Tj ( )Tj 
21.24200058 0 Td 
(text)Tj ( )Tj 

Which I want to turn into this:

1 0 0 1 0 188.28799438 cm
BT 
/F2 11.04 Tf
1 0 0 -1 0 9.38000011 Tm 
[(S)10(o)10(m)10(e)( )]TJ 
21.24200058 0 Td 
[(t)10(e)10(x)10(t)( )]TJ 

To add the spaces and then be able to manipulate them. However I was wondering how to calculate the ctm and the line matrix with those added values.

I know that we concatenate cm with the previous one.

cm2 x cm1

The Tms are not concatenated Tm2 replaces Tm1.

I am stuck with the td operator and the new spaces I added. Any clue?

Vendee answered 29/8, 2018 at 13:18 Comment(4)
Probably an aside - your TJ instruction arguments must be collected in an array [...].Countermarch
yes thank you! I corrected itVendee
Concerning your actual question "However I was wondering how to calculate the ctm and the line matrix with those added values. [...] Any clue?" - As Rob's answer explains, neither the current transformation matrix nor the text line matrix are influenced by those new gaps at all. If this does not help you yet, please explain in more detail what you are looking for.Countermarch
thank you for your patience lol. I think i should ask a more specific question as you said. I basically need to have info on the glyph coordinates. and since they are relative not absolute, i'm struggling to calculate them.Vendee
C
2

As clarified in comments, the OP is not asking for effects of the TJ numbers on the current transformation matrix or text line matrix but instead on the text matrix Tm.

This is explained in the specification ISO 32000-1 (and equivalently in ISO 32000-2) in section 9.4.4 Text Space Details: After drawing a glyph (probably followed by a number in a TJ instruction array argument), the text matrix shall be updated as follows:

assignment new Tm

In horizontal mode tx is the displacement and ty is zero, in vertical mode tx is zero and ty is the displacement. The applicable value is calculated as

determination tx and ty

I.e. if you do this calculation while processing a TJ instruction and there is a number following the character code for the currently drawn glyph, that number is considered here as the parameterTj.

Thus, if you want to determine the displacement caused by a number element of a TJ array argument alone — e.g. if the first element in the TJ array argument is a number or if there are multiple consecutive number elements in the TJ array argument and you want to know the effect of each one — the above reduces to

tx = (−Tj / 1000) × Tfs × Th

ty = (−Tj / 1000) × Tfs

Countermarch answered 30/8, 2018 at 10:50 Comment(2)
Thank you. tx = (−Tj / 1000) × Tfs × Th , here Tfs = 11.04 and Th = 1?Vendee
Yes, Tfs is 11.04. And unless there is a Tz instruction with an argument other than 100 preceding the content lines you show, Th is 1Countermarch
R
4

If you're working with horizontal text and only want to control the spacing between glyphs with the TJ operator, you don't need to worry about adding those values to the current transformation matrix or line matrix.

  • The CTM (current transformation matrix) is a master matrix that maps user space coordinates to output device coordinates; for each glyph, it is concatenated with other parameters to make a temporary text rendering matrix to position the glyph, but the CTM does not accumulate changes as glyphs are positioned (see 9.4.4 'Text Space Details' in the PDF 32000 reference)
  • The line matrix captures the value of the initial text matrix at the beginning of a line of a text; it's really only used for matching the vertical position of lines of text and isn't affected by the spacing between glyphs (see 9.4.2 'Text Positioning Operators')
Relict answered 29/8, 2018 at 19:56 Comment(3)
Thank you! So basically what I did won't affect the Tm and CTM. The Text rendering matrix however changes right? because it is calculated when each glyph is drawn.Vendee
"So basically what I did won't affect the Tm and CTM." - One moment, in your question you mentioned the text line matrix, not the text matrix. The text matrix is the matrix influenced by your numbers! The specification on the TJ array argument elements: If it is a number, the operator shall adjust the text position by that amount; that is, it shall translate the text matrix, Tm.Countermarch
Oh I thought they were the same thing. So the Tlm stays the same but the Tm is adjusted. We basically do 10*[1 0 0, -1 0 0, 0 9.38000011 1]?Vendee
C
2

As clarified in comments, the OP is not asking for effects of the TJ numbers on the current transformation matrix or text line matrix but instead on the text matrix Tm.

This is explained in the specification ISO 32000-1 (and equivalently in ISO 32000-2) in section 9.4.4 Text Space Details: After drawing a glyph (probably followed by a number in a TJ instruction array argument), the text matrix shall be updated as follows:

assignment new Tm

In horizontal mode tx is the displacement and ty is zero, in vertical mode tx is zero and ty is the displacement. The applicable value is calculated as

determination tx and ty

I.e. if you do this calculation while processing a TJ instruction and there is a number following the character code for the currently drawn glyph, that number is considered here as the parameterTj.

Thus, if you want to determine the displacement caused by a number element of a TJ array argument alone — e.g. if the first element in the TJ array argument is a number or if there are multiple consecutive number elements in the TJ array argument and you want to know the effect of each one — the above reduces to

tx = (−Tj / 1000) × Tfs × Th

ty = (−Tj / 1000) × Tfs

Countermarch answered 30/8, 2018 at 10:50 Comment(2)
Thank you. tx = (−Tj / 1000) × Tfs × Th , here Tfs = 11.04 and Th = 1?Vendee
Yes, Tfs is 11.04. And unless there is a Tz instruction with an argument other than 100 preceding the content lines you show, Th is 1Countermarch

© 2022 - 2024 — McMap. All rights reserved.