@hfyjq/crannia 1.1.1 → 1.2.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.
Files changed (306) hide show
  1. package/LICENSE +201 -201
  2. package/NOTICE +48 -45
  3. package/README.md +1 -1
  4. package/THIRD-PARTY-FRONTEND.md +5266 -5266
  5. package/THIRD-PARTY-PYTHON-linux.md +174 -0
  6. package/THIRD-PARTY-PYTHON.md +176 -162
  7. package/THIRD-PARTY.md +323 -316
  8. package/apps/desktop/quotes.txt +56 -56
  9. package/bus/core.py +677 -671
  10. package/bus/loaders/channel_loader.py +64 -64
  11. package/bus/loaders/provider_loader.py +4 -7
  12. package/bus/loaders/skill_loader.py +46 -27
  13. package/bus/loaders/skill_md.py +9 -9
  14. package/bus/mcp_disabled.py +4 -6
  15. package/bus/quarantine.py +10 -13
  16. package/bus/registry.py +2315 -2284
  17. package/bus/skill_state.py +124 -124
  18. package/channels/cli_channel/handler.py +3 -3
  19. package/channels/cli_channel/manifest.yaml +5 -5
  20. package/channels/dingtalk_channel/handler.py +32 -32
  21. package/channels/dingtalk_channel/manifest.yaml +12 -12
  22. package/channels/feishu_channel/handler.py +33 -33
  23. package/channels/feishu_channel/manifest.yaml +12 -12
  24. package/channels/qq_bot_channel/__init__.py +1 -1
  25. package/channels/qq_bot_channel/handler.py +24 -24
  26. package/channels/qq_bot_channel/manifest.yaml +4 -4
  27. package/channels/telegram_channel/__init__.py +1 -1
  28. package/channels/telegram_channel/handler.py +21 -21
  29. package/channels/telegram_channel/manifest.yaml +4 -4
  30. package/channels/telegram_channel/media.py +231 -231
  31. package/channels/textfmt.py +58 -58
  32. package/channels/wechat_channel/__init__.py +1 -1
  33. package/channels/wechat_channel/converter.py +1 -1
  34. package/channels/wechat_channel/handler.py +8 -8
  35. package/channels/wechat_channel/manifest.yaml +5 -5
  36. package/channels/wechat_channel/media.py +178 -178
  37. package/core/activity.py +753 -709
  38. package/core/agent_loop.py +7673 -7523
  39. package/core/allowlist.py +2 -3
  40. package/core/approval.py +36 -26
  41. package/core/approval_timeout.py +256 -228
  42. package/core/artifacts.py +3 -3
  43. package/core/attachment_gc.py +21 -0
  44. package/core/auto_review.py +2 -2
  45. package/core/browser/cdp.py +5 -2
  46. package/core/browser/discover.py +55 -19
  47. package/core/browser/session.py +4 -4
  48. package/core/cache_break_detector.py +97 -0
  49. package/core/cache_breakpoints.py +103 -0
  50. package/core/cache_policies.py +255 -0
  51. package/core/cancellation.py +3 -3
  52. package/core/channel_configs.py +3 -3
  53. package/core/channel_turns.py +13 -13
  54. package/core/child_approval.py +2 -3
  55. package/core/code_intel.py +11 -11
  56. package/core/command_policy.py +13 -7
  57. package/core/computer/annotate.py +231 -11
  58. package/core/computer/capture.py +140 -1
  59. package/core/computer/guard.py +7 -4
  60. package/core/computer/input.py +452 -26
  61. package/core/computer/session.py +563 -381
  62. package/core/computer/tools.py +2170 -928
  63. package/core/computer/uia.py +100 -1
  64. package/core/computer/watch.py +1 -1
  65. package/core/computer/win32.py +302 -7
  66. package/core/config_requirements.py +205 -205
  67. package/core/cron/runner.py +4 -4
  68. package/core/daily_log.py +2 -2
  69. package/core/dedup.py +2 -2
  70. package/core/egress_channels.py +326 -0
  71. package/core/egress_gate.py +96 -8
  72. package/core/egress_preapproved.py +59 -3
  73. package/core/endpoint.py +141 -141
  74. package/core/events.py +17 -8
  75. package/core/extension.py +2 -2
  76. package/core/file_history.py +15 -15
  77. package/core/file_tools.py +3 -3
  78. package/core/github_config.py +1 -1
  79. package/core/group_digest.py +3 -4
  80. package/core/group_minutes.py +5 -5
  81. package/core/group_reply.py +24 -0
  82. package/core/groups.py +5 -9
  83. package/core/hooks.py +15 -15
  84. package/core/i18n.py +2 -2
  85. package/core/ice/canary.py +3 -3
  86. package/core/ice/net_heuristics.py +2 -2
  87. package/core/im_routing.py +3 -3
  88. package/core/image_input.py +183 -43
  89. package/core/inbound.py +3 -3
  90. package/core/jobs.py +6 -6
  91. package/core/last_good.py +150 -150
  92. package/core/leaf_md.py +3 -3
  93. package/core/llm_errors.py +37 -28
  94. package/core/mail_config.py +2 -2
  95. package/core/memory/compressor.py +8 -8
  96. package/core/memory/manager.py +30 -45
  97. package/core/memory/session_memory.py +1 -2
  98. package/core/memory/short_term.py +4 -4
  99. package/core/memory_extract.py +1 -1
  100. package/core/memory_organize.py +8 -8
  101. package/core/memory_store.py +7 -9
  102. package/core/memory_transfer.py +2 -2
  103. package/core/model_capabilities.py +2 -2
  104. package/core/model_configs.py +219 -68
  105. package/core/model_profiles.py +333 -0
  106. package/core/modes.py +12 -11
  107. package/core/net_proxy.py +3 -3
  108. package/core/pipeline/scheduler.py +48 -43
  109. package/core/pipeline/stage.py +3 -0
  110. package/core/pipeline/stages/agent_loop.py +36 -15
  111. package/core/pipeline/stages/group_reply.py +22 -20
  112. package/core/pipeline/stages/post_process.py +7 -7
  113. package/core/pipeline/stages/pre_process.py +369 -77
  114. package/core/prompt_assembler.py +13 -4
  115. package/core/recycle.py +227 -16
  116. package/core/response_language.py +1 -1
  117. package/core/retention.py +2 -2
  118. package/core/rg.py +4 -3
  119. package/core/security.py +26 -8
  120. package/core/services.py +8 -8
  121. package/core/session_model.py +3 -5
  122. package/core/session_pool.py +35 -5
  123. package/core/session_recap.py +40 -3
  124. package/core/session_workspace.py +2 -3
  125. package/core/settings_baseline.py +2 -2
  126. package/core/settings_transfer.py +25 -1
  127. package/core/shell.py +52 -20
  128. package/core/side_call.py +3 -3
  129. package/core/skill_config.py +164 -0
  130. package/core/skill_factory.py +2 -2
  131. package/core/skill_guidance.py +2 -4
  132. package/core/skill_index.py +3 -4
  133. package/core/skill_settings.py +1 -1
  134. package/core/skill_sources.py +7 -7
  135. package/core/ssh_config.py +129 -0
  136. package/core/state_registry.py +67 -5
  137. package/core/subagent.py +28 -5
  138. package/core/subagent_defs.py +3 -3
  139. package/core/summarizer.py +4 -5
  140. package/core/tasks_notify.py +6 -6
  141. package/core/tiers.py +2 -2
  142. package/core/todos.py +1 -1
  143. package/core/token_calibrator.py +1 -4
  144. package/core/tool_outcome.py +108 -108
  145. package/core/touched_files.py +1 -1
  146. package/core/trace.py +12 -3
  147. package/core/unlock.py +4 -4
  148. package/core/update_tui.py +184 -158
  149. package/core/updater.py +5 -3
  150. package/core/version.py +14 -14
  151. package/core/vision_budget.py +235 -0
  152. package/core/web_cache.py +152 -152
  153. package/core/web_request.py +474 -0
  154. package/core/window_estimate.py +1 -1
  155. package/core/workspace.py +56 -26
  156. package/crannia_tray.py +278 -278
  157. package/licenses/GPL-3.0.txt +619 -619
  158. package/licenses/LGPL-2.1.txt +502 -502
  159. package/licenses/LGPL-3.0.txt +165 -165
  160. package/licenses/MIT-chat-on-wechat.txt +18 -18
  161. package/licenses/MIT-deepseek-harness.txt +21 -21
  162. package/licenses/Molio-LICENSE.txt +44 -44
  163. package/locales/en.json +1876 -1762
  164. package/main.py +154 -154
  165. package/mcpx/client.py +12 -1
  166. package/mcpx/manager.py +530 -522
  167. package/mcpx/servers/web_search.py +5 -13
  168. package/package.json +15 -23
  169. package/providers/anthropic/client.py +35 -33
  170. package/providers/base.py +13 -13
  171. package/providers/cache_policies.json +294 -0
  172. package/providers/cost_table.json +3779 -3779
  173. package/providers/lmstudio/client.py +2 -2
  174. package/providers/model_capabilities.json +6665 -6665
  175. package/providers/model_profiles.json +629 -0
  176. package/providers/ollama/client.py +2 -2
  177. package/providers/openai_compat.py +37 -16
  178. package/providers/openrouter/client.py +2 -2
  179. package/providers/qwen/__init__.py +1 -1
  180. package/providers/qwen/client.py +2 -2
  181. package/providers/qwen/manifest.yaml +16 -16
  182. package/providers/rate_limiter.py +2 -2
  183. package/providers/siliconflow/client.py +2 -2
  184. package/providers/validation.py +171 -171
  185. package/providers/vision_profiles.json +89 -0
  186. package/providers/window_table.json +8909 -8909
  187. package/requirements.txt +59 -51
  188. package/sbom-linux-x64.json +4580 -0
  189. package/sbom.json +4600 -4413
  190. package/skills/diagram/SKILL.md +47 -0
  191. package/skills/diagram/__init__.py +1 -0
  192. package/skills/diagram/handler.py +294 -0
  193. package/skills/diagram/layout.py +174 -0
  194. package/skills/diagram/manifest.yaml +106 -0
  195. package/skills/diagram/mermaid.py +170 -0
  196. package/skills/diagram/svg.py +223 -0
  197. package/skills/entertainment/SKILL.md +35 -35
  198. package/skills/entertainment/__init__.py +1 -1
  199. package/skills/entertainment/handler.py +91 -91
  200. package/skills/entertainment/manifest.yaml +26 -26
  201. package/skills/file_parser/GUIDANCE.md +10 -10
  202. package/skills/file_parser/handler.py +33 -11
  203. package/skills/github/SKILL.md +32 -32
  204. package/skills/github/__init__.py +1 -1
  205. package/skills/mail_parser/SKILL.md +68 -68
  206. package/skills/mail_parser/__init__.py +1 -1
  207. package/skills/mail_parser/handler.py +1 -1
  208. package/skills/media_gen/GUIDANCE.md +19 -19
  209. package/skills/media_gen/SKILL.md +43 -43
  210. package/skills/media_gen/__init__.py +1 -1
  211. package/skills/media_gen/handler.py +1 -1
  212. package/skills/media_gen/manifest.yaml +93 -93
  213. package/skills/netops/SKILL.md +45 -0
  214. package/skills/netops/__init__.py +1 -0
  215. package/skills/netops/handler.py +418 -0
  216. package/skills/netops/manifest.yaml +118 -0
  217. package/skills/office_parser/_excel_io.py +3 -3
  218. package/skills/office_parser/handler.py +2 -2
  219. package/skills/office_parser/ppt_tools.py +1 -1
  220. package/skills/office_parser/pptx_export/console_encoding.py +27 -27
  221. package/skills/office_parser/pptx_export/resource_paths.py +88 -88
  222. package/skills/office_parser/pptx_export/svg_finalize/__init__.py +12 -12
  223. package/skills/office_parser/pptx_export/svg_finalize/crop_images.py +366 -366
  224. package/skills/office_parser/pptx_export/svg_finalize/embed_images.py +288 -288
  225. package/skills/office_parser/pptx_export/svg_finalize/fix_image_aspect.py +388 -388
  226. package/skills/office_parser/pptx_export/svg_finalize/flatten_tspan.py +818 -818
  227. package/skills/office_parser/pptx_export/svg_finalize/svg_rect_to_path.py +345 -345
  228. package/skills/office_parser/pptx_export/svg_to_pptx/__init__.py +14 -14
  229. package/skills/office_parser/pptx_export/svg_to_pptx/drawingml/__init__.py +1 -1
  230. package/skills/office_parser/pptx_export/svg_to_pptx/drawingml/context.py +186 -186
  231. package/skills/office_parser/pptx_export/svg_to_pptx/drawingml/converter.py +781 -781
  232. package/skills/office_parser/pptx_export/svg_to_pptx/drawingml/paths.py +429 -429
  233. package/skills/office_parser/pptx_export/svg_to_pptx/drawingml/utils.py +40 -21
  234. package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/__init__.py +1 -1
  235. package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/builder.py +1267 -1267
  236. package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/dimensions.py +155 -155
  237. package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/discovery.py +101 -101
  238. package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/narration.py +236 -236
  239. package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/notes.py +257 -257
  240. package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/slide_xml.py +136 -136
  241. package/skills/office_parser/pptx_export/svg_to_pptx/tspan_flattener.py +44 -44
  242. package/skills/office_parser/pptx_export/svg_to_pptx/use_expander.py +129 -129
  243. package/skills/office_parser/references/LICENSE-Office-Word-MCP-Server.txt +20 -20
  244. package/skills/office_parser/references/LICENSE-excel-mcp-server.txt +21 -21
  245. package/skills/office_parser/references/layout_library.md +816 -816
  246. package/skills/office_parser/references/ppt-master/LICENSE +21 -21
  247. package/skills/office_parser/references/ppt-master/references/executor-base.md +474 -474
  248. package/skills/office_parser/references/ppt-master/references/shared-standards.md +1002 -1002
  249. package/skills/office_parser/references/ppt-master/references/strategist.md +880 -880
  250. package/skills/office_parser/references/ppt-master/references/template-designer.md +402 -402
  251. package/skills/office_parser/references/ppt-master/references/visual-review.md +240 -240
  252. package/skills/office_parser/references/ppt-master/references/visual-styles/_index.md +106 -106
  253. package/skills/office_parser/references/ppt-master/references/visual-styles/blueprint.md +37 -37
  254. package/skills/office_parser/references/ppt-master/references/visual-styles/brutalist.md +38 -38
  255. package/skills/office_parser/references/ppt-master/references/visual-styles/chalkboard.md +36 -36
  256. package/skills/office_parser/references/ppt-master/references/visual-styles/dark-tech.md +37 -37
  257. package/skills/office_parser/references/ppt-master/references/visual-styles/data-journalism.md +36 -36
  258. package/skills/office_parser/references/ppt-master/references/visual-styles/editorial.md +38 -38
  259. package/skills/office_parser/references/ppt-master/references/visual-styles/glassmorphism.md +36 -36
  260. package/skills/office_parser/references/ppt-master/references/visual-styles/ink-notes.md +37 -37
  261. package/skills/office_parser/references/ppt-master/references/visual-styles/ink-wash.md +36 -36
  262. package/skills/office_parser/references/ppt-master/references/visual-styles/memphis.md +37 -37
  263. package/skills/office_parser/references/ppt-master/references/visual-styles/paper-cut.md +36 -36
  264. package/skills/office_parser/references/ppt-master/references/visual-styles/photo-editorial.md +38 -38
  265. package/skills/office_parser/references/ppt-master/references/visual-styles/pixel-art.md +36 -36
  266. package/skills/office_parser/references/ppt-master/references/visual-styles/sketch-notes.md +36 -36
  267. package/skills/office_parser/references/ppt-master/references/visual-styles/soft-rounded.md +37 -37
  268. package/skills/office_parser/references/ppt-master/references/visual-styles/swiss-minimal.md +38 -38
  269. package/skills/office_parser/references/ppt-master/references/visual-styles/vintage-poster.md +36 -36
  270. package/skills/office_parser/references/ppt-master/references/visual-styles/zine.md +37 -37
  271. package/skills/office_parser/references/spec_lock.md +235 -235
  272. package/skills/office_parser/soffice.py +68 -68
  273. package/skills/office_parser/word_tools.py +3 -3
  274. package/skills/ssh/SKILL.md +81 -0
  275. package/skills/ssh/__init__.py +1 -0
  276. package/skills/ssh/handler.py +609 -0
  277. package/skills/ssh/manifest.yaml +182 -0
  278. package/tui/crannia.cmd +134 -134
  279. package/tui/crannia.sh +82 -0
  280. package/tui/crannia_tui/__init__.py +1 -1
  281. package/tui/crannia_tui/app.py +240 -17
  282. package/tui/crannia_tui/app.tcss +21 -0
  283. package/tui/crannia_tui/clipboard.py +78 -10
  284. package/tui/crannia_tui/commands.py +37 -0
  285. package/tui/crannia_tui/events.py +6 -0
  286. package/tui/crannia_tui/i18n.py +164 -164
  287. package/tui/crannia_tui/locales/en.json +32 -7
  288. package/tui/crannia_tui/paths.py +4 -1
  289. package/tui/crannia_tui/profile.py +178 -169
  290. package/tui/crannia_tui/settings.py +134 -134
  291. package/tui/crannia_tui/setup.py +44 -5
  292. package/tui/crannia_tui/ui.py +156 -16
  293. package/tui/crannia_tui/update.py +393 -389
  294. package/vendor/ripgrep/COPYING.txt +3 -3
  295. package/vendor/ripgrep/LICENSE-MIT.txt +21 -21
  296. package/vendor/ripgrep/NOTICE-vscode-ripgrep.txt +13 -13
  297. package/vendor/ripgrep/README.md +30 -14
  298. package/vendor/ripgrep/UNLICENSE.txt +24 -24
  299. package/vendor/ripgrep/linux-x64/rg +0 -0
  300. package/vendor/ripgrep/package-source.json +15 -16
  301. package/web/__main__.py +12 -15
  302. package/web/server.py +8481 -8594
  303. package/web/static/img/channel-telegram.svg +1 -1
  304. package/web/static/img/channel-wechat.svg +1 -1
  305. package/web/templates/logs.html +49 -49
  306. 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