@clipen/clipen-cli 0.0.0-stage → 1.0.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 (232) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +79 -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 +52 -4
  11. package/pyproject.toml +28 -0
  12. package/requirements/asr-runtime.txt +13 -0
  13. package/requirements/runtime.txt +20 -0
  14. package/src/clipen/__init__.py +3 -0
  15. package/src/clipen/analysis/__init__.py +89 -0
  16. package/src/clipen/analysis/artifacts.py +416 -0
  17. package/src/clipen/analysis/audio.py +392 -0
  18. package/src/clipen/analysis/evidence.py +102 -0
  19. package/src/clipen/analysis/evidence_service.py +751 -0
  20. package/src/clipen/analysis/image_similarity.py +143 -0
  21. package/src/clipen/analysis/keyframes.py +171 -0
  22. package/src/clipen/analysis/models.py +230 -0
  23. package/src/clipen/analysis/qwen_worker.py +629 -0
  24. package/src/clipen/analysis/resources.py +21 -0
  25. package/src/clipen/analysis/runtime.py +64 -0
  26. package/src/clipen/analysis/service.py +626 -0
  27. package/src/clipen/analysis/setup.py +935 -0
  28. package/src/clipen/analysis/setup_cli.py +117 -0
  29. package/src/clipen/analysis/storage.py +2046 -0
  30. package/src/clipen/analysis/transcription.py +948 -0
  31. package/src/clipen/analysis/visual.py +176 -0
  32. package/src/clipen/annotations/__init__.py +10 -0
  33. package/src/clipen/annotations/models.py +22 -0
  34. package/src/clipen/annotations/storage.py +380 -0
  35. package/src/clipen/annotations/suggestions.py +120 -0
  36. package/src/clipen/asr_release.py +139 -0
  37. package/src/clipen/bootstrap.py +967 -0
  38. package/src/clipen/cli.py +84 -0
  39. package/src/clipen/controller/__init__.py +17 -0
  40. package/src/clipen/controller/app.py +1470 -0
  41. package/src/clipen/controller/server.py +543 -0
  42. package/src/clipen/domain/__init__.py +30 -0
  43. package/src/clipen/domain/errors.py +5 -0
  44. package/src/clipen/domain/media_time.py +117 -0
  45. package/src/clipen/domain/timeline.py +584 -0
  46. package/src/clipen/domain/validation.py +15 -0
  47. package/src/clipen/editing/EDITORIAL_REGRESSIONS.md +38 -0
  48. package/src/clipen/editing/PROGRAM_FORMAT.md +102 -0
  49. package/src/clipen/editing/README.md +115 -0
  50. package/src/clipen/editing/REFACTOR_DESIGN.md +241 -0
  51. package/src/clipen/editing/REFACTOR_HANDOFF.md +73 -0
  52. package/src/clipen/editing/__init__.py +15 -0
  53. package/src/clipen/editing/agent.py +13 -0
  54. package/src/clipen/editing/agent_models.py +134 -0
  55. package/src/clipen/editing/agent_repository.py +757 -0
  56. package/src/clipen/editing/agent_tools.py +532 -0
  57. package/src/clipen/editing/alignment.py +110 -0
  58. package/src/clipen/editing/alignment_setup.py +108 -0
  59. package/src/clipen/editing/alignment_worker.py +22 -0
  60. package/src/clipen/editing/change_report.py +71 -0
  61. package/src/clipen/editing/compiler.py +41 -0
  62. package/src/clipen/editing/component_looks.py +107 -0
  63. package/src/clipen/editing/component_review.py +78 -0
  64. package/src/clipen/editing/coordinator.py +374 -0
  65. package/src/clipen/editing/editorial_review.py +187 -0
  66. package/src/clipen/editing/evidence_jobs.py +295 -0
  67. package/src/clipen/editing/history_changes.py +131 -0
  68. package/src/clipen/editing/music_choices.py +15 -0
  69. package/src/clipen/editing/production_catalog.py +192 -0
  70. package/src/clipen/editing/production_limits.py +59 -0
  71. package/src/clipen/editing/production_media.py +226 -0
  72. package/src/clipen/editing/production_preview.py +180 -0
  73. package/src/clipen/editing/production_progress.py +141 -0
  74. package/src/clipen/editing/production_requirements.py +50 -0
  75. package/src/clipen/editing/production_tools.py +596 -0
  76. package/src/clipen/editing/program_audio.py +118 -0
  77. package/src/clipen/editing/program_direction.py +49 -0
  78. package/src/clipen/editing/program_story.py +53 -0
  79. package/src/clipen/editing/program_subtitles.py +134 -0
  80. package/src/clipen/editing/programme_review.py +73 -0
  81. package/src/clipen/editing/prompt_history.py +47 -0
  82. package/src/clipen/editing/recut.py +127 -0
  83. package/src/clipen/editing/refinement_references.py +188 -0
  84. package/src/clipen/editing/resolve_export.py +616 -0
  85. package/src/clipen/editing/resolve_titles.py +234 -0
  86. package/src/clipen/editing/skill_tools.py +575 -0
  87. package/src/clipen/editing/source_dialogue.py +82 -0
  88. package/src/clipen/editing/speech_coverage.py +30 -0
  89. package/src/clipen/editing/speech_quality.py +6 -0
  90. package/src/clipen/editing/tool_receipts.py +52 -0
  91. package/src/clipen/editing/versions.py +161 -0
  92. package/src/clipen/editing/visual_preview.py +93 -0
  93. package/src/clipen/editing/workbench.py +74 -0
  94. package/src/clipen/filesystem.py +25 -0
  95. package/src/clipen/finishing/README.md +103 -0
  96. package/src/clipen/finishing/__init__.py +1 -0
  97. package/src/clipen/finishing/audio.py +149 -0
  98. package/src/clipen/finishing/catalog.py +181 -0
  99. package/src/clipen/finishing/models.py +14 -0
  100. package/src/clipen/finishing/renderer.py +26 -0
  101. package/src/clipen/mcp_editing/__init__.py +5 -0
  102. package/src/clipen/mcp_editing/__main__.py +40 -0
  103. package/src/clipen/mcp_editing/server.py +353 -0
  104. package/src/clipen/media/__init__.py +50 -0
  105. package/src/clipen/media/discovery.py +266 -0
  106. package/src/clipen/media/models.py +360 -0
  107. package/src/clipen/media/preview.py +147 -0
  108. package/src/clipen/media/probe.py +446 -0
  109. package/src/clipen/media/service.py +317 -0
  110. package/src/clipen/media/storage.py +742 -0
  111. package/src/clipen/music_vocabulary.py +5 -0
  112. package/src/clipen/planning/__init__.py +9 -0
  113. package/src/clipen/planning/models.py +240 -0
  114. package/src/clipen/power.py +23 -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 +269 -0
  119. package/src/clipen/projects/evidence_schema.py +52 -0
  120. package/src/clipen/projects/finishing_schema.py +34 -0
  121. package/src/clipen/projects/models.py +192 -0
  122. package/src/clipen/projects/picker.py +256 -0
  123. package/src/clipen/projects/preview_schema.py +29 -0
  124. package/src/clipen/projects/service.py +175 -0
  125. package/src/clipen/projects/storage.py +1478 -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 +75 -0
  129. package/src/clipen/providers/__init__.py +43 -0
  130. package/src/clipen/providers/account.py +194 -0
  131. package/src/clipen/providers/claude.py +375 -0
  132. package/src/clipen/providers/claude_editing.py +111 -0
  133. package/src/clipen/providers/codex.py +1036 -0
  134. package/src/clipen/providers/codex_editing.py +91 -0
  135. package/src/clipen/providers/editing_agent.py +565 -0
  136. package/src/clipen/providers/errors.py +51 -0
  137. package/src/clipen/providers/recovery.py +270 -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 +126 -0
  141. package/src/clipen/rendering/custom_components.py +240 -0
  142. package/src/clipen/rendering/custom_render.py +197 -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 +64 -0
  158. package/src/clipen/rendering/program_components.py +193 -0
  159. package/src/clipen/rendering/program_creator.py +649 -0
  160. package/src/clipen/rendering/program_identities.py +255 -0
  161. package/src/clipen/rendering/program_motion.py +141 -0
  162. package/src/clipen/rendering/program_studio.py +168 -0
  163. package/src/clipen/rendering/program_styles.py +235 -0
  164. package/src/clipen/rendering/program_subtitles.py +52 -0
  165. package/src/clipen/rendering/renderer.py +708 -0
  166. package/src/clipen/rendering/storage.py +243 -0
  167. package/src/clipen/rendering/titles.py +360 -0
  168. package/src/clipen/skills/__init__.py +4 -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 +218 -0
  228. package/src/clipen/skills/library.py +131 -0
  229. package/src/clipen/skills/package.py +231 -0
  230. package/src/clipen/skills/visual_styles.py +44 -0
  231. package/src/clipen/workspace.py +210 -0
  232. package/uninstall +25 -0
@@ -0,0 +1,649 @@
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
+ import math
15
+ from pathlib import Path
16
+
17
+ from PIL import Image, ImageDraw, ImageFilter
18
+
19
+ from .titles import TitleRenderError
20
+ from .program_styles import load_style_font, styled_text_length, draw_style_text
21
+
22
+ HERO = {'program_title', 'headline', 'impact', 'chapter', 'burst'}
23
+ POP = {'impact', 'burst', 'reaction', 'headline', 'program_title'}
24
+ SIZES = {'program_title': .105, 'headline': .082, 'chapter': .038, 'impact': .13, 'burst': .1,
25
+ 'reaction': .096, 'location': .036, 'person': .056, 'fact': .062, 'pointer': .052, 'circle': .05}
26
+ TILT = {'impact': -4, 'reaction': -5, 'burst': 4}
27
+ POP_SLACK = 1.08
28
+ GROUPS = {'program_title': 'title', 'headline': 'title', 'location': 'tag', 'chapter': 'tag',
29
+ 'impact': 'sticker', 'reaction': 'sticker', 'burst': 'sticker',
30
+ 'fact': 'info', 'person': 'info', 'pointer': 'info', 'circle': 'info'}
31
+ DEFAULT_LOOK = {'title': 'outline', 'tag': 'shadow', 'sticker': 'sticker', 'info': 'outline'}
32
+
33
+
34
+ def _row(fill, stroke=None, outer=None, track=0, shadow=None):
35
+ return {'fill': fill, 'stroke': stroke, 'outer': outer, 'track': track, 'shadow': shadow}
36
+
37
+
38
+ # Row styles are (primary, detail). Colours name palette entries, optionally
39
+ # with an alpha multiplier. A look may add a shape behind the text, extras
40
+ # drawn around single rows, a tilt and a row arrangement.
41
+ LOOKS = {
42
+ ('title', 'outline'): {'rows': (_row('paper', ('ink', .13)), _row('accent', ('ink', .14))), 'split': True},
43
+ ('title', 'block'): {'rows': (_row('paper', ('ink', .05)), _row('ink')), 'shape': 'block'},
44
+ ('title', 'serif'): {'rows': (_row('paper', track=.16, shadow='soft'), _row(('paper', .88), track=.22, shadow='soft')),
45
+ 'extras': ('rule_below',), 'detail_scale': .4, 'row_gap': .24},
46
+ ('title', 'pill'): {'rows': (_row('paper'), _row(('paper', .92))), 'shape': 'pill'},
47
+ ('title', 'doodle'): {'rows': (_row('paper', ('ink', .05), shadow='hard:accent'), _row('paper', ('ink', .08))),
48
+ 'extras': ('squiggle',), 'split': True, 'row_gap': .26},
49
+ ('title', 'panel'): {'rows': (_row('paper'), _row('accent')), 'shape': 'panel'},
50
+ ('title', 'bar'): {'rows': (_row('paper', ('ink', .1)), _row('paper', ('ink', .12))), 'extras': ('highlight',)},
51
+ ('title', 'rules'): {'rows': (_row('accent', track=.06, shadow='deep'), _row('paper', track=.3, shadow='deep')),
52
+ 'extras': ('rules',), 'detail_scale': .4},
53
+ ('title', 'meme'): {'rows': (_row('paper', ('secondary', .15), ('ink', .09)), _row('accent', ('ink', .14)))},
54
+ ('tag', 'shadow'): {'rows': (_row('paper', ('ink', .06), shadow='drop'),), 'join': ' | '},
55
+ ('tag', 'tape'): {'rows': (_row('ink'),), 'join': ' ', 'shape': 'tape', 'tilt': -2},
56
+ ('tag', 'label'): {'rows': (_row('paper'),), 'join': ' ', 'shape': 'label'},
57
+ ('tag', 'rules'): {'rows': (_row('paper', track=.24, shadow='deep'),), 'join': ' ', 'extras': ('rules',)},
58
+ ('tag', 'dot'): {'rows': (_row('paper', ('ink', .1)),), 'join': '|', 'extras': ('dot',)},
59
+ ('tag', 'keycap'): {'rows': (_row('ink'), _row('paper')), 'inline': True, 'boxes': ('accent', ('ink', .82)),
60
+ 'detail_scale': 1.},
61
+ ('tag', 'chip'): {'rows': (_row('ink'),), 'join': ' ', 'shape': 'chip'},
62
+ ('tag', 'flag'): {'rows': (_row('ink'),), 'join': '|', 'shape': 'flag'},
63
+ ('tag', 'meme'): {'rows': (_row('ink'),), 'join': ' ', 'shape': 'memebox', 'tilt': -2},
64
+ ('sticker', 'sticker'): {'rows': (_row('accent', ('ink', .10), ('paper', .24)), _row('paper', ('ink', .14)))},
65
+ ('sticker', 'burst'): {'rows': (_row('paper', ('ink', .1)), _row('ink')), 'shape': 'burst'},
66
+ ('sticker', 'quiet'): {'rows': (_row('paper', track=.1, shadow='soft'), _row(('paper', .85), track=.2, shadow='soft')),
67
+ 'tilt': 0},
68
+ ('sticker', 'bubble'): {'rows': (_row('ink'), _row(('ink', .7))), 'shape': 'bubble', 'tilt': -3},
69
+ ('sticker', 'doodle'): {'rows': (_row('accent', ('paper', .1), shadow='soft'), _row('paper', shadow='soft')),
70
+ 'extras': ('sparkles',)},
71
+ ('sticker', 'pill'): {'rows': (_row('ink'), _row(('ink', .75))), 'shape': 'pill', 'tilt': 0},
72
+ ('sticker', 'stamp'): {'rows': (_row('accent'), _row('accent')), 'shape': 'stamp', 'tilt': -7},
73
+ ('sticker', 'glow'): {'rows': (_row('accent', track=.08, shadow='glow'), _row('paper', track=.16, shadow='deep')),
74
+ 'tilt': 0},
75
+ ('sticker', 'meme'): {'rows': (_row('accent', ('ink', .11), ('paper', .2), shadow='hard:secondary'),
76
+ _row('paper', ('ink', .14)))},
77
+ ('info', 'outline'): {'rows': (_row('accent', ('ink', .12)), _row('paper', ('ink', .12)))},
78
+ ('info', 'label'): {'rows': (_row('paper'), _row('ink')), 'boxes': ('ink', 'accent')},
79
+ ('info', 'quiet'): {'rows': (_row('paper', track=.06, shadow='soft'), _row(('paper', .82), shadow='soft')),
80
+ 'extras': ('vline',)},
81
+ ('info', 'price'): {'rows': (_row('ink'), _row('accent')), 'shape': 'price'},
82
+ ('info', 'doodle'): {'rows': (_row('paper', ('ink', .05), shadow='hard:accent'), _row('paper', ('ink', .1)))},
83
+ ('info', 'callout'): {'rows': (_row('paper'), _row('accent')), 'shape': 'panel'},
84
+ ('info', 'verdict'): {'rows': (_row('paper', ('ink', .12)), _row('paper', ('ink', .12))), 'extras': ('icon',)},
85
+ ('info', 'name'): {'rows': (_row('accent', track=.04, shadow='deep'), _row('paper', shadow='deep')),
86
+ 'extras': ('underline',), 'row_gap': .2},
87
+ ('info', 'meme'): {'rows': (_row('paper', ('ink', .14)), _row('accent', ('ink', .14)))},
88
+ }
89
+ # A verdict reads as a drawback when the label starts like one.
90
+ CONS = ('缺點', '缺', '扣分', '不推', '踩雷')
91
+
92
+
93
+ def is_creator(profile):
94
+ return profile.get('presets', {}).get('appearance', {}).get('skin') == 'creator'
95
+
96
+
97
+ def _appearance(profile):
98
+ return profile['presets'].get('appearance', {})
99
+
100
+
101
+ def _look(profile):
102
+ group = GROUPS[profile['component_id']]
103
+ name = _appearance(profile).get('looks', {}).get(group, DEFAULT_LOOK[group])
104
+ if (group, name) not in LOOKS:
105
+ raise TitleRenderError('program_component_unknown_look')
106
+ return group, name, LOOKS[group, name]
107
+
108
+
109
+ def _motion(profile):
110
+ return _appearance(profile).get('motion_style', 'pop')
111
+
112
+
113
+ def frames(profile):
114
+ motion = profile['presets']['motion']
115
+ appearance = _appearance(profile)
116
+ hero = profile['component_id'] in HERO
117
+ entry = appearance.get('hero_entry_frames', motion['entry_frames']) if hero else motion['entry_frames']
118
+ exit_frames = appearance.get('hero_exit_frames', motion['exit_frames']) if hero else motion['exit_frames']
119
+ return max(1, entry), max(1, exit_frames)
120
+
121
+
122
+ def _rgba(palette, name, alpha=1.):
123
+ r, g, b, a = palette[name]
124
+ return r, g, b, round(a * alpha)
125
+
126
+
127
+ def _color(palette, spec):
128
+ if spec is None:
129
+ return None
130
+ name, alpha = (spec, 1.) if isinstance(spec, str) else spec
131
+ return _rgba(palette, name, alpha)
132
+
133
+
134
+ def _stroke(font, ratio):
135
+ return max(1, round(font.size * ratio)) if ratio else 0
136
+
137
+
138
+ def _length(value, font, track):
139
+ if not track:
140
+ return styled_text_length(value, font)
141
+ return sum(styled_text_length(c, font) for c in value) + font.size * track * (len(value) - 1)
142
+
143
+
144
+ def _draw_text(pen, x, y, value, font, fill, stroke=0, stroke_fill=None, track=0):
145
+ if not track:
146
+ draw_style_text(pen, (x, y), value, font, fill, stroke, stroke_fill)
147
+ return
148
+ # Keep one baseline for the line; each glyph would otherwise sit on its own top edge.
149
+ top = font.getbbox(value, anchor='ls')[1]
150
+ for character in value:
151
+ lift = 0 if character.isspace() else font.getbbox(character, anchor='ls')[1] - top
152
+ draw_style_text(pen, (x, y + lift), character, font, fill, stroke, stroke_fill)
153
+ x += styled_text_length(character, font) + font.size * track
154
+
155
+
156
+ def _shadow(font, shadow, palette):
157
+ """A row shadow as (colour, offset, blur) in pixels, or None."""
158
+ if not shadow:
159
+ return None
160
+ blur = round(font.size * {'glow': .16, 'deep': .1, 'soft': .07}[shadow]) if shadow in {'soft', 'deep', 'glow'} else 0
161
+ offset = round(font.size * (.07 if shadow.startswith('hard') else .04 if shadow in {'soft', 'deep'} else .06))
162
+ colour = {'drop': _rgba(palette, 'ink', .47), 'soft': _rgba(palette, 'ink', .6), 'deep': _rgba(palette, 'ink', .9),
163
+ 'glow': _rgba(palette, 'accent', .75)}.get(shadow) or _rgba(palette, shadow.split(':')[1])
164
+ return colour, offset, blur
165
+
166
+
167
+ def _row_image(value, font, spec, palette, text=True):
168
+ """One line of type with its outline, outer ring and shadow, plus the margin used.
169
+
170
+ Without text the row keeps its size but stays empty, so shapes around it land where they would."""
171
+ stroke_color, stroke_ratio = spec['stroke'] or (None, 0)
172
+ outer_color, outer_ratio = spec['outer'] or (None, 0)
173
+ inner, ring = _stroke(font, stroke_ratio), _stroke(font, stroke_ratio) + _stroke(font, outer_ratio)
174
+ shadow = _shadow(font, spec['shadow'], palette)
175
+ colour, offset, blur = shadow or (None, 0, 0)
176
+ margin = ring + blur * 3 + offset + 2
177
+ width, height = _length(value, font, spec['track']), font.size * 1.18
178
+ image = Image.new('RGBA', (math.ceil(width + margin * 2), math.ceil(height + margin * 2)))
179
+ if not text:
180
+ return image, margin, width, height
181
+ if shadow:
182
+ layer = Image.new('RGBA', image.size)
183
+ dx = 0 if spec['shadow'] == 'glow' else offset
184
+ _draw_text(ImageDraw.Draw(layer), margin + dx, margin + dx, value, font, colour, ring, colour, spec['track'])
185
+ if blur:
186
+ layer = layer.filter(ImageFilter.GaussianBlur(blur))
187
+ image.alpha_composite(layer)
188
+ pen = ImageDraw.Draw(image)
189
+ fill = _color(palette, spec['fill'])
190
+ if outer_color:
191
+ outer = _color(palette, outer_color)
192
+ _draw_text(pen, margin, margin, value, font, outer, ring, outer, spec['track'])
193
+ _draw_text(pen, margin, margin, value, font, fill, inner, _color(palette, stroke_color), spec['track'])
194
+ return image, margin, width, height
195
+
196
+
197
+ def _texts(item, look, group):
198
+ primary, _, detail = item.text.partition('\n')
199
+ if 'join' in look:
200
+ return [primary + (look['join'] + detail if detail else '')], False
201
+ rows = primary.split('・', 1) if look.get('split') and '・' in primary else [primary]
202
+ return [r for r in rows if r] + ([detail] if detail else []), bool(detail)
203
+
204
+
205
+ def _assemble(item, profile, font_path, size, look, group, text=True):
206
+ """Lay rows out (stacked or side by side) and return the canvas, each row's text box and its type.
207
+
208
+ A row's type is (text, font, spec, origin): origin is where the text is drawn on the canvas."""
209
+ palette = profile['presets']['palette']
210
+ kind = profile['component_id']
211
+ texts, has_detail = _texts(item, look, group)
212
+ font = load_style_font(font_path, size, profile)
213
+ default_scale = .62 if kind in {'program_title', 'headline'} else .7
214
+ small = load_style_font(font_path, max(10, round(size * look.get('detail_scale', default_scale))), profile, role='detail')
215
+ primary_spec, detail_spec = look['rows'][0], look['rows'][-1]
216
+ rows, types = [], []
217
+ for index, value in enumerate(texts):
218
+ is_detail = has_detail and index == len(texts) - 1
219
+ face, spec = (small, detail_spec) if is_detail else (font, primary_spec)
220
+ rows.append(_row_image(value, face, spec, palette, text))
221
+ types.append((value, face, spec))
222
+ boxes = look.get('boxes')
223
+ pad_x, pad_y = (round(size * .34), round(size * .12)) if boxes else (0, 0)
224
+ gap = (pad_x * 2 + round(size * .2)) if look.get('inline') else pad_y * 2 + round(size * look.get('row_gap', 0))
225
+ outer = max(margin for _, margin, _, _ in rows)
226
+ if look.get('inline'):
227
+ width = sum(w for _, _, w, _ in rows) + gap * (len(rows) - 1)
228
+ height = max(h for _, _, _, h in rows)
229
+ else:
230
+ width = max(w for _, _, w, _ in rows)
231
+ height = sum(h for _, _, _, h in rows) + gap * (len(rows) - 1)
232
+ edge = outer + max(pad_x, pad_y)
233
+ canvas = Image.new('RGBA', (math.ceil(width + edge * 2), math.ceil(height + edge * 2)))
234
+ rects, cursor = [], edge
235
+ for image, margin, w, h in rows:
236
+ x, y = (cursor, edge + (height - h) / 2) if look.get('inline') else (edge, cursor)
237
+ rects.append((x, y, x + w, y + h))
238
+ cursor += (w if look.get('inline') else h) + gap
239
+ if boxes:
240
+ pen = ImageDraw.Draw(canvas)
241
+ for (x0, y0, x1, y1), colour, (_, _, _, h) in zip(rects, boxes, rows):
242
+ radius = round(h * (.22 if look.get('inline') else .08))
243
+ pen.rounded_rectangle((x0 - pad_x, y0 - pad_y, x1 + pad_x, y1 + pad_y * .4), radius, fill=_color(palette, colour))
244
+ placed = []
245
+ for (image, margin, _, _), (x, y, _, _), row in zip(rows, rects, types):
246
+ canvas.alpha_composite(image, (round(x - margin), round(y - margin)))
247
+ placed.append(row + ((round(x - margin) + margin, round(y - margin) + margin),))
248
+ return canvas, rects, font.size, has_detail, placed
249
+
250
+
251
+ def _shape(canvas, rects, size, look, palette, has_detail=False, con=False, crop=None):
252
+ """Draw the look's shape behind the text on a larger canvas.
253
+
254
+ Returns the image, where the canvas origin landed on it, and the crop used; pass that crop back
255
+ to cut a second drawing of the same component (one without type) identically."""
256
+ name = look.get('shape')
257
+ extras = look.get('extras', ())
258
+ if not name and not extras:
259
+ return canvas, (0, 0), None
260
+ grow = round(size * 1.1)
261
+ out = Image.new('RGBA', (canvas.width + grow * 2, canvas.height + grow * 2))
262
+ pen = ImageDraw.Draw(out)
263
+ rects = [(x0 + grow, y0 + grow, x1 + grow, y1 + grow) for x0, y0, x1, y1 in rects]
264
+ x0, y0 = min(r[0] for r in rects), min(r[1] for r in rects)
265
+ x1, y1 = max(r[2] for r in rects), max(r[3] for r in rects)
266
+ paper, ink, accent = (_rgba(palette, n) for n in ('paper', 'ink', 'accent'))
267
+ s = size
268
+ if name == 'block':
269
+ skew = (y1 - y0) * .2
270
+ bx0, by0, bx1, by1 = x0 - s * .3, y0 - s * .04, x1 + s * .3 + skew, y1 + s * .02
271
+ poly = lambda dx, dy: [(bx0 + skew + dx, by0 + dy), (bx1 + dx, by0 + dy), (bx1 - skew + dx, by1 + dy), (bx0 + dx, by1 + dy)]
272
+ pen.polygon(poly(s * .09, s * .09), fill=ink)
273
+ pen.polygon(poly(0, 0), fill=accent)
274
+ elif name == 'pill':
275
+ box = (x0 - s * .42, y0 - s * .06, x1 + s * .42, y1 + s * .02)
276
+ radius = (box[3] - box[1]) / 2
277
+ pen.rounded_rectangle(tuple(v + s * .07 for v in box), radius, fill=ink)
278
+ pen.rounded_rectangle(box, radius, fill=accent)
279
+ elif name == 'panel':
280
+ box = (x0 - s * .55, y0 - s * .16, x1 + s * .45, y1 + s * .08)
281
+ pen.rounded_rectangle(box, round(s * .22), fill=_rgba(palette, 'ink', .8))
282
+ pen.rounded_rectangle((box[0] + s * .16, box[1] + s * .2, box[0] + s * .26, box[3] - s * .2), round(s * .05), fill=accent)
283
+ elif name == 'tape':
284
+ bx0, by0, bx1, by1 = x0 - s * .4, y0 - s * .08, x1 + s * .4, y1 + s * .02
285
+ teeth, depth = 5, s * .12
286
+ left = [(bx0 + (depth if n % 2 else 0), by0 + (by1 - by0) * n / teeth) for n in range(teeth + 1)]
287
+ right = [(bx1 - (depth if n % 2 else 0), by1 - (by1 - by0) * n / teeth) for n in range(teeth + 1)]
288
+ pen.polygon(left + right, fill=_rgba(palette, 'paper', .88))
289
+ elif name == 'label':
290
+ box = (x0 - s * .55, y0 - s * .1, x1 + s * .35, y1 + s * .02)
291
+ pen.rectangle(box, fill=ink)
292
+ pen.rectangle((box[0] + s * .16, box[1] + s * .32, box[0] + s * .34, box[3] - s * .3), fill=accent)
293
+ elif name == 'chip':
294
+ box = (x0 - s * .55, y0 - s * .1, x1 + s * .35, y1 + s * .02)
295
+ pen.rounded_rectangle(box, round(s * .25), fill=paper)
296
+ pen.rounded_rectangle((box[0] + s * .16, box[1] + s * .28, box[0] + s * .3, box[3] - s * .26), round(s * .07), fill=accent)
297
+ elif name == 'flag':
298
+ bx0, by0, bx1, by1 = x0 - s * .35, y0 - s * .1, x1 + s * .7, y1 + s * .02
299
+ pen.polygon([(bx0, by0), (bx1, by0), (bx1 - s * .32, (by0 + by1) / 2), (bx1, by1), (bx0, by1)], fill=accent)
300
+ elif name == 'memebox':
301
+ pen.rectangle((x0 - s * .22, y0 - s * .02, x1 + s * .22, y1 - s * .02), fill=accent)
302
+ elif name == 'burst':
303
+ cx, cy = (x0 + x1) / 2, (y0 + y1) / 2 - s * .04
304
+ rx, ry = (x1 - x0) / 2 + s * .62, (y1 - y0) / 2 + s * .5
305
+ def star(scale):
306
+ points = []
307
+ for n in range(28):
308
+ r = (1 if n % 2 == 0 else .76 + .06 * math.sin(n * 1.7)) * scale
309
+ a = -math.pi / 2 + math.pi * n / 14
310
+ points.append((cx + rx * r * math.cos(a), cy + ry * r * math.sin(a)))
311
+ return points
312
+ pen.polygon(star(1.07), fill=ink)
313
+ pen.polygon(star(1.), fill=accent)
314
+ elif name == 'bubble':
315
+ box = (x0 - s * .45, y0 - s * .14, x1 + s * .45, y1 + s * .06)
316
+ line = s * .07
317
+ tail = [(box[0] + s * .5, box[3] - s * .1), (box[0] + s * .1, box[3] + s * .5), (box[0] + s * 1.05, box[3] - s * .1)]
318
+ radius = round((box[3] - box[1]) * .42)
319
+ pen.rounded_rectangle(tuple(v + d for v, d in zip(box, (-line, -line, line, line))), radius + line, fill=ink)
320
+ pen.polygon([(tail[0][0] - line, tail[0][1]), (tail[1][0] - line * .8, tail[1][1] + line * 1.4), (tail[2][0] + line, tail[2][1])], fill=ink)
321
+ pen.rounded_rectangle(box, radius, fill=paper)
322
+ pen.polygon(tail, fill=paper)
323
+ elif name == 'stamp':
324
+ pen.rectangle((x0 - s * .38, y0 - s * .16, x1 + s * .38, y1 + s * .08), outline=accent, width=max(2, round(s * .08)))
325
+ pen.rectangle((x0 - s * .24, y0 - s * .05, x1 + s * .24, y1 - s * .03), outline=accent, width=max(1, round(s * .03)))
326
+ elif name == 'price':
327
+ bx0, by0, bx1, by1 = x0 - s * .3, y0 - s * .12, x1 + s * .4, y1 + s * .04
328
+ tip = bx0 - s * .5
329
+ outline = [(tip, (by0 + by1) / 2), (bx0, by0), (bx1, by0), (bx1, by1), (bx0, by1)]
330
+ line = s * .05
331
+ pen.polygon([(tip - line * 1.5, (by0 + by1) / 2), (bx0 - line * .4, by0 - line), (bx1 + line, by0 - line),
332
+ (bx1 + line, by1 + line), (bx0 - line * .4, by1 + line)], fill=ink)
333
+ pen.polygon(outline, fill=paper)
334
+ hole = (bx0 - s * .12, (by0 + by1) / 2)
335
+ pen.ellipse((hole[0] - s * .08, hole[1] - s * .08, hole[0] + s * .08, hole[1] + s * .08), fill=ink)
336
+ out.alpha_composite(canvas, (grow, grow))
337
+ _extras(pen, rects, extras, s, palette, out, has_detail, con)
338
+ box = crop or out.getchannel('A').getbbox()
339
+ if not box:
340
+ return out, (grow, grow), None
341
+ return out.crop(box), (grow - box[0], grow - box[1]), box
342
+
343
+
344
+ def _extras(pen, rects, extras, s, palette, out, has_detail, con):
345
+ paper, ink, accent = (_rgba(palette, n) for n in ('paper', 'ink', 'accent'))
346
+ first, last = rects[0], rects[-1]
347
+ titles = rects[:-1] if has_detail else rects
348
+ x0, y0, x1, y1 = min(r[0] for r in rects), min(r[1] for r in rects), max(r[2] for r in rects), max(r[3] for r in rects)
349
+ for extra in extras:
350
+ if extra == 'rule_below' and len(rects) > 1:
351
+ y = first[3] + s * .02
352
+ pen.line((x0 + s * .04, y, x0 + (x1 - x0) * .3, y), fill=paper, width=max(1, round(s * .03)))
353
+ elif extra == 'rules':
354
+ r = last
355
+ y = (r[1] + r[3]) / 2 + s * .02
356
+ length, gap = s * .9, s * .3
357
+ width = max(1, round(s * .025))
358
+ colour = accent if len(rects) > 1 else paper
359
+ pen.line((r[0] - gap - length, y, r[0] - gap, y), fill=colour, width=width)
360
+ pen.line((r[2] + gap, y, r[2] + gap + length, y), fill=colour, width=width)
361
+ elif extra == 'highlight':
362
+ # A marker stroke behind the lower half of each title row.
363
+ layer = Image.new('RGBA', out.size)
364
+ mark = ImageDraw.Draw(layer)
365
+ for r in titles:
366
+ h = r[3] - r[1]
367
+ mark.rectangle((r[0] - s * .12, r[1] + h * .52, r[2] + s * .12, r[1] + h * .95), fill=accent)
368
+ layer.alpha_composite(out)
369
+ out.paste(layer)
370
+ elif extra == 'squiggle':
371
+ target = titles[-1]
372
+ y, amplitude, period = target[3] + s * .02, s * .07, s * .55
373
+ points = [(target[0] + t, y + amplitude * math.sin(t / period * 2 * math.pi))
374
+ for t in range(0, max(2, round(target[2] - target[0])), 3)]
375
+ pen.line(points, fill=accent, width=max(2, round(s * .08)), joint='curve')
376
+ elif extra == 'underline':
377
+ y = first[3] + s * .02
378
+ pen.line((first[0], y, first[2], y), fill=accent, width=max(1, round(s * .04)))
379
+ elif extra == 'dot':
380
+ cx, cy, r = first[0] - s * .42, (first[1] + first[3]) / 2 + s * .02, s * .17
381
+ pen.ellipse((cx - r, cy - r, cx + r, cy + r), fill=accent)
382
+ elif extra == 'vline':
383
+ x = x0 - s * .35
384
+ pen.line((x, y0 + s * .12, x, y1 - s * .05), fill=paper, width=max(1, round(s * .04)))
385
+ elif extra == 'sparkles':
386
+ for (cx, cy), angles in (((x1 + s * .05, y0 + s * .05), (-75, -40, -5)), ((x0 - s * .05, y1 - s * .1), (105, 140, 175))):
387
+ for angle in angles:
388
+ a = math.radians(angle)
389
+ start = (cx + s * .18 * math.cos(a), cy + s * .18 * math.sin(a))
390
+ end = (cx + s * .5 * math.cos(a), cy + s * .5 * math.sin(a))
391
+ _brush(pen, [start, end], s * .045, ink)
392
+ _brush(pen, [start, end], s * .022, paper)
393
+ elif extra == 'icon':
394
+ # A check for a strength, a cross for a drawback.
395
+ r = first
396
+ h = r[3] - r[1]
397
+ box = (r[0] - h * 1.05, r[1] + h * .04, r[0] - h * .2, r[1] + h * .89)
398
+ pen.rounded_rectangle(box, round(h * .16), fill=_rgba(palette, 'secondary' if con else 'accent'), outline=ink,
399
+ width=max(1, round(s * .05)))
400
+ bx0, by0, bx1, by1 = box
401
+ w = max(2, round(s * .11))
402
+ if con:
403
+ inset = (bx1 - bx0) * .28
404
+ pen.line((bx0 + inset, by0 + inset, bx1 - inset, by1 - inset), fill=paper, width=w)
405
+ pen.line((bx0 + inset, by1 - inset, bx1 - inset, by0 + inset), fill=paper, width=w)
406
+ else:
407
+ pen.line([(bx0 + (bx1 - bx0) * .24, by0 + (by1 - by0) * .52), (bx0 + (bx1 - bx0) * .43, by0 + (by1 - by0) * .72),
408
+ (bx0 + (bx1 - bx0) * .78, by0 + (by1 - by0) * .3)], fill=paper, width=w, joint='curve')
409
+
410
+
411
+ def _compose(item, profile, font_path, size, text=True, crop=None):
412
+ """The component drawn on its own canvas, its look, its rows' type and where the canvas origin landed."""
413
+ group, name, look = _look(profile)
414
+ if (group, name) == ('sticker', 'sticker') and profile['component_id'] == 'burst':
415
+ look = {'rows': (_row('paper', ('accent', .12), ('ink', .22)), _row('paper', ('ink', .14)))}
416
+ canvas, rects, font_size, has_detail, rows = _assemble(item, profile, font_path, size, look, group, text)
417
+ con = item.text.lstrip().startswith(CONS)
418
+ image, origin, box = _shape(canvas, rects, font_size, look, profile['presets']['palette'], has_detail, con, crop)
419
+ return image, look, rows, origin, box
420
+
421
+
422
+ def _content(item, profile, font_path, size):
423
+ image, look, *_ = _compose(item, profile, font_path, size)
424
+ return image, look
425
+
426
+
427
+ def _tilted(image, kind, look):
428
+ angle = look.get('tilt', TILT.get(kind, 0) if GROUPS[kind] == 'sticker' else 0)
429
+ return image.rotate(angle, resample=Image.Resampling.BICUBIC, expand=True) if angle else image
430
+
431
+
432
+ def _base(timeline):
433
+ """Type scales with the short side, so vertical video keeps landscape proportions."""
434
+ return min(timeline.width, timeline.height)
435
+
436
+
437
+ def _size(timeline, profile, scale):
438
+ kind = profile['component_id']
439
+ appearance = _appearance(profile)
440
+ # type_scale enlarges every component (short-form video); hero_scale only the big ones.
441
+ factor = appearance.get('type_scale', 1.) * (appearance.get('hero_scale', 1.) if kind in HERO else 1.)
442
+ return max(11, round(_base(timeline) * SIZES[kind] * factor * scale))
443
+
444
+
445
+ def _pops(profile):
446
+ return profile['component_id'] in POP and _motion(profile) in {'pop', 'punch', 'bounce', 'stamp'}
447
+
448
+
449
+ def _fit(font_path, timeline, item, profile):
450
+ kind = profile['component_id']
451
+ w, h = timeline.width, timeline.height
452
+ x, y = round(w * profile['x']), round(h * profile['y'])
453
+ limit = min(w * (.86 if kind in HERO else .62), w * .95 - x)
454
+ scale = 1.
455
+ while True:
456
+ image, look = _content(item, profile, font_path, _size(timeline, profile, scale))
457
+ content = _tilted(image, kind, look)
458
+ slack = POP_SLACK if _pops(profile) else 1.
459
+ rise = 0 if _pops(profile) else _base(timeline) * .012
460
+ width, height = math.ceil(content.width * slack), math.ceil(content.height * slack + rise)
461
+ if (width <= limit and y + height <= h * .77) or scale <= .6:
462
+ break
463
+ scale -= .05
464
+ if width > limit or y + height > h * .77:
465
+ raise TitleRenderError('program_component_overflow')
466
+ return (x, y, x + width, y + height), content, scale
467
+
468
+
469
+ def layout(draw, font_path, timeline, item, profile):
470
+ box, _, scale = _fit(font_path, timeline, item, profile)
471
+ pad = max(6, round(_base(timeline) * .016))
472
+ return box, load_style_font(font_path, _size(timeline, profile, scale), profile), pad
473
+
474
+
475
+ _BUNDLED_FONTS = Path(__file__).parent / 'fonts'
476
+
477
+
478
+ def editable(font_path, timeline, item, profile):
479
+ """Split a resting component into its type and a frame-sized picture of everything else.
480
+
481
+ Editors that set type themselves (DaVinci Resolve's Fusion titles) rebuild the rows from
482
+ this. Each row gives its text, face, pixel size, tracking, fill, shadow and the frame point
483
+ where its baseline starts. None when the type cannot be rebuilt faithfully elsewhere: a
484
+ tilted component, an outlined row, or a face that only Clipen carries."""
485
+ kind = profile['component_id']
486
+ group, _, look = _look(profile)
487
+ if look.get('tilt', TILT.get(kind, 0) if group == 'sticker' else 0):
488
+ return None
489
+ (x, y, right, bottom), _, scale = _fit(font_path, timeline, item, profile)
490
+ size = _size(timeline, profile, scale)
491
+ image, look, rows, origin, box = _compose(item, profile, font_path, size)
492
+ if any(spec['stroke'] or spec['outer'] or Path(getattr(face, 'path', '')).parent == _BUNDLED_FONTS
493
+ for _, face, spec, _ in rows):
494
+ return None
495
+ bare, *_ = _compose(item, profile, font_path, size, text=False, crop=box)
496
+ # Where draw_graphic rests the component once its entrance has finished.
497
+ left, top = (x + ((right - x) - image.width) // 2, y + ((bottom - y) - image.height) // 2) if _pops(profile) else (x, y)
498
+ picture = Image.new('RGBA', (timeline.width, timeline.height))
499
+ picture.alpha_composite(bare, (max(0, left), max(0, top)))
500
+ palette = profile['presets']['palette']
501
+ types = []
502
+ for value, face, spec, (ox, oy) in rows:
503
+ family, style = face.getname()
504
+ shadow = _shadow(face, spec['shadow'], palette)
505
+ types.append({'text': value, 'family': family, 'style': style, 'size': face.size, 'track': spec['track'],
506
+ 'fill': list(_color(palette, spec['fill'])),
507
+ 'x': left + origin[0] + ox, 'baseline': top + origin[1] + oy - face.getbbox(value, anchor='ls')[1],
508
+ 'shadow': shadow and {'colour': list(shadow[0]), 'offset': 0 if spec['shadow'] == 'glow' else shadow[1],
509
+ 'blur': shadow[2]}})
510
+ return {'rows': types, 'picture': picture if picture.getbbox() else None}
511
+
512
+
513
+ def _pop(progress):
514
+ """Settle from 70% to full size with an overshoot capped by the footprint slack."""
515
+ if progress >= 1:
516
+ return 1.
517
+ c1 = 1.70158
518
+ t = progress - 1
519
+ back = 1 + (c1 + 1) * t ** 3 + c1 * t ** 2
520
+ return max(.7, min(POP_SLACK, .7 + .3 * back))
521
+
522
+
523
+ def _scaled(content, sx, sy):
524
+ return content.resize((max(1, round(content.width * sx)), max(1, round(content.height * sy))), Image.Resampling.BICUBIC)
525
+
526
+
527
+ def _faded(image, opacity):
528
+ if opacity >= 1:
529
+ return image
530
+ alpha = image.getchannel('A').point(lambda value: round(value * opacity))
531
+ image = image.copy()
532
+ image.putalpha(alpha)
533
+ return image
534
+
535
+
536
+ def _wiped(image, progress, steps=None):
537
+ """Reveal from the left; stepped reveals read like writing stroke by stroke."""
538
+ if progress >= 1:
539
+ return image
540
+ amount = math.floor(progress * steps) / steps if steps else progress
541
+ mask = Image.new('L', image.size, 0)
542
+ ImageDraw.Draw(mask).rectangle((0, 0, round(image.width * amount), image.height), fill=255)
543
+ alpha = Image.composite(image.getchannel('A'), mask, mask)
544
+ image = image.copy()
545
+ image.putalpha(alpha)
546
+ return image
547
+
548
+
549
+ def draw_graphic(draw, font_path, timeline, item, profile, relative_frame=None):
550
+ kind = profile['component_id']
551
+ motion = _motion(profile)
552
+ (x, y, right, bottom), content, _ = _fit(font_path, timeline, item, profile)
553
+ entry, exit_frames = frames(profile)
554
+ relative = entry if relative_frame is None else relative_frame
555
+ progress = min(1., max(0., (relative + 1) / entry))
556
+ leaving = min(1., max(0., (item.duration_frames - relative) / exit_frames))
557
+ ease = 1 - (1 - progress) ** 3
558
+ pops = _pops(profile)
559
+ opacity = min(min(1., progress * (2.5 if pops else 1.4)), leaving)
560
+ if motion == 'fade':
561
+ opacity = min(ease, leaving)
562
+ if pops:
563
+ if motion == 'bounce':
564
+ # Drop in squashed, stretch, then settle, all inside the footprint slack.
565
+ stretch = 1 + .08 * math.sin(progress * math.pi * 1.5) * (1 - progress) if progress < 1 else 1.
566
+ sized = _scaled(content, min(POP_SLACK, 2 - stretch) * (.75 + .25 * ease), min(POP_SLACK, stretch) * (.75 + .25 * ease))
567
+ elif motion == 'stamp':
568
+ # Thud down from slightly large to rest.
569
+ sized = _scaled(content, *(POP_SLACK - (POP_SLACK - 1) * ease,) * 2)
570
+ else:
571
+ sized = _scaled(content, *(_pop(progress),) * 2)
572
+ left = x + ((right - x) - sized.width) // 2
573
+ top = y + ((bottom - y) - sized.height) // 2
574
+ if motion == 'punch' and progress < 1:
575
+ # A short shake that decays inside the slack.
576
+ room = ((right - x) - sized.width) // 2
577
+ left += round(math.sin(relative * 2.6) * room * (1 - progress))
578
+ else:
579
+ sized = content
580
+ left = x
581
+ top = y + round((1 - ease) * _base(timeline) * .012) # a short rise within the footprint
582
+ if motion == 'write':
583
+ sized = _wiped(sized, progress, steps=max(4, len(item.text)))
584
+ elif motion == 'slide':
585
+ sized = _wiped(sized, ease)
586
+ sized = _faded(sized, opacity)
587
+ canvas = getattr(draw, '_image', None)
588
+ if canvas is not None:
589
+ canvas.alpha_composite(sized, (max(0, left), max(0, top)))
590
+ if kind in {'pointer', 'circle'} and 'target' in profile:
591
+ _target(draw, timeline, profile, (x, y, right, bottom), ease, opacity)
592
+ return x, y, right, bottom
593
+
594
+
595
+ def _brush(draw, points, radius, fill):
596
+ """A round-capped stroke through points, like a marker on the frame."""
597
+ for (x0, y0), (x1, y1) in zip(points, points[1:]):
598
+ draw.line((x0, y0, x1, y1), fill=fill, width=round(radius * 2))
599
+ for x, y in (points[0], points[-1]):
600
+ draw.ellipse((x - radius, y - radius, x + radius, y + radius), fill=fill)
601
+
602
+
603
+ def _marker(draw, strokes, width, paper, ink):
604
+ """Outline every stroke first so overlapping strokes join without seams."""
605
+ for points in strokes:
606
+ _brush(draw, points, width * 1.3, ink)
607
+ for points in strokes:
608
+ _brush(draw, points, width * .66, paper)
609
+
610
+
611
+ def _target(draw, timeline, profile, bounds, ease, opacity):
612
+ palette = profile['presets']['palette']
613
+ clean = _appearance(profile).get('arrow') == 'clean'
614
+ paper, ink = _rgba(palette, 'paper', opacity), _rgba(palette, 'ink', opacity)
615
+ if clean:
616
+ # Interface-style callouts: one accent line without a dark outline.
617
+ paper, ink = _rgba(palette, 'accent', opacity), _rgba(palette, 'accent', opacity)
618
+ target = profile['target']
619
+ tx, ty = target['x'] * timeline.width, target['y'] * timeline.height
620
+ radius = target['radius'] * min(timeline.width, timeline.height)
621
+ width = max(4., _base(timeline) * (.007 if clean else .0105))
622
+ if profile['component_id'] == 'circle':
623
+ # A slightly uneven hand-drawn loop, or a true circle for clean callouts.
624
+ wobble, turn = (0, 2.) if clean else (.06, 2.15)
625
+ steps = max(2, round(48 * ease))
626
+ loop = [(tx + radius * (1 + wobble * math.sin(n * .7)) * math.cos(-math.pi / 2 + turn * math.pi * n / 48),
627
+ ty + radius * (1 + wobble * math.sin(n * .7)) * math.sin(-math.pi / 2 + turn * math.pi * n / 48)) for n in range(steps + 1)]
628
+ _marker(draw, [loop], width, paper, ink)
629
+ return
630
+ x, y, right, bottom = bounds
631
+ # Leave from the label side facing the target, and bow the path like a quick hand stroke.
632
+ start = (right + width * 2, (y + bottom) / 2) if tx >= (x + right) / 2 else (x - width * 2, (y + bottom) / 2)
633
+ gap = radius * .9
634
+ distance = math.hypot(tx - start[0], ty - start[1]) or 1.
635
+ end = (tx - (tx - start[0]) / distance * gap, ty - (ty - start[1]) / distance * gap)
636
+ bend = 0 if clean else .22 * distance
637
+ control = ((start[0] + end[0]) / 2 + (end[1] - start[1]) / distance * bend,
638
+ (start[1] + end[1]) / 2 - (end[0] - start[0]) / distance * bend)
639
+ steps = max(2, round(32 * ease))
640
+ curve = [((1 - t) ** 2 * start[0] + 2 * (1 - t) * t * control[0] + t ** 2 * end[0],
641
+ (1 - t) ** 2 * start[1] + 2 * (1 - t) * t * control[1] + t ** 2 * end[1])
642
+ for t in (n / 32 for n in range(steps + 1))]
643
+ strokes = [curve]
644
+ if ease > .85:
645
+ tip = curve[-1]
646
+ angle = math.atan2(tip[1] - curve[-3][1], tip[0] - curve[-3][0])
647
+ length = width * (4 if clean else 5.5)
648
+ strokes += [[tip, (tip[0] - length * math.cos(angle + side), tip[1] - length * math.sin(angle + side))] for side in (-.6, .6)]
649
+ _marker(draw, strokes, width, paper, ink)