Search code examples
pdfpdfrw

PDF glyph spacing and TJ operator


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?


Solution

  • 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