Skip to content

mtkalms/rich-typography

Repository files navigation

Test Ruff Documentation

rich-typography

Large letter typography for Rich and Textual.

rich-typography example

Installation

Install with uv or your favorite PYPI package manager:

uv add rich-typography

Usage

from rich.console import Console
from rich_typography import Typography

console = Console()
text = Typography.from_markup("Hello from [purple]rich-typography[/purple]")
console.print(text)

rich-typography usage

Custom Markdown

Rich allows you to create your own Markdown renderable and overwrite how specific markdown blocks are rendered. With that we can modify this example to render H1 headings as Typography instead of Text.

from rich.markdown import Markdown as RichMarkdown, Heading as RichHeading
from rich.console import Console, RenderResult
from rich_typography import Typography


MARKDOWN = """
# This is an h1

Rich can do a pretty *decent* job of rendering markdown.

## This is an h2

1. This is a list item
2. This is another list item
"""


class Heading(RichHeading):
    def __rich_console__(self, *args, **kwargs) -> RenderResult:
        self.text.style = "red"
        if self.tag == "h1":
            text = Typography.from_text(self.text)
            text.justify = "center"
            yield text
        else:
            yield from super().__rich_console__(*args, **kwargs)


class Markdown(RichMarkdown):
    def __init__(self, *args, **kwargs) -> None:
        super().__init__(*args, **kwargs)
        self.elements["heading_open"] = Heading



output = Markdown(MARKDOWN)

markdown example

Alternative Approaches

Render with Pillow and use with textual-image

  • Allows you to use any font
  • Depends on terminal supporting terminal graphics protocol (TPG) or sixel graphics protocol
  • Layout is more difficult to control
from PIL import Image, ImageDraw, ImageFont
from textual_image.renderable import Image as RichImage
from rich.console import Console

img = Image.new("RGBA", (500, 64))
draw = ImageDraw.Draw(img)
font = ImageFont.truetype("CascadiaCode.ttf", 64)
draw.text((0, 0), "This is an h1", font=font, fill=(255, 0, 0))

console = Console()
console.print(RichImage(img))

TGP:

Pillow font rendering with terminal graphics protocol

Sixel:

Pillow font rendering with sixel graphics

Half block fallback:

Pillow font rendering with characters

Text Sizing Protocol

Kitty has proposed a text sizing protocol but support by popular terminals is currently still limited.

Similar Projects

About

Large letter typography for Rich and Textual.

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages