pydfig 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
pydfig-0.1.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Fenglong Li, Siyang Li
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
pydfig-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,154 @@
1
+ Metadata-Version: 2.4
2
+ Name: pydfig
3
+ Version: 0.1.0
4
+ Summary: Self-describing scientific figures (Figure-as-Data): embed exact data in SVG/PNG, decode losslessly to JSON (for AI) and HTML (for humans).
5
+ Author-email: Fenglong Li <15340666394@163.com>, Siyang Li <15340666394@163.com>
6
+ License: MIT
7
+ Project-URL: Homepage, https://pydfig.ai4c/schema
8
+ Keywords: scientific figures,Figure-as-Data,FAIR data,machine-readable,AI4Science,SVG,PNG,metadata
9
+ Classifier: Development Status :: 4 - Beta
10
+ Classifier: Intended Audience :: Science/Research
11
+ Classifier: License :: OSI Approved :: MIT License
12
+ Classifier: Programming Language :: Python :: 3
13
+ Classifier: Programming Language :: Python :: 3.8
14
+ Classifier: Programming Language :: Python :: 3.9
15
+ Classifier: Programming Language :: Python :: 3.10
16
+ Classifier: Programming Language :: Python :: 3.11
17
+ Classifier: Programming Language :: Python :: 3.12
18
+ Classifier: Programming Language :: Python :: 3.13
19
+ Classifier: Topic :: Scientific/Engineering :: Visualization
20
+ Requires-Python: >=3.8
21
+ Description-Content-Type: text/markdown
22
+ License-File: LICENSE
23
+ Provides-Extra: matplotlib
24
+ Requires-Dist: matplotlib; extra == "matplotlib"
25
+ Dynamic: license-file
26
+
27
+ # pydfig — Make your scientific figures carry their own data
28
+
29
+ A normal chart is just a **picture**. **pydfig** makes the same chart also
30
+ **contain its exact numbers** — so a computer can read them back perfectly,
31
+ without guessing from pixels (no OCR, no digitizing error).
32
+
33
+ > One sentence: pydfig hides a small JSON with your exact data *inside* the
34
+ > image file. The picture still looks identical; a parser reads the JSON back
35
+ > losslessly.
36
+
37
+ ## Why does this matter?
38
+
39
+ Scientific figures in papers and PDFs are images. If an AI agent — or a
40
+ colleague — wants the real data points, they usually have to scrape the PDF
41
+ or eyeball the pixels, and both lose precision. pydfig embeds the **exact
42
+ data** inside the figure file itself, invisibly.
43
+
44
+ ## How it works
45
+
46
+ | Carrier | Where the data lives | Still a normal file? |
47
+ |---------|----------------------|----------------------|
48
+ | **SVG** | the standard `<metadata>` element (renderers ignore it) | ✅ opens in any SVG viewer |
49
+ | **PNG** | a legal private chunk called `dDat` | ✅ opens in any image viewer |
50
+
51
+ ## Two things pydfig does
52
+
53
+ 1. **Encode** — export a PNG or SVG that carries your exact data (from a
54
+ `payload` dict, or directly from a matplotlib figure).
55
+ 2. **Decode** — read a pydfig PNG/SVG and write out:
56
+ - a **`.json`** file — machine-readable, for AI / data pipelines
57
+ - an **`.html`** file — human-readable, opens in any browser and shows the
58
+ data plus a reconstructed plot
59
+
60
+ ## Install
61
+
62
+ ```bash
63
+ pip install pydfig
64
+ # optional: to export directly from a matplotlib figure
65
+ pip install "pydfig[matplotlib]"
66
+ ```
67
+
68
+ The core encode/decode path is **pure Python standard library** — no heavy
69
+ dependencies required.
70
+
71
+ ## Quick start
72
+
73
+ ### Command line
74
+
75
+ ```bash
76
+ # Encode: inject a data JSON into an existing SVG or PNG
77
+ python -m pydfig encode chart.png --payload payload.json -o chart_pydfig.png
78
+
79
+ # Decode: get back JSON (for AI) + HTML (for humans)
80
+ python -m pydfig decode chart_pydfig.png -o result
81
+ # result.json <- exact data, machine-readable
82
+ # result.html <- open in a browser to see the data and a reconstructed chart
83
+ ```
84
+
85
+ ### In Python
86
+
87
+ ```python
88
+ from pydfig import build_payload, embed_in_svg, inject_png_ddat, decode_any
89
+
90
+ payload = build_payload(
91
+ "scatter",
92
+ points=[{"x": -1.95, "y": 0.42, "material": "NiO"}],
93
+ axes={"x": {"label": "d-band", "unit": "eV"}},
94
+ caption="My figure",
95
+ )
96
+
97
+ svg = embed_in_svg(open("chart.svg").read(), payload) # data in <metadata>
98
+ png = inject_png_ddat(open("chart.png", "rb").read(), payload) # data in dDat chunk
99
+
100
+ data = decode_any("chart_pydfig.png") # exact, lossless
101
+ ```
102
+
103
+ ### From a matplotlib figure (needs the `matplotlib` extra)
104
+
105
+ ```python
106
+ import matplotlib.pyplot as plt
107
+ from pydfig.matplotlib_converter import convert
108
+
109
+ fig, ax = plt.subplots()
110
+ ax.plot([1, 2, 3], [4, 5, 6])
111
+ ax.set_xlabel("d-band (eV)")
112
+ ax.set_ylabel("E_ads (eV)")
113
+
114
+ convert(fig, "out.svg", caption="experiment") # -> out.svg
115
+ convert(fig, "out.png", caption="experiment") # -> out.png
116
+ ```
117
+
118
+ `convert()` chooses PNG vs SVG automatically from the output file extension.
119
+
120
+ ### Decode to a readable HTML page
121
+
122
+ ```python
123
+ from pydfig import decode_any, write_html
124
+
125
+ payload = decode_any("out.png")
126
+ write_html(payload, "out.html") # self-contained, light-theme viewer
127
+ ```
128
+
129
+ ## What the embedded data looks like
130
+
131
+ ```json
132
+ {
133
+ "format": "pydfig",
134
+ "version": "2.0",
135
+ "figure_type": "xrd",
136
+ "caption": "XRD pattern",
137
+ "axes": {"x": {"label": "2θ", "unit": "degree"}, "y": {"label": "Intensity"}},
138
+ "points": [{"x": 44.0, "y": 20.0}, {"x": 64.0, "y": 12.0}],
139
+ "curves": [{"x": [0, 1, 2], "y": [10, 12, 11], "label": "intensity"}]
140
+ }
141
+ ```
142
+
143
+ ## Examples
144
+
145
+ The `examples/` folder ships `demo_volcano`, `demo_xrd`, and `demo_dband` as
146
+ both `.svg` and `.png` — each carrying its exact data. Regenerate them with:
147
+
148
+ ```bash
149
+ python examples/gen_examples.py
150
+ ```
151
+
152
+ ## License
153
+
154
+ MIT — see [LICENSE](LICENSE).
pydfig-0.1.0/README.md ADDED
@@ -0,0 +1,128 @@
1
+ # pydfig — Make your scientific figures carry their own data
2
+
3
+ A normal chart is just a **picture**. **pydfig** makes the same chart also
4
+ **contain its exact numbers** — so a computer can read them back perfectly,
5
+ without guessing from pixels (no OCR, no digitizing error).
6
+
7
+ > One sentence: pydfig hides a small JSON with your exact data *inside* the
8
+ > image file. The picture still looks identical; a parser reads the JSON back
9
+ > losslessly.
10
+
11
+ ## Why does this matter?
12
+
13
+ Scientific figures in papers and PDFs are images. If an AI agent — or a
14
+ colleague — wants the real data points, they usually have to scrape the PDF
15
+ or eyeball the pixels, and both lose precision. pydfig embeds the **exact
16
+ data** inside the figure file itself, invisibly.
17
+
18
+ ## How it works
19
+
20
+ | Carrier | Where the data lives | Still a normal file? |
21
+ |---------|----------------------|----------------------|
22
+ | **SVG** | the standard `<metadata>` element (renderers ignore it) | ✅ opens in any SVG viewer |
23
+ | **PNG** | a legal private chunk called `dDat` | ✅ opens in any image viewer |
24
+
25
+ ## Two things pydfig does
26
+
27
+ 1. **Encode** — export a PNG or SVG that carries your exact data (from a
28
+ `payload` dict, or directly from a matplotlib figure).
29
+ 2. **Decode** — read a pydfig PNG/SVG and write out:
30
+ - a **`.json`** file — machine-readable, for AI / data pipelines
31
+ - an **`.html`** file — human-readable, opens in any browser and shows the
32
+ data plus a reconstructed plot
33
+
34
+ ## Install
35
+
36
+ ```bash
37
+ pip install pydfig
38
+ # optional: to export directly from a matplotlib figure
39
+ pip install "pydfig[matplotlib]"
40
+ ```
41
+
42
+ The core encode/decode path is **pure Python standard library** — no heavy
43
+ dependencies required.
44
+
45
+ ## Quick start
46
+
47
+ ### Command line
48
+
49
+ ```bash
50
+ # Encode: inject a data JSON into an existing SVG or PNG
51
+ python -m pydfig encode chart.png --payload payload.json -o chart_pydfig.png
52
+
53
+ # Decode: get back JSON (for AI) + HTML (for humans)
54
+ python -m pydfig decode chart_pydfig.png -o result
55
+ # result.json <- exact data, machine-readable
56
+ # result.html <- open in a browser to see the data and a reconstructed chart
57
+ ```
58
+
59
+ ### In Python
60
+
61
+ ```python
62
+ from pydfig import build_payload, embed_in_svg, inject_png_ddat, decode_any
63
+
64
+ payload = build_payload(
65
+ "scatter",
66
+ points=[{"x": -1.95, "y": 0.42, "material": "NiO"}],
67
+ axes={"x": {"label": "d-band", "unit": "eV"}},
68
+ caption="My figure",
69
+ )
70
+
71
+ svg = embed_in_svg(open("chart.svg").read(), payload) # data in <metadata>
72
+ png = inject_png_ddat(open("chart.png", "rb").read(), payload) # data in dDat chunk
73
+
74
+ data = decode_any("chart_pydfig.png") # exact, lossless
75
+ ```
76
+
77
+ ### From a matplotlib figure (needs the `matplotlib` extra)
78
+
79
+ ```python
80
+ import matplotlib.pyplot as plt
81
+ from pydfig.matplotlib_converter import convert
82
+
83
+ fig, ax = plt.subplots()
84
+ ax.plot([1, 2, 3], [4, 5, 6])
85
+ ax.set_xlabel("d-band (eV)")
86
+ ax.set_ylabel("E_ads (eV)")
87
+
88
+ convert(fig, "out.svg", caption="experiment") # -> out.svg
89
+ convert(fig, "out.png", caption="experiment") # -> out.png
90
+ ```
91
+
92
+ `convert()` chooses PNG vs SVG automatically from the output file extension.
93
+
94
+ ### Decode to a readable HTML page
95
+
96
+ ```python
97
+ from pydfig import decode_any, write_html
98
+
99
+ payload = decode_any("out.png")
100
+ write_html(payload, "out.html") # self-contained, light-theme viewer
101
+ ```
102
+
103
+ ## What the embedded data looks like
104
+
105
+ ```json
106
+ {
107
+ "format": "pydfig",
108
+ "version": "2.0",
109
+ "figure_type": "xrd",
110
+ "caption": "XRD pattern",
111
+ "axes": {"x": {"label": "2θ", "unit": "degree"}, "y": {"label": "Intensity"}},
112
+ "points": [{"x": 44.0, "y": 20.0}, {"x": 64.0, "y": 12.0}],
113
+ "curves": [{"x": [0, 1, 2], "y": [10, 12, 11], "label": "intensity"}]
114
+ }
115
+ ```
116
+
117
+ ## Examples
118
+
119
+ The `examples/` folder ships `demo_volcano`, `demo_xrd`, and `demo_dband` as
120
+ both `.svg` and `.png` — each carrying its exact data. Regenerate them with:
121
+
122
+ ```bash
123
+ python examples/gen_examples.py
124
+ ```
125
+
126
+ ## License
127
+
128
+ MIT — see [LICENSE](LICENSE).
@@ -0,0 +1,43 @@
1
+ """
2
+ pydfig — self-describing scientific figures (Figure-as-Data).
3
+
4
+ A pydfig figure looks like a normal SVG/PNG in any viewer, but carries its
5
+ exact underlying data as structured JSON that parsers and LLM agents read
6
+ losslessly — no OCR, no pixel digitization, no error.
7
+
8
+ * SVG -> JSON payload inside the standard W3C <metadata> element
9
+ (renderers ignore it; parsers/LLMs read it losslessly),
10
+ plus per-element `data-*` attribute redundancy.
11
+ * PNG -> JSON payload inside a private ancillary chunk `dDat`
12
+ (a legal PNG; any viewer opens it).
13
+
14
+ Pure standard library for encode/decode; matplotlib is an optional extra for
15
+ the matplotlib export helper.
16
+ """
17
+
18
+ from .decoder import (
19
+ decode_svg,
20
+ decode_png,
21
+ decode_any,
22
+ extract_datapoints,
23
+ )
24
+ from .encoder import (
25
+ build_payload,
26
+ embed_in_svg,
27
+ inject_png_ddat,
28
+ PYDFIG_NS,
29
+ SCHEMA_VERSION,
30
+ )
31
+ from .html import render_html, write_html
32
+ from .matplotlib_converter import convert, quick_convert
33
+
34
+ __version__ = "0.1.0"
35
+
36
+ __all__ = [
37
+ "decode_svg", "decode_png", "decode_any", "extract_datapoints",
38
+ "build_payload", "embed_in_svg", "inject_png_ddat",
39
+ "PYDFIG_NS", "SCHEMA_VERSION",
40
+ "render_html", "write_html",
41
+ "convert", "quick_convert",
42
+ "__version__",
43
+ ]
@@ -0,0 +1,7 @@
1
+ """Enable ``python -m pydfig ...``."""
2
+ import sys
3
+
4
+ from .cli import main
5
+
6
+ if __name__ == "__main__":
7
+ sys.exit(main())
@@ -0,0 +1,88 @@
1
+ """
2
+ pydfig.cli — command line interface.
3
+
4
+ Usage:
5
+ python -m pydfig encode <file.svg|file.png> --payload P.json [-o OUT]
6
+ inject a pydfig payload into an SVG or PNG file.
7
+
8
+ python -m pydfig decode <file.svg|file.png> [-o OUT_BASE]
9
+ decode the payload and write two files:
10
+ OUT_BASE.json — machine-readable JSON for AI / pipelines
11
+ OUT_BASE.html — human-friendly HTML view of the data
12
+ """
13
+
14
+ import argparse
15
+ import json
16
+ import os
17
+ import sys
18
+
19
+ from .decoder import decode_any
20
+ from .encoder import embed_in_svg, inject_png_ddat
21
+
22
+
23
+ def _info(payload):
24
+ ft = payload.get("figure_type", "?")
25
+ n_pts = len(payload.get("points", []))
26
+ n_curves = len(payload.get("curves", []))
27
+ return f"type={ft} points={n_pts} curves={n_curves}"
28
+
29
+
30
+ def main(argv=None):
31
+ ap = argparse.ArgumentParser(prog="pydfig", description="pydfig self-describing figure toolkit")
32
+ sub = ap.add_subparsers(dest="cmd", required=True)
33
+
34
+ p_enc = sub.add_parser("encode", help="inject a pydfig payload into an SVG or PNG")
35
+ p_enc.add_argument("file", help="input SVG or PNG file")
36
+ p_enc.add_argument("--payload", required=True, help="JSON file with the pydfig payload")
37
+ p_enc.add_argument("-o", "--out", default=None, help="output file (default: <input>_pydfig.<ext>)")
38
+
39
+ p_dec = sub.add_parser("decode", help="decode payload -> JSON (AI) + HTML (human)")
40
+ p_dec.add_argument("file", help="input SVG or PNG file")
41
+ p_dec.add_argument("-o", "--out", default=None, help="output base name (default: <input>_pydfig)")
42
+
43
+ args = ap.parse_args(argv)
44
+
45
+ if args.cmd == "encode":
46
+ with open(args.payload, encoding="utf-8") as f:
47
+ payload = json.load(f)
48
+ ext = os.path.splitext(args.file)[1].lower()
49
+ if ext == ".svg":
50
+ with open(args.file, "r", encoding="utf-8") as f:
51
+ svg_text = f.read()
52
+ out = args.out or (os.path.splitext(args.file)[0] + "_pydfig.svg")
53
+ with open(out, "w", encoding="utf-8") as f:
54
+ f.write(embed_in_svg(svg_text, payload))
55
+ elif ext == ".png":
56
+ with open(args.file, "rb") as f:
57
+ png_bytes = f.read()
58
+ out = args.out or (os.path.splitext(args.file)[0] + "_pydfig.png")
59
+ with open(out, "wb") as f:
60
+ f.write(inject_png_ddat(png_bytes, payload))
61
+ else:
62
+ print("unsupported input extension; use .svg or .png", file=sys.stderr)
63
+ return 1
64
+ print("pydfig payload injected ->", out)
65
+ return 0
66
+
67
+ if args.cmd == "decode":
68
+ payload = decode_any(args.file)
69
+ if payload is None:
70
+ print("no pydfig payload found in", args.file, file=sys.stderr)
71
+ return 1
72
+ base = args.out or (os.path.splitext(args.file)[0] + "_pydfig")
73
+ json_path = base + ".json"
74
+ html_path = base + ".html"
75
+ with open(json_path, "w", encoding="utf-8") as f:
76
+ json.dump(payload, f, ensure_ascii=False, indent=2)
77
+ from .html import write_html
78
+ write_html(payload, html_path)
79
+ print("decoded:", _info(payload))
80
+ print(" ->", json_path, "(machine-readable JSON)")
81
+ print(" ->", html_path, "(human-friendly HTML)")
82
+ return 0
83
+
84
+ return 1
85
+
86
+
87
+ if __name__ == "__main__":
88
+ sys.exit(main())
@@ -0,0 +1,109 @@
1
+ """
2
+ pydfig.decoder — read pydfig payloads from SVG / PNG files.
3
+
4
+ Two embedding channels are supported:
5
+
6
+ 1. SVG ``<metadata>`` JSON payload (primary channel).
7
+ 2. SVG per-element ``data-*`` attributes (redundancy channel; useful for
8
+ spot-checking and as a fallback when the metadata block is stripped).
9
+ 3. PNG private ancillary chunk ``dDat``.
10
+ """
11
+
12
+ import json
13
+ import re
14
+ import struct
15
+
16
+ _PNG_SIG = b"\x89PNG\r\n\x1a\n"
17
+
18
+
19
+ def _read_text(source):
20
+ """Accept a filesystem path, raw text, or bytes; return text."""
21
+ if isinstance(source, (bytes, bytearray)):
22
+ return bytes(source).decode("utf-8")
23
+ if isinstance(source, str) and source.lstrip().startswith("<"):
24
+ return source
25
+ with open(source, "r", encoding="utf-8") as f:
26
+ return f.read()
27
+
28
+
29
+ def decode_svg(source):
30
+ """Extract the pydfig JSON payload from an SVG file/string.
31
+
32
+ Returns the payload dict, or None if no <metadata> payload is present.
33
+ """
34
+ text = _read_text(source)
35
+ m = re.search(r"<metadata>(.*?)</metadata>", text, re.S)
36
+ if not m:
37
+ return None
38
+ inner = m.group(1)
39
+ cdata = re.search(r"<!\[CDATA\[(.*?)\]\]>", inner, re.S)
40
+ js = cdata.group(1) if cdata else re.sub(r"<[^>]+>", "", inner)
41
+ return json.loads(js)
42
+
43
+
44
+ def extract_datapoints(source):
45
+ """Extract per-element ``data-*`` attributes (the redundancy channel).
46
+
47
+ Every graphical element a pydfig encoder produces carries its own copy of
48
+ the underlying values, e.g. ``<circle ... data-x="-1.95" data-y="0.42"
49
+ data-material="NiO"/>``. This function collects them all.
50
+
51
+ Returns a list of dicts; numeric-looking values are converted to float.
52
+ """
53
+ text = _read_text(source)
54
+ out = []
55
+ for m in re.finditer(r"<(circle|rect|path|line|polyline|text)\b[^>]*>", text):
56
+ tag = m.group(0)
57
+ attrs = dict(re.findall(r'(data-[\w-]+)="([^"]*)"', tag))
58
+ if not attrs:
59
+ continue
60
+ rec = {}
61
+ for k, v in attrs.items():
62
+ key = k[len("data-"):]
63
+ try:
64
+ rec[key] = float(v)
65
+ except ValueError:
66
+ rec[key] = v
67
+ out.append(rec)
68
+ return out
69
+
70
+
71
+ def decode_png(source):
72
+ """Extract the JSON payload from a PNG ``dDat`` private chunk.
73
+
74
+ Accepts a path or raw PNG bytes. Returns the payload dict or None.
75
+ """
76
+ if isinstance(source, (bytes, bytearray)):
77
+ data = bytes(source)
78
+ else:
79
+ with open(source, "rb") as f:
80
+ data = f.read()
81
+ if data[:8] != _PNG_SIG:
82
+ raise ValueError("not a PNG/pydfig file")
83
+ i = 8
84
+ while i < len(data):
85
+ length = struct.unpack(">I", data[i:i + 4])[0]
86
+ ctype = data[i + 4:i + 8]
87
+ cdata = data[i + 8:i + 8 + length]
88
+ if ctype == b"dDat":
89
+ return json.loads(cdata.decode("utf-8"))
90
+ i += 12 + length
91
+ return None
92
+
93
+
94
+ def decode_any(source):
95
+ """Auto-detect the container and decode the payload.
96
+
97
+ Dispatch order: PNG signature -> SVG text probe.
98
+ Returns the payload dict or None.
99
+ """
100
+ if isinstance(source, (bytes, bytearray)) and bytes(source)[:8] == _PNG_SIG:
101
+ return decode_png(source)
102
+ if isinstance(source, str):
103
+ if source.lstrip().startswith("<"):
104
+ return decode_svg(source)
105
+ with open(source, "rb") as f:
106
+ head = f.read(8)
107
+ if head == _PNG_SIG:
108
+ return decode_png(source)
109
+ return decode_svg(source)
@@ -0,0 +1,103 @@
1
+ """
2
+ pydfig.encoder — minimal pydfig writer (pure standard library).
3
+
4
+ Embedding primitives:
5
+
6
+ * ``embed_in_svg`` : insert a <metadata> JSON payload into existing SVG text
7
+ * ``inject_png_ddat``: insert a `dDat` private chunk into existing PNG bytes
8
+
9
+ A pydfig figure looks identical to a normal SVG/PNG in any viewer, but carries
10
+ its exact underlying data as structured JSON that parsers and LLM agents read
11
+ losslessly.
12
+ """
13
+
14
+ import json
15
+ import re
16
+ import struct
17
+ import zlib
18
+ from datetime import datetime, timezone
19
+
20
+ PYDFIG_NS = "https://pydfig.ai4c/schema"
21
+ SCHEMA_VERSION = "2.0"
22
+
23
+ _PNG_SIG = b"\x89PNG\r\n\x1a\n"
24
+
25
+
26
+ def build_payload(figure_type, points=None, curves=None, axes=None,
27
+ provenance=None, extra=None, caption=None, version=SCHEMA_VERSION):
28
+ """Construct a pydfig metadata payload.
29
+
30
+ Parameters
31
+ ----------
32
+ figure_type : str Semantic figure type (scatter, line, xrd, volcano, ...).
33
+ points : list Exact data points (dicts with numeric x, y).
34
+ curves : list Continuous curves as {"x": [...], "y": [...], "label": ...}.
35
+ axes : dict {"x": {"label", "unit", "range"}, "y": {...}}.
36
+ provenance : dict Free-form domain/technique/method/project metadata.
37
+ extra : dict Domain-specific extension fields.
38
+ caption : str Human-readable figure caption.
39
+ """
40
+ payload = {
41
+ "format": "pydfig",
42
+ "version": version,
43
+ "generated_at": datetime.now(timezone.utc).isoformat(),
44
+ "figure_type": figure_type,
45
+ "caption": caption or "",
46
+ "axes": axes or {},
47
+ "points": points or [],
48
+ "curves": curves or [],
49
+ "provenance": provenance or {},
50
+ }
51
+ if extra:
52
+ payload["extra"] = extra
53
+ return payload
54
+
55
+
56
+ def metadata_block(payload):
57
+ """Render the payload as an SVG <metadata> block (JSON inside CDATA)."""
58
+ js = json.dumps(payload, ensure_ascii=False, indent=2)
59
+ return (
60
+ "<metadata>\n"
61
+ f' <pydfig:payload xmlns:pydfig="{PYDFIG_NS}" format="pydfig" '
62
+ f'version="{payload.get("version", SCHEMA_VERSION)}">\n'
63
+ f" <![CDATA[\n{js}\n]]>\n"
64
+ " </pydfig:payload>\n"
65
+ "</metadata>\n"
66
+ )
67
+
68
+
69
+ def embed_in_svg(svg_text, payload):
70
+ """Insert a pydfig <metadata> block right after the root <svg ...> tag.
71
+
72
+ The visual content is untouched: every compliant SVG renderer ignores
73
+ <metadata>, so the figure looks identical while becoming machine-readable.
74
+ """
75
+ if "<metadata>" in svg_text:
76
+ raise ValueError("SVG already contains a <metadata> block; refusing to duplicate")
77
+ m = re.search(r"<svg\b[^>]*>", svg_text)
78
+ if m is None:
79
+ raise ValueError("no root <svg> tag found")
80
+ insert_at = m.end()
81
+ return svg_text[:insert_at] + metadata_block(payload) + svg_text[insert_at:]
82
+
83
+
84
+ def _chunk(chunk_type, data):
85
+ return (struct.pack(">I", len(data)) + chunk_type + data
86
+ + struct.pack(">I", zlib.crc32(chunk_type + data) & 0xFFFFFFFF))
87
+
88
+
89
+ def inject_png_ddat(png_bytes, payload):
90
+ """Insert a `dDat` private chunk (JSON payload) before IEND; return new PNG bytes.
91
+
92
+ `dDat` is a legal ancillary/private PNG chunk (lowercase second letter),
93
+ so the result remains a valid PNG that any viewer can open.
94
+ """
95
+ png_bytes = bytes(png_bytes)
96
+ if png_bytes[:8] != _PNG_SIG:
97
+ raise ValueError("not a PNG file")
98
+ idx = png_bytes.rfind(b"IEND")
99
+ if idx < 4:
100
+ raise ValueError("PNG has no IEND chunk")
101
+ chunk_start = idx - 4 # step back to the IEND length field
102
+ ddat = _chunk(b"dDat", json.dumps(payload, ensure_ascii=False).encode("utf-8"))
103
+ return png_bytes[:chunk_start] + ddat + png_bytes[chunk_start:]