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

Handle y offset of glyphs #1958

Closed
gauravsamudra opened this issue Sep 5, 2023 · 15 comments · Fixed by #1970
Closed

Handle y offset of glyphs #1958

gauravsamudra opened this issue Sep 5, 2023 · 15 comments · Fixed by #1970
Labels
feature New feature that should be supported good first issue Issues that can be quite easily solved by Python developers with a good CSS background
Milestone

Comments

@gauravsamudra
Copy link
Contributor

gauravsamudra commented Sep 5, 2023

Hello! I am creating a pdf with weasyprint's html to pdf feature. I've gotten everything to work fine except for one specific set of combination of devanagari characters - in this case a glyph made up of conjunct consonant with vowel diacritics - e.g. ङ्गु - which is made up of ङ, ग and ु. It looks like image in ITF Devanagari font (in vscode or chrome or firefox browsers). The glyph is ङ, underneath that a partial ग and underneath that ु

  • in the pdf generated using weasyprint, the ग and ु get overlapped and the character looks like image - notice the overlap between partial ग and ु
  • but in a pdf generated (print to pdf) by chrome or firefox browser, it works fine - image

I am a loss of what should I be investigating. I’ve tried playing with css’ font feature settings but that doesn’t seem to have any effect. I'll appreciate any help to point me in the right direction or forum!

@gauravsamudra gauravsamudra changed the title Specific character[s] in displays incorrectly in pdf Specific character[s] displays incorrectly in pdf Sep 5, 2023
@liZe
Copy link
Member

liZe commented Sep 5, 2023

Hi!

That’s probably a bug in Harfbuzz. I don’t have the ITF Devanagari font, but I also have the bug in my browser, based on the same shaping libraries as WeasyPrint, on this page (but not in Chrome and Firefox).

Could you please share the font by mail (for testing purpose, of course)?

@gauravsamudra
Copy link
Contributor Author

Thanks @liZe !

I am using ITF Devanagari font that is available by default on mac. I'm not sure if I can/should export and share it.
Here is a simplified version of my css and html using which I can reproduce the issue -

html {
  font-family: 'ITF Devanagari';
  font-size: 22pt;
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Test</title>
  </head>

  <body>
    <p>ङ्गु</p>
  </body>
</html>

and the python code I use

    html_01 = HTML(filename='./app/template/01-template.html')
    css_01 = CSS(filename='./app/template/01-style.css')
    pdf_01 = html_01.write_pdf(stylesheets=[css_01])
    with open('../dist/01.pdf', 'wb') as f:
        f.write(pdf_01)

@liZe
Copy link
Member

liZe commented Sep 5, 2023

Thanks for sharing your HTML/CSS, but I can already reproduce without 😄.

I am using ITF Devanagari font that is available by default on mac. I'm not sure if I can/should export and share it.

Even if I can swear that I won’t use the font for anything but testing, even if it’s technically possible (the font is just a regular file that you can copy and share), even if it’s included in macOS by default, it’s probably illegal to share the font with me. The problem is that I can’t help you without this font 😒.

If you don’t want to share the font, you can do the technical stuff I’d like to do, but it may be difficult, and obviously I don’t have a Mac to guide you! Here are the steps you can follow:

  • Find the hb-view command. If you installed WeasyPrint using Homebrew, it should be included in the HarfBuzz formula and should already be among executable commands installed by Homebrew.
  • Find the ITF Devanagari font on your system. It should be in one of these folders.
  • Launch hb-view /path/to/font.ttf ङ्गु -O png > output.png and see in the output.png file what the glyph looks like.

If you see the problem on the glyph, then it’s (probably) a bug in HarfBuzz and we should open a new issue on their bug tracker. If it’s correctly displayed, then we’ll have to dig further!

@gauravsamudra
Copy link
Contributor Author

gauravsamudra commented Sep 6, 2023

thanks! the glyph looks correct. I executed hb-view /System/Library/Fonts/Supplemental/ITFDevanagari.ttc ङ्गु -O png > output.png and got this -
output

@gauravsamudra
Copy link
Contributor Author

I also tried pango-view --font "ITF Devanagari" --text "ङ्गु" --output test.png --dpi 300 - and the glyph looks correct here too
test

@liZe
Copy link
Member

liZe commented Sep 6, 2023

Strange.

I’m not sure that it’s the same font used for your two examples, but both work, so…

Could you please share a PDF where the problem appears?

@gauravsamudra
Copy link
Contributor Author

sure! here you go - 01.pdf

@liZe
Copy link
Member

liZe commented Sep 15, 2023

I’ve tried to find the reason why we have this bug, but without the font I can’t do much. If someone wants to share the font…

@gauravsamudra
Copy link
Contributor Author

gauravsamudra commented Sep 16, 2023

thanks @liZe! would you be ok to share your email?
I can share the font files I received for trial purposes.

@liZe
Copy link
Member

liZe commented Sep 16, 2023

You’ll find it on my profile. Thanks!

@gauravsamudra
Copy link
Contributor Author

Thanks! I've sent you an email.

@liZe liZe changed the title Specific character[s] displays incorrectly in pdf Handle y offset of glyphs Sep 18, 2023
@liZe
Copy link
Member

liZe commented Sep 18, 2023

Thanks! I've sent you an email.

Thanks a lot, it really helped!

The bug is in WeasyPrint, in draw.py. We should take care of y_offset as we do for x_offset. I’ve managed to draw the glyph correctly using the Ts operator described in chapter 9.3.7 of the PDF 1.7 specification, but we should find a way to do this using pydyf cleanly.

Anyone intersted? It would be a nice first contribution for anyone with some Python knowledge who isn’t scared of reading specifications. 😄 I’ll help of course!

@liZe liZe added feature New feature that should be supported good first issue Issues that can be quite easily solved by Python developers with a good CSS background labels Sep 18, 2023
@gauravsamudra
Copy link
Contributor Author

Thanks @liZe! I would love to contribute!
I have made some changes locally and it seems to be working, although I'll need to test more.
While doing those change, I had to add a method to Stream class in pydyf to set the rise. it looks like this -

    def set_rise(self, height):
        """Set rise."""
        self.stream.append(_to_bytes(height) + b' Ts')

Should I raise a PR in that repo first to include this change and then upgrade it in here (WeasyPrint) to use it?

@liZe
Copy link
Member

liZe commented Sep 19, 2023

Thanks @liZe! I would love to contribute!

💜

Should I raise a PR in that repo first to include this change and then upgrade it in here (WeasyPrint) to use it?

Yes! You can call the function set_text_rise to be more explicit, otherwise it’s perfect.

@gauravsamudra
Copy link
Contributor Author

thanks @liZe, I've raised a PR in pydyf.

@grewn0uille grewn0uille added this to the 60.0 milestone Sep 25, 2023
@grewn0uille grewn0uille linked a pull request Sep 25, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature that should be supported good first issue Issues that can be quite easily solved by Python developers with a good CSS background
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants