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

Use ManimPango for Text rendering #1383

Merged
merged 1 commit into from Feb 12, 2021
Merged

Use ManimPango for Text rendering #1383

merged 1 commit into from Feb 12, 2021

Conversation

naveen521kk
Copy link
Contributor

Motivation

Pango is a lot better at Text Rendering rather than using a Toy API for Cairo. I had discussed this with @3b1b on Discord.. This will remove the dependency on pycairo.

It also provides another method to add a font file to Pango search path, whose family name can later be called. This method isn't available for macOS yet.

I couldn't find Code here, that would require a small change setting disable_ligatures, or else colours would be broken, due to ligatures.

Proposed changes

  • requirements.txt - add manimpango
  • text_mobject.py - use manimpango

Test

Running example scenes should work, no breaking API changes. register_font API can be used as

with register_font('roboto.ttf'):
    a=Text("Hello Roboto",font="Roboto")

A list of fonts visible to Pango can be got from

>>> import manimpango
>>> manimpango.list_fonts()
[ ... ]

More API at https://manimpango.manim.community/en/latest/

@3b1b 3b1b merged commit a5d5e40 into 3b1b:master Feb 12, 2021
@3b1b
Copy link
Owner

3b1b commented Feb 12, 2021

Is this the intended behavior?

image

class TextExample(Scene):
    def construct(self):
        fonts = Text(
            "And you can also set the font according to different words",
            font="Arial",
            t2f={"font": "Consolas", "words": "Consolas"},
            t2c={"font": BLUE, "words": GREEN}
        )
        fonts.set_width(FRAME_WIDTH - 1)
        self.play(Write(fonts))
        self.wait()

@naveen521kk naveen521kk deleted the manimpango branch February 13, 2021 05:58
@naveen521kk
Copy link
Contributor Author

Looks like a bug to me. I tried doing the same using Community edition's MarkupText (I didn't bring it here because CONFIG was something out of my understanding), and also the legacy CairoText. I get a video like

from manim import *
class Font(Scene):
    def construct(self):
        fonts = Text(
            "And you can also set the font according to different words",
            font="Arial",
            t2f={"font": "Consolas", "words": "Consolas"},
            t2c={"font": BLUE, "words": GREEN},
            disable_ligatures=True
        )
        fonts_c = CairoText(
            "And you can also set the font according to different words",
            font="Arial",
            t2f={"font": "Consolas", "words": "Consolas"},
            t2c={"font": BLUE, "words": GREEN},
        )
        markup = MarkupText(
            'And you can also set the <color col="BLUE"><span font_desc="Consolas">font</span></color> according to different <color col="GREEN"><span font_desc="Consolas" color="GREEN">words </span></color>',
            disable_ligature = True,
            font="Arial",
        )
        fonts.scale(.8)
        self.play(Write(fonts))
        self.wait()
        self.clear()
        fonts_c.scale(.8)
        self.play(Write(fonts_c))
        self.wait()
        self.clear()
        markup.scale(.6)
        self.play(Write(markup))
        self.wait()
Font.mp4

so it seems like a bug in aligning texts in ManimPango for Text. Can you open an issue in that repo?

@TonyCrane
Copy link
Collaborator

Is this the intended behavior?

image

class TextExample(Scene):
    def construct(self):
        fonts = Text(
            "And you can also set the font according to different words",
            font="Arial",
            t2f={"font": "Consolas", "words": "Consolas"},
            t2c={"font": BLUE, "words": GREEN}
        )
        fonts.set_width(FRAME_WIDTH - 1)
        self.play(Write(fonts))
        self.wait()

image
But why I don't have this problem?

@naveen521kk
Copy link
Contributor Author

@TonyCrane I think you don't have the font installed or something like that? Btw which OS are you using?

@TonyCrane
Copy link
Collaborator

I think you don't have the font installed or something like that? Btw which OS are you using?

But the result fonts are correct ("font" and "words" are Consolas, the rest are Arial), so I made sure I have installed the fonts.
I am using Windows 10, Python 3.7.3, ManimPango 0.2.2, and the manim source code is the latest.

@naveen521kk
Copy link
Contributor Author

I think maybe caching issue? Did you run the same example before updating to recent branch which uses pango and the svg file from cairo is cached? That's should be case I think. But still not sure...

@TonyCrane
Copy link
Collaborator

TonyCrane commented Feb 13, 2021

Oh, sorry, I just cleaned the cache of Text, and got the same result as you.
image
But if I kept the disable_ligatures as default True, then the coloring will be incorrect:
DYY} }`BQ6(Z} 0R7@_}HYQ


Fixed in c9e2234.
Because in SVGMobject of ManimCommunity/manim, the empty paths are ignored, but in 3b1b/manim are not. So I added back the remove_empty_path method which you accidentally deleted in this pr.

@naveen521kk
Copy link
Contributor Author

I don't think that will fix this. The colouring is a bit off because of ligatures where in two letter comes in a single path element of the SVG file. Pango would handle it but the svg parser in both the version isn't that great to handle styles. I have some plans to make it not use SVG instead directly return each character as an ImageMobject or rather return a set of points, but that requires work and isn't yet done. Once that's done this problem will be fixed 😉.

@eulertour
Copy link
Collaborator

This PR causes a crash immediately for me and it looks like it's causing other problems from the earlier posts. Can this at least be made optional until it can be done stably?

@naveen521kk
Copy link
Contributor Author

This PR causes a crash immediately for me

There could be much reason why things crash. ManimPango is necessary for Community Version also. If you say this crashes for you, it should crash for ManimCE also...

This was referenced Jun 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants