mirror of
https://github.com/kovidgoyal/kitty
synced 2026-07-18 14:34:52 +02:00
Implement rendering of sample text
This commit is contained in:
@@ -4,14 +4,18 @@
|
||||
import json
|
||||
import string
|
||||
import sys
|
||||
import tempfile
|
||||
from typing import Any, Dict, Tuple, TypedDict
|
||||
|
||||
from kitty.conf.utils import to_color
|
||||
from kitty.constants import kitten_exe
|
||||
from kitty.fonts import Descriptor
|
||||
from kitty.fonts.common import face_from_descriptor, get_font_files, get_variable_data_for_descriptor
|
||||
from kitty.fonts.list import create_family_groups
|
||||
from kitty.fonts.render import display_bitmap
|
||||
from kitty.options.types import Options
|
||||
from kitty.options.utils import parse_font_spec
|
||||
from kitty.utils import screen_size_function
|
||||
|
||||
|
||||
def send_to_kitten(x: Any) -> None:
|
||||
@@ -55,13 +59,13 @@ def opts_from_cmd(cmd: Dict[str, Any]) -> Tuple[Options, FamilyKey, float, float
|
||||
|
||||
BaseKey = Tuple[FamilyKey, int, int]
|
||||
FaceKey = Tuple[str, BaseKey]
|
||||
SAMPLE_TEXT = string.ascii_lowercase + string.digits + string.ascii_uppercase + ' ' + string.punctuation
|
||||
SAMPLE_TEXT = string.ascii_lowercase + ' ' + string.digits + ' ' + string.ascii_uppercase + ' ' + string.punctuation
|
||||
|
||||
|
||||
def render_face_sample(font: Descriptor, opts: Options, dpi_x: float, dpi_y: float, width: int, height: int, output_dir: str) -> str:
|
||||
def render_face_sample(font: Descriptor, opts: Options, dpi_x: float, dpi_y: float, width: int, height: int) -> bytes:
|
||||
face = face_from_descriptor(font)
|
||||
face.set_size(opts.font_size, dpi_x, dpi_y)
|
||||
face.render_sample_text(SAMPLE_TEXT, width, height, opts.foreground.rgb)
|
||||
return face.render_sample_text(SAMPLE_TEXT, width, height, opts.foreground.rgb)
|
||||
|
||||
|
||||
def render_family_sample(
|
||||
@@ -85,7 +89,9 @@ def render_family_sample(
|
||||
if cached is not None:
|
||||
ans[x] = cached
|
||||
else:
|
||||
cache[key] = ans[x] = render_face_sample(desc, opts, dpi_x, dpi_y, width, height, output_dir)
|
||||
with tempfile.NamedTemporaryFile(delete=False, suffix='.rgba', dir=output_dir) as tf:
|
||||
tf.write(render_face_sample(desc, opts, dpi_x, dpi_y, width, height))
|
||||
cache[key] = ans[x] = tf.name
|
||||
return ans
|
||||
|
||||
|
||||
@@ -106,3 +112,29 @@ def main() -> None:
|
||||
send_to_kitten(render_family_sample(opts, family_key, dpi_x, dpi_y, cmd['width'], cmd['height'], cmd['output_dir'], cache))
|
||||
else:
|
||||
raise SystemExit(f'Unknown action: {action}')
|
||||
|
||||
|
||||
def query_kitty() -> Dict[str, str]:
|
||||
import subprocess
|
||||
ans = {}
|
||||
for line in subprocess.check_output([kitten_exe(), 'query-terminal']).decode().splitlines():
|
||||
k, sep, v = line.partition(':')
|
||||
if sep == ':':
|
||||
ans[k] = v.strip()
|
||||
return ans
|
||||
|
||||
|
||||
def showcase(family: str) -> None:
|
||||
q = query_kitty()
|
||||
opts = Options()
|
||||
opts.foreground = to_color(q['foreground'])
|
||||
opts.background = to_color(q['background'])
|
||||
opts.font_size = float(q['font_size'])
|
||||
opts.font_family = parse_font_spec(family)
|
||||
font_files = get_font_files(opts)
|
||||
desc = font_files['medium']
|
||||
ss = screen_size_function()()
|
||||
width = ss.cell_width * ss.cols
|
||||
height = 5 * ss.cell_height
|
||||
bitmap = render_face_sample(desc, opts, float(q['dpi_x']), float(q['dpi_y']), width, height)
|
||||
display_bitmap(bitmap, width, height)
|
||||
|
||||
Reference in New Issue
Block a user