diff --git a/CHANGELOG.md b/CHANGELOG.md index fd85f9329..d2763b548 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,11 +19,11 @@ This can also be enabled programmatically with `warnings.simplefilter('default', ## [2.7.9] - Not released yet ### Added * support for overriding paragraph direction on bidirectional text -* new optional `li_prefix_color` parameter for `FPDF.write_html()` -* support for `start` & `type` attributes of `
`, `- `... * [`FPDF.write_html()`](https://py-pdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.write_html) now accepts a `tag_indents` parameter to control, for example, the indent of `
` elements -* allow to define custom `cell_fill_mode` logic for tables: [_Set cells background_ - documentation section](https://py-pdf.github.io/fpdf2/Tables.html#set-cells-background) +* allow to define custom `cell_fill_mode` logic for tables: [_Set cells background_ - documentation section](https://py-pdf.github.io/fpdf2/Tables.html#set-cells-background). Also added 2 new values: `TableCellFillMode.EVEN_ROWS` & `TableCellFillMode.EVEN_COLUMNS`: [documentation](https://py-pdf.github.io/fpdf2/fpdf/enums.html#fpdf.enums.TableCellFillMode) ### Changed * improved the performance of `FPDF.start_section()` - _cf._ [issue #1092](https://github.com/py-pdf/fpdf2/issues/1092) ### Deprecated diff --git a/docs/HTML.md b/docs/HTML.md index 810d9e3d0..0610dd1fb 100644 --- a/docs/HTML.md +++ b/docs/HTML.md @@ -81,6 +81,8 @@ pdf.output("html.pdf") ## Styling HTML tags globally +_New in [:octicons-tag-24: 2.7.9](https://github.com/py-pdf/fpdf2/blob/master/CHANGELOG.md)_ + The style of several HTML tags (``, ``, ``, `
`, ``, `
`, `
`...) can be set globally, for the whole HTML document, by passing `tag_styles` to `FPDF.write_html()`: ```python diff --git a/docs/Tables.md b/docs/Tables.md index f572d6e38..15a183c5b 100644 --- a/docs/Tables.md +++ b/docs/Tables.md @@ -212,7 +212,8 @@ Finally, it is possible to define your own cell-filling logic: ```python class EvenOddCellFillMode(): - def should_fill_cell(self, i, j): + @staticmethod + def should_fill_cell(i, j): return i % 2 and j % 2 ... diff --git a/fpdf/enums.py b/fpdf/enums.py index 32b988a01..b8cd14074 100644 --- a/fpdf/enums.py +++ b/fpdf/enums.py @@ -317,6 +317,12 @@ class TableCellFillMode(CoerciveEnum): COLUMNS = intern("COLUMNS") "Fill only table cells in odd columns" + EVEN_ROWS = intern("EVEN_ROWS") + "Fill only table cells in even rows" + + EVEN_COLUMNS = intern("EVEN_COLUMNS") + "Fill only table cells in even columns" + @classmethod def coerce(cls, value): "Any class that has a .should_fill_cell() method is considered a valid 'TableCellFillMode' (duck-typing)" @@ -330,9 +336,13 @@ def should_fill_cell(self, i, j): if self is self.ALL: return True if self is self.ROWS: - return bool(i % 2) + return i % 2 == 1 if self is self.COLUMNS: - return bool(j % 2) + return j % 2 == 1 + if self is self.EVEN_ROWS: + return i % 2 == 0 + if self is self.EVEN_COLUMNS: + return j % 2 == 0 raise NotImplementedError diff --git a/test/table/table_cell_fill_mode.pdf b/test/table/table_cell_fill_mode.pdf new file mode 100644 index 000000000..6c6277403 Binary files /dev/null and b/test/table/table_cell_fill_mode.pdf differ diff --git a/test/table/test_table.py b/test/table/test_table.py index 4926f2033..ed51cc16a 100644 --- a/test/table/test_table.py +++ b/test/table/test_table.py @@ -6,6 +6,8 @@ from fpdf import FPDF, FPDFException from fpdf.drawing import DeviceRGB from fpdf.fonts import FontFace +from fpdf.table import TableCellFillMode + from test.conftest import assert_pdf_equal, LOREM_IPSUM @@ -267,8 +269,9 @@ def test_table_with_cell_fill(tmp_path): assert_pdf_equal(pdf, HERE / "table_with_cell_fill.pdf", tmp_path) -class EvenOddCellFillMode(): - def should_fill_cell(self, i, j): +class EvenOddCellFillMode: + @staticmethod + def should_fill_cell(i, j): return i % 2 and j % 2 @@ -277,7 +280,9 @@ def test_table_with_cell_fill_custom_class(tmp_path): pdf.add_page() pdf.set_font("Times", size=16) lightblue = (173, 216, 230) - with pdf.table(cell_fill_color=lightblue, cell_fill_mode=EvenOddCellFillMode()) as table: + with pdf.table( + cell_fill_color=lightblue, cell_fill_mode=EvenOddCellFillMode() + ) as table: for data_row in TABLE_DATA: row = table.row() for datum in data_row: @@ -750,3 +755,23 @@ def test_table_with_varying_col_count(tmp_path): table.row(subset) assert_pdf_equal(pdf, HERE / "table_with_varying_col_count.pdf", tmp_path) + + +def test_table_cell_fill_mode(tmp_path): + pdf = FPDF() + pdf.add_page() + pdf.set_font("Helvetica") + light_sky_blue = (150, 200, 255) + headings_style = FontFace(fill_color=128) # grey + for mode in ("ROWS", "COLUMNS", "EVEN_ROWS", "EVEN_COLUMNS"): + pdf.cell(text=f"cell_fill_mode=TableCellFillMode.{mode}:") + pdf.ln(10) + with pdf.table( + TABLE_DATA, + headings_style=headings_style, + cell_fill_mode=getattr(TableCellFillMode, mode), + cell_fill_color=light_sky_blue, + ): + pass + pdf.ln() + assert_pdf_equal(pdf, HERE / "table_cell_fill_mode.pdf", tmp_path)