ManimCommunity / manim

A community-maintained Python framework for creating mathematical animations.
https://www.manim.community
MIT License
26.94k stars 1.85k forks source link

Bad kerning in `Text()` #2844

Open prism0x opened 2 years ago

prism0x commented 2 years ago

Description of bug / unexpected behavior

When using Text() or Paragraph(), the spacing between letters is generally off regardless of the choice of the font. This is probably an issue with Pango.

See the attached images.

Expected behavior

The spacing between the letters should be more balanced and evenly distributed.

How to reproduce the issue

Code for reproducing the problem ```py class DemoScene(Scene): def construct(self): paragraph = Paragraph( """ Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud.""", font="Helvetica", font_size=32, line_spacing = 0.5 ) self.add(paragraph) ```

Additional media files

Images/GIFs ![image](https://user-images.githubusercontent.com/93129268/175085934-27bcf9b4-2ea3-4a79-bace-ae1b0798469f.png) ![image](https://user-images.githubusercontent.com/93129268/175088197-f2cfca93-1491-46fd-ba3c-27de0dfc3cc2.png)

Logs

Terminal output ``` PASTE HERE OR PROVIDE LINK TO https://pastebin.com/ OR SIMILAR ```

System specifications

System Details I rendered it using the Manimator in the Manim Community Server. The version is 0.15.2 - OS (with version, e.g., Windows 10 v2004 or macOS 10.15 (Catalina)): - RAM: - Python version (`python/py/python3 --version`): - Installed modules (provide output from `pip list`): ``` PASTE HERE ```
LaTeX details + LaTeX distribution (e.g. TeX Live 2020): + Installed LaTeX packages:
FFMPEG Output of `ffmpeg -version`: ``` PASTE HERE ```

Additional comments

Fairlight8 commented 1 year ago

I've found this same issue. I think it's a problem about pango and/or cairo and the font size. Let me explain.

Cause of the issue

If you run this code:

class BadKerning(Scene):
    def construct(self):
        import manimpango

          g = VGroup()
          for size in range(0,100,2):
              g += Text("{}:THIN".format(size), font="Open Sans", font_size=size, disable_ligatures=True)
          self.add(g.arrange(DOWN).scale(0.25))

You will something like this:

kerning

Using large font sizes, the kerning is good. But if you download the image (it's a 5000x5000), and check smaller font sizes, then you can see bad kerning. I've read: https://gitlab.gnome.org/GNOME/pango/-/issues/686. And I think it's an issue about generating text for screens, in pixel size.

Proposed solution

Using bigger font sizes clearly improves the kerning. But I don't want to have bigger fonts all the time, obviously. That's why I changed the line 77 in text -> text_mobject.py

TEXT_MOB_SCALE_FACTOR = 0.005 # Or even a smaller value.

That reduces the scale or the text object. You have to choose bigger font sizes to look similar, but it will look nice even in small texts.

shokirovw commented 1 year ago

@Fairlight8 works fine, thank you for your answer