Refactor the box drawing showcase code

Make it work with the new font render infrastructure
This commit is contained in:
Kovid Goyal 2017-11-26 19:53:25 +05:30
parent 235f3940f2
commit 4c94e16936
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -344,8 +344,6 @@ for chars, func in (('╒╕╘╛', dvcorner), ('╓╖╙╜', dhcorner), ('
for ch in chars: for ch in chars:
box_chars[ch] = [p(func, which=ch)] box_chars[ch] = [p(func, which=ch)]
is_renderable_box_char = box_chars.__contains__
def render_box_char(ch, buf, width, height): def render_box_char(ch, buf, width, height):
for func in box_chars[ch]: for func in box_chars[ch]:
@ -362,34 +360,41 @@ def render_missing_glyph(buf, width, height):
draw_vline(buf, width, vgap, height - vgap + 1, width - hgap, 0) draw_vline(buf, width, vgap, height - vgap + 1, width - hgap, 0)
def join_rows(width, height, rows):
import ctypes
ans = (ctypes.c_ubyte * (width * height * len(rows)))()
pos = ctypes.addressof(ans)
row_sz = width * height
for row in rows:
ctypes.memmove(pos, ctypes.addressof(row), row_sz)
pos += row_sz
return ans
def test_drawing(sz=32, family='monospace'): def test_drawing(sz=32, family='monospace'):
from .render import join_cells, display_bitmap, render_cell, set_font_family from .render import display_bitmap, setup_for_testing
from kitty.config import defaults from kitty.fast_data_types import concat_cells, set_send_sprite_to_gpu
opts = defaults._replace(font_family=family, font_size=sz)
width, height = set_font_family(opts) width, height = setup_for_testing(family, sz)[1:]
space = bytearray(width * height)
def join_cells(cells):
cells = tuple(bytes(x) for x in cells)
return concat_cells(width, height, cells)
def render_chr(ch):
if ch in box_chars:
cell = bytearray(len(space))
render_box_char(ch, cell, width, height)
return cell
return space
pos = 0x2500 pos = 0x2500
rows = [] rows = []
space = render_cell()[0] space_row = join_cells(repeat(space, 32))
space_row = join_cells(width, height, *repeat(space, 32))
for r in range(8): try:
row = [] for r in range(8):
for i in range(16): row = []
row.append(render_cell(chr(pos))[0]), row.append(space) for i in range(16):
pos += 1 row.append(render_chr(chr(pos)))
rows.append(join_cells(width, height, *row)) row.append(space)
rows.append(space_row) pos += 1
width *= 32 rows.append(join_cells(row))
im = join_rows(width, height, rows) rows.append(space_row)
display_bitmap(im, width, height * len(rows)) rgb_data = b''.join(rows)
width *= 32
height *= len(rows)
assert len(rgb_data) == width * height * 3, '{} != {}'.format(len(rgb_data), width * height * 3)
display_bitmap(rgb_data, width, height)
finally:
set_send_sprite_to_gpu(None)