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

Parse and generate style for SVG #1717

Merged
merged 8 commits into from
Jan 26, 2022
Merged

Parse and generate style for SVG #1717

merged 8 commits into from
Jan 26, 2022

Conversation

TonyCrane
Copy link
Collaborator

@TonyCrane TonyCrane commented Jan 26, 2022

Motivation

Before, SVGMobject will remove all styles from a svg file.
This pull request parse the style for SVG by refering to ManimCommunity/manim.
In order to prevent the style generated layer by layer in init_points from being globally overwritten by the init_colors after it. I added a parameter override to init_colors, which is set to True by default (that is, consistent with the original performance), but when override = False, it will recurse layer by layer and set the color to the currently generated color.

Proposed changes

  • M manimlib/mobject/svg/svg_mobject.py: add SVG style parser and fix some bugs of path string
  • M manimlib/mobject/types/vectorized_mobject.py: add override parameter to init_colors
  • M manimlib/mobject/mobject.py: synchronously update the parameters of init_colors
  • M manimlib/mobject/geometry.py: synchronously update the parameters of init_colors
  • M manimlib/mobject/types/surface.py: synchronously update the parameters of init_colors
  • M manimlib/mobject/svg/tex_mobject.py: synchronously update the parameters of init_colors and synchronize SingleStringTex's stroke_width to SVGMobject
  • M manimlib/mobject/svg/tex_mobject.py: cache color in tex_string_to_mob_map together with tex_string
  • M manimlib/mobject/svg/text_mobject.py: replace warnings.warn with log.warning

Test

I tested all the svg files used in ManimCE for testing and each file renders correctly with color and stroke.
And Tex TexText can correctly generate texts with color according to the corresponding LaTeX expression (\color{})

Code:

class Code(Scene):
    def construct(self):
        svg = VGroup(
            TexText(r"test {\color{red}and} {\color{blue}test}").set_width(10),
            TexText(r"test {\color{red}and} {\color{blue}test}").set_width(10).set_color(WHITE),
            TexText(r"test {\color{red}and} {\color{blue}test}", color=BLACK).set_width(10),
        ).arrange(DOWN)
        self.add(svg)

Result:
image
As can be seen from the above example, the color parameter is only the default color, and this color will be overwritten as long as a color is included in the SVG. If you want to ignore the color in SVG, you can directly .set_color again.

@TonyCrane TonyCrane requested a review from 3b1b January 26, 2022 14:39
@3b1b
Copy link
Owner

3b1b commented Jan 26, 2022

This looks great, thanks for doing it

@3b1b 3b1b merged commit 27c666f into master Jan 26, 2022
@3b1b
Copy link
Owner

3b1b commented Jan 26, 2022

There are a few examples I can find, like the one attached, where due to how the style is represented in the SVG it does not quite work, but it's certainly better than current functionality so worth the merge.

beer
.

@TonyCrane
Copy link
Collaborator Author

Yes, it's because the styles are defined by the css in the <style> tag, which is not handled yet (nor is ManimCommunity). I'll try my best to handle this situation when I have time :)

@TonyCrane TonyCrane deleted the svg-style branch January 26, 2022 16:08
@3b1b
Copy link
Owner

3b1b commented Jan 26, 2022

I agree it seems a little tricky. I mostly just wanted to keep track of an edge case here, don't feel like it's something you have to do.

Also, just FYI, I made a few quick fixes based on its behavior with Text and MTex

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.

2 participants