Skip to content

Typography

Typography is meant as a drop-in replacement for Text. It largely implements the same interface, and follows the same conventions for styling, justification and overflow. For all convenience methods that Typography does not implement, text and styles can be assembled as a Text instance, and then converted to a Typography instance using Typography.from_text.

Kerning and Styles

Typography auto-adjusts the spacing between glyphs to create a more visually appealing image. In some cases, kerning can cause glyphs to overlap. When this happens and a style change occurs between overlapping glyphs, foreground and background styles are handled differently:

  • Foreground styles are applied to each individual glyph based on the cells it occupies
  • Background styles are applied column-by-column, using whichever glyph occupies the majority of cells in that column
Typography.from_markup("[magenta on blue]eff[/]ort")


    
    
    
        
            
        
        
    
            

    
            

    
            

    
            

    
            

    
            
    
    
    
    
    ┌──┐┌───┐┌──┐┌──┐┌─┐                                                
││╭╭╮││  ││  ││╷│        ╭╭   ╷ 
╭╮││┼┼ ││╭╮││┬╮││┼│      ╭╮┼┼╭╮┬╮┼ 
├┘││││ │││││││ ││││  ─→  ├┘│││││ │ 
╰╴││╵╵ ││╰╯││╵ ││╰│      ╰╴╵╵╰╯╵ ╰ 
││││  ││  ││ │      
└──┘└───┘└──┘└──┘└─┘                                                

    
    

Ligatures and Styles

When a style border falls within a ligature, the entire ligature is rendered either with the closing (first) or the opening style (last).

Typography.from_markup("[magenta]ef[/]fort", style_ligature="first")



    
    
    
        
            
        
        
    
            

    
            

    
            

    
            

    
            

    
            
    
    
    
    
    ┌──┐┌───┐┌──┐┌──┐┌─┐                     ┌─╴first╶──┐ ┌──╴last╶──┐  
│  ││╭╭╮││  ││  ││╷│        ╭╭╮   ╷      │  ╭╭╮   ╷ │ │╭╭╮   ╷ │
│╭╮││┼┼ ││╭╮││┬╮││┼│      ╭╮┼┼╭╮┬╮┼      │╭╮┼┼╭╮┬╮┼ │ │╭╮┼┼╭╮┬╮┼ │
│├┘││││ │││││││ ││││  ─→  ├┘│││││ │  ─→  │├┘│││││ │ │ │├┘│││││ │ │
│╰╴││╵╵ ││╰╯││╵ ││╰│      ╰╴╵╵╰╯╵ ╰      │╰╴╵╵╰╯╵ ╰ │ │╰╴╵╵╰╯╵ ╰ │
│  ││   ││  ││  ││ │                     │      │ │        │
└──┘└───┘└──┘└──┘└─┘                     └──────────┘ └──────────┘

    
    

Bases: JupyterMixin

Large text with color and style.

Parameters:

Name Type Description Default

text

str

Default unstyled text. Defaults to "".

''

style

Union[str, Style]

Base style for text. Defaults to "".

''

justify

str

Justify method: "left", "center", "full", "right". Defaults to None.

None

overflow

str

Overflow method: "crop", "fold", "ellipsis". Defaults to None.

None

no_wrap

bool

Disable text wrapping, or None for default. Defaults to None.

None

tab_size

int

Number of spaces per tab, or None to use console.tab_size. Defaults to None.

None

spans

List[Span]

A list of predefined style spans. Defaults to None.

None

font

Union[Font, Path, str] optional

Font instance, name or path. Defaults to "condensedsemi".

'condensedsemi'

adjust_spacing

int

Adjust letter spacing. Defaults to 0.

0

use_kerning

bool

Enable automatic kerning. Defaults to True.

True

use_ligatures

bool

Enable all ligatures the font provides. Defaults to True.

True

style_ligatures

str

Ligature style method: "first", "last". Defaults to None.

None

plain property writable

plain

Get the text as a single string.

spans property writable

spans

Get a reference to the internal list of spans.

copy

copy()

Return a copy of this instance.

from_markup classmethod

from_markup(
    text,
    *,
    style="",
    emoji=True,
    emoji_variant=None,
    justify=None,
    overflow=None,
    font="condensedsemi",
    adjust_spacing=0,
    use_kerning=True,
    use_ligatures=True,
    style_ligatures=None,
)

Create Typography instance from markup.

Parameters:

Name Type Description Default

text

str

A string containing console markup.

required

style

Union[str, Style]

Base style for text. Defaults to "".

''

emoji

bool

Also render emoji code. Defaults to True.

True

emoji_variant

str

Optional emoji variant, either "text" or "emoji". Defaults to None.

None

justify

str

Justify method: "left", "center", "full", "right". Defaults to None.

None

overflow

str

Overflow method: "crop", "fold", "ellipsis". Defaults to None.

None

font

Union[Font, Path, str] optional

Font instance, name or path. Defaults to "condensedsemi".

'condensedsemi'

adjust_spacing

int

Adjust letter spacing. Defaults to 0.

0

use_kerning

bool

Enable automatic kerning. Defaults to True.

True

use_ligatures

bool

Enable all ligatures the font provides. Defaults to True.

True

style_ligatures

str

Ligature style method: "first", "last". Defaults to None.

None

Returns:

Name Type Description
Typography Typography

A Typography instance with markup rendered.

from_text classmethod

from_text(
    text,
    *,
    font="condensedsemi",
    adjust_spacing=0,
    use_kerning=True,
    use_ligatures=True,
    style_ligatures=None,
)

Create Typography instance from Text.

Parameters:

Name Type Description Default

text

Text

Text instance.

required

font

Union[Font, Path, str] optional

Font instance, name or path. Defaults to "condensedsemi".

'condensedsemi'

adjust_spacing

int

Adjust letter spacing. Defaults to 0.

0

use_kerning

bool

Enable automatic kerning. Defaults to True.

True

use_ligatures

bool

Enable all ligatures the font provides. Defaults to True.

True

style_ligatures

str

Ligature style method: "first", "last". Defaults to None.

None

Returns:

Name Type Description
Typography Typography

A Typography instance based on Text.

letter_adjust

letter_adjust(left, right)

Calculates the spacing between two glyphs.

Parameters:

Name Type Description Default

left

str

Left character or ligature.

required

right

str

Right character or ligature.

required

Returns:

Name Type Description
int int

Spacing in number of cells.

render

render(console, width, *, justify=None, overflow=None)

Render as Segments.

Parameters:

Name Type Description Default

console

Console

Console instance.

required

width

int

Number of cells available.

required

justify

str

Justify method: "default", "left", "center", "full", "right". Defaults to "default".

None

overflow

str

Overflow method: "crop", "fold", or "ellipsis". Defaults to None.

None

Returns:

Type Description
Iterable[Segment]

Iterable[Segment]: Result of render that may be written to the console.

rendered_width

rendered_width(text)

Get length of rendered text with current settings.

Parameters:

Name Type Description Default

text

str

Text.

required

Returns:

Name Type Description
int int

Length of rendered text.

split_glyphs

split_glyphs(text)

Splits text into individual glyphs, based on the available ligatures in the font.

Parameters:

Name Type Description Default

text

str

description

required

Returns:

Type Description
Dict[int, str]

Dict[int, str]: description

to_text

to_text()

Create Text instance from Typography.

Returns:

Name Type Description
Text Text

A Text instance based on Typography.

truncate

truncate(max_width, *, overflow)

Truncate text if it is longer than a given width.

Parameters:

Name Type Description Default

max_width

int

Maximum number of characters in text.

required

overflow

str

Overflow method: "crop", "fold", or "ellipsis". Defaults to None, to use self.overflow.

required

wrap

wrap(width, *, overflow=None, tab_size=8, no_wrap=None)

Word wrap the text.

Parameters:

Name Type Description Default

width

int

Number of cells available per line.

required

overflow

str

Overflow method: "crop", "fold", or "ellipsis". Defaults to None.

None

tab_size

int

Default tab size. Defaults to 8.

8

no_wrap

bool

Disable wrapping, Defaults to False.

None

Returns:

Type Description
Iterable[Typography]

Iterable[Typography]: Typography for each line.