The fz_matrix structure is used to represent 2 dimensional matrices used for transforming points, shapes and other geometry.
The six fields of the fz_matrix structure correspond to a matrix of the form:
Such transformation matrices can be used to represent a wide range of different operations, including translations, rotations, scales, sheers, and any combination thereof.
Typically, a matrix will be created for a specific purpose, such as a scale, or a translation. For this reason, we have dedicated construction calls.
Mathematically, points are transformed by multiplying them (extended to 3 elements long). For example (x',y'), the point given by mapping (x,y) through such a matrix is calculated as follows:
There are various functions in MuPDF to perform such transformations:
Rectangles can be transformed using the following function, which allows for the fact that the image of a rectangle may `flip' the rectangle (i.e. that a minimum coordinate may end up as a maximum one after translation, and vice versa):
Also, it can be useful to transform a point, ignoring the translation components of a transformation, so we have a convenience function for this:
Transformations can be combined by multiplying their representative matrices together. Transforming a point by applying matrix A then matrix B, will give identical results to transforming the point by AB.
MuPDF provides an API for combining matrices in this way:
Alternatively, operations can be specifically applied to existing matrices. Because of the non-commutative nature of matrix operations, it matters whether the new operation is applied before or after the existing matrix.
For example, if you have a matrix that performs a rotation, and you wish to combine that with a translation, you must decide whether you want the translation to occur before the rotation (`pre') or afterwards (`post').
MuPDF has various API functions for such operations:
Finally, sometimes it is useful to find the matrix that would represent the reverse of a given transformation. This can be achieved by `inverting' the matrix. This is not possible in all cases, but can be achieved for most `well-behaved' transformations.