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
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).
Bases: JupyterMixin
Large text with color and style.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
Default unstyled text. Defaults to "". |
''
|
|
Union[str, Style]
|
Base style for text. Defaults to "". |
''
|
|
str
|
Justify method: "left", "center", "full", "right". Defaults to None. |
None
|
|
str
|
Overflow method: "crop", "fold", "ellipsis". Defaults to None. |
None
|
|
bool
|
Disable text wrapping, or None for default. Defaults to None. |
None
|
|
int
|
Number of spaces per tab, or |
None
|
|
List[Span]
|
A list of predefined style spans. Defaults to None. |
None
|
|
Union[Font, Path, str] optional
|
Font instance, name or path. Defaults to "condensedsemi". |
'condensedsemi'
|
|
int
|
Adjust letter spacing. Defaults to 0. |
0
|
|
bool
|
Enable automatic kerning. Defaults to True. |
True
|
|
bool
|
Enable all ligatures the font provides. Defaults to True. |
True
|
|
str
|
Ligature style method: "first", "last". Defaults to None. |
None
|
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 |
|---|---|---|---|
|
str
|
A string containing console markup. |
required |
|
Union[str, Style]
|
Base style for text. Defaults to "". |
''
|
|
bool
|
Also render emoji code. Defaults to True. |
True
|
|
str
|
Optional emoji variant, either "text" or "emoji". Defaults to None. |
None
|
|
str
|
Justify method: "left", "center", "full", "right". Defaults to None. |
None
|
|
str
|
Overflow method: "crop", "fold", "ellipsis". Defaults to None. |
None
|
|
Union[Font, Path, str] optional
|
Font instance, name or path. Defaults to "condensedsemi". |
'condensedsemi'
|
|
int
|
Adjust letter spacing. Defaults to 0. |
0
|
|
bool
|
Enable automatic kerning. Defaults to True. |
True
|
|
bool
|
Enable all ligatures the font provides. Defaults to True. |
True
|
|
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 instance. |
required |
|
Union[Font, Path, str] optional
|
Font instance, name or path. Defaults to "condensedsemi". |
'condensedsemi'
|
|
int
|
Adjust letter spacing. Defaults to 0. |
0
|
|
bool
|
Enable automatic kerning. Defaults to True. |
True
|
|
bool
|
Enable all ligatures the font provides. Defaults to True. |
True
|
|
str
|
Ligature style method: "first", "last". Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Typography |
Typography
|
A Typography instance based on Text. |
letter_adjust
Calculates the spacing between two glyphs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
Left character or ligature. |
required |
|
str
|
Right character or ligature. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Spacing in number of cells. |
render
Render as Segments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Console
|
Console instance. |
required |
|
int
|
Number of cells available. |
required |
|
str
|
Justify method: "default", "left", "center", "full", "right". Defaults to "default". |
None
|
|
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 |
|---|---|---|---|
|
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 |
|---|---|---|---|
|
str
|
description |
required |
Returns:
| Type | Description |
|---|---|
Dict[int, str]
|
Dict[int, str]: description |
to_text
Create Text instance from Typography.
Returns:
| Name | Type | Description |
|---|---|---|
Text |
Text
|
A Text instance based on Typography. |
truncate
Truncate text if it is longer than a given width.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
int
|
Maximum number of characters in text. |
required |
|
str
|
Overflow method: "crop", "fold", or "ellipsis". Defaults to None, to use self.overflow. |
required |
wrap
Word wrap the text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
int
|
Number of cells available per line. |
required |
|
str
|
Overflow method: "crop", "fold", or "ellipsis". Defaults to None. |
None
|
|
int
|
Default tab size. Defaults to 8. |
8
|
|
bool
|
Disable wrapping, Defaults to False. |
None
|
Returns:
| Type | Description |
|---|---|
Iterable[Typography]
|
Iterable[Typography]: Typography for each line. |