Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/pypdf/generic/_fit.py: 72%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1from typing import Any, Optional, Union
3from ._base import is_null_or_none
6class Fit:
7 def __init__(
8 self, fit_type: str, fit_args: tuple[Union[None, float, Any], ...] = ()
9 ) -> None:
10 from ._base import FloatObject, NameObject, NullObject, NumberObject # noqa: PLC0415
12 self.fit_type = NameObject(fit_type)
13 self.fit_args: list[Union[NullObject, FloatObject, NumberObject]] = [
14 NullObject() if is_null_or_none(a) else FloatObject(a) for a in fit_args
15 ]
17 @classmethod
18 def xyz(
19 cls,
20 left: Optional[float] = None,
21 top: Optional[float] = None,
22 zoom: Optional[float] = None,
23 ) -> "Fit":
24 """
25 Display the page designated by page, with the coordinates (left, top)
26 positioned at the upper-left corner of the window and the contents
27 of the page magnified by the factor zoom.
29 A null value for any of the parameters left, top, or zoom specifies
30 that the current value of that parameter is to be retained unchanged.
32 A zoom value of 0 has the same meaning as a null value.
34 Args:
35 left:
36 top:
37 zoom:
39 Returns:
40 The created fit object.
42 """
43 return Fit(fit_type="/XYZ", fit_args=(left, top, zoom))
45 @classmethod
46 def fit(cls) -> "Fit":
47 """
48 Display the page designated by page, with its contents magnified just
49 enough to fit the entire page within the window both horizontally and
50 vertically.
52 If the required horizontal and vertical magnification factors are
53 different, use the smaller of the two, centering the page within the
54 window in the other dimension.
55 """
56 return Fit(fit_type="/Fit")
58 @classmethod
59 def fit_horizontally(cls, top: Optional[float] = None) -> "Fit":
60 """
61 Display the page designated by page, with the vertical coordinate top
62 positioned at the top edge of the window and the contents of the page
63 magnified just enough to fit the entire width of the page within the
64 window.
66 A null value for ``top`` specifies that the current value of that
67 parameter is to be retained unchanged.
69 Args:
70 top:
72 Returns:
73 The created fit object.
75 """
76 return Fit(fit_type="/FitH", fit_args=(top,))
78 @classmethod
79 def fit_vertically(cls, left: Optional[float] = None) -> "Fit":
80 return Fit(fit_type="/FitV", fit_args=(left,))
82 @classmethod
83 def fit_rectangle(
84 cls,
85 left: Optional[float] = None,
86 bottom: Optional[float] = None,
87 right: Optional[float] = None,
88 top: Optional[float] = None,
89 ) -> "Fit":
90 """
91 Display the page designated by page, with its contents magnified
92 just enough to fit the rectangle specified by the coordinates
93 left, bottom, right, and top entirely within the window
94 both horizontally and vertically.
96 If the required horizontal and vertical magnification factors are
97 different, use the smaller of the two, centering the rectangle within
98 the window in the other dimension.
100 A null value for any of the parameters may result in unpredictable
101 behavior.
103 Args:
104 left:
105 bottom:
106 right:
107 top:
109 Returns:
110 The created fit object.
112 """
113 return Fit(fit_type="/FitR", fit_args=(left, bottom, right, top))
115 @classmethod
116 def fit_box(cls) -> "Fit":
117 """
118 Display the page designated by page, with its contents magnified just
119 enough to fit its bounding box entirely within the window both
120 horizontally and vertically.
122 If the required horizontal and vertical magnification factors are
123 different, use the smaller of the two, centering the bounding box
124 within the window in the other dimension.
125 """
126 return Fit(fit_type="/FitB")
128 @classmethod
129 def fit_box_horizontally(cls, top: Optional[float] = None) -> "Fit":
130 """
131 Display the page designated by page, with the vertical coordinate top
132 positioned at the top edge of the window and the contents of the page
133 magnified just enough to fit the entire width of its bounding box
134 within the window.
136 A null value for top specifies that the current value of that parameter
137 is to be retained unchanged.
139 Args:
140 top:
142 Returns:
143 The created fit object.
145 """
146 return Fit(fit_type="/FitBH", fit_args=(top,))
148 @classmethod
149 def fit_box_vertically(cls, left: Optional[float] = None) -> "Fit":
150 """
151 Display the page designated by page, with the horizontal coordinate
152 left positioned at the left edge of the window and the contents of the
153 page magnified just enough to fit the entire height of its bounding box
154 within the window.
156 A null value for left specifies that the current value of that
157 parameter is to be retained unchanged.
159 Args:
160 left:
162 Returns:
163 The created fit object.
165 """
166 return Fit(fit_type="/FitBV", fit_args=(left,))
168 def __str__(self) -> str:
169 if not self.fit_args:
170 return f"Fit({self.fit_type})"
171 return f"Fit({self.fit_type}, {self.fit_args})"
174DEFAULT_FIT = Fit.fit()