Skip to content

Commit

Permalink
A12
Browse files Browse the repository at this point in the history
  • Loading branch information
rafal committed Jul 1, 2023
1 parent 7d18a9b commit fca6b08
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 67 deletions.
6 changes: 3 additions & 3 deletions fpdf/drawing.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ class DeviceRGB(
OPERATOR = "rg"
"""The PDF drawing operator used to specify this type of color."""

def __new__(cls, r, g, b, a=None):
def __new__(cls, r: float, g: float, b: float, a: Optional[float] = None):
if a is not None:
_check_range(a)

Expand Down Expand Up @@ -240,7 +240,7 @@ class DeviceGray(
OPERATOR = "g"
"""The PDF drawing operator used to specify this type of color."""

def __new__(cls, g, a=None):
def __new__(cls, g: float, a: Optional[float] = None):
if a is not None:
_check_range(a)

Expand Down Expand Up @@ -3240,7 +3240,7 @@ class PaintedPath:
primitive path elements and `GraphicsContext`.
"""

def __init__(self, x=0, y=0):
def __init__(self, x: float = 0, y: float = 0) -> None:
self._root_graphics_context = GraphicsContext()
self._graphics_context = self._root_graphics_context

Expand Down
2 changes: 1 addition & 1 deletion fpdf/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class FPDFException(Exception):
class FPDFPageFormatException(FPDFException):
"""Error is thrown when a bad page format is given"""

def __init__(self, argument, unknown=False, one=False):
def __init__(self, argument, unknown: bool = False, one: bool = False):
super().__init__()
if unknown and one:
raise TypeError(
Expand Down
111 changes: 61 additions & 50 deletions fpdf/fpdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
from numbers import Number
from os.path import splitext
from pathlib import Path
from typing import Callable, NamedTuple, Optional, Union, List, Tuple
from typing import Callable, NamedTuple, Optional, Union, List, Tuple, Any

from .drawing import DeviceGray, DeviceRGB

try:
from endesive import signer
Expand Down Expand Up @@ -235,10 +237,10 @@ class FPDF(GraphicsStateMixin):

def __init__(
self,
orientation="portrait",
unit="mm",
format="A4",
font_cache_dir="DEPRECATED",
orientation: str = "portrait",
unit: Union[str, int, float] = "mm",
format: str = "A4",
font_cache_dir: Union[Path, str] = "DEPRECATED",
):
"""
Args:
Expand Down Expand Up @@ -399,11 +401,11 @@ def write_html(self, text, *args, **kwargs):
text = unescape(text) # To deal with HTML entities
html2pdf.feed(text)

def _set_min_pdf_version(self, version):
def _set_min_pdf_version(self, version: str) -> None:
self.pdf_version = max(self.pdf_version, version)

@property
def is_ttf_font(self):
def is_ttf_font(self) -> bool:
return self.current_font and self.current_font.type == "TTF"

@property
Expand Down Expand Up @@ -449,7 +451,7 @@ def set_margin(self, margin):
self.set_margins(margin, margin)
self.set_auto_page_break(self.auto_page_break, margin)

def set_margins(self, left, top, right=-1):
def set_margins(self, left: float, top: float, right: float = -1):
"""
Sets the document left, top & optionaly right margins to the same value.
By default, they equal 1 cm.
Expand All @@ -468,7 +470,7 @@ def set_margins(self, left, top, right=-1):
right = left
self.r_margin = right

def set_left_margin(self, margin):
def set_left_margin(self, margin: float):
"""
Sets the document left margin.
Also sets the current FPDF.x on the page to this minimum horizontal position.
Expand All @@ -480,7 +482,7 @@ def set_left_margin(self, margin):
self.x = margin
self.l_margin = margin

def set_top_margin(self, margin):
def set_top_margin(self, margin: float):
"""
Sets the document top margin.
Expand All @@ -489,7 +491,7 @@ def set_top_margin(self, margin):
"""
self.t_margin = margin

def set_right_margin(self, margin):
def set_right_margin(self, margin: float):
"""
Sets the document right margin.
Expand Down Expand Up @@ -521,7 +523,7 @@ def default_page_dimensions(self):
else (self.dh_pt, self.dw_pt)
)

def _set_orientation(self, orientation, page_width_pt, page_height_pt):
def _set_orientation(self, orientation: str, page_width_pt, page_height_pt):
orientation = orientation.lower()
if orientation in ("p", "portrait"):
self.cur_orientation = "P"
Expand All @@ -536,7 +538,7 @@ def _set_orientation(self, orientation, page_width_pt, page_height_pt):
self.w = self.w_pt / self.k
self.h = self.h_pt / self.k

def set_display_mode(self, zoom, layout="continuous"):
def set_display_mode(self, zoom: Union[str, int], layout="continuous"):
"""
Defines the way the document is to be displayed by the viewer.
Expand Down Expand Up @@ -938,7 +940,12 @@ def set_fill_color(self, r, g: int = -1, b: int = -1):
if self.page > 0:
self._out(self.fill_color.serialize().lower())

def set_text_color(self, r, g=-1, b=-1):
def set_text_color(
self,
r: Union[int, Tuple[float, float, float], DeviceGray, DeviceRGB],
g=-1,
b=-1,
):
"""
Defines the color used for text.
It can be expressed in RGB components or grey scale.
Expand Down Expand Up @@ -1381,7 +1388,7 @@ def ellipse(self, x, y, w, h, style=None):
style = RenderStyle.coerce(style)
self._draw_ellipse(x, y, w, h, style.operator)

def _draw_ellipse(self, x, y, w, h, operator):
def _draw_ellipse(self, x: float, y: float, w: float, h: float, operator):
cx = x + w / 2
cy = y + h / 2
rx = w / 2
Expand Down Expand Up @@ -3059,7 +3066,7 @@ def _render_styled_text_line(

return page_break_triggered

def _add_quad_points(self, x, y, w, h):
def _add_quad_points(self, x: float, y: float, w: float, h: float):
self._text_quad_points[self.page].extend(
[
x * self.k,
Expand All @@ -3073,7 +3080,7 @@ def _add_quad_points(self, x, y, w, h):
]
)

def _preload_font_styles(self, txt, markdown):
def _preload_font_styles(self, txt: str, markdown: bool):
"""
When Markdown styling is enabled, we require secondary fonts
to ender text in bold & italics.
Expand Down Expand Up @@ -3105,7 +3112,7 @@ def _preload_font_styles(self, txt, markdown):
self.page = page
return styled_txt_frags

def _parse_chars(self, txt):
def _parse_chars(self, txt: str):
"Check if the font has all the necessary glyphs. If a glyph from a fallback font is used, break into fragments"
fragments = []
txt_frag = []
Expand Down Expand Up @@ -3273,12 +3280,12 @@ def _perform_page_break_if_need_be(self, h):
return True
return False

def _perform_page_break(self):
def _perform_page_break(self) -> None:
x = self.x
self.add_page(same=True)
self.x = x # restore x but not y after drawing header

def _has_next_page(self):
def _has_next_page(self) -> bool:
return self.pages_count > self.page

@contextmanager
Expand Down Expand Up @@ -4059,11 +4066,11 @@ def ln(self, h=None):
self.x = self.l_margin
self.y += self._lasth if h is None else h

def get_x(self):
def get_x(self) -> float:
"""Returns the abscissa of the current position."""
return self.x

def set_x(self, x):
def set_x(self, x: float) -> None:
"""
Defines the abscissa of the current position.
If the value provided is negative, it is relative to the right of the page.
Expand All @@ -4073,15 +4080,15 @@ def set_x(self, x):
"""
self.x = x if x >= 0 else self.w + x

def get_y(self):
def get_y(self) -> float:
"""Returns the ordinate of the current position."""
if self._in_unbreakable:
raise FPDFException(
"Using get_y() inside an unbreakable() code block is error-prone"
)
return self.y

def set_y(self, y):
def set_y(self, y: float) -> None:
"""
Moves the current abscissa back to the left margin and sets the ordinate.
If the value provided is negative, it is relative to the bottom of the page.
Expand Down Expand Up @@ -4121,13 +4128,13 @@ def normalize_text(self, txt: str):

def sign_pkcs12(
self,
pkcs_filepath,
pkcs_filepath: str,
password=None,
hashalgo="sha256",
contact_info=None,
location=None,
signing_time=None,
reason=None,
hashalgo: str = "sha256",
contact_info: Optional[str] = None,
location: Optional[str] = None,
signing_time: Optional[datetime] = None,
reason: Optional[str] = None,
flags=(AnnotationFlag.PRINT, AnnotationFlag.LOCKED),
):
"""
Expand Down Expand Up @@ -4170,11 +4177,11 @@ def sign(
key,
cert,
extra_certs=(),
hashalgo="sha256",
contact_info=None,
location=None,
signing_time=None,
reason=None,
hashalgo: str = "sha256",
contact_info: Optional[str] = None,
location: Optional[str] = None,
signing_time: Optional[datetime] = None,
reason: Optional[str] = None,
flags=(AnnotationFlag.PRINT, AnnotationFlag.LOCKED),
):
"""
Expand Down Expand Up @@ -4368,7 +4375,7 @@ def interleaved2of5(self, txt, x, y, w=1, h=10):
x += line_width

@check_page
def code39(self, txt, x, y, w=1.5, h=5):
def code39(self, txt: str, x: float, y: float, w: float = 1.5, h: float = 5):
"""Barcode 3of9"""
dim = {"w": w, "n": w / 3}
if not txt.startswith("*") or not txt.endswith("*"):
Expand Down Expand Up @@ -4436,7 +4443,7 @@ def code39(self, txt, x, y, w=1.5, h=5):

@check_page
@contextmanager
def rect_clip(self, x, y, w, h):
def rect_clip(self, x: float, y: float, w: float, h: float):
"""
Context manager that defines a rectangular crop zone,
useful to render only part of an image.
Expand All @@ -4458,7 +4465,7 @@ def rect_clip(self, x, y, w, h):

@check_page
@contextmanager
def elliptic_clip(self, x, y, w, h):
def elliptic_clip(self, x: float, y: float, w: float, h: float):
"""
Context manager that defines an elliptic crop zone,
useful to render only part of an image.
Expand All @@ -4476,7 +4483,7 @@ def elliptic_clip(self, x, y, w, h):

@check_page
@contextmanager
def round_clip(self, x, y, r):
def round_clip(self, x: float, y: float, r: float):
"""
Context manager that defines a circular crop zone,
useful to render only part of an image.
Expand Down Expand Up @@ -4536,7 +4543,7 @@ def offset_rendering(self):
recorder.rewind()

@check_page
def insert_toc_placeholder(self, render_toc_function, pages=1):
def insert_toc_placeholder(self, render_toc_function, pages: int = 1):
"""
Configure Table Of Contents rendering at the end of the document generation,
and reserve some vertical space right now in order to insert it.
Expand Down Expand Up @@ -4566,13 +4573,13 @@ def insert_toc_placeholder(self, render_toc_function, pages=1):

def set_section_title_styles(
self,
level0,
level1=None,
level2=None,
level3=None,
level4=None,
level5=None,
level6=None,
level0: TitleStyle,
level1: Optional[TitleStyle] = None,
level2: Optional[TitleStyle] = None,
level3: Optional[TitleStyle] = None,
level4: Optional[TitleStyle] = None,
level5: Optional[TitleStyle] = None,
level6: Optional[TitleStyle] = None,
):
"""
Defines a style for section titles.
Expand Down Expand Up @@ -4603,7 +4610,7 @@ def set_section_title_styles(
}

@check_page
def start_section(self, name, level=0, strict=True):
def start_section(self, name: str, level: int = 0, strict: bool = True):
"""
Start a section in the document outline.
If section_title_styles have been configured,
Expand Down Expand Up @@ -4728,7 +4735,11 @@ def table(self, *args, **kwargs):
table.render()

def output(
self, name="", dest="", linearize=False, output_producer_class=OutputProducer
self,
name: str = "",
dest: str = "",
linearize: bool = False,
output_producer_class: type = OutputProducer,
):
"""
Output PDF to some destination.
Expand Down Expand Up @@ -4788,7 +4799,7 @@ def _convert_to_drawing_color(r, g, b):
return drawing.DeviceRGB(r / 255, g / 255, b / 255)


def _is_svg(bytes):
def _is_svg(bytes) -> bool:
return bytes.startswith(b"<?xml ") or bytes.startswith(b"<svg ")


Expand Down
6 changes: 3 additions & 3 deletions fpdf/recorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class FPDFRecorder:
when generating large PDFs, doubling memory usage may be troublesome.
"""

def __init__(self, pdf, accept_page_break=True):
def __init__(self, pdf, accept_page_break: bool = True) -> None:
self.pdf = pdf
self._initial = deepcopy(self.pdf.__dict__)
self._calls = []
Expand All @@ -41,7 +41,7 @@ def rewind(self):
self.pdf.__dict__ = self._initial
self._initial = deepcopy(self.pdf.__dict__)

def replay(self):
def replay(self) -> None:
for call in self._calls:
func, args, kwargs = call
try:
Expand All @@ -59,7 +59,7 @@ def replay(self):


class CallRecorder:
def __init__(self, func, calls):
def __init__(self, func, calls) -> None:
self._func = func
self._calls = calls

Expand Down
Loading

0 comments on commit fca6b08

Please sign in to comment.