MCPcopy Create free account
hub / github.com/pymupdf/PyMuPDF / calc_matrix

Function calc_matrix

src_classic/utils.py:146–178  ·  view source on GitHub ↗

Calculate transformation matrix from source to target rect. Notes: The product of four matrices in this sequence: (1) translate correct source corner to origin, (2) rotate, (3) scale, (4) translate to target's top-left corner. Args: sr

(sr, tr, keep=True, rotate=0)

Source from the content-addressed store, hash-verified

144 clip = kwargs.get("clip")
145
146 def calc_matrix(sr, tr, keep=True, rotate=0):
147 """Calculate transformation matrix from source to target rect.
148
149 Notes:
150 The product of four matrices in this sequence: (1) translate correct
151 source corner to origin, (2) rotate, (3) scale, (4) translate to
152 target's top-left corner.
153 Args:
154 sr: source rect in PDF (!) coordinate system
155 tr: target rect in PDF coordinate system
156 keep: whether to keep source ratio of width to height
157 rotate: rotation angle in degrees
158 Returns:
159 Transformation matrix.
160 """
161 # calc center point of source rect
162 smp = (sr.tl + sr.br) / 2.0
163 # calc center point of target rect
164 tmp = (tr.tl + tr.br) / 2.0
165
166 # m moves to (0, 0), then rotates
167 m = Matrix(1, 0, 0, 1, -smp.x, -smp.y) * Matrix(rotate)
168
169 sr1 = sr * m # resulting source rect to calculate scale factors
170
171 fw = tr.width / sr1.width # scale the width
172 fh = tr.height / sr1.height # scale the height
173 if keep:
174 fw = fh = min(fw, fh) # take min if keeping aspect ratio
175
176 m *= Matrix(fw, fh) # concat scale matrix
177 m *= Matrix(1, 0, 0, 1, tmp.x, tmp.y) # concat move to target center
178 return JM_TUPLE(m)
179
180 CheckParent(page)
181 doc = page.parent

Callers 2

show_pdf_pageMethod · 0.85
show_pdf_pageFunction · 0.85

Calls 2

MatrixClass · 0.85
JM_TUPLEFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…