"""Verify shipped font bytes and typography invariants without rasterizing anew."""
from pathlib import Path
import hashlib
import json
import unittest
HERE = Path(__file__).resolve().parent / "console"
METADATA = json.loads((HERE / "fonts.json").read_text(encoding="utf-8"))
WIDTH, HEIGHT = METADATA["cell_width"], METADATA["cell_height"]
FIRST, LAST = METADATA["codepoint_first"], METADATA["codepoint_last"]
def glyph(bank, character):
offset = (ord(character) - FIRST) * WIDTH * HEIGHT
return bank[offset:offset + WIDTH * HEIGHT]
def bottom(bank, character):
pixels = glyph(bank, character)
return max(y for y in range(HEIGHT) if any(pixels[y * WIDTH:(y + 1) * WIDTH]))
class ConsoleFonts(unittest.TestCase):
def test_all_native_sizes_have_valid_layout_coverage_and_descenders(self):
desc=METADATA['native_scales'];bank=(HERE/desc['file']).read_bytes()
self.assertEqual(hashlib.sha256(bank).hexdigest(),desc['sha256'])
end=0
for scale in desc['scales']:
with self.subTest(percent=scale['percent']):
width,height=scale['cell_width'],scale['cell_height'];record=width*height
self.assertEqual(scale['offset'],end)
subset=bank[end:end+96*record];self.assertEqual(len(subset),96*record)
self.assertTrue(any(0<a<255 for a in subset))
def glyph_bottom(c):
data=subset[(ord(c)-32)*record:(ord(c)-31)*record]
return max(y for y in range(height) if any(data[y*width:(y+1)*width]))
for c in 'gjpqy':self.assertGreater(glyph_bottom(c),glyph_bottom('x'))
end+=96*record
self.assertEqual(end,len(bank));self.assertEqual(len(desc['scales']),13)
# Native 200% must contain outline-rasterized coverage, not 2x enlarged
# base pixels; otherwise the high-DPI typography fix is ineffective.
large=desc['scales'][4];w,h=large['cell_width'],large['cell_height']
native=bank[large['offset']:large['offset']+96*w*h]
base=(HERE/METADATA['fonts'][0]['file']).read_bytes()
enlarged=bytes(base[c*WIDTH*HEIGHT+(y//2)*WIDTH+x//2]
for c in range(96) for y in range(h) for x in range(w))
self.assertNotEqual(native,enlarged)
def test_banks_match_declared_bytes_and_hashes(self):
for font in METADATA["fonts"]:
with self.subTest(font=font["name"]):
data = (HERE / font["file"]).read_bytes()
self.assertEqual(len(data), (LAST - FIRST + 1) * WIDTH * HEIGHT)
self.assertEqual(hashlib.sha256(data).hexdigest(), font["sha256"])
self.assertEqual(font["license"], "OFL-1.1")
def test_printables_have_ink_and_controls_do_not(self):
for font in METADATA["fonts"]:
data = (HERE / font["file"]).read_bytes()
for code in range(FIRST + 1, LAST):
with self.subTest(font=font["name"], code=code):
self.assertTrue(any(glyph(data, chr(code))))
self.assertFalse(any(glyph(data, " ")))
self.assertFalse(any(glyph(data, "\x7f")))
def test_descenders_are_below_letters_on_both_fonts(self):
baseline = METADATA["baseline_y"]
for font in METADATA["fonts"]:
data = (HERE / font["file"]).read_bytes()
for character in "gjpqy":
with self.subTest(font=font["name"], character=character):
self.assertGreaterEqual(bottom(data, character), baseline)
self.assertGreater(bottom(data, character), bottom(data, "a"))
self.assertGreater(bottom(data, character), bottom(data, "x"))
self.assertEqual(bottom(data, "a"), baseline - 1)
self.assertEqual(bottom(data, "x"), baseline - 1)
def test_xfe_mono_retains_antialiasing(self):
font = METADATA["fonts"][0]
data = (HERE / font["file"]).read_bytes()
self.assertTrue(any(0 < value < 255 for value in data))
self.assertEqual(font["ascent"] + font["descent"], 19)
self.assertEqual(font["ink_bounds"]["y"][3], HEIGHT)
if __name__ == "__main__":
unittest.main(verbosity=2)
"""Verify shipped font bytes and typography invariants without rasterizing anew."""
from pathlib import Path
import hashlib
import json
import unittest
HERE = Path(__file__).resolve().parent / "console"
METADATA = json.loads((HERE / "fonts.json").read_text(encoding="utf-8"))
WIDTH, HEIGHT = METADATA["cell_width"], METADATA["cell_height"]
FIRST, LAST = METADATA["codepoint_first"], METADATA["codepoint_last"]
def glyph(bank, character):
offset = (ord(character) - FIRST) * WIDTH * HEIGHT
return bank[offset:offset + WIDTH * HEIGHT]
def bottom(bank, character):
pixels = glyph(bank, character)
return max(y for y in range(HEIGHT) if any(pixels[y * WIDTH:(y + 1) * WIDTH]))
class ConsoleFonts(unittest.TestCase):
def test_all_native_sizes_have_valid_layout_coverage_and_descenders(self):
desc=METADATA['native_scales'];bank=(HERE/desc['file']).read_bytes()
self.assertEqual(hashlib.sha256(bank).hexdigest(),desc['sha256'])
end=0
for scale in desc['scales']:
with self.subTest(percent=scale['percent']):
width,height=scale['cell_width'],scale['cell_height'];record=width*height
self.assertEqual(scale['offset'],end)
subset=bank[end:end+96*record];self.assertEqual(len(subset),96*record)
self.assertTrue(any(0<a<255 for a in subset))
def glyph_bottom(c):
data=subset[(ord(c)-32)*record:(ord(c)-31)*record]
return max(y for y in range(height) if any(data[y*width:(y+1)*width]))
for c in 'gjpqy':self.assertGreater(glyph_bottom(c),glyph_bottom('x'))
end+=96*record
self.assertEqual(end,len(bank));self.assertEqual(len(desc['scales']),13)
# Native 200% must contain outline-rasterized coverage, not 2x enlarged
# base pixels; otherwise the high-DPI typography fix is ineffective.
large=desc['scales'][4];w,h=large['cell_width'],large['cell_height']
native=bank[large['offset']:large['offset']+96*w*h]
base=(HERE/METADATA['fonts'][0]['file']).read_bytes()
enlarged=bytes(base[c*WIDTH*HEIGHT+(y//2)*WIDTH+x//2]
for c in range(96) for y in range(h) for x in range(w))
self.assertNotEqual(native,enlarged)
def test_banks_match_declared_bytes_and_hashes(self):
for font in METADATA["fonts"]:
with self.subTest(font=font["name"]):
data = (HERE / font["file"]).read_bytes()
self.assertEqual(len(data), (LAST - FIRST + 1) * WIDTH * HEIGHT)
self.assertEqual(hashlib.sha256(data).hexdigest(), font["sha256"])
self.assertEqual(font["license"], "OFL-1.1")
def test_printables_have_ink_and_controls_do_not(self):
for font in METADATA["fonts"]:
data = (HERE / font["file"]).read_bytes()
for code in range(FIRST + 1, LAST):
with self.subTest(font=font["name"], code=code):
self.assertTrue(any(glyph(data, chr(code))))
self.assertFalse(any(glyph(data, " ")))
self.assertFalse(any(glyph(data, "\x7f")))
def test_descenders_are_below_letters_on_both_fonts(self):
baseline = METADATA["baseline_y"]
for font in METADATA["fonts"]:
data = (HERE / font["file"]).read_bytes()
for character in "gjpqy":
with self.subTest(font=font["name"], character=character):
self.assertGreaterEqual(bottom(data, character), baseline)
self.assertGreater(bottom(data, character), bottom(data, "a"))
self.assertGreater(bottom(data, character), bottom(data, "x"))
self.assertEqual(bottom(data, "a"), baseline - 1)
self.assertEqual(bottom(data, "x"), baseline - 1)
def test_xfe_mono_retains_antialiasing(self):
font = METADATA["fonts"][0]
data = (HERE / font["file"]).read_bytes()
self.assertTrue(any(0 < value < 255 for value in data))
self.assertEqual(font["ascent"] + font["descent"], 19)
self.assertEqual(font["ink_bounds"]["y"][3], HEIGHT)
if __name__ == "__main__":
unittest.main(verbosity=2)