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

write_html() ignores font changes #1220

Closed
gmischler opened this issue Jul 8, 2024 · 5 comments · Fixed by #1246
Closed

write_html() ignores font changes #1220

gmischler opened this issue Jul 8, 2024 · 5 comments · Fixed by #1246

Comments

@gmischler
Copy link
Collaborator

gmischler commented Jul 8, 2024

Error details
When write_html() encounters eg. a <font face="helvetica">, then it will process that but the changed font will not necessarily be reflected in the output.

Minimal code
The "html_features" test shows the problem, where the "hello helvetica" line is now actually rendered in Times.

It seems that this has happened in #1207. In my comments there I had tried to point out the reasons why the graphics state context switches had previously been done the way they were, but apparently I didn't make their purpose sufficiently clear. I also didn't notice at the time that the font handling got broken.

@Lucas-C
Copy link
Member

Lucas-C commented Jul 8, 2024

Thank you for the bug report @gmischler 👍

Given that I probably caused this regression, I will have a look at this myself during the week.

@Lucas-C
Copy link
Member

Lucas-C commented Jul 10, 2024

While investigating this issue, I was surprised by the behaviour of the following code snippets:

pdf = FPDF()
pdf.add_page()
pdf.set_font("Helvetica", size=32)
with pdf.text_columns() as cols:
    with cols.paragraph() as par:
        par.write(text="Un.")
    pdf.font_style = "B"
    with cols.paragraph() as par:
        par.write(text="Deux.")
        pdf.font_style = "I"
        par.write(text="Trois.")
        print(cols._paragraphs[0]._text_fragments[0].graphics_state["font_style"])  # ""
        print(cols._paragraphs[1]._text_fragments[0].graphics_state["font_style"])  # "B"
        print(cols._paragraphs[1]._text_fragments[1].graphics_state["font_style"])  # "I"
pdf.output("paragraph_emphasis.pdf")

Why the PDF produced by this code does not contain any emphasis (bold/italics)?
This seems to be the underlying problem causing this bug...

@gmischler
Copy link
Collaborator Author

gmischler commented Jul 10, 2024

That's an interesting observation.
The reason seems to be that setting pdf.font_style does not change pdf.current_font.
And once we know that, it doesn't surprise that the same effect can be observed by using eg. pdf.cell():

pdf.font_style = ""
pdf.cell(text="Eins.")
pdf.font_style = "B"
pdf.cell(text="Zwei.")
pdf.font_style = "I"
pdf.cell(text="Drei.")

Apparently hardly anyone uses FPDF.font_style in this manner, or that bug would have been discovered a long time ago...
FPDF.font_family may have the same problem.
And it doesn't make any difference whether you use a built-in or a TTF font.

All of that isn't directly related to our HTML font settings issue here, but should of course also be fixed.

@Lucas-C
Copy link
Member

Lucas-C commented Jul 11, 2024

The reason seems to be that setting pdf.font_style does not change pdf.current_font.

You are right. I opened this issue with a proposal to change that: #1223

I still think there is something fishy with how the GraphicsStates of TextFragments are handled...
I'll be back soon with another code snippet 🙂

@Lucas-C
Copy link
Member

Lucas-C commented Aug 19, 2024

I opened PR #1246 to fix this problem.

Would you like to review it @gmischler?

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

Successfully merging a pull request may close this issue.

2 participants