0

I have some text that I want to edit (justified text, really annoying), so I was wondering if this:

BT /FAAABA 10 Tf 
1 0 0 -1 0 9.38000011 Tm 
(Some) Tj
36.77199936 0 Td 
(text) Tj 
38.4280014 0 Td 
(stuff) Tj
33.42799759 0 Td

...

is equivalent to this:

BT
/FAAABA 10 Tf
1 0 0 -1 0 9.38000011 Tm
[(Some)-36.77199936*1000(text)-38.4280014*1000(stuff)-33.42799759*1000] TJ
...
  • The **TJ** array parameter may contain strings and numbers but not expressions like `-36.77199936*1000`. – mkl Sep 06 '18 at 16:01
  • Yes, I just meant the conversion since TJ numbers are expressed in thousandths of units of text space and Td are not. – maybefreedom Sep 07 '18 at 08:49

1 Answers1

0

Assuming horizontal text we determined in my answer to your previous question that the horizontal displacement tx corresponding to a number Tj in a TJ array can be calculated as

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

where Tfs is the current font size and Th is the current horizontal scaling factor.

Thus, if you have a horizontal displacement tx and want to calculate the corresponding number Tj for a TJ array, you simply resolve the equation above to:

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

BUT this is not exactly the situation in your case because Td does not simply shift the text matrix by its parameters but instead shifts the text line matrix by them and sets the text matrix to the new text line matrix value:

tx ty Td Move to the start of the next line, offset from the start of the current line by (tx, ty). tx and ty shall denote numbers expressed in unscaled text space units. More precisely, this operator shall perform these assignments:

enter image description here

(ISO 32000-1, Table 108 – Text-positioning operators)

Thus, the tx parameter of Td is not the tx to put into the equation above but you instead have to subtract the width of the text drawn since the last setting of the text line matrix.

So to transform your example

BT /FAAABA 10 Tf 
1 0 0 -1 0 9.38000011 Tm 
(Some) Tj
36.77199936 0 Td 
(text) Tj 
38.4280014 0 Td 
(stuff) Tj
33.42799759 0 Td

into a

BT
/FAAABA 10 Tf
1 0 0 -1 0 9.38000011 Tm
[(Some) NUM1 (text) NUM2 (stuff) NUM3] TJ

form, you calculate the numeric values NUM1, NUM2, and NUM3 like this:

NUM1 = -1000 × (36.77199936 - width("Some")) / (Tfs × Th)

NUM2 = -1000 × (38.4280014 - width("text")) / (Tfs × Th)

NUM3 = -1000 × (33.42799759 - width("stuff")) / (Tfs × Th)

When calculating the widths of those strings remember to take the font size, the character spacing, and the horizontal scaling into account!

And even then the two forms are not identical because the text line matrix at the end differs.

mkl
  • 90,588
  • 15
  • 125
  • 265
  • Thank you! This was very informative! Now to get the string width, I'll have to get the glyphs' widths, is it possible to use the FontBBox [x1 y1 x2 y2] and do x2-x1? – maybefreedom Sep 07 '18 at 12:26
  • @maybefreedom *"I'll have to get the glyphs' widths, is it possible to use the FontBBox [x1 y1 x2 y2] and do x2-x1?"* - The font boundary box can only be used if all characters of a font have the same width. Otherwise you need the individual widths for each character code. – mkl Sep 07 '18 at 13:16
  • Okay. Thank you for your help! – maybefreedom Sep 07 '18 at 13:23