def _bounding_box(self): # Determine the size of the page at 72 dpi. mediabox = mupdf.Rect() mupdf.bound_page(self._context, self._c_page, mediabox) # 'mediabox {}'.format(mupdf.str_rect(mediabox))) return mediabox
def _transform_bounding_box(self, rotation=0, resolution=72, width=0, height=0, fit=False): bounds = self._bounding_box() scale = resolution / 72. transform = mupdf.Matrix() mupdf.pre_scale(mupdf.rotate(transform, rotation), scale, scale) tmp_bounds = mupdf.Rect() mupdf.copy_rect(tmp_bounds, bounds) ibounds = mupdf.IRect() mupdf.round_rect(ibounds, mupdf.transform_rect(tmp_bounds, transform)) # If a resolution is specified, check to see whether width/height are exceeded if not, unset them. if resolution != 72: actual_width = ibounds.x1 - ibounds.x0 actual_height = ibounds.y1 - ibounds.y0 if width and actual_width <= width: width = 0 if height and actual_height <= height: height = 0 # Now width or height will be 0 unless they need to be enforced. if width or height: scale_x = width / (tmp_bounds.x1 - tmp_bounds.x0) scale_y = height / (tmp_bounds.y1 - tmp_bounds.y0) if fit: # ignore aspect if not scale_x: scale_x = 1.0 # keep computed width elif not scale_y: scale_y = 1.0 # keep computed height else: if not scale_x: scale_x = scale_y elif not scale_y: scale_y = scale_x else: # take the smallest scale if scale_x > scale_y: scale_x = scale_y else: scale_y = scale_x scale_mat = mupdf.Matrix() mupdf.scale(scale_mat, scale_x, scale_y) mupdf.concat(transform, transform, scale_mat) mupdf.copy_rect(tmp_bounds, bounds) mupdf.transform_rect(tmp_bounds, transform) mupdf.round_rect(ibounds, tmp_bounds) return transform, ibounds