@clipen/clipen-cli 1.0.7 → 1.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -6
- package/apps/web/dist/assets/index-DPgSvXZ1.js +15 -0
- package/apps/web/dist/assets/index-DmPWV61v.css +1 -0
- package/apps/web/dist/index.html +2 -2
- package/bin/clipen.js +229 -68
- package/package.json +8 -2
- package/pyproject.toml +1 -1
- package/src/clipen/__init__.py +1 -1
- package/src/clipen/analysis/qwen_worker.py +13 -4
- package/src/clipen/analysis/setup.py +2 -1
- package/src/clipen/analysis/setup_cli.py +58 -12
- package/src/clipen/bootstrap.py +499 -167
- package/src/clipen/cli.py +40 -15
- package/src/clipen/controller/app.py +120 -38
- package/src/clipen/controller/server.py +70 -27
- package/src/clipen/editing/agent_repository.py +8 -0
- package/src/clipen/editing/agent_tools.py +1 -1
- package/src/clipen/editing/caption_design.py +134 -0
- package/src/clipen/editing/change_report.py +17 -0
- package/src/clipen/editing/component_looks.py +3 -3
- package/src/clipen/editing/component_review.py +0 -38
- package/src/clipen/editing/coordinator.py +20 -0
- package/src/clipen/editing/evidence_jobs.py +3 -1
- package/src/clipen/editing/history_changes.py +56 -27
- package/src/clipen/editing/production_catalog.py +67 -13
- package/src/clipen/editing/production_media.py +13 -2
- package/src/clipen/editing/production_preview.py +11 -9
- package/src/clipen/editing/production_progress.py +48 -3
- package/src/clipen/editing/production_tools.py +11 -20
- package/src/clipen/editing/program_subtitles.py +5 -13
- package/src/clipen/editing/resolve_export.py +2 -183
- package/src/clipen/editing/skill_tools.py +142 -218
- package/src/clipen/editing/tool_receipts.py +2 -1
- package/src/clipen/editing/versions.py +55 -10
- package/src/clipen/editing/visual_preview.py +62 -0
- package/src/clipen/editing/workbench.py +21 -1
- package/src/clipen/i18n.py +155 -0
- package/src/clipen/locale_en.py +546 -0
- package/src/clipen/mcp_editing/__main__.py +8 -0
- package/src/clipen/media/storage.py +2 -2
- package/src/clipen/own_ffmpeg.py +113 -0
- package/src/clipen/projects/models.py +16 -1
- package/src/clipen/projects/picker.py +3 -1
- package/src/clipen/projects/storage.py +17 -1
- package/src/clipen/providers/claude_editing.py +3 -1
- package/src/clipen/providers/codex_editing.py +3 -1
- package/src/clipen/providers/editing_agent.py +42 -5
- package/src/clipen/providers/liveness.py +39 -0
- package/src/clipen/rendering/custom_components.py +216 -25
- package/src/clipen/rendering/custom_render.py +90 -54
- package/src/clipen/rendering/fetched_fonts.py +127 -0
- package/src/clipen/rendering/production_effects.py +24 -2
- package/src/clipen/rendering/program_components.py +18 -240
- package/src/clipen/rendering/renderer.py +23 -17
- package/src/clipen/rendering/titles.py +29 -12
- package/src/clipen/skills/builtin/SKILL.md +3 -3
- package/src/clipen/skills/builtin/assets/sample-daily-vlog.mp4 +0 -0
- package/src/clipen/skills/builtin/assets/sample-event-recap.mp4 +0 -0
- package/src/clipen/skills/builtin/assets/sample-food-review.mp4 +0 -0
- package/src/clipen/skills/builtin/assets/sample-product-demo.mp4 +0 -0
- package/src/clipen/skills/builtin/assets/sample-product-review.mp4 +0 -0
- package/src/clipen/skills/builtin/assets/sample-product-unboxing.mp4 +0 -0
- package/src/clipen/skills/builtin/assets/sample-shorts-highlight.mp4 +0 -0
- package/src/clipen/skills/builtin/assets/sample-travel-highlight.mp4 +0 -0
- package/src/clipen/skills/builtin/assets/sample-travel-vlog.mp4 +0 -0
- package/src/clipen/skills/builtin/assets/style-tpl-daily-vlog.png +0 -0
- package/src/clipen/skills/builtin/assets/style-tpl-event-recap.png +0 -0
- package/src/clipen/skills/builtin/assets/style-tpl-food-review.png +0 -0
- package/src/clipen/skills/builtin/assets/style-tpl-product-demo.png +0 -0
- package/src/clipen/skills/builtin/assets/style-tpl-product-review.png +0 -0
- package/src/clipen/skills/builtin/assets/style-tpl-product-unboxing.png +0 -0
- package/src/clipen/skills/builtin/assets/style-tpl-shorts-highlight.png +0 -0
- package/src/clipen/skills/builtin/assets/style-tpl-travel-highlight.png +0 -0
- package/src/clipen/skills/builtin/assets/style-tpl-travel-vlog.png +0 -0
- package/src/clipen/skills/builtin/display.en.json +89 -0
- package/src/clipen/skills/builtin/references/captions.md +12 -0
- package/src/clipen/skills/builtin/references/graphics.md +6 -6
- package/src/clipen/skills/builtin/template-skills/clipen-daily-vlog/SKILL.md +9 -4
- package/src/clipen/skills/builtin/template-skills/clipen-event-recap/SKILL.md +10 -5
- package/src/clipen/skills/builtin/template-skills/clipen-food-review/SKILL.md +9 -4
- package/src/clipen/skills/builtin/template-skills/clipen-product-demo/SKILL.md +10 -6
- package/src/clipen/skills/builtin/template-skills/clipen-product-review/SKILL.md +10 -6
- package/src/clipen/skills/builtin/template-skills/clipen-product-unboxing/SKILL.md +9 -7
- package/src/clipen/skills/builtin/template-skills/clipen-shorts-highlight/SKILL.md +10 -5
- package/src/clipen/skills/builtin/template-skills/clipen-travel-highlight/SKILL.md +10 -5
- package/src/clipen/skills/builtin/template-skills/clipen-travel-vlog/SKILL.md +10 -7
- package/src/clipen/skills/catalog.py +22 -8
- package/src/clipen/skills/package.py +1 -10
- package/src/clipen/terminal.py +344 -0
- package/src/clipen/workspace.py +97 -28
- package/apps/web/dist/assets/index-C3Zw0WaM.js +0 -14
- package/apps/web/dist/assets/index-CaxEZKLc.css +0 -1
- package/src/clipen/editing/resolve_titles.py +0 -291
- package/src/clipen/rendering/program_creator.py +0 -804
- package/src/clipen/rendering/program_identities.py +0 -350
- package/src/clipen/rendering/program_motion.py +0 -186
- package/src/clipen/rendering/program_studio.py +0 -227
- package/src/clipen/rendering/program_styles.py +0 -321
- package/src/clipen/skills/builtin/components.json +0 -133
|
@@ -1,804 +0,0 @@
|
|
|
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)
|