Skip to content

Commit a0c7a20

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

File tree

4 files changed

+27
-10
lines changed

4 files changed

+27
-10
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.char_index_for_last_forced_manual_break = None
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+
char_index_for_last_forced_manual_break = self.char_index_for_last_forced_manual_break
224+
self.char_index_for_last_forced_manual_break = None
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 char_index_for_last_forced_manual_break == self.character_index:
268+
raise FPDFException(
269+
"Not enough horizontal space to render a single character"
270+
)
271+
self.char_index_for_last_forced_manual_break = self.character_index
262272
return current_line.manual_break()
263273

264274
current_line.add_character(

test/text/test_line_break.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
from fpdf import FPDFException
12
from fpdf.line_break import Fragment, MultiLineBreak, TextLine
23

4+
import pytest
5+
36

47
def test_no_fragments():
58
"""
@@ -34,11 +37,10 @@ def test_width_calculation():
3437
assert multi_line_break.get_line_of_given_width(0) == TextLine(
3538
fragments=[], text_width=0, number_of_spaces_between_words=0, justify=False
3639
)
37-
# the first character has width of 2 units. request of 1 unit line returns
38-
# an empty line
39-
assert multi_line_break.get_line_of_given_width(1) == TextLine(
40-
fragments=[], text_width=0, number_of_spaces_between_words=0, justify=False
41-
)
40+
# the first character has width of 2 units.
41+
# request of 1 unit line raises an exception
42+
with pytest.raises(FPDFException):
43+
multi_line_break.get_line_of_given_width(1)
4244
# get other characters one by one
4345
assert multi_line_break.get_line_of_given_width(2) == TextLine(
4446
fragments=[Fragment.from_string("a", "normal", False)],

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)