odforge 0.3.0__py3-none-any.whl

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.
Files changed (66) hide show
  1. odforge/__init__.py +1 -0
  2. odforge/benchmark.py +287 -0
  3. odforge/check.py +416 -0
  4. odforge/cli.py +1027 -0
  5. odforge/components.py +270 -0
  6. odforge/critic.py +1260 -0
  7. odforge/docfonts.py +104 -0
  8. odforge/editor_api.py +33 -0
  9. odforge/editor_launch.py +134 -0
  10. odforge/editor_routes.py +131 -0
  11. odforge/extract.py +478 -0
  12. odforge/feedback.py +86 -0
  13. odforge/fontembed.py +166 -0
  14. odforge/ir.py +1181 -0
  15. odforge/layoutcheck.py +602 -0
  16. odforge/llm.py +1865 -0
  17. odforge/localguard.py +42 -0
  18. odforge/mcp_server.py +552 -0
  19. odforge/media.py +352 -0
  20. odforge/odpedit.py +1219 -0
  21. odforge/package.py +114 -0
  22. odforge/pagefacts.py +388 -0
  23. odforge/preview.py +104 -0
  24. odforge/references.py +153 -0
  25. odforge/render/__init__.py +40 -0
  26. odforge/render/odp.py +5687 -0
  27. odforge/render/ods.py +155 -0
  28. odforge/render/odt.py +223 -0
  29. odforge/scene.py +657 -0
  30. odforge/setup_agents.py +274 -0
  31. odforge/skills/odforge-design/SKILL.md +298 -0
  32. odforge/skills/odforge-design/themes/academic.md +52 -0
  33. odforge/skills/odforge-design/themes/clay.md +53 -0
  34. odforge/skills/odforge-design/themes/color-system.md +59 -0
  35. odforge/skills/odforge-design/themes/crimson.md +53 -0
  36. odforge/skills/odforge-design/themes/dark.md +53 -0
  37. odforge/skills/odforge-design/themes/forest.md +54 -0
  38. odforge/skills/odforge-design/themes/gold.md +53 -0
  39. odforge/skills/odforge-design/themes/minimal.md +53 -0
  40. odforge/skills/odforge-design/themes/navy.md +56 -0
  41. odforge/skills/odforge-design/themes/plum.md +53 -0
  42. odforge/skills/odforge-design/themes/sky.md +53 -0
  43. odforge/skills/odforge-design/themes/slate.md +53 -0
  44. odforge/skills/odforge-design/themes/teal.md +53 -0
  45. odforge/skills/odforge-design/themes/violet.md +56 -0
  46. odforge/templates.py +250 -0
  47. odforge/textmetrics.py +616 -0
  48. odforge/textutil.py +24 -0
  49. odforge/themes.py +886 -0
  50. odforge/validate.py +403 -0
  51. odforge/watch.py +266 -0
  52. odforge/webapi.py +2617 -0
  53. odforge/webui/assets/editor-CjpYXM1a.css +1 -0
  54. odforge/webui/assets/editor-DPToJGio.js +3 -0
  55. odforge/webui/assets/jsx-runtime-C7KMcVI_.js +8 -0
  56. odforge/webui/assets/main-BLOs7Hnk.js +7 -0
  57. odforge/webui/assets/main-D21yFVE6.css +1 -0
  58. odforge/webui/editor.html +14 -0
  59. odforge/webui/index.html +25 -0
  60. odforge/xmlsafe.py +46 -0
  61. odforge/zipguard.py +91 -0
  62. odforge-0.3.0.dist-info/METADATA +95 -0
  63. odforge-0.3.0.dist-info/RECORD +66 -0
  64. odforge-0.3.0.dist-info/WHEEL +4 -0
  65. odforge-0.3.0.dist-info/entry_points.txt +3 -0
  66. odforge-0.3.0.dist-info/licenses/LICENSE +21 -0
odforge/fontembed.py ADDED
@@ -0,0 +1,166 @@
1
+ """Embed subset CJK fonts into an ODF package.
2
+
3
+ Why this exists: a deck names "Noto Sans TC" / "Noto Serif TC", but the machine
4
+ that opens it often does not have them — or has the files without a working
5
+ registration. LibreOffice then substitutes a fallback face and *synthesises*
6
+ bold by smearing the regular outline, which turns dense glyphs (量、實、覺、讓)
7
+ into blots, and the serif display face silently becomes a heavy sans. Every
8
+ generated deck looked worse on the reviewer's machine than on ours.
9
+
10
+ ODF can carry its fonts (``<svg:font-face-src>`` pointing at ``Fonts/…`` parts,
11
+ which LibreOffice loads on open). Shipping four full Noto TC files would add
12
+ ~28MB to every deck, so each face is **subset to the characters the deck
13
+ actually uses** — typically a few hundred KB.
14
+
15
+ Everything here degrades gracefully: without fontTools, or without the source
16
+ font files on this machine, no font is embedded and the deck renders exactly
17
+ as before.
18
+ """
19
+
20
+ from __future__ import annotations
21
+
22
+ import io
23
+ import logging
24
+ import os
25
+ from dataclasses import dataclass
26
+ from functools import cache
27
+ from pathlib import Path
28
+
29
+ # (family, bold) -> file name, as distributed by Google Fonts / notofonts.
30
+ _FILES: dict[tuple[str, bool], tuple[str, ...]] = {
31
+ ("Noto Sans TC", False): ("NotoSansTC-Regular.otf", "NotoSansTC-Regular.ttf"),
32
+ ("Noto Sans TC", True): ("NotoSansTC-Bold.otf", "NotoSansTC-Bold.ttf"),
33
+ ("Noto Serif TC", False): ("NotoSerifTC-Regular.otf", "NotoSerifTC-Regular.ttf"),
34
+ ("Noto Serif TC", True): ("NotoSerifTC-Bold.otf", "NotoSerifTC-Bold.ttf"),
35
+ }
36
+
37
+ # Characters the renderer itself draws (bullets, ellipses, page numbers,
38
+ # quote marks) on top of whatever the deck's text contains.
39
+ _ALWAYS = (
40
+ "".join(chr(c) for c in range(0x20, 0x7F))
41
+ + "▪•·…—–“”‘’「」『』()、,。:;!?%+-×→←↑↓≥≤~/"
42
+ )
43
+
44
+
45
+ @dataclass(frozen=True)
46
+ class EmbeddedFont:
47
+ family: str
48
+ bold: bool
49
+ part: str # package path, e.g. "Fonts/NotoSansTC-Bold.otf"
50
+ data: bytes
51
+ fmt: str # "opentype" | "truetype"
52
+
53
+
54
+ def _search_dirs() -> list[Path]:
55
+ dirs = []
56
+ if env := os.environ.get("ODFORGE_FONT_DIR"):
57
+ dirs.append(Path(env))
58
+ home = Path.home()
59
+ dirs += [
60
+ home / "Library/Fonts",
61
+ Path("/Library/Fonts"),
62
+ home / ".local/share/fonts",
63
+ home / ".fonts",
64
+ Path("/usr/share/fonts"),
65
+ Path("/usr/local/share/fonts"),
66
+ ]
67
+ if windir := os.environ.get("WINDIR"):
68
+ dirs.append(Path(windir) / "Fonts")
69
+ if local := os.environ.get("LOCALAPPDATA"):
70
+ dirs.append(Path(local) / "Microsoft/Windows/Fonts")
71
+ return [d for d in dirs if d.is_dir()]
72
+
73
+
74
+ @cache
75
+ def find_font(family: str, bold: bool) -> Path | None:
76
+ """Locate the source file for a face, searching the usual font folders
77
+ (recursively, since Linux distros nest them). ``None`` when absent."""
78
+ names = _FILES.get((family, bold))
79
+ if not names:
80
+ return None
81
+ for directory in _search_dirs():
82
+ for name in names:
83
+ direct = directory / name
84
+ if direct.is_file():
85
+ return direct
86
+ for name in names:
87
+ found = next(directory.rglob(name), None)
88
+ if found is not None:
89
+ return found
90
+ return None
91
+
92
+
93
+ def _subset(path: Path, text: str) -> tuple[bytes, str] | None:
94
+ try:
95
+ from fontTools import subset
96
+ from fontTools.ttLib import TTFont
97
+ except ImportError: # optional dependency
98
+ return None
99
+ # fontTools narrates every table it prunes at INFO; under a host that logs
100
+ # INFO (the MCP server does) one subset run is half a megabyte of noise.
101
+ logging.getLogger("fontTools").setLevel(logging.WARNING)
102
+ options = subset.Options()
103
+ # Keep every name record: LibreOffice matches the embedded face to the
104
+ # document's font-face by family name.
105
+ options.name_IDs = ["*"]
106
+ options.name_languages = ["*"]
107
+ options.notdef_outline = True
108
+ options.hinting = False
109
+ options.layout_features = ["*"]
110
+ font = TTFont(path, lazy=False)
111
+ subsetter = subset.Subsetter(options)
112
+ subsetter.populate(text=text)
113
+ subsetter.subset(font)
114
+ buf = io.BytesIO()
115
+ font.save(buf)
116
+ fmt = "opentype" if "CFF " in font or "CFF2" in font else "truetype"
117
+ return buf.getvalue(), fmt
118
+
119
+
120
+ def embed_fonts(families: set[str], text: str) -> list[EmbeddedFont]:
121
+ """Subset regular + bold of each family to ``text`` (plus the glyphs the
122
+ renderer draws itself). Families without a source file are skipped."""
123
+ chars = "".join(sorted(set(text) | set(_ALWAYS)))
124
+ out: list[EmbeddedFont] = []
125
+ for family in sorted(families):
126
+ for bold in (False, True):
127
+ path = find_font(family, bold)
128
+ if path is None:
129
+ continue
130
+ result = _subset(path, chars)
131
+ if result is None:
132
+ return []
133
+ data, fmt = result
134
+ ext = ".otf" if fmt == "opentype" else ".ttf"
135
+ out.append(
136
+ EmbeddedFont(family, bold, f"Fonts/{path.stem}{ext}", data, fmt)
137
+ )
138
+ return out
139
+
140
+
141
+ def font_face_decls(
142
+ families: list[str], embedded: list[EmbeddedFont], escape_attr
143
+ ) -> str:
144
+ """``<style:font-face>`` elements for ``families``, each pointing at its
145
+ embedded parts when there are any."""
146
+ by_family: dict[str, list[EmbeddedFont]] = {}
147
+ for font in embedded:
148
+ by_family.setdefault(font.family, []).append(font)
149
+ parts = []
150
+ for family in families:
151
+ name = escape_attr(family)
152
+ uris = "".join(
153
+ f'<svg:font-face-uri xlink:href="{escape_attr(f.part)}" xlink:type="simple"'
154
+ f' loext:font-style="normal"'
155
+ f' loext:font-weight="{"bold" if f.bold else "normal"}">'
156
+ f'<svg:font-face-format svg:string="{f.fmt}"/>'
157
+ f"</svg:font-face-uri>"
158
+ for f in by_family.get(family, [])
159
+ )
160
+ src = f"<svg:font-face-src>{uris}</svg:font-face-src>" if uris else ""
161
+ parts.append(
162
+ f'<style:font-face style:name="{name}"'
163
+ f" svg:font-family=\"'{name}'\" style:font-pitch=\"variable\">"
164
+ f"{src}</style:font-face>"
165
+ )
166
+ return "".join(parts)