From fc56c54c6eb33484da7fb52020eb69c2b1e0459a Mon Sep 17 00:00:00 2001 From: Lucas Cimon <925560+Lucas-C@users.noreply.github.com> Date: Thu, 29 Feb 2024 19:36:16 +0100 Subject: [PATCH] Adding enum values EVEN_ROWS & EVEN_COLUMNS --- CHANGELOG.md | 6 +++--- docs/HTML.md | 2 ++ docs/Tables.md | 3 ++- fpdf/enums.py | 14 +++++++++++-- test/table/table_cell_fill_mode.pdf | Bin 0 -> 2061 bytes test/table/test_table.py | 31 +++++++++++++++++++++++++--- 6 files changed, 47 insertions(+), 9 deletions(-) create mode 100644 test/table/table_cell_fill_mode.pdf 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 0000000000000000000000000000000000000000..6c6277403809325bde0cdaf2ce4218167f892529 GIT binary patch literal 2061 zcmai#YgiL!7RNzKYqO#dE
VKA|1aPmPI00YG_u#({6L4f5^xE;mFHsByE-z7n%h
8;~c+ zWS9Y-$`>ai2`B&t$YC}jONf)dk(B`6p5Sc}wip%5kQk#fc#H44m!q&8RRXSWastH) z?DwsM09GOi3JI(P=*E_KaU(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R?J~lWQfPqb%?GTgBgeX)9 zVv?~H1~Hw;WT(wW$w>-0nm`5^1U&v`0dyLTN&}*ScN!B|3<2_Y8WXz>zKk@8`R8*K zw?AnRjf`FSzjGlv8JmCqt1*mnQH5A4gB6QYH%NLI###mi2!aCm+{EUck31Ry@Tb7P zIe>`8qflIV91erdWl|^*k3)84Gd*{?v1xR&D~-$Kc>Mnpi?6Ii#R_!snIJbBnLr|O I19*gg1A($ZegFUf literal 0 HcmV?d00001 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)