"""List declared fonts and embedding evidence. Standard-library only; no rendering.
Usage: python3 inspect-pptx-fonts.py presentation.pptx [other.pptx ...]
Theme tokens are not installed font names. Output cannot prove visual fidelity.
"""
import hashlib
import json
import sys
import zipfile
from pathlib import Path
from xml.etree import ElementTree as ET


def inspect(path):
    path = Path(path)
    declarations = []
    embedded_lists = []
    with zipfile.ZipFile(path) as archive:
        names = archive.namelist()
        for name in names:
            if not name.endswith('.xml') or not name.startswith('ppt/'):
                continue
            root = ET.fromstring(archive.read(name))
            for node in root.iter():
                if 'typeface' in node.attrib and node.attrib['typeface']:
                    declarations.append({'part': name, 'element': node.tag.split('}')[-1], 'typeface': node.attrib['typeface']})
                if node.tag.endswith('}embeddedFontLst'):
                    embedded_lists.append({'part': name, 'entries': len(node)})
        slide_parts = [n for n in names if n.startswith('ppt/slides/slide') and n.endswith('.xml')]
        fonts = sorted({d['typeface'] for d in declarations if d['part'] in slide_parts})
        return {'file': path.name, 'sha256': hashlib.sha256(path.read_bytes()).hexdigest(),
                'slide_count': len(slide_parts), 'slide_typefaces': fonts,
                'embedded_font_parts': [n for n in names if n.startswith('ppt/fonts/')],
                'embedded_font_lists': embedded_lists, 'declarations': declarations}


if __name__ == '__main__':
    print(json.dumps([inspect(p) for p in sys.argv[1:]], indent=2))
