Files
jobtrackingapp/docs/p10-entry-final/inspect-font-tables.py
T

36 lines
2.0 KiB
Python

"""Read shipped font tables and explain platform descriptor differences; no font edits."""
import argparse
import hashlib
import json
import struct
from pathlib import Path
parser=argparse.ArgumentParser(description=__doc__)
parser.add_argument('output',type=Path)
args=parser.parse_args()
if args.output.exists():raise ValueError('Keep earlier font inventories immutable')
rows=[]
for name in ['selawkb.ttf','Roboto-Bold.ttf','Arimo-Bold.ttf','Gelasio-Bold.ttf','Gelasio-SemiBold.ttf','Poppins-Bold.ttf']:
file=Path(__file__).resolve().parents[2]/'JobTrackerApi/DocumentFonts'/name
data=file.read_bytes()
assert data[:4]==b'\x00\x01\x00\x00' and len(data)<5*1024*1024
count=struct.unpack_from('>H',data,4)[0];assert 1<=count<=64
tables={}
for index in range(count):
tag,checksum,offset,length=struct.unpack_from('>4sIII',data,12+index*16)
assert offset+length<=len(data)
tables[tag]=data[offset:offset+length]
assert len(tables[b'head'])>=20 and len(tables[b'hhea'])>=8 and len(tables[b'OS/2'])>=78
units=struct.unpack_from('>H',tables[b'head'],18)[0];assert units>0
ascent,descent=struct.unpack_from('>hh',tables[b'hhea'],4)
typo_ascent,typo_descent=struct.unpack_from('>hh',tables[b'OS/2'],68)
win_ascent,win_descent=struct.unpack_from('>HH',tables[b'OS/2'],74)
selection=struct.unpack_from('>H',tables[b'OS/2'],62)[0]
rows.append(dict(file=file.name,sha256=hashlib.sha256(data).hexdigest(),unitsPerEm=units,
hheaAscent1000=ascent/units*1000,hheaDescent1000=descent/units*1000,
typoAscent1000=typo_ascent/units*1000,typoDescent1000=typo_descent/units*1000,
windowsAscent1000=win_ascent/units*1000,windowsDescent1000=win_descent/units*1000,
useTypoMetrics=bool(selection & (1<<7))))
args.output.write_bytes((json.dumps(dict(evidenceOnly=True,fonts=rows),indent=2)+'\n').encode())
print('Inspected6 bundled font metric tables without modifying fonts')