Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/pypdf/generic/_fit.py: 73%
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 collections.abc import Sequence
2from typing import Any, Optional, Union
4from ._base import is_null_or_none
7class Fit:
8 def __init__(
9 self, fit_type: str, fit_args: Sequence[Union[float, Any, None]] = ()
10 ) -> None:
11 from ._base import FloatObject, NameObject, NullObject, NumberObject # noqa: PLC0415
13 self.fit_type = NameObject(fit_type)
14 self.fit_args: list[Union[NullObject, FloatObject, NumberObject]] = [
15 NullObject() if is_null_or_none(a) else FloatObject(a) for a in fit_args
16 ]
18 @classmethod
19 def xyz(
20 cls,
21 left: Optional[float] = None,
22 top: Optional[float] = None,
23 zoom: Optional[float] = None,
24 ) -> "Fit":
25 """
26 Display the page designated by page, with the coordinates (left, top)
27 positioned at the upper-left corner of the window and the contents
28 of the page magnified by the factor zoom.
30 A null value for any of the parameters left, top, or zoom specifies
31 that the current value of that parameter is to be retained unchanged.
33 A zoom value of 0 has the same meaning as a null value.
35 Args:
36 left:
37 top:
38 zoom:
40 Returns:
41 The created fit object.
43 """
44 return Fit(fit_type="/XYZ", fit_args=(left, top, zoom))
46 @classmethod
47 def fit(cls) -> "Fit":
48 """
49 Display the page designated by page, with its contents magnified just
50 enough to fit the entire page within the window both horizontally and
51 vertically.
53 If the required horizontal and vertical magnification factors are
54 different, use the smaller of the two, centering the page within the
55 window in the other dimension.
56 """
57 return Fit(fit_type="/Fit")
59 @classmethod
60 def fit_horizontally(cls, top: Optional[float] = None) -> "Fit":
61 """
62 Display the page designated by page, with the vertical coordinate top
63 positioned at the top edge of the window and the contents of the page
64 magnified just enough to fit the entire width of the page within the
65 window.
67 A null value for ``top`` specifies that the current value of that
68 parameter is to be retained unchanged.
70 Args:
71 top:
73 Returns:
74 The created fit object.
76 """
77 return Fit(fit_type="/FitH", fit_args=(top,))
79 @classmethod
80 def fit_vertically(cls, left: Optional[float] = None) -> "Fit":
81 return Fit(fit_type="/FitV", fit_args=(left,))
83 @classmethod
84 def fit_rectangle(
85 cls,
86 left: Optional[float] = None,
87 bottom: Optional[float] = None,
88 right: Optional[float] = None,
89 top: Optional[float] = None,
90 ) -> "Fit":
91 """
92 Display the page designated by page, with its contents magnified
93 just enough to fit the rectangle specified by the coordinates
94 left, bottom, right, and top entirely within the window
95 both horizontally and vertically.
97 If the required horizontal and vertical magnification factors are
98 different, use the smaller of the two, centering the rectangle within
99 the window in the other dimension.
101 A null value for any of the parameters may result in unpredictable
102 behavior.
104 Args:
105 left:
106 bottom:
107 right:
108 top:
110 Returns:
111 The created fit object.
113 """
114 return Fit(fit_type="/FitR", fit_args=(left, bottom, right, top))
116 @classmethod
117 def fit_box(cls) -> "Fit":
118 """
119 Display the page designated by page, with its contents magnified just
120 enough to fit its bounding box entirely within the window both
121 horizontally and vertically.
123 If the required horizontal and vertical magnification factors are
124 different, use the smaller of the two, centering the bounding box
125 within the window in the other dimension.
126 """
127 return Fit(fit_type="/FitB")
129 @classmethod
130 def fit_box_horizontally(cls, top: Optional[float] = None) -> "Fit":
131 """
132 Display the page designated by page, with the vertical coordinate top
133 positioned at the top edge of the window and the contents of the page
134 magnified just enough to fit the entire width of its bounding box
135 within the window.
137 A null value for top specifies that the current value of that parameter
138 is to be retained unchanged.
140 Args:
141 top:
143 Returns:
144 The created fit object.
146 """
147 return Fit(fit_type="/FitBH", fit_args=(top,))
149 @classmethod
150 def fit_box_vertically(cls, left: Optional[float] = None) -> "Fit":
151 """
152 Display the page designated by page, with the horizontal coordinate
153 left positioned at the left edge of the window and the contents of the
154 page magnified just enough to fit the entire height of its bounding box
155 within the window.
157 A null value for left specifies that the current value of that
158 parameter is to be retained unchanged.
160 Args:
161 left:
163 Returns:
164 The created fit object.
166 """
167 return Fit(fit_type="/FitBV", fit_args=(left,))
169 def __str__(self) -> str:
170 if not self.fit_args:
171 return f"Fit({self.fit_type})"
172 return f"Fit({self.fit_type}, {self.fit_args})"
175DEFAULT_FIT = Fit.fit()