Skip to content

Commit

Permalink
Added Cell level control for Table Borders
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaustbh committed Oct 15, 2024
1 parent ab0bd4d commit 1221732
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
29 changes: 29 additions & 0 deletions fpdf/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,35 @@ class TableBordersLayout(CoerciveEnum):
"Draw only the top horizontal border, below the headings"


# pylint: disable=super-with-arguments
class CellBordersLayout(CoerciveIntFlag):
NONE = 0
LEFT = 1
RIGHT = 2
TOP = 4
BOTTOM = 8
ALL = LEFT | RIGHT | TOP | BOTTOM
INHERIT = 16

@classmethod
def coerce(cls, value):
if isinstance(value, int) and value > 16:
raise ValueError("INHERIT cannot be combined with other values")
return super(cls, cls).coerce(value)

def __and__(self, value):
value = super(CellBordersLayout, self).__and__(value)
if value > 16:
raise ValueError("INHERIT cannot be combined with other values")
return value

def __or__(self, value):
value = super(CellBordersLayout, self).__or__(value)
if value > 16:
raise ValueError("INHERIT cannot be combined with other values")
return value


class TableCellFillMode(CoerciveEnum):
"Defines which table cells to fill"

Expand Down
24 changes: 24 additions & 0 deletions fpdf/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
WrapMode,
VAlign,
TableSpan,
CellBordersLayout,
)
from .errors import FPDFException
from .fonts import CORE_FONTS, FontFace
Expand Down Expand Up @@ -251,13 +252,32 @@ def render(self):
self._fpdf.l_margin = prev_l_margin
self._fpdf.x = self._fpdf.l_margin

# pylint: disable=too-many-return-statements
def get_cell_border(self, i, j, cell):
"""
Defines which cell borders should be drawn.
Returns a string containing some or all of the letters L/R/T/B,
to be passed to `fpdf.FPDF.multi_cell()`.
Can be overriden to customize this logic
"""

if hasattr(cell, "border"):
border = CellBordersLayout.coerce(cell.border)

if border != CellBordersLayout.INHERIT:
border2 = []
if border == CellBordersLayout.ALL:
return "LRTB"
if border == CellBordersLayout.LEFT:
border2.append("L")
if border == CellBordersLayout.RIGHT:
border2.append("R")
if border == CellBordersLayout.TOP:
border2.append("T")
if border == CellBordersLayout.BOTTOM:
border2.append("B")
return "".join(border2)

if self._borders_layout == TableBordersLayout.ALL:
return 1
if self._borders_layout == TableBordersLayout.NONE:
Expand Down Expand Up @@ -770,6 +790,7 @@ def cell(
rowspan=1,
padding=None,
link=None,
border=CellBordersLayout.ALL,
):
"""
Adds a cell to the row.
Expand Down Expand Up @@ -819,6 +840,7 @@ def cell(
rowspan,
padding,
link,
border,
)
self.cells.append(cell)
return cell
Expand All @@ -838,6 +860,7 @@ class Cell:
"rowspan",
"padding",
"link",
"border",
)
text: str
align: Optional[Union[str, Align]]
Expand All @@ -849,6 +872,7 @@ class Cell:
rowspan: int
padding: Optional[Union[int, tuple, type(None)]]
link: Optional[Union[str, int]]
border: Optional[Union[str, int, CellBordersLayout]]

def write(self, text, align=None):
raise NotImplementedError("Not implemented yet")
Expand Down

0 comments on commit 1221732

Please sign in to comment.