Anmelden
DE
games/open-lemmings / src / Service / Fonts.php · commit 04e2e2a
<?phpnamespace App\Service;use JumPHP\Kernel\Engine;use JumPHP\Renderer\Renderer;use JumPHP\Renderer\TtfFont;use JumPHP\Util\Lang;/** * The text of the game: a TrueType font that has the letters of the current language (Latin and Cyrillic: Lato; * Japanese and Chinese, Korean: subsets made by tools/fonts.py with the characters of config/lang.yaml). */final class Fonts{    private const FILES = ['ja' => 'cjk', 'zh' => 'cjk', 'ko' => 'ko'];    /** @var array<string, TtfFont> */    private array $fonts = [];    public function font(Engine $engine): TtfFont    {        $file = self::FILES[$engine->lang->getLang()] ?? 'ui';        return $this->fonts[$file] ??= new TtfFont($engine->sdl, "assets/fonts/$file.ttf", 10, smooth: true);    }    /** Translated text with a crisp dark shadow. $align: left | center | right. */    public function text(Engine $engine, Renderer $r, string $key, float $x, float $y, float $scale = 1, array|string $color = [255, 255, 255],                         string $align = 'left', array $params = [], array|string|null $outline = [20, 12, 40]): void    {        $this->print($engine, $r, Lang::t($key, $params), $x, $y, $scale, $color, $align, $outline);    }    /** Text as it is (numbers), placed on whole real pixels so that it stays sharp. */    public function print(Engine $engine, Renderer $r, string $text, float $x, float $y, float $scale = 1, array|string $color = [255, 255, 255],                          string $align = 'left', array|string|null $shadow = [20, 12, 40]): void    {        $font = $this->font($engine);        $k = max(1, $r->hd());        if ($align !== 'left') {            $x -= $font->width($text, $scale) * ($align === 'center' ? 0.5 : 1);        }        $font->draw($r, $text, round($x * $k) / $k, round($y * $k) / $k, $scale, $color, 'left', $shadow);    }    public function width(Engine $engine, string $key, float $scale = 1, array $params = []): int    {        return $this->font($engine)->width(Lang::t($key, $params), $scale);    }}