mirror of
https://github.com/kovidgoyal/kitty.git
synced 2026-11-19 14:45:29 +00:00
Some checks are pending
CI / Linux (python=3.14 cc=clang sanitize=1) (push) Waiting to run
CI / Linux (python=3.12 cc=gcc sanitize=0) (push) Waiting to run
CI / Linux (python=3.13 cc=gcc sanitize=1) (push) Waiting to run
CI / Linux package (push) Waiting to run
CI / Bundle test (macos-latest) (push) Waiting to run
CI / Bundle test (ubuntu-latest) (push) Waiting to run
CI / macOS Brew (push) Waiting to run
CI / Test ./dev.sh and benchmark (push) Waiting to run
Font collection files contain multiple faces, which can have different variable data, such as different named styles. Both the C font selection code and the Python variable data cache were keyed on the file path alone, so once the data for one face of a collection was cached, other faces in the same file got that data. For example, selecting a named style that only exists in the second face of a collection failed if the first face was looked at first, falling back to a different family. Key the caches on the face index for fontconfig and the postscript name for CoreText, which does not expose the face index. Do not share cache entries for fonts without a path in the C code, matching the Python code. Expose the face index on the FreeType Face object for the Python cache.
2016 lines
72 KiB
Python
2016 lines
72 KiB
Python
#!/usr/bin/env python
|
||
# License: GPL v3 Copyright: 2017, Kovid Goyal <kovid at kovidgoyal.net>
|
||
|
||
import array
|
||
import os
|
||
import sys
|
||
import tempfile
|
||
import unittest
|
||
from collections.abc import Iterable
|
||
from functools import lru_cache, partial
|
||
from itertools import repeat
|
||
from math import ceil
|
||
|
||
from kitty.constants import is_macos, read_kitty_resource
|
||
from kitty.fast_data_types import (
|
||
DECAWM,
|
||
ParsedFontFeature,
|
||
Screen,
|
||
get_fallback_font,
|
||
render_decoration,
|
||
set_allow_use_of_box_fonts,
|
||
sprite_idx_to_pos,
|
||
sprite_map_set_layout,
|
||
sprite_map_set_limits,
|
||
test_render_line,
|
||
test_shape,
|
||
test_sprite_position_increment,
|
||
test_sprite_tracker_ynum,
|
||
wcwidth,
|
||
)
|
||
from kitty.fonts import family_name_to_key
|
||
from kitty.fonts.common import FontSpec, all_fonts_map, face_from_descriptor, get_font_files, get_named_style, spec_for_face
|
||
from kitty.fonts.render import coalesce_symbol_maps, create_face, render_string, setup_for_testing, shape_string
|
||
from kitty.options.types import Options
|
||
|
||
from .base import BaseTest, draw_multicell
|
||
|
||
|
||
def parse_font_spec(spec):
|
||
return FontSpec.from_setting(spec)
|
||
|
||
|
||
@lru_cache(maxsize=64)
|
||
def testing_font_data(name):
|
||
return read_kitty_resource(name, __name__.rpartition('.')[0])
|
||
|
||
|
||
class Selection(BaseTest):
|
||
def test_font_selection(self):
|
||
self.set_options({'font_features': {'LiberationMono': (ParsedFontFeature('-dlig'),)}})
|
||
opts = Options()
|
||
fonts_map = all_fonts_map(True)
|
||
names = set(fonts_map['family_map']) | set(fonts_map['variable_map'])
|
||
del fonts_map
|
||
|
||
def s(family: str, *expected: str, alternate=None) -> None:
|
||
opts.font_family = parse_font_spec(family)
|
||
ff = get_font_files(opts)
|
||
actual = tuple(face_from_descriptor(ff[x]).postscript_name() for x in ('medium', 'bold', 'italic', 'bi')) # type: ignore
|
||
del ff
|
||
for x in actual:
|
||
if '/' in x: # Old FreeType failed to generate postscript name for a variable font probably
|
||
return
|
||
with self.subTest(spec=family):
|
||
try:
|
||
self.ae(expected, actual)
|
||
except AssertionError:
|
||
if alternate:
|
||
self.ae(alternate, actual)
|
||
else:
|
||
raise
|
||
|
||
def both(family: str, *expected: str, alternate=None) -> None:
|
||
for family in (family, f'family="{family}"'):
|
||
s(family, *expected, alternate=alternate)
|
||
|
||
def has(family, allow_missing_in_ci=False):
|
||
ans = family_name_to_key(family) in names
|
||
if self.is_ci and not allow_missing_in_ci and not ans:
|
||
raise AssertionError(f'The family: {family} is not available')
|
||
return ans
|
||
|
||
def t(family, psprefix, bold='Bold', italic='Italic', bi='', reg='Regular', allow_missing_in_ci=False, alternate=None):
|
||
if has(family, allow_missing_in_ci=allow_missing_in_ci):
|
||
bi = bi or bold + italic
|
||
if reg:
|
||
reg = '-' + reg
|
||
both(family, f'{psprefix}{reg}', f'{psprefix}-{bold}', f'{psprefix}-{italic}', f'{psprefix}-{bi}', alternate=alternate)
|
||
|
||
t('Source Code Pro', 'SourceCodePro', 'Semibold', 'It')
|
||
t('sourcecodeVf', 'SourceCodeVF', 'Semibold')
|
||
|
||
# The Arch ttf-fira-code package excludes the variable fonts for some reason
|
||
t(
|
||
'fira code',
|
||
'FiraCodeRoman',
|
||
'SemiBold',
|
||
'Regular',
|
||
'SemiBold',
|
||
alternate=('FiraCode-Regular', 'FiraCode-SemiBold', 'FiraCode-Retina', 'FiraCode-SemiBold'),
|
||
)
|
||
t('hack', 'Hack')
|
||
# some ubuntu systems (such as the build VM) have only the regular and
|
||
# bold faces of DejaVu Sans Mono installed.
|
||
# t('DejaVu Sans Mono', 'DejaVuSansMono', reg='', italic='Oblique')
|
||
t('ubuntu mono', 'UbuntuMono')
|
||
t('liberation mono', 'LiberationMono', reg='')
|
||
t('ibm plex mono', 'IBMPlexMono', 'SmBld', reg='')
|
||
t('iosevka fixed', 'Iosevka-Fixed', 'Semibold', reg='', bi='Semibold-Italic', allow_missing_in_ci=True)
|
||
t('iosevka term', 'Iosevka-Term', 'Semibold', reg='', bi='Semibold-Italic', allow_missing_in_ci=True)
|
||
t('fantasque sans mono', 'FantasqueSansMono')
|
||
t('jetbrains mono', 'JetBrainsMono', 'SemiBold')
|
||
t('consolas', 'Consolas', reg='', allow_missing_in_ci=True)
|
||
if has('cascadia code'):
|
||
if is_macos:
|
||
both('cascadia code', 'CascadiaCode-Regular', 'CascadiaCode-Regular_SemiBold', 'CascadiaCode-Italic', 'CascadiaCode-Italic_SemiBold-Italic')
|
||
else:
|
||
both('cascadia code', 'CascadiaCodeRoman-Regular', 'CascadiaCodeRoman-SemiBold', 'CascadiaCode-Italic', 'CascadiaCode-SemiBoldItalic')
|
||
if has('cascadia mono'):
|
||
if is_macos:
|
||
both('cascadia mono', 'CascadiaMono-Regular', 'CascadiaMono-Regular_SemiBold', 'CascadiaMono-Italic', 'CascadiaMono-Italic_SemiBold-Italic')
|
||
else:
|
||
both('cascadia mono', 'CascadiaMonoRoman-Regular', 'CascadiaMonoRoman-SemiBold', 'CascadiaMono-Italic', 'CascadiaMono-SemiBoldItalic')
|
||
if has('operator mono', allow_missing_in_ci=True):
|
||
both('operator mono', 'OperatorMono-Medium', 'OperatorMono-Bold', 'OperatorMono-MediumItalic', 'OperatorMono-BoldItalic')
|
||
|
||
# Test variable font selection
|
||
|
||
if has('SourceCodeVF'):
|
||
opts = Options()
|
||
opts.font_family = parse_font_spec('family="SourceCodeVF" variable_name="SourceCodeUpright" style="Bold"')
|
||
ff = get_font_files(opts)
|
||
face = face_from_descriptor(ff['medium'])
|
||
self.ae(get_named_style(face)['name'], 'Bold')
|
||
face = face_from_descriptor(ff['italic'])
|
||
self.ae(get_named_style(face)['name'], 'Bold Italic')
|
||
face = face_from_descriptor(ff['bold'])
|
||
self.ae(get_named_style(face)['name'], 'Black')
|
||
face = face_from_descriptor(ff['bi'])
|
||
self.ae(get_named_style(face)['name'], 'Black Italic')
|
||
opts.font_family = parse_font_spec('family=SourceCodeVF variable_name=SourceCodeUpright wght=470')
|
||
opts.italic_font = parse_font_spec('family=SourceCodeVF variable_name=SourceCodeItalic style=Black')
|
||
ff = get_font_files(opts)
|
||
self.assertFalse(get_named_style(ff['medium']))
|
||
self.ae(get_named_style(ff['italic'])['name'], 'Black Italic')
|
||
if has('cascadia code'):
|
||
opts = Options()
|
||
opts.font_family = parse_font_spec('family="cascadia code"')
|
||
opts.italic_font = parse_font_spec('family="cascadia code" variable_name= style="Light Italic"')
|
||
ff = get_font_files(opts)
|
||
|
||
def t(x, **kw):
|
||
if 'spec' in kw:
|
||
fs = FontSpec.from_setting('family="Cascadia Code" ' + kw['spec'])._replace(created_from_string='')
|
||
else:
|
||
kw['family'] = 'Cascadia Code'
|
||
fs = FontSpec(**kw)
|
||
face = face_from_descriptor(ff[x])
|
||
self.ae(fs.as_setting, spec_for_face('Cascadia Code', face).as_setting)
|
||
|
||
t('medium', variable_name='CascadiaCodeRoman', style='Regular')
|
||
t('italic', variable_name='', style='Light Italic')
|
||
|
||
opts = Options()
|
||
opts.font_family = parse_font_spec('family="cascadia code" variable_name=CascadiaCodeRoman wght=455')
|
||
opts.italic_font = parse_font_spec('family="cascadia code" variable_name= wght=405')
|
||
opts.bold_font = parse_font_spec('family="cascadia code" variable_name=CascadiaCodeRoman wght=603')
|
||
ff = get_font_files(opts)
|
||
t('medium', spec='variable_name=CascadiaCodeRoman wght=455')
|
||
t('italic', spec='variable_name= wght=405')
|
||
t('bold', spec='variable_name=CascadiaCodeRoman wght=603')
|
||
t('bi', spec='variable_name= wght=603')
|
||
|
||
# Test font features
|
||
if has('liberation mono'):
|
||
opts = Options()
|
||
opts.font_family = parse_font_spec('family="liberation mono"')
|
||
ff = get_font_files(opts)
|
||
self.ae(face_from_descriptor(ff['medium']).applied_features(), {'dlig': '-dlig'})
|
||
self.ae(face_from_descriptor(ff['bold']).applied_features(), {})
|
||
opts.font_family = parse_font_spec('family="liberation mono" features="dlig test=3"')
|
||
ff = get_font_files(opts)
|
||
self.ae(face_from_descriptor(ff['medium']).applied_features(), {'dlig': 'dlig', 'test': 'test=3'})
|
||
self.ae(face_from_descriptor(ff['bold']).applied_features(), {'dlig': 'dlig', 'test': 'test=3'})
|
||
|
||
def test_font_selection_api(self):
|
||
# Font selection is done in C, optionally in a thread, see font-select.c
|
||
from kitty.fast_data_types import finish_font_selection
|
||
from kitty.fonts.common import finish_font_files_selection, start_font_files_selection
|
||
|
||
def key(d):
|
||
return tuple((k, d.get(k)) for k in ('path', 'index', 'named_style', 'axes', 'axis_map', 'matrix', 'postscript_name'))
|
||
|
||
def keys(ff):
|
||
return tuple(key(ff[x]) for x in ('medium', 'bold', 'italic', 'bi'))
|
||
|
||
def threaded(opts, symbol_map_families=()):
|
||
start_font_files_selection(opts, symbol_map_families, threaded=True)
|
||
return finish_font_files_selection(opts)
|
||
|
||
with self.assertRaises(RuntimeError):
|
||
finish_font_selection()
|
||
families = sorted({v[0]['family'] for v in all_fonts_map(True)['family_map'].values()})[:6]
|
||
opts = Options()
|
||
for family in families + ['NoSuchFontXYZ']:
|
||
with self.subTest(family=family):
|
||
opts.font_family = parse_font_spec(f'family="{family}"')
|
||
expected = keys(get_font_files(opts))
|
||
self.ae(expected, keys(threaded(opts)[0]))
|
||
if family.isascii():
|
||
# names are matched ignoring case and extra whitespace
|
||
opts.font_family = parse_font_spec(f'family=" {family.swapcase().replace(" ", " ")} "')
|
||
self.ae(expected, keys(get_font_files(opts)))
|
||
# A system font specification
|
||
opts.font_family = parse_font_spec('monospace')
|
||
expected = keys(get_font_files(opts))
|
||
self.ae(expected, keys(threaded(opts)[0]))
|
||
# Starting a new selection discards a running one
|
||
other = Options()
|
||
other.font_family = parse_font_spec('family="NoSuchFontXYZ"')
|
||
start_font_files_selection(other, threaded=True)
|
||
self.ae(expected, keys(threaded(opts)[0]))
|
||
# Fonts for symbol_map
|
||
symbol_families = ('monospace', families[0]) if is_macos else ('monospace', families[0], 'NoSuchFontXYZ')
|
||
ff, symbols, nerd_font = threaded(opts, symbol_families)
|
||
self.assertIsNone(nerd_font)
|
||
self.ae(expected, keys(ff))
|
||
self.ae(len(symbols), len(symbol_families))
|
||
for d, bold, italic in symbols:
|
||
self.assertTrue(d['path'])
|
||
self.assertIsInstance(bold, bool)
|
||
self.assertIsInstance(italic, bool)
|
||
# features are applied to the selected faces
|
||
opts.font_family = parse_font_spec(f'family="{families[0]}" features="+zero"')
|
||
ff = get_font_files(opts)
|
||
for x in ('medium', 'bold', 'italic', 'bi'):
|
||
self.ae(ff[x].get('features'), (ParsedFontFeature('+zero'),))
|
||
|
||
def test_builtin_nerd_font(self):
|
||
# The builtin NERD font is prepared by the font selection thread and is
|
||
# used for symbol_map entries for it and for fallback
|
||
from kitty.constants import fonts_dir
|
||
from kitty.fast_data_types import create_test_font_group, set_send_sprite_to_gpu, sprite_map_set_limits
|
||
from kitty.fonts import render
|
||
|
||
path = os.path.join(fonts_dir, 'SymbolsNerdFontMono-Regular.ttf')
|
||
if not os.path.exists(path):
|
||
self.skipTest('The builtin NERD font is not present')
|
||
opts = self.set_options({'symbol_map': {(0xE0B0, 0xE0B0): 'Symbols Nerd Font Mono'}})
|
||
sprite_map_set_limits(100000, 100)
|
||
set_send_sprite_to_gpu(lambda *a: None)
|
||
try:
|
||
render.set_font_family(opts, add_builtin_nerd_font=True, threaded=True)
|
||
create_test_font_group(11.0, 96.0, 96.0) # uses the results of the selection
|
||
d = render.builtin_nerd_font_descriptor
|
||
self.assertIsNotNone(d)
|
||
self.ae(d['path'], path) # type: ignore
|
||
self.ae(len(render.current_faces), 5)
|
||
self.ae(face_from_descriptor(render.current_faces[4][0]).postscript_name(), 'SymbolsNFM') # type: ignore
|
||
# The builtin NERD font is used for fallback for NERD font icons,
|
||
# unless a font installed on the system has them, which is
|
||
# preferred. Such fonts need not have NERD in their names, for
|
||
# example, PragmataPro has the NERD font icons.
|
||
face = get_fallback_font('\U000f0001', False, False)
|
||
if face.path != path:
|
||
self.assertNotIn('LastResort', face.postscript_name(), f'Unexpected fallback font for a NERD font icon: {face.path}')
|
||
else:
|
||
self.ae(face.postscript_name(), 'SymbolsNFM')
|
||
finally:
|
||
set_send_sprite_to_gpu(None)
|
||
|
||
def test_variable_data_from_font_file(self):
|
||
# The pure C reading of variable font data used by font selection must
|
||
# match the data read via the platform font APIs
|
||
from kitty.fast_data_types import variable_data_from_font_file
|
||
|
||
seen = set()
|
||
count = 0
|
||
for descriptors in all_fonts_map(False)['variable_map'].values():
|
||
for d in descriptors:
|
||
path = d['path']
|
||
if path in seen or (is_macos and not path.lower().endswith(('.ttf', '.otf'))):
|
||
continue
|
||
seen.add(path)
|
||
index = d.get('index', 0) & 0xFFFF # type: ignore
|
||
expected = dict(face_from_descriptor(d).get_variable_data())
|
||
expected.pop('multi_axis_styles', None) # type: ignore
|
||
expected['axes'] = tuple({k: v for k, v in ax.items() if k != 'name'} for ax in expected['axes']) # type: ignore
|
||
with self.subTest(path=path):
|
||
self.ae(expected, variable_data_from_font_file(path, index))
|
||
count += 1
|
||
if self.is_ci and not count:
|
||
raise AssertionError('No variable fonts found')
|
||
|
||
def test_variable_data_from_crafted_font(self):
|
||
import struct
|
||
|
||
from kitty.fast_data_types import variable_data_from_font_file
|
||
|
||
def name_table(names: dict[int, str]) -> bytes:
|
||
records, storage = b'', b''
|
||
for name_id, text in names.items():
|
||
data = text.encode('utf-16-be')
|
||
records += struct.pack('>6H', 3, 1, 1033, name_id, len(data), len(storage))
|
||
storage += data
|
||
return struct.pack('>3H', 0, len(names), 6 + len(records)) + records + storage
|
||
|
||
def fvar_table(axes: list[tuple[bytes, int]], instances: list[tuple[int, list[float], int]]) -> bytes:
|
||
instance_size = 4 + 4 * len(axes) + 2
|
||
ans = struct.pack('>8H', 1, 0, 16, 2, len(axes), 20, len(instances), instance_size)
|
||
for tag, name_id in axes:
|
||
ans += tag + struct.pack('>3iHH', 0, 0, 1000 << 16, 0, name_id)
|
||
for name_id, coords, psname_id in instances:
|
||
ans += struct.pack('>HH', name_id, 0) + b''.join(struct.pack('>i', int(c * 65536)) for c in coords) + struct.pack('>H', psname_id)
|
||
return ans
|
||
|
||
def sfnt(tables: dict[bytes, bytes]) -> bytes:
|
||
offset = 12 + 16 * len(tables)
|
||
header, data = struct.pack('>IHHHH', 0x00010000, len(tables), 0, 0, 0), b''
|
||
for tag, table in tables.items():
|
||
header += tag + struct.pack('>III', 0, offset + len(data), len(table))
|
||
data += table + b'\0' * (-len(table) % 4)
|
||
return header + data
|
||
|
||
names = {256: 'Weight', 257: 'Odd', 258: 'Bold', 259: 'Test-Bold', 1: 'Test', 25: 'TestVar'}
|
||
# An axis tag that is not valid UTF-8 and a duplicated axis tag
|
||
odd_tag = b'\xff\xfe\x80a'
|
||
fvar = fvar_table([(b'wght', 256), (odd_tag, 257), (b'wght', 256)], [(258, [700, 1, 800], 259)])
|
||
with tempfile.TemporaryDirectory() as tdir:
|
||
path = os.path.join(tdir, 'test.ttf')
|
||
with open(path, 'wb') as f:
|
||
f.write(sfnt({b'fvar': fvar, b'name': name_table(names)}))
|
||
vd = variable_data_from_font_file(path)
|
||
odd = odd_tag.decode('latin-1')
|
||
self.ae([a['tag'] for a in vd['axes']], ['wght', odd, 'wght'])
|
||
self.ae([a['strid'] for a in vd['axes']], ['Weight', 'Odd', 'Weight'])
|
||
self.ae(vd['variations_postscript_name_prefix'], 'TestVar')
|
||
self.ae(len(vd['named_styles']), 1)
|
||
ns = vd['named_styles'][0]
|
||
self.ae((ns['name'], ns['psname']), ('Bold', 'Test-Bold'))
|
||
# each tag used once, in order of first occurrence, with the value of the last occurrence
|
||
self.ae(list(ns['axis_values'].items()), [('wght', 800.0), (odd, 1.0)])
|
||
|
||
# A file that is not a valid font is a ValueError, not an OSError with a meaningless errno
|
||
with open(path, 'wb') as f:
|
||
f.write(b'\0\1\0\0\0')
|
||
with self.assertRaises(ValueError):
|
||
variable_data_from_font_file(path)
|
||
with self.assertRaises(FileNotFoundError):
|
||
variable_data_from_font_file(os.path.join(tdir, 'does-not-exist.ttf'))
|
||
|
||
def test_variable_data_for_font_collection_faces(self):
|
||
# Variable data must be cached per face, not per file, as font
|
||
# collection files contain multiple faces with different variable data.
|
||
import json
|
||
import subprocess
|
||
|
||
try:
|
||
from fontTools.fontBuilder import FontBuilder
|
||
from fontTools.pens.ttGlyphPen import TTGlyphPen
|
||
from fontTools.ttLib import TTCollection
|
||
except ImportError:
|
||
self.skipTest('fontTools is not available')
|
||
from kitty.constants import kitty_exe
|
||
|
||
def build_face(family: str, bold_name: str):
|
||
fb = FontBuilder(1000, isTTF=True)
|
||
chars = {ch: f'g{ch}' for ch in range(33, 127)}
|
||
fb.setupGlyphOrder(['.notdef', 'space'] + list(chars.values()))
|
||
fb.setupCharacterMap({32: 'space', **chars})
|
||
pen = TTGlyphPen(None)
|
||
pen.moveTo((100, 0))
|
||
pen.lineTo((100, 700))
|
||
pen.lineTo((500, 700))
|
||
pen.lineTo((500, 0))
|
||
pen.closePath()
|
||
box = pen.glyph()
|
||
fb.setupGlyf({'.notdef': box, 'space': TTGlyphPen(None).glyph(), **{g: box for g in chars.values()}})
|
||
fb.setupHorizontalMetrics({g: (600, 100) for g in fb.font.getGlyphOrder()})
|
||
fb.setupHorizontalHeader(ascent=800, descent=-200)
|
||
fb.setupNameTable({'familyName': family, 'styleName': 'Regular', 'psName': f'{family}-Regular', 'variationsPostScriptNamePrefix': family})
|
||
fb.setupOS2(sTypoAscender=800, sTypoDescender=-200, usWinAscent=800, usWinDescent=200, xAvgCharWidth=600)
|
||
fb.setupPost(isFixedPitch=1)
|
||
fb.setupFvar(
|
||
[('wght', 100, 400, 900, 'Weight')],
|
||
[{'location': {'wght': w}, 'stylename': n, 'postscriptfontname': f'{family}-{n}'} for w, n in ((400, 'Regular'), (700, bold_name))],
|
||
)
|
||
return fb.font
|
||
|
||
checker = """
|
||
import json, sys
|
||
from kitty.fast_data_types import add_font_file, set_options
|
||
from kitty.fonts.common import FontSpec, all_fonts_map, get_font_files, get_variable_data_for_descriptor
|
||
from kitty.options.types import Options
|
||
# not an assert, as kitty runs python with optimizations, which removes asserts
|
||
if not add_font_file(sys.argv[-1]):
|
||
raise SystemExit('Failed to add font file')
|
||
set_options(Options())
|
||
opts = Options()
|
||
opts.font_family = FontSpec.from_setting('family=KittyTestCollectionA style=Regular')
|
||
opts.bold_font = FontSpec.from_setting('family=KittyTestCollectionB style=Heavy')
|
||
ff = get_font_files(opts)
|
||
vmap = all_fonts_map(True)['variable_map']
|
||
# fontconfig descriptors identify the named style by index, CoreText ones by axis values
|
||
def style(d):
|
||
return d.get('axis_map', {}).get('wght') if d['descriptor_type'] == 'core_text' else d.get('named_style')
|
||
print(json.dumps({
|
||
'medium': [ff['medium'].get('family'), style(ff['medium'])],
|
||
'bold': [ff['bold'].get('family'), style(ff['bold'])],
|
||
'named_styles': [
|
||
[ns['name'] for ns in get_variable_data_for_descriptor(vmap[k][0])['named_styles']]
|
||
for k in ('kittytestcollectiona', 'kittytestcollectionb')],
|
||
}))
|
||
"""
|
||
with tempfile.TemporaryDirectory() as tdir:
|
||
path = os.path.join(tdir, 'collection.ttc')
|
||
c = TTCollection()
|
||
c.fonts = [build_face('KittyTestCollectionA', 'Bold'), build_face('KittyTestCollectionB', 'Heavy')]
|
||
c.save(path)
|
||
script = os.path.join(tdir, 'checker.py')
|
||
with open(script, 'w') as f:
|
||
f.write(checker)
|
||
# Use a separate process so that the added font does not affect other tests
|
||
cp = subprocess.run([kitty_exe(), '+launch', script, path], capture_output=True, text=True)
|
||
self.assertEqual(cp.returncode, 0, cp.stderr)
|
||
result = json.loads(cp.stdout.strip().splitlines()[-1])
|
||
self.ae(result['medium'], ['KittyTestCollectionA', 400 if is_macos else 0])
|
||
self.ae(result['bold'], ['KittyTestCollectionB', 700 if is_macos else 1])
|
||
self.ae(result['named_styles'], [['Regular', 'Bold'], ['Regular', 'Heavy']])
|
||
|
||
def test_invalid_font_feature(self):
|
||
with self.assertRaises(ValueError) as cm:
|
||
ParsedFontFeature('!!bad!!')
|
||
self.assertIn('!!bad!!', str(cm.exception))
|
||
|
||
def test_render_decoration(self):
|
||
w, h = 10, 20
|
||
for which in ('curl', 'dashed', 'dotted', 'double', 'straight', 'strikethrough'):
|
||
data = render_decoration(which, w, h, 15, 2)
|
||
self.assertIsInstance(data, bytes)
|
||
self.ae(len(data), w * h)
|
||
# Strikethrough metrics default to zero
|
||
if which != 'strikethrough':
|
||
self.assertTrue(any(data), f'{which} decoration is empty')
|
||
with self.assertRaises(KeyError):
|
||
render_decoration('nonexistent', w, h, 15, 2)
|
||
|
||
def test_synthetic_italic_matrix(self):
|
||
# A roman-only font that find_best_match finds (e.g. Fira Code, which ships
|
||
# no italic face) must get fontconfig's synthetic-italic FC_MATRIX
|
||
# (90-synthetic.conf) attached, so its italic renders slanted rather than
|
||
# upright; real-italic faces must not. The shear value is fontconfig's, not
|
||
# ours, so assert the invariant (a non-identity matrix is present), not the
|
||
# exact tuple, for cross-config stability.
|
||
if is_macos:
|
||
self.skipTest('synthetic-italic FC_MATRIX is a fontconfig feature')
|
||
from kitty.fonts.fontconfig import FC_MONO, fc_match
|
||
|
||
names = set(all_fonts_map(True)['family_map']) | set(all_fonts_map(True)['variable_map'])
|
||
if family_name_to_key('fira code') not in names:
|
||
self.skipTest('Fira Code not installed')
|
||
# Probe fc_match directly so we can tell "environment lacks the rule" (skip)
|
||
# from "code did not attach the matrix" (fail).
|
||
if fc_match('Fira Code', False, True, FC_MONO).get('matrix') is None:
|
||
self.skipTest('fontconfig 90-synthetic.conf not active; no synthetic-italic matrix')
|
||
opts = Options()
|
||
opts.font_family = parse_font_spec('Fira Code')
|
||
ff = get_font_files(opts)
|
||
self.assertIsNone(ff['medium'].get('matrix')) # upright stays upright
|
||
mi = ff['italic'].get('matrix')
|
||
self.assertIsNotNone(mi) # roman, no italic -> sheared
|
||
self.assertNotEqual(mi[1], 0.0) # actually slanted, not identity
|
||
# Faces are built from a size-specialized descriptor at render time; the
|
||
# matrix must survive specialize_font_descriptor or the glyphs render
|
||
# upright despite the descriptor above being correct.
|
||
from kitty.fast_data_types import specialize_font_descriptor
|
||
|
||
sd = specialize_font_descriptor(dict(ff['italic']), 12.0, 96.0, 96.0)
|
||
self.ae(sd.get('matrix'), mi)
|
||
if family_name_to_key('liberation mono') in names: # real-italic control
|
||
opts.font_family = parse_font_spec('Liberation Mono')
|
||
self.assertIsNone(get_font_files(opts)['italic'].get('matrix'))
|
||
|
||
|
||
def block_helpers(s, sprites, cell_width, cell_height):
|
||
mr = {}
|
||
actual = b''
|
||
block_size = cell_width * cell_height * 4
|
||
|
||
def full_block():
|
||
return b'\xff' * block_size
|
||
|
||
def empty_block():
|
||
return b'\0' * block_size
|
||
|
||
def half_block(first=b'\xff', second=b'\0', swap=False):
|
||
frac = 0.5
|
||
height = ceil(frac * cell_height)
|
||
rest = cell_height - height
|
||
if swap:
|
||
height, rest = rest, height
|
||
first, second = second, first
|
||
return (first * (height * cell_width * 4)) + (second * rest * cell_width * 4)
|
||
|
||
def quarter_block():
|
||
frac = 0.5
|
||
height = ceil(frac * cell_height)
|
||
width = ceil(frac * cell_width)
|
||
ans = array.array('I', b'\0' * block_size)
|
||
for y in range(height):
|
||
pos = cell_width * y
|
||
for x in range(width):
|
||
ans[pos + x] = 0xFFFFFFFF
|
||
return ans.tobytes()
|
||
|
||
def upper_half_block():
|
||
return half_block()
|
||
|
||
def lower_half_block():
|
||
return half_block(swap=True)
|
||
|
||
def block_as_str(a):
|
||
pixels = array.array('I', a)
|
||
|
||
def row(y):
|
||
pos = y * cell_width
|
||
return ' '.join(f'{int(pixels[pos + x] != 0)}' for x in range(cell_width))
|
||
|
||
return '\n'.join(row(y) for y in range(cell_height))
|
||
|
||
def assert_blocks(a, b, msg=''):
|
||
nonlocal mr, actual, full_block, half_block, quarter_block, block_test, empty_block
|
||
if a != b:
|
||
del mr, actual, full_block, half_block, quarter_block, block_test, empty_block
|
||
msg = msg or 'block not equal'
|
||
if len(a) != len(b):
|
||
assert_blocks.__msg = msg + f' block lengths not equal: {len(a) / 4} != {len(b) / 4}'
|
||
else:
|
||
assert_blocks.__msg = msg + '\n' + block_as_str(a) + '\n\n' + block_as_str(b)
|
||
del a, b
|
||
raise AssertionError(assert_blocks.__msg)
|
||
|
||
def multiline_render(text, scale=1, width=1, **kw):
|
||
s.reset()
|
||
draw_multicell(s, text, scale=scale, width=width, **kw)
|
||
ans = []
|
||
for y in range(scale):
|
||
line = s.line(y)
|
||
test_render_line(line)
|
||
for x in range(width * scale):
|
||
ans.append(sprites[sprite_idx_to_pos(line.sprite_at(x), setup_for_testing.xnum, setup_for_testing.ynum)])
|
||
return ans
|
||
|
||
def block_test(*expected, **kw):
|
||
nonlocal mr, actual
|
||
mr = multiline_render(kw.pop('text', '█'), **kw)
|
||
try:
|
||
z = zip(expected, mr, strict=True)
|
||
except TypeError:
|
||
z = zip(expected, mr)
|
||
for i, (expected, actual) in enumerate(z):
|
||
assert_blocks(expected(), actual, f'Block {i} expected != actual')
|
||
|
||
return full_block, empty_block, upper_half_block, lower_half_block, quarter_block, block_as_str, block_test
|
||
|
||
|
||
class FontBaseTest(BaseTest):
|
||
font_size = 16.0
|
||
dpi = 72.0
|
||
font_name = 'FiraCode-Medium.otf'
|
||
|
||
def path_for_font(self, name):
|
||
if name not in self.font_path_cache:
|
||
with open(os.path.join(self.tdir, name), 'wb') as f:
|
||
self.font_path_cache[name] = f.name
|
||
f.write(testing_font_data(name))
|
||
return self.font_path_cache[name]
|
||
|
||
def setUp(self):
|
||
super().setUp()
|
||
self.font_path_cache = {}
|
||
self.tdir = tempfile.mkdtemp()
|
||
self.addCleanup(self.rmtree_ignoring_errors, self.tdir)
|
||
path = self.path_for_font(self.font_name) if self.font_name else ''
|
||
tc = setup_for_testing(size=self.font_size, dpi=self.dpi, main_face_path=path)
|
||
self.sprites, self.cell_width, self.cell_height = tc.__enter__()
|
||
self.addCleanup(tc.__exit__)
|
||
self.assertEqual([k[0] for k in self.sprites], list(range(11)))
|
||
|
||
def tearDown(self):
|
||
del self.sprites, self.cell_width, self.cell_height
|
||
self.font_path_cache = {}
|
||
super().tearDown()
|
||
|
||
|
||
class Rendering(FontBaseTest):
|
||
def test_decorations_map_growth(self):
|
||
from kitty.fast_data_types import decorations_map_new_size as size
|
||
|
||
m = 4096
|
||
self.ae(size(0, 0, m), (4096, 1))
|
||
self.ae(size(5000, 4096, m), (4096, 2))
|
||
cap, reallocs = 0, 0
|
||
for count in range(100000):
|
||
if count >= cap:
|
||
w, h = size(count, cap, m)
|
||
cap, reallocs = w * h, reallocs + 1
|
||
self.assertLess(reallocs, 10)
|
||
full = m * m
|
||
self.ae(size(m * (m - 1), m * (m - 1), m), (m, m))
|
||
self.ae(size(full - 1, full // 2, m), (m, m))
|
||
self.assertIsNone(size(full, full, m))
|
||
|
||
def test_sprite_map(self):
|
||
sprite_map_set_limits(10, 3)
|
||
sprite_map_set_layout(5, 4) # 4 because of underline_exclusion row
|
||
self.ae(test_sprite_position_increment(), (0, 0, 0))
|
||
self.ae(test_sprite_position_increment(), (1, 0, 0))
|
||
self.ae(test_sprite_position_increment(), (0, 1, 0))
|
||
self.ae(test_sprite_position_increment(), (1, 1, 0))
|
||
self.ae(test_sprite_position_increment(), (0, 0, 1))
|
||
self.ae(test_sprite_position_increment(), (1, 0, 1))
|
||
self.ae(test_sprite_position_increment(), (0, 1, 1))
|
||
self.ae(test_sprite_position_increment(), (1, 1, 1))
|
||
self.ae(test_sprite_position_increment(), (0, 0, 2))
|
||
self.ae(test_sprite_position_increment(), (1, 0, 2))
|
||
|
||
def test_sprite_map_ynum_growth(self):
|
||
sprite_map_set_limits(10, 3)
|
||
sprite_map_set_layout(1, 1)
|
||
self.ae(test_sprite_tracker_ynum(), 1)
|
||
seen = [1]
|
||
for i in range(10 * 5 * 2 + 3):
|
||
self.ae(test_sprite_position_increment(), (i % 10, (i // 10) % 5, i // 50))
|
||
ynum = test_sprite_tracker_ynum()
|
||
self.assertLessEqual(ynum, 5)
|
||
if ynum != seen[-1]:
|
||
seen.append(ynum)
|
||
self.ae(seen, [1, 2, 4, 5])
|
||
|
||
def test_box_drawing(self):
|
||
s = self.create_screen(cols=len(box_chars) + 1, lines=1, scrollback=0)
|
||
prerendered = len(self.sprites)
|
||
s.draw(''.join(box_chars))
|
||
line = s.line(0)
|
||
test_render_line(line)
|
||
self.assertEqual(len(self.sprites) - prerendered, len(box_chars))
|
||
|
||
def test_render_lines_scrolled_in(self):
|
||
# lines cleared by scrolling keep stale render data, so they must be
|
||
# dirty, so that rendering them replaces it
|
||
s = self.create_screen(cols=5, lines=3, scrollback=0)
|
||
s.draw('abcde' * s.lines)
|
||
for y in range(s.lines):
|
||
test_render_line(s.line(y))
|
||
self.assertTrue(all(s.line(y).sprite_at(x) for y in range(s.lines) for x in range(s.columns)))
|
||
for reverse in (False, True):
|
||
s.reverse_index() if reverse else s.index()
|
||
y = 0 if reverse else s.lines - 1
|
||
self.ae(str(s.line(y)), '')
|
||
self.assertIn(y, s.linebuf.dirty_lines())
|
||
line = s.line(y)
|
||
test_render_line(line)
|
||
self.ae([line.sprite_at(x) for x in range(s.columns)], [0] * s.columns)
|
||
s.cursor_position(1, 1)
|
||
|
||
def test_line_thickness_scales_with_font_size(self):
|
||
from kitty.fast_data_types import create_test_font_group, render_box_char
|
||
from kitty.fonts.render import set_font_family
|
||
|
||
options = {'font_size': self.font_size}
|
||
opts = self.set_options(options)
|
||
chars = '─│╭┃'
|
||
|
||
def check(factor):
|
||
set_font_family(opts) # resets font groups so that the first one is at the specified size
|
||
cell_width, cell_height, _ = create_test_font_group(factor * self.font_size, self.dpi, self.dpi)
|
||
s = self.create_screen(cols=len(chars) + 1, lines=1, scrollback=0, options=options)
|
||
s.draw(chars)
|
||
line = s.line(0)
|
||
test_render_line(line)
|
||
|
||
def alpha(sprite_idx):
|
||
pixels = array.array('I', self.sprites[sprite_idx_to_pos(sprite_idx, setup_for_testing.xnum, setup_for_testing.ynum)])
|
||
return bytes(p & 0xFF for p in pixels)
|
||
|
||
for i, ch in enumerate(chars):
|
||
expected = render_box_char(ord(ch), cell_width, cell_height, factor, self.dpi, self.dpi)
|
||
self.ae(alpha(line.sprite_at(i)), expected, f'{ch!r} not rendered with line thickness scale: {factor}')
|
||
beam = alpha(2)
|
||
mid = (cell_height // 2) * cell_width
|
||
# dpi is 72 so 1pt == 1px
|
||
self.ae(sum(1 for x in range(cell_width) if beam[mid + x]), round(factor * opts.cursor_beam_thickness))
|
||
|
||
check(1)
|
||
check(2)
|
||
check(4)
|
||
|
||
def test_scaled_box_drawing(self):
|
||
self.scaled_drawing_test()
|
||
|
||
def test_scaled_font_drawing(self):
|
||
set_allow_use_of_box_fonts(False)
|
||
try:
|
||
self.scaled_drawing_test()
|
||
finally:
|
||
set_allow_use_of_box_fonts(True)
|
||
|
||
def scaled_drawing_test(self):
|
||
s = self.create_screen(cols=8, lines=8, scrollback=0)
|
||
full_block, empty_block, upper_half_block, lower_half_block, quarter_block, block_as_str, block_test = block_helpers(
|
||
s, self.sprites, self.cell_width, self.cell_height
|
||
)
|
||
block_test(full_block)
|
||
block_test(full_block, full_block, full_block, full_block, scale=2)
|
||
block_test(full_block, empty_block, empty_block, empty_block, scale=2, subscale_n=1, subscale_d=2)
|
||
block_test(empty_block, full_block, empty_block, empty_block, scale=2, subscale_n=1, subscale_d=2, horizontal_align=1)
|
||
block_test(full_block, full_block, empty_block, empty_block, scale=2, subscale_n=1, subscale_d=2, text='██')
|
||
block_test(empty_block, empty_block, full_block, empty_block, scale=2, subscale_n=1, subscale_d=2, vertical_align=1)
|
||
block_test(quarter_block, scale=1, subscale_n=1, subscale_d=2)
|
||
block_test(upper_half_block, scale=1, subscale_n=1, subscale_d=2, text='██')
|
||
block_test(lower_half_block, scale=1, subscale_n=1, subscale_d=2, text='██', vertical_align=1)
|
||
|
||
def test_font_rendering(self):
|
||
render_string('ab\u0347\u0305你好|\U0001f601|\U0001f64f|\U0001f63a|')
|
||
text = 'He\u0347\u0305llo\u0341, w\u0302or\u0306l\u0354d!'
|
||
# macOS has no fonts capable of rendering combining chars
|
||
if is_macos:
|
||
text = text.encode('ascii', 'ignore').decode('ascii')
|
||
cells = render_string(text)[-1]
|
||
self.ae(len(cells), len(text.encode('ascii', 'ignore')))
|
||
text = '你好,世界'
|
||
sz = sum(map(lambda x: wcwidth(ord(x)), text))
|
||
cells = render_string(text)[-1]
|
||
self.ae(len(cells), sz)
|
||
|
||
@unittest.skipIf(is_macos, 'COLRv1 is only supported on Linux')
|
||
def test_rendering_colrv1(self):
|
||
f = create_face(self.path_for_font('twemoji_smiley-cff2_colr_1.otf'))
|
||
f.set_size(64, 96, 96)
|
||
for char in '😁😇😈':
|
||
_, w, h = f.render_codepoint(ord(char))
|
||
self.assertGreater(w, 64)
|
||
self.assertGreater(h, 64)
|
||
|
||
@unittest.skipIf(is_macos, 'fontconfig descriptors are not used on macOS')
|
||
def test_shaping_uses_variation_axes(self):
|
||
# Annotation Mono switches to cursive glyphs via GSUB FeatureVariations
|
||
# conditioned on its CRSV axis, so shaping must use the variation
|
||
# coordinates set on the face, see https://github.com/kovidgoyal/kitty/issues/10897
|
||
path = self.path_for_font('AnnotationMono-VF-subset.ttf')
|
||
plain, cursive = [1, 3, 6, 8], [2, 4, 7, 9] # a f k l and a.ital f.ital k.ital l.ital
|
||
|
||
def shaped_glyphs(**extra):
|
||
descriptor = {'descriptor_type': 'fontconfig', 'path': path, 'index': 0, 'hinting': True, 'hint_style': 3, **extra}
|
||
with setup_for_testing(size=self.font_size, dpi=self.dpi, main_face_descriptor=descriptor):
|
||
s = Screen(None, 1, 8)
|
||
s.draw('afkl')
|
||
return [g for group in test_shape(s.line(0), None) for g in group[3]]
|
||
|
||
# axes are in the order: wght, slnt, CRSV
|
||
self.ae(shaped_glyphs(axes=(250.0, 0.0, 0.0)), plain)
|
||
self.ae(shaped_glyphs(axes=(250.0, 0.0, 1.0)), cursive)
|
||
self.ae(shaped_glyphs(axes=(250.0, -15.0, 0.5)), cursive)
|
||
self.ae(shaped_glyphs(named_style=5), plain) # DemiBold
|
||
self.ae(shaped_glyphs(named_style=35), cursive) # DemiBold Upright Italic
|
||
|
||
def test_color_emoji_not_shrunk(self):
|
||
# Regression test for https://github.com/kovidgoyal/kitty/issues/10144.
|
||
# fontconfig gives fixed-size color faces (e.g. Noto Color Emoji) a
|
||
# pixel-size fixup encoded as FC_MATRIX. That scale must not reach the
|
||
# cairo font matrix used for color glyphs; applying it shrinks color emoji
|
||
# to a dot (ee937bdd1b). Render the same font two ways at the same size:
|
||
# from its fontconfig descriptor, which carries the fixup matrix, and from
|
||
# its file path, which does not. A correct build renders both at the same
|
||
# size; the bug shrinks the descriptor one. Comparing the two is
|
||
# environment-independent since only the matrix differs.
|
||
if is_macos:
|
||
self.skipTest('FC_MATRIX is a fontconfig feature, not used on macOS')
|
||
from kitty.fonts.fontconfig import fc_match
|
||
|
||
desc = dict(fc_match('emoji', False, False, 0))
|
||
if not (desc.get('color') and desc.get('matrix')):
|
||
self.skipTest('no fixed-size color emoji font with a fontconfig fixup matrix')
|
||
with_matrix = face_from_descriptor(desc)
|
||
with_matrix.set_size(64, 96, 96)
|
||
without_matrix = create_face(desc['path'])
|
||
without_matrix.set_size(64, 96, 96)
|
||
_, mw, mh = with_matrix.render_codepoint(0x1F40D)
|
||
_, rw, rh = without_matrix.render_codepoint(0x1F40D)
|
||
self.assertGreater(mh, 0.5 * rh, f'color emoji shrunk by FC_MATRIX: {mh}px vs {rh}px (#10144)')
|
||
|
||
def test_concat_cells(self):
|
||
from kitty.fast_data_types import concat_cells
|
||
|
||
# sprite pixels are 0xRRGGBBAA and the result is opaque RGBA bytes
|
||
cell = array.array('I', (0xFF000080, 0xFFFFFF00, 0x00FF00FF)).tobytes()
|
||
self.ae(concat_cells(3, 1, True, (cell,)), bytes((0x80, 0, 0, 0xFF, 0, 0, 0, 0xFF, 0, 0xFF, 0, 0xFF)))
|
||
self.ae(concat_cells(1, 1, True, (cell[:4], cell[4:8])), bytes((0x80, 0, 0, 0xFF, 0, 0, 0, 0xFF)))
|
||
self.ae(concat_cells(2, 1, True, (cell[:8],), 0x0000FF), bytes((0x80, 0, 0x7F, 0xFF, 0, 0, 0xFF, 0xFF)))
|
||
# alpha mask pixels are white text and the result is RGBA bytes, as above
|
||
self.ae(concat_cells(2, 1, False, (bytes((0xFF, 0)),), 0x0000FF), bytes((0xFF, 0xFF, 0xFF, 0xFF, 0, 0, 0xFF, 0xFF)))
|
||
self.ae(concat_cells(1, 1, False, (bytes((0x80,)),), 0x102030), bytes((0x87, 0x8F, 0x97, 0xFF)))
|
||
self.ae(concat_cells(0, 1, True, (b'',)), b'')
|
||
|
||
def cell_alpha(self, font_name, text, ppem):
|
||
"The alpha of the pixels of the first cell of text, and the cell width and height"
|
||
with setup_for_testing(size=ppem, dpi=72.0, main_face_path=self.path_for_font(font_name)) as (sprites, cell_width, cell_height):
|
||
s = Screen(None, 1, 4)
|
||
s.draw(text)
|
||
line = s.line(0)
|
||
test_render_line(line)
|
||
pos = sprite_idx_to_pos(line.sprite_at(0) & 0x7FFFFFFF, setup_for_testing.xnum, setup_for_testing.ynum)
|
||
# the low byte of each pixel is its alpha, the color bits are set even for empty pixels on macOS
|
||
return bytes(p & 0xFF for p in array.array('I', sprites[pos])), cell_width, cell_height
|
||
|
||
def glyph_ink(self, font_name, char, ppem):
|
||
"The number of pixel rows and columns char occupies when drawn in a single cell, and the cell width"
|
||
alpha, cell_width, cell_height = self.cell_alpha(font_name, char, ppem)
|
||
rows = [y for y in range(cell_height) if any(alpha[y * cell_width : (y + 1) * cell_width])]
|
||
cols = [x for x in range(cell_width) if any(alpha[x::cell_width])]
|
||
height = rows[-1] - rows[0] + 1 if rows else 0
|
||
width = cols[-1] - cols[0] + 1 if cols else 0
|
||
return height, width, cell_width
|
||
|
||
def test_small_glyph_overhang_is_not_rescaled(self):
|
||
# Fonts have glyphs whose outlines overhang their advance a little, such
|
||
# as the texture healing alternates of Monaspace and 0xProto. These must
|
||
# be drawn at full size, not shrunk to fit the cell, and cropped to
|
||
# it, which they then fill. The expected height is the height of the
|
||
# glyph's bounding box in font units, times ppem / units per em,
|
||
# rounded down. Cropping removes ink, so it is only done for glyphs
|
||
# at most 2/10 wider than the cell.
|
||
for font_name, char, ppem, full_height in (
|
||
('CascadiaCode-Regular.otf', 'ď', 40, 31), # 1588 * 40 / 2048, outline is 7% wider than the cell
|
||
('LiberationMono-Regular.ttf', 'Ƴ', 32, 21), # 1370 * 32 / 2048, 10% wider
|
||
('FiraCode-Medium.otf', 'œ', 64, 36), # 1111 * 64 / 1950, 3% wider
|
||
# 935 * 120 / 1000, 15% wider, like the widest texture healing alternates of Monaspace
|
||
('iosevka-regular.ttf', 'ꭦ', 120, 112),
|
||
):
|
||
with self.subTest(font=font_name, char=char):
|
||
height, width, cell_width = self.glyph_ink(font_name, char, ppem)
|
||
self.assertGreaterEqual(height, full_height)
|
||
self.assertEqual(width, cell_width)
|
||
|
||
def test_large_glyph_overhang_is_rescaled(self):
|
||
# Ὦ has its breathing mark and accent to the left of the letter, outside
|
||
# its cell, so cropping would cut them off. Its outline is 1594 units
|
||
# wide and 1766 tall with an advance of 1229, so scaled to fit the cell
|
||
# it is at most 1766 * (1229 / 1594) * 32 / 2048 = 21.3 pixels tall,
|
||
# which, depending on where it falls relative to the pixel grid, covers
|
||
# at most 23 rows. Drawn at full size it covers at least 27.
|
||
height, width, cell_width = self.glyph_ink('LiberationMono-Regular.ttf', 'Ὦ', 32)
|
||
self.assertLessEqual(height, 23)
|
||
# The rescaled glyph is moved into the cell so that the marks are
|
||
# not cut off and it spans the whole cell.
|
||
self.assertGreaterEqual(width, cell_width - 1)
|
||
|
||
def test_marks_move_with_rescaled_glyph(self):
|
||
# When Ὦ is rescaled and moved into its cell, a combining dot below it
|
||
# must move with it, so that it stays centered under the letter
|
||
font = 'LiberationMono-Regular.ttf'
|
||
base, cell_width, cell_height = self.cell_alpha(font, 'Ὦ', 32)
|
||
combined = self.cell_alpha(font, 'Ὦ\u0323', 32)[0]
|
||
|
||
def ink_columns(alpha, rows):
|
||
return [x for x in range(cell_width) if any(alpha[y * cell_width + x] for y in rows)]
|
||
|
||
def center(cols):
|
||
return (cols[0] + cols[-1]) / 2
|
||
|
||
rows_with_ink = [y for y in range(cell_height) if any(base[y * cell_width : (y + 1) * cell_width])]
|
||
# the bottom row of Ω spans its width
|
||
letter = ink_columns(base, rows_with_ink[-1:])
|
||
dot = ink_columns(combined, range(rows_with_ink[-1] + 1, cell_height))
|
||
self.assertTrue(dot)
|
||
self.assertLessEqual(abs(center(dot) - center(letter)), cell_width / 8, f'dot: {dot} letter: {letter}')
|
||
|
||
def test_shaping(self):
|
||
|
||
def ss(text, font=None):
|
||
path = self.path_for_font(font) if font else None
|
||
return shape_string(text, path=path)
|
||
|
||
def groups(text, font=None):
|
||
return [x[:2] for x in ss(text, font)]
|
||
|
||
for font in ('FiraCode-Medium.otf', 'CascadiaCode-Regular.otf', 'iosevka-regular.ttf'):
|
||
g = partial(groups, font=font)
|
||
self.ae(g('abcd'), [(1, 1) for i in range(4)])
|
||
self.ae(g('A===B!=C'), [(1, 1), (3, 3), (1, 1), (2, 2), (1, 1)])
|
||
self.ae(g('A=>>B!=C'), [(1, 1), (3, 3), (1, 1), (2, 2), (1, 1)])
|
||
self.ae(g('->'), [(2, 2)])
|
||
self.ae(g('<-'), [(2, 2)])
|
||
self.ae(g('==>'), [(3, 3)])
|
||
self.ae(g('<=='), [(3, 3)])
|
||
self.ae(g('a->b'), [(1, 1), (2, 2), (1, 1)])
|
||
self.ae(g('a<-b'), [(1, 1), (2, 2), (1, 1)])
|
||
self.ae(g('a==>b'), [(1, 1), (3, 3), (1, 1)])
|
||
self.ae(g('a<==b'), [(1, 1), (3, 3), (1, 1)])
|
||
if 'iosevka' in font:
|
||
self.ae(g('--->'), [(4, 4)])
|
||
self.ae(g('-' * 12 + '>'), [(13, 13)])
|
||
self.ae(g('<~~~'), [(4, 4)])
|
||
self.ae(g('a<~~~b'), [(1, 1), (4, 4), (1, 1)])
|
||
else:
|
||
self.ae(g('----'), [(4, 4)])
|
||
self.ae(g('F--a--'), [(1, 1), (2, 2), (1, 1), (2, 2)])
|
||
self.ae(g('===--<>=='), [(3, 3), (2, 2), (2, 2), (2, 2)])
|
||
self.ae(g('==!=<>==<><><>'), [(4, 4), (2, 2), (2, 2), (2, 2), (2, 2), (2, 2)])
|
||
self.ae(g('-' * 18), [(18, 18)])
|
||
self.ae(g('<==>'), [(4, 4)])
|
||
self.ae(g('<!--'), [(4, 4)])
|
||
self.ae(g('a<==>b'), [(1, 1), (4, 4), (1, 1)])
|
||
self.ae(g('a<!--b'), [(1, 1), (4, 4), (1, 1)])
|
||
self.ae(g('a>\u2060<b'), [(1, 1), (1, 2), (1, 1), (1, 1)])
|
||
comfy = partial(groups, font='ComfyCode-Regular.ttf')
|
||
comfy_cases = {
|
||
'->': ('a->b', 2),
|
||
'<-': ('a<-b', 2),
|
||
'==>': ('a==>b', 3),
|
||
'<==': ('a<==b', 3),
|
||
}
|
||
for text, (wrapped, ligature_width) in comfy_cases.items():
|
||
baseline = comfy(text)
|
||
self.assertIn(baseline, ([(ligature_width, ligature_width)], [(1, 1) for i in range(ligature_width)]))
|
||
if baseline == [(ligature_width, ligature_width)]:
|
||
self.ae(comfy(wrapped), [(1, 1), (ligature_width, ligature_width), (1, 1)])
|
||
else:
|
||
self.ae(comfy(wrapped), [(1, 1) for i in range(len(wrapped))])
|
||
colon_glyph = ss('9:30', font='FiraCode-Medium.otf')[1][2]
|
||
self.assertNotEqual(colon_glyph, ss(':', font='FiraCode-Medium.otf')[0][2])
|
||
self.ae(colon_glyph, 1031)
|
||
self.ae(groups('9:30', font='FiraCode-Medium.otf'), [(1, 1), (1, 1), (1, 1), (1, 1)])
|
||
self.ae(groups('#_(', font='FiraCode-Medium.otf'), [(3, 3)])
|
||
self.ae(groups('a#_(b', font='FiraCode-Medium.otf'), [(1, 1), (3, 3), (1, 1)])
|
||
self.ae(groups('<*>>', font='FiraCode-Medium.otf'), [(3, 3), (1, 1)])
|
||
self.ae(groups('a<*>>b', font='FiraCode-Medium.otf'), [(1, 1), (3, 3), (1, 1), (1, 1)])
|
||
|
||
self.ae(groups('|\U0001f601|\U0001f64f|\U0001f63a|'), [(1, 1), (2, 1), (1, 1), (2, 1), (1, 1), (2, 1), (1, 1)])
|
||
self.ae(groups('He\u0347\u0305llo\u0337,', font='LiberationMono-Regular.ttf'), [(1, 1), (1, 3), (1, 1), (1, 1), (1, 2), (1, 1)])
|
||
|
||
self.ae(groups('i\u0332\u0308', font='LiberationMono-Regular.ttf'), [(1, 2)])
|
||
self.ae(groups('u\u0332 u\u0332\u0301', font='LiberationMono-Regular.ttf'), [(1, 2), (1, 1), (1, 2)])
|
||
|
||
def test_rtl_shaping(self):
|
||
# HarfBuzz shapes runs containing Hebrew as backward (RTL) buffers and
|
||
# returns their glyphs in visual order, that is, with the glyphs of the
|
||
# last cell first. Each group must still cover exactly the cells its
|
||
# glyphs come from.
|
||
lm = 'LiberationMono-Regular.ttf'
|
||
|
||
def shape(draw, font):
|
||
with setup_for_testing(size=self.font_size, dpi=self.dpi):
|
||
s = Screen(None, 4, 64)
|
||
draw(s)
|
||
return test_shape(s.line(0), self.path_for_font(font))
|
||
|
||
def ss(text, font=lm):
|
||
return shape(lambda s: s.draw(text), font)
|
||
|
||
def groups(text, font=lm):
|
||
return [g[:2] for g in ss(text, font)]
|
||
|
||
def check_units(units, font=lm, rtl=True):
|
||
# every unit is a single cell whose glyphs, shaped on its own,
|
||
# must be the glyphs of its group
|
||
text = ''.join(units)
|
||
shaped = ss(text, font)
|
||
self.ae([g[0] for g in shaped], [1] * len(units), f'cells per group for: {text!r}')
|
||
expected = [ss(u, font)[0][3] for u in (reversed(units) if rtl else units)]
|
||
self.ae([g[3] for g in shaped], expected, f'glyphs per group for: {text!r}')
|
||
|
||
hebrew_units = (
|
||
('\u05e9', '\u05dc', '\u05d5', '\u05dd'), # unpointed
|
||
# pointed, the shin and vav cells become a single composed glyph
|
||
('\u05e9\u05b8\u05c1', '\u05dc', '\u05d5\u05b9', '\u05dd'),
|
||
('\u05d1\u05bc', '\u05d2'), # bet with dagesh composes into a single glyph
|
||
('\u05ea\u05bc\u05c2', '\u05db', '\u05e1\u05b0\u05b7'), # several marks per cell
|
||
)
|
||
for units in hebrew_units:
|
||
check_units(units)
|
||
# LTR text inside an RTL buffer
|
||
check_units(('\u05e9\u05b8\u05c1', '\u05dc', '\u05d5\u05b9', '\u05dd', ' ', 'a', 'b', '1'))
|
||
# the marks of a Latin letter come before it in an RTL buffer
|
||
self.ae(groups('\u05e9\u05b8\u05c1a\u0332'), [(1, 2), (1, 2)])
|
||
# mirrored brackets
|
||
self.ae(groups('(\u05e9\u05dc\u05d5\u05dd)'), [(1, 1)] * 6)
|
||
# the cells of a run that starts with an LTR letter are shaped as an
|
||
# LTR buffer, in logical order
|
||
check_units(('a', '\u05e9', '\u05dc'), rtl=False)
|
||
# forcing LTR gives logical order and the same per cell groups
|
||
with setup_for_testing(size=self.font_size, dpi=self.dpi):
|
||
self.set_options({'force_ltr': True})
|
||
s = Screen(None, 1, 64)
|
||
s.draw('\u05e9\u05b8\u05c1\u05dc\u05d5\u05b9\u05dd')
|
||
self.ae([g[:2] for g in test_shape(s.line(0), self.path_for_font(lm))], [(1, 2), (1, 1), (1, 1), (1, 1)])
|
||
self.set_options()
|
||
|
||
# cells wider than one column
|
||
self.ae(groups('\u05e9\u597d\u05dc'), [(1, 1), (2, 1), (1, 1)])
|
||
self.ae(groups('\u05e9\u2716\ufe0f\u05dc'), [(1, 1), (2, 2), (1, 1)])
|
||
for kw in ({'scale': 2}, {'width': 2}):
|
||
|
||
def draw(s, kw=kw):
|
||
s.draw('\u05e9')
|
||
draw_multicell(s, '\u05dc', **kw)
|
||
s.draw('\u05d5\u05dd')
|
||
|
||
shaped = shape(draw, lm)
|
||
self.ae([g[:2] for g in shaped], [(1, 1), (1, 1), (2, 1), (1, 1)], f'multicell: {kw}')
|
||
self.ae([g[3] for g in shaped], [ss(u)[0][3] for u in '\u05dd\u05d5\u05dc\u05e9'], f'multicell: {kw}')
|
||
|
||
def draw(s):
|
||
s.draw('\u05e9\u05b8\u05c1')
|
||
draw_multicell(s, '\u05d5\u05b9', scale=2)
|
||
s.draw('\u05dd')
|
||
|
||
self.ae([g[:2] for g in shape(draw, lm)], [(1, 1), (2, 1), (1, 2)])
|
||
|
||
# Arabic lam-alef is a single glyph covering two cells
|
||
dv = 'DejaVuSansMono.ttf'
|
||
lam_alef = ss('لا', dv)
|
||
self.ae([g[:2] for g in lam_alef], [(2, 1)])
|
||
self.ae(groups('سلام', dv), [(1, 1), (2, 1), (1, 1)])
|
||
self.ae(groups('السلام', dv), [(1, 1), (2, 1), (1, 1), (1, 1), (1, 1)])
|
||
# a mark on the lam, the ligature still spans both cells
|
||
self.ae(groups('لَا', dv), [(2, 2)])
|
||
# LTR text and wide cells next to the ligature
|
||
self.ae(groups('سلام abc', dv), [(1, 1)] * 4 + [(1, 1), (2, 1), (1, 1)])
|
||
self.ae(groups('لا好', dv), [(2, 1), (2, 1)])
|
||
|
||
# iosevka has no Hebrew so marks are not composed with their letters and
|
||
# come before them, its grouping must still give each letter its marks
|
||
self.ae(groups('\u05e9\u05b8\u05c1\u05dc\u05d5\u05b9\u05dd', 'iosevka-regular.ttf'), [(1, 1), (1, 2), (1, 1), (1, 3)])
|
||
|
||
# ligature fonts substitute glyphs designed for LTR in Hebrew runs, so
|
||
# only check that the cells are accounted for
|
||
for font in ('FiraCode-Medium.otf', 'CascadiaCode-Regular.otf', 'iosevka-regular.ttf'):
|
||
for text in ('\u05d0==\u05d1', '\u05d0===\u05d1', '\u05d0->\u05d1', '\u05d0<!--\u05d1', '\u05d0----\u05d1', '\u05e9==', '==\u05e9'):
|
||
g = groups(text, font)
|
||
self.assertTrue(all(n > 0 for n, _ in g), f'{font}: {text!r}: {g}')
|
||
self.ae(sum(n for n, _ in g), len(text), f'{font}: {text!r}: {g}')
|
||
self.ae(sum(n for _, n in g), len(text), f'{font}: {text!r}: {g}')
|
||
|
||
def test_shaped_run_cache(self):
|
||
# test_shape() already checks that a cached run matches the HarfBuzz
|
||
# output for the same run, here we check that different runs sharing a
|
||
# font, and therefore a cache, do not collide with each other
|
||
ctx = partial(setup_for_testing, size=self.font_size, dpi=self.dpi, main_face_path=self.path_for_font(self.font_name))
|
||
|
||
def shape(text):
|
||
s = Screen(None, 1, max(8, len(text) * 2))
|
||
s.draw(text)
|
||
return test_shape(s.line(0), None)
|
||
|
||
texts = (
|
||
'abcd',
|
||
'a===b',
|
||
'->',
|
||
'<==>',
|
||
'-' * 18,
|
||
'x' * 32,
|
||
'x' * 33,
|
||
'a<!--b',
|
||
'\u00e1\u00e9',
|
||
'a>\u2060<b',
|
||
'\u05e9\u05b8\u05c1\u05dc\u05d5\u05b9\u05dd',
|
||
'\u05e9\u597d\u05dc',
|
||
'\u05d0==\u05d1',
|
||
)
|
||
cold = {}
|
||
for text in texts:
|
||
with ctx(): # a fresh font, and therefore an empty cache, for every run
|
||
cold[text] = shape(text)
|
||
with ctx(): # all runs now share a single cache
|
||
for text in texts + tuple(reversed(texts)):
|
||
self.ae(cold[text], shape(text), f'cached shaping differs for: {text!r}')
|
||
|
||
def test_shaping_with_many_combining_chars(self):
|
||
# cells can hold more codepoints than MAX_NUM_CODEPOINTS_PER_CELL when
|
||
# built via the Python API, which must not overflow the shaped run cache key
|
||
s = Screen(None, 1, 64)
|
||
s.draw('a' * 32)
|
||
line = s.line(0)
|
||
for x in range(32):
|
||
for i in range(40):
|
||
line.add_combining_char(x, chr(0x300 + (i % 16)))
|
||
self.ae(sum(g[0] for g in test_shape(line, None)), 32)
|
||
|
||
def test_emoji_presentation(self):
|
||
s = self.create_screen()
|
||
s.draw('\u2716\u2716\ufe0f')
|
||
self.ae((s.cursor.x, s.cursor.y), (3, 0))
|
||
s.draw('\u2716\u2716')
|
||
self.ae((s.cursor.x, s.cursor.y), (5, 0))
|
||
s.draw('\ufe0f')
|
||
self.ae((s.cursor.x, s.cursor.y), (2, 1))
|
||
self.ae(str(s.line(0)), '\u2716\u2716\ufe0f\u2716')
|
||
self.ae(str(s.line(1)), '\u2716\ufe0f')
|
||
s.draw('\u2716' * 3)
|
||
self.ae((s.cursor.x, s.cursor.y), (5, 1))
|
||
self.ae(str(s.line(1)), '\u2716\ufe0f\u2716\u2716\u2716')
|
||
self.ae((s.cursor.x, s.cursor.y), (5, 1))
|
||
s.reset_mode(DECAWM)
|
||
s.draw('\ufe0f')
|
||
s.set_mode(DECAWM)
|
||
self.ae((s.cursor.x, s.cursor.y), (5, 1))
|
||
self.ae(str(s.line(1)), '\u2716\ufe0f\u2716\u2716\ufe0f')
|
||
s.cursor.y = s.lines - 1
|
||
s.draw('\u2716' * s.columns)
|
||
self.ae((s.cursor.x, s.cursor.y), (5, 4))
|
||
s.draw('\ufe0f')
|
||
self.ae((s.cursor.x, s.cursor.y), (2, 4))
|
||
self.ae(str(s.line(s.cursor.y)), '\u2716\ufe0f')
|
||
|
||
def test_get_fallback_font_returns_new_reference(self):
|
||
# get_fallback_font() used to return a borrowed reference, which freed
|
||
# the Face while the FontGroup was still holding on to it.
|
||
for text in '\u4e2d\u0627\u05d0\u0905\u0e01\u10a0':
|
||
try:
|
||
face = get_fallback_font(text, False, False)
|
||
except ValueError:
|
||
continue
|
||
# one reference held by the FontGroup, one by face, one by the
|
||
# argument to getrefcount()
|
||
self.assertGreaterEqual(sys.getrefcount(face), 3, f'get_fallback_font() returned a borrowed reference for {text!r}')
|
||
del face
|
||
# the Face must still be usable via the FontGroup
|
||
self.assertIsInstance(get_fallback_font(text, False, False).postscript_name(), str)
|
||
break
|
||
else:
|
||
self.skipTest('No font on this system has a fallback font for any of the tested codepoints')
|
||
|
||
def test_private_use_fallback_font_ignores_style(self):
|
||
# private use codepoints such as Nerd Font icons must use the same
|
||
# fallback font in every style, rather than whatever unrelated bold or
|
||
# italic font happens to cover the codepoint
|
||
for text in '\uf00c\uf071\uf110\ue654':
|
||
try:
|
||
regular = get_fallback_font(text, False, False)
|
||
except ValueError:
|
||
continue
|
||
for bold, italic in ((True, False), (False, True), (True, True)):
|
||
self.ae(get_fallback_font(text, bold, italic).postscript_name(), regular.postscript_name(), f'{text!r} {bold=} {italic=}')
|
||
|
||
@unittest.skipIf(is_macos, 'fontconfig is not used on macOS')
|
||
def test_fallback_font_matching(self):
|
||
# Fallback font matching narrows the candidates down using a cached,
|
||
# pre-sorted candidate list before asking fontconfig to score them,
|
||
# instead of scoring the entire font database for every codepoint. See
|
||
# https://github.com/kovidgoyal/kitty/issues/10496
|
||
from kitty.fast_data_types import Face, clear_fallback_font_cache, fc_match_fallback
|
||
|
||
clear_fallback_font_cache()
|
||
faces = {}
|
||
|
||
def has_chars(descriptor, text):
|
||
key = descriptor['path'], descriptor['index']
|
||
if key not in faces:
|
||
faces[key] = Face(descriptor=descriptor)
|
||
face = faces[key]
|
||
return tuple(face.has_codepoint(ord(ch)) for ch in text)
|
||
|
||
def cells_to_test():
|
||
# a spread over every plane, plus the ranges most likely to need a
|
||
# fallback font: Private Use Area icons, Nerd Font icons, CJK,
|
||
# emoji, math and Braille
|
||
for cp in range(0, 0x110000, 0x1553):
|
||
yield chr(cp)
|
||
for start in (0xE000, 0xF0000, 0x4E00, 0x1F600, 0x2200, 0x2800):
|
||
for cp in range(start, start + 0x100, 0x11):
|
||
yield chr(cp)
|
||
# multi-codepoint cells, where the best font is the one missing the
|
||
# fewest codepoints
|
||
for cp in (0x53, 0x4E2D, 0x905, 0xE0B0, 0x1F600):
|
||
yield chr(cp) + '\u0301'
|
||
yield chr(cp) + '\u20e3'
|
||
|
||
num_tested = num_changed = 0
|
||
# prefer_color matters most, as fontconfig ranks FC_COLOR above coverage
|
||
for bold, italic, prefer_color in ((False, False, False), (True, True, False), (False, False, True), (True, False, True)):
|
||
kw = {'bold': bold, 'italic': italic, 'prefer_color': prefer_color}
|
||
for text in cells_to_test():
|
||
if 0xD800 <= ord(text[0]) <= 0xDFFF:
|
||
continue
|
||
expected = fc_match_fallback(text, use_candidate_cache=False, **kw)
|
||
actual = fc_match_fallback(text, use_candidate_cache=True, **kw)
|
||
num_tested += 1
|
||
if (expected['path'], expected['index']) == (actual['path'], actual['index']):
|
||
continue
|
||
# fontconfig sorts with an unstable sort, so among fonts it
|
||
# scores identically a different one can be picked. That is
|
||
# acceptable only as long as glyph coverage is unchanged.
|
||
num_changed += 1
|
||
self.ae(
|
||
has_chars(actual, text),
|
||
has_chars(expected, text),
|
||
f'Fallback font for {text!r} with {kw} has different coverage: {actual["path"]} instead of {expected["path"]}',
|
||
)
|
||
self.assertGreater(num_tested, 100)
|
||
# How often fonts fontconfig scores identically are ordered differently
|
||
# depends on the installed fonts, so this bound is deliberately loose:
|
||
# the meaningful check is the glyph coverage assertion above. Observed
|
||
# rates: 0% with a handful of fonts, 0.5% with 1600 families installed.
|
||
self.assertLess(num_changed, num_tested // 2, f'{num_changed} of {num_tested} fallback fonts changed')
|
||
|
||
@unittest.skipUnless(is_macos, 'Only macOS has a Last Resort font')
|
||
def test_fallback_font_not_last_resort(self):
|
||
# Ensure that the LastResort font is not reported as a fallback font on
|
||
# macOS. See https://github.com/kovidgoyal/kitty/issues/799
|
||
with self.assertRaises(ValueError, msg='No fallback font found'):
|
||
get_fallback_font('\U0010ffff', False, False)
|
||
|
||
def test_coalesce_symbol_maps(self):
|
||
q = {(2, 3): 'a', (4, 6): 'b', (5, 5): 'b', (7, 7): 'b', (9, 9): 'b', (1, 1): 'a'}
|
||
self.ae(coalesce_symbol_maps(q), {(1, 3): 'a', (4, 7): 'b', (9, 9): 'b'})
|
||
q = {(1, 4): 'a', (2, 3): 'b'}
|
||
self.ae(coalesce_symbol_maps(q), {(1, 1): 'a', (2, 3): 'b', (4, 4): 'a'})
|
||
q = {(2, 3): 'b', (1, 4): 'a'}
|
||
self.ae(coalesce_symbol_maps(q), {(1, 4): 'a'})
|
||
q = {(1, 4): 'a', (2, 5): 'b'}
|
||
self.ae(coalesce_symbol_maps(q), {(1, 1): 'a', (2, 5): 'b'})
|
||
q = {(2, 5): 'b', (1, 4): 'a'}
|
||
self.ae(coalesce_symbol_maps(q), {(1, 4): 'a', (5, 5): 'b'})
|
||
q = {(1, 4): 'a', (2, 5): 'a'}
|
||
self.ae(coalesce_symbol_maps(q), {(1, 5): 'a'})
|
||
q = {(1, 4): 'a', (4, 5): 'b'}
|
||
self.ae(coalesce_symbol_maps(q), {(1, 3): 'a', (4, 5): 'b'})
|
||
q = {(4, 5): 'b', (1, 4): 'a'}
|
||
self.ae(coalesce_symbol_maps(q), {(1, 4): 'a', (5, 5): 'b'})
|
||
q = {(0, 30): 'a', (10, 10): 'b', (11, 11): 'b', (2, 2): 'c', (1, 1): 'c'}
|
||
self.ae(coalesce_symbol_maps(q), {(0, 0): 'a', (1, 2): 'c', (3, 9): 'a', (10, 11): 'b', (12, 30): 'a'})
|
||
|
||
|
||
def test_chars(chars: str = '╌', sz: int = 128) -> None:
|
||
# kitty +runpy "from kitty.fonts.box_drawing import test_chars; test_chars('XXX')"
|
||
from kitty.fast_data_types import concat_cells, render_box_char, set_send_sprite_to_gpu
|
||
from kitty.fonts.render import display_bitmap, setup_for_testing
|
||
|
||
if not chars:
|
||
import sys
|
||
|
||
chars = sys.argv[-1]
|
||
|
||
def as_ord(x: str) -> int:
|
||
if x.lower().startswith('u+'):
|
||
return int(x[2:], 16)
|
||
return ord(x)
|
||
|
||
if '...' in chars:
|
||
start, end = chars.partition('...')[::2]
|
||
chars = ''.join(map(chr, range(as_ord(start), as_ord(end) + 1)))
|
||
|
||
with setup_for_testing('monospace', sz) as (_, width, height):
|
||
try:
|
||
for ch in chars:
|
||
nb = render_box_char(as_ord(ch), width, height)
|
||
rgb_data = concat_cells(width, height, False, (nb,))
|
||
display_bitmap(rgb_data, width, height)
|
||
print()
|
||
finally:
|
||
set_send_sprite_to_gpu(None)
|
||
|
||
|
||
def test_drawing(sz: int = 48, family: str = 'monospace', start: int = 0x2500, num_rows: int = 10, num_cols: int = 16) -> None:
|
||
from kitty.fast_data_types import concat_cells, render_box_char, set_send_sprite_to_gpu
|
||
|
||
from .render import display_bitmap, setup_for_testing
|
||
|
||
with setup_for_testing(family, sz) as (_, width, height):
|
||
space = bytearray(width * height)
|
||
|
||
def join_cells(cells: Iterable[bytes]) -> bytes:
|
||
cells = tuple(bytes(x) for x in cells)
|
||
return concat_cells(width, height, False, cells)
|
||
|
||
def render_chr(ch: str) -> bytearray:
|
||
if ch in box_chars:
|
||
return bytearray(render_box_char(ord(ch), width, height))
|
||
return space
|
||
|
||
pos = start
|
||
rows = []
|
||
space_row = join_cells(repeat(space, 32))
|
||
|
||
try:
|
||
for r in range(num_rows):
|
||
row = []
|
||
for i in range(num_cols):
|
||
row.append(render_chr(chr(pos)))
|
||
row.append(space)
|
||
pos += 1
|
||
rows.append(join_cells(row))
|
||
rows.append(space_row)
|
||
rgb_data = b''.join(rows)
|
||
width *= 32
|
||
height *= len(rows)
|
||
assert len(rgb_data) == width * height * 4, f'{len(rgb_data)} != {width * height * 4}'
|
||
display_bitmap(rgb_data, width, height)
|
||
finally:
|
||
set_send_sprite_to_gpu(None)
|
||
|
||
|
||
box_chars = { # {{{
|
||
'─',
|
||
'━',
|
||
'│',
|
||
'┃',
|
||
'┄',
|
||
'┅',
|
||
'┆',
|
||
'┇',
|
||
'┈',
|
||
'┉',
|
||
'┊',
|
||
'┋',
|
||
'┌',
|
||
'┍',
|
||
'┎',
|
||
'┏',
|
||
'┐',
|
||
'┑',
|
||
'┒',
|
||
'┓',
|
||
'└',
|
||
'┕',
|
||
'┖',
|
||
'┗',
|
||
'┘',
|
||
'┙',
|
||
'┚',
|
||
'┛',
|
||
'├',
|
||
'┝',
|
||
'┞',
|
||
'┟',
|
||
'┠',
|
||
'┡',
|
||
'┢',
|
||
'┣',
|
||
'┤',
|
||
'┥',
|
||
'┦',
|
||
'┧',
|
||
'┨',
|
||
'┩',
|
||
'┪',
|
||
'┫',
|
||
'┬',
|
||
'┭',
|
||
'┮',
|
||
'┯',
|
||
'┰',
|
||
'┱',
|
||
'┲',
|
||
'┳',
|
||
'┴',
|
||
'┵',
|
||
'┶',
|
||
'┷',
|
||
'┸',
|
||
'┹',
|
||
'┺',
|
||
'┻',
|
||
'┼',
|
||
'┽',
|
||
'┾',
|
||
'┿',
|
||
'╀',
|
||
'╁',
|
||
'╂',
|
||
'╃',
|
||
'╄',
|
||
'╅',
|
||
'╆',
|
||
'╇',
|
||
'╈',
|
||
'╉',
|
||
'╊',
|
||
'╋',
|
||
'╌',
|
||
'╍',
|
||
'╎',
|
||
'╏',
|
||
'═',
|
||
'║',
|
||
'╒',
|
||
'╓',
|
||
'╔',
|
||
'╕',
|
||
'╖',
|
||
'╗',
|
||
'╘',
|
||
'╙',
|
||
'╚',
|
||
'╛',
|
||
'╜',
|
||
'╝',
|
||
'╞',
|
||
'╟',
|
||
'╠',
|
||
'╡',
|
||
'╢',
|
||
'╣',
|
||
'╤',
|
||
'╥',
|
||
'╦',
|
||
'╧',
|
||
'╨',
|
||
'╩',
|
||
'╪',
|
||
'╫',
|
||
'╬',
|
||
'╭',
|
||
'╮',
|
||
'╯',
|
||
'╰',
|
||
'╱',
|
||
'╲',
|
||
'╳',
|
||
'╴',
|
||
'╵',
|
||
'╶',
|
||
'╷',
|
||
'╸',
|
||
'╹',
|
||
'╺',
|
||
'╻',
|
||
'╼',
|
||
'╽',
|
||
'╾',
|
||
'╿',
|
||
'▀',
|
||
'▁',
|
||
'▂',
|
||
'▃',
|
||
'▄',
|
||
'▅',
|
||
'▆',
|
||
'▇',
|
||
'█',
|
||
'▉',
|
||
'▊',
|
||
'▋',
|
||
'▌',
|
||
'▍',
|
||
'▎',
|
||
'▏',
|
||
'▐',
|
||
'░',
|
||
'▒',
|
||
'▓',
|
||
'▔',
|
||
'▕',
|
||
'▖',
|
||
'▗',
|
||
'▘',
|
||
'▙',
|
||
'▚',
|
||
'▛',
|
||
'▜',
|
||
'▝',
|
||
'▞',
|
||
'▟',
|
||
'◉',
|
||
'○',
|
||
'●',
|
||
'◖',
|
||
'◗',
|
||
'◜',
|
||
'◝',
|
||
'◞',
|
||
'◟',
|
||
'◠',
|
||
'◡',
|
||
'◢',
|
||
'◣',
|
||
'◤',
|
||
'◥',
|
||
'⠀',
|
||
'⠁',
|
||
'⠂',
|
||
'⠃',
|
||
'⠄',
|
||
'⠅',
|
||
'⠆',
|
||
'⠇',
|
||
'⠈',
|
||
'⠉',
|
||
'⠊',
|
||
'⠋',
|
||
'⠌',
|
||
'⠍',
|
||
'⠎',
|
||
'⠏',
|
||
'⠐',
|
||
'⠑',
|
||
'⠒',
|
||
'⠓',
|
||
'⠔',
|
||
'⠕',
|
||
'⠖',
|
||
'⠗',
|
||
'⠘',
|
||
'⠙',
|
||
'⠚',
|
||
'⠛',
|
||
'⠜',
|
||
'⠝',
|
||
'⠞',
|
||
'⠟',
|
||
'⠠',
|
||
'⠡',
|
||
'⠢',
|
||
'⠣',
|
||
'⠤',
|
||
'⠥',
|
||
'⠦',
|
||
'⠧',
|
||
'⠨',
|
||
'⠩',
|
||
'⠪',
|
||
'⠫',
|
||
'⠬',
|
||
'⠭',
|
||
'⠮',
|
||
'⠯',
|
||
'⠰',
|
||
'⠱',
|
||
'⠲',
|
||
'⠳',
|
||
'⠴',
|
||
'⠵',
|
||
'⠶',
|
||
'⠷',
|
||
'⠸',
|
||
'⠹',
|
||
'⠺',
|
||
'⠻',
|
||
'⠼',
|
||
'⠽',
|
||
'⠾',
|
||
'⠿',
|
||
'⡀',
|
||
'⡁',
|
||
'⡂',
|
||
'⡃',
|
||
'⡄',
|
||
'⡅',
|
||
'⡆',
|
||
'⡇',
|
||
'⡈',
|
||
'⡉',
|
||
'⡊',
|
||
'⡋',
|
||
'⡌',
|
||
'⡍',
|
||
'⡎',
|
||
'⡏',
|
||
'⡐',
|
||
'⡑',
|
||
'⡒',
|
||
'⡓',
|
||
'⡔',
|
||
'⡕',
|
||
'⡖',
|
||
'⡗',
|
||
'⡘',
|
||
'⡙',
|
||
'⡚',
|
||
'⡛',
|
||
'⡜',
|
||
'⡝',
|
||
'⡞',
|
||
'⡟',
|
||
'⡠',
|
||
'⡡',
|
||
'⡢',
|
||
'⡣',
|
||
'⡤',
|
||
'⡥',
|
||
'⡦',
|
||
'⡧',
|
||
'⡨',
|
||
'⡩',
|
||
'⡪',
|
||
'⡫',
|
||
'⡬',
|
||
'⡭',
|
||
'⡮',
|
||
'⡯',
|
||
'⡰',
|
||
'⡱',
|
||
'⡲',
|
||
'⡳',
|
||
'⡴',
|
||
'⡵',
|
||
'⡶',
|
||
'⡷',
|
||
'⡸',
|
||
'⡹',
|
||
'⡺',
|
||
'⡻',
|
||
'⡼',
|
||
'⡽',
|
||
'⡾',
|
||
'⡿',
|
||
'⢀',
|
||
'⢁',
|
||
'⢂',
|
||
'⢃',
|
||
'⢄',
|
||
'⢅',
|
||
'⢆',
|
||
'⢇',
|
||
'⢈',
|
||
'⢉',
|
||
'⢊',
|
||
'⢋',
|
||
'⢌',
|
||
'⢍',
|
||
'⢎',
|
||
'⢏',
|
||
'⢐',
|
||
'⢑',
|
||
'⢒',
|
||
'⢓',
|
||
'⢔',
|
||
'⢕',
|
||
'⢖',
|
||
'⢗',
|
||
'⢘',
|
||
'⢙',
|
||
'⢚',
|
||
'⢛',
|
||
'⢜',
|
||
'⢝',
|
||
'⢞',
|
||
'⢟',
|
||
'⢠',
|
||
'⢡',
|
||
'⢢',
|
||
'⢣',
|
||
'⢤',
|
||
'⢥',
|
||
'⢦',
|
||
'⢧',
|
||
'⢨',
|
||
'⢩',
|
||
'⢪',
|
||
'⢫',
|
||
'⢬',
|
||
'⢭',
|
||
'⢮',
|
||
'⢯',
|
||
'⢰',
|
||
'⢱',
|
||
'⢲',
|
||
'⢳',
|
||
'⢴',
|
||
'⢵',
|
||
'⢶',
|
||
'⢷',
|
||
'⢸',
|
||
'⢹',
|
||
'⢺',
|
||
'⢻',
|
||
'⢼',
|
||
'⢽',
|
||
'⢾',
|
||
'⢿',
|
||
'⣀',
|
||
'⣁',
|
||
'⣂',
|
||
'⣃',
|
||
'⣄',
|
||
'⣅',
|
||
'⣆',
|
||
'⣇',
|
||
'⣈',
|
||
'⣉',
|
||
'⣊',
|
||
'⣋',
|
||
'⣌',
|
||
'⣍',
|
||
'⣎',
|
||
'⣏',
|
||
'⣐',
|
||
'⣑',
|
||
'⣒',
|
||
'⣓',
|
||
'⣔',
|
||
'⣕',
|
||
'⣖',
|
||
'⣗',
|
||
'⣘',
|
||
'⣙',
|
||
'⣚',
|
||
'⣛',
|
||
'⣜',
|
||
'⣝',
|
||
'⣞',
|
||
'⣟',
|
||
'⣠',
|
||
'⣡',
|
||
'⣢',
|
||
'⣣',
|
||
'⣤',
|
||
'⣥',
|
||
'⣦',
|
||
'⣧',
|
||
'⣨',
|
||
'⣩',
|
||
'⣪',
|
||
'⣫',
|
||
'⣬',
|
||
'⣭',
|
||
'⣮',
|
||
'⣯',
|
||
'⣰',
|
||
'⣱',
|
||
'⣲',
|
||
'⣳',
|
||
'⣴',
|
||
'⣵',
|
||
'⣶',
|
||
'⣷',
|
||
'⣸',
|
||
'⣹',
|
||
'⣺',
|
||
'⣻',
|
||
'⣼',
|
||
'⣽',
|
||
'⣾',
|
||
'⣿',
|
||
'\ue0b0',
|
||
'\ue0b1',
|
||
'\ue0b2',
|
||
'\ue0b3',
|
||
'\ue0b4',
|
||
'\ue0b5',
|
||
'\ue0b6',
|
||
'\ue0b7',
|
||
'\ue0b8',
|
||
'\ue0b9',
|
||
'\ue0ba',
|
||
'\ue0bb',
|
||
'\ue0bc',
|
||
'\ue0bd',
|
||
'\ue0be',
|
||
'\ue0bf',
|
||
'\ue0d6',
|
||
'\ue0d7',
|
||
'\uee00',
|
||
'\uee01',
|
||
'\uee02',
|
||
'\uee03',
|
||
'\uee04',
|
||
'\uee05',
|
||
'\uee06',
|
||
'\uee07',
|
||
'\uee08',
|
||
'\uee09',
|
||
'\uee0a',
|
||
'\uee0b',
|
||
'\uf5d0',
|
||
'\uf5d1',
|
||
'\uf5d2',
|
||
'\uf5d3',
|
||
'\uf5d4',
|
||
'\uf5d5',
|
||
'\uf5d6',
|
||
'\uf5d7',
|
||
'\uf5d8',
|
||
'\uf5d9',
|
||
'\uf5da',
|
||
'\uf5db',
|
||
'\uf5dc',
|
||
'\uf5dd',
|
||
'\uf5de',
|
||
'\uf5df',
|
||
'\uf5e0',
|
||
'\uf5e1',
|
||
'\uf5e2',
|
||
'\uf5e3',
|
||
'\uf5e4',
|
||
'\uf5e5',
|
||
'\uf5e6',
|
||
'\uf5e7',
|
||
'\uf5e8',
|
||
'\uf5e9',
|
||
'\uf5ea',
|
||
'\uf5eb',
|
||
'\uf5ec',
|
||
'\uf5ed',
|
||
'\uf5ee',
|
||
'\uf5ef',
|
||
'\uf5f0',
|
||
'\uf5f1',
|
||
'\uf5f2',
|
||
'\uf5f3',
|
||
'\uf5f4',
|
||
'\uf5f5',
|
||
'\uf5f6',
|
||
'\uf5f7',
|
||
'\uf5f8',
|
||
'\uf5f9',
|
||
'\uf5fa',
|
||
'\uf5fb',
|
||
'\uf5fc',
|
||
'\uf5fd',
|
||
'\uf5fe',
|
||
'\uf5ff',
|
||
'\uf600',
|
||
'\uf601',
|
||
'\uf602',
|
||
'\uf603',
|
||
'\uf604',
|
||
'\uf605',
|
||
'\uf606',
|
||
'\uf607',
|
||
'\uf608',
|
||
'\uf609',
|
||
'\uf60a',
|
||
'\uf60b',
|
||
'\uf60c',
|
||
'\uf60d',
|
||
'🬀',
|
||
'🬁',
|
||
'🬂',
|
||
'🬃',
|
||
'🬄',
|
||
'🬅',
|
||
'🬆',
|
||
'🬇',
|
||
'🬈',
|
||
'🬉',
|
||
'🬊',
|
||
'🬋',
|
||
'🬌',
|
||
'🬍',
|
||
'🬎',
|
||
'🬏',
|
||
'🬐',
|
||
'🬑',
|
||
'🬒',
|
||
'🬓',
|
||
'🬔',
|
||
'🬕',
|
||
'🬖',
|
||
'🬗',
|
||
'🬘',
|
||
'🬙',
|
||
'🬚',
|
||
'🬛',
|
||
'🬜',
|
||
'🬝',
|
||
'🬞',
|
||
'🬟',
|
||
'🬠',
|
||
'🬡',
|
||
'🬢',
|
||
'🬣',
|
||
'🬤',
|
||
'🬥',
|
||
'🬦',
|
||
'🬧',
|
||
'🬨',
|
||
'🬩',
|
||
'🬪',
|
||
'🬫',
|
||
'🬬',
|
||
'🬭',
|
||
'🬮',
|
||
'🬯',
|
||
'🬰',
|
||
'🬱',
|
||
'🬲',
|
||
'🬳',
|
||
'🬴',
|
||
'🬵',
|
||
'🬶',
|
||
'🬷',
|
||
'🬸',
|
||
'🬹',
|
||
'🬺',
|
||
'🬻',
|
||
'🬼',
|
||
'🬽',
|
||
'🬾',
|
||
'🬿',
|
||
'🭀',
|
||
'🭁',
|
||
'🭂',
|
||
'🭃',
|
||
'🭄',
|
||
'🭅',
|
||
'🭆',
|
||
'🭇',
|
||
'🭈',
|
||
'🭉',
|
||
'🭊',
|
||
'🭋',
|
||
'🭌',
|
||
'🭍',
|
||
'🭎',
|
||
'🭏',
|
||
'🭐',
|
||
'🭑',
|
||
'🭒',
|
||
'🭓',
|
||
'🭔',
|
||
'🭕',
|
||
'🭖',
|
||
'🭗',
|
||
'🭘',
|
||
'🭙',
|
||
'🭚',
|
||
'🭛',
|
||
'🭜',
|
||
'🭝',
|
||
'🭞',
|
||
'🭟',
|
||
'🭠',
|
||
'🭡',
|
||
'🭢',
|
||
'🭣',
|
||
'🭤',
|
||
'🭥',
|
||
'🭦',
|
||
'🭧',
|
||
'🭨',
|
||
'🭩',
|
||
'🭪',
|
||
'🭫',
|
||
'🭬',
|
||
'🭭',
|
||
'🭮',
|
||
'🭯',
|
||
'🭰',
|
||
'🭱',
|
||
'🭲',
|
||
'🭳',
|
||
'🭴',
|
||
'🭵',
|
||
'🭶',
|
||
'🭷',
|
||
'🭸',
|
||
'🭹',
|
||
'🭺',
|
||
'🭻',
|
||
'🭼',
|
||
'🭽',
|
||
'🭾',
|
||
'🭿',
|
||
'🮀',
|
||
'🮁',
|
||
'🮂',
|
||
'🮃',
|
||
'🮄',
|
||
'🮅',
|
||
'🮆',
|
||
'🮇',
|
||
'🮈',
|
||
'🮉',
|
||
'🮊',
|
||
'🮋',
|
||
'🮌',
|
||
'🮍',
|
||
'🮎',
|
||
'🮏',
|
||
'🮐',
|
||
'🮑',
|
||
'🮒',
|
||
'\U0001fb93',
|
||
'🮔',
|
||
'🮕',
|
||
'🮖',
|
||
'🮗',
|
||
'🮘',
|
||
'🮙',
|
||
'🮚',
|
||
'🮛',
|
||
'🮜',
|
||
'🮝',
|
||
'🮞',
|
||
'🮟',
|
||
'🮠',
|
||
'🮡',
|
||
'🮢',
|
||
'🮣',
|
||
'🮤',
|
||
'🮥',
|
||
'🮦',
|
||
'🮧',
|
||
'🮨',
|
||
'🮩',
|
||
'🮪',
|
||
'🮫',
|
||
'🮬',
|
||
'🮭',
|
||
'🮮',
|
||
'\U0001fbe6',
|
||
'\U0001fbe7',
|
||
} # }}}
|
||
for ch in range(0x1CD00, 0x1CDE5 + 1): # octants
|
||
box_chars.add(chr(ch))
|
||
for ch in range(0x1FBCE, 0x1FBF0): # blocks, diagonals, circles (legacy computing)
|
||
box_chars.add(chr(ch))
|
||
for ch in range(0x1CC1B, 0x1CC40): # box drawing variants, separated quadrants, circle arcs (supplement)
|
||
box_chars.add(chr(ch))
|
||
for ch in range(0x1CE16, 0x1CE1A): # box drawings light vertical T-junctions (supplement)
|
||
box_chars.add(chr(ch))
|
||
for ch in range(0x1CE51, 0x1CEB0): # separated block sextants, sixteenth blocks, quarter parts (supplement)
|
||
box_chars.add(chr(ch))
|