1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
|
#!/usr/bin/env python
# License: GPL v3 Copyright: 2017, Kovid Goyal <kovid at kovidgoyal.net>
import array
import os
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,
get_fallback_font,
set_allow_use_of_box_fonts,
sprite_idx_to_pos,
sprite_map_set_layout,
sprite_map_set_limits,
test_render_line,
test_sprite_position_increment,
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 . 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:
print(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 block_helpers(s, sprites, cell_width, cell_height):
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=''):
if a != b:
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):
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} is not equal')
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.
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_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_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_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)
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)])
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<b'), [(1, 1), (1, 2), (1, 1), (1, 1)])
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('|\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_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')
@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))
|