Skip to content

Commit

Permalink
all tests running through
Browse files Browse the repository at this point in the history
  • Loading branch information
gmischler committed Jun 16, 2024
1 parent 1300ada commit 40a0235
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 33 deletions.
4 changes: 3 additions & 1 deletion fpdf/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ def __init__(
self._gutter_height = gutter_height
self._gutter_width = gutter_width
self._headings_style = headings_style
self._line_height = 2 * fpdf.font_size if line_height is None else line_height
abs_line_height = 2 * fpdf.font_size if line_height is None else line_height
self.line_height = abs_line_height / fpdf.font_size
self._markdown = markdown
self.text_align = text_align
self._width = fpdf.epw if width is None else width
Expand Down Expand Up @@ -494,6 +495,7 @@ def cell(
text=text,
text_align=align if align else self._table.text_align,
v_align=v_align if v_align else self._table.v_align,
line_height=line_height if line_height else self._table.line_height,
style=style or self.style,
img=img,
img_fill_width=img_fill_width,
Expand Down
61 changes: 34 additions & 27 deletions fpdf/text_region.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,10 @@ def __init__(
f"Align must be 'LEFT', 'CENTER', or 'RIGHT', not '{align.value}'."
)
self.align = align
self.width = width
self.height = height
self._req_width = width
self.width = width or 0.0 # set in build_line().
self._req_height = height
self.height = height or 0.0 # set in build_line().
self.fill_width = fill_width
self.keep_aspect_ratio = keep_aspect_ratio
self.top_margin = top_margin
Expand All @@ -179,13 +181,31 @@ def __init__(
self.title = title
self.alt_text = alt_text
self.img = self.info = None
self.line = self # mimick a text line wrapper

def build_line(self):
# We do double duty as a "text line wrapper" here, since all the necessary
# information is already in the ImageParagraph object.
#print('img - region: ', self.region)
col_left, col_right = self.region.current_x_extents(self.region.pdf.y, 0)
col_width = col_right - col_left
self.name, self.img, self.info = preload_image(
self.region.pdf.image_cache, self.name
)
if self._req_height:
self.height = self._req_height
else:
native_h = self.info["h"] / self.region.pdf.k
if self._req_width:
self.width = self._req_width
else:
native_w = self.info["w"] / self.region.pdf.k
if native_w > col_width or self.fill_width:
self.width = col_width
else:
self.width = native_w
if not self._req_height:
self.height = self.width * native_h / native_w
# We do double duty as a "text line wrapper" here, since all the necessary
# information is already in the ImageParagraph object.
return self

def render(self, col_left, col_width, max_height):
Expand All @@ -195,37 +215,24 @@ def render(self, col_left, col_width, max_height):
)
is_svg = isinstance(self.info, VectorImageInfo)

# pylint: disable=possibly-used-before-assignment
if self.height:
h = self.height
else:
native_h = self.info["h"] / self.region.pdf.k
if self.width:
w = self.width
else:
native_w = self.info["w"] / self.region.pdf.k
if native_w > col_width or self.fill_width:
w = col_width
else:
w = native_w
if not self.height:
h = w * native_h / native_w
if h > max_height:
# xpylint: disable=possibly-used-before-assignment

if self.height > max_height:
return None
x = col_left
if self.align:
if self.align == Align.R:
x += col_width - w
x += col_width - self.width
elif self.align == Align.C:
x += (col_width - w) / 2
x += (col_width - self.width) / 2
if is_svg:
return self.region.pdf._vector_image(
svg=self.img,
info=self.info,
x=x,
y=None,
w=w,
h=h,
w=self.width,
h=self.height,
link=self.link,
title=self.title,
alt_text=self.alt_text,
Expand All @@ -237,8 +244,8 @@ def render(self, col_left, col_width, max_height):
info=self.info,
x=x,
y=None,
w=w,
h=h,
w=self.width,
h=self.height,
link=self.link,
title=self.title,
alt_text=self.alt_text,
Expand Down Expand Up @@ -499,7 +506,7 @@ class TextColumnarMixin:
"""Enable a TextRegion to perform page breaks"""

def __init__(self, pdf, *args, l_margin=None, r_margin=None, **kwargs):
print('args:', args, kwargs)
#print('args:', args, kwargs)
super().__init__(*args, **kwargs)
self.l_margin = pdf.l_margin if l_margin is None else l_margin
left = self.l_margin
Expand Down
5 changes: 0 additions & 5 deletions test/table/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ def test_table_with_multiline_cells(tmp_path):
row = table.row()
for datum in data_row:
row.cell(datum)
assert pdf.pages_count == 2
assert_pdf_equal(pdf, HERE / "table_with_multiline_cells.pdf", tmp_path)


Expand All @@ -159,8 +158,6 @@ def test_table_with_multiline_cells_and_fixed_row_height(tmp_path):
row = table.row()
for datum in data_row:
row.cell(datum)
assert pdf.pages_count == 2

assert_pdf_equal(
pdf, HERE / "table_with_multiline_cells_and_fixed_row_height.pdf", tmp_path
)
Expand Down Expand Up @@ -211,7 +208,6 @@ def test_table_with_multiline_cells_and_without_headings(tmp_path):
row = table.row()
for datum in data_row:
row.cell(datum)
assert pdf.pages_count == 4
assert_pdf_equal(
pdf,
HERE / "table_with_multiline_cells_and_without_headings.pdf",
Expand Down Expand Up @@ -243,7 +239,6 @@ def test_table_with_multiline_cells_and_split_over_3_pages(tmp_path):
row = table.row()
for datum in data_row:
row.cell(datum)
assert pdf.pages_count == 4
assert_pdf_equal(
pdf, HERE / "table_with_multiline_cells_and_split_over_3_pages.pdf", tmp_path
)
Expand Down

0 comments on commit 40a0235

Please sign in to comment.