#!/usr/bin/env python # License: GPL v3 Copyright: 2017, Kovid Goyal import os import shutil import sys import tempfile import unittest from functools import partial from kitty.constants import is_macos, read_kitty_resource from kitty.fast_data_types import DECAWM, get_fallback_font, sprite_map_set_layout, sprite_map_set_limits, test_render_line, test_sprite_position_for, wcwidth from kitty.fonts.box_drawing import box_chars from kitty.fonts.render import coalesce_symbol_maps, render_string, setup_for_testing, shape_string from . import BaseTest class Rendering(BaseTest): def setUp(self): super().setUp() self.test_ctx = setup_for_testing() self.test_ctx.__enter__() self.sprites, self.cell_width, self.cell_height = self.test_ctx.__enter__() try: self.assertEqual([k[0] for k in self.sprites], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) except Exception: self.test_ctx.__exit__() del self.test_ctx raise self.tdir = tempfile.mkdtemp() def tearDown(self): self.test_ctx.__exit__() del self.sprites, self.cell_width, self.cell_height, self.test_ctx shutil.rmtree(self.tdir) super().tearDown() def test_sprite_map(self): sprite_map_set_limits(10, 2) sprite_map_set_layout(5, 5) self.ae(test_sprite_position_for(0), (0, 0, 0)) self.ae(test_sprite_position_for(1), (1, 0, 0)) self.ae(test_sprite_position_for(2), (0, 1, 0)) self.ae(test_sprite_position_for(3), (1, 1, 0)) self.ae(test_sprite_position_for(4), (0, 0, 1)) self.ae(test_sprite_position_for(5), (1, 0, 1)) self.ae(test_sprite_position_for(6), (0, 1, 1)) self.ae(test_sprite_position_for(7), (1, 1, 1)) self.ae(test_sprite_position_for(0, 1), (0, 0, 2)) self.ae(test_sprite_position_for(0, 2), (1, 0, 2)) def test_box_drawing(self): prerendered = len(self.sprites) s = self.create_screen(cols=len(box_chars) + 1, lines=1, scrollback=0) s.draw(''.join(box_chars)) line = s.line(0) test_render_line(line) self.assertEqual(len(self.sprites) - prerendered, len(box_chars)) 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) def test_shaping(self): font_path_cache = {} def path_for_font(name): if name not in font_path_cache: with open(os.path.join(self.tdir, name), 'wb') as f: font_path_cache[name] = f.name data = read_kitty_resource(name, __name__.rpartition('.')[0]) f.write(data) return font_path_cache[name] def ss(text, font=None): path = 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)]) 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('a>\u2060