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

36 statements  

1from typing import Any, List, Optional, Tuple, Union 

2 

3from ._base import is_null_or_none 

4 

5 

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 

11 

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 ] 

16 

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. 

28 

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. 

31 

32 A zoom value of 0 has the same meaning as a null value. 

33 

34 Args: 

35 left: 

36 top: 

37 zoom: 

38 

39 Returns: 

40 The created fit object. 

41 

42 """ 

43 return Fit(fit_type="/XYZ", fit_args=(left, top, zoom)) 

44 

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. 

51 

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") 

57 

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. 

65 

66 A null value for ``top`` specifies that the current value of that 

67 parameter is to be retained unchanged. 

68 

69 Args: 

70 top: 

71 

72 Returns: 

73 The created fit object. 

74 

75 """ 

76 return Fit(fit_type="/FitH", fit_args=(top,)) 

77 

78 @classmethod 

79 def fit_vertically(cls, left: Optional[float] = None) -> "Fit": 

80 return Fit(fit_type="/FitV", fit_args=(left,)) 

81 

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. 

95 

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. 

99 

100 A null value for any of the parameters may result in unpredictable 

101 behavior. 

102 

103 Args: 

104 left: 

105 bottom: 

106 right: 

107 top: 

108 

109 Returns: 

110 The created fit object. 

111 

112 """ 

113 return Fit(fit_type="/FitR", fit_args=(left, bottom, right, top)) 

114 

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. 

121 

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") 

127 

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. 

135 

136 A null value for top specifies that the current value of that parameter 

137 is to be retained unchanged. 

138 

139 Args: 

140 top: 

141 

142 Returns: 

143 The created fit object. 

144 

145 """ 

146 return Fit(fit_type="/FitBH", fit_args=(top,)) 

147 

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. 

155 

156 A null value for left specifies that the current value of that 

157 parameter is to be retained unchanged. 

158 

159 Args: 

160 left: 

161 

162 Returns: 

163 The created fit object. 

164 

165 """ 

166 return Fit(fit_type="/FitBV", fit_args=(left,)) 

167 

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})" 

172 

173 

174DEFAULT_FIT = Fit.fit()