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

Refactor Text with the latest manimpango #1751

Merged
merged 20 commits into from
Mar 22, 2022
Merged

Refactor Text with the latest manimpango #1751

merged 20 commits into from
Mar 22, 2022

Conversation

YishiMichael
Copy link
Contributor

@YishiMichael YishiMichael commented Mar 2, 2022

Motivation

The current Text, MarkupText classes are a copied version from community edition, and should run together with old manimpango versions. I heard that the community had a future development plan for manimpango, so I decided to refactor the two classes to fit the code style in manimgl, while using the latest manimpango version as a requirement.

In my modified code, MarkupText becomes almost an alias of Text. Parameters are rearranged:

Added

  • is_markup: A boolean (default False in Text, while True in MarkupText) representing whether the given string uses markup syntax.
  • justify: A boolean (default False) to determine whether each line of a paragraph is slightly stretched to align at both edges.
  • indent: An integer (default 0) representing the indent of each paragraph.
  • alignment: A string (one of LEFT, CENTER, RIGHT, default LEFT) to determine which direction to align if not justified.
  • line_width_factor: A float | None (default None) to determine where the text should wrap, e.g. 1.0 means the text is wrapped just to fit the full screen. None stands for disabling automatic wrappings (you can still wrap lines via inserting "\n").
  • apply_space_chars: A boolean (default True in Text, while False in MarkupText) to determine whether empty mobjects should be inserted as spaces. However, applying spaces may raise IndexError if some characters exceed the canvas in pango, or if ligatures, underlines, background rectangles occur in text. When slicing Text object via get_part(s)_by_text, apply_space_chars should be made sure to set to True, or a warning is raised. (Sidenote, currently the parameter disable_ligatures doesn't affect the output, and ligatures are always applied if there exist some in the font.)
  • global_config: A dict (type dict[str, Any], default {}) holding extra global configurations on text. Keys can be any attribute supported by pango. (You may look through all attributes supported by pango in this website: https://docs.gtk.org/Pango/pango_markup.html)
  • local_configs: A dict (type dict[str | tuple[int, int], dict[str, Any]], default {}) holding extra local configuration dicts on text. Keys can be either a string (indicating several ranges that match) or a tuple (indicating a single range), and keys of each local configuration dict can be any attribute supported by pango.

Modified

  • t2c, t2f, t2s, t2w: (type dict[str | tuple[int, int], Any], default {}) Users can now set either a string (indicating several ranges that match) or a tuple (indicating a single range) as a key of any one of those dicts. Strings like [2:5] will no longer be regarded as ranges.
  • t2g: Pango cannot handle gradients, so please use set_color_by_gradient instead. A deprecation warning is raised if t2g is set.

Removed

  • size: This is originally an alias of font_size. The deprecation warning is removed.
  • tab_width: Originally tab_width * " " is used to replace all tabs, but it's not useful since "\t" can be handled in pango.

Behaviors of the rest parameters in CONFIG are preserved for backwards compatibility.

Proposed changes

  • M manimlib/mobject/svg/text_mobject.py: Refactor Text, MarkupText, Code.
  • M manimlib/mobject/svg/svg_mobject.py: Avoid issue brought by trailing "Z M" commands.
  • M setup.cfg: Update version requirement for manimpango.
  • M requirement.txt: Update version requirement for manimpango.

Test

Code:

text = Text(
    "Hello Pango",
    global_config={"underline": "double", "underline_color": MAROON},
    local_configs={"Pango": {"bgcolor": TEAL}}
)
self.add(text)

Result:
TextTest

@TonyCrane TonyCrane requested a review from 3b1b March 22, 2022 02:16
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.

3 participants