Allow more subscales

This commit is contained in:
Kovid Goyal
2024-12-03 10:55:59 +05:30
parent b1c80d212f
commit 8030cbd361
9 changed files with 77 additions and 54 deletions

View File

@@ -37,8 +37,8 @@ def parse_bytes(screen, data, dump_callback=None):
screen.test_parse_written_data(dump_callback)
def draw_multicell(screen: Screen, text: str, width: int = 0, scale: int = 1, subscale: int = 0, vertical_align: int = 0) -> None:
cmd = f'\x1b]{TEXT_SIZE_CODE};w={width}:s={scale}:f={subscale}:v={vertical_align};{text}\a'
def draw_multicell(screen: Screen, text: str, width: int = 0, scale: int = 1, subscale_n: int = 0, subscale_d: int = 0, vertical_align: int = 0) -> None:
cmd = f'\x1b]{TEXT_SIZE_CODE};w={width}:s={scale}:n={subscale_n}:d={subscale_d}:v={vertical_align};{text}\a'
parse_bytes(screen, cmd.encode())

View File

@@ -211,39 +211,42 @@ class Rendering(BaseTest):
def test_scaled_box_drawing(self):
block_size = self.cell_width * self.cell_height * 4
def full_block(subscale):
def full_block():
return b'\xff' * block_size
def empty_block(subscale):
def empty_block():
return b'\0' * block_size
def half_block(subscale, first=b'\xff', second=b'\0'):
frac = 1 / (subscale + 1)
def half_block(first=b'\xff', second=b'\0'):
frac = 0.5
height = ceil(frac * self.cell_height)
rest = self.cell_height - height
return (first * (rest * self.cell_width * 4)) + (second * height * self.cell_width * 4)
def upper_half_block(subscale):
return half_block(subscale)
def upper_half_block():
return half_block()
def lower_half_block(subscale):
return half_block(subscale, b'\0', b'\xff')
def lower_half_block():
return half_block(b'\0', b'\xff')
s = self.create_screen(cols=8, lines=8, scrollback=0)
def block_test(a=empty_block, b=empty_block, c=empty_block, d=empty_block, scale=2, subscale=1, vertical_align=0):
def block_test(a=empty_block, b=empty_block, c=empty_block, d=empty_block, scale=2, half_block=True, vertical_align=0):
s.reset()
before = len(self.sprites)
draw_multicell(s, '', scale=scale, subscale=subscale, vertical_align=vertical_align)
subscale_n = subscale_d = 0
if half_block:
subscale_n, subscale_d = 1, 2
draw_multicell(s, '', scale=scale, subscale_n=subscale_n, subscale_d=subscale_d, vertical_align=vertical_align)
test_render_line(s.line(0))
self.ae(len(self.sprites), before + 2)
test_render_line(s.line(1))
self.ae(len(self.sprites), before + 4)
blocks = tuple(self.sprites)[before:]
for i, (expected, actual) in enumerate(zip((a(subscale), b(subscale), c(subscale), d(subscale)), blocks)):
for i, (expected, actual) in enumerate(zip((a(), b(), c(), d()), blocks)):
self.ae(self.sprites[actual], expected, f'The {i} block differs')
block_test(full_block, full_block, full_block, full_block, subscale=0)
block_test(full_block, full_block, full_block, full_block, half_block=False)
block_test(a=full_block)
block_test(c=full_block, vertical_align=1)
block_test(a=lower_half_block, c=upper_half_block, vertical_align=2)

View File

@@ -41,7 +41,8 @@ def test_multicell(self: TestMulticell) -> None:
ae('y')
ae('width')
ae('scale')
ae('subscale')
ae('subscale_n')
ae('subscale_d')
ae('vertical_align')
ae('text')
ae('natural_width')
@@ -90,21 +91,21 @@ def test_multicell(self: TestMulticell) -> None:
s.reset()
ac(0, 0, is_multicell=False)
multicell(s, 'a')
ac(0, 0, is_multicell=True, width=1, scale=1, subscale=0, x=0, y=0, text='a', natural_width=True, cursor=(1, 0))
ac(0, 0, is_multicell=True, width=1, scale=1, subscale_n=0, x=0, y=0, text='a', natural_width=True, cursor=(1, 0))
ac(0, 1, is_multicell=False), ac(1, 0, is_multicell=False), ac(1, 1, is_multicell=False)
s.draw('')
ac(0, 0, is_multicell=True, width=1, scale=1, subscale=0, x=0, y=0, text='a', natural_width=True)
ac(1, 0, is_multicell=True, width=2, scale=1, subscale=0, x=0, y=0, text='', natural_width=True, cursor=(3, 0))
ac(2, 0, is_multicell=True, width=2, scale=1, subscale=0, x=1, y=0, text='', natural_width=True)
ac(0, 0, is_multicell=True, width=1, scale=1, subscale_n=0, x=0, y=0, text='a', natural_width=True)
ac(1, 0, is_multicell=True, width=2, scale=1, subscale_n=0, x=0, y=0, text='', natural_width=True, cursor=(3, 0))
ac(2, 0, is_multicell=True, width=2, scale=1, subscale_n=0, x=1, y=0, text='', natural_width=True)
for x in range(s.columns):
ac(x, 1, is_multicell=False)
s.cursor.x = 0
multicell(s, 'a', width=2, scale=2, subscale=3)
ac(0, 0, is_multicell=True, width=2, scale=2, subscale=3, x=0, y=0, text='a', natural_width=False, cursor=(4, 0))
multicell(s, 'a', width=2, scale=2, subscale_n=3)
ac(0, 0, is_multicell=True, width=2, scale=2, subscale_n=3, x=0, y=0, text='a', natural_width=False, cursor=(4, 0))
for x in range(1, 4):
ac(x, 0, is_multicell=True, width=2, scale=2, subscale=3, x=x, y=0, text='', natural_width=False)
ac(x, 0, is_multicell=True, width=2, scale=2, subscale_n=3, x=x, y=0, text='', natural_width=False)
for x in range(0, 4):
ac(x, 1, is_multicell=True, width=2, scale=2, subscale=3, x=x, y=1, text='', natural_width=False)
ac(x, 1, is_multicell=True, width=2, scale=2, subscale_n=3, x=x, y=1, text='', natural_width=False)
# Test draw with cursor in a multicell
s.reset()
@@ -116,7 +117,7 @@ def test_multicell(self: TestMulticell) -> None:
s.cursor.x = 0
s.draw('a'), ac(0, 0, is_multicell=False), ac(1, 0, is_multicell=False)
s.reset()
multicell(s, 'a', width=2, scale=2, subscale=3)
multicell(s, 'a', width=2, scale=2, subscale_n=3, subscale_d=4)
s.cursor.x, s.cursor.y = 1, 1
s.draw('b')
self.ae(8, count_multicells())
@@ -398,6 +399,8 @@ def test_multicell(self: TestMulticell) -> None:
s.reset()
s.reset()
multicell(s, 'a', width=2, scale=3, subscale_n=1, subscale_d=2, vertical_align=1)
ta('\x1b]66;w=2:s=3:n=1:d=2;a\x07')
s.draw('a')
multicell(s, 'b', width=2)
s.draw('c')