Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/pypdf/generic/_viewerpref.py: 25%

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

77 statements  

1# Copyright (c) 2023, Pubpub-ZZ 

2# 

3# All rights reserved. 

4# 

5# Redistribution and use in source and binary forms, with or without 

6# modification, are permitted provided that the following conditions are 

7# met: 

8# 

9# * Redistributions of source code must retain the above copyright notice, 

10# this list of conditions and the following disclaimer. 

11# * Redistributions in binary form must reproduce the above copyright notice, 

12# this list of conditions and the following disclaimer in the documentation 

13# and/or other materials provided with the distribution. 

14# * The name of the author may not be used to endorse or promote products 

15# derived from this software without specific prior written permission. 

16# 

17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 

18# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 

19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 

20# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 

21# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 

22# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 

23# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 

24# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 

25# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 

26# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 

27# POSSIBILITY OF SUCH DAMAGE. 

28 

29from typing import ( 

30 Any, 

31 cast, 

32) 

33 

34from ..constants import PageAttributes 

35from ._base import BooleanObject, NameObject, NumberObject, is_null_or_none 

36from ._data_structures import ArrayObject, DictionaryObject 

37 

38f_obj = BooleanObject(False) 

39 

40#: The page boundaries a viewer preference may name, per Table 147 in the 2.0 

41#: reference. 

42BOX_NAMES = [ 

43 PageAttributes.MEDIABOX, 

44 PageAttributes.CROPBOX, 

45 PageAttributes.BLEEDBOX, 

46 PageAttributes.TRIMBOX, 

47 PageAttributes.ARTBOX, 

48] 

49 

50 

51class ViewerPreferences(DictionaryObject): 

52 def __init__(self, obj: DictionaryObject | None = None) -> None: 

53 super().__init__(self) 

54 if not is_null_or_none(obj): 

55 self.update(obj.items()) # type: ignore[union-attr] 

56 try: 

57 self.indirect_reference = obj.indirect_reference # type: ignore[union-attr] 

58 except AttributeError: 

59 pass 

60 

61 def _get_bool(self, key: str, default: BooleanObject | None) -> BooleanObject | None: 

62 return self.get(key, default) 

63 

64 def _set_bool(self, key: str, v: bool) -> None: 

65 self[NameObject(key)] = BooleanObject(v is True) 

66 

67 def _get_name(self, key: str, default: NameObject | None) -> NameObject | None: 

68 return self.get(key, default) 

69 

70 def _set_name(self, key: str, lst: list[str], v: NameObject) -> None: 

71 if lst != [] and v not in lst: 

72 raise ValueError(f"{v} is an unacceptable value") 

73 self[NameObject(key)] = NameObject(v) 

74 

75 def _get_arr(self, key: str, default: list[Any] | None) -> ArrayObject | None: 

76 return self.get(key, None if default is None else ArrayObject(default)) 

77 

78 def _set_arr(self, key: str, v: ArrayObject | None) -> None: 

79 if v is None: 

80 try: 

81 del self[NameObject(key)] 

82 except KeyError: 

83 pass 

84 return 

85 if not isinstance(v, ArrayObject): 

86 raise ValueError("ArrayObject is expected") 

87 if key == "/PrintPageRange" and len(v) % 2: 

88 # The array holds first/last page pairs, so an odd length leaves a 

89 # range without its end. 

90 raise ValueError( 

91 f"/PrintPageRange holds page pairs, got {len(v)} entries: {v}" 

92 ) 

93 self[NameObject(key)] = v 

94 

95 def _get_int(self, key: str, default: NumberObject | None) -> NumberObject | None: 

96 return self.get(key, default) 

97 

98 def _set_int(self, key: str, v: int) -> None: 

99 if v < 0: 

100 raise ValueError(f"{v} is an unacceptable value for {key}") 

101 self[NameObject(key)] = NumberObject(v) 

102 

103 @property 

104 def PRINT_SCALING(self) -> NameObject: 

105 return NameObject("/PrintScaling") 

106 

107 def __new__(cls: Any, value: Any = None) -> "ViewerPreferences": # noqa: PYI034 

108 def _add_prop_bool(key: str, default: BooleanObject | None) -> property: 

109 return property( 

110 lambda self: self._get_bool(key, default), 

111 lambda self, v: self._set_bool(key, v), 

112 None, 

113 f""" 

114 Returns/Modify the status of {key}, Returns {default} if not defined 

115 """, 

116 ) 

117 

118 def _add_prop_name( 

119 key: str, lst: list[str], default: NameObject | None 

120 ) -> property: 

121 return property( 

122 lambda self: self._get_name(key, default), 

123 lambda self, v: self._set_name(key, lst, NameObject(v)), 

124 None, 

125 f""" 

126 Returns/Modify the status of {key}, Returns {default} if not defined. 

127 Acceptable values: {lst} 

128 """, 

129 ) 

130 

131 def _add_prop_arr(key: str, default: ArrayObject | None) -> property: 

132 return property( 

133 lambda self: self._get_arr(key, default), 

134 lambda self, v: self._set_arr(key, v), 

135 None, 

136 f""" 

137 Returns/Modify the status of {key}, Returns {default} if not defined 

138 """, 

139 ) 

140 

141 def _add_prop_int(key: str, default: int | None) -> property: 

142 return property( 

143 lambda self: self._get_int(key, default), 

144 lambda self, v: self._set_int(key, v), 

145 None, 

146 f""" 

147 Returns/Modify the status of {key}, Returns {default} if not defined 

148 """, 

149 ) 

150 

151 cls.hide_toolbar = _add_prop_bool("/HideToolbar", f_obj) 

152 cls.hide_menubar = _add_prop_bool("/HideMenubar", f_obj) 

153 cls.hide_windowui = _add_prop_bool("/HideWindowUI", f_obj) 

154 cls.fit_window = _add_prop_bool("/FitWindow", f_obj) 

155 cls.center_window = _add_prop_bool("/CenterWindow", f_obj) 

156 cls.display_doctitle = _add_prop_bool("/DisplayDocTitle", f_obj) 

157 

158 cls.non_fullscreen_pagemode = _add_prop_name( 

159 "/NonFullScreenPageMode", 

160 ["/UseNone", "/UseOutlines", "/UseThumbs", "/UseOC"], 

161 NameObject("/UseNone"), 

162 ) 

163 cls.direction = _add_prop_name( 

164 "/Direction", ["/L2R", "/R2L"], NameObject("/L2R") 

165 ) 

166 cls.view_area = _add_prop_name("/ViewArea", BOX_NAMES, None) 

167 cls.view_clip = _add_prop_name("/ViewClip", BOX_NAMES, None) 

168 cls.print_area = _add_prop_name("/PrintArea", BOX_NAMES, None) 

169 cls.print_clip = _add_prop_name("/PrintClip", BOX_NAMES, None) 

170 cls.print_scaling = _add_prop_name( 

171 "/PrintScaling", ["/None", "/AppDefault"], None 

172 ) 

173 cls.duplex = _add_prop_name( 

174 "/Duplex", ["/Simplex", "/DuplexFlipShortEdge", "/DuplexFlipLongEdge"], None 

175 ) 

176 cls.pick_tray_by_pdfsize = _add_prop_bool("/PickTrayByPDFSize", None) 

177 cls.print_pagerange = _add_prop_arr("/PrintPageRange", None) 

178 cls.num_copies = _add_prop_int("/NumCopies", None) 

179 

180 cls.enforce = _add_prop_arr("/Enforce", ArrayObject()) 

181 

182 return cast("ViewerPreferences", DictionaryObject.__new__(cls))