I gone through different solutions on CTM matrix calculations(someof them are this and this).
What I know about content stream is when "q " encounters we need to push identity matrix in a graphics_stack and keep multiply with next position operator(cm , Tm, Td, TD) CTM. When "Q" encounters we need to pop the last matrix.
For text positioning parsing when "BT" encounters push in identity matrix in position_stack and keep multiply with next position operator(cm , Tm, Td, TD) CTM. When "ET" encounters we need to pop the last matrix.
Here the some times we need to multiply with last CTM matrix and some times just multiply with identity matrix. When these cases are occurs?
Case 1:
From as shown in image 1 and 2 case merely replacement of new matrix from Td to Tm. And from 2 to 3 it's again last CTM multiplication. How I know?(By visually I can tell by looks)
Case 2:
In this case which how the matrix pushing and calculation will be there ?
Case 3:
BT
TT_1 20 Tf
35.56 150.24533 Td _______________ 1
(some sample text) Tj
50.526 250.36 Td ________________ 2
(second line new replace) Tj
0 -16.2 Td _____________________ 3
(Line end.) Tj
ET
This case 1 and 2 are merely replacement, 2 and 3 previous matrix multiplication. How do I know?
Case 4:
Please parse these positions at least till 10. The source file of this pdf
Case 5:
In above one need to calculate the l position. I highlighted with 1 to 4 numbers. I need to calculate the positions of each and every l How can do that? pdf
case6:
What is the change in calculation when the page(pdf) is rotated to 90 or 180 or 270 and 315 degrees?
These are some cases what I saw. What else might cases I can encounter and What is the generic approach to solve this ?
First of all, I get the impression you mix up two different aspects. You have the current transformation matrix (CTM) and you have the text and text line matrices. The CTM is subject to cm, q, and Q. The text and text line matrices are subject to BT, Tm, Td, ... And to determine the exact position and direction of drawn text you need the product of text matrix and CTM at the time that text is drawn.
How those operators change the matrices, is described in the PDF specification, ISO 32000 part 1 or 2.
tx ty Td: Move to the start of the next line, offset from the start of the current line by (tx, ty). More precisely, this operator shall perform these assignments:
a b c d e f Tm: Set the text matrix, Tm, and the text line matrix, Tlm:
Furthermore, TD, T*, ', and " operate on Tm and Tlm in a way specified using the Td operator.
Whenever a glyph is drawn, its entire transformation from text space may be represented by a text rendering matrix, Trm:
where Tfs is the current font size, Th is the current horizontal scaling factor, and Trise is the current text rise value.
After drawing that glyph, Tm is updated according to the glyph displacement
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
In the following paragraphs I use rounded values to concentrate on the essentials.
The CTM starts as the Identity matrix, and as there is no cm operation here, it remains so all the time. Tm and Tlm on the other hand do change:
BT
Tm and Tlm are both set to the identity matrix
/GS0 gs /T1_0 10 Tf
No change to Tm or Tlm.
317 65 Td
This multiplies a translation matrix as described above from the left to the former value of the Tlm and sets Tm and Tlm to the result:
1 0 0 1 0 0 1 0 0
T = T = 0 1 0 * 0 1 0 = 0 1 0
m lm 317 65 1 0 0 1 317 65 1
As that former value was the identity, this may look like a replacement by the Td translation matrix but it actually is a multiplication.
(F)Tj
This draws a glyph transformed by the text rendering matrix
10 × 1 0 0 1 0 0 1 0 0 10 0 0
T = 0 10 0 * 0 1 0 * 0 1 0 = 0 10 0
rm 0 0 1 317 65 1 0 0 1 317 65 1
Thereafter Tm is updated as described above. Unfortunately we don't have the widths of the font T1_0, so we cannot calculate the updated value.
1 0 0 1 370 87 Tm
This sets the text matrix, Tm, and the text line matrix, Tlm, to the given matrix:
1 0 0
T = T = 0 1 0
m lm 370 87 1
So now we know the current Tm value again.
-47 -22 Td
This multiplies a translation matrix as described above from the left to the former value of the Tlm and sets Tm and Tlm to the result:
1 0 0 1 0 0 1 0 0
T = T = 0 1 0 * 0 1 0 = 0 1 0
m lm -47 -22 1 370 87 1 323 65 1
[(igure)-251(2.3:)-621(P)16...] TJ
This draws the strings in the argument array with a starting text rendering matrix of
10 × 1 0 0 1 0 0 1 0 0 10 0 0
T = 0 10 0 * 0 1 0 * 0 1 0 = 0 10 0
rm 0 0 1 323 65 1 0 0 1 323 65 1
updating Tm again and again as described above.
In the following paragraphs I use rounded values to concentrate on the essentials.
The CTM starts as the Identity matrix.
q
This saves the current graphics state, including the current CTM. As there is no Q operation in the example, though, we can ignore that for now.
.24 0 0 .24 91 740 cm
This updates the CTM by the given matrix:
0.24 0 0 1 0 0 0.24 0 0
CTM = 0 0.24 0 * 0 1 0 = 0 0.24 0
91 740 1 0 0 1 91 740 1
BT
Tm and Tlm are both set to the identity matrix
133 0 0 133 0 0 Tm
This sets the text matrix, Tm, and the text line matrix, Tlm, to the given matrix:
133 0 0
T = T = 0 133 0
m lm 0 0 1
/TT1.0 1 Tf .002 Tc
No change to CTM, Tm, or Tlm.
[(The)1( )1(Long )1(Tai)1(l)]TJ
This draws the strings in the argument array with a starting text rendering matrix of
1 × 1 0 0 133 0 0 0.24 0 0 32 0 0
T = 0 1 0 * 0 133 0 * 0 0.24 0 = 0 32 0
rm 0 0 1 0 0 1 91 740 1 91 740 1
updating Tm again and again as described above.
BT
TT_1 20 Tf
35.56 150.24533 Td _______________ 1
(some sample text) Tj
50.526 250.36 Td ________________ 2
(second line new replace) Tj
0 -16.2 Td _____________________ 3
(Line end.) Tj
ET
In the following paragraphs I use rounded values to concentrate on the essentials.
The CTM starts as the Identity matrix, and as there is no cm operation here, it remains so all the time. Tm and Tlm on the other hand do change:
BT
Tm and Tlm are both set to the identity matrix
TT_1 20 Tf
No change to Tm or Tlm.
36 150 Td
This multiplies a translation matrix as described above from the left to the former value of the Tlm and sets Tm and Tlm to the result:
1 0 0 1 0 0 1 0 0
T = T = 0 1 0 * 0 1 0 = 0 1 0
m lm 36 150 1 0 0 1 36 150 1
As that former value was the identity, this may look like a replacement by the Td translation matrix but it actually is a multiplication.
(some sample text) Tj
This draws glyphs transformed by the text rendering matrix
20 × 1 0 0 1 0 0 1 0 0 20 0 0
T = 0 20 0 * 0 1 0 * 0 1 0 = 0 20 0
rm 0 0 1 36 150 1 0 0 1 36 150 1
Thereafter Tm is updated as described above. Unfortunately we don't have the widths of the font TT_1, so we cannot calculate the updated value.
51 250 Td
This multiplies a translation matrix as described above from the left to the former value of the Tlm and sets Tm and Tlm to the result:
1 0 0 1 0 0 1 0 0
T = T = 0 1 0 * 0 1 0 = 0 1 0
m lm 51 250 1 36 150 1 87 400 1
So now we know the current Tm value again.
(second line new replace) Tj
This draws glyphs transformed by the text rendering matrix
20 × 1 0 0 1 0 0 1 0 0 20 0 0
T = 0 20 0 * 0 1 0 * 0 1 0 = 0 20 0
rm 0 0 1 87 400 1 0 0 1 87 400 1
Thereafter Tm is updated as described above. Unfortunately we don't have the widths of the font TT_1, so we cannot calculate the updated value.
0 -16 Td
This multiplies a translation matrix as described above from the left to the former value of the Tlm and sets Tm and Tlm to the result:
1 0 0 1 0 0 1 0 0
T = T = 0 1 0 * 0 1 0 = 0 1 0
m lm 0 -16 1 87 400 1 87 384 1
So now we know the current Tm value again.
(Line end.) Tj
This draws glyphs transformed by the text rendering matrix
20 × 1 0 0 1 0 0 1 0 0 20 0 0
T = 0 20 0 * 0 1 0 * 0 1 0 = 0 20 0
rm 0 0 1 87 384 1 0 0 1 87 384 1
Thereafter Tm is updated as described above. Unfortunately we don't have the widths of the font TT_1, so we cannot calculate the updated value.
We discussed example 4 in your answer and the comments to it.
q 0.1 0 0 0.1 0 0 cm
/R108 gs
0 g
q
...
Q
0 0 1 rg
q
...
Q
4.05 w
0 G
722.023 4082.13 m
722.023 4490.28 l
S
723.961 4488.25 m
2872.98 4488.25 l
S
404.1 w
0 0 0.199951 0 K
723.961 4284.18 m
2872.98 4284.18 l
S
4.05 w
0 G
720 4080.2 m
2876.94 4080.2 l
S
2874.91 4082.13 m
2874.91 4490.28 l
S
0 g
q
Why does your view of those instructions show the numbers inaccurately? The above is copy&pasted from the stream contents, there is no need to change the numbers like that...
q 0.1 0 0 0.1 0 0 cm
Sets the CTM to
0.1 0 0
CTM = 0 0.1 0
0 0 1
/R108 gs ... 0 G
Nothing happens to the CTM
722.023 4082.13 m 722.023 4490.28 l S
We have to apply the CTM to these coordinates
0.1 0 0
[722.023 4082.13] * 0 0.1 0 = [72.2023 408.213]
0 0 1
0.1 0 0
[722.023 4490.28] * 0 0.1 0 = [72.2023 449.028]
0 0 1
Thus, a line is stroked from 72.2023,408.213 to 72.2023,449.028.
723.961 4488.25 m 2872.98 4488.25 l S
Just like above, a line is drawn from 72.3961,448.825 to 287.298,448.825.
404.1 w 0 0 0.199951 0 K 723.961 4284.18 m 2872.98 4284.18 l S
And again, a line is drawn from 72.3961,428.418 to 287.298,428.418. The only notable thing here is that the line is quite wide, ca. 40 units, so this "line" actually looks more like a filled rectangle and represents the background of the text box with the Lorentz force characterization.
4.05 w 0 G 720 4080.2 m 2876.94 4080.2 l S
Another line is drawn, narrow again and, therefore, looking like a line, this time from 72,408.02 to 287.694 408.02.
2874.91 4082.13 m 2874.91 4490.28 l S
And finally the last line, this time from 287.491,408.213 to 287.491,449.028.