*/ private static array $fonts = []; public function __construct(public Renderer $r, public ?Sdl $sdl, public string $fontDir = 'assets/fonts') { } /** Draws at the origin of a window (desktop pixels). */ public function at(float $x, float $y): self { $this->ox = $x; $this->oy = $y; return $this; } public function vx(float $x): float { return $this->ox * $this->scale + $x * $this->scale; } public function vy(float $y): float { return $this->oy * $this->scale + $y * $this->scale; } /** A picture of the skin at (x, y), its own size or stretched. @return bool false when the skin has none */ public function image(string $name, float $x, float $y, ?float $w = null, ?float $h = null, ?array $src = null): bool { $file = $this->skin?->image($name); if ($file === null || ($this->sdl === null && $this->native === null)) { return false; } return $this->file($file, $x, $y, $w, $h, $src); } /** Any picture file. */ public function file(string $file, float $x, float $y, ?float $w = null, ?float $h = null, ?array $src = null): bool { if ($this->native !== null) { $t = $this->native->file($file, $this->scale); if ($t === null) { return false; } $sw = $src[2] ?? (float)$t[1]; $sh = $src[3] ?? (float)$t[2]; $k = $t[3]; $s = $src === null ? null : [$src[0] * $k, $src[1] * $k, $src[2] * $k, $src[3] * $k]; // on whole pixels of the screen (a picture at x = 57.6 was spread over two pixels: blurred), its edges rounded $x1 = round($this->vx($x)); $y1 = round($this->vy($y)); $x2 = round($this->vx($x) + ($w ?? $sw) * $this->scale); $y2 = round($this->vy($y) + ($h ?? $sh) * $this->scale); $this->r->copy($t[0], $s, $x1, $y1, $x2 - $x1, $y2 - $y1); return true; } $name = Picture::texture($this->sdl, $file); if ($name === null || $this->sdl === null) { return false; } $tex = $this->sdl->getTexture($name); $sw = $src[2] ?? (float)$tex['width']; $sh = $src[3] ?? (float)$tex['height']; $this->r->copy($tex['texture'], $src, $this->vx($x), $this->vy($y), ($w ?? $sw) * $this->scale, ($h ?? $sh) * $this->scale); return true; } /** @var array per SDL: the film's picture loaded now (one texture, replaced at each new picture) */ private static array $frameFile = []; /** A picture of a film, kept in its proportions inside the box (black around); only the last one stays in memory. */ public function videoFrame(string $file, float $x, float $y, float $w, float $h): void { if ($this->native !== null) { $t = $this->native->frame($file); $tw = $t[1] ?? 0; $th = $t[2] ?? 0; $texture = $t[0] ?? null; } elseif ($this->sdl !== null) { $key = spl_object_id($this->sdl); if ((self::$frameFile[$key] ?? '') !== $file) { if ($this->sdl->hasTexture('winzik-frame')) { $this->sdl->releaseTexture('winzik-frame'); } try { $this->sdl->loadTexture('winzik-frame', $file); self::$frameFile[$key] = $file; } catch (\Throwable) { return; } } $tex = $this->sdl->getTexture('winzik-frame'); [$texture, $tw, $th] = [$tex['texture'], (int)$tex['width'], (int)$tex['height']]; } else { return; } if ($texture === null || $tw <= 0 || $th <= 0) { return; } $k = min($w / $tw, $h / $th); $dw = $tw * $k; $dh = $th * $k; $this->r->copy($texture, null, $this->vx($x + ($w - $dw) / 2), $this->vy($y + ($h - $dh) / 2), $dw * $this->scale, $dh * $this->scale); } /** A loader: 8 dots in a circle, the bright one going round (0.8 s a turn). */ public function spinner(float $cx, float $cy, float $r, array|string $color, float $time): void { $c = \JumPHP\Util\Color::of($color); $lit = (int)floor(fmod($time, 0.8) / 0.1); for ($i = 0; $i < 8; $i++) { $a = $i * M_PI / 4 - M_PI / 2; $age = ($lit - $i + 8) % 8; $alpha = max(40, 255 - $age * 30); $d = max(1.0, $r / 3.2); $this->fill($cx + cos($a) * $r - $d / 2, $cy + sin($a) * $r - $d / 2, $d, $d, [$c->r, $c->g, $c->b, $alpha]); } } /** @return array{0: int, 1: int}|null the size of a picture of the skin */ public function imageSize(string $name): ?array { $file = $this->skin?->image($name); return $file === null ? null : Picture::size($this->sdl, $file); } /** A disc of radius $r around (cx, cy), in skin pixels. */ public function disc(float $cx, float $cy, float $r, array|string $color): void { $this->r->fillCircle($this->vx($cx), $this->vy($cy), $r * $this->scale, $color); } public function fill(float $x, float $y, float $w, float $h, array|string $color): void { $this->r->fillRect($this->vx($x), $this->vy($y), $w * $this->scale, $h * $this->scale, $color); } public function frame(float $x, float $y, float $w, float $h, array|string $color): void { $this->r->drawRect($this->vx($x), $this->vy($y), $w * $this->scale, $h * $this->scale, $color, max(1, (int)round($this->scale))); } /** A 3D box of Windows 98 (raised: light top-left, dark bottom-right). */ public function bevel(float $x, float $y, float $w, float $h, bool $raised = true, string $face = '#c0c0c0'): void { $this->fill($x, $y, $w, $h, $face); $light = $raised ? '#ffffff' : '#808080'; $dark = $raised ? '#808080' : '#ffffff'; $one = 1 / $this->scale * max(1, round($this->scale)); $this->fill($x, $y, $w, $one, $light); $this->fill($x, $y, $one, $h, $light); $this->fill($x, $y + $h - $one, $w, $one, $dark); $this->fill($x + $w - $one, $y, $one, $h, $dark); } /** A font of the application at a size in skin pixels (cached). */ public function font(float $size, string $file = 'ui.ttf'): TextDrawer { $px = max(5, (int)round($size * $this->scale)); $key = $file . ':' . $px . ':' . ($this->sdl !== null ? spl_object_id($this->sdl) : 0); if (!isset(self::$fonts[$key]) && $this->sdl !== null) { try { self::$fonts[$key] = new TtfFont($this->sdl, $this->fontDir . '/' . $file, $px, true); } catch (\Throwable) { self::$fonts[$key] = new \JumPHP\Gui\ScaledFont(new \JumPHP\Renderer\BitmapFont($this->sdl), $px / 9); } } return self::$fonts[$key] ?? new \JumPHP\Gui\EstimatedFont($px); } /** * The text as the fonts can draw it: yt-dlp writes " / : ? | * < > in file names as their fullwidth forms (" / :...) * or as ⧸ ⧹, which the fonts do not have (a box was drawn): shown as the plain ones. The files keep their names. */ public static function drawable(string $text): string { if (!preg_match('/[\x{FF01}-\x{FF5E}\x{29F8}\x{29F9}\x{3000}]/u', $text)) { return $text; } $text = (string)preg_replace_callback('/[\x{FF01}-\x{FF5E}]/u', fn ($m) => chr(mb_ord($m[0]) - 0xFEE0), $text); return str_replace(["\u{29F8}", "\u{29F9}", "\u{3000}"], ['/', '\\', ' '], $text); } /** A text at (x, y) (its top), in skin pixels. */ public function text(string $text, float $x, float $y, float $size, array|string $color, string $align = 'left', string $file = 'ui.ttf'): void { if ($text === '') { return; } $text = self::drawable($text); if ($this->native !== null) { $c = \JumPHP\Util\Color::of($color); $t = $this->native->text($text, max(5, (int)round($size * $this->scale)), [$c->r, $c->g, $c->b, $c->a], $file); if ($t !== null) { $x0 = $this->vx($x) - ($align === 'center' ? $t[1] / 2 : ($align === 'right' ? $t[1] : 0)); $this->r->copy($t[0], null, round($x0), round($this->vy($y)), $t[1], $t[2]); // whole pixels: sharp letters } return; } $this->font($size, $file)->draw($this->r, $text, $this->vx($x), $this->vy($y), 1, $color, $align); } /** The width of a text in skin pixels. */ public function textWidth(string $text, float $size, string $file = 'ui.ttf'): float { $text = self::drawable($text); if ($this->native !== null) { return $this->native->measure($text, max(5, (int)round($size * $this->scale)), $file)[0] / $this->scale; } return $this->font($size, $file)->width($text) / $this->scale; } /** The beginning of a text that fits in a width (the labels of 2001 cut what did not fit). */ public function fit(string $text, float $width, float $size, string $file = 'ui.ttf'): string { if ($this->textWidth($text, $size, $file) <= $width) { return $text; } $chars = mb_str_split($text) ?: []; $lo = 0; $hi = count($chars); while ($lo < $hi) { $mid = intdiv($lo + $hi + 1, 2); if ($this->textWidth(implode('', array_slice($chars, 0, $mid)), $size, $file) <= $width) { $lo = $mid; } else { $hi = $mid - 1; } } return implode('', array_slice($chars, 0, $lo)); } /** Fonts are made again after a hot reload / new SDL renderer. */ public static function forgetFonts(): void { self::$fonts = []; } }