MCPcopy
hub / github.com/ArtifexSoftware/pdf2docx / Element

Class Element

pdf2docx/common/Element.py:23–312  ·  view source on GitHub ↗

Boundary box with attribute in fitz.Rect type.

Source from the content-addressed store, hash-verified

21
22
23class Element(IText):
24 '''Boundary box with attribute in fitz.Rect type.'''
25
26 # all coordinates are related to un-rotated page in PyMuPDF
27 # e.g. Matrix(0.0, 1.0, -1.0, 0.0, 842.0, 0.0)
28 ROTATION_MATRIX = fitz.Matrix(0.0) # rotation angle = 0 degree by default
29
30
31 @classmethod
32 def set_rotation_matrix(cls, rotation_matrix):
33 """Set global rotation matrix.
34
35 Args:
36 Rotation_matrix (fitz.Matrix): target matrix
37 """
38 if rotation_matrix and isinstance(rotation_matrix, fitz.Matrix):
39 cls.ROTATION_MATRIX = rotation_matrix
40
41
42 @classmethod
43 def pure_rotation_matrix(cls):
44 '''Pure rotation matrix used for calculating text direction after rotation.'''
45 a,b,c,d,e,f = cls.ROTATION_MATRIX
46 return fitz.Matrix(a,b,c,d,0,0)
47
48
49 def __init__(self, raw:dict=None, parent=None):
50 ''' Initialize Element and convert to the real (rotation considered) page CS.'''
51 self.bbox = fitz.Rect() # type: fitz.Rect
52 self._parent = parent # type: Element
53
54 # NOTE: Any coordinates provided in raw is in original page CS
55 # (without considering page rotation).
56 if 'bbox' in (raw or {}):
57 rect = fitz.Rect(raw['bbox']) * Element.ROTATION_MATRIX
58 self.update_bbox(rect)
59
60
61 def __bool__(self):
62 '''Real object when bbox is defined.'''
63 # NOTE inconsistent results of fitz.Rect for different version of pymupdf, e.g.,
64 # a = fitz.Rect(3,3,2,2)
65 # bool(a) a.get_area() a.is_empty
66 # pymupdf 1.23.5 True 1.0 True
67 # pymupdf 1.23.8 True 0.0 True
68 # bool(fitz.Rect())==False
69 # NOTE: do not use `return not self.bbox.is_empty` here
70 return bool(self.bbox)
71
72
73 def __repr__(self): return f'{self.__class__.__name__}({tuple(self.bbox)})'
74
75
76 # ------------------------------------------------
77 # parent element
78 # ------------------------------------------------
79 @property
80 def parent(self): return self._parent

Callers 4

stream_tablesMethod · 0.90
parse_shadingMethod · 0.90
_group_h_v_strokesMethod · 0.90
working_bboxMethod · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…