Skip to content

Commit 6bfb440

Browse files
committed
Improving fix for issue #389
1 parent 0cead69 commit 6bfb440

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

fpdf/fpdf.py

-5
Original file line numberDiff line numberDiff line change
@@ -2859,11 +2859,6 @@ def multi_cell(
28592859
# If width is 0, set width to available width between margins
28602860
if w == 0:
28612861
w = self.w - self.r_margin - self.x
2862-
if w <= 2 * self.c_margin:
2863-
raise FPDFException(
2864-
"Not enough horizontal space to render cell."
2865-
" Consider introducing a line break or using x_pos=XPos.LEFT in your previous call."
2866-
)
28672862
maximum_allowed_emwidth = (w - 2 * self.c_margin) * 1000 / self.font_size
28682863

28692864
# Calculate text length

fpdf/line_break.py

+10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from typing import NamedTuple, Any, Union, Sequence
22

3+
from .errors import FPDFException
4+
35
SOFT_HYPHEN = "\u00ad"
46
HYPHEN = "\u002d"
57
SPACE = " "
@@ -208,6 +210,7 @@ def __init__(
208210
self.print_sh = print_sh
209211
self.fragment_index = 0
210212
self.character_index = 0
213+
self.last_line_is_forced_manual_break = False
211214

212215
def _get_character_width(self, character: str, style: str = ""):
213216
if character == SOFT_HYPHEN and not self.print_sh:
@@ -217,6 +220,8 @@ def _get_character_width(self, character: str, style: str = ""):
217220

218221
# pylint: disable=too-many-return-statements
219222
def get_line_of_given_width(self, maximum_width: float, wordsplit: bool = True):
223+
last_line_is_forced_manual_break = self.last_line_is_forced_manual_break
224+
self.last_line_is_forced_manual_break = False
220225

221226
if self.fragment_index == len(self.styled_text_fragments):
222227
return None
@@ -259,6 +264,11 @@ def get_line_of_given_width(self, maximum_width: float, wordsplit: bool = True):
259264
if not wordsplit:
260265
line_full = True
261266
break
267+
if last_line_is_forced_manual_break:
268+
raise FPDFException(
269+
"Not enough horizontal space to render a single character"
270+
)
271+
self.last_line_is_forced_manual_break = True
262272
return current_line.manual_break()
263273

264274
current_line.add_character(

test/text/test_multi_cell.py

+10
Original file line numberDiff line numberDiff line change
@@ -314,3 +314,13 @@ def test_multi_cell_with_zero_horizontal_space(): # issue #389
314314
pdf.multi_cell(w=0, h=5, txt="test")
315315
with pytest.raises(FPDFException):
316316
pdf.multi_cell(w=0, h=5, txt="test")
317+
318+
319+
def test_multi_cell_with_limited_horizontal_space(): # issue #389
320+
pdf = FPDF()
321+
pdf.add_page()
322+
pdf.set_font("Helvetica", "", 10)
323+
pdf.multi_cell(w=pdf.epw - 2 * pdf.c_margin - 1, h=5, txt="test")
324+
assert pdf.x == pdf.l_margin + pdf.epw - 2 * pdf.c_margin - 1
325+
with pytest.raises(FPDFException):
326+
pdf.multi_cell(w=0, h=5, txt="test")

0 commit comments

Comments
 (0)