Skip to content

Commit

Permalink
3.7?
Browse files Browse the repository at this point in the history
  • Loading branch information
qarmin committed Jun 29, 2023
1 parent afafaf4 commit 7a1acc6
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 30 deletions.
4 changes: 2 additions & 2 deletions fpdf/annotations.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import hashlib
from datetime import datetime
from typing import NamedTuple, Tuple, Union, Optional
from typing import NamedTuple, Tuple, Union, Optional, Dict

from .actions import Action
from .enums import AnnotationFlag, AnnotationName, FileAttachmentAnnotationName
Expand Down Expand Up @@ -130,7 +130,7 @@ def __init__(
):
super().__init__(contents=contents, compress=compress)
self.type = Name("EmbeddedFile")
params: dict[str, Union[int, str]] = {"/Size": len(contents)}
params: Dict[str, Union[int, str]] = {"/Size": len(contents)}
if creation_date:
params["/CreationDate"] = PDFDate(creation_date, with_tz=True).serialize()
if modification_date:
Expand Down
4 changes: 2 additions & 2 deletions fpdf/drawing.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import copy, decimal, math, re
from collections import OrderedDict
from contextlib import contextmanager
from typing import Optional, NamedTuple, Union
from typing import Optional, NamedTuple, Union, Dict

from .enums import (
BlendMode,
Expand All @@ -15,7 +15,7 @@
from .syntax import Name, Raw
from .util import escape_parens

__pdoc__: dict[str, Union[bool, str]] = {"force_nodocument": False}
__pdoc__: Dict[str, Union[bool, str]] = {"force_nodocument": False}


def force_nodocument(item):
Expand Down
84 changes: 61 additions & 23 deletions fpdf/fpdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from numbers import Number
from os.path import splitext
from pathlib import Path
from typing import Callable, NamedTuple, Optional, Union
from typing import Callable, NamedTuple, Optional, Union, List, Tuple

try:
from endesive import signer
Expand Down Expand Up @@ -1757,7 +1757,7 @@ def add_font(self, family=None, style="", fname=None, uni="DEPRECATED"):

self.fonts[fontkey] = TTFFont(self, font_file_path, fontkey, style)

def set_font(self, family=None, style="", size=0):
def set_font(self, family: Optional[str] = None, style: str = "", size: float = 0):
"""
Sets the font used to print character strings.
It is mandatory to call this method at least once before printing text.
Expand Down Expand Up @@ -1841,7 +1841,7 @@ def set_font(self, family=None, style="", size=0):
if self.page > 0:
self._out(f"BT /F{self.current_font.i} {self.font_size_pt:.2f} Tf ET")

def set_font_size(self, size):
def set_font_size(self, size: float):
"""
Configure the font size in points
Expand All @@ -1858,7 +1858,7 @@ def set_font_size(self, size):
)
self._out(f"BT /F{self.current_font.i} {self.font_size_pt:.2f} Tf ET")

def set_char_spacing(self, spacing):
def set_char_spacing(self, spacing: float):
"""
Sets horizontal character spacing.
A positive value increases the space between characters, a negative value
Expand All @@ -1874,7 +1874,7 @@ def set_char_spacing(self, spacing):
if self.page > 0:
self._out(f"BT {spacing:.2f} Tc ET")

def set_stretching(self, stretching):
def set_stretching(self, stretching: float):
"""
Sets horizontal font stretching.
By default, no stretching is set (which is equivalent to a value of 100).
Expand All @@ -1888,7 +1888,7 @@ def set_stretching(self, stretching):
if self.page > 0:
self._out(f"BT {stretching:.2f} Tz ET")

def set_fallback_fonts(self, fallback_fonts, exact_match=True):
def set_fallback_fonts(self, fallback_fonts: List[str], exact_match: bool = True):
"""
Allows you to specify a list of fonts to be used if any character is not available on the font currently set.
Detailed documentation: https://pyfpdf.github.io/fpdf2/Unicode.html#fallback-fonts
Expand Down Expand Up @@ -1916,7 +1916,7 @@ def set_fallback_fonts(self, fallback_fonts, exact_match=True):
self._fallback_font_ids = tuple(fallback_font_ids)
self._fallback_font_exact_match = exact_match

def add_link(self, y=0, x=0, page=-1, zoom="null"):
def add_link(self, y: float = 0, x: float = 0, page: int = -1, zoom: str = "null"):
"""
Creates a new internal link and returns its identifier.
An internal link is a clickable area which directs to another place within the document.
Expand Down Expand Up @@ -1944,7 +1944,9 @@ def add_link(self, y=0, x=0, page=-1, zoom="null"):
self.links[link_index] = link
return link_index

def set_link(self, link, y=0, x=0, page=-1, zoom="null"):
def set_link(
self, link: int, y: float = 0, x: float = 0, page: int = -1, zoom: str = "null"
):
"""
Defines the page and position a link points to.
Expand All @@ -1969,7 +1971,16 @@ def set_link(self, link, y=0, x=0, page=-1, zoom="null"):
link.zoom = zoom

@check_page
def link(self, x, y, w, h, link, alt_text=None, border_width=0):
def link(
self,
x: float,
y: float,
w: float,
h: float,
link,
alt_text: Optional[str] = None,
border_width: int = 0,
):
"""
Puts a link annotation on a rectangular area of the page.
Text or image links are generally put via `FPDF.cell`,
Expand Down Expand Up @@ -2020,9 +2031,9 @@ def link(self, x, y, w, h, link, alt_text=None, border_width=0):
def embed_file(
self,
file_path=None,
bytes=None,
basename=None,
modification_date=None,
bytes: Optional[bytes] = None,
basename: Optional[str] = None,
modification_date: Optional[datetime] = None,
**kwargs,
):
"""
Expand Down Expand Up @@ -2076,7 +2087,15 @@ def embed_file(

@check_page
def file_attachment_annotation(
self, file_path, x, y, w=1, h=1, name=None, flags=DEFAULT_ANNOT_FLAGS, **kwargs
self,
file_path,
x: float,
y: float,
w: float = 1,
h: float = 1,
name=None,
flags=DEFAULT_ANNOT_FLAGS,
**kwargs,
):
"""
Puts a file attachment annotation on a rectangular area of the page.
Expand Down Expand Up @@ -2114,7 +2133,14 @@ def file_attachment_annotation(

@check_page
def text_annotation(
self, x, y, text, w=1, h=1, name=None, flags=DEFAULT_ANNOT_FLAGS
self,
x: float,
y: float,
text: str,
w: float = 1,
h: float = 1,
name=None,
flags=DEFAULT_ANNOT_FLAGS,
):
"""
Puts a text annotation on a rectangular area of the page.
Expand Down Expand Up @@ -2142,7 +2168,7 @@ def text_annotation(
return annotation

@check_page
def add_action(self, action, x, y, w, h):
def add_action(self, action, x: float, y: float, w: float, h: float):
"""
Puts an Action annotation on a rectangular area of the page.
Expand All @@ -2166,7 +2192,12 @@ def add_action(self, action, x, y, w, h):

@contextmanager
def highlight(
self, text, title="", type="Highlight", color=(1, 1, 0), modification_time=None
self,
text: str,
title: str = "",
type="Highlight",
color: Tuple[float, float, float] = (1, 1, 0),
modification_time: Optional[datetime] = None,
):
"""
Context manager that adds a single highlight annotation based on the text lines inserted
Expand Down Expand Up @@ -2204,12 +2235,12 @@ def highlight(
def add_text_markup_annotation(
self,
type,
text,
text: str,
quad_points,
title="",
color=(1, 1, 0),
modification_time=None,
page=None,
color: Tuple[float, float, float] = (1, 1, 0),
modification_time: Optional[datetime] = None,
page: Optional[int] = None,
):
"""
Adds a text markup annotation on some quadrilateral areas of the page.
Expand Down Expand Up @@ -2255,7 +2286,12 @@ def add_text_markup_annotation(

@check_page
def ink_annotation(
self, coords, contents="", title="", color=(1, 1, 0), border_width=1
self,
coords,
contents: str = "",
title: str = "",
color: Tuple[float, float, float] = (1, 1, 0),
border_width: int = 1,
):
"""
Adds add an ink annotation on the page.
Expand Down Expand Up @@ -2290,7 +2326,7 @@ def ink_annotation(
return annotation

@check_page
def text(self, x, y, txt=""):
def text(self, x: float, y: float, txt: str = ""):
"""
Prints a character string. The origin is on the left of the first character,
on the baseline. This method allows placing a string precisely on the page,
Expand Down Expand Up @@ -2334,7 +2370,9 @@ def text(self, x, y, txt=""):
self._out(" ".join(sl))

@check_page
def rotate(self, angle, x=None, y=None):
def rotate(
self, angle: float, x: Optional[float] = None, y: Optional[float] = None
):
"""
.. deprecated:: 2.1.0
Use `FPDF.rotation()` instead.
Expand Down
4 changes: 2 additions & 2 deletions fpdf/line_break.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
They may change at any time without prior warning or any deprecation period.
"""

from typing import NamedTuple, Any, Optional, Union, Sequence
from typing import NamedTuple, Any, Optional, Union, Sequence, List

from .enums import CharVPos, WrapMode
from .errors import FPDFException
Expand Down Expand Up @@ -241,7 +241,7 @@ def __init__(self, print_sh: bool = False):
normally, instead of triggering a line break. Default: False
"""
self.print_sh = print_sh
self.fragments: list[Fragment] = []
self.fragments: List[Fragment] = []
self.width = 0
self.number_of_spaces = 0

Expand Down
3 changes: 2 additions & 1 deletion fpdf/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
__license__ = "LGPL 3.0"

import csv, locale, warnings
from typing import Optional

from .errors import FPDFException
from .fpdf import FPDF
Expand Down Expand Up @@ -673,7 +674,7 @@ def add_page(self):
self.pdf.add_page()

# pylint: disable=arguments-differ
def render(self, outfile=None, dest=None):
def render(self, outfile: Optional[str] = None, dest: Optional[str] = None):
"""
Finish the document and process all pending data.
Expand Down

0 comments on commit 7a1acc6

Please sign in to comment.