@hfyjq/crannia 1.0.0 → 1.1.0
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/LICENSE +201 -201
- package/NOTICE +45 -45
- package/THIRD-PARTY-FRONTEND.md +5266 -5266
- package/THIRD-PARTY-PYTHON.md +162 -153
- package/THIRD-PARTY.md +316 -316
- package/apps/desktop/quotes.txt +56 -56
- package/bus/core.py +671 -671
- package/bus/loaders/channel_loader.py +64 -64
- package/bus/loaders/skill_md.py +676 -676
- package/bus/skill_state.py +124 -124
- package/channels/cli_channel/manifest.yaml +5 -5
- package/channels/dingtalk_channel/manifest.yaml +12 -12
- package/channels/feishu_channel/manifest.yaml +12 -12
- package/channels/qq_bot_channel/__init__.py +1 -1
- package/channels/qq_bot_channel/handler.py +881 -881
- package/channels/qq_bot_channel/manifest.yaml +4 -4
- package/channels/telegram_channel/__init__.py +1 -1
- package/channels/telegram_channel/handler.py +813 -813
- package/channels/telegram_channel/manifest.yaml +4 -4
- package/channels/telegram_channel/media.py +231 -231
- package/channels/textfmt.py +58 -58
- package/channels/wechat_channel/__init__.py +1 -1
- package/channels/wechat_channel/auth.py +23 -5
- package/channels/wechat_channel/manifest.yaml +5 -5
- package/channels/wechat_channel/media.py +178 -178
- package/config.yaml +2 -2
- package/core/activity.py +3 -0
- package/core/agent_loop.py +390 -55
- package/core/approval.py +17 -6
- package/core/artifacts.py +7 -0
- package/core/auto_review.py +23 -5
- package/core/channel_configs.py +34 -2
- package/core/channel_turns.py +759 -759
- package/core/child_approval.py +150 -0
- package/core/config_requirements.py +205 -0
- package/core/cron/runner.py +118 -6
- package/core/cron/store.py +14 -5
- package/core/cron/tools.py +17 -5
- package/core/endpoint.py +141 -0
- package/core/events.py +23 -0
- package/core/i18n.py +329 -329
- package/core/ice/store.py +191 -191
- package/core/inbound.py +193 -193
- package/core/jobs.py +19 -3
- package/core/last_good.py +150 -150
- package/core/llm_errors.py +227 -227
- package/core/llm_retry.py +230 -230
- package/core/memory/compressor.py +1214 -1214
- package/core/memory/manager.py +14 -0
- package/core/model_configs.py +80 -0
- package/core/pipeline/scheduler.py +2 -2
- package/core/pipeline/stages/agent_loop.py +24 -4
- package/core/pipeline/stages/pre_process.py +58 -7
- package/core/prompt_assembler.py +680 -680
- package/core/rg.py +255 -255
- package/core/security.py +1544 -1544
- package/core/settings_baseline.py +70 -1
- package/core/shell.py +5 -17
- package/core/skill_settings.py +7 -2
- package/core/state_registry.py +19 -0
- package/core/subagent.py +64 -3
- package/core/task_tool.py +1 -1
- package/core/tasks_notify.py +69 -4
- package/core/tiers.py +9 -3
- package/core/tool_outcome.py +108 -0
- package/core/update_tui.py +158 -0
- package/core/version.py +2 -2
- package/core/web_cache.py +152 -0
- package/crannia_tray.py +278 -278
- package/creators/models.json +4 -4
- package/data/settings.example.json +67 -0
- package/licenses/GPL-3.0.txt +619 -619
- package/licenses/LGPL-2.1.txt +502 -502
- package/licenses/LGPL-3.0.txt +165 -165
- package/licenses/MIT-chat-on-wechat.txt +18 -18
- package/licenses/MIT-deepseek-harness.txt +21 -21
- package/licenses/Molio-LICENSE.txt +44 -44
- package/locales/en.json +9 -4
- package/main.py +154 -154
- package/mcpx/manager.py +522 -522
- package/mcpx/servers/__init__.py +22 -22
- package/mcpx/servers/web_search.py +321 -33
- package/package.json +14 -9
- package/providers/anthropic/client.py +772 -772
- package/providers/cost_table.json +3779 -3779
- package/providers/deepseek/client.py +20 -20
- package/providers/deepseek/manifest.yaml +2 -2
- package/providers/factory.py +136 -136
- package/providers/lmstudio/client.py +2 -2
- package/providers/metadata.py +80 -80
- package/providers/model_capabilities.json +6665 -6665
- package/providers/moonshot/client.py +12 -12
- package/providers/ollama/client.py +2 -2
- package/providers/openai/client.py +13 -13
- package/providers/openrouter/client.py +2 -2
- package/providers/qwen/__init__.py +1 -1
- package/providers/qwen/client.py +2 -2
- package/providers/qwen/manifest.yaml +16 -16
- package/providers/rate_limiter.py +144 -144
- package/providers/siliconflow/client.py +2 -2
- package/providers/validation.py +171 -171
- package/providers/window_table.json +8909 -8909
- package/sbom.json +4413 -4296
- package/skills/entertainment/SKILL.md +35 -35
- package/skills/entertainment/__init__.py +1 -1
- package/skills/entertainment/handler.py +91 -91
- package/skills/entertainment/manifest.yaml +26 -26
- package/skills/file_parser/GUIDANCE.md +10 -10
- package/skills/file_parser/handler.py +177 -4
- package/skills/file_parser/manifest.yaml +22 -1
- package/skills/github/SKILL.md +32 -32
- package/skills/github/__init__.py +1 -1
- package/skills/github/manifest.yaml +5 -0
- package/skills/mail_parser/SKILL.md +68 -68
- package/skills/mail_parser/__init__.py +1 -1
- package/skills/mail_parser/handler.py +806 -806
- package/skills/mail_parser/manifest.yaml +5 -0
- package/skills/media_gen/GUIDANCE.md +19 -19
- package/skills/media_gen/SKILL.md +43 -43
- package/skills/media_gen/__init__.py +1 -1
- package/skills/media_gen/handler.py +430 -430
- package/skills/media_gen/manifest.yaml +93 -88
- package/skills/office_parser/cell_validation.py +127 -127
- package/skills/office_parser/excel_transform.py +936 -936
- package/skills/office_parser/layout_spec.py +243 -243
- package/skills/office_parser/pptx_export/console_encoding.py +27 -27
- package/skills/office_parser/pptx_export/resource_paths.py +88 -88
- package/skills/office_parser/pptx_export/svg_finalize/__init__.py +12 -12
- package/skills/office_parser/pptx_export/svg_finalize/align_embed_images.py +575 -575
- package/skills/office_parser/pptx_export/svg_finalize/crop_images.py +366 -366
- package/skills/office_parser/pptx_export/svg_finalize/embed_icons.py +498 -498
- package/skills/office_parser/pptx_export/svg_finalize/embed_images.py +288 -288
- package/skills/office_parser/pptx_export/svg_finalize/fix_image_aspect.py +388 -388
- package/skills/office_parser/pptx_export/svg_finalize/flatten_tspan.py +818 -818
- package/skills/office_parser/pptx_export/svg_finalize/svg_rect_to_path.py +345 -345
- package/skills/office_parser/pptx_export/svg_to_pptx/__init__.py +14 -14
- package/skills/office_parser/pptx_export/svg_to_pptx/drawingml/__init__.py +1 -1
- package/skills/office_parser/pptx_export/svg_to_pptx/drawingml/context.py +186 -186
- package/skills/office_parser/pptx_export/svg_to_pptx/drawingml/converter.py +781 -781
- package/skills/office_parser/pptx_export/svg_to_pptx/drawingml/elements.py +2490 -2490
- package/skills/office_parser/pptx_export/svg_to_pptx/drawingml/paths.py +429 -429
- package/skills/office_parser/pptx_export/svg_to_pptx/drawingml/styles.py +656 -656
- package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/__init__.py +1 -1
- package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/builder.py +1267 -1267
- package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/dimensions.py +155 -155
- package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/discovery.py +101 -101
- package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/media.py +141 -141
- package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/narration.py +236 -236
- package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/notes.py +257 -257
- package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/slide_xml.py +136 -136
- package/skills/office_parser/pptx_export/svg_to_pptx/tspan_flattener.py +44 -44
- package/skills/office_parser/pptx_export/svg_to_pptx/use_expander.py +129 -129
- package/skills/office_parser/references/LICENSE-Office-Word-MCP-Server.txt +20 -20
- package/skills/office_parser/references/LICENSE-excel-mcp-server.txt +21 -21
- package/skills/office_parser/references/layout_library.md +816 -816
- package/skills/office_parser/references/ppt-master/LICENSE +21 -21
- package/skills/office_parser/references/ppt-master/references/executor-base.md +474 -474
- package/skills/office_parser/references/ppt-master/references/shared-standards.md +1002 -1002
- package/skills/office_parser/references/ppt-master/references/strategist.md +880 -880
- package/skills/office_parser/references/ppt-master/references/template-designer.md +402 -402
- package/skills/office_parser/references/ppt-master/references/visual-review.md +240 -240
- package/skills/office_parser/references/ppt-master/references/visual-styles/_index.md +106 -106
- package/skills/office_parser/references/ppt-master/references/visual-styles/blueprint.md +37 -37
- package/skills/office_parser/references/ppt-master/references/visual-styles/brutalist.md +38 -38
- package/skills/office_parser/references/ppt-master/references/visual-styles/chalkboard.md +36 -36
- package/skills/office_parser/references/ppt-master/references/visual-styles/dark-tech.md +37 -37
- package/skills/office_parser/references/ppt-master/references/visual-styles/data-journalism.md +36 -36
- package/skills/office_parser/references/ppt-master/references/visual-styles/editorial.md +38 -38
- package/skills/office_parser/references/ppt-master/references/visual-styles/glassmorphism.md +36 -36
- package/skills/office_parser/references/ppt-master/references/visual-styles/ink-notes.md +37 -37
- package/skills/office_parser/references/ppt-master/references/visual-styles/ink-wash.md +36 -36
- package/skills/office_parser/references/ppt-master/references/visual-styles/memphis.md +37 -37
- package/skills/office_parser/references/ppt-master/references/visual-styles/paper-cut.md +36 -36
- package/skills/office_parser/references/ppt-master/references/visual-styles/photo-editorial.md +38 -38
- package/skills/office_parser/references/ppt-master/references/visual-styles/pixel-art.md +36 -36
- package/skills/office_parser/references/ppt-master/references/visual-styles/sketch-notes.md +36 -36
- package/skills/office_parser/references/ppt-master/references/visual-styles/soft-rounded.md +37 -37
- package/skills/office_parser/references/ppt-master/references/visual-styles/swiss-minimal.md +38 -38
- package/skills/office_parser/references/ppt-master/references/visual-styles/vintage-poster.md +36 -36
- package/skills/office_parser/references/ppt-master/references/visual-styles/zine.md +37 -37
- package/skills/office_parser/references/spec_lock.md +235 -235
- package/skills/office_parser/soffice.py +68 -68
- package/skills/office_parser/word_tools.py +2909 -2909
- package/tui/crannia.cmd +134 -0
- package/tui/crannia_tui/__init__.py +1 -0
- package/tui/crannia_tui/__main__.py +223 -0
- package/tui/crannia_tui/app.py +1318 -0
- package/tui/crannia_tui/app.tcss +153 -0
- package/tui/crannia_tui/approval.py +85 -0
- package/tui/crannia_tui/backend.py +279 -0
- package/tui/crannia_tui/channel.py +472 -0
- package/tui/crannia_tui/client.py +125 -0
- package/tui/crannia_tui/clipboard.py +62 -0
- package/tui/crannia_tui/commands.py +65 -0
- package/tui/crannia_tui/doctor.py +304 -0
- package/tui/crannia_tui/events.py +84 -0
- package/tui/crannia_tui/headless.py +152 -0
- package/tui/crannia_tui/i18n.py +164 -0
- package/tui/crannia_tui/locales/en.json +263 -0
- package/tui/crannia_tui/outcome.py +62 -0
- package/tui/crannia_tui/paths.py +181 -0
- package/tui/crannia_tui/probe.py +53 -0
- package/tui/crannia_tui/profile.py +169 -0
- package/tui/crannia_tui/render.py +209 -0
- package/tui/crannia_tui/settings.py +134 -0
- package/tui/crannia_tui/setup.py +380 -0
- package/tui/crannia_tui/term.py +116 -0
- package/tui/crannia_tui/ui.py +1236 -0
- package/tui/crannia_tui/update.py +389 -0
- package/vendor/ripgrep/COPYING.txt +3 -3
- package/vendor/ripgrep/LICENSE-MIT.txt +21 -21
- package/vendor/ripgrep/NOTICE-vscode-ripgrep.txt +13 -13
- package/vendor/ripgrep/README.md +51 -51
- package/vendor/ripgrep/UNLICENSE.txt +24 -24
- package/vendor/ripgrep/package-source.json +19 -19
- package/web/__main__.py +15 -0
- package/web/server.py +168 -19
- package/web/static/img/channel-telegram.svg +1 -1
- package/web/static/img/channel-wechat.svg +1 -1
- package/web/templates/logs.html +49 -49
- package/web/templates/models.html +46 -46
|
@@ -1,429 +1,429 @@
|
|
|
1
|
-
"""SVG path parsing, normalization, and DrawingML path command generation."""
|
|
2
|
-
|
|
3
|
-
from __future__ import annotations
|
|
4
|
-
|
|
5
|
-
import math
|
|
6
|
-
import re
|
|
7
|
-
from dataclasses import dataclass, field
|
|
8
|
-
|
|
9
|
-
from .utils import px_to_emu
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
@dataclass
|
|
13
|
-
class PathCommand:
|
|
14
|
-
"""A single SVG path command with its arguments."""
|
|
15
|
-
cmd: str # M, L, C, Z, etc. (uppercase = absolute)
|
|
16
|
-
args: list[float] = field(default_factory=list)
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
# Argument counts per SVG path command
|
|
20
|
-
_ARG_COUNTS = {
|
|
21
|
-
'M': 2, 'm': 2, 'L': 2, 'l': 2,
|
|
22
|
-
'H': 1, 'h': 1, 'V': 1, 'v': 1,
|
|
23
|
-
'C': 6, 'c': 6, 'S': 4, 's': 4,
|
|
24
|
-
'Q': 4, 'q': 4, 'T': 2, 't': 2,
|
|
25
|
-
'A': 7, 'a': 7, 'Z': 0, 'z': 0,
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
def parse_svg_path(d: str) -> list[PathCommand]:
|
|
30
|
-
"""Parse SVG path d attribute into a list of PathCommands."""
|
|
31
|
-
if not d:
|
|
32
|
-
return []
|
|
33
|
-
|
|
34
|
-
commands: list[PathCommand] = []
|
|
35
|
-
tokens = re.findall(
|
|
36
|
-
r'[MmLlHhVvCcSsQqTtAaZz]|[-+]?(?:\d+\.?\d*|\.\d+)(?:[eE][-+]?\d+)?', d
|
|
37
|
-
)
|
|
38
|
-
|
|
39
|
-
current_cmd: str | None = None
|
|
40
|
-
current_args: list[float] = []
|
|
41
|
-
|
|
42
|
-
def flush() -> None:
|
|
43
|
-
nonlocal current_cmd, current_args
|
|
44
|
-
if current_cmd is None:
|
|
45
|
-
return
|
|
46
|
-
|
|
47
|
-
n = _ARG_COUNTS.get(current_cmd, 0)
|
|
48
|
-
if n == 0:
|
|
49
|
-
commands.append(PathCommand(current_cmd, []))
|
|
50
|
-
elif n > 0 and len(current_args) >= n:
|
|
51
|
-
i = 0
|
|
52
|
-
while i + n <= len(current_args):
|
|
53
|
-
commands.append(PathCommand(current_cmd, current_args[i:i + n]))
|
|
54
|
-
# After first M, implicit commands become L
|
|
55
|
-
if current_cmd == 'M':
|
|
56
|
-
current_cmd = 'L'
|
|
57
|
-
elif current_cmd == 'm':
|
|
58
|
-
current_cmd = 'l'
|
|
59
|
-
i += n
|
|
60
|
-
current_args = []
|
|
61
|
-
|
|
62
|
-
for token in tokens:
|
|
63
|
-
if token in 'MmLlHhVvCcSsQqTtAaZz':
|
|
64
|
-
flush()
|
|
65
|
-
current_cmd = token
|
|
66
|
-
current_args = []
|
|
67
|
-
else:
|
|
68
|
-
try:
|
|
69
|
-
current_args.append(float(token))
|
|
70
|
-
except ValueError:
|
|
71
|
-
pass
|
|
72
|
-
|
|
73
|
-
flush()
|
|
74
|
-
return commands
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
def svg_path_to_absolute(commands: list[PathCommand]) -> list[PathCommand]:
|
|
78
|
-
"""Convert all relative path commands to absolute."""
|
|
79
|
-
result: list[PathCommand] = []
|
|
80
|
-
cx, cy = 0.0, 0.0 # current point
|
|
81
|
-
sx, sy = 0.0, 0.0 # subpath start
|
|
82
|
-
|
|
83
|
-
for cmd in commands:
|
|
84
|
-
a = cmd.args
|
|
85
|
-
if cmd.cmd == 'M':
|
|
86
|
-
cx, cy = a[0], a[1]
|
|
87
|
-
sx, sy = cx, cy
|
|
88
|
-
result.append(PathCommand('M', [cx, cy]))
|
|
89
|
-
elif cmd.cmd == 'm':
|
|
90
|
-
cx += a[0]; cy += a[1]
|
|
91
|
-
sx, sy = cx, cy
|
|
92
|
-
result.append(PathCommand('M', [cx, cy]))
|
|
93
|
-
elif cmd.cmd == 'L':
|
|
94
|
-
cx, cy = a[0], a[1]
|
|
95
|
-
result.append(PathCommand('L', [cx, cy]))
|
|
96
|
-
elif cmd.cmd == 'l':
|
|
97
|
-
cx += a[0]; cy += a[1]
|
|
98
|
-
result.append(PathCommand('L', [cx, cy]))
|
|
99
|
-
elif cmd.cmd == 'H':
|
|
100
|
-
cx = a[0]
|
|
101
|
-
result.append(PathCommand('L', [cx, cy]))
|
|
102
|
-
elif cmd.cmd == 'h':
|
|
103
|
-
cx += a[0]
|
|
104
|
-
result.append(PathCommand('L', [cx, cy]))
|
|
105
|
-
elif cmd.cmd == 'V':
|
|
106
|
-
cy = a[0]
|
|
107
|
-
result.append(PathCommand('L', [cx, cy]))
|
|
108
|
-
elif cmd.cmd == 'v':
|
|
109
|
-
cy += a[0]
|
|
110
|
-
result.append(PathCommand('L', [cx, cy]))
|
|
111
|
-
elif cmd.cmd == 'C':
|
|
112
|
-
result.append(PathCommand('C', list(a)))
|
|
113
|
-
cx, cy = a[4], a[5]
|
|
114
|
-
elif cmd.cmd == 'c':
|
|
115
|
-
abs_args = [
|
|
116
|
-
cx + a[0], cy + a[1],
|
|
117
|
-
cx + a[2], cy + a[3],
|
|
118
|
-
cx + a[4], cy + a[5],
|
|
119
|
-
]
|
|
120
|
-
result.append(PathCommand('C', abs_args))
|
|
121
|
-
cx, cy = abs_args[4], abs_args[5]
|
|
122
|
-
elif cmd.cmd == 'S':
|
|
123
|
-
result.append(PathCommand('S', list(a)))
|
|
124
|
-
cx, cy = a[2], a[3]
|
|
125
|
-
elif cmd.cmd == 's':
|
|
126
|
-
abs_args = [cx + a[0], cy + a[1], cx + a[2], cy + a[3]]
|
|
127
|
-
result.append(PathCommand('S', abs_args))
|
|
128
|
-
cx, cy = abs_args[2], abs_args[3]
|
|
129
|
-
elif cmd.cmd == 'Q':
|
|
130
|
-
result.append(PathCommand('Q', list(a)))
|
|
131
|
-
cx, cy = a[2], a[3]
|
|
132
|
-
elif cmd.cmd == 'q':
|
|
133
|
-
abs_args = [cx + a[0], cy + a[1], cx + a[2], cy + a[3]]
|
|
134
|
-
result.append(PathCommand('Q', abs_args))
|
|
135
|
-
cx, cy = abs_args[2], abs_args[3]
|
|
136
|
-
elif cmd.cmd == 'T':
|
|
137
|
-
result.append(PathCommand('T', list(a)))
|
|
138
|
-
cx, cy = a[0], a[1]
|
|
139
|
-
elif cmd.cmd == 't':
|
|
140
|
-
abs_args = [cx + a[0], cy + a[1]]
|
|
141
|
-
result.append(PathCommand('T', abs_args))
|
|
142
|
-
cx, cy = abs_args[0], abs_args[1]
|
|
143
|
-
elif cmd.cmd == 'A':
|
|
144
|
-
result.append(PathCommand('A', list(a)))
|
|
145
|
-
cx, cy = a[5], a[6]
|
|
146
|
-
elif cmd.cmd == 'a':
|
|
147
|
-
abs_args = [a[0], a[1], a[2], a[3], a[4], cx + a[5], cy + a[6]]
|
|
148
|
-
result.append(PathCommand('A', abs_args))
|
|
149
|
-
cx, cy = abs_args[5], abs_args[6]
|
|
150
|
-
elif cmd.cmd in ('Z', 'z'):
|
|
151
|
-
result.append(PathCommand('Z', []))
|
|
152
|
-
cx, cy = sx, sy
|
|
153
|
-
|
|
154
|
-
return result
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
def _reflect_control_point(
|
|
158
|
-
cp_x: float, cp_y: float,
|
|
159
|
-
cx: float, cy: float,
|
|
160
|
-
) -> tuple[float, float]:
|
|
161
|
-
"""Reflect a control point through the current point."""
|
|
162
|
-
return 2 * cx - cp_x, 2 * cy - cp_y
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
def _quad_to_cubic(
|
|
166
|
-
qp_x: float, qp_y: float,
|
|
167
|
-
p0_x: float, p0_y: float,
|
|
168
|
-
p3_x: float, p3_y: float,
|
|
169
|
-
) -> list[float]:
|
|
170
|
-
"""Convert quadratic bezier control point to cubic bezier control points."""
|
|
171
|
-
cp1_x = p0_x + 2.0 / 3.0 * (qp_x - p0_x)
|
|
172
|
-
cp1_y = p0_y + 2.0 / 3.0 * (qp_y - p0_y)
|
|
173
|
-
cp2_x = p3_x + 2.0 / 3.0 * (qp_x - p3_x)
|
|
174
|
-
cp2_y = p3_y + 2.0 / 3.0 * (qp_y - p3_y)
|
|
175
|
-
return [cp1_x, cp1_y, cp2_x, cp2_y, p3_x, p3_y]
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
def _arc_to_cubic_beziers(
|
|
179
|
-
cx_: float, cy_: float,
|
|
180
|
-
rx: float, ry: float,
|
|
181
|
-
phi: float,
|
|
182
|
-
large_arc: int, sweep: int,
|
|
183
|
-
x2: float, y2: float,
|
|
184
|
-
) -> list[PathCommand]:
|
|
185
|
-
"""Convert SVG arc (endpoint parameterization) to cubic bezier curves.
|
|
186
|
-
|
|
187
|
-
Uses the algorithm from the SVG spec (F.6.5) to convert endpoint to center
|
|
188
|
-
parameterization, then approximates each arc segment with cubic beziers.
|
|
189
|
-
"""
|
|
190
|
-
x1, y1 = cx_, cy_
|
|
191
|
-
|
|
192
|
-
if abs(x1 - x2) < 1e-10 and abs(y1 - y2) < 1e-10:
|
|
193
|
-
return []
|
|
194
|
-
|
|
195
|
-
rx = abs(rx)
|
|
196
|
-
ry = abs(ry)
|
|
197
|
-
if rx < 1e-10 or ry < 1e-10:
|
|
198
|
-
return [PathCommand('L', [x2, y2])]
|
|
199
|
-
|
|
200
|
-
phi_rad = math.radians(phi)
|
|
201
|
-
cos_phi = math.cos(phi_rad)
|
|
202
|
-
sin_phi = math.sin(phi_rad)
|
|
203
|
-
|
|
204
|
-
# Step 1: Compute (x1', y1')
|
|
205
|
-
dx = (x1 - x2) / 2.0
|
|
206
|
-
dy = (y1 - y2) / 2.0
|
|
207
|
-
x1p = cos_phi * dx + sin_phi * dy
|
|
208
|
-
y1p = -sin_phi * dx + cos_phi * dy
|
|
209
|
-
|
|
210
|
-
# Step 2: Compute (cx', cy')
|
|
211
|
-
x1p2 = x1p * x1p
|
|
212
|
-
y1p2 = y1p * y1p
|
|
213
|
-
rx2 = rx * rx
|
|
214
|
-
ry2 = ry * ry
|
|
215
|
-
|
|
216
|
-
# Ensure radii are large enough
|
|
217
|
-
lam = x1p2 / rx2 + y1p2 / ry2
|
|
218
|
-
if lam > 1:
|
|
219
|
-
lam_sqrt = math.sqrt(lam)
|
|
220
|
-
rx *= lam_sqrt
|
|
221
|
-
ry *= lam_sqrt
|
|
222
|
-
rx2 = rx * rx
|
|
223
|
-
ry2 = ry * ry
|
|
224
|
-
|
|
225
|
-
num = max(rx2 * ry2 - rx2 * y1p2 - ry2 * x1p2, 0)
|
|
226
|
-
den = rx2 * y1p2 + ry2 * x1p2
|
|
227
|
-
sq = math.sqrt(num / den) if den > 1e-10 else 0.0
|
|
228
|
-
|
|
229
|
-
if large_arc == sweep:
|
|
230
|
-
sq = -sq
|
|
231
|
-
|
|
232
|
-
cxp = sq * rx * y1p / ry
|
|
233
|
-
cyp = -sq * ry * x1p / rx
|
|
234
|
-
|
|
235
|
-
# Step 3: Compute (cx, cy)
|
|
236
|
-
arc_cx = cos_phi * cxp - sin_phi * cyp + (x1 + x2) / 2.0
|
|
237
|
-
arc_cy = sin_phi * cxp + cos_phi * cyp + (y1 + y2) / 2.0
|
|
238
|
-
|
|
239
|
-
# Step 4: Compute theta1 and dtheta
|
|
240
|
-
def angle_between(ux: float, uy: float, vx: float, vy: float) -> float:
|
|
241
|
-
n = math.sqrt((ux * ux + uy * uy) * (vx * vx + vy * vy))
|
|
242
|
-
if n < 1e-10:
|
|
243
|
-
return 0
|
|
244
|
-
c = max(-1, min(1, (ux * vx + uy * vy) / n))
|
|
245
|
-
a = math.acos(c)
|
|
246
|
-
if ux * vy - uy * vx < 0:
|
|
247
|
-
a = -a
|
|
248
|
-
return a
|
|
249
|
-
|
|
250
|
-
theta1 = angle_between(1, 0, (x1p - cxp) / rx, (y1p - cyp) / ry)
|
|
251
|
-
dtheta = angle_between(
|
|
252
|
-
(x1p - cxp) / rx, (y1p - cyp) / ry,
|
|
253
|
-
(-x1p - cxp) / rx, (-y1p - cyp) / ry,
|
|
254
|
-
)
|
|
255
|
-
|
|
256
|
-
if sweep == 0 and dtheta > 0:
|
|
257
|
-
dtheta -= 2 * math.pi
|
|
258
|
-
elif sweep == 1 and dtheta < 0:
|
|
259
|
-
dtheta += 2 * math.pi
|
|
260
|
-
|
|
261
|
-
# Split arc into segments of at most 90 degrees
|
|
262
|
-
n_segs = max(1, int(math.ceil(abs(dtheta) / (math.pi / 2))))
|
|
263
|
-
d_per_seg = dtheta / n_segs
|
|
264
|
-
|
|
265
|
-
result: list[PathCommand] = []
|
|
266
|
-
alpha = 4.0 / 3.0 * math.tan(d_per_seg / 4.0)
|
|
267
|
-
|
|
268
|
-
for i in range(n_segs):
|
|
269
|
-
t1 = theta1 + i * d_per_seg
|
|
270
|
-
t2 = theta1 + (i + 1) * d_per_seg
|
|
271
|
-
|
|
272
|
-
cos_t1 = math.cos(t1)
|
|
273
|
-
sin_t1 = math.sin(t1)
|
|
274
|
-
cos_t2 = math.cos(t2)
|
|
275
|
-
sin_t2 = math.sin(t2)
|
|
276
|
-
|
|
277
|
-
ep1_x = cos_t1 - alpha * sin_t1
|
|
278
|
-
ep1_y = sin_t1 + alpha * cos_t1
|
|
279
|
-
ep2_x = cos_t2 + alpha * sin_t2
|
|
280
|
-
ep2_y = sin_t2 - alpha * cos_t2
|
|
281
|
-
ep_x = cos_t2
|
|
282
|
-
ep_y = sin_t2
|
|
283
|
-
|
|
284
|
-
def transform_pt(px: float, py: float) -> tuple[float, float]:
|
|
285
|
-
x = rx * px
|
|
286
|
-
y = ry * py
|
|
287
|
-
xr = cos_phi * x - sin_phi * y + arc_cx
|
|
288
|
-
yr = sin_phi * x + cos_phi * y + arc_cy
|
|
289
|
-
return xr, yr
|
|
290
|
-
|
|
291
|
-
cp1 = transform_pt(ep1_x, ep1_y)
|
|
292
|
-
cp2 = transform_pt(ep2_x, ep2_y)
|
|
293
|
-
ep = transform_pt(ep_x, ep_y)
|
|
294
|
-
|
|
295
|
-
result.append(PathCommand('C', [cp1[0], cp1[1], cp2[0], cp2[1], ep[0], ep[1]]))
|
|
296
|
-
|
|
297
|
-
return result
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
def normalize_path_commands(commands: list[PathCommand]) -> list[PathCommand]:
|
|
301
|
-
"""Normalize path commands to M/L/C/Z only.
|
|
302
|
-
|
|
303
|
-
Converts S -> C, Q -> C, T -> C, A -> C sequences.
|
|
304
|
-
"""
|
|
305
|
-
result: list[PathCommand] = []
|
|
306
|
-
cx, cy = 0.0, 0.0
|
|
307
|
-
last_cp_x, last_cp_y = 0.0, 0.0
|
|
308
|
-
last_cmd = ''
|
|
309
|
-
|
|
310
|
-
for cmd in commands:
|
|
311
|
-
a = cmd.args
|
|
312
|
-
|
|
313
|
-
if cmd.cmd == 'M':
|
|
314
|
-
cx, cy = a[0], a[1]
|
|
315
|
-
last_cp_x, last_cp_y = cx, cy
|
|
316
|
-
result.append(cmd)
|
|
317
|
-
elif cmd.cmd == 'L':
|
|
318
|
-
cx, cy = a[0], a[1]
|
|
319
|
-
last_cp_x, last_cp_y = cx, cy
|
|
320
|
-
result.append(cmd)
|
|
321
|
-
elif cmd.cmd == 'C':
|
|
322
|
-
last_cp_x, last_cp_y = a[2], a[3]
|
|
323
|
-
cx, cy = a[4], a[5]
|
|
324
|
-
result.append(cmd)
|
|
325
|
-
elif cmd.cmd == 'S':
|
|
326
|
-
if last_cmd in ('C', 'S'):
|
|
327
|
-
rcp_x, rcp_y = _reflect_control_point(last_cp_x, last_cp_y, cx, cy)
|
|
328
|
-
else:
|
|
329
|
-
rcp_x, rcp_y = cx, cy
|
|
330
|
-
last_cp_x, last_cp_y = a[0], a[1]
|
|
331
|
-
new_cx, new_cy = a[2], a[3]
|
|
332
|
-
result.append(PathCommand('C', [rcp_x, rcp_y, a[0], a[1], new_cx, new_cy]))
|
|
333
|
-
cx, cy = new_cx, new_cy
|
|
334
|
-
elif cmd.cmd == 'Q':
|
|
335
|
-
cubic = _quad_to_cubic(a[0], a[1], cx, cy, a[2], a[3])
|
|
336
|
-
last_cp_x, last_cp_y = a[0], a[1]
|
|
337
|
-
result.append(PathCommand('C', cubic))
|
|
338
|
-
cx, cy = a[2], a[3]
|
|
339
|
-
elif cmd.cmd == 'T':
|
|
340
|
-
if last_cmd in ('Q', 'T'):
|
|
341
|
-
qp_x, qp_y = _reflect_control_point(last_cp_x, last_cp_y, cx, cy)
|
|
342
|
-
else:
|
|
343
|
-
qp_x, qp_y = cx, cy
|
|
344
|
-
last_cp_x, last_cp_y = qp_x, qp_y
|
|
345
|
-
cubic = _quad_to_cubic(qp_x, qp_y, cx, cy, a[0], a[1])
|
|
346
|
-
result.append(PathCommand('C', cubic))
|
|
347
|
-
cx, cy = a[0], a[1]
|
|
348
|
-
elif cmd.cmd == 'A':
|
|
349
|
-
arc_beziers = _arc_to_cubic_beziers(
|
|
350
|
-
cx, cy, a[0], a[1], a[2], int(a[3]), int(a[4]), a[5], a[6],
|
|
351
|
-
)
|
|
352
|
-
for bc in arc_beziers:
|
|
353
|
-
result.append(bc)
|
|
354
|
-
cx, cy = a[5], a[6]
|
|
355
|
-
last_cp_x, last_cp_y = cx, cy
|
|
356
|
-
elif cmd.cmd == 'Z':
|
|
357
|
-
result.append(cmd)
|
|
358
|
-
else:
|
|
359
|
-
result.append(cmd)
|
|
360
|
-
|
|
361
|
-
last_cmd = cmd.cmd
|
|
362
|
-
|
|
363
|
-
return result
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
def path_commands_to_drawingml(
|
|
367
|
-
commands: list[PathCommand],
|
|
368
|
-
offset_x: float = 0,
|
|
369
|
-
offset_y: float = 0,
|
|
370
|
-
scale_x: float = 1.0,
|
|
371
|
-
scale_y: float = 1.0,
|
|
372
|
-
) -> tuple[str, float, float, float, float]:
|
|
373
|
-
"""Convert normalized path commands to DrawingML <a:path> inner XML.
|
|
374
|
-
|
|
375
|
-
Returns:
|
|
376
|
-
(path_xml, min_x, min_y, width, height) in scaled+offset coordinates.
|
|
377
|
-
"""
|
|
378
|
-
if not commands:
|
|
379
|
-
return '', 0, 0, 0, 0
|
|
380
|
-
|
|
381
|
-
# First pass: calculate bounding box
|
|
382
|
-
points: list[tuple[float, float]] = []
|
|
383
|
-
for cmd in commands:
|
|
384
|
-
if cmd.cmd in ('M', 'L'):
|
|
385
|
-
points.append((
|
|
386
|
-
cmd.args[0] * scale_x + offset_x,
|
|
387
|
-
cmd.args[1] * scale_y + offset_y,
|
|
388
|
-
))
|
|
389
|
-
elif cmd.cmd == 'C':
|
|
390
|
-
for i in range(0, 6, 2):
|
|
391
|
-
points.append((
|
|
392
|
-
cmd.args[i] * scale_x + offset_x,
|
|
393
|
-
cmd.args[i + 1] * scale_y + offset_y,
|
|
394
|
-
))
|
|
395
|
-
|
|
396
|
-
if not points:
|
|
397
|
-
return '', 0, 0, 0, 0
|
|
398
|
-
|
|
399
|
-
min_x = min(p[0] for p in points)
|
|
400
|
-
min_y = min(p[1] for p in points)
|
|
401
|
-
max_x = max(p[0] for p in points)
|
|
402
|
-
max_y = max(p[1] for p in points)
|
|
403
|
-
|
|
404
|
-
width = max(max_x - min_x, 1)
|
|
405
|
-
height = max(max_y - min_y, 1)
|
|
406
|
-
|
|
407
|
-
# Second pass: generate DrawingML path commands (EMU, relative to shape)
|
|
408
|
-
parts: list[str] = []
|
|
409
|
-
for cmd in commands:
|
|
410
|
-
if cmd.cmd == 'M':
|
|
411
|
-
x_emu = px_to_emu(cmd.args[0] * scale_x + offset_x - min_x)
|
|
412
|
-
y_emu = px_to_emu(cmd.args[1] * scale_y + offset_y - min_y)
|
|
413
|
-
parts.append(f'<a:moveTo><a:pt x="{x_emu}" y="{y_emu}"/></a:moveTo>')
|
|
414
|
-
elif cmd.cmd == 'L':
|
|
415
|
-
x_emu = px_to_emu(cmd.args[0] * scale_x + offset_x - min_x)
|
|
416
|
-
y_emu = px_to_emu(cmd.args[1] * scale_y + offset_y - min_y)
|
|
417
|
-
parts.append(f'<a:lnTo><a:pt x="{x_emu}" y="{y_emu}"/></a:lnTo>')
|
|
418
|
-
elif cmd.cmd == 'C':
|
|
419
|
-
pts = []
|
|
420
|
-
for i in range(0, 6, 2):
|
|
421
|
-
x_emu = px_to_emu(cmd.args[i] * scale_x + offset_x - min_x)
|
|
422
|
-
y_emu = px_to_emu(cmd.args[i + 1] * scale_y + offset_y - min_y)
|
|
423
|
-
pts.append(f'<a:pt x="{x_emu}" y="{y_emu}"/>')
|
|
424
|
-
parts.append(f'<a:cubicBezTo>{"".join(pts)}</a:cubicBezTo>')
|
|
425
|
-
elif cmd.cmd == 'Z':
|
|
426
|
-
parts.append('<a:close/>')
|
|
427
|
-
|
|
428
|
-
path_inner = '\n'.join(parts)
|
|
429
|
-
return path_inner, min_x, min_y, width, height
|
|
1
|
+
"""SVG path parsing, normalization, and DrawingML path command generation."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import math
|
|
6
|
+
import re
|
|
7
|
+
from dataclasses import dataclass, field
|
|
8
|
+
|
|
9
|
+
from .utils import px_to_emu
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@dataclass
|
|
13
|
+
class PathCommand:
|
|
14
|
+
"""A single SVG path command with its arguments."""
|
|
15
|
+
cmd: str # M, L, C, Z, etc. (uppercase = absolute)
|
|
16
|
+
args: list[float] = field(default_factory=list)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
# Argument counts per SVG path command
|
|
20
|
+
_ARG_COUNTS = {
|
|
21
|
+
'M': 2, 'm': 2, 'L': 2, 'l': 2,
|
|
22
|
+
'H': 1, 'h': 1, 'V': 1, 'v': 1,
|
|
23
|
+
'C': 6, 'c': 6, 'S': 4, 's': 4,
|
|
24
|
+
'Q': 4, 'q': 4, 'T': 2, 't': 2,
|
|
25
|
+
'A': 7, 'a': 7, 'Z': 0, 'z': 0,
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def parse_svg_path(d: str) -> list[PathCommand]:
|
|
30
|
+
"""Parse SVG path d attribute into a list of PathCommands."""
|
|
31
|
+
if not d:
|
|
32
|
+
return []
|
|
33
|
+
|
|
34
|
+
commands: list[PathCommand] = []
|
|
35
|
+
tokens = re.findall(
|
|
36
|
+
r'[MmLlHhVvCcSsQqTtAaZz]|[-+]?(?:\d+\.?\d*|\.\d+)(?:[eE][-+]?\d+)?', d
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
current_cmd: str | None = None
|
|
40
|
+
current_args: list[float] = []
|
|
41
|
+
|
|
42
|
+
def flush() -> None:
|
|
43
|
+
nonlocal current_cmd, current_args
|
|
44
|
+
if current_cmd is None:
|
|
45
|
+
return
|
|
46
|
+
|
|
47
|
+
n = _ARG_COUNTS.get(current_cmd, 0)
|
|
48
|
+
if n == 0:
|
|
49
|
+
commands.append(PathCommand(current_cmd, []))
|
|
50
|
+
elif n > 0 and len(current_args) >= n:
|
|
51
|
+
i = 0
|
|
52
|
+
while i + n <= len(current_args):
|
|
53
|
+
commands.append(PathCommand(current_cmd, current_args[i:i + n]))
|
|
54
|
+
# After first M, implicit commands become L
|
|
55
|
+
if current_cmd == 'M':
|
|
56
|
+
current_cmd = 'L'
|
|
57
|
+
elif current_cmd == 'm':
|
|
58
|
+
current_cmd = 'l'
|
|
59
|
+
i += n
|
|
60
|
+
current_args = []
|
|
61
|
+
|
|
62
|
+
for token in tokens:
|
|
63
|
+
if token in 'MmLlHhVvCcSsQqTtAaZz':
|
|
64
|
+
flush()
|
|
65
|
+
current_cmd = token
|
|
66
|
+
current_args = []
|
|
67
|
+
else:
|
|
68
|
+
try:
|
|
69
|
+
current_args.append(float(token))
|
|
70
|
+
except ValueError:
|
|
71
|
+
pass
|
|
72
|
+
|
|
73
|
+
flush()
|
|
74
|
+
return commands
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def svg_path_to_absolute(commands: list[PathCommand]) -> list[PathCommand]:
|
|
78
|
+
"""Convert all relative path commands to absolute."""
|
|
79
|
+
result: list[PathCommand] = []
|
|
80
|
+
cx, cy = 0.0, 0.0 # current point
|
|
81
|
+
sx, sy = 0.0, 0.0 # subpath start
|
|
82
|
+
|
|
83
|
+
for cmd in commands:
|
|
84
|
+
a = cmd.args
|
|
85
|
+
if cmd.cmd == 'M':
|
|
86
|
+
cx, cy = a[0], a[1]
|
|
87
|
+
sx, sy = cx, cy
|
|
88
|
+
result.append(PathCommand('M', [cx, cy]))
|
|
89
|
+
elif cmd.cmd == 'm':
|
|
90
|
+
cx += a[0]; cy += a[1]
|
|
91
|
+
sx, sy = cx, cy
|
|
92
|
+
result.append(PathCommand('M', [cx, cy]))
|
|
93
|
+
elif cmd.cmd == 'L':
|
|
94
|
+
cx, cy = a[0], a[1]
|
|
95
|
+
result.append(PathCommand('L', [cx, cy]))
|
|
96
|
+
elif cmd.cmd == 'l':
|
|
97
|
+
cx += a[0]; cy += a[1]
|
|
98
|
+
result.append(PathCommand('L', [cx, cy]))
|
|
99
|
+
elif cmd.cmd == 'H':
|
|
100
|
+
cx = a[0]
|
|
101
|
+
result.append(PathCommand('L', [cx, cy]))
|
|
102
|
+
elif cmd.cmd == 'h':
|
|
103
|
+
cx += a[0]
|
|
104
|
+
result.append(PathCommand('L', [cx, cy]))
|
|
105
|
+
elif cmd.cmd == 'V':
|
|
106
|
+
cy = a[0]
|
|
107
|
+
result.append(PathCommand('L', [cx, cy]))
|
|
108
|
+
elif cmd.cmd == 'v':
|
|
109
|
+
cy += a[0]
|
|
110
|
+
result.append(PathCommand('L', [cx, cy]))
|
|
111
|
+
elif cmd.cmd == 'C':
|
|
112
|
+
result.append(PathCommand('C', list(a)))
|
|
113
|
+
cx, cy = a[4], a[5]
|
|
114
|
+
elif cmd.cmd == 'c':
|
|
115
|
+
abs_args = [
|
|
116
|
+
cx + a[0], cy + a[1],
|
|
117
|
+
cx + a[2], cy + a[3],
|
|
118
|
+
cx + a[4], cy + a[5],
|
|
119
|
+
]
|
|
120
|
+
result.append(PathCommand('C', abs_args))
|
|
121
|
+
cx, cy = abs_args[4], abs_args[5]
|
|
122
|
+
elif cmd.cmd == 'S':
|
|
123
|
+
result.append(PathCommand('S', list(a)))
|
|
124
|
+
cx, cy = a[2], a[3]
|
|
125
|
+
elif cmd.cmd == 's':
|
|
126
|
+
abs_args = [cx + a[0], cy + a[1], cx + a[2], cy + a[3]]
|
|
127
|
+
result.append(PathCommand('S', abs_args))
|
|
128
|
+
cx, cy = abs_args[2], abs_args[3]
|
|
129
|
+
elif cmd.cmd == 'Q':
|
|
130
|
+
result.append(PathCommand('Q', list(a)))
|
|
131
|
+
cx, cy = a[2], a[3]
|
|
132
|
+
elif cmd.cmd == 'q':
|
|
133
|
+
abs_args = [cx + a[0], cy + a[1], cx + a[2], cy + a[3]]
|
|
134
|
+
result.append(PathCommand('Q', abs_args))
|
|
135
|
+
cx, cy = abs_args[2], abs_args[3]
|
|
136
|
+
elif cmd.cmd == 'T':
|
|
137
|
+
result.append(PathCommand('T', list(a)))
|
|
138
|
+
cx, cy = a[0], a[1]
|
|
139
|
+
elif cmd.cmd == 't':
|
|
140
|
+
abs_args = [cx + a[0], cy + a[1]]
|
|
141
|
+
result.append(PathCommand('T', abs_args))
|
|
142
|
+
cx, cy = abs_args[0], abs_args[1]
|
|
143
|
+
elif cmd.cmd == 'A':
|
|
144
|
+
result.append(PathCommand('A', list(a)))
|
|
145
|
+
cx, cy = a[5], a[6]
|
|
146
|
+
elif cmd.cmd == 'a':
|
|
147
|
+
abs_args = [a[0], a[1], a[2], a[3], a[4], cx + a[5], cy + a[6]]
|
|
148
|
+
result.append(PathCommand('A', abs_args))
|
|
149
|
+
cx, cy = abs_args[5], abs_args[6]
|
|
150
|
+
elif cmd.cmd in ('Z', 'z'):
|
|
151
|
+
result.append(PathCommand('Z', []))
|
|
152
|
+
cx, cy = sx, sy
|
|
153
|
+
|
|
154
|
+
return result
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def _reflect_control_point(
|
|
158
|
+
cp_x: float, cp_y: float,
|
|
159
|
+
cx: float, cy: float,
|
|
160
|
+
) -> tuple[float, float]:
|
|
161
|
+
"""Reflect a control point through the current point."""
|
|
162
|
+
return 2 * cx - cp_x, 2 * cy - cp_y
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def _quad_to_cubic(
|
|
166
|
+
qp_x: float, qp_y: float,
|
|
167
|
+
p0_x: float, p0_y: float,
|
|
168
|
+
p3_x: float, p3_y: float,
|
|
169
|
+
) -> list[float]:
|
|
170
|
+
"""Convert quadratic bezier control point to cubic bezier control points."""
|
|
171
|
+
cp1_x = p0_x + 2.0 / 3.0 * (qp_x - p0_x)
|
|
172
|
+
cp1_y = p0_y + 2.0 / 3.0 * (qp_y - p0_y)
|
|
173
|
+
cp2_x = p3_x + 2.0 / 3.0 * (qp_x - p3_x)
|
|
174
|
+
cp2_y = p3_y + 2.0 / 3.0 * (qp_y - p3_y)
|
|
175
|
+
return [cp1_x, cp1_y, cp2_x, cp2_y, p3_x, p3_y]
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
def _arc_to_cubic_beziers(
|
|
179
|
+
cx_: float, cy_: float,
|
|
180
|
+
rx: float, ry: float,
|
|
181
|
+
phi: float,
|
|
182
|
+
large_arc: int, sweep: int,
|
|
183
|
+
x2: float, y2: float,
|
|
184
|
+
) -> list[PathCommand]:
|
|
185
|
+
"""Convert SVG arc (endpoint parameterization) to cubic bezier curves.
|
|
186
|
+
|
|
187
|
+
Uses the algorithm from the SVG spec (F.6.5) to convert endpoint to center
|
|
188
|
+
parameterization, then approximates each arc segment with cubic beziers.
|
|
189
|
+
"""
|
|
190
|
+
x1, y1 = cx_, cy_
|
|
191
|
+
|
|
192
|
+
if abs(x1 - x2) < 1e-10 and abs(y1 - y2) < 1e-10:
|
|
193
|
+
return []
|
|
194
|
+
|
|
195
|
+
rx = abs(rx)
|
|
196
|
+
ry = abs(ry)
|
|
197
|
+
if rx < 1e-10 or ry < 1e-10:
|
|
198
|
+
return [PathCommand('L', [x2, y2])]
|
|
199
|
+
|
|
200
|
+
phi_rad = math.radians(phi)
|
|
201
|
+
cos_phi = math.cos(phi_rad)
|
|
202
|
+
sin_phi = math.sin(phi_rad)
|
|
203
|
+
|
|
204
|
+
# Step 1: Compute (x1', y1')
|
|
205
|
+
dx = (x1 - x2) / 2.0
|
|
206
|
+
dy = (y1 - y2) / 2.0
|
|
207
|
+
x1p = cos_phi * dx + sin_phi * dy
|
|
208
|
+
y1p = -sin_phi * dx + cos_phi * dy
|
|
209
|
+
|
|
210
|
+
# Step 2: Compute (cx', cy')
|
|
211
|
+
x1p2 = x1p * x1p
|
|
212
|
+
y1p2 = y1p * y1p
|
|
213
|
+
rx2 = rx * rx
|
|
214
|
+
ry2 = ry * ry
|
|
215
|
+
|
|
216
|
+
# Ensure radii are large enough
|
|
217
|
+
lam = x1p2 / rx2 + y1p2 / ry2
|
|
218
|
+
if lam > 1:
|
|
219
|
+
lam_sqrt = math.sqrt(lam)
|
|
220
|
+
rx *= lam_sqrt
|
|
221
|
+
ry *= lam_sqrt
|
|
222
|
+
rx2 = rx * rx
|
|
223
|
+
ry2 = ry * ry
|
|
224
|
+
|
|
225
|
+
num = max(rx2 * ry2 - rx2 * y1p2 - ry2 * x1p2, 0)
|
|
226
|
+
den = rx2 * y1p2 + ry2 * x1p2
|
|
227
|
+
sq = math.sqrt(num / den) if den > 1e-10 else 0.0
|
|
228
|
+
|
|
229
|
+
if large_arc == sweep:
|
|
230
|
+
sq = -sq
|
|
231
|
+
|
|
232
|
+
cxp = sq * rx * y1p / ry
|
|
233
|
+
cyp = -sq * ry * x1p / rx
|
|
234
|
+
|
|
235
|
+
# Step 3: Compute (cx, cy)
|
|
236
|
+
arc_cx = cos_phi * cxp - sin_phi * cyp + (x1 + x2) / 2.0
|
|
237
|
+
arc_cy = sin_phi * cxp + cos_phi * cyp + (y1 + y2) / 2.0
|
|
238
|
+
|
|
239
|
+
# Step 4: Compute theta1 and dtheta
|
|
240
|
+
def angle_between(ux: float, uy: float, vx: float, vy: float) -> float:
|
|
241
|
+
n = math.sqrt((ux * ux + uy * uy) * (vx * vx + vy * vy))
|
|
242
|
+
if n < 1e-10:
|
|
243
|
+
return 0
|
|
244
|
+
c = max(-1, min(1, (ux * vx + uy * vy) / n))
|
|
245
|
+
a = math.acos(c)
|
|
246
|
+
if ux * vy - uy * vx < 0:
|
|
247
|
+
a = -a
|
|
248
|
+
return a
|
|
249
|
+
|
|
250
|
+
theta1 = angle_between(1, 0, (x1p - cxp) / rx, (y1p - cyp) / ry)
|
|
251
|
+
dtheta = angle_between(
|
|
252
|
+
(x1p - cxp) / rx, (y1p - cyp) / ry,
|
|
253
|
+
(-x1p - cxp) / rx, (-y1p - cyp) / ry,
|
|
254
|
+
)
|
|
255
|
+
|
|
256
|
+
if sweep == 0 and dtheta > 0:
|
|
257
|
+
dtheta -= 2 * math.pi
|
|
258
|
+
elif sweep == 1 and dtheta < 0:
|
|
259
|
+
dtheta += 2 * math.pi
|
|
260
|
+
|
|
261
|
+
# Split arc into segments of at most 90 degrees
|
|
262
|
+
n_segs = max(1, int(math.ceil(abs(dtheta) / (math.pi / 2))))
|
|
263
|
+
d_per_seg = dtheta / n_segs
|
|
264
|
+
|
|
265
|
+
result: list[PathCommand] = []
|
|
266
|
+
alpha = 4.0 / 3.0 * math.tan(d_per_seg / 4.0)
|
|
267
|
+
|
|
268
|
+
for i in range(n_segs):
|
|
269
|
+
t1 = theta1 + i * d_per_seg
|
|
270
|
+
t2 = theta1 + (i + 1) * d_per_seg
|
|
271
|
+
|
|
272
|
+
cos_t1 = math.cos(t1)
|
|
273
|
+
sin_t1 = math.sin(t1)
|
|
274
|
+
cos_t2 = math.cos(t2)
|
|
275
|
+
sin_t2 = math.sin(t2)
|
|
276
|
+
|
|
277
|
+
ep1_x = cos_t1 - alpha * sin_t1
|
|
278
|
+
ep1_y = sin_t1 + alpha * cos_t1
|
|
279
|
+
ep2_x = cos_t2 + alpha * sin_t2
|
|
280
|
+
ep2_y = sin_t2 - alpha * cos_t2
|
|
281
|
+
ep_x = cos_t2
|
|
282
|
+
ep_y = sin_t2
|
|
283
|
+
|
|
284
|
+
def transform_pt(px: float, py: float) -> tuple[float, float]:
|
|
285
|
+
x = rx * px
|
|
286
|
+
y = ry * py
|
|
287
|
+
xr = cos_phi * x - sin_phi * y + arc_cx
|
|
288
|
+
yr = sin_phi * x + cos_phi * y + arc_cy
|
|
289
|
+
return xr, yr
|
|
290
|
+
|
|
291
|
+
cp1 = transform_pt(ep1_x, ep1_y)
|
|
292
|
+
cp2 = transform_pt(ep2_x, ep2_y)
|
|
293
|
+
ep = transform_pt(ep_x, ep_y)
|
|
294
|
+
|
|
295
|
+
result.append(PathCommand('C', [cp1[0], cp1[1], cp2[0], cp2[1], ep[0], ep[1]]))
|
|
296
|
+
|
|
297
|
+
return result
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
def normalize_path_commands(commands: list[PathCommand]) -> list[PathCommand]:
|
|
301
|
+
"""Normalize path commands to M/L/C/Z only.
|
|
302
|
+
|
|
303
|
+
Converts S -> C, Q -> C, T -> C, A -> C sequences.
|
|
304
|
+
"""
|
|
305
|
+
result: list[PathCommand] = []
|
|
306
|
+
cx, cy = 0.0, 0.0
|
|
307
|
+
last_cp_x, last_cp_y = 0.0, 0.0
|
|
308
|
+
last_cmd = ''
|
|
309
|
+
|
|
310
|
+
for cmd in commands:
|
|
311
|
+
a = cmd.args
|
|
312
|
+
|
|
313
|
+
if cmd.cmd == 'M':
|
|
314
|
+
cx, cy = a[0], a[1]
|
|
315
|
+
last_cp_x, last_cp_y = cx, cy
|
|
316
|
+
result.append(cmd)
|
|
317
|
+
elif cmd.cmd == 'L':
|
|
318
|
+
cx, cy = a[0], a[1]
|
|
319
|
+
last_cp_x, last_cp_y = cx, cy
|
|
320
|
+
result.append(cmd)
|
|
321
|
+
elif cmd.cmd == 'C':
|
|
322
|
+
last_cp_x, last_cp_y = a[2], a[3]
|
|
323
|
+
cx, cy = a[4], a[5]
|
|
324
|
+
result.append(cmd)
|
|
325
|
+
elif cmd.cmd == 'S':
|
|
326
|
+
if last_cmd in ('C', 'S'):
|
|
327
|
+
rcp_x, rcp_y = _reflect_control_point(last_cp_x, last_cp_y, cx, cy)
|
|
328
|
+
else:
|
|
329
|
+
rcp_x, rcp_y = cx, cy
|
|
330
|
+
last_cp_x, last_cp_y = a[0], a[1]
|
|
331
|
+
new_cx, new_cy = a[2], a[3]
|
|
332
|
+
result.append(PathCommand('C', [rcp_x, rcp_y, a[0], a[1], new_cx, new_cy]))
|
|
333
|
+
cx, cy = new_cx, new_cy
|
|
334
|
+
elif cmd.cmd == 'Q':
|
|
335
|
+
cubic = _quad_to_cubic(a[0], a[1], cx, cy, a[2], a[3])
|
|
336
|
+
last_cp_x, last_cp_y = a[0], a[1]
|
|
337
|
+
result.append(PathCommand('C', cubic))
|
|
338
|
+
cx, cy = a[2], a[3]
|
|
339
|
+
elif cmd.cmd == 'T':
|
|
340
|
+
if last_cmd in ('Q', 'T'):
|
|
341
|
+
qp_x, qp_y = _reflect_control_point(last_cp_x, last_cp_y, cx, cy)
|
|
342
|
+
else:
|
|
343
|
+
qp_x, qp_y = cx, cy
|
|
344
|
+
last_cp_x, last_cp_y = qp_x, qp_y
|
|
345
|
+
cubic = _quad_to_cubic(qp_x, qp_y, cx, cy, a[0], a[1])
|
|
346
|
+
result.append(PathCommand('C', cubic))
|
|
347
|
+
cx, cy = a[0], a[1]
|
|
348
|
+
elif cmd.cmd == 'A':
|
|
349
|
+
arc_beziers = _arc_to_cubic_beziers(
|
|
350
|
+
cx, cy, a[0], a[1], a[2], int(a[3]), int(a[4]), a[5], a[6],
|
|
351
|
+
)
|
|
352
|
+
for bc in arc_beziers:
|
|
353
|
+
result.append(bc)
|
|
354
|
+
cx, cy = a[5], a[6]
|
|
355
|
+
last_cp_x, last_cp_y = cx, cy
|
|
356
|
+
elif cmd.cmd == 'Z':
|
|
357
|
+
result.append(cmd)
|
|
358
|
+
else:
|
|
359
|
+
result.append(cmd)
|
|
360
|
+
|
|
361
|
+
last_cmd = cmd.cmd
|
|
362
|
+
|
|
363
|
+
return result
|
|
364
|
+
|
|
365
|
+
|
|
366
|
+
def path_commands_to_drawingml(
|
|
367
|
+
commands: list[PathCommand],
|
|
368
|
+
offset_x: float = 0,
|
|
369
|
+
offset_y: float = 0,
|
|
370
|
+
scale_x: float = 1.0,
|
|
371
|
+
scale_y: float = 1.0,
|
|
372
|
+
) -> tuple[str, float, float, float, float]:
|
|
373
|
+
"""Convert normalized path commands to DrawingML <a:path> inner XML.
|
|
374
|
+
|
|
375
|
+
Returns:
|
|
376
|
+
(path_xml, min_x, min_y, width, height) in scaled+offset coordinates.
|
|
377
|
+
"""
|
|
378
|
+
if not commands:
|
|
379
|
+
return '', 0, 0, 0, 0
|
|
380
|
+
|
|
381
|
+
# First pass: calculate bounding box
|
|
382
|
+
points: list[tuple[float, float]] = []
|
|
383
|
+
for cmd in commands:
|
|
384
|
+
if cmd.cmd in ('M', 'L'):
|
|
385
|
+
points.append((
|
|
386
|
+
cmd.args[0] * scale_x + offset_x,
|
|
387
|
+
cmd.args[1] * scale_y + offset_y,
|
|
388
|
+
))
|
|
389
|
+
elif cmd.cmd == 'C':
|
|
390
|
+
for i in range(0, 6, 2):
|
|
391
|
+
points.append((
|
|
392
|
+
cmd.args[i] * scale_x + offset_x,
|
|
393
|
+
cmd.args[i + 1] * scale_y + offset_y,
|
|
394
|
+
))
|
|
395
|
+
|
|
396
|
+
if not points:
|
|
397
|
+
return '', 0, 0, 0, 0
|
|
398
|
+
|
|
399
|
+
min_x = min(p[0] for p in points)
|
|
400
|
+
min_y = min(p[1] for p in points)
|
|
401
|
+
max_x = max(p[0] for p in points)
|
|
402
|
+
max_y = max(p[1] for p in points)
|
|
403
|
+
|
|
404
|
+
width = max(max_x - min_x, 1)
|
|
405
|
+
height = max(max_y - min_y, 1)
|
|
406
|
+
|
|
407
|
+
# Second pass: generate DrawingML path commands (EMU, relative to shape)
|
|
408
|
+
parts: list[str] = []
|
|
409
|
+
for cmd in commands:
|
|
410
|
+
if cmd.cmd == 'M':
|
|
411
|
+
x_emu = px_to_emu(cmd.args[0] * scale_x + offset_x - min_x)
|
|
412
|
+
y_emu = px_to_emu(cmd.args[1] * scale_y + offset_y - min_y)
|
|
413
|
+
parts.append(f'<a:moveTo><a:pt x="{x_emu}" y="{y_emu}"/></a:moveTo>')
|
|
414
|
+
elif cmd.cmd == 'L':
|
|
415
|
+
x_emu = px_to_emu(cmd.args[0] * scale_x + offset_x - min_x)
|
|
416
|
+
y_emu = px_to_emu(cmd.args[1] * scale_y + offset_y - min_y)
|
|
417
|
+
parts.append(f'<a:lnTo><a:pt x="{x_emu}" y="{y_emu}"/></a:lnTo>')
|
|
418
|
+
elif cmd.cmd == 'C':
|
|
419
|
+
pts = []
|
|
420
|
+
for i in range(0, 6, 2):
|
|
421
|
+
x_emu = px_to_emu(cmd.args[i] * scale_x + offset_x - min_x)
|
|
422
|
+
y_emu = px_to_emu(cmd.args[i + 1] * scale_y + offset_y - min_y)
|
|
423
|
+
pts.append(f'<a:pt x="{x_emu}" y="{y_emu}"/>')
|
|
424
|
+
parts.append(f'<a:cubicBezTo>{"".join(pts)}</a:cubicBezTo>')
|
|
425
|
+
elif cmd.cmd == 'Z':
|
|
426
|
+
parts.append('<a:close/>')
|
|
427
|
+
|
|
428
|
+
path_inner = '\n'.join(parts)
|
|
429
|
+
return path_inner, min_x, min_y, width, height
|