77 lines
4.1 KiB
Python
77 lines
4.1 KiB
Python
#!/usr/bin/env python3
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
"""Update outlined FD 501 labels to FD 502 without changing their perspective.
|
|
The digit 2 is from the Eurostile-outlined controller label. Stable SVG IDs
|
|
remain independent of the displayed product identities.
|
|
Idempotent; ordinary artwork compilation does not require this migration.
|
|
"""
|
|
from pathlib import Path
|
|
import re
|
|
ROOT = Path(__file__).resolve().parents[3]
|
|
NUMBER = r"-?\d+(?:\.\d+)?"
|
|
ONE = [1111.25395,43.12691,1111.25395,72,1101.90949,72,1101.90949,47.90738,1089.10145,55.35304,1083.54531,51.82058,1099.45613,43.12691]
|
|
TWO = 'M 414.70665 236.92635 L 414.70665 237.35687 L 411.32842 237.35687 L 411.32842 236.77305 Q 411.32842 236.31944 411.58212 236.17033 Q 411.83581 236.02123 412.78839 235.91203 Q 413.5773 235.82172 413.72297 235.74717 Q 413.86864 235.67262 413.86864 235.36181 Q 413.86864 235.0636 413.72297 234.9838 Q 413.5773 234.904 413.03063 234.904 Q 412.46759 234.904 412.32029 234.98485 Q 412.17298 235.0657 412.17298 235.37861 L 412.17298 235.50041 L 411.34152 235.50041 L 411.34152 235.38071 Q 411.34152 234.841 411.68851 234.65409 Q 412.0355 234.46719 413.0339 234.46719 Q 414.0094 234.46719 414.35802 234.65619 Q 414.70665 234.8452 414.70665 235.37441 Q 414.70665 235.87422 414.4415 236.04328 Q 414.17635 236.21234 413.22049 236.32574 Q 412.43159 236.41814 412.30392 236.48219 Q 412.17626 236.54624 412.17626 236.84865 L 412.17626 236.92635 Z'
|
|
|
|
def points(path):
|
|
n=list(map(float,re.findall(NUMBER,path)))
|
|
return list(zip(n[::2],n[1::2]))
|
|
|
|
def solve(a,b):
|
|
rows=[row+[v] for row,v in zip(a,b)]
|
|
for col in range(8):
|
|
pivot=max(range(col,8),key=lambda i:abs(rows[i][col]))
|
|
rows[col],rows[pivot]=rows[pivot],rows[col]
|
|
divisor=rows[col][col]
|
|
assert abs(divisor)>1e-12
|
|
rows[col]=[v/divisor for v in rows[col]]
|
|
for i in range(8):
|
|
if i!=col:
|
|
factor=rows[i][col]
|
|
rows[i]=[x-factor*y for x,y in zip(rows[i],rows[col])]
|
|
return [row[-1] for row in rows]
|
|
|
|
def replace_digit(path):
|
|
original=list(zip(ONE[::2],ONE[1::2])); target=points(path)
|
|
assert len(target)==len(original), 'Unexpected digit 1 outline'
|
|
# Normalize source coordinates for a well-conditioned projective fit.
|
|
norm=lambda p:((p[0]-1083.54531)/40,(p[1]-42.89424)/29.33843)
|
|
a=[];b=[]
|
|
for i in [0,1,2,4]:
|
|
x,y=norm(original[i]);u,v=target[i]
|
|
a.extend([[x,y,1,0,0,0,-u*x,-u*y],[0,0,0,x,y,1,-v*x,-v*y]])
|
|
b.extend([u,v])
|
|
h=solve(a,b)
|
|
def project(x,y):
|
|
d=h[6]*x+h[7]*y+1
|
|
return ((h[0]*x+h[1]*y+h[2])/d,(h[3]*x+h[4]*y+h[5])/d)
|
|
for old,new in zip(original,target):
|
|
mapped=project(*norm(old))
|
|
assert max(abs(x-y) for x,y in zip(mapped,new))<.005, 'Perspective mismatch'
|
|
pts=points(TWO)
|
|
x0=min(x for x,y in pts);x1=max(x for x,y in pts)
|
|
y0=min(y for x,y in pts);y1=max(y for x,y in pts)
|
|
def convert(m):
|
|
x,y=map(float,m.groups());u,v=project((x-x0)/(x1-x0),(y-y0)/(y1-y0))
|
|
return f'{u:.5f} {v:.5f}'
|
|
return re.sub('('+NUMBER+r')\s+('+NUMBER+')',convert,TWO)
|
|
|
|
def migrate(path):
|
|
text=path.read_text();count=0
|
|
def label(m):
|
|
nonlocal count
|
|
block=m.group();matches=list(re.finditer(r'<path\b[^>]*\bd="([^"]+)"',block))
|
|
assert matches
|
|
last=matches[-1];start,end=last.span(1)
|
|
block=block[:start]+replace_digit(last.group(1))+block[end:]
|
|
count+=1
|
|
return block.replace('aria-label="DISK DRIVE FD 501"','aria-label="DISK DRIVE FD 502"').replace('aria-label="FD 501"','aria-label="FD 502"')
|
|
updated=re.sub(r'<g\b[^>]*aria-label="(?:DISK DRIVE FD 501|FD 501)"[^>]*>.*?</g>',label,text,flags=re.S)
|
|
# Accessibility descriptions follow visible labels; source metadata is retained.
|
|
updated=re.sub(r'<(?:title|desc)\b[^>]*>.*?</(?:title|desc)>',lambda m:m.group().replace('FD 501','FD 502').replace('FD-501','FD-502'),updated,flags=re.S)
|
|
if updated!=text:path.write_text(updated)
|
|
return count
|
|
|
|
if __name__=='__main__':
|
|
count=sum(migrate(p) for p in (ROOT/'docs/media').rglob('*.svg'))
|
|
print(f'Updated {count} outlined labels')
|