"""Render a weight sweep and compare variable endpoints against static references.""" import hashlib import json from pathlib import Path from fontTools.ttLib import TTFont from fontTools.varLib.instancer import instantiateVariableFont from PIL import Image, ImageChops, ImageDraw, ImageFont, ImageStat from render import ROOT, code_mask def main(): output = ROOT / 'build/variable' path = ROOT / 'build/TTF/iocgnMono-Variable.ttf' sweep = Image.new('RGB', (2200, 3640), '#fafafa') rows = [('Hno mWw r g 0O1Il| \'"`', 110, 165), ('comment format com rma minimum WWWW wwww mmmm', 48, 255), ('const row = `${name}: ${count}`; // + - = * < > ()[]{}', 42, 325), ('if (value != null && count >= 0) { return "airport"; }', 42, 385), ('aopqnr mWw 6G9g 8B @#%& \'quoted\' "double" `grave`', 24, 455)] report = {'font': {'path': str(path.relative_to(ROOT)), 'sha256': hashlib.sha256(path.read_bytes()).hexdigest()}, 'weights': [200, 300, 400, 550, 700, 750, 800], 'em_pixels': [110, 48, 42, 24], 'endpoints': {}} with TTFont(path) as variable: for i, weight in enumerate(report['weights']): font = instantiateVariableFont(variable, {'wght': weight, 'ital': 0}, inplace=False) panel = Image.new('RGB', (2200, 520), '#fafafa') panel.paste('#171c22', (0, 0), code_mask(font, rows, panel.size)) ImageDraw.Draw(panel).text((38, 20), f'iocgn Mono / wght {weight}', fill='#58616d', font=ImageFont.load_default(size=28)) sweep.paste(panel, (0, i * 520)) font.close() for style, weight in [('ExtraLight', 200), ('Regular', 400), ('Bold', 700), ('ExtraBold', 800)]: font = instantiateVariableFont(variable, {'wght': weight, 'ital': 0}, inplace=False) directory = ROOT / 'build/native' with TTFont(directory / f'iocgnMono-{style}.ttf') as static: grid = [(''.join(chr(cp) for cp in range(start, min(start + 13, 127))), 256, 240 + i * 300) for i, start in enumerate(range(33, 127, 13))] masks = [code_mask(f, grid, (1800, 2450)) for f in (static, font)] difference = ImageChops.difference(*masks) pixel_error = sum(ImageStat.Stat(difference).sum) ink = sum(ImageStat.Stat(masks[0]).sum) report['endpoints'][style] = {'absolute_raster_difference_over_ink': pixel_error / ink, 'em_pixels': 256} assert pixel_error / ink < .025, (style, pixel_error / ink) overlay = Image.merge('RGB', (masks[1], masks[0], ImageChops.lighter(*masks))) overlay.save(output / f'endpoint-{style.lower()}.png') font.close() charset = Image.new('RGB', (6000, 1650), '#fafafa') grid = [(''.join(chr(cp) for cp in range(start, min(start + 13, 127))), 160, 210 + i * 180) for i, start in enumerate(range(33, 127, 13))] for column, weight in enumerate((200, 400, 550, 700, 800)): font = instantiateVariableFont(variable, {'wght': weight, 'ital': 0}, inplace=False) panel = Image.new('RGB', (1200, 1650), '#fafafa') panel.paste('#171c22', (0, 0), code_mask(font, grid, panel.size)) ImageDraw.Draw(panel).text((38, 20), f'wght {weight}', fill='#58616d', font=ImageFont.load_default(size=28)) charset.paste(panel, (column * 1200, 0)) panel.save(output / f'charset-{weight}.png') font.close() charset.save(output / 'charset.png') sweep.save(output / 'weight-sweep.png') template = ROOT / 'tools/variable-demo.html' (output / 'index.html').write_bytes(template.read_bytes()) report['demo_sha256'] = hashlib.sha256(template.read_bytes()).hexdigest() report['renderer_sha256'] = hashlib.sha256(Path(__file__).read_bytes()).hexdigest() (output / 'render-manifest.json').write_text(json.dumps(report, indent=2) + '\n') print(output / 'weight-sweep.png') print('Endpoint raster differences:', report['endpoints']) if __name__ == '__main__': main()