๐Ÿ“ฆ Stirling-Tools / Stirling-PDF

๐Ÿ“„ summarize_type3_signatures.py ยท 104 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104#!/usr/bin/env python3
"""
Summarize captured Type3 signature dumps as a Markdown inventory.

Usage:
    scripts/summarize_type3_signatures.py \
        --input docs/type3/signatures \
        --output docs/type3/signature_inventory.md
"""

from __future__ import annotations

import argparse
import json
from collections import defaultdict
from pathlib import Path
from typing import Dict, List


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(
        description="Summarize Type3 signature JSON dumps."
    )
    parser.add_argument(
        "--input",
        default="docs/type3/signatures",
        help="Directory containing signature JSON files (default: %(default)s)",
    )
    parser.add_argument(
        "--output",
        default="docs/type3/signature_inventory.md",
        help="Markdown file to write (default: %(default)s)",
    )
    return parser.parse_args()


def load_signatures(directory: Path) -> Dict[str, List[dict]]:
    inventory: Dict[str, List[dict]] = defaultdict(list)
    for path in sorted(directory.glob("*.json")):
        with path.open("r", encoding="utf-8") as handle:
            payload = json.load(handle)
        source_pdf = payload.get("pdf") or path.name
        for font in payload.get("fonts", []):
            alias = (font.get("alias") or font.get("baseName") or "unknown").lower()
            entry = {
                "source": source_pdf,
                "file": path.name,
                "alias": alias,
                "baseName": font.get("baseName"),
                "signature": font.get("signature"),
                "glyphCount": font.get("glyphCount"),
                "glyphCoverage": font.get("glyphCoverage"),
            }
            inventory[alias].append(entry)
    return inventory


def write_markdown(
    inventory: Dict[str, List[dict]], output: Path, input_dir: Path
) -> None:
    lines: List[str] = []
    lines.append("# Type3 Signature Inventory")
    lines.append("")
    lines.append(
        f"_Generated from `{input_dir}`. "
        "Run `scripts/summarize_type3_signatures.py` after capturing new samples._"
    )
    lines.append("")

    for alias in sorted(inventory.keys()):
        entries = inventory[alias]
        lines.append(f"## Alias: `{alias}`")
        lines.append("")
        lines.append("| Signature | Samples | Glyph Count | Coverage (first 10) |")
        lines.append("| --- | --- | --- | --- |")
        for entry in entries:
            signature = entry.get("signature") or "โ€”"
            sample = Path(entry["source"]).name
            glyph_count = (
                entry.get("glyphCount") if entry.get("glyphCount") is not None else "โ€”"
            )
            coverage = entry.get("glyphCoverage") or []
            preview = ", ".join(str(code) for code in coverage[:10])
            lines.append(f"| `{signature}` | `{sample}` | {glyph_count} | {preview} |")
        lines.append("")

    output.parent.mkdir(parents=True, exist_ok=True)
    output.write_text("\n".join(lines), encoding="utf-8")


def main() -> None:
    args = parse_args()
    input_dir = Path(args.input)
    if not input_dir.exists():
        raise SystemExit(f"Input directory not found: {input_dir}")
    inventory = load_signatures(input_dir)
    output_path = Path(args.output)
    write_markdown(inventory, output_path, input_dir)
    print(f"Wrote inventory for {len(inventory)} aliases to {output_path}")


if __name__ == "__main__":
    main()