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

Superscripts and subscripts with markdown and/or html #514

Closed
TchilDill opened this issue Aug 31, 2022 · 4 comments
Closed

Superscripts and subscripts with markdown and/or html #514

TchilDill opened this issue Aug 31, 2022 · 4 comments

Comments

@TchilDill
Copy link

TchilDill commented Aug 31, 2022

Hey guys,

This package is already a wonderful thing. For my usage, the game changer would be to make it easier to write superscript and subscript in the pdf file.

So at least the html shortcuts for subscripts and superscripts would be good, the best way would be to have the mardown working for all math/latex style input.

The reason for this is mostly for maths to be rendered easily in tables.
For instance, say you have a table with parameters that are commonly known to be written down with very specific superscript and subscripts.

I acknowledge the current workaround to do this, but it is too messy for me to have all this rendered as images.

Thanks! :)

@gmischler
Copy link
Collaborator

In an earlier discussion (#298) I have pointed to the unicode subscript/superscript range as another workaround: Unicode subscripts and superscripts
The result is not always particularly pretty, but it still may still be an option.

I'm currently playing with a refactoring of the text flow routines, in order to better encapsulate their functionality and make them more flexible. The PDF specs actually include special operators to position text as subscripts/superscripts. They may eventually make it in there, but don't hold your breath just yet...

@Lucas-C
Copy link
Member

Lucas-C commented Sep 1, 2022

I fully second @gmischler answer.

So at least the html shortcuts for subscripts and superscripts would be good, the best way would be to have the mardown working for all math/latex style input.

This sounds like a great addition, I would be happy to accept PR adding support for <sup> & <sub> HTML tags in HTML2FPDF

@gmischler
Copy link
Collaborator

Once my current PR and the one coming after it are merged, it should be "relatively" easy to do this.

  • We need to add new parameters to the state stack. eg.:
    • char_vpos ("SUPER"/"SUB"/None), for choosing a shift direction.
    • vpos_shift (default eg. 0.5), for the vertical movement relative to font size.
    • vpos_scale (default eg. 0.7), for how much smaller the shifted characters should be.
  • Fragment.get_width() needs to take those and reduce the returned width by vpos_scale if char_vpos is not None.
  • ._render_styled_text_line() needs to wrap each sub-/superscripted fragment in something like this:
    f"q {shift} Ts /F{frag.font['i']} {frag.font_size_pt * frag.vpos_scale} Tf {frag.string} Tj Q"
    where shift is positive frag.font_size_pt * frag.vpos_shift for "SUPER" and negative for "SUB".

This is just from reading the specs without any testing, so the details may not be quite right... 😉
I'm also not sure if this is the best combination and naming of options, or if maybe we should just use fixed shift and scale values for simplicity.

After that, ist should be straightforward for write_html() to pass <sub>/<sup> sequences to write() with the right settings.

Markdown doesn't seem to have an established code for subscribt/superscript, not even commonmark does. The examples I've seen used pairs of single ^ and ~ characters, which seems rather prone to conflict with the normal use of those characters.
And if you'd like to use Tex math syntax (eg. if it's more than just numbers with exponents), maybe inserting a SVG created by matplotlib is the better way.

@gmischler
Copy link
Collaborator

Implemented in #520 and published with release 2.5.7.

@Lucas-C Lucas-C closed this as completed Sep 9, 2022
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