Skip to content

Commit

Permalink
Improve PDF sequence spacing
Browse files Browse the repository at this point in the history
  • Loading branch information
SupraSummus committed Jan 10, 2024
1 parent 50455d2 commit 3186fed
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
14 changes: 9 additions & 5 deletions red_tape_kit/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions tests/test_renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 3186fed

Please sign in to comment.