From 3186fedbe227c40b7a1625e38910567ad8a7f4d8 Mon Sep 17 00:00:00 2001 From: Jan Rydzewski Date: Wed, 10 Jan 2024 11:06:43 +0100 Subject: [PATCH] Improve PDF sequence spacing --- red_tape_kit/pdf.py | 14 +++++++++----- tests/test_renderers.py | 27 +++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/red_tape_kit/pdf.py b/red_tape_kit/pdf.py index 021adc7..413fc3f 100644 --- a/red_tape_kit/pdf.py +++ b/red_tape_kit/pdf.py @@ -19,7 +19,10 @@ class FPDFRenderer(FPDF): MARGIN_RIGHT = 20 # mm MARGIN_TOP = 20 # mm MARGIN_BOTTOM = 20 # mm + SEQUENCE_SPACE = 7 # mm + UNORDERED_LIST_HSPACE = 7 # mm UNORDERED_LIST_BULLET = '-' + UNORDERED_LIST_BULLET_SPACE = 5 # mm def __init__(self, document, **kwargs): super().__init__(**kwargs, unit='mm', format='A4') @@ -114,7 +117,7 @@ def add_sequence(self, sequence, level): ln_required = False for sub_element in sequence.items: if ln_required: - self.ln(7) + self.ln(self.SEQUENCE_SPACE) ln_required = self.add_element(sub_element, level) if ln_required: anything_generated = True @@ -125,7 +128,8 @@ def add_section(self, section, level): return self.add_element(section.body, level + 1) def add_paragraph(self, paragraph): - return self.add_inline_element(paragraph.text) + self.add_inline_element(paragraph.text) + return True def add_table(self, table_data): with self.table( @@ -150,11 +154,11 @@ def add_elementary_table(self, pdf_table, elementary_table): def add_unordered_list(self, unordered_list): orig_left_margin = self.l_margin - new_left_margin = orig_left_margin + 5 + new_left_margin = orig_left_margin + self.UNORDERED_LIST_BULLET_SPACE self.set_left_margin(new_left_margin) for i, item in enumerate(unordered_list.items): if i > 0: - self.ln(7) + self.ln(self.UNORDERED_LIST_HSPACE) with self.unbreakable() as self: self.set_x(orig_left_margin) self.cell(text=self.UNORDERED_LIST_BULLET) @@ -191,7 +195,7 @@ def add_inline_element(self, inline_element): def add_text(self, text): self.write_lh(text.text) - return text != '' + return text.text != '' def add_inline_sequence(self, inline_sequence): anything_generated = False diff --git a/tests/test_renderers.py b/tests/test_renderers.py index 42cabf6..4b60b9d 100644 --- a/tests/test_renderers.py +++ b/tests/test_renderers.py @@ -142,6 +142,33 @@ def test_not_dependent_on_current_date(render): assert a == b +@pytest.mark.xfail(reason='Preserving final ln in fpdf is hard') +def test_empty_paragraph_at_the_end_is_represented(render): + doc_a = DocumentFactory( + body=Sequence([ + Paragraph(text=Text('A')), + Paragraph(text=Text('')), + ]), + ) + doc_b = DocumentFactory( + body=Paragraph(text=Text('A')), + ) + assert render(doc_a) != render(doc_b) + + +def test_empty_paragraph_is_represented(render): + doc_a = DocumentFactory( + body=Sequence([ + Paragraph(text=Text('')), + Paragraph(text=Text('A')), + ]), + ) + doc_b = DocumentFactory( + body=Paragraph(text=Text('A')), + ) + assert render(doc_a) != render(doc_b) + + def test_empty_attachment_no_smoke(render): doc = DocumentFactory( body=Paragraph(text=Attachment(