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

Fix #1039 (<span foreground=> tags) #1058

Merged
merged 17 commits into from Mar 1, 2021

Conversation

markromanmiller
Copy link
Collaborator

@markromanmiller markromanmiller commented Feb 26, 2021

Motivation

In the interest of letting go of <color> tags and using the Pango-recommended <span foreground="color_spec"> tags, we need to ensure that color passed from the MarkupText object into Pango into the SVG and back to the mobject.

Overview / Explanation for Changes

One bug involved in this process was that the local style of <use> tags was entirely ignored, instead of merely being lower priority. This fix rewrites the <def> section of the SVG module to work closer to SVG standards.

@PhilippImhof will be updating the MarkupText object to work in line with expectations

Oneline Summary of Changes

- Replace <color> syntax by Pango's <span foreground> for coloring parts of MarkupText (#1039) (:pr:`1058`)
- Allow using colors for underline, overline and strikethrough in MarkupText (:pr:`1058`)

I suggest two oneline summaries, because this PR also makes other Pango features available that we could not support until now.

Testing Status

One test has been added, focusing specifically on correct inheritance of <use> element properties.

Further Comments

Acknowledgements

Reviewer Checklist

  • Newly added functions/classes are either private or have a docstring
  • Newly added functions/classes have tests added and (optional) examples in the docs
  • Newly added documentation builds, looks correctly formatted, and adds no additional build warnings
  • The oneline summary has been included in the wiki

@PhilippImhof
Copy link
Member

@MrMallIronmaker I took the liberty to change the one-line summary in order to be more specific. Feel free to change it back, if you don't like it.

* underline_color is now supported
* overline and overline_color are now working
* strikethrough and strikethrough_color are now working
* additional examples for those features
@PhilippImhof PhilippImhof added the enhancement Additions and improvements in general label Feb 27, 2021
manim/mobject/svg/style_utils.py Outdated Show resolved Hide resolved
manim/mobject/svg/style_utils.py Outdated Show resolved Hide resolved
manim/mobject/svg/text_mobject.py Show resolved Hide resolved
manim/mobject/svg/text_mobject.py Outdated Show resolved Hide resolved
@PhilippImhof
Copy link
Member

@naveen521kk I suppose @MrMallIronmaker will still have to address the changes you requested for the SVG part; I did not want to mess around in his code.

@markromanmiller
Copy link
Collaborator Author

Changes made, as long as everything passes tests.

@PhilippImhof would it be possible to write up some visual tests for MarkupText?

@PhilippImhof
Copy link
Member

Yes, that's planned. But that's a separate thing, because we need to bundle it with a font file. Graphical unit tests are very likely to fail, because fonts/results differ slightly from one platform to another.

@PhilippImhof
Copy link
Member

I have just noticed that this seems to break the Write animation. (Remember my fear for that?) The characters simply appear one after the other. This is probably linked to some issue with the stroke / surrounding path.

@markromanmiller
Copy link
Collaborator Author

markromanmiller commented Feb 27, 2021

Bother. I won't be able to check anything in the next six-to-eight hours or so. Does Write work in the master branch (after the SVG PR)? Or is this an issue in this PR?

If it's just this PR, then my first guess is that there's an issue in how I rewrote handling the defs - it might have an added layer of VGroups or something... Idk.

@markromanmiller
Copy link
Collaborator Author

Re: tests - is there an issue for that / should there be?

@PhilippImhof
Copy link
Member

PhilippImhof commented Feb 27, 2021

It's this PR; the Write animation works in current master. The tests are noted here: https://github.com/ManimCommunity/manim/projects/7

I'd say no need to open an issue.

@markromanmiller
Copy link
Collaborator Author

It's this PR.

Whew, was worried for a sec. I'm still unsure why the animation would be broken; I'll have to do some digging.

@PhilippImhof
Copy link
Member

I wouldn't have missed that in the last PR :)

Copy link
Member

@PhilippImhof PhilippImhof left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to avoid an accidental merge, as this PR currently breaks the Write animation.

@markromanmiller
Copy link
Collaborator Author

After a little bit of debugging, I've found that the write animation still works if stroke opacity is set to 1 beforehand. How does Write handle objects with stroke opacity set to zero? If the stroke is fully transparent (or if it's black too? idk) it looks like it's not written.

@markromanmiller
Copy link
Collaborator Author

So objects of type Text and MathTex have, by default, a white stroke with 1.0 opacity and 0 width. In contrast, MarkupText has, by default, 0 opacity and 4 width. I made a rather arbitrary decision that when, in an SVG file, stroke=none, to set stroke opacity to 0. I think I can set stroke width to 0 instead and it would match these other text-based mobjects.

@markromanmiller
Copy link
Collaborator Author

@PhilippImhof - Write(MarkupText) should work now.

For reference, I tested with:

class Foobar(Scene):
    def construct(self):
        txt = MarkupText("foo<span foreground='#ffff00'>bar</span>")
        self.play(Write(txt))
        self.wait()

@PhilippImhof
Copy link
Member

So objects of type Text and MathTex have, by default, a white stroke with 1.0 opacity and 0 width.

You mean Tex and MathTex? Because I copied the defaults for MarkupText from Text.

In contrast, MarkupText has, by default, 0 opacity and 4 width. I made a rather arbitrary decision that when, in an SVG file, stroke=none, to set stroke opacity to 0. I think I can set stroke width to 0 instead and it would match these other text-based mobjects.

That's strange, because of

def __init__(
self,
text: str,
fill_opacity: int = 1,
stroke_width: int = 0,
color: str = WHITE,
size: int = 1,
line_spacing: int = -1,
font: str = "",
slant: str = NORMAL,
weight: str = NORMAL,
gradient: tuple = None,
tab_width: int = 4,
height: int = None,
width: int = None,
should_center: bool = True,
unpack_groups: bool = True,
disable_ligatures: bool = False,
**kwargs,
):

Compared to

def __init__(
self,
tex_string,
stroke_width=0,
fill_opacity=1.0,
background_stroke_width=0,
background_stroke_color=BLACK,
should_center=True,
height=None,
organize_left_to_right=False,
tex_environment="align*",
tex_template=None,
**kwargs,
):

So MarkupText and MathTex both have the same default. However, I can confirm that your last commit fixes the problem.

@PhilippImhof PhilippImhof dismissed their stale review February 28, 2021 09:15

The negative review was to prevent accidental merging. (There was already one approval.) As I also contributed to this PR, I cannot give a positive review myself. Therefore I simply revoke this one.

Copy link
Member

@naveen521kk naveen521kk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@markromanmiller
Copy link
Collaborator Author

Before I dive in to this wall of text, I should say the PR looks good to me.


You mean Tex and MathTex? Because I copied the defaults for MarkupText from Text.

Well, technically Tex, Text, and MathTex.

I see what you're referencing - and the Manim object defaults are still the same. However, none of fill-opacity, stroke, and stroke-opacity are passed from the manim object to the SVG file. (Fill is, through the wrapping foreground color call). Then, the individual VMobjects that compose the SVG VMobject are specified either by the SVG file itself, or by SVG defaults, which means most default styling kwargs don't have any effect, e.g:

class Foobar(Scene):
    def construct(self):
        # txt = MathTex("3ab", "\\cdot uv")
        txt = MarkupText("foo<span foreground='#ffff00'>bar</span>", stroke_color=RED)
        self.play(Write(txt))
        self.wait()

completely ignores stroke_color=RED.

Each individual issue like this one can be 'patched,' but I think this is a larger problem with specifying color and style properties that I outlined in #1023 (but I do admit the problem description I gave is unclear). In short, the code is (to my knowledge) unclear about which style defaults and arguments take priority, both in terms of SVGs and regular old groups, and even the cases when it is clear, it's really easy to put a line of code in the wrong place so that the style priorities are out of order.

Note that this problem only tangentially relates to this PR; I do think this PR itself is good to go.

@PhilippImhof
Copy link
Member

Great. I'd like to wait for @jsonvillanueva's review (although we have one approval), because they were also reviewing your SVG rewrite.

@naveen521kk
Copy link
Member

completely ignores stroke_color=RED.

does this happen for Text also?

@markromanmiller
Copy link
Collaborator Author

markromanmiller commented Feb 28, 2021 via email

@markromanmiller
Copy link
Collaborator Author

markromanmiller commented Feb 28, 2021

@naveen521kk Got a chance to check - this works with Text. Of course, with the default stroke width being 0, the stroke fades out after writing.

Screenshot from 2021-02-28 11-23-14

(I spelled "foobar" wrong lol)

from manim import *

class Foobar(Scene):
    def construct(self):
        txt = Text("fbooar", stroke_color=RED)
        self.play(Write(txt))
        self.wait()

@PhilippImhof
Copy link
Member

So do you suggest I change something in the MarkupText class?

@markromanmiller
Copy link
Collaborator Author

I don't think there's anything to change for this PR. If there were to be something to change, it would have to be in something in SVGMobject, or even Mobject more broadly.

Happy to discuss why SVGs are parsed that way (ignoring style kwargs) if that is helpful, but I don't want to add noise to the conversation if not.

Copy link
Member

@jsonvillanueva jsonvillanueva left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM as far as processing style sheets in increasing order of precedence; although, I can't speak much about the write animation fix.

I would ask to improve the test case to demonstrate the inheritance ability: specifically having fill:rgb(x%,y%,z%) inside the <use>' style tag... but this is non-blocking in the interest of time (involves regenerating the .npz control data); however, if you do have time to update the test, please do so. Otherwise, it works and this would be nice to include in the next release!

tests/test_graphical_units/img_svg_resources/aabbb.svg Outdated Show resolved Hide resolved
@naveen521kk
Copy link
Member

Is it ok to merge @jsonvillanueva

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Additions and improvements in general
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Render MarkupText colors using <span> tags
4 participants