Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Table of Contents with different orientation, font style and size #1122

Open
CayhanBoran opened this issue Feb 27, 2024 · 3 comments
Open

Table of Contents with different orientation, font style and size #1122

CayhanBoran opened this issue Feb 27, 2024 · 3 comments

Comments

@CayhanBoran
Copy link

CayhanBoran commented Feb 27, 2024

Hey everyone,
I am trying to create a table of contents with different font style for landscape format. The aligments of dots in the texts are not aligning. I tried different formats, but the only best option is using the Courier format.

used function for table of contents:

        pdf.set_font("Arial", size=12)
        for section in outline:

            link = pdf.add_link(page=section.page_number, x=80, y=0)
            if pdf.get_y() >= 170: # passing the header for the next page
                pdf.page += 1
                pdf.set_y(35)
                pdf.set_x(10)
                pdf.cell(w=pdf.epw,
                         h=pdf.font_size,
                         text=f'{" " * section.level * 2} {section.name} {"." * (60 - section.level*2 - len(section.name))} {section.page_number}',
                         new_x="LMARGIN",
                         new_y="NEXT",
                         align="C",
                         link=link)  # type: ignore
            else:
                pdf.cell(w=pdf.epw,
                         h=pdf.font_size,
                         text=f'{" " * section.level * 2} {section.name} {"." * (60 - section.level*2 - len(section.name))} {section.page_number}',
                         new_x="LMARGIN",
                         new_y="NEXT",
                         align="C",
                         link=link)  # type: ignore

If I make it with Courier and font size 12:

courier_font

If I make it with Arial and font size 12:

arial_font

If you could help me and solve this issue of font style in table of contents, I would be really happy.
Thanks!

@Lucas-C
Copy link
Member

Lucas-C commented Feb 27, 2024

Hi @CayhanBoran!

Basically your issue is that Courrier is a monospace (or fixed-width) font: https://en.wikipedia.org/wiki/Monospaced_font
Whereas Arial is not monospace, leading to varying alignements in the resulting table of content.

You could try using a render function that right-aligns page numbers, instead of using dots, like this:

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()

Does this answer solve your issue? 🙂

@CayhanBoran
Copy link
Author

Yes solves partially, I wanted to use Helvetica or Arial with dots, but if that is not the case. I guess I will not use dots in the table of contents.
Thanks.

@gmischler
Copy link
Collaborator

The underlying issue is that using dots to align the page number is a very ugly hack, which was cobbled together decades ago, and never fixed.

Ideally, someone would submit a PR creating a new and more functional TOC class, which contains adaptable TocEntry instances.
Desirable features might be:

  • Text part of each TocEntry implemented as a TextRegion, so it can use varying fonts for multilingual entries, possible spanning several lines.
  • Ability to vary the total width of the TOC, in absolute terms or relative to the page width.
  • Right aligned page numbers column with separately configurable font.
  • Multicolumn TOCs.
    • Autowrapping.
    • With configuration to wrap between individual entries, or between higher level chapter headings.
  • Configurable visual element to connect text and page number columns.
    • Automatically adapting to the available width between text and page number.
    • This could be dots, dashes, a line, some graphic, etc, in a configurable color (or nothing).
    • With multiline entries, configurable whether the connection (and page number) vertically aligns with the top or bottom of the entry.
  • All the bells and whistles I didn't think about.

Any takers?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants