@clipen/clipen-cli 0.0.0-stage → 1.0.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.
Files changed (232) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +89 -2
  3. package/apps/web/dist/assets/index-D5D2HiB1.js +14 -0
  4. package/apps/web/dist/assets/index-DIMuOXSu.css +1 -0
  5. package/apps/web/dist/favicon.svg +1 -0
  6. package/apps/web/dist/index.html +16 -0
  7. package/bin/clipen.js +157 -0
  8. package/clipen +18 -0
  9. package/install +25 -0
  10. package/package.json +55 -4
  11. package/pyproject.toml +44 -0
  12. package/requirements/asr-runtime.txt +13 -0
  13. package/requirements/dev.txt +2 -0
  14. package/requirements/runtime.txt +20 -0
  15. package/src/clipen/__init__.py +3 -0
  16. package/src/clipen/analysis/__init__.py +89 -0
  17. package/src/clipen/analysis/artifacts.py +410 -0
  18. package/src/clipen/analysis/audio.py +394 -0
  19. package/src/clipen/analysis/evidence.py +102 -0
  20. package/src/clipen/analysis/evidence_service.py +929 -0
  21. package/src/clipen/analysis/image_similarity.py +134 -0
  22. package/src/clipen/analysis/keyframes.py +159 -0
  23. package/src/clipen/analysis/models.py +228 -0
  24. package/src/clipen/analysis/qwen_worker.py +594 -0
  25. package/src/clipen/analysis/resources.py +22 -0
  26. package/src/clipen/analysis/runtime.py +62 -0
  27. package/src/clipen/analysis/service.py +594 -0
  28. package/src/clipen/analysis/setup.py +921 -0
  29. package/src/clipen/analysis/setup_cli.py +107 -0
  30. package/src/clipen/analysis/storage.py +1864 -0
  31. package/src/clipen/analysis/transcription.py +906 -0
  32. package/src/clipen/analysis/visual.py +237 -0
  33. package/src/clipen/annotations/__init__.py +13 -0
  34. package/src/clipen/annotations/models.py +22 -0
  35. package/src/clipen/annotations/storage.py +428 -0
  36. package/src/clipen/annotations/suggestions.py +115 -0
  37. package/src/clipen/asr_release.py +132 -0
  38. package/src/clipen/bootstrap.py +978 -0
  39. package/src/clipen/candidates.py +222 -0
  40. package/src/clipen/cli.py +84 -0
  41. package/src/clipen/controller/__init__.py +17 -0
  42. package/src/clipen/controller/app.py +1657 -0
  43. package/src/clipen/controller/server.py +533 -0
  44. package/src/clipen/domain/__init__.py +30 -0
  45. package/src/clipen/domain/errors.py +5 -0
  46. package/src/clipen/domain/media_time.py +110 -0
  47. package/src/clipen/domain/timeline.py +582 -0
  48. package/src/clipen/domain/validation.py +14 -0
  49. package/src/clipen/editing/EDITORIAL_REGRESSIONS.md +38 -0
  50. package/src/clipen/editing/PROGRAM_FORMAT.md +102 -0
  51. package/src/clipen/editing/README.md +115 -0
  52. package/src/clipen/editing/REFACTOR_DESIGN.md +241 -0
  53. package/src/clipen/editing/REFACTOR_HANDOFF.md +73 -0
  54. package/src/clipen/editing/__init__.py +15 -0
  55. package/src/clipen/editing/agent.py +13 -0
  56. package/src/clipen/editing/agent_models.py +127 -0
  57. package/src/clipen/editing/agent_repository.py +978 -0
  58. package/src/clipen/editing/agent_tools.py +613 -0
  59. package/src/clipen/editing/alignment.py +150 -0
  60. package/src/clipen/editing/alignment_setup.py +156 -0
  61. package/src/clipen/editing/alignment_worker.py +24 -0
  62. package/src/clipen/editing/change_report.py +128 -0
  63. package/src/clipen/editing/compiler.py +40 -0
  64. package/src/clipen/editing/component_looks.py +131 -0
  65. package/src/clipen/editing/component_review.py +74 -0
  66. package/src/clipen/editing/coordinator.py +497 -0
  67. package/src/clipen/editing/editorial_review.py +309 -0
  68. package/src/clipen/editing/evidence_jobs.py +552 -0
  69. package/src/clipen/editing/history_changes.py +192 -0
  70. package/src/clipen/editing/music_choices.py +18 -0
  71. package/src/clipen/editing/production_catalog.py +629 -0
  72. package/src/clipen/editing/production_limits.py +85 -0
  73. package/src/clipen/editing/production_media.py +435 -0
  74. package/src/clipen/editing/production_preview.py +373 -0
  75. package/src/clipen/editing/production_progress.py +236 -0
  76. package/src/clipen/editing/production_requirements.py +117 -0
  77. package/src/clipen/editing/production_tools.py +1198 -0
  78. package/src/clipen/editing/program_audio.py +248 -0
  79. package/src/clipen/editing/program_direction.py +83 -0
  80. package/src/clipen/editing/program_story.py +91 -0
  81. package/src/clipen/editing/program_subtitles.py +232 -0
  82. package/src/clipen/editing/programme_review.py +121 -0
  83. package/src/clipen/editing/prompt_history.py +84 -0
  84. package/src/clipen/editing/recut.py +167 -0
  85. package/src/clipen/editing/refinement_references.py +278 -0
  86. package/src/clipen/editing/resolve_export.py +847 -0
  87. package/src/clipen/editing/resolve_titles.py +291 -0
  88. package/src/clipen/editing/skill_tools.py +951 -0
  89. package/src/clipen/editing/source_dialogue.py +192 -0
  90. package/src/clipen/editing/speech_coverage.py +50 -0
  91. package/src/clipen/editing/speech_quality.py +11 -0
  92. package/src/clipen/editing/tool_receipts.py +98 -0
  93. package/src/clipen/editing/versions.py +219 -0
  94. package/src/clipen/editing/visual_preview.py +189 -0
  95. package/src/clipen/editing/workbench.py +136 -0
  96. package/src/clipen/filesystem.py +25 -0
  97. package/src/clipen/finishing/README.md +103 -0
  98. package/src/clipen/finishing/__init__.py +1 -0
  99. package/src/clipen/finishing/audio.py +188 -0
  100. package/src/clipen/finishing/catalog.py +264 -0
  101. package/src/clipen/finishing/models.py +14 -0
  102. package/src/clipen/finishing/renderer.py +45 -0
  103. package/src/clipen/mcp_editing/__init__.py +5 -0
  104. package/src/clipen/mcp_editing/__main__.py +42 -0
  105. package/src/clipen/mcp_editing/server.py +396 -0
  106. package/src/clipen/media/__init__.py +50 -0
  107. package/src/clipen/media/discovery.py +253 -0
  108. package/src/clipen/media/models.py +349 -0
  109. package/src/clipen/media/preview.py +137 -0
  110. package/src/clipen/media/probe.py +418 -0
  111. package/src/clipen/media/service.py +304 -0
  112. package/src/clipen/media/storage.py +694 -0
  113. package/src/clipen/music_vocabulary.py +34 -0
  114. package/src/clipen/power.py +28 -0
  115. package/src/clipen/projects/__init__.py +26 -0
  116. package/src/clipen/projects/agent_schema.py +94 -0
  117. package/src/clipen/projects/annotation_schema.py +40 -0
  118. package/src/clipen/projects/deletion.py +256 -0
  119. package/src/clipen/projects/evidence_schema.py +52 -0
  120. package/src/clipen/projects/finishing_schema.py +36 -0
  121. package/src/clipen/projects/models.py +189 -0
  122. package/src/clipen/projects/picker.py +232 -0
  123. package/src/clipen/projects/preview_schema.py +29 -0
  124. package/src/clipen/projects/service.py +159 -0
  125. package/src/clipen/projects/storage.py +1458 -0
  126. package/src/clipen/projects/timeline_schema.py +32 -0
  127. package/src/clipen/projects/validation_schema.py +28 -0
  128. package/src/clipen/provider_registry.py +77 -0
  129. package/src/clipen/providers/__init__.py +43 -0
  130. package/src/clipen/providers/account.py +245 -0
  131. package/src/clipen/providers/claude.py +409 -0
  132. package/src/clipen/providers/claude_editing.py +118 -0
  133. package/src/clipen/providers/codex.py +991 -0
  134. package/src/clipen/providers/codex_editing.py +91 -0
  135. package/src/clipen/providers/editing_agent.py +567 -0
  136. package/src/clipen/providers/errors.py +51 -0
  137. package/src/clipen/providers/recovery.py +269 -0
  138. package/src/clipen/py.typed +1 -0
  139. package/src/clipen/rendering/__init__.py +12 -0
  140. package/src/clipen/rendering/cover.py +139 -0
  141. package/src/clipen/rendering/custom_components.py +328 -0
  142. package/src/clipen/rendering/custom_render.py +218 -0
  143. package/src/clipen/rendering/fonts/anton-OFL.txt +93 -0
  144. package/src/clipen/rendering/fonts/bebasneue-OFL.txt +93 -0
  145. package/src/clipen/rendering/fonts/caveat-OFL.txt +93 -0
  146. package/src/clipen/rendering/fonts/chenyu-OFL.txt +93 -0
  147. package/src/clipen/rendering/fonts/delagothicone-OFL.txt +93 -0
  148. package/src/clipen/rendering/fonts/hachimarupop-OFL.txt +93 -0
  149. package/src/clipen/rendering/fonts/iansui-OFL.txt +93 -0
  150. package/src/clipen/rendering/fonts/manifest.json +145 -0
  151. package/src/clipen/rendering/fonts/playfairdisplay-OFL.txt +93 -0
  152. package/src/clipen/rendering/fonts/rocknrollone-OFL.txt +93 -0
  153. package/src/clipen/rendering/fonts/yomogi-OFL.txt +94 -0
  154. package/src/clipen/rendering/fonts/yuseimagic-OFL.txt +93 -0
  155. package/src/clipen/rendering/fonts/zenkurenaido-OFL.txt +93 -0
  156. package/src/clipen/rendering/fonts/zenmarugothic-OFL.txt +93 -0
  157. package/src/clipen/rendering/production_effects.py +96 -0
  158. package/src/clipen/rendering/program_components.py +254 -0
  159. package/src/clipen/rendering/program_creator.py +804 -0
  160. package/src/clipen/rendering/program_identities.py +350 -0
  161. package/src/clipen/rendering/program_motion.py +186 -0
  162. package/src/clipen/rendering/program_studio.py +227 -0
  163. package/src/clipen/rendering/program_styles.py +321 -0
  164. package/src/clipen/rendering/program_subtitles.py +89 -0
  165. package/src/clipen/rendering/renderer.py +737 -0
  166. package/src/clipen/rendering/storage.py +230 -0
  167. package/src/clipen/rendering/titles.py +467 -0
  168. package/src/clipen/skills/__init__.py +5 -0
  169. package/src/clipen/skills/builtin/SKILL.md +72 -0
  170. package/src/clipen/skills/builtin/assets/sample-daily-vlog.mp4 +0 -0
  171. package/src/clipen/skills/builtin/assets/sample-event-recap.mp4 +0 -0
  172. package/src/clipen/skills/builtin/assets/sample-food-review.mp4 +0 -0
  173. package/src/clipen/skills/builtin/assets/sample-product-demo.mp4 +0 -0
  174. package/src/clipen/skills/builtin/assets/sample-product-review.mp4 +0 -0
  175. package/src/clipen/skills/builtin/assets/sample-product-unboxing.mp4 +0 -0
  176. package/src/clipen/skills/builtin/assets/sample-shorts-highlight.mp4 +0 -0
  177. package/src/clipen/skills/builtin/assets/sample-travel-highlight.mp4 +0 -0
  178. package/src/clipen/skills/builtin/assets/sample-travel-vlog.mp4 +0 -0
  179. package/src/clipen/skills/builtin/assets/style-cinema.png +0 -0
  180. package/src/clipen/skills/builtin/assets/style-editorial.png +0 -0
  181. package/src/clipen/skills/builtin/assets/style-tpl-daily-vlog.png +0 -0
  182. package/src/clipen/skills/builtin/assets/style-tpl-event-recap.png +0 -0
  183. package/src/clipen/skills/builtin/assets/style-tpl-food-review.png +0 -0
  184. package/src/clipen/skills/builtin/assets/style-tpl-product-demo.png +0 -0
  185. package/src/clipen/skills/builtin/assets/style-tpl-product-review.png +0 -0
  186. package/src/clipen/skills/builtin/assets/style-tpl-product-unboxing.png +0 -0
  187. package/src/clipen/skills/builtin/assets/style-tpl-shorts-highlight.png +0 -0
  188. package/src/clipen/skills/builtin/assets/style-tpl-travel-highlight.png +0 -0
  189. package/src/clipen/skills/builtin/assets/style-tpl-travel-vlog.png +0 -0
  190. package/src/clipen/skills/builtin/assets/style-variety.png +0 -0
  191. package/src/clipen/skills/builtin/components.json +133 -0
  192. package/src/clipen/skills/builtin/font_sources.json +20 -0
  193. package/src/clipen/skills/builtin/font_styles.json +329 -0
  194. package/src/clipen/skills/builtin/manifest.json +32 -0
  195. package/src/clipen/skills/builtin/presets.json +8 -0
  196. package/src/clipen/skills/builtin/references/captions.md +14 -0
  197. package/src/clipen/skills/builtin/references/channel-design.md +37 -0
  198. package/src/clipen/skills/builtin/references/editing.md +30 -0
  199. package/src/clipen/skills/builtin/references/font-chenyu-OFL.txt +93 -0
  200. package/src/clipen/skills/builtin/references/font-iansui-OFL.txt +93 -0
  201. package/src/clipen/skills/builtin/references/graphics.md +59 -0
  202. package/src/clipen/skills/builtin/references/review.md +16 -0
  203. package/src/clipen/skills/builtin/references/sound.md +8 -0
  204. package/src/clipen/skills/builtin/references/story.md +7 -0
  205. package/src/clipen/skills/builtin/skills/travel-program/SKILL.md +27 -0
  206. package/src/clipen/skills/builtin/skills/travel-program/manifest.json +6 -0
  207. package/src/clipen/skills/builtin/template-skills/clipen-daily-vlog/SKILL.md +33 -0
  208. package/src/clipen/skills/builtin/template-skills/clipen-daily-vlog/manifest.json +8 -0
  209. package/src/clipen/skills/builtin/template-skills/clipen-event-recap/SKILL.md +34 -0
  210. package/src/clipen/skills/builtin/template-skills/clipen-event-recap/manifest.json +8 -0
  211. package/src/clipen/skills/builtin/template-skills/clipen-food-review/SKILL.md +33 -0
  212. package/src/clipen/skills/builtin/template-skills/clipen-food-review/manifest.json +8 -0
  213. package/src/clipen/skills/builtin/template-skills/clipen-product-demo/SKILL.md +35 -0
  214. package/src/clipen/skills/builtin/template-skills/clipen-product-demo/manifest.json +8 -0
  215. package/src/clipen/skills/builtin/template-skills/clipen-product-review/SKILL.md +36 -0
  216. package/src/clipen/skills/builtin/template-skills/clipen-product-review/manifest.json +8 -0
  217. package/src/clipen/skills/builtin/template-skills/clipen-product-unboxing/SKILL.md +39 -0
  218. package/src/clipen/skills/builtin/template-skills/clipen-product-unboxing/manifest.json +8 -0
  219. package/src/clipen/skills/builtin/template-skills/clipen-shorts-highlight/SKILL.md +38 -0
  220. package/src/clipen/skills/builtin/template-skills/clipen-shorts-highlight/manifest.json +8 -0
  221. package/src/clipen/skills/builtin/template-skills/clipen-travel-highlight/SKILL.md +34 -0
  222. package/src/clipen/skills/builtin/template-skills/clipen-travel-highlight/manifest.json +8 -0
  223. package/src/clipen/skills/builtin/template-skills/clipen-travel-vlog/SKILL.md +37 -0
  224. package/src/clipen/skills/builtin/template-skills/clipen-travel-vlog/manifest.json +8 -0
  225. package/src/clipen/skills/builtin/templates.json +284 -0
  226. package/src/clipen/skills/builtin/visual_styles.json +1074 -0
  227. package/src/clipen/skills/catalog.py +314 -0
  228. package/src/clipen/skills/library.py +137 -0
  229. package/src/clipen/skills/package.py +254 -0
  230. package/src/clipen/skills/visual_styles.py +55 -0
  231. package/src/clipen/workspace.py +181 -0
  232. package/uninstall +25 -0
@@ -0,0 +1,804 @@
1
+ """Taiwanese YouTuber-style packaging drawn on top of real footage.
2
+
3
+ Modelled on how channels such as Joeman, The DoDo Men and 壹加壹 actually dress
4
+ their videos: information sits as small corner tags or text next to the
5
+ subject, real reactions become sticker words, titles are bold display type.
6
+
7
+ Each template picks a "look" for four component groups (title, tag, sticker,
8
+ info) and a motion style, so templates differ in shape and movement, not only
9
+ in colour. A component is drawn on its own small canvas, optionally tilted and
10
+ popped, then composited at its footprint. Layout returns that footprint, so
11
+ collision and safe-area checks cover every frame of the animation (pointer and
12
+ circle targets are drawn separately at their target).
13
+ """
14
+
15
+ import math
16
+ from pathlib import Path
17
+
18
+ from PIL import Image, ImageDraw, ImageFilter
19
+
20
+ from .program_styles import draw_style_text, load_style_font, styled_text_length
21
+ from .titles import TitleRenderError
22
+
23
+ HERO = {"program_title", "headline", "impact", "chapter", "burst"}
24
+ POP = {"impact", "burst", "reaction", "headline", "program_title"}
25
+ SIZES = {
26
+ "program_title": 0.105,
27
+ "headline": 0.082,
28
+ "chapter": 0.038,
29
+ "impact": 0.13,
30
+ "burst": 0.1,
31
+ "reaction": 0.096,
32
+ "location": 0.036,
33
+ "person": 0.056,
34
+ "fact": 0.062,
35
+ "pointer": 0.052,
36
+ "circle": 0.05,
37
+ }
38
+ TILT = {"impact": -4, "reaction": -5, "burst": 4}
39
+ POP_SLACK = 1.08
40
+ GROUPS = {
41
+ "program_title": "title",
42
+ "headline": "title",
43
+ "location": "tag",
44
+ "chapter": "tag",
45
+ "impact": "sticker",
46
+ "reaction": "sticker",
47
+ "burst": "sticker",
48
+ "fact": "info",
49
+ "person": "info",
50
+ "pointer": "info",
51
+ "circle": "info",
52
+ }
53
+ DEFAULT_LOOK = {"title": "outline", "tag": "shadow", "sticker": "sticker", "info": "outline"}
54
+
55
+
56
+ def _row(fill, stroke=None, outer=None, track=0, shadow=None):
57
+ return {"fill": fill, "stroke": stroke, "outer": outer, "track": track, "shadow": shadow}
58
+
59
+
60
+ # Row styles are (primary, detail). Colours name palette entries, optionally
61
+ # with an alpha multiplier. A look may add a shape behind the text, extras
62
+ # drawn around single rows, a tilt and a row arrangement.
63
+ LOOKS = {
64
+ ("title", "outline"): {"rows": (_row("paper", ("ink", 0.13)), _row("accent", ("ink", 0.14))), "split": True},
65
+ ("title", "block"): {"rows": (_row("paper", ("ink", 0.05)), _row("ink")), "shape": "block"},
66
+ ("title", "serif"): {
67
+ "rows": (_row("paper", track=0.16, shadow="soft"), _row(("paper", 0.88), track=0.22, shadow="soft")),
68
+ "extras": ("rule_below",),
69
+ "detail_scale": 0.4,
70
+ "row_gap": 0.24,
71
+ },
72
+ ("title", "pill"): {"rows": (_row("paper"), _row(("paper", 0.92))), "shape": "pill"},
73
+ ("title", "doodle"): {
74
+ "rows": (_row("paper", ("ink", 0.05), shadow="hard:accent"), _row("paper", ("ink", 0.08))),
75
+ "extras": ("squiggle",),
76
+ "split": True,
77
+ "row_gap": 0.26,
78
+ },
79
+ ("title", "panel"): {"rows": (_row("paper"), _row("accent")), "shape": "panel"},
80
+ ("title", "bar"): {"rows": (_row("paper", ("ink", 0.1)), _row("paper", ("ink", 0.12))), "extras": ("highlight",)},
81
+ ("title", "rules"): {
82
+ "rows": (_row("accent", track=0.06, shadow="deep"), _row("paper", track=0.3, shadow="deep")),
83
+ "extras": ("rules",),
84
+ "detail_scale": 0.4,
85
+ },
86
+ ("title", "meme"): {"rows": (_row("paper", ("secondary", 0.15), ("ink", 0.09)), _row("accent", ("ink", 0.14)))},
87
+ ("tag", "shadow"): {"rows": (_row("paper", ("ink", 0.06), shadow="drop"),), "join": " | "},
88
+ ("tag", "tape"): {"rows": (_row("ink"),), "join": " ", "shape": "tape", "tilt": -2},
89
+ ("tag", "label"): {"rows": (_row("paper"),), "join": " ", "shape": "label"},
90
+ ("tag", "rules"): {"rows": (_row("paper", track=0.24, shadow="deep"),), "join": " ", "extras": ("rules",)},
91
+ ("tag", "dot"): {"rows": (_row("paper", ("ink", 0.1)),), "join": "|", "extras": ("dot",)},
92
+ ("tag", "keycap"): {
93
+ "rows": (_row("ink"), _row("paper")),
94
+ "inline": True,
95
+ "boxes": ("accent", ("ink", 0.82)),
96
+ "detail_scale": 1.0,
97
+ },
98
+ ("tag", "chip"): {"rows": (_row("ink"),), "join": " ", "shape": "chip"},
99
+ ("tag", "flag"): {"rows": (_row("ink"),), "join": "|", "shape": "flag"},
100
+ ("tag", "meme"): {"rows": (_row("ink"),), "join": " ", "shape": "memebox", "tilt": -2},
101
+ ("sticker", "sticker"): {"rows": (_row("accent", ("ink", 0.10), ("paper", 0.24)), _row("paper", ("ink", 0.14)))},
102
+ ("sticker", "burst"): {"rows": (_row("paper", ("ink", 0.1)), _row("ink")), "shape": "burst"},
103
+ ("sticker", "quiet"): {
104
+ "rows": (_row("paper", track=0.1, shadow="soft"), _row(("paper", 0.85), track=0.2, shadow="soft")),
105
+ "tilt": 0,
106
+ },
107
+ ("sticker", "bubble"): {"rows": (_row("ink"), _row(("ink", 0.7))), "shape": "bubble", "tilt": -3},
108
+ ("sticker", "doodle"): {
109
+ "rows": (_row("accent", ("paper", 0.1), shadow="soft"), _row("paper", shadow="soft")),
110
+ "extras": ("sparkles",),
111
+ },
112
+ ("sticker", "pill"): {"rows": (_row("ink"), _row(("ink", 0.75))), "shape": "pill", "tilt": 0},
113
+ ("sticker", "stamp"): {"rows": (_row("accent"), _row("accent")), "shape": "stamp", "tilt": -7},
114
+ ("sticker", "glow"): {
115
+ "rows": (_row("accent", track=0.08, shadow="glow"), _row("paper", track=0.16, shadow="deep")),
116
+ "tilt": 0,
117
+ },
118
+ ("sticker", "meme"): {
119
+ "rows": (_row("accent", ("ink", 0.11), ("paper", 0.2), shadow="hard:secondary"), _row("paper", ("ink", 0.14)))
120
+ },
121
+ ("info", "outline"): {"rows": (_row("accent", ("ink", 0.12)), _row("paper", ("ink", 0.12)))},
122
+ ("info", "label"): {"rows": (_row("paper"), _row("ink")), "boxes": ("ink", "accent")},
123
+ ("info", "quiet"): {
124
+ "rows": (_row("paper", track=0.06, shadow="soft"), _row(("paper", 0.82), shadow="soft")),
125
+ "extras": ("vline",),
126
+ },
127
+ ("info", "price"): {"rows": (_row("ink"), _row("accent")), "shape": "price"},
128
+ ("info", "doodle"): {"rows": (_row("paper", ("ink", 0.05), shadow="hard:accent"), _row("paper", ("ink", 0.1)))},
129
+ ("info", "callout"): {"rows": (_row("paper"), _row("accent")), "shape": "panel"},
130
+ ("info", "verdict"): {"rows": (_row("paper", ("ink", 0.12)), _row("paper", ("ink", 0.12))), "extras": ("icon",)},
131
+ ("info", "name"): {
132
+ "rows": (_row("accent", track=0.04, shadow="deep"), _row("paper", shadow="deep")),
133
+ "extras": ("underline",),
134
+ "row_gap": 0.2,
135
+ },
136
+ ("info", "meme"): {"rows": (_row("paper", ("ink", 0.14)), _row("accent", ("ink", 0.14)))},
137
+ }
138
+ # A verdict reads as a drawback when the label starts like one.
139
+ CONS = ("缺點", "缺", "扣分", "不推", "踩雷")
140
+
141
+
142
+ def is_creator(profile):
143
+ return profile.get("presets", {}).get("appearance", {}).get("skin") == "creator"
144
+
145
+
146
+ def _appearance(profile):
147
+ return profile["presets"].get("appearance", {})
148
+
149
+
150
+ def _look(profile):
151
+ group = GROUPS[profile["component_id"]]
152
+ name = _appearance(profile).get("looks", {}).get(group, DEFAULT_LOOK[group])
153
+ if (group, name) not in LOOKS:
154
+ raise TitleRenderError("program_component_unknown_look")
155
+ return group, name, LOOKS[group, name]
156
+
157
+
158
+ def _motion(profile):
159
+ return _appearance(profile).get("motion_style", "pop")
160
+
161
+
162
+ def frames(profile):
163
+ motion = profile["presets"]["motion"]
164
+ appearance = _appearance(profile)
165
+ hero = profile["component_id"] in HERO
166
+ entry = appearance.get("hero_entry_frames", motion["entry_frames"]) if hero else motion["entry_frames"]
167
+ exit_frames = appearance.get("hero_exit_frames", motion["exit_frames"]) if hero else motion["exit_frames"]
168
+ return max(1, entry), max(1, exit_frames)
169
+
170
+
171
+ def _rgba(palette, name, alpha=1.0):
172
+ r, g, b, a = palette[name]
173
+ return r, g, b, round(a * alpha)
174
+
175
+
176
+ def _color(palette, spec):
177
+ if spec is None:
178
+ return None
179
+ name, alpha = (spec, 1.0) if isinstance(spec, str) else spec
180
+ return _rgba(palette, name, alpha)
181
+
182
+
183
+ def _stroke(font, ratio):
184
+ return max(1, round(font.size * ratio)) if ratio else 0
185
+
186
+
187
+ def _length(value, font, track):
188
+ if not track:
189
+ return styled_text_length(value, font)
190
+ return sum(styled_text_length(c, font) for c in value) + font.size * track * (len(value) - 1)
191
+
192
+
193
+ def _draw_text(pen, x, y, value, font, fill, stroke=0, stroke_fill=None, track=0):
194
+ if not track:
195
+ draw_style_text(pen, (x, y), value, font, fill, stroke, stroke_fill)
196
+ return
197
+ # Keep one baseline for the line; each glyph would otherwise sit on its own top edge.
198
+ top = font.getbbox(value, anchor="ls")[1]
199
+ for character in value:
200
+ lift = 0 if character.isspace() else font.getbbox(character, anchor="ls")[1] - top
201
+ draw_style_text(pen, (x, y + lift), character, font, fill, stroke, stroke_fill)
202
+ x += styled_text_length(character, font) + font.size * track
203
+
204
+
205
+ def _shadow(font, shadow, palette):
206
+ """A row shadow as (colour, offset, blur) in pixels, or None."""
207
+ if not shadow:
208
+ return None
209
+ blur = (
210
+ round(font.size * {"glow": 0.16, "deep": 0.1, "soft": 0.07}[shadow])
211
+ if shadow in {"soft", "deep", "glow"}
212
+ else 0
213
+ )
214
+ offset = round(font.size * (0.07 if shadow.startswith("hard") else 0.04 if shadow in {"soft", "deep"} else 0.06))
215
+ colour = {
216
+ "drop": _rgba(palette, "ink", 0.47),
217
+ "soft": _rgba(palette, "ink", 0.6),
218
+ "deep": _rgba(palette, "ink", 0.9),
219
+ "glow": _rgba(palette, "accent", 0.75),
220
+ }.get(shadow) or _rgba(palette, shadow.split(":")[1])
221
+ return colour, offset, blur
222
+
223
+
224
+ def _row_image(value, font, spec, palette, text=True):
225
+ """One line of type with its outline, outer ring and shadow, plus the margin used.
226
+
227
+ Without text the row keeps its size but stays empty, so shapes around it land where they would."""
228
+ stroke_color, stroke_ratio = spec["stroke"] or (None, 0)
229
+ outer_color, outer_ratio = spec["outer"] or (None, 0)
230
+ inner, ring = _stroke(font, stroke_ratio), _stroke(font, stroke_ratio) + _stroke(font, outer_ratio)
231
+ shadow = _shadow(font, spec["shadow"], palette)
232
+ colour, offset, blur = shadow or (None, 0, 0)
233
+ margin = ring + blur * 3 + offset + 2
234
+ width, height = _length(value, font, spec["track"]), font.size * 1.18
235
+ image = Image.new("RGBA", (math.ceil(width + margin * 2), math.ceil(height + margin * 2)))
236
+ if not text:
237
+ return image, margin, width, height
238
+ if shadow:
239
+ layer = Image.new("RGBA", image.size)
240
+ dx = 0 if spec["shadow"] == "glow" else offset
241
+ _draw_text(ImageDraw.Draw(layer), margin + dx, margin + dx, value, font, colour, ring, colour, spec["track"])
242
+ if blur:
243
+ layer = layer.filter(ImageFilter.GaussianBlur(blur))
244
+ image.alpha_composite(layer)
245
+ pen = ImageDraw.Draw(image)
246
+ fill = _color(palette, spec["fill"])
247
+ if outer_color:
248
+ outer = _color(palette, outer_color)
249
+ _draw_text(pen, margin, margin, value, font, outer, ring, outer, spec["track"])
250
+ _draw_text(pen, margin, margin, value, font, fill, inner, _color(palette, stroke_color), spec["track"])
251
+ return image, margin, width, height
252
+
253
+
254
+ def _texts(item, look, group):
255
+ primary, _, detail = item.text.partition("\n")
256
+ if "join" in look:
257
+ return [primary + (look["join"] + detail if detail else "")], False
258
+ rows = primary.split("・", 1) if look.get("split") and "・" in primary else [primary]
259
+ return [r for r in rows if r] + ([detail] if detail else []), bool(detail)
260
+
261
+
262
+ def _assemble(item, profile, font_path, size, look, group, text=True):
263
+ """Lay rows out (stacked or side by side) and return the canvas, each row's text box and its type.
264
+
265
+ A row's type is (text, font, spec, origin): origin is where the text is drawn on the canvas."""
266
+ palette = profile["presets"]["palette"]
267
+ kind = profile["component_id"]
268
+ texts, has_detail = _texts(item, look, group)
269
+ font = load_style_font(font_path, size, profile)
270
+ default_scale = 0.62 if kind in {"program_title", "headline"} else 0.7
271
+ small = load_style_font(
272
+ font_path, max(10, round(size * look.get("detail_scale", default_scale))), profile, role="detail"
273
+ )
274
+ primary_spec, detail_spec = look["rows"][0], look["rows"][-1]
275
+ rows, types = [], []
276
+ for index, value in enumerate(texts):
277
+ is_detail = has_detail and index == len(texts) - 1
278
+ face, spec = (small, detail_spec) if is_detail else (font, primary_spec)
279
+ rows.append(_row_image(value, face, spec, palette, text))
280
+ types.append((value, face, spec))
281
+ boxes = look.get("boxes")
282
+ pad_x, pad_y = (round(size * 0.34), round(size * 0.12)) if boxes else (0, 0)
283
+ gap = (pad_x * 2 + round(size * 0.2)) if look.get("inline") else pad_y * 2 + round(size * look.get("row_gap", 0))
284
+ outer = max(margin for _, margin, _, _ in rows)
285
+ if look.get("inline"):
286
+ width = sum(w for _, _, w, _ in rows) + gap * (len(rows) - 1)
287
+ height = max(h for _, _, _, h in rows)
288
+ else:
289
+ width = max(w for _, _, w, _ in rows)
290
+ height = sum(h for _, _, _, h in rows) + gap * (len(rows) - 1)
291
+ edge = outer + max(pad_x, pad_y)
292
+ canvas = Image.new("RGBA", (math.ceil(width + edge * 2), math.ceil(height + edge * 2)))
293
+ rects, cursor = [], edge
294
+ for image, margin, w, h in rows:
295
+ x, y = (cursor, edge + (height - h) / 2) if look.get("inline") else (edge, cursor)
296
+ rects.append((x, y, x + w, y + h))
297
+ cursor += (w if look.get("inline") else h) + gap
298
+ if boxes:
299
+ pen = ImageDraw.Draw(canvas)
300
+ for (x0, y0, x1, y1), colour, (_, _, _, h) in zip(rects, boxes, rows):
301
+ radius = round(h * (0.22 if look.get("inline") else 0.08))
302
+ pen.rounded_rectangle(
303
+ (x0 - pad_x, y0 - pad_y, x1 + pad_x, y1 + pad_y * 0.4), radius, fill=_color(palette, colour)
304
+ )
305
+ placed = []
306
+ for (image, margin, _, _), (x, y, _, _), row in zip(rows, rects, types):
307
+ canvas.alpha_composite(image, (round(x - margin), round(y - margin)))
308
+ placed.append(row + ((round(x - margin) + margin, round(y - margin) + margin),))
309
+ return canvas, rects, font.size, has_detail, placed
310
+
311
+
312
+ def _shape(canvas, rects, size, look, palette, has_detail=False, con=False, crop=None):
313
+ """Draw the look's shape behind the text on a larger canvas.
314
+
315
+ Returns the image, where the canvas origin landed on it, and the crop used; pass that crop back
316
+ to cut a second drawing of the same component (one without type) identically."""
317
+ name = look.get("shape")
318
+ extras = look.get("extras", ())
319
+ if not name and not extras:
320
+ return canvas, (0, 0), None
321
+ grow = round(size * 1.1)
322
+ out = Image.new("RGBA", (canvas.width + grow * 2, canvas.height + grow * 2))
323
+ pen = ImageDraw.Draw(out)
324
+ rects = [(x0 + grow, y0 + grow, x1 + grow, y1 + grow) for x0, y0, x1, y1 in rects]
325
+ x0, y0 = min(r[0] for r in rects), min(r[1] for r in rects)
326
+ x1, y1 = max(r[2] for r in rects), max(r[3] for r in rects)
327
+ paper, ink, accent = (_rgba(palette, n) for n in ("paper", "ink", "accent"))
328
+ s = size
329
+ if name == "block":
330
+ skew = (y1 - y0) * 0.2
331
+ bx0, by0, bx1, by1 = x0 - s * 0.3, y0 - s * 0.04, x1 + s * 0.3 + skew, y1 + s * 0.02
332
+ poly = lambda dx, dy: [
333
+ (bx0 + skew + dx, by0 + dy),
334
+ (bx1 + dx, by0 + dy),
335
+ (bx1 - skew + dx, by1 + dy),
336
+ (bx0 + dx, by1 + dy),
337
+ ]
338
+ pen.polygon(poly(s * 0.09, s * 0.09), fill=ink)
339
+ pen.polygon(poly(0, 0), fill=accent)
340
+ elif name == "pill":
341
+ box = (x0 - s * 0.42, y0 - s * 0.06, x1 + s * 0.42, y1 + s * 0.02)
342
+ radius = (box[3] - box[1]) / 2
343
+ pen.rounded_rectangle(tuple(v + s * 0.07 for v in box), radius, fill=ink)
344
+ pen.rounded_rectangle(box, radius, fill=accent)
345
+ elif name == "panel":
346
+ box = (x0 - s * 0.55, y0 - s * 0.16, x1 + s * 0.45, y1 + s * 0.08)
347
+ pen.rounded_rectangle(box, round(s * 0.22), fill=_rgba(palette, "ink", 0.8))
348
+ pen.rounded_rectangle(
349
+ (box[0] + s * 0.16, box[1] + s * 0.2, box[0] + s * 0.26, box[3] - s * 0.2), round(s * 0.05), fill=accent
350
+ )
351
+ elif name == "tape":
352
+ bx0, by0, bx1, by1 = x0 - s * 0.4, y0 - s * 0.08, x1 + s * 0.4, y1 + s * 0.02
353
+ teeth, depth = 5, s * 0.12
354
+ left = [(bx0 + (depth if n % 2 else 0), by0 + (by1 - by0) * n / teeth) for n in range(teeth + 1)]
355
+ right = [(bx1 - (depth if n % 2 else 0), by1 - (by1 - by0) * n / teeth) for n in range(teeth + 1)]
356
+ pen.polygon(left + right, fill=_rgba(palette, "paper", 0.88))
357
+ elif name == "label":
358
+ box = (x0 - s * 0.55, y0 - s * 0.1, x1 + s * 0.35, y1 + s * 0.02)
359
+ pen.rectangle(box, fill=ink)
360
+ pen.rectangle((box[0] + s * 0.16, box[1] + s * 0.32, box[0] + s * 0.34, box[3] - s * 0.3), fill=accent)
361
+ elif name == "chip":
362
+ box = (x0 - s * 0.55, y0 - s * 0.1, x1 + s * 0.35, y1 + s * 0.02)
363
+ pen.rounded_rectangle(box, round(s * 0.25), fill=paper)
364
+ pen.rounded_rectangle(
365
+ (box[0] + s * 0.16, box[1] + s * 0.28, box[0] + s * 0.3, box[3] - s * 0.26), round(s * 0.07), fill=accent
366
+ )
367
+ elif name == "flag":
368
+ bx0, by0, bx1, by1 = x0 - s * 0.35, y0 - s * 0.1, x1 + s * 0.7, y1 + s * 0.02
369
+ pen.polygon([(bx0, by0), (bx1, by0), (bx1 - s * 0.32, (by0 + by1) / 2), (bx1, by1), (bx0, by1)], fill=accent)
370
+ elif name == "memebox":
371
+ pen.rectangle((x0 - s * 0.22, y0 - s * 0.02, x1 + s * 0.22, y1 - s * 0.02), fill=accent)
372
+ elif name == "burst":
373
+ cx, cy = (x0 + x1) / 2, (y0 + y1) / 2 - s * 0.04
374
+ rx, ry = (x1 - x0) / 2 + s * 0.62, (y1 - y0) / 2 + s * 0.5
375
+
376
+ def star(scale):
377
+ points = []
378
+ for n in range(28):
379
+ r = (1 if n % 2 == 0 else 0.76 + 0.06 * math.sin(n * 1.7)) * scale
380
+ a = -math.pi / 2 + math.pi * n / 14
381
+ points.append((cx + rx * r * math.cos(a), cy + ry * r * math.sin(a)))
382
+ return points
383
+
384
+ pen.polygon(star(1.07), fill=ink)
385
+ pen.polygon(star(1.0), fill=accent)
386
+ elif name == "bubble":
387
+ box = (x0 - s * 0.45, y0 - s * 0.14, x1 + s * 0.45, y1 + s * 0.06)
388
+ line = s * 0.07
389
+ tail = [
390
+ (box[0] + s * 0.5, box[3] - s * 0.1),
391
+ (box[0] + s * 0.1, box[3] + s * 0.5),
392
+ (box[0] + s * 1.05, box[3] - s * 0.1),
393
+ ]
394
+ radius = round((box[3] - box[1]) * 0.42)
395
+ pen.rounded_rectangle(tuple(v + d for v, d in zip(box, (-line, -line, line, line))), radius + line, fill=ink)
396
+ pen.polygon(
397
+ [
398
+ (tail[0][0] - line, tail[0][1]),
399
+ (tail[1][0] - line * 0.8, tail[1][1] + line * 1.4),
400
+ (tail[2][0] + line, tail[2][1]),
401
+ ],
402
+ fill=ink,
403
+ )
404
+ pen.rounded_rectangle(box, radius, fill=paper)
405
+ pen.polygon(tail, fill=paper)
406
+ elif name == "stamp":
407
+ pen.rectangle(
408
+ (x0 - s * 0.38, y0 - s * 0.16, x1 + s * 0.38, y1 + s * 0.08), outline=accent, width=max(2, round(s * 0.08))
409
+ )
410
+ pen.rectangle(
411
+ (x0 - s * 0.24, y0 - s * 0.05, x1 + s * 0.24, y1 - s * 0.03), outline=accent, width=max(1, round(s * 0.03))
412
+ )
413
+ elif name == "price":
414
+ bx0, by0, bx1, by1 = x0 - s * 0.3, y0 - s * 0.12, x1 + s * 0.4, y1 + s * 0.04
415
+ tip = bx0 - s * 0.5
416
+ outline = [(tip, (by0 + by1) / 2), (bx0, by0), (bx1, by0), (bx1, by1), (bx0, by1)]
417
+ line = s * 0.05
418
+ pen.polygon(
419
+ [
420
+ (tip - line * 1.5, (by0 + by1) / 2),
421
+ (bx0 - line * 0.4, by0 - line),
422
+ (bx1 + line, by0 - line),
423
+ (bx1 + line, by1 + line),
424
+ (bx0 - line * 0.4, by1 + line),
425
+ ],
426
+ fill=ink,
427
+ )
428
+ pen.polygon(outline, fill=paper)
429
+ hole = (bx0 - s * 0.12, (by0 + by1) / 2)
430
+ pen.ellipse((hole[0] - s * 0.08, hole[1] - s * 0.08, hole[0] + s * 0.08, hole[1] + s * 0.08), fill=ink)
431
+ out.alpha_composite(canvas, (grow, grow))
432
+ _extras(pen, rects, extras, s, palette, out, has_detail, con)
433
+ box = crop or out.getchannel("A").getbbox()
434
+ if not box:
435
+ return out, (grow, grow), None
436
+ return out.crop(box), (grow - box[0], grow - box[1]), box
437
+
438
+
439
+ def _extras(pen, rects, extras, s, palette, out, has_detail, con):
440
+ paper, ink, accent = (_rgba(palette, n) for n in ("paper", "ink", "accent"))
441
+ first, last = rects[0], rects[-1]
442
+ titles = rects[:-1] if has_detail else rects
443
+ x0, y0, x1, y1 = (
444
+ min(r[0] for r in rects),
445
+ min(r[1] for r in rects),
446
+ max(r[2] for r in rects),
447
+ max(r[3] for r in rects),
448
+ )
449
+ for extra in extras:
450
+ if extra == "rule_below" and len(rects) > 1:
451
+ y = first[3] + s * 0.02
452
+ pen.line((x0 + s * 0.04, y, x0 + (x1 - x0) * 0.3, y), fill=paper, width=max(1, round(s * 0.03)))
453
+ elif extra == "rules":
454
+ r = last
455
+ y = (r[1] + r[3]) / 2 + s * 0.02
456
+ length, gap = s * 0.9, s * 0.3
457
+ width = max(1, round(s * 0.025))
458
+ colour = accent if len(rects) > 1 else paper
459
+ pen.line((r[0] - gap - length, y, r[0] - gap, y), fill=colour, width=width)
460
+ pen.line((r[2] + gap, y, r[2] + gap + length, y), fill=colour, width=width)
461
+ elif extra == "highlight":
462
+ # A marker stroke behind the lower half of each title row.
463
+ layer = Image.new("RGBA", out.size)
464
+ mark = ImageDraw.Draw(layer)
465
+ for r in titles:
466
+ h = r[3] - r[1]
467
+ mark.rectangle((r[0] - s * 0.12, r[1] + h * 0.52, r[2] + s * 0.12, r[1] + h * 0.95), fill=accent)
468
+ layer.alpha_composite(out)
469
+ out.paste(layer)
470
+ elif extra == "squiggle":
471
+ target = titles[-1]
472
+ y, amplitude, period = target[3] + s * 0.02, s * 0.07, s * 0.55
473
+ points = [
474
+ (target[0] + t, y + amplitude * math.sin(t / period * 2 * math.pi))
475
+ for t in range(0, max(2, round(target[2] - target[0])), 3)
476
+ ]
477
+ pen.line(points, fill=accent, width=max(2, round(s * 0.08)), joint="curve")
478
+ elif extra == "underline":
479
+ y = first[3] + s * 0.02
480
+ pen.line((first[0], y, first[2], y), fill=accent, width=max(1, round(s * 0.04)))
481
+ elif extra == "dot":
482
+ cx, cy, r = first[0] - s * 0.42, (first[1] + first[3]) / 2 + s * 0.02, s * 0.17
483
+ pen.ellipse((cx - r, cy - r, cx + r, cy + r), fill=accent)
484
+ elif extra == "vline":
485
+ x = x0 - s * 0.35
486
+ pen.line((x, y0 + s * 0.12, x, y1 - s * 0.05), fill=paper, width=max(1, round(s * 0.04)))
487
+ elif extra == "sparkles":
488
+ for (cx, cy), angles in (
489
+ ((x1 + s * 0.05, y0 + s * 0.05), (-75, -40, -5)),
490
+ ((x0 - s * 0.05, y1 - s * 0.1), (105, 140, 175)),
491
+ ):
492
+ for angle in angles:
493
+ a = math.radians(angle)
494
+ start = (cx + s * 0.18 * math.cos(a), cy + s * 0.18 * math.sin(a))
495
+ end = (cx + s * 0.5 * math.cos(a), cy + s * 0.5 * math.sin(a))
496
+ _brush(pen, [start, end], s * 0.045, ink)
497
+ _brush(pen, [start, end], s * 0.022, paper)
498
+ elif extra == "icon":
499
+ # A check for a strength, a cross for a drawback.
500
+ r = first
501
+ h = r[3] - r[1]
502
+ box = (r[0] - h * 1.05, r[1] + h * 0.04, r[0] - h * 0.2, r[1] + h * 0.89)
503
+ pen.rounded_rectangle(
504
+ box,
505
+ round(h * 0.16),
506
+ fill=_rgba(palette, "secondary" if con else "accent"),
507
+ outline=ink,
508
+ width=max(1, round(s * 0.05)),
509
+ )
510
+ bx0, by0, bx1, by1 = box
511
+ w = max(2, round(s * 0.11))
512
+ if con:
513
+ inset = (bx1 - bx0) * 0.28
514
+ pen.line((bx0 + inset, by0 + inset, bx1 - inset, by1 - inset), fill=paper, width=w)
515
+ pen.line((bx0 + inset, by1 - inset, bx1 - inset, by0 + inset), fill=paper, width=w)
516
+ else:
517
+ pen.line(
518
+ [
519
+ (bx0 + (bx1 - bx0) * 0.24, by0 + (by1 - by0) * 0.52),
520
+ (bx0 + (bx1 - bx0) * 0.43, by0 + (by1 - by0) * 0.72),
521
+ (bx0 + (bx1 - bx0) * 0.78, by0 + (by1 - by0) * 0.3),
522
+ ],
523
+ fill=paper,
524
+ width=w,
525
+ joint="curve",
526
+ )
527
+
528
+
529
+ def _compose(item, profile, font_path, size, text=True, crop=None):
530
+ """The component drawn on its own canvas, its look, its rows' type and where the canvas origin landed."""
531
+ group, name, look = _look(profile)
532
+ if (group, name) == ("sticker", "sticker") and profile["component_id"] == "burst":
533
+ look = {"rows": (_row("paper", ("accent", 0.12), ("ink", 0.22)), _row("paper", ("ink", 0.14)))}
534
+ canvas, rects, font_size, has_detail, rows = _assemble(item, profile, font_path, size, look, group, text)
535
+ con = item.text.lstrip().startswith(CONS)
536
+ image, origin, box = _shape(canvas, rects, font_size, look, profile["presets"]["palette"], has_detail, con, crop)
537
+ return image, look, rows, origin, box
538
+
539
+
540
+ def _content(item, profile, font_path, size):
541
+ image, look, *_ = _compose(item, profile, font_path, size)
542
+ return image, look
543
+
544
+
545
+ def _tilted(image, kind, look):
546
+ angle = look.get("tilt", TILT.get(kind, 0) if GROUPS[kind] == "sticker" else 0)
547
+ return image.rotate(angle, resample=Image.Resampling.BICUBIC, expand=True) if angle else image
548
+
549
+
550
+ def _base(timeline):
551
+ """Type scales with the short side, so vertical video keeps landscape proportions."""
552
+ return min(timeline.width, timeline.height)
553
+
554
+
555
+ def _size(timeline, profile, scale):
556
+ kind = profile["component_id"]
557
+ appearance = _appearance(profile)
558
+ # type_scale enlarges every component (short-form video); hero_scale only the big ones.
559
+ factor = appearance.get("type_scale", 1.0) * (appearance.get("hero_scale", 1.0) if kind in HERO else 1.0)
560
+ return max(11, round(_base(timeline) * SIZES[kind] * factor * scale))
561
+
562
+
563
+ def _pops(profile):
564
+ return profile["component_id"] in POP and _motion(profile) in {"pop", "punch", "bounce", "stamp"}
565
+
566
+
567
+ def _fit(font_path, timeline, item, profile):
568
+ kind = profile["component_id"]
569
+ w, h = timeline.width, timeline.height
570
+ x, y = round(w * profile["x"]), round(h * profile["y"])
571
+ limit = min(w * (0.86 if kind in HERO else 0.62), w * 0.95 - x)
572
+ scale = 1.0
573
+ while True:
574
+ image, look = _content(item, profile, font_path, _size(timeline, profile, scale))
575
+ content = _tilted(image, kind, look)
576
+ slack = POP_SLACK if _pops(profile) else 1.0
577
+ rise = 0 if _pops(profile) else _base(timeline) * 0.012
578
+ width, height = math.ceil(content.width * slack), math.ceil(content.height * slack + rise)
579
+ if (width <= limit and y + height <= h * 0.77) or scale <= 0.6:
580
+ break
581
+ scale -= 0.05
582
+ if width > limit or y + height > h * 0.77:
583
+ raise TitleRenderError("program_component_overflow")
584
+ return (x, y, x + width, y + height), content, scale
585
+
586
+
587
+ def layout(draw, font_path, timeline, item, profile):
588
+ box, _, scale = _fit(font_path, timeline, item, profile)
589
+ pad = max(6, round(_base(timeline) * 0.016))
590
+ return box, load_style_font(font_path, _size(timeline, profile, scale), profile), pad
591
+
592
+
593
+ _BUNDLED_FONTS = Path(__file__).parent / "fonts"
594
+
595
+
596
+ def editable(font_path, timeline, item, profile):
597
+ """Split a resting component into its type and a frame-sized picture of everything else.
598
+
599
+ Editors that set type themselves (DaVinci Resolve's Fusion titles) rebuild the rows from
600
+ this. Each row gives its text, face, pixel size, tracking, fill, shadow and the frame point
601
+ where its baseline starts. None when the type cannot be rebuilt faithfully elsewhere: a
602
+ tilted component, an outlined row, or a face that only Clipen carries."""
603
+ kind = profile["component_id"]
604
+ group, _, look = _look(profile)
605
+ if look.get("tilt", TILT.get(kind, 0) if group == "sticker" else 0):
606
+ return None
607
+ (x, y, right, bottom), _, scale = _fit(font_path, timeline, item, profile)
608
+ size = _size(timeline, profile, scale)
609
+ image, look, rows, origin, box = _compose(item, profile, font_path, size)
610
+ if any(
611
+ spec["stroke"] or spec["outer"] or Path(getattr(face, "path", "")).parent == _BUNDLED_FONTS
612
+ for _, face, spec, _ in rows
613
+ ):
614
+ return None
615
+ bare, *_ = _compose(item, profile, font_path, size, text=False, crop=box)
616
+ # Where draw_graphic rests the component once its entrance has finished.
617
+ left, top = (
618
+ (x + ((right - x) - image.width) // 2, y + ((bottom - y) - image.height) // 2) if _pops(profile) else (x, y)
619
+ )
620
+ picture = Image.new("RGBA", (timeline.width, timeline.height))
621
+ picture.alpha_composite(bare, (max(0, left), max(0, top)))
622
+ palette = profile["presets"]["palette"]
623
+ types = []
624
+ for value, face, spec, (ox, oy) in rows:
625
+ family, style = face.getname()
626
+ shadow = _shadow(face, spec["shadow"], palette)
627
+ types.append(
628
+ {
629
+ "text": value,
630
+ "family": family,
631
+ "style": style,
632
+ "size": face.size,
633
+ "track": spec["track"],
634
+ "fill": list(_color(palette, spec["fill"])),
635
+ "x": left + origin[0] + ox,
636
+ "baseline": top + origin[1] + oy - face.getbbox(value, anchor="ls")[1],
637
+ "shadow": shadow
638
+ and {
639
+ "colour": list(shadow[0]),
640
+ "offset": 0 if spec["shadow"] == "glow" else shadow[1],
641
+ "blur": shadow[2],
642
+ },
643
+ }
644
+ )
645
+ return {"rows": types, "picture": picture if picture.getbbox() else None}
646
+
647
+
648
+ def _pop(progress):
649
+ """Settle from 70% to full size with an overshoot capped by the footprint slack."""
650
+ if progress >= 1:
651
+ return 1.0
652
+ c1 = 1.70158
653
+ t = progress - 1
654
+ back = 1 + (c1 + 1) * t**3 + c1 * t**2
655
+ return max(0.7, min(POP_SLACK, 0.7 + 0.3 * back))
656
+
657
+
658
+ def _scaled(content, sx, sy):
659
+ return content.resize(
660
+ (max(1, round(content.width * sx)), max(1, round(content.height * sy))), Image.Resampling.BICUBIC
661
+ )
662
+
663
+
664
+ def _faded(image, opacity):
665
+ if opacity >= 1:
666
+ return image
667
+ alpha = image.getchannel("A").point(lambda value: round(value * opacity))
668
+ image = image.copy()
669
+ image.putalpha(alpha)
670
+ return image
671
+
672
+
673
+ def _wiped(image, progress, steps=None):
674
+ """Reveal from the left; stepped reveals read like writing stroke by stroke."""
675
+ if progress >= 1:
676
+ return image
677
+ amount = math.floor(progress * steps) / steps if steps else progress
678
+ mask = Image.new("L", image.size, 0)
679
+ ImageDraw.Draw(mask).rectangle((0, 0, round(image.width * amount), image.height), fill=255)
680
+ alpha = Image.composite(image.getchannel("A"), mask, mask)
681
+ image = image.copy()
682
+ image.putalpha(alpha)
683
+ return image
684
+
685
+
686
+ def draw_graphic(draw, font_path, timeline, item, profile, relative_frame=None):
687
+ kind = profile["component_id"]
688
+ motion = _motion(profile)
689
+ (x, y, right, bottom), content, _ = _fit(font_path, timeline, item, profile)
690
+ entry, exit_frames = frames(profile)
691
+ relative = entry if relative_frame is None else relative_frame
692
+ progress = min(1.0, max(0.0, (relative + 1) / entry))
693
+ leaving = min(1.0, max(0.0, (item.duration_frames - relative) / exit_frames))
694
+ ease = 1 - (1 - progress) ** 3
695
+ pops = _pops(profile)
696
+ opacity = min(min(1.0, progress * (2.5 if pops else 1.4)), leaving)
697
+ if motion == "fade":
698
+ opacity = min(ease, leaving)
699
+ if pops:
700
+ if motion == "bounce":
701
+ # Drop in squashed, stretch, then settle, all inside the footprint slack.
702
+ stretch = 1 + 0.08 * math.sin(progress * math.pi * 1.5) * (1 - progress) if progress < 1 else 1.0
703
+ sized = _scaled(
704
+ content,
705
+ min(POP_SLACK, 2 - stretch) * (0.75 + 0.25 * ease),
706
+ min(POP_SLACK, stretch) * (0.75 + 0.25 * ease),
707
+ )
708
+ elif motion == "stamp":
709
+ # Thud down from slightly large to rest.
710
+ sized = _scaled(content, *(POP_SLACK - (POP_SLACK - 1) * ease,) * 2)
711
+ else:
712
+ sized = _scaled(content, *(_pop(progress),) * 2)
713
+ left = x + ((right - x) - sized.width) // 2
714
+ top = y + ((bottom - y) - sized.height) // 2
715
+ if motion == "punch" and progress < 1:
716
+ # A short shake that decays inside the slack.
717
+ room = ((right - x) - sized.width) // 2
718
+ left += round(math.sin(relative * 2.6) * room * (1 - progress))
719
+ else:
720
+ sized = content
721
+ left = x
722
+ top = y + round((1 - ease) * _base(timeline) * 0.012) # a short rise within the footprint
723
+ if motion == "write":
724
+ sized = _wiped(sized, progress, steps=max(4, len(item.text)))
725
+ elif motion == "slide":
726
+ sized = _wiped(sized, ease)
727
+ sized = _faded(sized, opacity)
728
+ canvas = getattr(draw, "_image", None)
729
+ if canvas is not None:
730
+ canvas.alpha_composite(sized, (max(0, left), max(0, top)))
731
+ if kind in {"pointer", "circle"} and "target" in profile:
732
+ _target(draw, timeline, profile, (x, y, right, bottom), ease, opacity)
733
+ return x, y, right, bottom
734
+
735
+
736
+ def _brush(draw, points, radius, fill):
737
+ """A round-capped stroke through points, like a marker on the frame."""
738
+ for (x0, y0), (x1, y1) in zip(points, points[1:]):
739
+ draw.line((x0, y0, x1, y1), fill=fill, width=round(radius * 2))
740
+ for x, y in (points[0], points[-1]):
741
+ draw.ellipse((x - radius, y - radius, x + radius, y + radius), fill=fill)
742
+
743
+
744
+ def _marker(draw, strokes, width, paper, ink):
745
+ """Outline every stroke first so overlapping strokes join without seams."""
746
+ for points in strokes:
747
+ _brush(draw, points, width * 1.3, ink)
748
+ for points in strokes:
749
+ _brush(draw, points, width * 0.66, paper)
750
+
751
+
752
+ def _target(draw, timeline, profile, bounds, ease, opacity):
753
+ palette = profile["presets"]["palette"]
754
+ clean = _appearance(profile).get("arrow") == "clean"
755
+ paper, ink = _rgba(palette, "paper", opacity), _rgba(palette, "ink", opacity)
756
+ if clean:
757
+ # Interface-style callouts: one accent line without a dark outline.
758
+ paper, ink = _rgba(palette, "accent", opacity), _rgba(palette, "accent", opacity)
759
+ target = profile["target"]
760
+ tx, ty = target["x"] * timeline.width, target["y"] * timeline.height
761
+ radius = target["radius"] * min(timeline.width, timeline.height)
762
+ width = max(4.0, _base(timeline) * (0.007 if clean else 0.0105))
763
+ if profile["component_id"] == "circle":
764
+ # A slightly uneven hand-drawn loop, or a true circle for clean callouts.
765
+ wobble, turn = (0, 2.0) if clean else (0.06, 2.15)
766
+ steps = max(2, round(48 * ease))
767
+ loop = [
768
+ (
769
+ tx + radius * (1 + wobble * math.sin(n * 0.7)) * math.cos(-math.pi / 2 + turn * math.pi * n / 48),
770
+ ty + radius * (1 + wobble * math.sin(n * 0.7)) * math.sin(-math.pi / 2 + turn * math.pi * n / 48),
771
+ )
772
+ for n in range(steps + 1)
773
+ ]
774
+ _marker(draw, [loop], width, paper, ink)
775
+ return
776
+ x, y, right, bottom = bounds
777
+ # Leave from the label side facing the target, and bow the path like a quick hand stroke.
778
+ start = (right + width * 2, (y + bottom) / 2) if tx >= (x + right) / 2 else (x - width * 2, (y + bottom) / 2)
779
+ gap = radius * 0.9
780
+ distance = math.hypot(tx - start[0], ty - start[1]) or 1.0
781
+ end = (tx - (tx - start[0]) / distance * gap, ty - (ty - start[1]) / distance * gap)
782
+ bend = 0 if clean else 0.22 * distance
783
+ control = (
784
+ (start[0] + end[0]) / 2 + (end[1] - start[1]) / distance * bend,
785
+ (start[1] + end[1]) / 2 - (end[0] - start[0]) / distance * bend,
786
+ )
787
+ steps = max(2, round(32 * ease))
788
+ curve = [
789
+ (
790
+ (1 - t) ** 2 * start[0] + 2 * (1 - t) * t * control[0] + t**2 * end[0],
791
+ (1 - t) ** 2 * start[1] + 2 * (1 - t) * t * control[1] + t**2 * end[1],
792
+ )
793
+ for t in (n / 32 for n in range(steps + 1))
794
+ ]
795
+ strokes = [curve]
796
+ if ease > 0.85:
797
+ tip = curve[-1]
798
+ angle = math.atan2(tip[1] - curve[-3][1], tip[0] - curve[-3][0])
799
+ length = width * (4 if clean else 5.5)
800
+ strokes += [
801
+ [tip, (tip[0] - length * math.cos(angle + side), tip[1] - length * math.sin(angle + side))]
802
+ for side in (-0.6, 0.6)
803
+ ]
804
+ _marker(draw, strokes, width, paper, ink)