Text

Text #

PhpTui\Tui\Extension\Bdf\Shape\TextShape

Renders text on the canvas.

This widget requires a bitmap font in the BDF format. You can use the PhpTui\Tui\Adapter\Bdf\FontRegistry to load and manage fonts. It has a default font built in.

Example #

Show code
<?php

declare(strict_types=1);

use PhpTui\Tui\Canvas\Marker;
use PhpTui\Tui\Color\AnsiColor;
use PhpTui\Tui\DisplayBuilder;
use PhpTui\Tui\Extension\Bdf\BdfExtension;
use PhpTui\Tui\Extension\Bdf\FontRegistry;
use PhpTui\Tui\Extension\Bdf\Shape\TextShape;
use PhpTui\Tui\Extension\Core\Widget\CanvasWidget;
use PhpTui\Tui\Position\FloatPosition;

require 'vendor/autoload.php';

// create the font registry
// this is EXPENSIVE to create, only do it once!
$registry = FontRegistry::default();

$display = DisplayBuilder::default()
    ->addExtension(new BdfExtension())
    ->build();

$display->draw(
    CanvasWidget::fromIntBounds(0, 50, 0, 20)
        ->marker(Marker::Block)
        ->draw(
            new TextShape(
                font: 'default',
                text: 'Hello!',
                color: AnsiColor::Green,
                position: FloatPosition::at(10, 7),
            ),
        )
);

Parameters #

Configure the shape using the builder methods named as follows:

NameTypeDescription
fontstringFont name as it is known in the font registry
textstringText to render
colorPhpTui\Tui\Color\ColorColor of the text
positionPhpTui\Tui\Position\FloatPositionPosition of the text (bottom left corner)
scaleXfloatHorizontal scale of the font
scaleYfloatVerttical scale of the font