Skip to content

Commit

Permalink
fixed draw_box borders
Browse files Browse the repository at this point in the history
  • Loading branch information
RubendeBruin committed Jul 21, 2023
1 parent d77e88a commit 11cb956
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
14 changes: 12 additions & 2 deletions fpdf/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ def draw_box(pdf, x1, y1, x2, y2, border, fill = None):
if isinstance(border, str):
if "L" in border:
sl.append(f"{x1:.2f} {y2:.2f} m " f"{x1:.2f} {y1:.2f} l S")
if "T" in border:
if "B" in border:
sl.append(f"{x1:.2f} {y2:.2f} m " f"{x2:.2f} {y2:.2f} l S")
if "R" in border:
sl.append(f"{x2:.2f} {y2:.2f} m " f"{x2:.2f} {y1:.2f} l S")
if "B" in border:
if "T" in border:
sl.append(f"{x1:.2f} {y1:.2f} m " f"{x2:.2f} {y1:.2f} l S")

s = " ".join(sl)
Expand Down Expand Up @@ -169,6 +169,15 @@ def __init__(
else:
self._padding = get_padding_tuple(padding)

# check table_border_layout and outer_border_width
if self._borders_layout != TableBordersLayout.ALL:
if outer_border_width is not None:
raise ValueError(
"outer_border_width is not allowed when borders_layout is not ALL"
)
self._outer_border_width = 0


for row in rows:
self.row(row)

Expand Down Expand Up @@ -399,6 +408,7 @@ def _render_table_cell(
)

# draw outer box if needed

if self._outer_border_width:

_remember_linewidth = self._fpdf.line_width
Expand Down
64 changes: 64 additions & 0 deletions test/table/table_padding.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,29 @@ def test_table_colspan_and_padding_and_gutter(tmp_path):

show(pdf)

def test_table_colspan_and_padding_and_gutter_and_width(tmp_path):
pdf = FPDF()

pdf.set_font("Times", size=12)
pdf.add_page()
with pdf.table(col_widths=(1, 2, 1,1), padding=5, gutter_height=3, gutter_width=5, width=100) as table:
row = table.row()
row.cell("0")
row.cell("1")
row.cell("2")
row.cell("3")
row = table.row()
row.cell("A1")
row.cell("A2,3,4", colspan=3)

row = table.row()
row.cell("B1", colspan=2)
row.cell("B3")
row.cell("B4")


show(pdf)

def test_table_with_cell_overflow(tmp_path):
pdf = FPDF()
pdf.set_font("Times", size=30)
Expand Down Expand Up @@ -509,3 +532,44 @@ def test_table_with_cell_overflow(tmp_path):
show(pdf)
# assert_pdf_equal(pdf, HERE / "table_with_cell_overflow.pdf", tmp_path)

TABLE_DATA_OLD = (
("First name", "Last name", "Age", "City"),
("Jules", "Smith", "34", "San Juan"),
("Mary", "Ramos", "45", "Orlando"),
("Carlson", "Banks", "19", "Los Angeles"),
("Lucas", "Cimon", "31", "Angers"),
)

def test_table_with_varying_col_widths(tmp_path):
pdf = FPDF()
pdf.add_page()
pdf.set_font("Times", size=16)
with pdf.table(col_widths=(30, 30, 10, 30)) as table:
for data_row in TABLE_DATA_OLD:
row = table.row()
for datum in data_row:
row.cell(datum)
assert_pdf_equal(pdf, HERE / "table_with_varying_col_widths.pdf", tmp_path)



def test_draw_box(tmp_path):
pdf = FPDF()
pdf.set_font("Times", size=16)
pdf.add_page()

from fpdf.table import draw_box

def box(x,y, borders):
draw_box(pdf, x-10, y-10, x+40, y+20, fill=(200,200,200), border="None")
draw_box(pdf, x-20,y-20,x+50, y+30,border=borders)
pdf.set_xy(x,y)
pdf.cell(txt=borders)

box(40,40, "L")
box(140, 40, "R")

box(40, 140, "T")
box(140, 140, "B")

show(pdf)

0 comments on commit 11cb956

Please sign in to comment.