_fit.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. from typing import Any, Optional, Union
  2. from ._base import is_null_or_none
  3. class Fit:
  4. def __init__(
  5. self, fit_type: str, fit_args: tuple[Union[None, float, Any], ...] = ()
  6. ) -> None:
  7. from ._base import FloatObject, NameObject, NullObject, NumberObject # noqa: PLC0415
  8. self.fit_type = NameObject(fit_type)
  9. self.fit_args: list[Union[NullObject, FloatObject, NumberObject]] = [
  10. NullObject() if is_null_or_none(a) else FloatObject(a) for a in fit_args
  11. ]
  12. @classmethod
  13. def xyz(
  14. cls,
  15. left: Optional[float] = None,
  16. top: Optional[float] = None,
  17. zoom: Optional[float] = None,
  18. ) -> "Fit":
  19. """
  20. Display the page designated by page, with the coordinates (left, top)
  21. positioned at the upper-left corner of the window and the contents
  22. of the page magnified by the factor zoom.
  23. A null value for any of the parameters left, top, or zoom specifies
  24. that the current value of that parameter is to be retained unchanged.
  25. A zoom value of 0 has the same meaning as a null value.
  26. Args:
  27. left:
  28. top:
  29. zoom:
  30. Returns:
  31. The created fit object.
  32. """
  33. return Fit(fit_type="/XYZ", fit_args=(left, top, zoom))
  34. @classmethod
  35. def fit(cls) -> "Fit":
  36. """
  37. Display the page designated by page, with its contents magnified just
  38. enough to fit the entire page within the window both horizontally and
  39. vertically.
  40. If the required horizontal and vertical magnification factors are
  41. different, use the smaller of the two, centering the page within the
  42. window in the other dimension.
  43. """
  44. return Fit(fit_type="/Fit")
  45. @classmethod
  46. def fit_horizontally(cls, top: Optional[float] = None) -> "Fit":
  47. """
  48. Display the page designated by page, with the vertical coordinate top
  49. positioned at the top edge of the window and the contents of the page
  50. magnified just enough to fit the entire width of the page within the
  51. window.
  52. A null value for ``top`` specifies that the current value of that
  53. parameter is to be retained unchanged.
  54. Args:
  55. top:
  56. Returns:
  57. The created fit object.
  58. """
  59. return Fit(fit_type="/FitH", fit_args=(top,))
  60. @classmethod
  61. def fit_vertically(cls, left: Optional[float] = None) -> "Fit":
  62. return Fit(fit_type="/FitV", fit_args=(left,))
  63. @classmethod
  64. def fit_rectangle(
  65. cls,
  66. left: Optional[float] = None,
  67. bottom: Optional[float] = None,
  68. right: Optional[float] = None,
  69. top: Optional[float] = None,
  70. ) -> "Fit":
  71. """
  72. Display the page designated by page, with its contents magnified
  73. just enough to fit the rectangle specified by the coordinates
  74. left, bottom, right, and top entirely within the window
  75. both horizontally and vertically.
  76. If the required horizontal and vertical magnification factors are
  77. different, use the smaller of the two, centering the rectangle within
  78. the window in the other dimension.
  79. A null value for any of the parameters may result in unpredictable
  80. behavior.
  81. Args:
  82. left:
  83. bottom:
  84. right:
  85. top:
  86. Returns:
  87. The created fit object.
  88. """
  89. return Fit(fit_type="/FitR", fit_args=(left, bottom, right, top))
  90. @classmethod
  91. def fit_box(cls) -> "Fit":
  92. """
  93. Display the page designated by page, with its contents magnified just
  94. enough to fit its bounding box entirely within the window both
  95. horizontally and vertically.
  96. If the required horizontal and vertical magnification factors are
  97. different, use the smaller of the two, centering the bounding box
  98. within the window in the other dimension.
  99. """
  100. return Fit(fit_type="/FitB")
  101. @classmethod
  102. def fit_box_horizontally(cls, top: Optional[float] = None) -> "Fit":
  103. """
  104. Display the page designated by page, with the vertical coordinate top
  105. positioned at the top edge of the window and the contents of the page
  106. magnified just enough to fit the entire width of its bounding box
  107. within the window.
  108. A null value for top specifies that the current value of that parameter
  109. is to be retained unchanged.
  110. Args:
  111. top:
  112. Returns:
  113. The created fit object.
  114. """
  115. return Fit(fit_type="/FitBH", fit_args=(top,))
  116. @classmethod
  117. def fit_box_vertically(cls, left: Optional[float] = None) -> "Fit":
  118. """
  119. Display the page designated by page, with the horizontal coordinate
  120. left positioned at the left edge of the window and the contents of the
  121. page magnified just enough to fit the entire height of its bounding box
  122. within the window.
  123. A null value for left specifies that the current value of that
  124. parameter is to be retained unchanged.
  125. Args:
  126. left:
  127. Returns:
  128. The created fit object.
  129. """
  130. return Fit(fit_type="/FitBV", fit_args=(left,))
  131. def __str__(self) -> str:
  132. if not self.fit_args:
  133. return f"Fit({self.fit_type})"
  134. return f"Fit({self.fit_type}, {self.fit_args})"
  135. DEFAULT_FIT = Fit.fit()