Skip to content

Commit

Permalink
[Test] Adding test_toc_with_right_aligned_page_numbers() (#1123)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas-C authored Feb 27, 2024
1 parent 5553aee commit e85ec4e
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 74 deletions.
2 changes: 1 addition & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
max-line-length=150

# Disable superflous / noisy rules:
disable = attribute-defined-outside-init, use-dict-literal, invalid-name, method-hidden, missing-docstring, multiple-imports, too-few-public-methods, too-many-arguments, too-many-instance-attributes, too-many-nested-blocks, too-many-branches, too-many-lines, too-many-locals, too-many-public-methods, too-many-statements, wrong-import-order
disable = attribute-defined-outside-init, consider-using-max-builtin, consider-using-min-builtin, invalid-name, method-hidden, missing-docstring, multiple-imports, too-few-public-methods, too-many-arguments, too-many-instance-attributes, too-many-nested-blocks, too-many-branches, too-many-lines, too-many-locals, too-many-public-methods, too-many-statements, use-dict-literal, wrong-import-order

# Enable some rules that are not activated by default:
enable = bad-inline-option, deprecated-pragma, file-ignored, use-symbolic-message-instead, useless-suppression
Expand Down
2 changes: 1 addition & 1 deletion docs/Tables.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ TABLE_DATA = (
("Jules", "Smith", "34", "San Juan"),
("Mary", "Ramos", "45", "Orlando"),
("Carlson", "Banks", "19", "Los Angeles"),
("Lucas", "Cimon", "31", "Saint-Mahturin-sur-Loire"),
("Lucas", "Cimon", "31", "Saint-Mathurin-sur-Loire"),
)
pdf = FPDF()
pdf.add_page()
Expand Down
8 changes: 3 additions & 5 deletions fpdf/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,20 +459,18 @@ def handle_starttag(self, tag, attrs):
if tag == "br":
self._write_paragraph("\n")
if tag == "p":
align = None
if "align" in attrs:
align = attrs.get("align")[0].upper()
if not align in ["L", "R", "J", "C"]:
align = None
else:
align = None
line_height = None
if "line-height" in attrs:
try:
# YYY parse and convert non-float line_height values
line_height = float(attrs.get("line-height"))
except ValueError:
line_height = None
else:
line_height = None
pass
self._new_paragraph(align=align, line_height=line_height)
if tag in self.heading_sizes:
prev_font_height = self.font_size / self.pdf.k
Expand Down
159 changes: 92 additions & 67 deletions test/outline/test_outline.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,80 +13,16 @@
def test_simple_outline(tmp_path):
pdf = FPDF()
pdf.set_font("Helvetica")
pdf.set_section_title_styles(
# Level 0 titles:
TitleStyle(
font_family="Times",
font_style="B",
font_size_pt=24,
color=128,
underline=True,
t_margin=10,
l_margin=10,
b_margin=0,
),
# Level 1 subtitles:
TitleStyle(
font_family="Times",
font_style="B",
font_size_pt=20,
color=128,
underline=True,
t_margin=10,
l_margin=20,
b_margin=5,
),
)

pdf.add_page()
pdf.set_y(50)
pdf.set_font(size=40)
p(pdf, "Doc Title", align="C")
pdf.set_font(size=12)
pdf.insert_toc_placeholder(render_toc)
pdf.start_section("Title 1")
pdf.start_section("Subtitle 1.1", level=1)
p(
pdf,
(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit,"
" sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
),
)
pdf.add_page()
pdf.start_section("Subtitle 1.2", level=1)
p(
pdf,
"Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.",
)
pdf.add_page()
pdf.start_section("Title 2")
pdf.start_section("Subtitle 2.1", level=1)
p(
pdf,
"Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.",
)
pdf.add_page()
pdf.start_section("Subtitle 2.2", level=1)
p(
pdf,
"Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
)
insert_test_content(pdf)
assert_pdf_equal(pdf, HERE / "simple_outline.pdf", tmp_path)


def p(pdf, text, **kwargs):
"Inserts a paragraph"
pdf.multi_cell(
w=pdf.epw,
h=pdf.font_size,
text=text,
new_x="LMARGIN",
new_y="NEXT",
**kwargs,
)


def render_toc(pdf, outline):
pdf.y += 50
pdf.set_font("Helvetica", size=16)
Expand Down Expand Up @@ -205,7 +141,7 @@ def test_toc_with_russian_heading(tmp_path): # issue-320
pdf.add_page()
pdf.start_section("Русский, English, 1 2 3...")
pdf.write(8, "Русский текст в параграфе.")
assert_pdf_equal(pdf, HERE / "russian_heading.pdf", tmp_path)
assert_pdf_equal(pdf, HERE / "toc_with_russian_heading.pdf", tmp_path)


def test_toc_with_thai_headings(tmp_path): # issue-458
Expand All @@ -218,7 +154,7 @@ def test_toc_with_thai_headings(tmp_path): # issue-458
]:
pdf.add_page()
pdf.start_section(txt)
assert_pdf_equal(pdf, HERE / "thai_headings.pdf", tmp_path)
assert_pdf_equal(pdf, HERE / "toc_with_thai_headings.pdf", tmp_path)


def test_toc_without_font_style(tmp_path): # issue-676
Expand Down Expand Up @@ -257,3 +193,92 @@ def render_toc_with_table(pdf: FPDF, outline: list):
pdf.cell(text=str(i))
pdf.ln()
assert_pdf_equal(pdf, HERE / "toc_with_table.pdf", tmp_path)


def test_toc_with_right_aligned_page_numbers(tmp_path):
def render_toc_with_right_aligned_page_numbers(pdf, outline):
pdf.set_font("Helvetica", size=16)
for section in outline:
link = pdf.add_link(page=section.page_number)
pdf.cell(
text=f'{" " * section.level * 2} {section.name}',
link=link,
new_x="LEFT",
)
pdf.cell(text=f"{section.page_number}", link=link, w=pdf.epw, align="R")
pdf.ln()

pdf = FPDF()
pdf.set_font("Helvetica", size=12)
pdf.add_page()
pdf.insert_toc_placeholder(render_toc_with_right_aligned_page_numbers)
insert_test_content(pdf)
assert_pdf_equal(pdf, HERE / "toc_with_right_aligned_page_numbers.pdf", tmp_path)


def p(pdf, text, **kwargs):
"Inserts a paragraph"
pdf.multi_cell(
w=pdf.epw,
h=pdf.font_size,
text=text,
new_x="LMARGIN",
new_y="NEXT",
**kwargs,
)


def insert_test_content(pdf):
pdf.set_section_title_styles(
# Level 0 titles:
TitleStyle(
font_family="Times",
font_style="B",
font_size_pt=24,
color=128,
underline=True,
t_margin=10,
l_margin=10,
b_margin=0,
),
# Level 1 subtitles:
TitleStyle(
font_family="Times",
font_style="B",
font_size_pt=20,
color=128,
underline=True,
t_margin=10,
l_margin=20,
b_margin=5,
),
)

pdf.start_section("Title 1")
pdf.start_section("Subtitle 1.1", level=1)
p(
pdf,
(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit,"
" sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
),
)
pdf.add_page()
pdf.start_section("Subtitle 1.2", level=1)
p(
pdf,
"Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.",
)
pdf.add_page()
pdf.start_section("Title 2")
pdf.start_section("Subtitle 2.1", level=1)
p(
pdf,
"Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.",
)
pdf.add_page()
pdf.start_section("Subtitle 2.2", level=1)
p(
pdf,
"Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
)
Binary file not shown.
File renamed without changes.
File renamed without changes.

0 comments on commit e85ec4e

Please sign in to comment.