title: PDF 1.7 - How exactly does the CTM (Current Transformation Matrix) work?
body: <p>After reading the <a href="http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/pdf_reference_1-7.pdf" rel="noreferrer">Adobe PDF 1.7 (ISO 32000-1:2008) specification</a>, I'm still having trouble understanding how to properly create my transformation matrix.</p>
  
  <p>The specification in section 4.2/4.3 state the following:</p>
  
  <blockquote>
    <p>• Translations are specified as [ 1 0 0 1 tx ty ], where tx and ty
    are the distances  to translate the origin of the coordinate system in
    the horizontal and vertical  dimensions, respectively.<br />  •
    Scaling is obtained by [ sx 0 0 sy 0 0 ]. This scales the coordinates
    so that 1  unit in the horizontal and vertical dimensions of the new
    coordinate system is  the same size as sx and sy units, respectively,
    in the previous coordinate system.<br /> • Rotations are produced by [
    cos θ sin θ −sin θ cos θ 0 0 ], which has the effect  of rotating the
    coordinate system axes by an angle θ counterclockwise.<br /> • Skew is
    specified by [ 1 tan α tan β 1 0 0 ], which skews the x axis by an
    angle  α and the y axis by an angle β.</p>
  </blockquote>
  
  <p>Given this, how exactly does one go about using transformations in sequence with each other?</p>
  
  <p>I can successfully use the <code>Translation</code> and <code>Rotation</code> together, but when I attempt to also use <code>Scaling</code> or <code>Skewing</code> things go severely wrong. Perhaps I'm using the CTM incorrectly or maybe even my math is off. I am attempting to create text at coordinate position (50, 50) with a rotation of 45 degrees and a scaling of 2 (in that order). The reason why I state "<i>In that order</i>" is because the specification states that ordering of transformations makes a difference (the spec gives a graphical example of the differences based on the transformation ordering). So what would the stream object look like and/or how would the matrix mathematics apply here?</p>
  
  <p>Working (Transformation of (50, 50) + 45 degree rotation)</p>
  
  <pre><code>[ 1  0  0 ]   [  0.707 0.707 0 ]   [  0.707  0.707 0 ]
  [ 0  1  0 ] x [ -0.707 0.707 0 ] = [ -0.707  0.707 0 ]
  [ 50 50 1 ]   [  0     0     1 ]   [ 50.000 50.000 1 ]
  
  BT
    0.707 0.707 -0.707 0.707 50 50 Tm
    /F1 36 Tf
    (Hello, World!) Tj
  ET
  </code></pre>
  
  <p>When I try to do matrix multiplication to add scaling, it doesn't seem to work:</p>
  
  <pre><code>[  0.707  0.707 0 ]   [ 2 0 0 ]   [   1.414   1.414 0 ]
  [ -0.707  0.707 0 ] x [ 0 2 0 ] = [  -1.414   1.414 0 ]
  [ 50.000 50.000 1 ]   [ 0 0 1 ]   [ 100.000 100.000 1 ]
  </code></pre>
  
  <p>The math seems correct, except now the text starts at coordinate (100, 100) instead of (50, 50). This just doesn't seem correct to me, since I'm trying to start at (50, 50), rotate by 45 degrees, then scale it by 2.</p>