Skip to content

Commit

Permalink
ln() before text uses font_htight, fixes #937 (#940)
Browse files Browse the repository at this point in the history
* ln() before text uses font_htight, fixes #937

* more test files

* eliminate pylint implicit-str-concat warnings
  • Loading branch information
gmischler authored Oct 3, 2023
1 parent b7b48f3 commit 30eb1a4
Show file tree
Hide file tree
Showing 58 changed files with 225 additions and 92 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ This release is the first performed from the [@py-pdf GitHub org](https://github
* [`FPDF.table()`](https://py-pdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.table): If the height of a row is governed by an image, then the default vertical alignment of the other cells is "center". This was "top".
This change was made for consistency between row-height governed by text or images. The old behaviour can be enforced using the new vertical alignment parameter.
### Fixed
* [`FPDF.ln()`](https://py-pdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.ln), when called before any text has been written, will now use the current font height instead of doing nothing - _cf._ issue [#937](https://github.com/py-pdf/fpdf2/issues/937)
* [`FPDF.image()`](https://py-pdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.image), when provided a `BytesIO` instance, does not close it anymore - _cf._ issue [#881](https://github.com/py-pdf/fpdf2/issues/881)
* Invalid characters were being generated when a string contains parentheses - _cf._ issue [#884](https://github.com/py-pdf/fpdf2/issues/884)
* Frozen Glyph dataclass was causing problems for FPDFRecorder with TTF fonts - _cf._ issue [#890](https://github.com/py-pdf/fpdf2/issues/890)
Expand Down
19 changes: 10 additions & 9 deletions fpdf/deprecation.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
class WarnOnDeprecatedModuleAttributes(ModuleType):
def __call__(self):
raise TypeError(
# pylint: disable=implicit-str-concat
"You tried to instantied the fpdf module."
" You probably want to import the FPDF class instead:"
" from fpdf import FPDF"
Expand All @@ -25,10 +24,11 @@ def __call__(self):
def __getattr__(self, name):
if name in ("FPDF_CACHE_DIR", "FPDF_CACHE_MODE"):
warnings.warn(
# pylint: disable=implicit-str-concat
"fpdf.FPDF_CACHE_DIR & fpdf.FPDF_CACHE_MODE"
" have been deprecated in favour of"
" FPDF(font_cache_dir=...)",
(
"fpdf.FPDF_CACHE_DIR & fpdf.FPDF_CACHE_MODE"
" have been deprecated in favour of"
" FPDF(font_cache_dir=...)"
),
DeprecationWarning,
stacklevel=get_stack_level(),
)
Expand All @@ -38,10 +38,11 @@ def __getattr__(self, name):
def __setattr__(self, name, value):
if name in ("FPDF_CACHE_DIR", "FPDF_CACHE_MODE"):
warnings.warn(
# pylint: disable=implicit-str-concat
"fpdf.FPDF_CACHE_DIR & fpdf.FPDF_CACHE_MODE"
" have been deprecated in favour of"
" FPDF(font_cache_dir=...)",
(
"fpdf.FPDF_CACHE_DIR & fpdf.FPDF_CACHE_MODE"
" have been deprecated in favour of"
" FPDF(font_cache_dir=...)"
),
DeprecationWarning,
stacklevel=get_stack_level(),
)
Expand Down
1 change: 0 additions & 1 deletion fpdf/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ def __init__(self, argument, unknown=False, one=False):
super().__init__()
if unknown and one:
raise TypeError(
# pylint: disable=implicit-str-concat
"FPDF Page Format Exception cannot be both for "
"unknown type and for wrong number of arguments"
)
Expand Down
97 changes: 56 additions & 41 deletions fpdf/fpdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,10 @@ def __init__(
"""
if font_cache_dir != "DEPRECATED":
warnings.warn(
# pylint: disable=implicit-str-concat
'"font_cache_dir" parameter is deprecated since v2.5.1, '
"unused and will soon be removed",
(
'"font_cache_dir" parameter is deprecated since v2.5.1, '
"unused and will soon be removed"
),
DeprecationWarning,
stacklevel=get_stack_level(),
)
Expand Down Expand Up @@ -763,10 +764,11 @@ def set_doc_option(self, opt, value):
Simply set the `FPDF.core_fonts_encoding` property as a replacement.
"""
warnings.warn(
# pylint: disable=implicit-str-concat
"set_doc_option() is deprecated since v2.4.0 "
"and will be removed in a future release. "
"Simply set the `.core_fonts_encoding` property as a replacement.",
(
"set_doc_option() is deprecated since v2.4.0 "
"and will be removed in a future release. "
"Simply set the `.core_fonts_encoding` property as a replacement."
),
DeprecationWarning,
stacklevel=get_stack_level(),
)
Expand Down Expand Up @@ -1261,9 +1263,10 @@ def polyline(self, point_list, fill=False, polygon=False, style=None):
"""
if fill:
warnings.warn(
# pylint: disable=implicit-str-concat
'"fill" parameter is deprecated since v2.5.4, '
'use style="F" or style="DF" instead',
(
'"fill" parameter is deprecated since v2.5.4, '
'use style="F" or style="DF" instead'
),
DeprecationWarning,
stacklevel=get_stack_level(),
)
Expand Down Expand Up @@ -1318,10 +1321,11 @@ def dashed_line(self, x1, y1, x2, y2, dash_length=1, space_length=1):
Use `FPDF.set_dash_pattern()` and the normal drawing operations instead.
"""
warnings.warn(
# pylint: disable=implicit-str-concat
"dashed_line() is deprecated since v2.4.6, "
"and will be removed in a future release. "
"Use set_dash_pattern() and the normal drawing operations instead.",
(
"dashed_line() is deprecated since v2.4.6, "
"and will be removed in a future release. "
"Use set_dash_pattern() and the normal drawing operations instead."
),
DeprecationWarning,
stacklevel=get_stack_level(),
)
Expand Down Expand Up @@ -1801,9 +1805,10 @@ def add_font(self, family=None, style="", fname=None, uni="DEPRECATED"):

if uni != "DEPRECATED":
warnings.warn(
# pylint: disable=implicit-str-concat
'"uni" parameter is deprecated since v2.5.1, '
"unused and will soon be removed",
(
'"uni" parameter is deprecated since v2.5.1, '
"unused and will soon be removed"
),
DeprecationWarning,
stacklevel=get_stack_level(),
)
Expand Down Expand Up @@ -2421,10 +2426,11 @@ def rotate(self, angle, x=None, y=None):
Use `FPDF.rotation()` instead.
"""
warnings.warn(
# pylint: disable=implicit-str-concat
"rotate() can produces malformed PDFs and is deprecated since v2.1.0. "
"It will be removed in a future release. "
"Use the rotation() context manager instead.",
(
"rotate() can produces malformed PDFs and is deprecated since v2.1.0. "
"It will be removed in a future release. "
"Use the rotation() context manager instead."
),
DeprecationWarning,
stacklevel=get_stack_level(),
)
Expand Down Expand Up @@ -2726,7 +2732,6 @@ def cell(
raise FPDFException("No font set, you need to call set_font() beforehand")
if isinstance(w, str) or isinstance(h, str):
raise ValueError(
# pylint: disable=implicit-str-concat
"Parameter 'w' and 'h' must be numbers, not strings."
" You can omit them by passing string content with txt="
)
Expand Down Expand Up @@ -3408,9 +3413,10 @@ def multi_cell(

if split_only:
warnings.warn(
# pylint: disable=implicit-str-concat
'The parameter "split_only" is deprecated since v2.7.4.'
' Use instead dry_run=True and output="LINES".',
(
'The parameter "split_only" is deprecated since v2.7.4.'
' Use instead dry_run=True and output="LINES".'
),
DeprecationWarning,
stacklevel=get_stack_level(),
)
Expand Down Expand Up @@ -3442,7 +3448,6 @@ def multi_cell(
wrapmode = WrapMode.coerce(wrapmode)
if isinstance(w, str) or isinstance(h, str):
raise ValueError(
# pylint: disable=implicit-str-concat
"Parameter 'w' and 'h' must be numbers, not strings."
" You can omit them by passing string content with txt="
)
Expand Down Expand Up @@ -3676,7 +3681,6 @@ def write(
raise FPDFException("No font set, you need to call set_font() beforehand")
if isinstance(h, str):
raise ValueError(
# pylint: disable=implicit-str-concat
"Parameter 'h' must be a number, not a string."
" You can omit it by passing string content with txt="
)
Expand Down Expand Up @@ -3795,9 +3799,10 @@ def image(
"""
if type:
warnings.warn(
# pylint: disable=implicit-str-concat
'"type" parameter is deprecated since v2.2.0, '
"unused and will soon be removed",
(
'"type" parameter is deprecated since v2.2.0, '
"unused and will soon be removed"
),
DeprecationWarning,
stacklevel=get_stack_level(),
)
Expand Down Expand Up @@ -4019,9 +4024,10 @@ def _downscale_image(self, name, img, info, w, h):
)
if self.oversized_images.lower().startswith("warn"):
LOGGER.warning(
# pylint: disable=implicit-str-concat
"OVERSIZED: Image %s with size %.1fx%.1fpx is rendered at size %.1fx%.1fpt."
" Set pdf.oversized_images = 'DOWNSCALE' to reduce embedded image size by a factor %.1f",
(
"OVERSIZED: Image %s with size %.1fx%.1fpx is rendered at size %.1fx%.1fpt."
" Set pdf.oversized_images = 'DOWNSCALE' to reduce embedded image size by a factor %.1f"
),
name,
info["w"],
info["h"],
Expand Down Expand Up @@ -4115,10 +4121,17 @@ def ln(self, h=None):
Args:
h (float): The height of the break.
By default, the value equals the height of the last printed cell.
By default, the value equals the height of the last printed text line
(except when written by `.text()`). If no text has been written yet to
the document, then the current font height is used.
"""
self.x = self.l_margin
self.y += self._lasth if h is None else h
if h:
self.y += h
elif self._lasth:
self.y += self._lasth
else:
self.y += self.font_size

def get_x(self):
"""Returns the abscissa of the current position."""
Expand Down Expand Up @@ -4434,9 +4447,10 @@ def code39(self, txt, x, y, w=1.5, h=5):
dim = {"w": w, "n": w / 3}
if not txt.startswith("*") or not txt.endswith("*"):
warnings.warn(
# pylint: disable=implicit-str-concat
"Code 39 input must start and end with a '*' character to be valid."
" This method does not insert it automatically.",
(
"Code 39 input must start and end with a '*' character to be valid."
" This method does not insert it automatically."
),
stacklevel=get_stack_level(),
)
chars = {
Expand Down Expand Up @@ -4812,9 +4826,10 @@ def output(
"""
if dest:
warnings.warn(
# pylint: disable=implicit-str-concat
'"dest" parameter is deprecated since v2.2.0, '
"unused and will soon be removed",
(
'"dest" parameter is deprecated since v2.2.0, '
"unused and will soon be removed"
),
DeprecationWarning,
stacklevel=get_stack_level(),
)
Expand Down
7 changes: 4 additions & 3 deletions fpdf/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -736,9 +736,10 @@ class HTMLMixin:
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
warnings.warn(
# pylint: disable=implicit-str-concat
"The HTMLMixin class is deprecated since v2.6.0. "
"Simply use the FPDF class as a replacement.",
(
"The HTMLMixin class is deprecated since v2.6.0. "
"Simply use the FPDF class as a replacement."
),
DeprecationWarning,
stacklevel=get_stack_level(),
)
1 change: 0 additions & 1 deletion fpdf/svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ def force_nodocument(item):

NUMBER_SPLIT = re.compile(r"(?:\s+,\s+|\s+,|,\s+|\s+|,)")
TRANSFORM_GETTER = re.compile(
# pylint: disable=implicit-str-concat
r"(matrix|rotate|scale|scaleX|scaleY|skew|skewX|skewY|translate|translateX|translateY)"
r"\(((?:\s*(?:[-+]?[\d\.]+,?)+\s*)+)\)"
)
Expand Down
1 change: 0 additions & 1 deletion fpdf/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,6 @@ def cell(
"""
if text and img:
raise NotImplementedError(
# pylint: disable=implicit-str-concat
"fpdf2 currently does not support inserting text with an image in the same table cell."
"Pull Requests are welcome to implement this 😊"
)
Expand Down
7 changes: 4 additions & 3 deletions fpdf/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,9 +467,10 @@ def _code39(
):
if x is not None or y is not None or w is not None or h is not None:
warnings.warn(
# pylint: disable=implicit-str-concat
"code39 arguments x/y/w/h are deprecated since v2.4.4,"
" please use x1/y1/y2/size instead",
(
"code39 arguments x/y/w/h are deprecated since v2.4.4,"
" please use x1/y1/y2/size instead"
),
DeprecationWarning,
stacklevel=get_stack_level(),
)
Expand Down
2 changes: 0 additions & 2 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,13 @@
QPDF_AVAILABLE = bool(shutil.which("qpdf"))
if not QPDF_AVAILABLE:
warnings.warn(
# pylint: disable=implicit-str-concat
"qpdf command not available on the $PATH, falling back to hash-based "
"comparisons in tests"
)

EPOCH = datetime(1969, 12, 31, 19, 00, 00).replace(tzinfo=timezone.utc)

LOREM_IPSUM = (
# pylint: disable=implicit-str-concat
"Lorem ipsum Ut nostrud irure reprehenderit anim nostrud dolore sed "
"ut Excepteur dolore ut sunt irure consectetur tempor eu tempor "
"nostrud dolore sint exercitation aliquip velit ullamco esse dolore "
Expand Down
Loading

0 comments on commit 30eb1a4

Please sign in to comment.