PTMatrix2D

@interface PTMatrix2D : TRN_matrix2d

2D Matrix

A Matrix2D object represents a 3x3 matrix that, in turn, represents an affine transformation. A Matrix2D object stores only six of the nine numbers in a 3x3 matrix because all 3x3 matrices that represent affine transformations have the same third column (0, 0, 1).

Affine transformations include rotating, scaling, reflecting, shearing, and translating. In PDFNet, the Matrix2D class provides the foundation for performing affine transformations on vector drawings, images, and text.

A transformation matrix specifies the relationship between two coordinate spaces. By modifying a transformation matrix, objects can be scaled, rotated, translated, or transformed in other ways.

A transformation matrix in PDF is specified by six numbers, usually in the form of an array containing six elements. In its most general form, this array is denoted [a b c d h v]; The following table lists the arrays that specify the most common transformations:

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

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

  • Rotations are produced by [cos(A) sin(A) -sin(A) cos(A) 0 0], which has the effect of rotating the coordinate system axes by an angle ‘A’ counterclockwise.

  • Skew is specified by [1 tan(A) tan(B) 1 0 0], which skews the x axis by an angle A and the y axis by an angle B.

Matrix2D elements are positioned as follows : | m_a m_b 0 | | m_c m_d 0 | | m_h m_v 1 |

A single Matrix2D object can store a single transformation or a sequence of transformations. The latter is called a composite transformation. The matrix of a composite transformation is obtained by multiplying (concatenating) the matrices of the individual transformations. Because matrix multiplication is not commutative-the order in which matrices are multiplied is significant. For example, if you first rotate, then scale, then translate, you get a different result than if you first translate, then rotate, then scale.

For more information on properties of PDF matrices please refer to PDF Reference Manual (Sections 4.2 ‘Coordinate Systems’ and 4.2.3 ‘Transformation Matrices’)

 The following sample illustrates how to use Matrix2D in order to position
 an image on the page. Note that PDFNet uses the same convention of matrix
 multiplication used in PostScript and OpenGL.

    Element element = eb.CreateImage(Image(...));
    double deg2rad = 3.1415926535 / 180.0;

    Matrix2D mtx = Matrix2D(1, 0, 0, 1, 0, 200); // Translate
    mtx *= Matrix2D(300, 0, 0, 200, 0, 0);    // Scale
    mtx *= Matrix2D::RotationMatrix( 90 * deg2rad ); // Rotate
    element.GetGState().SetTransform(mtx);
    writer.WritePlacedElement(element);
 The following sample sample illustrates how to use Matrix2D in order to calculate
 absolute positioning for the text on the page.
 ...
 Matrix2D text_mtx = text_element.GetTextMatrix();
 double x, y;
 for (CharIterator itr = text_element.GetCharIterator(); itr.HasNext(); itr.Next()) {
   x = itr.Current().x; // character positioning information
   y = itr.Current().y;
   // Get current transformation matrix (CTM)
   Matrix2D ctm = text_element.GetCTM();

   // To get the absolute character positioning information concatenate current
   // text matrix with CTM and then multiply relative positioning coordinates with
   // the resulting matrix.
   Matrix2D mtx = ctm * text_mtx;
   mtx.Mult(x, y);
 }
  • Creates and initializes a Matrix object based on six numbers that define an affine transformation.

    @default when none the arguments are specified, an identity matrix is created.

    Declaration

    Objective-C

    - (instancetype)initWithA:(double)a
                            b:(double)b
                            c:(double)c
                            d:(double)d
                            h:(double)h
                            v:(double)v;

    Swift

    init!(a: Double, b: Double, c: Double, d: Double, h: Double, v: Double)

    Parameters

    a

    the matrix element in the first row, first column.

    b

    the matrix element in the first row, second column.

    c

    the matrix element in the second row, first column.

    d

    the matrix element in the second row, second column.

    h

    the matrix element in the third row, first column.

    v

    the matrix element in the third row, second column.

  • The Set method sets the elements of this matrix.

    Declaration

    Objective-C

    - (void)Set:(double)a
              b:(double)b
              c:(double)c
              d:(double)d
              h:(double)h
              v:(double)v;

    Swift

    func set(_ a: Double, b: Double, c: Double, d: Double, h: Double, v: Double)

    Parameters

    a

    the matrix element in the first row, first column.

    b

    the matrix element in the first row, second column.

    c

    the matrix element in the second row, first column.

    d

    the matrix element in the second row, second column.

    h

    the matrix element in the third row, first column.

    v

    the matrix element in the third row, second column.

  • The Concat method updates this matrix with the product of itself and another matrix specified through an argument list.

    Declaration

    Objective-C

    - (void)Concat:(double)a
                 b:(double)b
                 c:(double)c
                 d:(double)d
                 h:(double)h
                 v:(double)v;

    Swift

    func concat(_ a: Double, b: Double, c: Double, d: Double, h: Double, v: Double)

    Parameters

    a

    the matrix element in the first row, first column.

    b

    the matrix element in the first row, second column.

    c

    the matrix element in the second row, first column.

    d

    the matrix element in the second row, second column.

    h

    the matrix element in the third row, first column.

    v

    the matrix element in the third row, second column.

  • Transform/multiply the point (in_out_x, in_out_y) using this matrix

    Declaration

    Objective-C

    - (PTMatrix2D *)Multiply:(PTMatrix2D *)m;

    Swift

    func multiply(_ m: PTMatrix2D!) -> PTMatrix2D!
  • Undocumented

    Declaration

    Objective-C

    - (BOOL)IsEquals: (PTMatrix2D*)m;

    Swift

    func isEquals(_ m: PTMatrix2D!) -> Bool
  • Undocumented

    Declaration

    Objective-C

    - (BOOL)IsNotEquals: (PTMatrix2D*)m;

    Swift

    func isNotEquals(_ m: PTMatrix2D!) -> Bool
  • Undocumented

    Declaration

    Objective-C

    - (PTPDFPoint*)Mult: (PTPDFPoint*)pt;

    Swift

    func mult(_ pt: PTPDFPoint!) -> PTPDFPoint!
  • Declaration

    Objective-C

    - (PTMatrix2D *)Inverse;

    Swift

    func inverse() -> PTMatrix2D!

    Return Value

    If this matrix is invertible, the Inverse method returns its inverse matrix.

  • Updates this matrix with the product of itself and a translation matrix (i.e. it is equivalent to this.m_h += h; this.m_v += v).

    Note

    This method is deprecated. Please use PreTranslate or PostTranslate instead. The behavior of this method is identical to PreTranslate, but PostTranslate will be more suitable for some use cases.

    Declaration

    Objective-C

    - (void)Translate:(double)h v:(double)v;

    Swift

    func translate(_ h: Double, v: Double)

    Parameters

    h

    the horizontal component of the translation.

    v

    the vertical component of the translation.

  • Updates this matrix to the concatenation of a translation matrix and the original matrix. M’ = T(h, v) * M. It is equivalent to this.m_h += h; this.m_v += v.

    Declaration

    Objective-C

    - (void)PreTranslate:(double)h v:(double)v;

    Swift

    func preTranslate(_ h: Double, v: Double)

    Parameters

    h

    the horizontal component of the translation.

    v

    the vertical component of the translation.

  • Updates this matrix by concatenating a translation matrix. M’ = M * T(h, v). It is equivalent to this.Concat(1,0,0,1,h,v).

    Declaration

    Objective-C

    - (void)PostTranslate:(double)h v:(double)v;

    Swift

    func postTranslate(_ h: Double, v: Double)

    Parameters

    h

    the horizontal component of the translation.

    v

    the vertical component of the translation.

  • The Scale method updates this matrix with the product of itself and a scaling matrix.

    Declaration

    Objective-C

    - (void)Scale:(double)h v:(double)v;

    Swift

    func scale(_ h: Double, v: Double)

    Parameters

    h

    the horizontal scale factor.

    v

    the vertical scale factor

  • Create zero matrix (0 0 0 0 0 0)

    Declaration

    Objective-C

    + (PTMatrix2D *)ZeroMatrix;

    Swift

    class func zero() -> PTMatrix2D!
  • Create identity matrix (1 0 0 1 0 0)

    Declaration

    Objective-C

    + (PTMatrix2D *)IdentityMatrix;

    Swift

    class func identity() -> PTMatrix2D!
  • Declaration

    Objective-C

    + (PTMatrix2D *)RotationMatrix:(double)angle;

    Swift

    class func rotationMatrix(_ angle: Double) -> PTMatrix2D!

    Parameters

    angle

    the angle of rotation in radians. Positive values specify clockwise rotation.

    Return Value

    A rotation matrix for a given angle.