63 lines
3.7 KiB
Python
63 lines
3.7 KiB
Python
#!/usr/bin/env python3
|
|
"""Freeze independent fontTools Type1 path hashes for all actual bundled PFBs.
|
|
|
|
fontTools is needed only to regenerate this test oracle, never by the kernel.
|
|
Paths are unhinted, absolute M/L/C/Z commands in the standard 1000-unit space.
|
|
"""
|
|
import hashlib,json,math
|
|
from pathlib import Path
|
|
from fontTools.t1Lib import T1Font
|
|
from fontTools.pens.recordingPen import DecomposingRecordingPen
|
|
from fontTools.pens.transformPen import TransformPen
|
|
from fontTools.misc.psCharStrings import T1CharString
|
|
ROOT=Path(__file__).resolve().parents[1]
|
|
def number(v):return format(v,'.6f').rstrip('0').rstrip('.')
|
|
def canonical(commands):
|
|
out=[]
|
|
for op,args in commands:
|
|
if op=='endPath':continue
|
|
if op=='closePath':out.append('Z');continue
|
|
if op not in ['moveTo','lineTo','curveTo']:raise ValueError(op)
|
|
out.append({'moveTo':'M','lineTo':'L','curveTo':'C'}[op]+ ' '.join(number(v) for point in args for v in point))
|
|
return ''.join(out)
|
|
# A valid, independently serialized program exercises seac, subroutines, div,
|
|
# lenIV=0, nonstandard FontMatrix, and glyphs absent from the builtin encoding.
|
|
fixture=ROOT/'crates/texrra-tex-core/tests/fixtures/type1-fixture.pfb'
|
|
f=T1Font(str(ROOT/'assets/fonts/cmr10.pfb'));f.parse()
|
|
f.font['FontName']='NativeType1Fixture'
|
|
f.font.pop('UniqueID',None)
|
|
f.font['FontMatrix']=[.002,0,.0005,.001,.01,-.02]
|
|
f.font['FontBBox']=[-100,-100,1000,1000]
|
|
private={'lenIV':0,**f.font['Private']};private.pop('UniqueID',None)
|
|
private['Subrs']=[T1CharString(program=[100,0,'rlineto',0,100,'rlineto',-100,0,'rlineto','closepath','return'])]
|
|
f.font['Private']=private
|
|
programs={
|
|
'.notdef':[0,600,'hsbw','endchar'],
|
|
'A':[0,600,'hsbw',50,0,'rmoveto',200,600,'rlineto',200,-600,'rlineto','closepath','endchar'],
|
|
'acute':[0,300,'hsbw',100,650,'rmoveto',100,100,'rlineto',50,0,'rlineto',-100,-100,'rlineto','closepath','endchar'],
|
|
'Aacute':[0,600,'hsbw',0,0,0,65,194,'seac'],
|
|
'divided':[0,600,'hsbw',100,3,'div',0,'rmoveto',200,0,'rlineto',10,20,30,40,50,60,'rrcurveto','closepath','endchar'],
|
|
'subr':[0,600,'hsbw',100,100,'rmoveto',0,'callsubr','endchar'],
|
|
}
|
|
programs['uni00410301']=programs['A'];programs['u1F600']=programs['A']
|
|
f.font['CharStrings']={n:T1CharString(program=v,subrs=private['Subrs']) for n,v in programs.items()}
|
|
f.saveAs(str(fixture),'PFB')
|
|
faces=[]
|
|
paths=sorted((ROOT/'assets/fonts').glob('*.pfb'))+[fixture]
|
|
for path in paths:
|
|
f=T1Font(str(path));glyphs=f.getGlyphSet();matrix=[n*1000 for n in f['FontMatrix']]
|
|
records={}
|
|
for name,glyph in sorted(glyphs.items()):
|
|
pen=DecomposingRecordingPen(glyphs);glyph.draw(TransformPen(pen,matrix));d=canonical(pen.value)
|
|
records[name]=hashlib.sha256(d.encode()).hexdigest()
|
|
bbox=f['FontBBox']
|
|
corners=[(matrix[0]*x+matrix[2]*y+matrix[4],matrix[1]*x+matrix[3]*y+matrix[5]) for x in [bbox[0],bbox[2]] for y in [bbox[1],bbox[3]]]
|
|
normalized_bbox=[math.floor(min(p[0] for p in corners)),math.floor(min(p[1] for p in corners)),math.ceil(max(p[0] for p in corners)),math.ceil(max(p[1] for p in corners))]
|
|
stem=f['Private'].get('StdVW',[0])[0]
|
|
faces.append(dict(file=path.name,path=str(path.relative_to(ROOT)),sha256=hashlib.sha256(path.read_bytes()).hexdigest(),
|
|
italic_angle=f['FontInfo'].get('ItalicAngle',0),fixed_pitch=bool(f['FontInfo'].get('isFixedPitch',False)),
|
|
stem_v=round(stem*abs(matrix[0])),normalized_bbox=normalized_bbox,glyphs=records))
|
|
out=ROOT/'crates/texrra-tex-core/tests/fixtures/type1-outlines.json'
|
|
out.write_text(json.dumps(dict(source='fontTools 4.68.0 T1OutlineExtractor + DecomposingRecordingPen; independent of Rust decoder',faces=faces),indent=2)+'\n')
|
|
print(len(faces),'actual PFB files;',sum(len(f['glyphs']) for f in faces),'complete glyph programs;',out)
|