CTM matrix multiplication with previous state vs with Identity matrix in PDF position parsing?

孤者浪人 提交于 2020-01-15 04:41:30

问题


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

These are some cases what I saw. What else might cases I can encounter and What is the generic approach to solve this ?


回答1:


Operators of interest

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.

From ISO 32000-1 Table 57 – Graphics State Operators –

  • cm: Modify the current transformation matrix (CTM) by concatenating the specified matrix
  • q: Save the current graphics state including the CTM on the graphics state stack
  • Q: Restore the graphics state including the CTM by removing the most recently saved state from the stack and making it the current state

From ISO 32000-1 Table 107 – Text object operators –

  • BT: Begin a text object, initializing the text matrix, Tm, and the text line matrix, Tlm, to the identity matrix.

From ISO 32000-1 Table 108 – Text-positioning operators –

  • txtyTd: 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.

From ISO 32000-1 section 9.4.4 – Text Space Details –

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

Example 1

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.

Example 2

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.

Example 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

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.




回答2:


Example 4 solution:

The CTM starts as the Identity matrix.

q

This saves the current graphics state (Graphics state = Identity ---- 1).

1 0 0 1 62.692 277.671 cm

This updates the CTM by the given matrix:

        1      0      0     1 0 0      1      0      0
CTM =   0      0      0  *  0 1 0  =   0      1      0    ----- 2
        62.692 277.67 1     0 0 1     62.692  277.67 1

CTM is updated to above result

q

push the CTM save to graphics state(Graphics state = 1, 2)

q

Again push the same CTM matrix to graphics state(Graphics state = 1, 2, 2)

1 0 0 1 286.59 207.54 cm

Update the CTM by current matrix.

      1      0      0   1      0      0        1      0      0
CTM = 0      1      0 * 0      0      0    =   0      1      0    ----- 3
      286.49 207.54 1  62.692 277.67 1        349.18 485.21  1

CTM updated to above result matrix.

q

push the CTM save to graphics state(Graphics state = 1, 2,2,3)

.75 .75 .75 RG
n
11.33 19.84 171.67 232.146 re
S

This will nothing change in position matrices.

Q

Assign the CTM to last available graphics state CTM = 3. Q will remove the last graphics state from graphics stack.(Graphics state = 1, 2,2)

1 0 0 1 17.007 23.52 cm 

Update the CTM by current matrix.

      1      0      0   0      1      0        0      1      0
CTM = 0      1      0 * 0      0      0    =   0      1      0    ----- 4
      17.007 23.52  1  349.18 485.21  1        366.18 508.73 1

CTM updated to above result matrix.

q

push the last CTM to graphics state(Graphics state = 1, 2,2,4) Skip rg, RG

BT

assign Tm and Tlm to Identity matrix.

1 0 0 1 0 5.6 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      0   5.6  1

Tm and Tlm updated.

46.22 0 Td

Translate the matrix according to above Td matrix.

             1   0  0     1 0   0       1     0  0
T  = T   =   0   1  0  *  0 1   0  =    0     1  0
 m    lm   46.22 0  1     0 5.6 1       46.22 5.6  1

update the Tm and Tlm with above matrix. Next Tf you can leave.

12 TL

This sets the graphics state leading text parameter, no direct influence on Tm or Tlm.

(William Shakespeare) Tj

This draws the string glyph-by-glyph, the first glyph transformed by this text rendering matrix

       8 × 1   0  0       1    0   0     1     0      0      8     0       0
T   =       0  8  0  *    0    1   0  *  0     1      0  =   0     8       0
 rm         0  0  1     46.22 5.6  1    366.18 508.73 1     412.4 514.33  1

While the string is rendered glyph-by-glyph, Tm is updated as described in ISO 32000-1 section 9.4.4, and so is the Trm.

T*

As the text leading currently is 12, T* is equivalent to 0 -12 Td, so:

             1   0  0     1     0   0       1       0   0
T  = T   =   0   1  0  *  0     1   0  =    0       1   0
 m    lm     0  -12 1     46.22 5.6 1       46.22 -6.4  1


来源:https://stackoverflow.com/questions/59573104/ctm-matrix-multiplication-with-previous-state-vs-with-identity-matrix-in-pdf-pos

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!