dknowc-dsh 1.0.4 → 1.1.1
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.
- package/README.md +9 -4
- package/package.json +2 -2
- package/skills/dknowc-ppt-assistant/README.md +54 -0
- package/skills/dknowc-ppt-assistant/SKILL.md +151 -0
- package/skills/dknowc-ppt-assistant/THIRD_PARTY_NOTICES.md +36 -0
- package/skills/dknowc-ppt-assistant/official-docs/output/.gitkeep +0 -0
- package/skills/dknowc-ppt-assistant/official-docs/search-results/.gitkeep +0 -0
- package/skills/dknowc-ppt-assistant/projects/.gitkeep +0 -0
- package/skills/dknowc-ppt-assistant/references/content-pack.md +55 -0
- package/skills/dknowc-ppt-assistant/references/material_usage.md +51 -0
- package/skills/dknowc-ppt-assistant/references/sample_effect.html +89 -0
- package/skills/dknowc-ppt-assistant/references/sample_search_result.md +48 -0
- package/skills/dknowc-ppt-assistant/references/search_intro.md +79 -0
- package/skills/dknowc-ppt-assistant/references/style-presets.md +126 -0
- package/skills/dknowc-ppt-assistant/references/svg-authoring.md +81 -0
- package/skills/dknowc-ppt-assistant/references/upstream-docs/svg-pipeline.md +842 -0
- package/skills/dknowc-ppt-assistant/references/upstream-example/01_cover.svg +28 -0
- package/skills/dknowc-ppt-assistant/references/upstream-example/02_quote.svg +38 -0
- package/skills/dknowc-ppt-assistant/references/upstream-example/03_origin.svg +96 -0
- package/skills/dknowc-ppt-assistant/references/upstream-example/README.md +15 -0
- package/skills/dknowc-ppt-assistant/references/upstream-example/design_spec.md +408 -0
- package/skills/dknowc-ppt-assistant/references/upstream-example/spec_lock.md +78 -0
- package/skills/dknowc-ppt-assistant/scripts/adapt_mcp_result.py +279 -0
- package/skills/dknowc-ppt-assistant/scripts/check_release.py +106 -0
- package/skills/dknowc-ppt-assistant/scripts/compact_svg_coordinates.py +345 -0
- package/skills/dknowc-ppt-assistant/scripts/config.py +707 -0
- package/skills/dknowc-ppt-assistant/scripts/console_encoding.py +33 -0
- package/skills/dknowc-ppt-assistant/scripts/error_helper.py +434 -0
- package/skills/dknowc-ppt-assistant/scripts/finalize_svg.py +507 -0
- package/skills/dknowc-ppt-assistant/scripts/hyperlink_contract.py +399 -0
- package/skills/dknowc-ppt-assistant/scripts/initialize.py +182 -0
- package/skills/dknowc-ppt-assistant/scripts/language_tags.py +283 -0
- package/skills/dknowc-ppt-assistant/scripts/mcp_direct.py +158 -0
- package/skills/dknowc-ppt-assistant/scripts/native_payloads.py +655 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_animation_presets.json +3440 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_animations.py +3965 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_effects.py +143 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_opc_validation.py +346 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_shapes/__init__.py +130 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_shapes/data/LICENSE-APACHE-2.0.txt +202 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_shapes/data/LICENSE-OPEN-XML-SDK-MIT.txt +23 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_shapes/data/NOTICE.md +54 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_shapes/data/presetShapeDefinitions.xml +19898 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_shapes/data/presetShapeSemantics.json +1217 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_shapes/data/shape_type_values.txt +187 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_shapes/errors.py +39 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_shapes/formula.py +336 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_shapes/loader.py +364 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_shapes/models.py +201 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_shapes/registry.py +270 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_shapes/semantic_hash.py +456 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_shapes/semantics.py +543 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_shapes/xml_safety.py +21 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/__init__.py +21 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/animation_import.py +287 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/chart_to_svg.py +2377 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/chartex_to_svg.py +422 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/color_resolver.py +894 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/converter.py +1143 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/custgeom_to_svg.py +165 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/effect_to_svg.py +410 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/emu_units.py +258 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/fill_to_svg.py +816 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/formula_import.py +611 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/hyperlinks.py +93 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/import_diagnostics.py +37 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/ln_to_svg.py +362 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/normalized_chart_svg.py +1217 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/ooxml_loader.py +506 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/pic_to_svg.py +653 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/preset_authoring.py +894 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/preset_registry_to_svg.py +216 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/preset_svg_markup.py +198 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/prstgeom_to_svg.py +162 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/shape_walker.py +556 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/slide_to_svg.py +2002 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/tbl_to_svg.py +1841 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/transition_import.py +201 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/txbody_to_svg.py +1433 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_transitions.py +3153 -0
- package/skills/dknowc-ppt-assistant/scripts/preset_shape_svg.py +450 -0
- package/skills/dknowc-ppt-assistant/scripts/project_utils.py +645 -0
- package/skills/dknowc-ppt-assistant/scripts/register_key.mjs +183 -0
- package/skills/dknowc-ppt-assistant/scripts/register_template.py +1327 -0
- package/skills/dknowc-ppt-assistant/scripts/render_trace_html.py +1048 -0
- package/skills/dknowc-ppt-assistant/scripts/resource_paths.py +282 -0
- package/skills/dknowc-ppt-assistant/scripts/shape_boolean_svg.py +189 -0
- package/skills/dknowc-ppt-assistant/scripts/slide_roster.py +44 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_finalize/__init__.py +12 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_finalize/align_embed_images.py +672 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_finalize/crop_images.py +370 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_finalize/embed_icons.py +564 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_finalize/embed_images.py +288 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_finalize/fix_image_aspect.py +389 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_finalize/flatten_tspan.py +948 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_finalize/svg_rect_to_path.py +346 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_quality/__init__.py +15 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_quality/checker.py +7791 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_quality/cli.py +193 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_quality/svg_contracts.py +1121 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_quality/xml_support.py +35 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_quality_checker.py +59 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/__init__.py +37 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/animation_config.py +1622 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/canvas_contract.py +226 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/drawingml/__init__.py +1 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/drawingml/context.py +291 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/drawingml/converter.py +1980 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/drawingml/elements.py +5272 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/drawingml/hyperlinks.py +162 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/drawingml/paths.py +847 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/drawingml/styles.py +752 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/drawingml/text_properties.py +796 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/drawingml/theme_colors.py +283 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/drawingml/theme_fonts.py +330 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/drawingml/utils.py +3555 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/geometry_properties.py +140 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/native_objects/__init__.py +356 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/native_objects/chart_data.py +1449 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/native_objects/chart_style.py +886 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/native_objects/chart_xml.py +1495 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/native_objects/chartex.py +272 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/native_objects/fallback_hash.py +161 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/native_objects/formula.py +243 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/native_objects/formula_ast.py +592 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/native_objects/formula_compiler.py +96 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/native_objects/formula_omml.py +1049 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/native_objects/formula_parser.py +2181 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/native_objects/formula_profile.py +739 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/native_objects/formula_run_properties.py +234 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/native_objects/inline_formula.py +311 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/native_objects/marker_attributes.py +182 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/native_objects/marker_common.py +806 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/native_objects/marker_status.py +190 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/native_objects/table.py +1113 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/native_objects/workbook.py +216 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/pptx_package/__init__.py +1 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/pptx_package/builder.py +6273 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/pptx_package/cli.py +2059 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/pptx_package/dimensions.py +189 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/pptx_package/discovery.py +120 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/pptx_package/media.py +160 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/pptx_package/narration.py +696 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/pptx_package/notes.py +336 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/pptx_package/slide_xml.py +138 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/pptx_package/template_structure.py +3457 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/pptx_package/template_validation.py +1536 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/semantic_markers.py +240 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/shape_boolean.py +1355 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/text_outline.py +1121 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/tspan_flattener.py +64 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/use_expander.py +577 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx.py +21 -0
- package/skills/dknowc-ppt-assistant/scripts/trusted_search.py +336 -0
- package/skills/dknowc-ppt-assistant/scripts/update_spec.py +404 -0
- package/skills/dknowc-ppt-assistant/workflows/generate-pptx.md +142 -0
- package/skills/dknowc-ppt-assistant/workflows/routing.md +38 -0
- package/skills/dknowc-trusted-search/SKILL.md +8 -7
- package/skills/dknowc-trusted-search/scripts/adapt_mcp_result.py +19 -37
- package/skills/dknowc-trusted-search/scripts/deep_query.py +117 -137
- package/skills/dknowc-trusted-search/scripts/render_trace_html.py +27 -22
- package/src/index.js +7 -2
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
"""Project root-SVG canvas parsing and cross-file validation."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import re
|
|
6
|
+
from dataclasses import dataclass
|
|
7
|
+
from decimal import Decimal, InvalidOperation, ROUND_HALF_UP, localcontext
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from typing import Iterable
|
|
10
|
+
from xml.etree import ElementTree as ET
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class CanvasContractError(ValueError):
|
|
14
|
+
"""Raised when a root SVG canvas cannot map faithfully to one slide."""
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
PPTX_SLIDE_EMU_MIN = 914400
|
|
18
|
+
PPTX_SLIDE_EMU_MAX = 51206400
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
_SVG_NUMBER = r"[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?"
|
|
22
|
+
_VIEWBOX_RE = re.compile(
|
|
23
|
+
rf"^\s*({_SVG_NUMBER})(?:\s*,\s*|\s+)"
|
|
24
|
+
rf"({_SVG_NUMBER})(?:\s*,\s*|\s+)"
|
|
25
|
+
rf"({_SVG_NUMBER})(?:\s*,\s*|\s+)"
|
|
26
|
+
rf"({_SVG_NUMBER})\s*$"
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@dataclass(frozen=True)
|
|
31
|
+
class ProjectViewBox:
|
|
32
|
+
"""One validated zero-origin project canvas."""
|
|
33
|
+
|
|
34
|
+
width: Decimal
|
|
35
|
+
height: Decimal
|
|
36
|
+
|
|
37
|
+
@property
|
|
38
|
+
def values(self) -> tuple[Decimal, Decimal, Decimal, Decimal]:
|
|
39
|
+
return Decimal(0), Decimal(0), self.width, self.height
|
|
40
|
+
|
|
41
|
+
@property
|
|
42
|
+
def pixel_dimensions(self) -> tuple[float, float]:
|
|
43
|
+
return float(self.width), float(self.height)
|
|
44
|
+
|
|
45
|
+
@property
|
|
46
|
+
def emu_dimensions(self) -> tuple[int, int]:
|
|
47
|
+
"""Quantize the SVG canvas to DrawingML's integer EMU dimensions."""
|
|
48
|
+
width_emu, height_emu = self._scaled_emu_values()
|
|
49
|
+
width = int(width_emu.to_integral_value(ROUND_HALF_UP))
|
|
50
|
+
height = int(height_emu.to_integral_value(ROUND_HALF_UP))
|
|
51
|
+
return width, height
|
|
52
|
+
|
|
53
|
+
def _scaled_emu_values(self) -> tuple[Decimal, Decimal]:
|
|
54
|
+
precision = max(
|
|
55
|
+
28,
|
|
56
|
+
len(self.width.as_tuple().digits) + 8,
|
|
57
|
+
len(self.height.as_tuple().digits) + 8,
|
|
58
|
+
)
|
|
59
|
+
with localcontext() as context:
|
|
60
|
+
context.prec = precision
|
|
61
|
+
return self.width * Decimal(9525), self.height * Decimal(9525)
|
|
62
|
+
|
|
63
|
+
@property
|
|
64
|
+
def canonical(self) -> str:
|
|
65
|
+
return f"0 0 {_format_decimal(self.width)} {_format_decimal(self.height)}"
|
|
66
|
+
|
|
67
|
+
@property
|
|
68
|
+
def has_integer_dimensions(self) -> bool:
|
|
69
|
+
return (
|
|
70
|
+
self.width == self.width.to_integral_value()
|
|
71
|
+
and self.height == self.height.to_integral_value()
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def _format_decimal(value: Decimal) -> str:
|
|
76
|
+
token = format(value, "f")
|
|
77
|
+
if "." in token:
|
|
78
|
+
token = token.rstrip("0").rstrip(".")
|
|
79
|
+
return token or "0"
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def parse_project_viewbox(
|
|
83
|
+
raw: str | None,
|
|
84
|
+
*,
|
|
85
|
+
context: str = "root viewBox",
|
|
86
|
+
) -> ProjectViewBox:
|
|
87
|
+
"""Parse the registered project root-viewBox subset.
|
|
88
|
+
|
|
89
|
+
Equivalent SVG numeric spellings are accepted so callers can distinguish
|
|
90
|
+
compatible input from canonical authoring. Semantic values stay closed:
|
|
91
|
+
the origin is exactly zero and dimensions map to positive DrawingML sizes.
|
|
92
|
+
Fractional dimensions remain read-compatible because imported custom PPTX
|
|
93
|
+
slide sizes are not necessarily whole CSS pixels.
|
|
94
|
+
"""
|
|
95
|
+
if raw is None or not raw.strip():
|
|
96
|
+
raise CanvasContractError(f"{context} is required")
|
|
97
|
+
|
|
98
|
+
match = _VIEWBOX_RE.fullmatch(raw)
|
|
99
|
+
if match is None:
|
|
100
|
+
raise CanvasContractError(
|
|
101
|
+
f"{context} must contain exactly four SVG numbers; got {raw!r}"
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
try:
|
|
105
|
+
values = tuple(Decimal(token) for token in match.groups())
|
|
106
|
+
except InvalidOperation as exc:
|
|
107
|
+
raise CanvasContractError(
|
|
108
|
+
f"{context} contains an invalid numeric value; got {raw!r}"
|
|
109
|
+
) from exc
|
|
110
|
+
if not all(value.is_finite() for value in values):
|
|
111
|
+
raise CanvasContractError(
|
|
112
|
+
f"{context} values must be finite; got {raw!r}"
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
x, y, width, height = values
|
|
116
|
+
if x != 0 or y != 0:
|
|
117
|
+
raise CanvasContractError(
|
|
118
|
+
f'{context} origin must be "0 0"; got {raw!r}'
|
|
119
|
+
)
|
|
120
|
+
if width <= 0 or height <= 0:
|
|
121
|
+
raise CanvasContractError(
|
|
122
|
+
f"{context} width and height must be positive; got {raw!r}"
|
|
123
|
+
)
|
|
124
|
+
viewbox = ProjectViewBox(width=width, height=height)
|
|
125
|
+
require_powerpoint_slide_size(viewbox, context=context)
|
|
126
|
+
return viewbox
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def read_project_viewbox(svg_path: str | Path) -> ProjectViewBox:
|
|
130
|
+
"""Read and validate one file's root SVG viewBox."""
|
|
131
|
+
path = Path(svg_path)
|
|
132
|
+
try:
|
|
133
|
+
root = ET.parse(path).getroot()
|
|
134
|
+
except (OSError, ET.ParseError) as exc:
|
|
135
|
+
raise CanvasContractError(f"{path.name}: unable to parse root SVG: {exc}") from exc
|
|
136
|
+
return parse_project_svg_root(
|
|
137
|
+
root,
|
|
138
|
+
context=path.name,
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def parse_project_svg_root(
|
|
143
|
+
root: ET.Element,
|
|
144
|
+
*,
|
|
145
|
+
context: str = "document",
|
|
146
|
+
) -> ProjectViewBox:
|
|
147
|
+
"""Validate one page root element and its project viewBox."""
|
|
148
|
+
if root.tag.rsplit("}", 1)[-1] != "svg":
|
|
149
|
+
raise CanvasContractError(f"{context}: root element must be <svg>")
|
|
150
|
+
return parse_project_viewbox(
|
|
151
|
+
root.get("viewBox"),
|
|
152
|
+
context=f"{context} root viewBox",
|
|
153
|
+
)
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def require_powerpoint_slide_size(
|
|
157
|
+
viewbox: ProjectViewBox,
|
|
158
|
+
*,
|
|
159
|
+
context: str = "SVG canvas",
|
|
160
|
+
) -> tuple[int, int]:
|
|
161
|
+
"""Return EMU dimensions or reject values outside PowerPoint's range."""
|
|
162
|
+
# Reject before converting to int so an adversarial exponent cannot force
|
|
163
|
+
# construction of an enormous Python integer or fixed-point string.
|
|
164
|
+
values = viewbox.width, viewbox.height
|
|
165
|
+
outside_coarse_bound = any(
|
|
166
|
+
value > Decimal(PPTX_SLIDE_EMU_MAX) for value in values
|
|
167
|
+
)
|
|
168
|
+
scaled = () if outside_coarse_bound else viewbox._scaled_emu_values()
|
|
169
|
+
outside_emu_bound = outside_coarse_bound or not all(
|
|
170
|
+
Decimal(PPTX_SLIDE_EMU_MIN) - Decimal("0.5")
|
|
171
|
+
<= value
|
|
172
|
+
< Decimal(PPTX_SLIDE_EMU_MAX) + Decimal("0.5")
|
|
173
|
+
for value in scaled
|
|
174
|
+
)
|
|
175
|
+
if outside_emu_bound:
|
|
176
|
+
dimensions = f"{viewbox.width} x {viewbox.height} px"
|
|
177
|
+
raise CanvasContractError(
|
|
178
|
+
f"{context} must map to PowerPoint's supported slide range "
|
|
179
|
+
f"({PPTX_SLIDE_EMU_MIN}..{PPTX_SLIDE_EMU_MAX} EMU per side); "
|
|
180
|
+
f"got {dimensions}"
|
|
181
|
+
)
|
|
182
|
+
return viewbox.emu_dimensions
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def require_consistent_project_viewboxes(
|
|
186
|
+
svg_paths: Iterable[str | Path],
|
|
187
|
+
*,
|
|
188
|
+
expected_viewbox: str | None = None,
|
|
189
|
+
expected_label: str = "expected canvas",
|
|
190
|
+
) -> ProjectViewBox:
|
|
191
|
+
"""Validate every public/internal SVG and return their shared canvas."""
|
|
192
|
+
paths = [Path(path) for path in svg_paths]
|
|
193
|
+
if not paths:
|
|
194
|
+
raise CanvasContractError("at least one SVG is required to resolve the canvas")
|
|
195
|
+
|
|
196
|
+
errors: list[str] = []
|
|
197
|
+
parsed: list[tuple[Path, ProjectViewBox]] = []
|
|
198
|
+
for path in paths:
|
|
199
|
+
try:
|
|
200
|
+
parsed.append((path, read_project_viewbox(path)))
|
|
201
|
+
except CanvasContractError as exc:
|
|
202
|
+
errors.append(str(exc))
|
|
203
|
+
if errors:
|
|
204
|
+
details = "\n".join(f" - {error}" for error in errors)
|
|
205
|
+
raise CanvasContractError("SVG canvas validation failed:\n" + details)
|
|
206
|
+
|
|
207
|
+
if expected_viewbox is not None:
|
|
208
|
+
reference = parse_project_viewbox(
|
|
209
|
+
expected_viewbox,
|
|
210
|
+
context=f"{expected_label} viewBox",
|
|
211
|
+
)
|
|
212
|
+
else:
|
|
213
|
+
reference = parsed[0][1]
|
|
214
|
+
|
|
215
|
+
mismatches = [
|
|
216
|
+
f"{path.name}: expected {reference.canonical}, got {viewbox.canonical}"
|
|
217
|
+
for path, viewbox in parsed
|
|
218
|
+
if viewbox != reference
|
|
219
|
+
]
|
|
220
|
+
if mismatches:
|
|
221
|
+
details = "\n".join(f" - {message}" for message in mismatches)
|
|
222
|
+
raise CanvasContractError(
|
|
223
|
+
f"SVG canvases must all match {expected_label} "
|
|
224
|
+
f"({reference.canonical}):\n{details}"
|
|
225
|
+
)
|
|
226
|
+
return reference
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""DrawingML conversion helpers for SVG-to-PPTX export."""
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
"""ConvertContext — shared state passed through the SVG → DrawingML pipeline."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
from typing import TYPE_CHECKING, Any
|
|
7
|
+
from xml.etree import ElementTree as ET
|
|
8
|
+
from dataclasses import dataclass, field
|
|
9
|
+
|
|
10
|
+
if TYPE_CHECKING:
|
|
11
|
+
from .theme_colors import ThemeColorSpec
|
|
12
|
+
from .theme_fonts import ThemeFontSpec
|
|
13
|
+
|
|
14
|
+
AffineMatrix = tuple[float, float, float, float, float, float]
|
|
15
|
+
IDENTITY_MATRIX: AffineMatrix = (1.0, 0.0, 0.0, 1.0, 0.0, 0.0)
|
|
16
|
+
|
|
17
|
+
TEXT_FLOW_PRESERVE = 'preserve'
|
|
18
|
+
TEXT_FLOW_REFLOW = 'reflow'
|
|
19
|
+
TEXT_FLOW_SPLIT = 'split'
|
|
20
|
+
TEXT_FLOW_MODES = frozenset({
|
|
21
|
+
TEXT_FLOW_PRESERVE,
|
|
22
|
+
TEXT_FLOW_REFLOW,
|
|
23
|
+
TEXT_FLOW_SPLIT,
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def resolve_text_flow(
|
|
28
|
+
text_flow: str | None = None,
|
|
29
|
+
merge_paragraphs: bool | None = None,
|
|
30
|
+
) -> str:
|
|
31
|
+
"""Resolve the public text-layout options to one internal mode."""
|
|
32
|
+
if text_flow is not None and merge_paragraphs is not None:
|
|
33
|
+
raise ValueError(
|
|
34
|
+
'text_flow and legacy merge_paragraphs cannot be used together'
|
|
35
|
+
)
|
|
36
|
+
if merge_paragraphs is not None:
|
|
37
|
+
return TEXT_FLOW_REFLOW if merge_paragraphs else TEXT_FLOW_SPLIT
|
|
38
|
+
resolved = TEXT_FLOW_PRESERVE if text_flow is None else text_flow
|
|
39
|
+
if resolved not in TEXT_FLOW_MODES:
|
|
40
|
+
choices = ', '.join(sorted(TEXT_FLOW_MODES))
|
|
41
|
+
raise ValueError(
|
|
42
|
+
f'unsupported text_flow {resolved!r}; expected one of: {choices}'
|
|
43
|
+
)
|
|
44
|
+
return resolved
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
@dataclass
|
|
48
|
+
class ShapeResult:
|
|
49
|
+
"""Internal conversion result carrying XML plus resolved EMU bounds."""
|
|
50
|
+
|
|
51
|
+
xml: str
|
|
52
|
+
bounds_emu: tuple[int, int, int, int] | None = None
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
@dataclass
|
|
56
|
+
class ConvertContext:
|
|
57
|
+
"""Shared context passed through the SVG → DrawingML conversion pipeline.
|
|
58
|
+
|
|
59
|
+
Derived via child() during recursive SVG tree traversal to accumulate
|
|
60
|
+
translate / scale / inherited style information.
|
|
61
|
+
"""
|
|
62
|
+
|
|
63
|
+
defs: dict[str, ET.Element] = field(default_factory=dict)
|
|
64
|
+
id_counter: int = 2 # 1 is reserved for spTree root
|
|
65
|
+
# Imported PPTX shape ids are reserved before conversion so newly authored
|
|
66
|
+
# SVG elements cannot steal an id referenced by a native connector.
|
|
67
|
+
reserved_shape_ids: frozenset[int] = frozenset()
|
|
68
|
+
source_shape_id_map: dict[tuple[str, str], int] = field(default_factory=dict)
|
|
69
|
+
claimed_shape_ids: set[int] = field(default_factory=set)
|
|
70
|
+
referenced_shape_ids: set[int] = field(default_factory=set)
|
|
71
|
+
slide_num: int = 1
|
|
72
|
+
# Public presentation roster size, used to fail closed on #slide-N links.
|
|
73
|
+
slide_count: int | None = None
|
|
74
|
+
translate_x: float = 0.0
|
|
75
|
+
translate_y: float = 0.0
|
|
76
|
+
scale_x: float = 1.0
|
|
77
|
+
scale_y: float = 1.0
|
|
78
|
+
viewport_width: float = 1280.0
|
|
79
|
+
viewport_height: float = 720.0
|
|
80
|
+
transform_matrix: AffineMatrix = IDENTITY_MATRIX
|
|
81
|
+
use_transform_matrix: bool = False
|
|
82
|
+
filter_id: str | None = None
|
|
83
|
+
media_files: dict[str, bytes] = field(default_factory=dict)
|
|
84
|
+
rel_entries: list[dict[str, str]] = field(default_factory=list)
|
|
85
|
+
package_files: dict[str, bytes] = field(default_factory=dict)
|
|
86
|
+
content_type_overrides: dict[str, str] = field(default_factory=dict)
|
|
87
|
+
rel_id_counter: int = 2 # rId1 reserved for slideLayout
|
|
88
|
+
svg_dir: Path | None = None
|
|
89
|
+
inherited_styles: dict[str, str] = field(default_factory=dict)
|
|
90
|
+
# Effective SVG font sizes keyed by element identity. Shared resolution
|
|
91
|
+
# keeps relative sizes and em tracking identical across checker/exporter.
|
|
92
|
+
text_font_sizes: dict[int, float] = field(default_factory=dict)
|
|
93
|
+
# Effective source-pixel tracking resolved where each declaration occurs.
|
|
94
|
+
text_letter_spacings: dict[int, float] = field(default_factory=dict)
|
|
95
|
+
# SVG group opacity is post-compositing, not an inherited presentation
|
|
96
|
+
# property. DrawingML has no equivalent group alpha, so native export
|
|
97
|
+
# approximates it by multiplying this value into each descendant object.
|
|
98
|
+
opacity_multiplier: float = 1.0
|
|
99
|
+
# Recursion depth — only the depth==0 (root) context records anim targets.
|
|
100
|
+
depth: int = 0
|
|
101
|
+
# Top-level <g id="..."> groups, recorded as (shape_id, svg_id) in z-order.
|
|
102
|
+
# Used by the PPTX builder to emit per-element object-animation timing.
|
|
103
|
+
anim_targets: list = field(default_factory=list)
|
|
104
|
+
# Explicit sidecar group ids may override the legacy chrome-name heuristic.
|
|
105
|
+
# Explicit structural layer/role/placeholder markers remain non-animatable.
|
|
106
|
+
animation_group_overrides: frozenset[str] = frozenset()
|
|
107
|
+
# Text-layout policy for positional tspans: preserve authored line breaks
|
|
108
|
+
# in one frame, reflow them, or split them into independent frames.
|
|
109
|
+
text_flow: str = TEXT_FLOW_PRESERVE
|
|
110
|
+
# Explicit opt-in: replace marked chart/table fallback groups with editable
|
|
111
|
+
# PowerPoint graphicFrames. Default stays off to preserve SVG output.
|
|
112
|
+
native_objects_enabled: bool = False
|
|
113
|
+
# Native PPTX image optimization. Keeps generated decks compact by
|
|
114
|
+
# downsampling oversized raster assets to their rendered size.
|
|
115
|
+
image_optimize: bool = True
|
|
116
|
+
image_max_dimension: int | None = 2560
|
|
117
|
+
image_sizing: str = 'cap'
|
|
118
|
+
image_scale: float = 2.0
|
|
119
|
+
image_quality: int = 85
|
|
120
|
+
# Optional per-element conversion diagnostics. Shared by child contexts so
|
|
121
|
+
# callers can inspect native / skipped / unsupported decisions per slide.
|
|
122
|
+
trace_events: list[dict[str, Any]] | None = None
|
|
123
|
+
# Optional project theme contract. Matching SVG title/body families emit
|
|
124
|
+
# DrawingML +mj/+mn tokens instead of fixed typeface names.
|
|
125
|
+
theme_font_spec: ThemeFontSpec | None = None
|
|
126
|
+
# Optional project theme-color contract. Exact locked colors are promoted
|
|
127
|
+
# to context-safe DrawingML scheme slots while local colors stay concrete.
|
|
128
|
+
theme_color_spec: ThemeColorSpec | None = None
|
|
129
|
+
# Canonical BCP-47 content language from spec_lock.md. ``None`` preserves
|
|
130
|
+
# the legacy per-run script heuristic for older projects and lockless quick generation.
|
|
131
|
+
primary_language: str | None = None
|
|
132
|
+
# Reuse media relationships when the same image fills multiple text runs.
|
|
133
|
+
text_image_fill_cache: dict[tuple[str, str], str] = field(default_factory=dict)
|
|
134
|
+
|
|
135
|
+
def next_id(self) -> int:
|
|
136
|
+
"""Allocate the next shape ID."""
|
|
137
|
+
cid = self.id_counter
|
|
138
|
+
while cid in self.reserved_shape_ids or cid in self.claimed_shape_ids:
|
|
139
|
+
cid += 1
|
|
140
|
+
self.id_counter = cid + 1
|
|
141
|
+
self.claimed_shape_ids.add(cid)
|
|
142
|
+
return cid
|
|
143
|
+
|
|
144
|
+
def claim_shape_id(
|
|
145
|
+
self,
|
|
146
|
+
source_id: str | None,
|
|
147
|
+
source_scope: str | None = None,
|
|
148
|
+
) -> int:
|
|
149
|
+
"""Claim a pre-reserved imported shape id, or allocate a fresh one."""
|
|
150
|
+
if source_id is None:
|
|
151
|
+
return self.next_id()
|
|
152
|
+
scope = source_scope or 'slide'
|
|
153
|
+
key = (scope, source_id)
|
|
154
|
+
shape_id = self.source_shape_id_map.get(key)
|
|
155
|
+
if shape_id is None:
|
|
156
|
+
raise ValueError(
|
|
157
|
+
f'Unreserved data-pptx-shape-id {source_id!r} in scope {scope!r}'
|
|
158
|
+
)
|
|
159
|
+
if shape_id in self.claimed_shape_ids:
|
|
160
|
+
raise ValueError(
|
|
161
|
+
f'Duplicate data-pptx-shape-id {source_id!r} in scope {scope!r}'
|
|
162
|
+
)
|
|
163
|
+
self.claimed_shape_ids.add(shape_id)
|
|
164
|
+
return shape_id
|
|
165
|
+
|
|
166
|
+
def reference_shape_id(
|
|
167
|
+
self,
|
|
168
|
+
source_id: str,
|
|
169
|
+
source_scope: str | None = None,
|
|
170
|
+
) -> int:
|
|
171
|
+
"""Resolve and record a connector target in the imported id space."""
|
|
172
|
+
scope = source_scope or 'slide'
|
|
173
|
+
shape_id = self.source_shape_id_map.get((scope, source_id))
|
|
174
|
+
if shape_id is None:
|
|
175
|
+
raise ValueError(
|
|
176
|
+
f'Unknown connector shape reference {source_id!r} in scope {scope!r}'
|
|
177
|
+
)
|
|
178
|
+
self.referenced_shape_ids.add(shape_id)
|
|
179
|
+
return shape_id
|
|
180
|
+
|
|
181
|
+
def next_rel_id(self) -> str:
|
|
182
|
+
"""Allocate the next relationship ID (rIdN)."""
|
|
183
|
+
rid = f'rId{self.rel_id_counter}'
|
|
184
|
+
self.rel_id_counter += 1
|
|
185
|
+
return rid
|
|
186
|
+
|
|
187
|
+
def child(
|
|
188
|
+
self,
|
|
189
|
+
dx: float = 0,
|
|
190
|
+
dy: float = 0,
|
|
191
|
+
sx: float = 1.0,
|
|
192
|
+
sy: float = 1.0,
|
|
193
|
+
transform_matrix: AffineMatrix | None = None,
|
|
194
|
+
filter_id: str | None = None,
|
|
195
|
+
style_overrides: dict[str, str] | None = None,
|
|
196
|
+
opacity_multiplier: float = 1.0,
|
|
197
|
+
) -> ConvertContext:
|
|
198
|
+
"""Create a child context with accumulated translate / scale / styles.
|
|
199
|
+
|
|
200
|
+
Args:
|
|
201
|
+
dx: X translation delta.
|
|
202
|
+
dy: Y translation delta.
|
|
203
|
+
sx: X scale factor.
|
|
204
|
+
sy: Y scale factor.
|
|
205
|
+
transform_matrix: Full affine transform to accumulate for
|
|
206
|
+
converters that can faithfully map it to DrawingML.
|
|
207
|
+
filter_id: Override filter ID.
|
|
208
|
+
style_overrides: Style attribute overrides from child element.
|
|
209
|
+
opacity_multiplier: Local group opacity to multiply into descendants.
|
|
210
|
+
"""
|
|
211
|
+
local_matrix = transform_matrix or IDENTITY_MATRIX
|
|
212
|
+
# When first crossing from scalar to matrix mode, fold accumulated
|
|
213
|
+
# translate_x/y and scale_x/y into the matrix base. Otherwise the
|
|
214
|
+
# ancestor's scalar transform — which matrix-path readers (e.g.
|
|
215
|
+
# <image>) never look at — is silently lost, and the descendant
|
|
216
|
+
# lands at raw SVG coordinates (typically near (0,0)).
|
|
217
|
+
if transform_matrix is not None and not self.use_transform_matrix:
|
|
218
|
+
base_matrix: AffineMatrix = (
|
|
219
|
+
self.scale_x, 0.0,
|
|
220
|
+
0.0, self.scale_y,
|
|
221
|
+
self.translate_x, self.translate_y,
|
|
222
|
+
)
|
|
223
|
+
else:
|
|
224
|
+
base_matrix = self.transform_matrix
|
|
225
|
+
a1, b1, c1, d1, e1, f1 = base_matrix
|
|
226
|
+
a2, b2, c2, d2, e2, f2 = local_matrix
|
|
227
|
+
combined_matrix: AffineMatrix = (
|
|
228
|
+
a1 * a2 + c1 * b2,
|
|
229
|
+
b1 * a2 + d1 * b2,
|
|
230
|
+
a1 * c2 + c1 * d2,
|
|
231
|
+
b1 * c2 + d1 * d2,
|
|
232
|
+
a1 * e2 + c1 * f2 + e1,
|
|
233
|
+
b1 * e2 + d1 * f2 + f1,
|
|
234
|
+
)
|
|
235
|
+
|
|
236
|
+
merged = dict(self.inherited_styles)
|
|
237
|
+
if style_overrides:
|
|
238
|
+
merged.update(style_overrides)
|
|
239
|
+
|
|
240
|
+
local_opacity = max(0.0, min(1.0, opacity_multiplier))
|
|
241
|
+
|
|
242
|
+
return ConvertContext(
|
|
243
|
+
defs=self.defs,
|
|
244
|
+
id_counter=self.id_counter,
|
|
245
|
+
reserved_shape_ids=self.reserved_shape_ids,
|
|
246
|
+
source_shape_id_map=self.source_shape_id_map,
|
|
247
|
+
claimed_shape_ids=self.claimed_shape_ids,
|
|
248
|
+
referenced_shape_ids=self.referenced_shape_ids,
|
|
249
|
+
slide_num=self.slide_num,
|
|
250
|
+
slide_count=self.slide_count,
|
|
251
|
+
translate_x=self.translate_x + dx,
|
|
252
|
+
translate_y=self.translate_y + dy,
|
|
253
|
+
scale_x=self.scale_x * sx,
|
|
254
|
+
scale_y=self.scale_y * sy,
|
|
255
|
+
viewport_width=self.viewport_width,
|
|
256
|
+
viewport_height=self.viewport_height,
|
|
257
|
+
transform_matrix=combined_matrix,
|
|
258
|
+
use_transform_matrix=self.use_transform_matrix or transform_matrix is not None,
|
|
259
|
+
filter_id=filter_id or self.filter_id,
|
|
260
|
+
media_files=self.media_files,
|
|
261
|
+
rel_entries=self.rel_entries,
|
|
262
|
+
package_files=self.package_files,
|
|
263
|
+
content_type_overrides=self.content_type_overrides,
|
|
264
|
+
rel_id_counter=self.rel_id_counter,
|
|
265
|
+
svg_dir=self.svg_dir,
|
|
266
|
+
inherited_styles=merged,
|
|
267
|
+
text_font_sizes=self.text_font_sizes,
|
|
268
|
+
text_letter_spacings=self.text_letter_spacings,
|
|
269
|
+
opacity_multiplier=self.opacity_multiplier * local_opacity,
|
|
270
|
+
depth=self.depth + 1,
|
|
271
|
+
# anim_targets is intentionally a fresh list on the child;
|
|
272
|
+
# only the root-level context's list is read by the builder.
|
|
273
|
+
animation_group_overrides=self.animation_group_overrides,
|
|
274
|
+
text_flow=self.text_flow,
|
|
275
|
+
native_objects_enabled=self.native_objects_enabled,
|
|
276
|
+
image_optimize=self.image_optimize,
|
|
277
|
+
image_max_dimension=self.image_max_dimension,
|
|
278
|
+
image_sizing=self.image_sizing,
|
|
279
|
+
image_scale=self.image_scale,
|
|
280
|
+
image_quality=self.image_quality,
|
|
281
|
+
trace_events=self.trace_events,
|
|
282
|
+
theme_font_spec=self.theme_font_spec,
|
|
283
|
+
theme_color_spec=self.theme_color_spec,
|
|
284
|
+
primary_language=self.primary_language,
|
|
285
|
+
text_image_fill_cache=self.text_image_fill_cache,
|
|
286
|
+
)
|
|
287
|
+
|
|
288
|
+
def sync_from_child(self, child_ctx: ConvertContext) -> None:
|
|
289
|
+
"""Sync counters back from a child context."""
|
|
290
|
+
self.id_counter = child_ctx.id_counter
|
|
291
|
+
self.rel_id_counter = child_ctx.rel_id_counter
|