#!/usr/bin/env python3
"""Remove the extra 0.1 mm legacy PPT page-grid snap in the existing WASM native build."""
from pathlib import Path
import os,json,hashlib,difflib,datetime
root=Path.cwd();core=Path(os.environ['OFFICE_BUILD_ROOT'])/'core-fc03117d3f8f2d7137d973e5db3b2514e8eba990';p=core/'filter/source/msfilter/svdfppt.cxx';old=p.read_text();needle='''    // PPT works with units of 576 dpi in any case. To avoid inaccuracies
    // I do round the last decimal digit away.
    if ( nMapMul > 2 * nMapDiv )''';assert old.count(needle)==1
new=old.replace(needle,'''    // Preserve the finer model-unit page dimensions for on-device PDF export.
    // The historic additional 0.1 mm snap changes a 405 pt source to 405.070866 pt.
    // Native non-WASM builds keep their previous import policy.
    if ( !getenv("TABMORPH_PRECISE_PPT_PAGE_SIZE") && nMapMul > 2 * nMapDiv )''')
# getenv is already declared by the source's normal includes; the trusted worker sets this flag.
assert 'getenv(' in new
p.write_text(new);out=root/'benchmarks/research/office-legacy/native-page-size';out.mkdir(parents=True,exist_ok=False);patch=''.join(difflib.unified_diff(old.splitlines(True),new.splitlines(True),fromfile='a/filter/source/msfilter/svdfppt.cxx',tofile='b/filter/source/msfilter/svdfppt.cxx'));(out/'precise-ppt-page-size.patch').write_text(patch);(out/'patch.json').write_text(json.dumps({'checkedAt':datetime.datetime.now(datetime.timezone.utc).isoformat(),'source':'filter/source/msfilter/svdfppt.cxx','beforeSha256':hashlib.sha256(old.encode()).hexdigest(),'afterSha256':hashlib.sha256(new.encode()).hexdigest(),'scope':'Only bypasses the additional 0.1 mm page-size snap when a trusted WASM worker sets TABMORPH_PRECISE_PPT_PAGE_SIZE. Existing model-unit scaling and all other formats/imports remain. Native ordinary CLI policy unchanged. No claimed provider advantage.'},indent=2)+'\n');print(str(out))
