ManimCommunity / ManimCommunity/manim
RFC: New Text API
@naveen521kk is already working on this.
Since Mar 30, 2022.
- Dominant language
- Python
- Stars
- 40.9k
- Forks
- 3.1k
- Avg merge
- 3d 12h
- Merged PRs (30d)
- 25
Description
Currently, the text API which uses Pango isn't great and I would like to refactor it. Here is my proposal:
1. New Class: `FormattedString` - It's similar to a list of strings but would give more control over the formatting of each string. Initialising it would require the following optional parameters:
- `text`: The text to show. This would be added as the first in the list.
- `font`: The fonts of the strings. Default when not overridden individually. (much like how `text` in `MarkupText` current is)
- `font_size`: The size of the font. Default when not overridden individually.
- `fallback_font`: Font to fall back to when the main one isn't available or doesn't contain a specific character. This can also be overridden individually.
- `color`: The color of the text. Default to white. Probably should be overridden by themes.
- `strikethrough`: whether to strikethrough
- `strikethrough_color`: the color of strikethrough
- `insert_hypens`: use hyphens when breaking a work (default to True)
- `underline`: Underline the text.
- `overline`: Draw an overline on text.
- `underline_color`: Color of underline
- `overline_color`: Color of overline
- `baseline_shift`: Set's baseline shift for a given text
- `open_type_features`: which features to enable
- `language`: The language of the text
- `indent`: the indent of the para
- `style`: styles of text
- `weight`: Font weight
- `letter_spacing`: Space between letters
and more see https://gnome.pages.gitlab.gnome.org/gtk/Pango/enum.AttrType.html
2. The `FormattedString` will have the functions:
- `clear`: clear all the texts stored
- `append`: Add a new text. Accepts all arguments from `__init__()`.
3. Addition should be possible with `FormattedString`. If the addition is `FormattedString` and `FormattedString` it should add items from second to the first one. If the addition is `FormattedString` and `str` a new text should be added to the original one.
5. The `Text` class which exists should accept an `str` and a `FormattedString`. The string passed here will be considered as [Pango Markup](https://gnome.pages.gitlab.gnome.org/gtk/Pango/pango_markup.html).
6. The `MarkupText` class will be deprecated.
7. All the `t2*` parameters are deprecated and removed from the `Text` class.
8. `Paragraph` will be aliased to `Text`
9. `Text` will have new parameters `width` and `height` which will be considered when creating text and paragraphs would be broken based on this. By default `width` and `height` should be full screen.
10. `Text` will be treated as a paragraph, which means it has parameters like
- `justify`: Justify paragraph
- `align`: Align the text should be `left`, `right`, `center`
- `line_height`: Height of each line.
11. `Text` should also accept parameters from `FormattedString` for backwards compatibility.
## Example Code
```py
class TextScene(Scene):
def construct(self):
a = FormattedString(font="Roboto")
a += "Hello"
a += "World"
a.append("bold", weigth=BOLD)
self.add(Text(a))
```
CC: @PhilippImhof
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.