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

Some characters in certain fonts are rendered incorrectly. #2180

Closed
kimwonj77 opened this issue Jun 11, 2024 · 11 comments
Closed

Some characters in certain fonts are rendered incorrectly. #2180

kimwonj77 opened this issue Jun 11, 2024 · 11 comments
Labels
bug Existing features not working as expected
Milestone

Comments

@kimwonj77
Copy link

kimwonj77 commented Jun 11, 2024

Somehow, Certain font's certain character rendered weirdly.

from weasyprint import HTML

html_content = """
<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <title>Report</title>
    <style>
        @font-face {
          font-family: "Pretendard";
          src: local("PretendardVariable"), url("./fonts/PretendardVariable.ttf") format("truetype");
        }

        html {
            font-family: "Pretendard", sans-serif;
        }
    </style>
</head>
<body>

<div class="container">
    <p>Hello, World--!</p>
    <p>Test(1, 2, 3)</p>
</div>

</body>
</html>

"""

HTML(string=html_content, base_url=".").write_pdf("output.pdf", full_fonts=True, hinting=True, presentational_hints=True)

Adobe acrobat reader:
image

Chrome viewer:
image

The font I'm using is Pretendard.
I'm not sure if this issue is related to the font or WeasyPrint. Since it's rendered correctly on the web and other files, I'm reporting it here.

Additionally, I tried using the WOFF2 format and the OTF format, but neither worked.

@kimwonj77 kimwonj77 changed the title Font rendered weirdly when using certain font Some characters in certain fonts are rendered incorrectly. Jun 11, 2024
@liZe
Copy link
Member

liZe commented Jun 11, 2024

Hi!

Here’s the PDF I get. It’s rendered correctly in all PDF readers I tested, including Adobe Reader.

output.pdf

Maybe there’s a font mismatch between 2 different Pretendard fonts, one installed on your system and one configured by the @font-face rule?

@kimwonj77
Copy link
Author

I removed local font and no luck. (using Pretendard 1.3.9)

Additionally, I'm using Windows, So maybe older GTK(pango) has a problem...

@kimwonj77
Copy link
Author

Still no luck, I tried to use msys2's pango(1.54.0 instead of Windows GTK Runtime's 1.50.4) and still got same issue.
Using font Pretendard 1.3.9/static/variable/PretendardVariable.ttf or each variant outs same result.

@font-face {
  font-family: "Pretendard";
  src: url("./fonts/PretendardVariable.ttf") format("truetype");
}

or

@font-face {
  font-family: "Pretendard";
  font-weight: 900;
  font-display: swap;
  src: url(./fonts/Pretendard/Pretendard-Black.woff2) format("woff2");
}

@font-face {
  font-family: "Pretendard";
  font-weight: 800;
  font-display: swap;
  src: url(./fonts/Pretendard/Pretendard-ExtraBold.woff2) format("woff2");
}

@font-face {
  font-family: "Pretendard";
  font-weight: 700;
  font-display: swap;
  src: url(./fonts/Pretendard/Pretendard-Bold.woff2) format("woff2");
}

@font-face {
  font-family: "Pretendard";
  font-weight: 600;
  font-display: swap;
  src: url(./fonts/Pretendard/Pretendard-SemiBold.woff2) format("woff2");
}

@font-face {
  font-family: "Pretendard";
  font-weight: 500;
  font-display: swap;
  src: url(./fonts/Pretendard/Pretendard-Medium.woff2) format("woff2");
}

@font-face {
  font-family: "Pretendard";
  font-weight: 400;
  font-display: swap;
  src: url(./fonts/Pretendard/Pretendard-Regular.woff2) format("woff2");
}

@font-face {
  font-family: "Pretendard";
  font-weight: 300;
  font-display: swap;
  src: url(./fonts/Pretendard/Pretendard-Light.woff2) format("woff2");
}

@font-face {
  font-family: "Pretendard";
  font-weight: 200;
  font-display: swap;
  src: url(./fonts/Pretendard/Pretendard-ExtraLight.woff2) format("woff2");
}

@font-face {
  font-family: "Pretendard";
  font-weight: 100;
  font-display: swap;
  src: url(./fonts/Pretendard/Pretendard-Thin.woff2) format("woff2");
}

@liZe
Copy link
Member

liZe commented Jun 12, 2024

Could you please share your PDF, and the result of weasyprint --info?

@kimwonj77
Copy link
Author

output.pdf

System: Windows
Machine: AMD64
Version: 10.0.22631
Release: 11

WeasyPrint version: 62.2
Python version: 3.12.2
Pydyf version: 0.10.0
Pango version: 15003

Here is.

@liZe
Copy link
Member

liZe commented Jun 14, 2024

I don’t know why this problem appears for you, I can’t reproduce…

Could you please try using a totally different font-family (something like aaaaaa), so that we’re sure that it’s not using another font installed somewhere on your system?

@kimwonj77
Copy link
Author

kimwonj77 commented Jun 17, 2024

I uninstalled the locally installed Pretendard fonts, and it works fine now. It appears that the local font was being loaded incorrectly. I configured it not to use those fonts, but it seems to have been ignored.

        @font-face {
          font-family: "Pretendard";
          src: url("./fonts/PretendardVariable.ttf") format("truetype");
        }

This is my current configuration.

Edit: I reproduced the issue. It appears that the OTF font has a problem.

@kimwonj77
Copy link
Author

kimwonj77 commented Jun 17, 2024

Another issue is the incorrect rendering of character width with a hyphen between numeric and capitalized characters.
image

from weasyprint import HTML

html_content = """
<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <title>Report</title>
    <style>
        @font-face {
          font-family: "Pretendard";
          src: url("./fonts/PretendardVariable.ttf") format("truetype");
        }

        html {
            font-family: "Pretendard", sans-serif;
        }
    </style>
</head>
<body>

<div class="container">
    <p>Hello, World--!</p>
    <p>Test(1, 2, 3)</p>
    <p>01-001-0001-1234</p>
</div>

</body>
</html>

"""

HTML(string=html_content, base_url=".").write_pdf("output.pdf", full_fonts=True, hinting=True, presentational_hints=True)

This occurs regardless of the font type used.

Edit: It only happens between numeric and capitalized characters.
image

    <p>HellO-World</p>
    <p>Hello-World</p>
    <p>hello-world</p>
    <p>Hell0-World</p>
    <p>01-002-003-0004</p>

@liZe
Copy link
Member

liZe commented Jun 17, 2024

The bug only appears with the "full_fonts" options, that shouldn’t be used in most cases (and is really slow in your case). Do you need it?

@kimwonj77
Copy link
Author

kimwonj77 commented Jun 17, 2024

  1. We observed that sometimes the font does not load correctly without the full font. (At 59.x)
  2. For testing purposes, I encountered a weird bug: AttributeError: function/symbol 'hb_set_add_sorted_array' not found in library 'C:\Program Files\GTK3-Runtime Win64\bin\libharfbuzz-0.dll': error 0x7f
Traceback (most recent call last):
  File "[redacted]\test.py", line 37, in <module>
    HTML(string=html_content, base_url=".").write_pdf("output.pdf")
  File "[redacted]\.venv\Lib\site-packages\weasyprint\__init__.py", line 259, in write_pdf
    self.render(font_config, counter_style, **options)
  File "[redacted]\.venv\Lib\site-packages\weasyprint\document.py", line 404, in write_pdf
    pdf = generate_pdf(self, target, zoom, **options)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "[redacted]\.venv\Lib\site-packages\weasyprint\pdf\__init__.py", line 270, in generate_pdf
    pdf_fonts = build_fonts_dictionary(
                ^^^^^^^^^^^^^^^^^^^^^^^
  File "[redacted]\.venv\Lib\site-packages\weasyprint\pdf\fonts.py", line 286, in build_fonts_dictionary
    font.clean(cmap, options['hinting'])
  File "[redacted]\.venv\Lib\site-packages\weasyprint\pdf\fonts.py", line 112, in clean
    self.subset(cmap, hinting)
  File "[redacted]\.venv\Lib\site-packages\weasyprint\pdf\fonts.py", line 184, in subset
    self._harfbuzz_subset(cmap, hinting)
  File "[redacted]\.venv\Lib\site-packages\weasyprint\pdf\fonts.py", line 195, in _harfbuzz_subset
    harfbuzz.hb_set_add_sorted_array(gid_set, gid_array, len(cmap))
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "[redacted]\.venv\Lib\site-packages\cffi\api.py", line 912, in __getattr__
    make_accessor(name)
  File "[redacted]\.venv\Lib\site-packages\cffi\api.py", line 908, in make_accessor
    accessors[name](name)
  File "[redacted]\.venv\Lib\site-packages\cffi\api.py", line 838, in accessor_function
    value = backendlib.load_function(BType, name)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: function/symbol 'hb_set_add_sorted_array' not found in library 'C:\Program Files\GTK3-Runtime Win64\bin\libharfbuzz-0.dll': error 0x7f

P.S. I deleted the previous comment that contained sensitive information.

Edit: Seems like this issue introduced on somewhat 62.x.
Edit2: harfbuzz on Windows-GTK3-Runtime is 3.1.2 and that function is intoroduced on 4.1.0. msys2's mingw64 has 8.5.0 thankfully.

@liZe
Copy link
Member

liZe commented Jun 17, 2024

OK, let’s try to stay focus!

We can keep this issue to talk about the spacing problem with full fonts.

1. We observed that sometimes the font does not load correctly without the full font. (At 59.x)

There shouldn’t be any problem without the full font option. If you have a problem without full fonts, with version 62.2, please share your example in a new issue.

2. For testing purposes, I encountered a weird bug: AttributeError: function/symbol 'hb_set_add_sorted_array' not found in library 'C:\Program Files\GTK3-Runtime Win64\bin\libharfbuzz-0.dll': error 0x7f

Please open a new issue for this.

@liZe liZe added the bug Existing features not working as expected label Jun 17, 2024
@liZe liZe closed this as completed in e8540e7 Jun 17, 2024
@liZe liZe added this to the 63.0 milestone Jun 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Existing features not working as expected
Projects
None yet
Development

No branches or pull requests

2 participants