From 122173245133f0b172ef520ed9120b431f0a15fc Mon Sep 17 00:00:00 2001 From: Kaustbh Date: Wed, 16 Oct 2024 01:32:56 +0530 Subject: [PATCH] Added Cell level control for Table Borders --- fpdf/enums.py | 29 +++++++++++++++++++++++++++++ fpdf/table.py | 24 ++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/fpdf/enums.py b/fpdf/enums.py index 22cb84cb5..9067222a1 100644 --- a/fpdf/enums.py +++ b/fpdf/enums.py @@ -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" diff --git a/fpdf/table.py b/fpdf/table.py index 3d2528810..32ed990a3 100644 --- a/fpdf/table.py +++ b/fpdf/table.py @@ -11,6 +11,7 @@ WrapMode, VAlign, TableSpan, + CellBordersLayout, ) from .errors import FPDFException from .fonts import CORE_FONTS, FontFace @@ -251,6 +252,7 @@ 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. @@ -258,6 +260,24 @@ def get_cell_border(self, i, j, cell): 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: @@ -770,6 +790,7 @@ def cell( rowspan=1, padding=None, link=None, + border=CellBordersLayout.ALL, ): """ Adds a cell to the row. @@ -819,6 +840,7 @@ def cell( rowspan, padding, link, + border, ) self.cells.append(cell) return cell @@ -838,6 +860,7 @@ class Cell: "rowspan", "padding", "link", + "border", ) text: str align: Optional[Union[str, Align]] @@ -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")