matrix.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. # SPDX-FileCopyrightText: 2024 geisserml <geisserml@gmail.com>
  2. # SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause
  3. __all__ = ("PdfMatrix", )
  4. import math
  5. import ctypes
  6. import pypdfium2.raw as pdfium_c
  7. # TODO consider adding PdfRectangle support model to calculate size and corner points
  8. # NOTE the code below was written by a non-mathematician - might contain mistakes!
  9. class PdfMatrix:
  10. """
  11. PDF transformation matrix helper class.
  12. See the PDF 1.7 specification, Section 8.3.3 ("Common Transformations").
  13. Note:
  14. * The PDF format uses row vectors.
  15. * Transformations operate from the origin of the coordinate system
  16. (PDF coordinates: bottom left corner, Device coordinates: top left corner).
  17. * Matrix calculations are implemented independently in Python.
  18. * Matrix objects are immutable, so transforming methods return a new matrix.
  19. * Matrix objects implement ctypes auto-conversion to ``FS_MATRIX`` for easy use as C function parameter.
  20. Attributes:
  21. a (float): Matrix value [0][0].
  22. b (float): Matrix value [0][1].
  23. c (float): Matrix value [1][0].
  24. d (float): Matrix value [1][1].
  25. e (float): Matrix value [2][0] (X translation).
  26. f (float): Matrix value [2][1] (Y translation).
  27. """
  28. # See also pdfium/core/fxcrt/fx_coordinates.{h,cpp} (unfortunately, pdfium's matrix implementation is non-public)
  29. def __init__(self, a=1, b=0, c=0, d=1, e=0, f=0):
  30. self.a, self.b, self.c, self.d, self.e, self.f = a, b, c, d, e, f
  31. def __repr__(self):
  32. return f"PdfMatrix{self.get()}"
  33. def __eq__(self, other):
  34. if type(self) is not type(other):
  35. return False
  36. return (self.get() == other.get())
  37. @property
  38. def _as_parameter_(self):
  39. return ctypes.byref( self.to_raw() )
  40. def get(self):
  41. """
  42. Get the matrix as tuple of the form (a, b, c, d, e, f).
  43. """
  44. return (self.a, self.b, self.c, self.d, self.e, self.f)
  45. @classmethod
  46. def from_raw(cls, raw):
  47. """
  48. Load a :class:`.PdfMatrix` from a raw :class:`FS_MATRIX` object.
  49. """
  50. return cls(raw.a, raw.b, raw.c, raw.d, raw.e, raw.f)
  51. def to_raw(self):
  52. """
  53. Convert the matrix to a raw :class:`FS_MATRIX` object.
  54. """
  55. return pdfium_c.FS_MATRIX(*self.get())
  56. def multiply(self, other):
  57. """
  58. Multiply this matrix by another :class:`.PdfMatrix`, to concatenate transformations.
  59. """
  60. # M1 x M2 (self x other)
  61. # (a1, b1, 0) (a2, b2, 0) (a1a2+b1c2, a1b2+b1d2, 0)
  62. # (c1, d1, 0) x (c2, d2, 0) = (c1a2+d1c2, c1b2+d1d2, 0)
  63. # (e1, f1, 1) (e2, f2, 1) (e1a2+f1c2+e2, e1b2+f1d2+f2, 1)
  64. return PdfMatrix(
  65. a = self.a*other.a + self.b*other.c,
  66. b = self.a*other.b + self.b*other.d,
  67. c = self.c*other.a + self.d*other.c,
  68. d = self.c*other.b + self.d*other.d,
  69. e = self.e*other.a + self.f*other.c + other.e,
  70. f = self.e*other.b + self.f*other.d + other.f,
  71. )
  72. def translate(self, x, y):
  73. """
  74. Parameters:
  75. x (float): Horizontal shift (<0: left, >0: right).
  76. y (float): Vertical shift.
  77. """
  78. # same as return PdfMatrix(self.a, self.b, self.c, self.d, self.e+x, self.f+y)
  79. return self.multiply( PdfMatrix(1, 0, 0, 1, x, y) )
  80. def scale(self, x, y):
  81. """
  82. Parameters:
  83. x (float): A factor to scale the X axis (<1: compress, >1: stretch).
  84. y (float): A factor to scale the Y axis.
  85. """
  86. # same as return PdfMatrix(self.a*x, self.b*y, self.c*x, self.d*y, self.e*x, self.f*y)
  87. return self.multiply( PdfMatrix(x, 0, 0, y) )
  88. def rotate(self, angle, ccw=False, rad=False):
  89. """
  90. Parameters:
  91. angle (float): Angle by which to rotate the matrix.
  92. ccw (bool): If True, rotate counter-clockwise.
  93. rad (bool): If True, interpret the angle as radians.
  94. """
  95. if not rad:
  96. angle = math.radians(angle)
  97. c, s = math.cos(angle), math.sin(angle)
  98. return self.multiply( PdfMatrix(c, s, -s, c) if ccw else PdfMatrix(c, -s, s, c) )
  99. def mirror(self, v, h):
  100. """
  101. Parameters:
  102. v (bool): Whether to mirror vertically (at the Y axis).
  103. h (bool): Whether to mirror horizontall (at the X axis).
  104. """
  105. return self.scale(x=(-1 if v else 1), y=(-1 if h else 1))
  106. def skew(self, x_angle, y_angle, rad=False):
  107. """
  108. Parameters:
  109. x_angle (float): Inner angle to skew the X axis.
  110. y_angle (float): Inner angle to skew the Y axis.
  111. rad (bool): If True, interpret the angles as radians.
  112. """
  113. if not rad:
  114. x_angle = math.radians(x_angle)
  115. y_angle = math.radians(y_angle)
  116. return self.multiply( PdfMatrix(1, math.tan(x_angle), math.tan(y_angle), 1) )
  117. def on_point(self, x, y):
  118. """
  119. Returns:
  120. (float, float): Transformed point.
  121. """
  122. # (x, y) -> (ax+cy+e, bx+dy+f)
  123. return ( # new point
  124. self.a*x + self.c*y + self.e, # x
  125. self.b*x + self.d*y + self.f, # y
  126. )
  127. def on_rect(self, left, bottom, right, top):
  128. """
  129. Returns:
  130. (float, float, float, float): Transformed rectangle.
  131. """
  132. points = (
  133. self.on_point(left, top),
  134. self.on_point(left, bottom),
  135. self.on_point(right, top),
  136. self.on_point(right, bottom),
  137. )
  138. # NOTE maybe a single loop with min/max x/y vars and </> comparisons would be more efficient...
  139. return ( # new rect
  140. min(p[0] for p in points), # left
  141. min(p[1] for p in points), # bottom
  142. max(p[0] for p in points), # right
  143. max(p[1] for p in points), # top
  144. )