ManimCommunity / manim

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

set_color_by_t2c in Text mobject doesn't account for white space #1674

Closed Sylk1-9 closed 3 years ago

Sylk1-9 commented 3 years ago

Description of bug / unexpected behavior

(using ManimCE_v0.7.0)

When using the internal function set_color_by_t2c(t2c={word:color}) on an mobject.svg.text_mobject (created with Text("mystring") ), the coloring of the string is sometimes shifted. When running some researches and tests, I am almost certain that it badly accounts for blanck spaces when calculating the index at which the word to color starts and end (done by the internal function self.find_indexes(word, self.original_text)). This results in a single positif shift of the index of the word to color for each previous blank spaces present in the string (see example and image below).

Expected behavior

The coloring should correspond to the word passed by in the dictionary {word:color}

How to reproduce the issue

Code for reproducing the problem ```py from manim import * class Test(Scene): def construct(self): TxExample = Text("This example contains white spaces", color=WHITE).scale_in_place(0.8) TxExample.set_color_by_t2c({"This": GREEN}) TxExample.set_color_by_t2c({"example": RED}) TxExample.set_color_by_t2c({"contains": BLUE}) TxExample.set_color_by_t2c({"white": GOLD}) self.add(TxExample) ```

Additional media files

Images/GIFs ![Test_ManimCE_v0 7 0](https://user-images.githubusercontent.com/25033218/121774271-041b0a00-cb82-11eb-83ee-fb249f77d314.png)

Logs

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

System specifications

System Details - 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

Sylk1-9 commented 3 years ago

Solution found! The option disable_ligatures must be set to True when creating the Text mobject. So the example in the issue must be modified to


from manim import *

class Test(Scene):
    def construct(self):
        TxExample = Text("This example contains white spaces", color=WHITE, disable_ligatures=True).scale_in_place(0.8)
        TxExample.set_color_by_t2c({"This": GREEN})
        TxExample.set_color_by_t2c({"example": RED})
        TxExample.set_color_by_t2c({"contains": BLUE})
        TxExample.set_color_by_t2c({"white": GOLD})
        self.add(TxExample)
naveen521kk commented 3 years ago

It's expected behaviour when ligatures are ON. Also, you can use MarkupText and use markup like HTML which you can use to colour text and don't need to turn off ligatures.

Hope you found the solution. Closing this issue.