Skip to content

Commit

Permalink
Improving fix for issue #389 (#392)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas-C authored Apr 18, 2022
1 parent 0cead69 commit 15e8e00
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 10 deletions.
5 changes: 0 additions & 5 deletions fpdf/fpdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2859,11 +2859,6 @@ def multi_cell(
# If width is 0, set width to available width between margins
if w == 0:
w = self.w - self.r_margin - self.x
if w <= 2 * self.c_margin:
raise FPDFException(
"Not enough horizontal space to render cell."
" Consider introducing a line break or using x_pos=XPos.LEFT in your previous call."
)
maximum_allowed_emwidth = (w - 2 * self.c_margin) * 1000 / self.font_size

# Calculate text length
Expand Down
12 changes: 12 additions & 0 deletions fpdf/line_break.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from typing import NamedTuple, Any, Union, Sequence

from .errors import FPDFException

SOFT_HYPHEN = "\u00ad"
HYPHEN = "\u002d"
SPACE = " "
Expand Down Expand Up @@ -208,6 +210,7 @@ def __init__(
self.print_sh = print_sh
self.fragment_index = 0
self.character_index = 0
self.char_index_for_last_forced_manual_break = None

def _get_character_width(self, character: str, style: str = ""):
if character == SOFT_HYPHEN and not self.print_sh:
Expand All @@ -217,6 +220,10 @@ def _get_character_width(self, character: str, style: str = ""):

# pylint: disable=too-many-return-statements
def get_line_of_given_width(self, maximum_width: float, wordsplit: bool = True):
char_index_for_last_forced_manual_break = (
self.char_index_for_last_forced_manual_break
)
self.char_index_for_last_forced_manual_break = None

if self.fragment_index == len(self.styled_text_fragments):
return None
Expand Down Expand Up @@ -259,6 +266,11 @@ def get_line_of_given_width(self, maximum_width: float, wordsplit: bool = True):
if not wordsplit:
line_full = True
break
if char_index_for_last_forced_manual_break == self.character_index:
raise FPDFException(
"Not enough horizontal space to render a single character"
)
self.char_index_for_last_forced_manual_break = self.character_index
return current_line.manual_break()

current_line.add_character(
Expand Down
12 changes: 7 additions & 5 deletions test/text/test_line_break.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from fpdf import FPDFException
from fpdf.line_break import Fragment, MultiLineBreak, TextLine

import pytest


def test_no_fragments():
"""
Expand Down Expand Up @@ -34,11 +37,10 @@ def test_width_calculation():
assert multi_line_break.get_line_of_given_width(0) == TextLine(
fragments=[], text_width=0, number_of_spaces_between_words=0, justify=False
)
# the first character has width of 2 units. request of 1 unit line returns
# an empty line
assert multi_line_break.get_line_of_given_width(1) == TextLine(
fragments=[], text_width=0, number_of_spaces_between_words=0, justify=False
)
# the first character has width of 2 units.
# request of 1 unit line raises an exception
with pytest.raises(FPDFException):
multi_line_break.get_line_of_given_width(1)
# get other characters one by one
assert multi_line_break.get_line_of_given_width(2) == TextLine(
fragments=[Fragment.from_string("a", "normal", False)],
Expand Down
10 changes: 10 additions & 0 deletions test/text/test_multi_cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,3 +314,13 @@ def test_multi_cell_with_zero_horizontal_space(): # issue #389
pdf.multi_cell(w=0, h=5, txt="test")
with pytest.raises(FPDFException):
pdf.multi_cell(w=0, h=5, txt="test")


def test_multi_cell_with_limited_horizontal_space(): # issue #389
pdf = FPDF()
pdf.add_page()
pdf.set_font("Helvetica", "", 10)
pdf.multi_cell(w=pdf.epw - 2 * pdf.c_margin - 1, h=5, txt="test")
assert pdf.x == pdf.l_margin + pdf.epw - 2 * pdf.c_margin - 1
with pytest.raises(FPDFException):
pdf.multi_cell(w=0, h=5, txt="test")

0 comments on commit 15e8e00

Please sign in to comment.