Skip to content

RFC: New Text API #2355

Open
Open
@naveen521kk

Description

@naveen521kk

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.

  4. The Text class which exists should accept an str and a FormattedString. The string passed here will be considered as Pango Markup.

  5. The MarkupText class will be deprecated.

  6. All the t2* parameters are deprecated and removed from the Text class.

  7. Paragraph will be aliased to Text

  8. 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.

  9. 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.

  10. Text should also accept parameters from FormattedString for backwards compatibility.

Example Code

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

Metadata

Metadata

Assignees

Labels

enhancementAdditions and improvements in generalneeds discussionThings which needs to be discussed before implemented.

Type

No type

Projects

Status

🆕 New

Relationships

None yet

Development

No branches or pull requests

Issue actions