ffmpeg-skill 1.15.0 → 1.16.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.
@@ -0,0 +1,1515 @@
1
+ """Text people can see: font resolution per script, emoji clusters and their assets, drawtext
2
+ escaping and option building, and the per-character advance table the caption wrap measures with.
3
+ """
4
+ from __future__ import annotations
5
+
6
+ import os
7
+ import platform
8
+ import re
9
+ import shutil
10
+ import subprocess
11
+ import unicodedata
12
+ from pathlib import Path
13
+ from typing import Any, Dict, List, Optional, Sequence, Tuple
14
+ from _common.decision import escape_filter_path
15
+ from _common.emit import die, info
16
+ from _common.runner import STATE, _DRAWTEXT_PENDING, _drawtext_tmpdir, ffmpeg_version
17
+
18
+
19
+ def drawtext_boxborderw(vertical: int, horizontal: int) -> str:
20
+ """drawtext's per-side `boxborderw=top|right|bottom|left` (and the two-value `v|h` form)
21
+ arrived in FFmpeg 6.1; 5.x and 6.0 reject the `|` with "Error setting option boxborderw"
22
+ (found by the FFmpeg 5.1.1 CI job, #146). Older builds get the larger single value."""
23
+ if ffmpeg_version() >= (6, 1):
24
+ return f"{vertical}|{horizontal}"
25
+ return str(max(vertical, horizontal))
26
+
27
+
28
+ def default_font_file(font_name: str) -> Optional[str]:
29
+ """Resolve `font_name` to a concrete on-disk font file, so a caller can tell drawtext
30
+ `fontfile=<path>` instead of `font=<name>`, when possible.
31
+
32
+ On some real Windows ffmpeg builds (winget's gyan.dev 9.x), drawtext's own fontconfig
33
+ resolution crashes with an access violation whenever it has to resolve a font by family name
34
+ -- with or without a valid fonts.conf on FONTCONFIG_FILE. `fontfile=` is the only form
35
+ confirmed not to crash (#100), since it never touches fontconfig at all. `font_name` itself is
36
+ ignored on Windows for that reason: a fixed, near-universally-present system font is used
37
+ instead of trying to resolve the requested family (which would crash the same way).
38
+
39
+ On Linux/macOS this is best-effort and uses the real requested family: `fc-match` reports the
40
+ same file fontconfig would resolve `font_name` to anyway, so a caller gets the identical font,
41
+ just already resolved to a path -- fontfile= skips a redundant fontconfig lookup and equally
42
+ sidesteps the same class of crash if it exists on some build there too, but the fallback below
43
+ (returning None) is exercised routinely there, not just on failure.
44
+
45
+ Returns None when nothing could be resolved (fc-match missing/unavailable, or no well-known
46
+ Windows font file present); the caller falls back to font=<font_name>, the prior behaviour.
47
+ """
48
+ if platform.system() == "Windows":
49
+ windir = os.environ.get("WINDIR", "C:\\Windows")
50
+ fonts = Path(windir) / "Fonts"
51
+ # The requested family first: a file whose name starts with the family name with spaces
52
+ # removed (Noto Sans CJK JP -> NotoSansCJKjp-Regular.otf, Meiryo -> meiryo.ttc), then the
53
+ # common CJK system fonts when the request looks CJK (so Japanese text does not render as
54
+ # boxes in Arial), and Arial only as the last resort.
55
+ wanted = re.sub(r"[^a-z0-9]", "", (font_name or "").lower())
56
+ try:
57
+ files = sorted(fonts.iterdir()) if fonts.is_dir() else []
58
+ except OSError:
59
+ files = []
60
+ if wanted:
61
+ for f in files:
62
+ stem = re.sub(r"[^a-z0-9]", "", f.stem.lower())
63
+ if f.suffix.lower() in (".ttf", ".otf", ".ttc") and stem.startswith(wanted):
64
+ return str(f)
65
+ if any(k in wanted for k in ("cjk", "gothic", "mincho", "meiryo", "yugoth", "msgothic", "malgun", "simhei", "simsun", "jp", "kr", "sc", "tc")):
66
+ for name in ("NotoSansCJKjp-Regular.otf", "NotoSansCJK-Regular.ttc", "meiryo.ttc", "YuGothM.ttc", "msgothic.ttc", "malgun.ttf", "msyh.ttc"):
67
+ if (fonts / name).exists():
68
+ return str(fonts / name)
69
+ candidate = fonts / "arial.ttf"
70
+ return str(candidate) if candidate.exists() else None
71
+ exe = shutil.which("fc-match")
72
+ if not exe:
73
+ return None
74
+ try:
75
+ proc = subprocess.run([exe, "--format=%{file}\n", font_name], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, timeout=5)
76
+ except (subprocess.TimeoutExpired, OSError):
77
+ return None
78
+ if proc.returncode != 0:
79
+ return None
80
+ path = proc.stdout.splitlines()[0].strip() if proc.stdout.strip() else ""
81
+ return path if path and os.path.exists(path) else None
82
+
83
+
84
+ # --------------------------------------------------------------------------- script detection
85
+ # 1.12: non-Latin caption/overlay text used to render as tofu (empty boxes) whenever the default
86
+ # family carried no glyphs for it -- silently, because fontconfig substitutes SOMETHING for every
87
+ # request and ffmpeg exits 0 either way. The tools now detect the script of the text they are about
88
+ # to draw and resolve a font file that actually covers it; nothing found is a failed job, not a
89
+ # warning (a video full of boxes is not a delivery).
90
+ SCRIPTS = ("ja", "zh", "ko", "ar", "he", "hi", "bn", "ta", "th", "lo", "ru", "el", "latin")
91
+
92
+
93
+ LANGUAGE_NAMES = {
94
+ "ja": "Japanese", "zh": "Chinese", "ko": "Korean", "ar": "Arabic", "he": "Hebrew",
95
+ "hi": "Devanagari (Hindi/Marathi/Nepali)", "th": "Thai", "ru": "Cyrillic (Russian and others)",
96
+ "el": "Greek", "latin": "Latin", "bn": "Bengali", "ta": "Tamil", "lo": "Lao",
97
+ }
98
+
99
+
100
+ # fontconfig's own :lang= codes for each script we detect (zh uses zh-cn, the Simplified subset
101
+ # every CJK font that claims zh carries; the rest are the plain two-letter codes).
102
+ FC_LANG = {"ja": "ja", "zh": "zh-cn", "ko": "ko", "ar": "ar", "he": "he", "hi": "hi", "th": "th", "ru": "ru", "el": "el",
103
+ "bn": "bn", "ta": "ta", "lo": "lo"}
104
+
105
+
106
+ # Families tried in order, best first. The names are matched case-insensitively against the start
107
+ # of any family fontconfig reports for a file, so "Noto Sans CJK JP" also matches
108
+ # "Noto Sans CJK JP Black". Anything not listed still qualifies -- it just sorts after these.
109
+ PREFERRED_FAMILIES = {
110
+ "ja": ["Noto Sans CJK JP", "Noto Serif CJK JP", "Noto Sans JP", "Source Han Sans", "IPAPGothic", "IPAGothic", "IPA", "VL Gothic", "TakaoGothic", "WenQuanYi Zen Hei"],
111
+ "zh": ["Noto Sans CJK SC", "Noto Serif CJK SC", "Noto Sans SC", "Source Han Sans", "WenQuanYi Zen Hei", "WenQuanYi Micro Hei", "Droid Sans Fallback"],
112
+ "ko": ["Noto Sans CJK KR", "Noto Serif CJK KR", "Noto Sans KR", "Source Han Sans K", "NanumGothic", "Nanum Gothic", "Malgun Gothic", "WenQuanYi Zen Hei"],
113
+ "ar": ["Noto Sans Arabic", "Noto Naskh Arabic", "Amiri", "Scheherazade", "DejaVu Sans", "FreeSans", "FreeSerif"],
114
+ "he": ["Noto Sans Hebrew", "Noto Serif Hebrew", "DejaVu Sans", "FreeSans", "FreeSerif"],
115
+ "hi": ["Noto Sans Devanagari", "Noto Serif Devanagari", "Lohit Devanagari", "Mangal", "Nirmala UI", "Samyak Devanagari", "FreeSans", "FreeSerif"],
116
+ "th": ["Noto Sans Thai", "Noto Serif Thai", "Loma", "Garuda", "Waree", "Umpush", "Norasi", "Sarabun", "Leelawadee UI", "FreeSerif"],
117
+ "bn": ["Noto Sans Bengali", "Noto Serif Bengali", "Lohit Bengali", "Mukti Narrow", "Vrinda", "Nirmala UI", "FreeSerif"],
118
+ "ta": ["Noto Sans Tamil", "Noto Serif Tamil", "Lohit Tamil", "Latha", "Nirmala UI", "FreeSerif"],
119
+ "lo": ["Noto Sans Lao", "Noto Serif Lao", "Phetsarath OT", "Souliyo Unicode", "Saysettha OT", "DokChampa", "Leelawadee UI"],
120
+ "ru": ["Noto Sans", "DejaVu Sans", "Liberation Sans", "FreeSans", "FreeSerif"],
121
+ "el": ["Noto Sans", "DejaVu Sans", "Liberation Sans", "FreeSans", "FreeSerif"],
122
+ }
123
+
124
+
125
+ # Windows has no fontconfig: the system fonts are looked up by file name instead, best first.
126
+ WINDOWS_FONTS = {
127
+ "ko": [("malgun.ttf", "Malgun Gothic"), ("gulim.ttc", "Gulim"), ("batang.ttc", "Batang")],
128
+ "zh": [("msyh.ttc", "Microsoft YaHei"), ("simhei.ttf", "SimHei"), ("simsun.ttc", "SimSun")],
129
+ "ja": [("meiryo.ttc", "Meiryo"), ("YuGothM.ttc", "Yu Gothic Medium"), ("YuGothR.ttc", "Yu Gothic"), ("msgothic.ttc", "MS Gothic")],
130
+ "ar": [("tahoma.ttf", "Tahoma"), ("arial.ttf", "Arial")],
131
+ "he": [("tahoma.ttf", "Tahoma"), ("arial.ttf", "Arial")],
132
+ "hi": [("mangal.ttf", "Mangal"), ("Nirmala.ttf", "Nirmala UI"), ("NirmalaB.ttf", "Nirmala UI")],
133
+ "th": [("leelawui.ttf", "Leelawadee UI"), ("leelawad.ttf", "Leelawadee"), ("tahoma.ttf", "Tahoma")],
134
+ "bn": [("Nirmala.ttf", "Nirmala UI"), ("vrinda.ttf", "Vrinda")],
135
+ "ta": [("Nirmala.ttf", "Nirmala UI"), ("latha.ttf", "Latha")],
136
+ "lo": [("leelawui.ttf", "Leelawadee UI"), ("DokChamp.ttf", "DokChampa")],
137
+ "ru": [("arial.ttf", "Arial"), ("segoeui.ttf", "Segoe UI")],
138
+ "el": [("arial.ttf", "Arial"), ("segoeui.ttf", "Segoe UI")],
139
+ }
140
+
141
+
142
+ _SCRIPT_RANGES = (
143
+ ("ko", ((0x1100, 0x11FF), (0x3130, 0x318F), (0xA960, 0xA97F), (0xAC00, 0xD7FF))), # Hangul syllables + Jamo
144
+ ("kana", ((0x3040, 0x309F), (0x30A0, 0x30FF), (0x31F0, 0x31FF), (0xFF66, 0xFF9F))), # hiragana/katakana
145
+ ("han", ((0x3400, 0x4DBF), (0x4E00, 0x9FFF), (0xF900, 0xFAFF), (0x20000, 0x2A6DF))),
146
+ ("ar", ((0x0600, 0x06FF), (0x0750, 0x077F), (0x08A0, 0x08FF), (0xFB50, 0xFDFF), (0xFE70, 0xFEFF))),
147
+ ("he", ((0x0590, 0x05FF), (0xFB1D, 0xFB4F))),
148
+ ("hi", ((0x0900, 0x097F), (0xA8E0, 0xA8FF))),
149
+ ("bn", ((0x0980, 0x09FF),)),
150
+ ("ta", ((0x0B80, 0x0BFF),)),
151
+ ("th", ((0x0E00, 0x0E7F),)),
152
+ ("lo", ((0x0E80, 0x0EFF),)),
153
+ ("ru", ((0x0400, 0x04FF), (0x0500, 0x052F), (0x2DE0, 0x2DFF))),
154
+ ("el", ((0x0370, 0x03FF), (0x1F00, 0x1FFF))),
155
+ )
156
+
157
+
158
+ # --------------------------------------------------------------------------- emoji (1.15)
159
+ # Emoji are orthogonal to the writing system: "やった 🎉" is Japanese AND emoji. They are detected
160
+ # separately from detect_script() so a cue's font resolution is still decided by its letters.
161
+ EMOJI_RANGES = (
162
+ (0x1F300, 0x1FAFF), # symbols & pictographs, supplemental, extended-A
163
+ (0x1F000, 0x1F0FF), # mahjong/domino/playing cards
164
+ (0x2600, 0x27BF), # misc symbols + dingbats
165
+ (0x2B00, 0x2BFF), # misc symbols and arrows
166
+ (0xFE0F, 0xFE0F), # VS16 (emoji presentation selector)
167
+ (0x1F1E6, 0x1F1FF), # regional indicators (flags)
168
+ (0x20E3, 0x20E3), # combining enclosing keycap
169
+ (0x1F3FB, 0x1F3FF), # skin-tone modifiers
170
+ )
171
+
172
+
173
+ # U+200D ZWJ is deliberately NOT in EMOJI_RANGES: it is ordinary Indic/Persian orthography
174
+ # (क्‍ष is ka + virama + ZWJ + ssa) and only becomes emoji glue *between two emoji bases*.
175
+ # Characters that never START a cluster: they bind to whatever stands before them.
176
+ _EMOJI_TAIL = frozenset({0x200D, 0xFE0F, 0x20E3} | set(range(0x1F3FB, 0x1F400)))
177
+
178
+
179
+ _EMOJI_REGIONAL = range(0x1F1E6, 0x1F200)
180
+
181
+
182
+ _ZWJ = 0x200D
183
+
184
+
185
+ _VS15 = 0xFE0E # text-presentation selector: "draw this as a character, not as an emoji"
186
+
187
+
188
+ _VS16 = 0xFE0F
189
+
190
+
191
+ _KEYCAP = 0x20E3
192
+
193
+
194
+ _KEYCAP_BASES = frozenset("0123456789#*")
195
+
196
+
197
+ def _is_emoji_char(ch: str) -> bool:
198
+ cp = ord(ch)
199
+ return any(lo <= cp <= hi for lo, hi in EMOJI_RANGES)
200
+
201
+
202
+ def _is_emoji_base(ch: str) -> bool:
203
+ """Can this character START an emoji cluster? Pictographs and regional indicators can;
204
+ the joiners and modifiers (ZWJ, VS16, keycap, skin tone) never can -- they only bind to an
205
+ emoji base that already stands before them. Without this, a ZWJ or a VS16 sitting after an
206
+ ordinary letter turned that letter into "an emoji" and the PNG route replaced it with a gap."""
207
+ return ord(ch) not in _EMOJI_TAIL and _is_emoji_char(ch)
208
+
209
+
210
+ def emoji_clusters(text: str) -> "List[Tuple[int, str]]":
211
+ """(index in `text`, cluster) for every emoji in it, ZWJ sequences, VS16, keycaps, flag pairs
212
+ and skin-tone modifiers kept together -- 👩‍💻 is one cluster, not three, and 1️⃣ starts at the
213
+ digit even though the digit is not itself an emoji character.
214
+
215
+ A cluster can only START at an emoji base (a pictograph, a regional indicator) or at a keycap
216
+ base (`0-9 # *`) that is actually followed by U+20E3. A ZWJ is glue *inside* a cluster, never
217
+ a starter and never a tail on its own: `क्‍ष` (Hindi ka + virama + ZWJ + ssa) and `abc‍def`
218
+ contain no emoji. A base explicitly marked with U+FE0E (VS15, text presentation) is likewise
219
+ not an emoji -- the author asked for the character, not the picture.
220
+ """
221
+ out: "List[Tuple[int, str]]" = []
222
+ i = 0
223
+ n = len(text or "")
224
+ while i < n:
225
+ ch = text[i]
226
+ start = i
227
+ if _is_emoji_base(ch):
228
+ j = i + 1
229
+ if j < n and ord(text[j]) == _VS15: # text presentation requested: not an emoji
230
+ i = j + 1
231
+ continue
232
+ elif ch in _KEYCAP_BASES:
233
+ j = i + 1
234
+ if j < n and ord(text[j]) == _VS16:
235
+ j += 1
236
+ if not (j < n and ord(text[j]) == _KEYCAP):
237
+ i += 1
238
+ continue
239
+ j += 1
240
+ else:
241
+ i += 1
242
+ continue
243
+ # extend: modifiers bind rightwards, a ZWJ only when a real emoji base follows it
244
+ while j < n:
245
+ cp = ord(text[j])
246
+ if cp in (_VS16, _KEYCAP) or 0x1F3FB <= cp <= 0x1F3FF:
247
+ j += 1
248
+ continue
249
+ if cp == _ZWJ and j + 1 < n and _is_emoji_base(text[j + 1]):
250
+ j += 2
251
+ continue
252
+ if (j == start + 1 and ord(ch) in _EMOJI_REGIONAL and cp in _EMOJI_REGIONAL):
253
+ j += 1
254
+ continue
255
+ break
256
+ out.append((start, text[start:j]))
257
+ i = j
258
+ return out
259
+
260
+
261
+ def has_emoji(text: str) -> bool:
262
+ return bool(emoji_clusters(text or ""))
263
+
264
+
265
+ def emoji_codepoint_name(cluster: str) -> str:
266
+ """The asset filename stem for a cluster: lowercase hex code points joined by '-', the
267
+ Twemoji/Noto convention (1f389, 1f469-200d-1f4bb, 1f1ef-1f1f5)."""
268
+ return "-".join(f"{ord(c):x}" for c in cluster)
269
+
270
+
271
+ def _emoji_name_candidates(cluster: str) -> "List[str]":
272
+ """Asset stems to try, most specific first: exact, without VS16, without skin tone, the ZWJ
273
+ sequence reduced to its first code point, the bare base."""
274
+ cps = [ord(c) for c in cluster]
275
+ names = [emoji_codepoint_name(cluster)]
276
+
277
+ def add(seq):
278
+ name = "-".join(f"{c:x}" for c in seq)
279
+ if name and name not in names:
280
+ names.append(name)
281
+ add([c for c in cps if c != 0xFE0F])
282
+ add([c for c in cps if c != 0xFE0F and not (0x1F3FB <= c <= 0x1F3FF)])
283
+ if 0x200D in cps:
284
+ add([cps[0]])
285
+ add([cps[0]])
286
+ return names
287
+
288
+
289
+ def emoji_asset_for(cluster: str, assets_dir: "Optional[str]") -> "Optional[str]":
290
+ """The PNG for `cluster` under `assets_dir`, or None when nothing matches."""
291
+ if not assets_dir or not os.path.isdir(assets_dir):
292
+ return None
293
+ for name in _emoji_name_candidates(cluster):
294
+ for ext in (".png", ".PNG"):
295
+ candidate = os.path.join(assets_dir, name + ext)
296
+ if os.path.isfile(candidate):
297
+ return candidate
298
+ return None
299
+
300
+
301
+ EMOJI_ASSET_HINT = (
302
+ "point --emoji-assets at a directory of PNGs named by code point (1f389.png): "
303
+ "twemoji/assets/72x72 (Twemoji, CC-BY 4.0) or noto-emoji/png/128 (Noto Emoji, OFL/Apache-2.0) "
304
+ "are the two people already have. The skill has no network at runtime, so the assets must "
305
+ "already exist on this machine -- nothing is ever downloaded")
306
+
307
+
308
+ _EMOJI_COLOR_FAMILIES = ("Noto Color Emoji", "Apple Color Emoji", "Segoe UI Emoji")
309
+
310
+
311
+ _EMOJI_SUPPORT_CACHE: "Dict[Tuple[Optional[str], bool], Dict[str, Any]]" = {}
312
+
313
+
314
+ def _emoji_color_font() -> "Tuple[Optional[str], Optional[str], bool]":
315
+ """(family, file, fontconfig_answered) for the first installed colour emoji family."""
316
+ exe = shutil.which("fc-list")
317
+ if not exe:
318
+ return None, None, False
319
+ for family in _EMOJI_COLOR_FAMILIES:
320
+ try:
321
+ proc = subprocess.run([exe, f":family={family}", "file"], stdout=subprocess.PIPE,
322
+ stderr=subprocess.DEVNULL, text=True, timeout=10)
323
+ except (subprocess.TimeoutExpired, OSError):
324
+ return None, None, False
325
+ if proc.returncode != 0:
326
+ return None, None, False
327
+ for line in proc.stdout.splitlines():
328
+ path = line.split(":", 1)[0].strip()
329
+ if path and os.path.exists(path):
330
+ return family, path, True
331
+ return None, None, True
332
+
333
+
334
+ def _libass_color_probe() -> "Optional[bool]":
335
+ """Does THIS ffmpeg render an emoji in colour through libass? Answered by a render, never by
336
+ the font listing: Noto Color Emoji installs happily on builds whose freetype/libass has no
337
+ colour-bitmap path at all, and those render a monochrome outline instead (measured). ~80 ms.
338
+ None means the probe could not be run (no ffmpeg, a failure) -- unknown, not false."""
339
+ exe = shutil.which("ffmpeg")
340
+ if not exe:
341
+ return None
342
+ import tempfile
343
+ with tempfile.TemporaryDirectory() as td:
344
+ srt = os.path.join(td, "e.srt")
345
+ with open(srt, "w", encoding="utf-8") as fh:
346
+ fh.write("1\n00:00:00,000 --> 00:00:01,000\n\U0001F389\n")
347
+ try:
348
+ proc = subprocess.run(
349
+ [exe, "-hide_banner", "-loglevel", "error", "-f", "lavfi",
350
+ "-i", "color=c=black:s=64x64:d=0.04",
351
+ "-vf", "subtitles=" + srt.replace("\\", "/"), "-frames:v", "1",
352
+ "-f", "rawvideo", "-pix_fmt", "rgb24", "-"],
353
+ stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, timeout=20)
354
+ except (subprocess.TimeoutExpired, OSError):
355
+ return None
356
+ if proc.returncode != 0 or len(proc.stdout) < 64 * 64 * 3:
357
+ return None
358
+ data = proc.stdout
359
+ for i in range(0, 64 * 64 * 3, 3):
360
+ r, g, b = data[i], data[i + 1], data[i + 2]
361
+ if max(r, g, b) - min(r, g, b) > 40:
362
+ return True
363
+ return False
364
+
365
+
366
+ def emoji_support(assets: "Optional[str]" = None, probe: bool = True) -> "Dict[str, Any]":
367
+ """What this machine can actually do with emoji, cached per process.
368
+
369
+ `mode` is `color` when a render probe proves libass draws colour, else `png` when an assets
370
+ directory resolves, else `mono` when some installed face has a glyph at all, else `none`.
371
+ An installed colour emoji font proves nothing on its own -- that is why `libass_color` comes
372
+ from a render (see references/gotchas.md#emoji). `probe=False` (`contract --json --static`, and every
373
+ static/JSON-only path) skips the render entirely and leaves `libass_color` unknown.
374
+ """
375
+ key = (assets or None, bool(probe))
376
+ if key in _EMOJI_SUPPORT_CACHE:
377
+ return dict(_EMOJI_SUPPORT_CACHE[key])
378
+ family, file, fc_answered = _emoji_color_font()
379
+ libass_color = _libass_color_probe() if probe else None
380
+ assets_dir = assets if (assets and os.path.isdir(assets)) else None
381
+ if libass_color:
382
+ mode = "color"
383
+ elif assets_dir:
384
+ mode = "png"
385
+ elif family:
386
+ mode = "mono"
387
+ elif not fc_answered:
388
+ # No fontconfig to ask (a static ffmpeg build, a bare container): the PNG path needs none,
389
+ # so the honest answer is png-or-none, never "none because fc-list is missing".
390
+ mode = "none"
391
+ else:
392
+ mode = "none"
393
+ if not fc_answered:
394
+ detail = "no fontconfig on this machine; the PNG overlay path needs none"
395
+ elif libass_color:
396
+ detail = f"{family or 'an installed face'} renders in colour through libass on this ffmpeg"
397
+ elif family and libass_color is False:
398
+ detail = f"{family} installed but libass renders it monochrome on this build"
399
+ elif family and libass_color is None:
400
+ detail = f"{family} installed; the colour render probe was not run"
401
+ elif assets_dir:
402
+ detail = "no colour emoji family installed; using the PNG assets directory"
403
+ else:
404
+ detail = "no colour emoji family installed and no --emoji-assets directory"
405
+ result = {"mode": mode, "color_font": family, "color_font_file": file,
406
+ "libass_color": libass_color, "assets": assets_dir,
407
+ "detail": detail, "fix": EMOJI_ASSET_HINT}
408
+ _EMOJI_SUPPORT_CACHE[key] = result
409
+ return dict(result)
410
+
411
+
412
+ def resolve_emoji_assets(flag: "Optional[str]" = None, project: "Optional[str]" = None,
413
+ brand: "Optional[dict]" = None) -> "Optional[str]":
414
+ """--emoji-assets DIR, else the project key, else brand.json, else FFMPEG_SKILL_EMOJI_ASSETS.
415
+ A directory that was named but does not exist is a failed job, never a silent downgrade."""
416
+ brand = brand or {}
417
+ styles = (brand.get("styles") or {}).get("caption") or {}
418
+ for value, where in ((flag, "--emoji-assets"), (project, "the project's text.emoji_assets"),
419
+ (styles.get("emoji_assets"), "brand.json styles.caption.emoji_assets"),
420
+ (brand.get("emoji_assets"), "brand.json emoji_assets"),
421
+ (os.environ.get("FFMPEG_SKILL_EMOJI_ASSETS"), "FFMPEG_SKILL_EMOJI_ASSETS")):
422
+ if not value:
423
+ continue
424
+ if not os.path.isdir(str(value)):
425
+ die(f"{where}: {value} is not a readable directory -- {EMOJI_ASSET_HINT}", kind="input")
426
+ return str(value)
427
+ return None
428
+
429
+
430
+ # --------------------------------------------------------------------------- text measurement (1.12)
431
+ # Moved here in 1.15 so graphics.py's ASS route and the emoji placement share caption.py's table.
432
+ # Average advance width per character, in em (a fraction of the font size). Proportional Latin text
433
+ # averages a bit over half an em; CJK and Thai are drawn on a full-width grid; Arabic/Hebrew and
434
+ # Devanagari sit in between. These are deliberately averages, not per-glyph metrics: measuring the
435
+ # real advance needs a font parser (no stdlib one) and would still be wrong for libass's own
436
+ # shaping, while a cue wrapped from an average is right to within a character on every line.
437
+ # (Latin is measured per character from LATIN_EM below, not from this average.)
438
+ ADVANCE_EM = {"ja": 1.0, "zh": 1.0, "ko": 1.0, "th": 1.0, "hi": 0.7, "ar": 0.6, "he": 0.6,
439
+ "ru": 0.55, "el": 0.55, "latin": 0.55}
440
+
441
+
442
+ # Scripts written without spaces: a line breaks between any two characters.
443
+ NO_SPACE_SCRIPTS = ("ja", "zh", "ko", "th")
444
+
445
+
446
+ # Per-character Latin advances in em, read off DejaVu Sans (the default caption family, and close
447
+ # enough to any other proportional sans for a wrap) and rounded UP: a capital runs 0.56-0.99 em
448
+ # against the single 0.55 average that used to stand for all of Latin, so an all-caps caption --
449
+ # the style most burn-ins use -- overflowed the safe area and was silently re-wrapped by libass
450
+ # past --max-lines. Rounding up is the safe direction: libass re-wraps a too-long line, it never
451
+ # un-wraps a short one. Characters outside the table fall back by class (0.7 uppercase/digit,
452
+ # 0.57 lowercase and anything else Latin-ish).
453
+ LATIN_EM = {
454
+ ' ': 0.32, '!': 0.41, '"': 0.46, '#': 0.84, '$': 0.64, '%': 0.96, '&': 0.78, "'": 0.28,
455
+ '(': 0.4, ')': 0.4, '*': 0.5, '+': 0.84, ',': 0.32, '-': 0.37, '.': 0.32, '/': 0.34, '0': 0.64,
456
+ '1': 0.64, '2': 0.64, '3': 0.64, '4': 0.64, '5': 0.64, '6': 0.64, '7': 0.64, '8': 0.64,
457
+ '9': 0.64, ':': 0.34, ';': 0.34, '<': 0.84, '=': 0.84, '>': 0.84, '?': 0.54, '@': 1.0,
458
+ 'A': 0.69, 'B': 0.69, 'C': 0.7, 'D': 0.78, 'E': 0.64, 'F': 0.58, 'G': 0.78, 'H': 0.76,
459
+ 'I': 0.3, 'J': 0.3, 'K': 0.66, 'L': 0.56, 'M': 0.87, 'N': 0.75, 'O': 0.79, 'P': 0.61,
460
+ 'Q': 0.79, 'R': 0.7, 'S': 0.64, 'T': 0.62, 'U': 0.74, 'V': 0.69, 'W': 0.99, 'X': 0.69,
461
+ 'Y': 0.62, 'Z': 0.69, '[': 0.4, '\\': 0.34, ']': 0.4, '^': 0.84, '_': 0.5, '`': 0.5, 'a': 0.62,
462
+ 'b': 0.64, 'c': 0.55, 'd': 0.64, 'e': 0.62, 'f': 0.36, 'g': 0.64, 'h': 0.64, 'i': 0.28,
463
+ 'j': 0.28, 'k': 0.58, 'l': 0.28, 'm': 0.98, 'n': 0.64, 'o': 0.62, 'p': 0.64, 'q': 0.64,
464
+ 'r': 0.42, 's': 0.53, 't': 0.4, 'u': 0.64, 'v': 0.6, 'w': 0.82, 'x': 0.6, 'y': 0.6, 'z': 0.53,
465
+ '{': 0.64, '|': 0.34, '}': 0.64, '~': 0.84
466
+ }
467
+
468
+
469
+ # Thai and Lao write some vowels BEFORE the consonant they belong to: the break must not land
470
+ # between them and the base that follows.
471
+ LEADING_VOWELS = set(range(0x0E40, 0x0E45)) | set(range(0x0EC0, 0x0EC5))
472
+
473
+
474
+ def _is_mark(ch: str) -> bool:
475
+ """A character that hangs off the one before it: a combining mark (any script) or one of the
476
+ Thai/Lao vowel signs and tone marks, which are Mn/Mc but carry no combining class."""
477
+ return unicodedata.combining(ch) != 0 or unicodedata.category(ch) in ("Mn", "Mc")
478
+
479
+
480
+ def _char_em(ch: str) -> float:
481
+ # CJK punctuation and the fullwidth forms (、。,!? and U+FF01-FF60) are drawn on the same
482
+ # full-width grid as the ideographs they sit between, even though they are not "Han" to a
483
+ # script detector -- measuring them as Latin under-counts a wrapped CJK line by a character.
484
+ cp = ord(ch)
485
+ # A combining mark is drawn on top of (or under) its base and advances the pen by nothing:
486
+ # charging it a full em wrapped Thai and Devanagari lines far shorter than they needed to be.
487
+ if unicodedata.combining(ch) != 0 or unicodedata.category(ch) in ("Mn", "Cf"):
488
+ # "Cf" catches ZWJ/ZWNJ: an Indic joiner is orthography, and it advances the pen by
489
+ # nothing -- charging it a full em (it used to count as "emoji") shrank a Hindi line.
490
+ return 0.0
491
+ if 0x3000 <= cp <= 0x303F or 0xFF01 <= cp <= 0xFF60 or 0xFFE0 <= cp <= 0xFFE6:
492
+ return 1.0
493
+ script = char_script(ch)
494
+ if script == "emoji":
495
+ # 1.15: an emoji is drawn (or reserved) at a full em box, not at Latin's 0.57 -- counting
496
+ # it as Latin overflowed the safe area on an emoji-heavy line.
497
+ return 1.0
498
+ if script == "latin":
499
+ if ch in LATIN_EM:
500
+ return LATIN_EM[ch]
501
+ if ch.isupper() or ch.isdigit():
502
+ return 0.7
503
+ return 0.57
504
+ return ADVANCE_EM.get(script, 0.55)
505
+
506
+
507
+ def text_width_em(text: str, emoji_em: float = 1.0) -> float:
508
+ """Width of `text` in em, from the per-script average advance table. `emoji_em` is what one
509
+ emoji cluster costs (--emoji-scale), so a wrap counts the box that will actually be drawn."""
510
+ total = 0.0
511
+ spans = {i: len(c) for i, c in emoji_clusters(text)}
512
+ i = 0
513
+ while i < len(text):
514
+ if i in spans:
515
+ total += emoji_em
516
+ i += spans[i]
517
+ continue
518
+ total += _char_em(text[i])
519
+ i += 1
520
+ return total
521
+
522
+
523
+ def emoji_filter_chain(plan, base_label, out_label, first_input=1):
524
+ """(chains, inputs) that composite the planned PNGs on top of `base_label`.
525
+
526
+ `inputs` is a list of argv fragments, each ending in the asset path, to be appended to the
527
+ ffmpeg command in order (an overlay that fades needs `-loop 1` on its input so the still has
528
+ a timeline the fade filter can move along; one that does not is a plain `-i`).
529
+ """
530
+ overlays = plan.get("overlays") or []
531
+ if not overlays:
532
+ return [], []
533
+ # Group by everything that makes two uses of the same PNG a different STREAM: the fade is
534
+ # expressed in the cue's own timeline, so two cues cannot share one faded input.
535
+ def _key(o):
536
+ fades = (round(float(o.get("fade_in") or 0.0), 3), round(float(o.get("fade_out") or 0.0), 3))
537
+ window = (round(float(o["start"]), 3), round(float(o["end"]), 3)) if any(fades) else (None, None)
538
+ return (o["asset"], o["box"]) + fades + window
539
+
540
+ groups: "List[Tuple]" = []
541
+ for o in overlays:
542
+ if _key(o) not in groups:
543
+ groups.append(_key(o))
544
+ chains: List[str] = []
545
+ inputs: "List[List[str]]" = []
546
+ pads: "Dict[Tuple, List[str]]" = {}
547
+ for k, key in enumerate(groups):
548
+ asset, box, fin, fout, gstart, gend = key
549
+ uses = [o for o in overlays if _key(o) == key]
550
+ idx = first_input + k
551
+ labels = [f"e{k}_{j}" for j in range(len(uses))]
552
+ chain = f"[{idx}:v]format=rgba,scale={box}:{box}"
553
+ if fin or fout:
554
+ # -loop 1 gives the still an advancing timeline on the SAME clock as the main video,
555
+ # so the fade times below are the cue's own seconds. The emoji then appears and
556
+ # leaves with the text instead of popping in against a fading line.
557
+ # -t bounds the loop at the cue's end: an unbounded looped still never EOFs and the
558
+ # whole encode hangs (overlay keeps pulling from it after the main video is done).
559
+ inputs.append(["-loop", "1", "-t", f"{gend:.3f}", "-i", asset])
560
+ if fin:
561
+ chain += f",fade=t=in:st={gstart:.3f}:d={fin:.3f}:alpha=1"
562
+ if fout:
563
+ chain += f",fade=t=out:st={max(gstart, gend - fout):.3f}:d={fout:.3f}:alpha=1"
564
+ else:
565
+ inputs.append(["-i", asset])
566
+ if len(labels) > 1:
567
+ chain += f",split={len(labels)}"
568
+ chains.append(chain + "".join(f"[{l}]" for l in labels))
569
+ pads[key] = labels
570
+ cur = base_label
571
+ remaining = {key: list(v) for key, v in pads.items()}
572
+ for j, o in enumerate(overlays):
573
+ label = remaining[_key(o)].pop(0)
574
+ nxt = out_label if j == len(overlays) - 1 else f"eov{j}"
575
+ x = o["x"]
576
+ x = f"'{x}'" if isinstance(x, str) else x
577
+ # No eof_action=pass here: a PNG input is a SINGLE frame at pts 0, and eof_action=pass
578
+ # switches off overlay's default "hold the last frame of the secondary input", so the
579
+ # asset would be composited on frame 0 only and vanish for the rest of the cue (that is
580
+ # exactly what shipped first). eof_action=repeat (the default) holds the still for the
581
+ # whole timeline; enable= is what confines it to the cue's window.
582
+ chains.append(f"[{cur}][{label}]overlay=x={x}:y={o['y']}:"
583
+ f"enable='between(t,{o['start']:.3f},{o['end']:.3f})'[{nxt}]")
584
+ cur = nxt
585
+ return chains, inputs
586
+
587
+
588
+ # --------------------------------------------------------------------------- shaping (1.15)
589
+ # Scripts whose correct rendering needs harfbuzz-class reordering and re-clustering (Indic matras,
590
+ # Thai/Lao mark stacking). drawtext does NOT use harfbuzz even in an --enable-libharfbuzz build, so
591
+ # these come out wrong through drawtext on every build and must go through libass. Arabic and
592
+ # Hebrew are NOT here: drawtext's text_shaping uses fribidi, which does bidi and Arabic joining
593
+ # correctly -- they only join this set on a build compiled without fribidi.
594
+ SHAPING_SCRIPTS = frozenset({"hi", "bn", "ta", "te", "kn", "ml", "gu", "pa", "si", "th", "lo", "km", "my"})
595
+
596
+
597
+ BIDI_SCRIPTS = frozenset({"ar", "he"})
598
+
599
+
600
+ _SHAPING_BUILD_CACHE: "Dict[str, bool]" = {}
601
+
602
+
603
+ def drawtext_shaping() -> "Dict[str, bool]":
604
+ """Which shaping libraries THIS ffmpeg was built with, from -buildconf (falling back to the
605
+ `configuration:` line of -version). Cached per process."""
606
+ if _SHAPING_BUILD_CACHE:
607
+ return dict(_SHAPING_BUILD_CACHE)
608
+ text = ""
609
+ exe = shutil.which("ffmpeg")
610
+ if exe:
611
+ for flag in ("-buildconf", "-version"):
612
+ try:
613
+ proc = subprocess.run([exe, "-hide_banner", flag], stdout=subprocess.PIPE,
614
+ stderr=subprocess.STDOUT, text=True, timeout=10)
615
+ except (subprocess.TimeoutExpired, OSError):
616
+ break
617
+ if proc.returncode == 0 and proc.stdout.strip():
618
+ text = proc.stdout
619
+ break
620
+ _SHAPING_BUILD_CACHE.update({"fribidi": "--enable-libfribidi" in text,
621
+ "harfbuzz": "--enable-libharfbuzz" in text})
622
+ return dict(_SHAPING_BUILD_CACHE)
623
+
624
+
625
+ def needs_shaping(script: str) -> bool:
626
+ """Whether drawtext would render `script` wrongly on this build."""
627
+ if script in SHAPING_SCRIPTS:
628
+ return True
629
+ return script in BIDI_SCRIPTS and not drawtext_shaping()["fribidi"]
630
+
631
+
632
+ def font_family_of_file(path: str) -> "Optional[str]":
633
+ """The family name of a font FILE -- what libass wants, given a --font-file. `fc-scan` reads
634
+ the file directly; without fontconfig the file stem is the honest best guess."""
635
+ if not path or not os.path.isfile(path):
636
+ return None
637
+ exe = shutil.which("fc-scan")
638
+ if exe:
639
+ try:
640
+ proc = subprocess.run([exe, "--format", "%{family[0]}", path], stdout=subprocess.PIPE,
641
+ stderr=subprocess.DEVNULL, text=True, timeout=10)
642
+ if proc.returncode == 0 and proc.stdout.strip():
643
+ return proc.stdout.strip().splitlines()[0].strip()
644
+ except (subprocess.TimeoutExpired, OSError):
645
+ pass
646
+ return Path(path).stem
647
+
648
+
649
+ def char_script(ch: str) -> str:
650
+ """The script of one character: one of SCRIPTS, or "latin" for anything else (including
651
+ digits, punctuation and spaces -- they are measured and wrapped like Latin)."""
652
+ cp = ord(ch)
653
+ # 1.15: an emoji cluster is not Latin. detect_script() skips "emoji" the way it skips "latin",
654
+ # so font resolution still follows the letters around it.
655
+ if _is_emoji_char(ch):
656
+ return "emoji"
657
+ for name, ranges in _SCRIPT_RANGES:
658
+ for lo, hi in ranges:
659
+ if lo <= cp <= hi:
660
+ return "ja" if name == "kana" else ("zh" if name == "han" else name)
661
+ return "latin"
662
+
663
+
664
+ def detect_script(text: str, lang: "Optional[str]" = None) -> str:
665
+ """Which script `text` is written in, as one of SCRIPTS.
666
+
667
+ Hangul wins for Korean, any kana makes the whole string Japanese (Japanese mixes kana and
668
+ Han), Han alone is Chinese. Mixed text is decided by character count: the non-Latin script
669
+ with the most characters wins, ties going to whichever appeared first, and text with no
670
+ non-Latin characters at all is "latin". `lang` (a --lang/--language hint, or brand.json's
671
+ `lang`) only resolves the one ambiguity the characters genuinely cannot: Han with no kana
672
+ is Chinese by default but Japanese (or Korean hanja) when the caller says so.
673
+ """
674
+ counts: "Dict[str, int]" = {}
675
+ order: "List[str]" = []
676
+ kana = 0
677
+ for ch in text or "":
678
+ s = char_script(ch)
679
+ if s in ("latin", "emoji"):
680
+ continue
681
+ if ord(ch) in range(0x3040, 0x3100) or ord(ch) in range(0x31F0, 0x3200) or ord(ch) in range(0xFF66, 0xFFA0):
682
+ kana += 1
683
+ if s not in counts:
684
+ order.append(s)
685
+ counts[s] = counts.get(s, 0) + 1
686
+ if kana: # Japanese: the Han characters in the same string are Japanese too
687
+ counts["ja"] = counts.pop("ja", 0) + counts.pop("zh", 0)
688
+ order = [s for s in order if s != "zh"]
689
+ if not counts:
690
+ return "latin"
691
+ best = max(counts, key=lambda s: (counts[s], -order.index(s)))
692
+ hint = (lang or "").strip().lower().replace("_", "-").split("-")[0]
693
+ if not kana and best == "zh" and hint in ("ja", "zh", "ko"):
694
+ return hint # Han-only text: only the caller knows whether it is Chinese, Japanese or hanja
695
+ return best
696
+
697
+
698
+ _SCRIPT_FONT_CACHE: "Dict[Tuple[str, Optional[str]], Optional[Tuple[str, str]]]" = {}
699
+
700
+
701
+ def _fc_list_fonts(fc_lang: str) -> "Optional[List[Tuple[str, List[str]]]]":
702
+ """(file, families) for every font fontconfig says covers `fc_lang`.
703
+
704
+ `[]` means fontconfig answered and nothing covers the language; `None` means it could not be
705
+ asked at all (no `fc-list` on PATH, or it failed/timed out) -- the difference between
706
+ "missing" and "unknown", which the caller must not collapse: unknown is not a refusal.
707
+ """
708
+ exe = shutil.which("fc-list")
709
+ if not exe:
710
+ return None
711
+ try:
712
+ proc = subprocess.run([exe, f":lang={fc_lang}", "file", "family"],
713
+ stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True, timeout=10)
714
+ except (subprocess.TimeoutExpired, OSError):
715
+ return None
716
+ if proc.returncode != 0:
717
+ return None
718
+ out = []
719
+ for line in proc.stdout.splitlines():
720
+ if ": " not in line:
721
+ continue
722
+ path, _, families = line.partition(": ")
723
+ path = path.strip()
724
+ if not path or not os.path.exists(path):
725
+ continue
726
+ names = [f.replace("\\-", "-").strip() for f in families.split(",") if f.strip()]
727
+ out.append((path, names or [Path(path).stem]))
728
+ return out
729
+
730
+
731
+ def _family_rank(families: "Sequence[str]", preferred: "Sequence[str]") -> int:
732
+ for i, want in enumerate(preferred):
733
+ w = want.lower()
734
+ if any(f.lower().startswith(w) for f in families):
735
+ return i
736
+ return len(preferred)
737
+
738
+
739
+ def _script_font_entry(script: str, family_hint: "Optional[str]" = None) -> "Optional[Tuple[str, str]]":
740
+ key = (script, family_hint)
741
+ if key in _SCRIPT_FONT_CACHE:
742
+ return _SCRIPT_FONT_CACHE[key]
743
+ _SCRIPT_FONT_CACHE[key] = result = _script_font_uncached(script, family_hint)
744
+ return result
745
+
746
+
747
+ FC_UNKNOWN = "unknown" # sentinel: fontconfig could not be asked (absent or failing), not "no font"
748
+
749
+
750
+ def _script_font_uncached(script: str, family_hint: "Optional[str]" = None):
751
+ """(file, family), None when nothing covers `script`, or FC_UNKNOWN when it cannot be asked."""
752
+ if script not in FC_LANG:
753
+ return None
754
+ if platform.system() == "Windows":
755
+ fonts = Path(os.environ.get("WINDIR", "C:\\Windows")) / "Fonts"
756
+ for name, family in WINDOWS_FONTS.get(script, []):
757
+ if (fonts / name).exists():
758
+ return str(fonts / name), family
759
+ return None
760
+ preferred = list(PREFERRED_FAMILIES.get(script, []))
761
+ if family_hint:
762
+ preferred.insert(0, family_hint)
763
+ candidates = _fc_list_fonts(FC_LANG[script])
764
+ if candidates is None:
765
+ return FC_UNKNOWN
766
+ if not candidates:
767
+ return None
768
+ scored = []
769
+ for path, families in candidates:
770
+ joined = " ".join(families).lower()
771
+ stem = Path(path).stem.lower()
772
+ # "Unifont Sample" is fontconfig's tofu-with-hex-digits fallback: it "covers" every script
773
+ # by drawing the code point, which is exactly the unreadable result this feature exists to
774
+ # avoid -- it is only ever chosen when nothing else covers the script at all. A *Mono* face
775
+ # is legible but wrong for a caption band, so it sorts after every proportional one.
776
+ last_resort = 1 if "unifont" in joined else 0
777
+ mono = 1 if "mono" in joined else 0
778
+ # regular weights before Bold/Italic/Oblique cuts, so a default caption is not bold by accident
779
+ styled = 1 if any(k in stem for k in ("bold", "italic", "oblique", "light", "thin", "black")) else 0
780
+ scored.append((last_resort, _family_rank(families, preferred), mono, styled, path, families[0]))
781
+ scored.sort(key=lambda row: (row[0], row[1], row[2], row[3], row[4]))
782
+ best = scored[0]
783
+ return best[4], best[5]
784
+
785
+
786
+ def font_for_script(script: str, family_hint: "Optional[str]" = None) -> "Optional[str]":
787
+ """A font FILE path that covers `script`, or None when this machine has none.
788
+
789
+ Linux/macOS ask fontconfig (`fc-list :lang=xx file family`) and rank what it reports by the
790
+ PREFERRED_FAMILIES table; Windows has no fontconfig, so the known system files are probed by
791
+ name. Cached per process: a caption job resolves the same script for every cue.
792
+ """
793
+ entry = _script_font_entry(script, family_hint)
794
+ return entry[0] if entry and entry is not FC_UNKNOWN else None
795
+
796
+
797
+ def font_family_for_script(script: str, family_hint: "Optional[str]" = None) -> "Optional[str]":
798
+ """The family NAME of font_for_script()'s file -- what libass wants in an ASS Fontname."""
799
+ entry = _script_font_entry(script, family_hint)
800
+ return entry[1] if entry and entry is not FC_UNKNOWN else None
801
+
802
+
803
+ def script_font_status(script: str) -> str:
804
+ """"available" (a font file covers `script`), "missing" (fontconfig answered, none does) or
805
+ "unknown" (there is no working fontconfig to ask). Only "missing" is a refusal."""
806
+ entry = _script_font_entry(script)
807
+ if entry is FC_UNKNOWN:
808
+ return "unknown"
809
+ return "available" if entry else "missing"
810
+
811
+
812
+ def font_covers_script(font_name: str, script: str) -> bool:
813
+ """Whether the installed family `font_name` actually carries glyphs for `script`.
814
+
815
+ `fc-match` cannot answer this: given a family that IS installed it returns that family
816
+ whatever `:lang=` asks for (verified -- `fc-match "DejaVu Sans:lang=zh-cn"` answers
817
+ "DejaVu Sans", which has no Han glyphs at all). `fc-list :lang=xx:family=<name>` does: it
818
+ lists only files that satisfy BOTH, so an empty listing is the proof of no coverage. Unknown
819
+ (no fontconfig at all) counts as covering: a warning nobody can verify is worse than none.
820
+ """
821
+ if script not in FC_LANG or not font_name:
822
+ return True
823
+ exe = shutil.which("fc-list")
824
+ if not exe:
825
+ return True
826
+ try:
827
+ proc = subprocess.run([exe, f":lang={FC_LANG[script]}:family={font_name}", "file"],
828
+ stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True, timeout=10)
829
+ except (subprocess.TimeoutExpired, OSError):
830
+ return True
831
+ if proc.returncode != 0:
832
+ return True
833
+ return bool(proc.stdout.strip())
834
+
835
+
836
+ # Named flags differ per tool: overlay.py and graphics.py take a font FILE, caption.py takes a
837
+ # directory of faces plus the family name -- naming a flag the tool does not have is worse than
838
+ # naming none, so the hint says both (review 10).
839
+ FONT_FLAG_HINT = "pass a font file (--font-file on overlay.py/graphics.py, --fonts-dir with --font on caption.py)"
840
+
841
+
842
+ FONT_INSTALL_HINT = ("install fonts-noto-cjk / fonts-noto-core (apt), "
843
+ "brew install --cask font-noto-sans-cjk / font-noto-sans-arabic (mac), or "
844
+ + FONT_FLAG_HINT)
845
+
846
+
847
+ def fonts_dir_covers_script(fonts_dir: str, script: str) -> "Optional[bool]":
848
+ """Does any font under `fonts_dir` cover `script`? None when it cannot be checked.
849
+
850
+ `--fonts-dir` says "also look here", not "this exact face", so it must not switch the
851
+ coverage guarantee off. fontconfig's `fc-scan` reads the files directly (no cache, no
852
+ installed-font database), which is exactly the question: `%{lang}` lists the languages each
853
+ face claims.
854
+ """
855
+ if script not in FC_LANG or not fonts_dir or not os.path.isdir(fonts_dir):
856
+ return None
857
+ exe = shutil.which("fc-scan")
858
+ if not exe:
859
+ return None
860
+ try:
861
+ proc = subprocess.run([exe, "--format", "%{lang}\n", fonts_dir],
862
+ stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True, timeout=10)
863
+ except (subprocess.TimeoutExpired, OSError):
864
+ return None
865
+ if proc.returncode != 0:
866
+ return None
867
+ want = FC_LANG[script].lower()
868
+ for line in proc.stdout.splitlines():
869
+ if want in [tag.strip().lower() for tag in line.split("|")]:
870
+ return True
871
+ return False
872
+
873
+
874
+ def script_font_for_text(text: str, *, lang: "Optional[str]" = None, font: "Optional[str]" = None,
875
+ font_explicit: bool = False, font_file: "Optional[str]" = None,
876
+ fonts_dir: "Optional[str]" = None
877
+ ) -> "Tuple[str, Optional[str], Optional[str]]":
878
+ """(script, font file, family) to draw `text` with, resolving by script when nothing explicit
879
+ was asked for.
880
+
881
+ Returns (script, None, None) when the caller's own choice stands: Latin text, an explicit
882
+ --font-file, an explicit --font (which is kept even when fontconfig says it does not cover
883
+ the script -- with one info line saying so, because overriding a user's stated font silently
884
+ is worse than a warning), or a --fonts-dir that does carry the script. Otherwise the resolved
885
+ file is returned with ONE info line naming it.
886
+
887
+ A script fontconfig says nothing covers is a failed job (tofu is not a delivery). A machine
888
+ with no working fontconfig at all answers "unknown", not "missing": the job continues with
889
+ the caller's font -- libass and drawtext still have their own font backends -- and one info
890
+ line says the coverage could not be verified.
891
+ """
892
+ script = detect_script(text or "", lang)
893
+ if script == "latin":
894
+ return script, None, None
895
+ if font_file:
896
+ return script, None, None
897
+ if font_explicit and font:
898
+ if not font_covers_script(font, script):
899
+ info(f"font: '{font}' does not cover {LANGUAGE_NAMES[script]} text on this machine; keeping it as asked "
900
+ f"(drop --font, or {FONT_FLAG_HINT}, to pick one by script automatically)")
901
+ return script, None, None
902
+ if fonts_dir:
903
+ covered = fonts_dir_covers_script(fonts_dir, script)
904
+ if covered:
905
+ return script, None, None
906
+ if covered is None:
907
+ info(f"font: could not verify that {fonts_dir} covers {LANGUAGE_NAMES[script]} text "
908
+ "(no fc-scan on this machine); using it as given")
909
+ return script, None, None
910
+ info(f"font: no face in {fonts_dir} covers {LANGUAGE_NAMES[script]} text; "
911
+ "picking one by script instead (the directory is still searched first)")
912
+ entry = _script_font_entry(script)
913
+ if entry is FC_UNKNOWN:
914
+ info(f"font: could not verify that this machine can render {LANGUAGE_NAMES[script]} text "
915
+ "(no working fontconfig); rendering with the font as given -- "
916
+ "doctor --json .fonts.scripts reports what is known")
917
+ return script, None, None
918
+ if not entry:
919
+ die(f"no installed font covers {LANGUAGE_NAMES[script]} text on this machine — {FONT_INSTALL_HINT}", kind="input")
920
+ info(f"font: {entry[0]} (covers {script})")
921
+ return script, entry[0], entry[1]
922
+
923
+
924
+ def escape_drawtext(text: str) -> str:
925
+ """Escape a FONT NAME for a single-quoted drawtext option value (`font='<this>'`).
926
+
927
+ Since 1.15 this is no longer the route for drawn TEXT -- use drawtext_text_opts(), which puts
928
+ the text in a file and keeps `\'` and `%` verbatim. It remains the escape for the font-name
929
+ fallback, where the value is a family name that never legitimately contains a quote or a
930
+ percent sign.
931
+
932
+ Every ffmpeg filter-graph special character (`\\ : % , [ ] ;`) needs a backslash escape
933
+ regardless of the surrounding quotes -- the graph parser still splits on an unescaped `,`/`;`
934
+ or ends an option list on an unescaped `:`/`[`/`]` even while "inside" a quoted value. The
935
+ quote character itself has no reliable backslash escape at all: `\\'` and the POSIX shell
936
+ close-insert-reopen trick both parse fine in a simple `-vf` chain but silently corrupt a
937
+ `-filter_complex` chain that uses explicit `[label]` pads (confirmed by rendering the result:
938
+ trailing option names leak into the picture as literal text). `%` has the same problem as far
939
+ as drawtext's own expansion scanner is concerned. Both are therefore dropped here rather than
940
+ escaped -- which is exactly why drawn text no longer comes through this function.
941
+ """
942
+ text = re.sub(r"[\x00-\x1f\x7f]", "", text)
943
+ return (
944
+ text.replace("'", "")
945
+ .replace("%", "")
946
+ .replace("\\", "\\\\")
947
+ .replace(":", "\\:")
948
+ .replace(",", "\\,")
949
+ .replace("[", "\\[")
950
+ .replace("]", "\\]")
951
+ .replace(";", "\\;")
952
+ )
953
+
954
+
955
+ def drawtext_text_opts(text: str, tmpdir: "Optional[str]" = None) -> str:
956
+ """`textfile=<path>:expansion=none` for drawtext -- the one route that is provably safe for
957
+ every character on every build shape this repo uses.
958
+
959
+ The filter-graph parser never sees the text at all: only the PATH is parsed, and
960
+ escape_filter_path() already handles that. `expansion=none` switches off drawtext's own
961
+ `%{...}` scanner, which is the reason `%` was unsafe (a bare `\%` logs "Stray %" on one build
962
+ and fails the whole filter chain on another). With the scanner off, `'`, `%`, `:`, `,`, `[`,
963
+ `]`, `;` and `\` all reach the picture verbatim -- 1.15 fixes `overlay.py --text "it's 100%
964
+ done"` losing both characters. Control characters are still stripped: a one-line burnt-in
965
+ label has no use for them.
966
+
967
+ The file is UTF-8, mode 0600, in a private per-run directory (see _drawtext_tmpdir) that is
968
+ removed when the process ends. It is *registered* here and written by run() only if the
969
+ command about to run actually names it, so --dry-run and the ASS route write nothing; a
970
+ printed plan therefore names a path that no longer exists once the run is over, which is the
971
+ same promise every other temp file in this skill makes.
972
+ """
973
+ cleaned = re.sub(r"[\x00-\x1f\x7f]", "", text or "")
974
+ import hashlib
975
+ name = "t_" + hashlib.sha256(cleaned.encode("utf-8")).hexdigest()[:16] + ".txt"
976
+ if tmpdir is None:
977
+ tmpdir = _drawtext_tmpdir(create=not STATE.dry_run)
978
+ path = os.path.join(tmpdir, name)
979
+ _DRAWTEXT_PENDING[path] = cleaned
980
+ return f"textfile={escape_filter_path(path)}:expansion=none"
981
+
982
+
983
+ # --------------------------------------------------------------- caption line breaking (1.16)
984
+ # Lifted out of caption.py in 1.16.0 so graphics.py can wrap the same way (caption.py keeps the
985
+ # names it exported, re-imported from here). The whole breaker is pure: a string in, a list of
986
+ # lines out, no subprocess and no probe, which is what makes the eval regression corpus cheap
987
+ # to lock down in unit tests.
988
+
989
+ # How much of the frame width a caption line may use. libass's own default SRT margins are 10 of a
990
+ # 384-wide script (2.6 % a side); 5 % a side is the safe area every platform check in this repo uses.
991
+ SAFE_WIDTH_FRACTION = 0.9
992
+ # ORPHAN_MIN_EM: one full-width CJK/Thai character plus a hair. A last line narrower than this is a
993
+ # single stranded character -- eval 14's th1 (a lone 'ล') and dl3 (a lone '行').
994
+ ORPHAN_MIN_EM = 1.1
995
+
996
+ WRAP_MODES = ("phrase", "measured")
997
+
998
+ # R3's Japanese preference table. These are *preferences* applied only among positions that
999
+ # already fit the line, so the table can never make a line too wide or change the line count.
1000
+ #
1001
+ # JA_PARTICLES is a "do not strand at the start of a line" table, which is the direction kinsoku
1002
+ # practice actually goes: a particle is enclitic -- it attaches to the word BEFORE it and marks
1003
+ # that word's role -- so a line beginning with は or が reads as a fragment torn off its phrase.
1004
+ # A break AFTER a particle is therefore preferred (the particle stays with what it marks) and a
1005
+ # break BEFORE one is forbidden. The list is the eight case/topic particles named in the 1.16.0
1006
+ # task brief (は が を に で と の へ) plus も や から まで より, which a reader of Japanese would
1007
+ # add for the same reason. It is a judgement call with no upstream source; treat it as tunable
1008
+ # data, not as grammar.
1009
+ JA_PARTICLES = "はがをにでとのへもや" # a break AFTER one of these is preferred, BEFORE one forbidden
1010
+ # The multi-character members of the same table. They are matched as whole strings against the
1011
+ # text on each side of a candidate break -- putting them in the character string above turned
1012
+ # か, ら, ま, で, よ and り into one-character particles of their own, which none of them is.
1013
+ JA_PARTICLE_WORDS = ("から", "まで", "より")
1014
+ JA_SENTENCE_END = "。、!?」』)" # a break AFTER one of these is preferred
1015
+ # Characters that may never start a line: small kana, the prolonged sound mark, closing brackets
1016
+ # and the Japanese punctuation that hangs on the end of the line before it.
1017
+ JA_NO_LINE_START = "ぁぃぅぇぉっゃゅょァィゥェォッャュョーヽヾゝゞ、。!?)」』】〕》’”%"
1018
+ JA_NO_LINE_END = "(「『【〔《‘“" # ... and the ones that may never end a line
1019
+
1020
+ # R4. Function words belong to the phrase that FOLLOWS them: an article or preposition begins the
1021
+ # noun phrase it governs, so a break before one is the good break (the word opens the next line
1022
+ # with its phrase) and a break after one is the bad break (it is stranded at the end of a line,
1023
+ # away from what it governs). Both directions are scored, which is what makes the rule decide
1024
+ # rather than merely veto. Frozen data, matched case-folded on the atom with its punctuation
1025
+ # stripped; six languages because those are the Latin-script languages the eval corpus covers. A
1026
+ # word in several sets means the same thing structurally in each, so the union is used when no
1027
+ # --lang was given.
1028
+ FUNCTION_WORDS = {
1029
+ "en": {"a", "an", "the", "of", "to", "in", "on", "at", "for", "with", "by", "from", "and",
1030
+ "or", "as", "is", "it", "its", "this", "that", "into", "than", "but", "so"},
1031
+ "es": {"el", "la", "los", "las", "un", "una", "unos", "unas", "de", "del", "al", "en", "con",
1032
+ "por", "para", "y", "o", "que", "su", "sus", "lo", "se", "es"},
1033
+ "pt": {"o", "a", "os", "as", "um", "uma", "de", "do", "da", "dos", "das", "em", "no", "na",
1034
+ "nos", "nas", "com", "por", "para", "e", "que", "se", "ao", "aos"},
1035
+ "fr": {"le", "la", "les", "un", "une", "de", "du", "des", "à", "au", "aux", "en", "dans",
1036
+ "et", "ou", "que", "qui", "ce", "ces", "son", "sa", "ses", "par", "pour", "avec", "sur"},
1037
+ "de": {"der", "die", "das", "ein", "eine", "einen", "einem", "einer", "den", "dem", "des",
1038
+ "zu", "in", "im", "auf", "mit", "und", "oder", "von", "vom", "für", "aus", "an"},
1039
+ "it": {"il", "lo", "la", "i", "gli", "le", "un", "una", "uno", "di", "del", "della", "da",
1040
+ "in", "nel", "con", "per", "e", "che", "su", "al", "ai"},
1041
+ }
1042
+ _FUNCTION_WORDS_ANY = frozenset().union(*FUNCTION_WORDS.values())
1043
+
1044
+ # Penalty scores. Only the ordering matters; 1.0 means "never choose this if anything else fits".
1045
+ PENALTY_FORBIDDEN = 1.0
1046
+ PENALTY_OKURIGANA = 0.9 # between a kanji stem and the hiragana that inflects it
1047
+ PENALTY_FUNCTION_WORD = 0.8 # R4: the line before the break ends in an article/preposition
1048
+ PENALTY_IDEOGRAPHS = 0.6 # between two kanji: no evidence either way, mildly discouraged
1049
+ PENALTY_NEUTRAL = 0.5 # between two content words, or two characters with nothing to say
1050
+ PENALTY_FUNCTION_WORD_START = 0.2 # R4: the next line opens with the article/preposition it governs
1051
+ PENALTY_PARTICLE = 0.2 # R3: after a particle, so the particle stays with the word it marks
1052
+ PENALTY_SENTENCE_END = 0.0 # R3: after 。、!? -- the one break a reader expects
1053
+
1054
+ _HYPHENS = ("-", "‐") # ‑ (non-breaking hyphen) is deliberately NOT here
1055
+
1056
+
1057
+ def _atoms(line: str) -> "List[Tuple[str, bool]]":
1058
+ """Break a line into the smallest pieces a wrap may separate -- one atom per CJK/Thai
1059
+ character, one per emoji cluster, one per whitespace-delimited word otherwise -- each with
1060
+ whether a space stood before it in the original. The flag is what puts the text back together
1061
+ exactly as written: "Hello 世界" keeps its space, "世界です" gains none."""
1062
+ out: "List[Tuple[str, bool]]" = []
1063
+ word = ""
1064
+ spaced = False # a space stands before the atom being built
1065
+ pending = False # a space stands before the NEXT atom
1066
+ attach_next = False # a leading Thai/Lao vowel is waiting for its base consonant
1067
+ # An emoji cluster is one atom: a wrap must never land inside a ZWJ sequence, a flag pair or
1068
+ # between a base and its skin-tone modifier (the same rule combining marks already follow).
1069
+ clusters = {i: len(cl) for i, cl in emoji_clusters(line)}
1070
+ i = 0
1071
+ while i < len(line):
1072
+ ch = line[i]
1073
+ if i in clusters:
1074
+ cluster = line[i:i + clusters[i]]
1075
+ if word:
1076
+ out.append((word, spaced))
1077
+ word = ""
1078
+ out.append((cluster, pending))
1079
+ pending = False
1080
+ attach_next = False
1081
+ i += clusters[i]
1082
+ continue
1083
+ i += 1
1084
+ if char_script(ch) in NO_SPACE_SCRIPTS:
1085
+ if word:
1086
+ out.append((word, spaced))
1087
+ word = ""
1088
+ if out and (attach_next or _is_mark(ch)):
1089
+ # never break between a base and the mark (or the leading vowel) that belongs to
1090
+ # it: the line would start with an orphaned tone mark or vowel sign
1091
+ out[-1] = (out[-1][0] + ch, out[-1][1])
1092
+ else:
1093
+ out.append((ch, pending))
1094
+ pending = False
1095
+ attach_next = ord(ch) in LEADING_VOWELS
1096
+ elif ch.isspace():
1097
+ if word:
1098
+ out.append((word, spaced))
1099
+ word = ""
1100
+ pending = True
1101
+ else:
1102
+ if not word:
1103
+ spaced, pending = pending, False
1104
+ word += ch
1105
+ if word:
1106
+ out.append((word, spaced))
1107
+ return out
1108
+
1109
+
1110
+ def _split_hyphens(atoms: "List[Tuple[str, bool]]") -> "List[Tuple[str, bool]]":
1111
+ """R1's one addition to the atom list: a hyphenated token may break *after* its hyphen.
1112
+
1113
+ "end-to-end" becomes `end-` / `to-` / `end`, each piece carrying the space flag of the token
1114
+ it came from for the first piece and False for the rest, so _join() puts it back with no space
1115
+ at all. A hyphen that is the first or last character of the token (`-5`, `well-`) is never a
1116
+ break point: the guard is that both sides must be non-empty."""
1117
+ out: "List[Tuple[str, bool]]" = []
1118
+ for atom, spaced in atoms:
1119
+ if len(atom) < 3 or not any(h in atom[1:-1] for h in _HYPHENS):
1120
+ out.append((atom, spaced))
1121
+ continue
1122
+ piece = ""
1123
+ first = True
1124
+ for i, ch in enumerate(atom):
1125
+ piece += ch
1126
+ if ch in _HYPHENS and 0 < i < len(atom) - 1:
1127
+ out.append((piece, spaced if first else False))
1128
+ piece = ""
1129
+ first = False
1130
+ if piece:
1131
+ out.append((piece, spaced if first else False))
1132
+ return out
1133
+
1134
+
1135
+ def _join(left: str, atom: str, spaced: bool) -> str:
1136
+ """Put an atom back on a line, restoring the space that stood before it."""
1137
+ if not left:
1138
+ return atom
1139
+ return left + (" " if spaced else "") + atom
1140
+
1141
+
1142
+ def _break_spaced(first: str, second: str) -> bool:
1143
+ """Did a space stand at the break between these two wrapped lines? Only spaced scripts put one
1144
+ there -- a CJK/Thai break sits between two characters that were written with nothing between
1145
+ them, and re-joining them with a space would insert a character the cue never had."""
1146
+ if not first or not second:
1147
+ return False
1148
+ return char_script(first[-1]) not in NO_SPACE_SCRIPTS and char_script(second[0]) not in NO_SPACE_SCRIPTS \
1149
+ and char_script(first[-1]) != "emoji" and char_script(second[0]) != "emoji"
1150
+
1151
+
1152
+ def _is_kana(ch: str) -> bool:
1153
+ return 0x3040 <= ord(ch) <= 0x30FF
1154
+
1155
+
1156
+ def _is_hiragana(ch: str) -> bool:
1157
+ return 0x3040 <= ord(ch) <= 0x309F
1158
+
1159
+
1160
+ def _is_ideograph(ch: str) -> bool:
1161
+ cp = ord(ch)
1162
+ return 0x3400 <= cp <= 0x4DBF or 0x4E00 <= cp <= 0x9FFF or 0xF900 <= cp <= 0xFAFF
1163
+
1164
+
1165
+ def _is_weak_line(line: str) -> "bool":
1166
+ """A line no reader should be given on its own (R2).
1167
+
1168
+ 1.15 asked only "is the last line one atom narrower than ORPHAN_MIN_EM", which a full-width
1169
+ character passes: dl3 still showed a lone `2` and a stranded `行`. Three cases instead, any of
1170
+ which makes a line too thin to stand alone:
1171
+ - a single character narrower than ORPHAN_MIN_EM (1.15's rule, kept);
1172
+ - nothing but digits, punctuation and symbols, at most two characters ("2", "--");
1173
+ - a single kana, whatever its width -- a kana is a full em and passes the width test, but a
1174
+ line holding one is a syllable, not a word.
1175
+ """
1176
+ stripped = (line or "").strip()
1177
+ if not stripped:
1178
+ return True
1179
+ if len(stripped) == 1 and text_width_em(stripped) < ORPHAN_MIN_EM:
1180
+ return True
1181
+ if len(stripped) <= 2 and all(unicodedata.category(c)[0] in "NPS" for c in stripped):
1182
+ return True
1183
+ if len(stripped) == 1 and char_script(stripped) == "ja" and _is_kana(stripped):
1184
+ return True
1185
+ return False
1186
+
1187
+
1188
+ def _function_words(lang: "Optional[str]") -> "frozenset":
1189
+ """R4's table for this language. An unknown or absent language gets the union of the six sets:
1190
+ a token that appears in several of them is the same kind of word in each, which is why the
1191
+ rule is a penalty and not a refusal."""
1192
+ key = (lang or "").strip().lower().split("-")[0]
1193
+ if key in FUNCTION_WORDS:
1194
+ return frozenset(FUNCTION_WORDS[key])
1195
+ return _FUNCTION_WORDS_ANY
1196
+
1197
+
1198
+ def _bare_word(atom: str) -> str:
1199
+ return "".join(c for c in (atom or "") if c.isalpha() or c == "'").strip("'").lower()
1200
+
1201
+
1202
+ def _particle_starts(text: str) -> bool:
1203
+ """Does `text` begin with a particle -- one character, or one of the two-character ones?"""
1204
+ if not text:
1205
+ return False
1206
+ return text[0] in JA_PARTICLES or text.startswith(JA_PARTICLE_WORDS)
1207
+
1208
+
1209
+ def _particle_ends(text: str) -> bool:
1210
+ """Does `text` end with a particle? `から` counts, a bare `ら` does not."""
1211
+ if not text:
1212
+ return False
1213
+ return text[-1] in JA_PARTICLES or text.endswith(JA_PARTICLE_WORDS)
1214
+
1215
+
1216
+ def break_penalty(prev_char: str, next_char: str, lang: "Optional[str]" = None,
1217
+ before: str = "", after: str = "") -> float:
1218
+ """How bad a break between these two characters is, 0.0 (preferred) to 1.0 (forbidden).
1219
+
1220
+ Only consulted among break positions that already fit `max_em`, so a preference can never
1221
+ widen a line or change the line count. Japanese gets the particle half of the table -- a break
1222
+ AFTER a particle is preferred and a break BEFORE one forbidden, because a particle attaches to
1223
+ the word before it; Chinese gets only the sentence-end and forbidden halves, because particles
1224
+ are Japanese grammar.
1225
+
1226
+ `before`/`after` are the text on each side of the break when the caller has it, which is what
1227
+ lets the two-character particles (から/まで/より) be matched as words. Without them only the
1228
+ single-character table applies."""
1229
+ if not prev_char or not next_char:
1230
+ return PENALTY_NEUTRAL
1231
+ script = (lang or "").strip().lower().split("-")[0]
1232
+ if script not in ("ja", "zh"):
1233
+ # A kana on either side settles it: only Japanese has them, and char_script() reads a bare
1234
+ # Han character as Chinese, which used to switch the particle rules off for exactly the
1235
+ # break they exist to judge (`...が|決まる` -- kana before, kanji after).
1236
+ if _is_kana(prev_char) or _is_kana(next_char):
1237
+ script = "ja"
1238
+ else:
1239
+ script = char_script(next_char)
1240
+ if script not in ("ja", "zh"):
1241
+ script = char_script(prev_char)
1242
+ if next_char in JA_NO_LINE_START or prev_char in JA_NO_LINE_END or _is_mark(next_char):
1243
+ return PENALTY_FORBIDDEN
1244
+ if script not in ("ja", "zh"):
1245
+ return PENALTY_NEUTRAL
1246
+ if prev_char in JA_SENTENCE_END:
1247
+ return PENALTY_SENTENCE_END
1248
+ if script == "ja" and _particle_starts(after or next_char):
1249
+ # a particle may not open a line: it belongs to the word before it (kinsoku)
1250
+ return PENALTY_FORBIDDEN
1251
+ if script == "ja" and _particle_ends(before or prev_char):
1252
+ return PENALTY_PARTICLE
1253
+ if script == "ja" and _is_ideograph(prev_char) and _is_hiragana(next_char):
1254
+ # okurigana: 決|まる is inside a word even though neither half is a "word" on its own
1255
+ return PENALTY_OKURIGANA
1256
+ if _is_ideograph(prev_char) and _is_ideograph(next_char):
1257
+ return PENALTY_IDEOGRAPHS
1258
+ return PENALTY_NEUTRAL
1259
+
1260
+
1261
+ def _cut_penalty(atoms: "Sequence[Tuple[str, bool]]", cut: int, lang: "Optional[str]") -> float:
1262
+ """The penalty of breaking `atoms` before index `cut`."""
1263
+ prev_atom = atoms[cut - 1][0]
1264
+ next_atom = atoms[cut][0]
1265
+ if not prev_atom or not next_atom:
1266
+ return PENALTY_NEUTRAL
1267
+ if atoms[cut][1]:
1268
+ # a space stood here: a spaced script, so R4 is the rule that applies, in both directions
1269
+ if all(not ch.isalnum() for ch in next_atom):
1270
+ return PENALTY_FORBIDDEN # never strand punctuation at the start of a line
1271
+ words = _function_words(lang)
1272
+ if _bare_word(prev_atom) in words:
1273
+ return PENALTY_FUNCTION_WORD # stranded at the end of a line, away from its noun
1274
+ if _bare_word(next_atom) in words:
1275
+ return PENALTY_FUNCTION_WORD_START # opens the next line with the phrase it governs
1276
+ return PENALTY_NEUTRAL
1277
+ if prev_atom.endswith(_HYPHENS):
1278
+ return PENALTY_NEUTRAL # R1: a hyphen is a legitimate break point
1279
+ # the text on each side, so a two-character particle (から/まで/より) is seen as one
1280
+ before = "".join(a for a, _sp in atoms[:cut])
1281
+ after = "".join(a for a, _sp in atoms[cut:])
1282
+ return break_penalty(prev_atom[-1], next_atom[0], lang, before=before, after=after)
1283
+
1284
+
1285
+ def best_break(atoms: "Sequence[Tuple[str, bool]]", max_em: float,
1286
+ lang: "Optional[str]" = None) -> "Optional[int]":
1287
+ """The index to break `atoms` at so they become two lines, or None when none fits.
1288
+
1289
+ Among every position whose two halves both fit `max_em`, the one minimising
1290
+ (penalty, widest line, |width difference|) wins: R1-R4 choose first, and 1.15's
1291
+ minimise-the-widest-line rule breaks the ties it used to decide alone."""
1292
+ best = None
1293
+ for cut in range(1, len(atoms)):
1294
+ a = b = ""
1295
+ for atom, sp in atoms[:cut]:
1296
+ a = _join(a, atom, sp)
1297
+ for atom, sp in atoms[cut:]:
1298
+ b = _join(b, atom, sp)
1299
+ wa, wb = text_width_em(a), text_width_em(b)
1300
+ if max(wa, wb) > max_em:
1301
+ continue
1302
+ if _is_weak_line(a) or _is_weak_line(b):
1303
+ continue
1304
+ key = (_cut_penalty(atoms, cut, lang), max(wa, wb), abs(wa - wb))
1305
+ if best is None or key < best[0]:
1306
+ best = (key, cut)
1307
+ return None if best is None else best[1]
1308
+
1309
+
1310
+ def _fix_orphans(lines: "List[str]", max_em: float) -> "List[str]":
1311
+ """No last line that is a single stranded atom.
1312
+
1313
+ Greedy wrapping leaves one character alone whenever the line before it filled exactly: eval 14
1314
+ produced a Thai cue ending in a lone `ล` and a Japanese one ending in a lone `行`. While the
1315
+ last line is one atom narrower than ORPHAN_MIN_EM, the last atom of the line above moves down
1316
+ onto it -- but only while the result still fits and the line above does not become an orphan
1317
+ itself, so a two-word cue is never made worse."""
1318
+ lines = list(lines)
1319
+ for _ in range(len(lines)):
1320
+ if len(lines) < 2:
1321
+ break
1322
+ tail = _atoms(lines[-1])
1323
+ if len(tail) != 1 or text_width_em(lines[-1]) >= ORPHAN_MIN_EM:
1324
+ break
1325
+ prev = _atoms(lines[-2])
1326
+ if len(prev) < 2:
1327
+ break
1328
+ moved, spaced = prev[-1]
1329
+ new_prev = ""
1330
+ for atom, sp in prev[:-1]:
1331
+ new_prev = _join(new_prev, atom, sp)
1332
+ new_last = _join(moved, tail[0][0], _break_spaced(lines[-2], lines[-1]))
1333
+ if text_width_em(new_last) > max_em or text_width_em(new_prev) < ORPHAN_MIN_EM:
1334
+ break
1335
+ lines[-2], lines[-1] = new_prev, new_last
1336
+ return lines
1337
+
1338
+
1339
+ def _fix_weak_lines(lines: "List[str]", max_em: float) -> "List[str]":
1340
+ """R2, generalised: _fix_orphans run at *every* boundary, against _is_weak_line.
1341
+
1342
+ 1.15 only ever looked at the last line, so a stranded digit or kana in the middle of a
1343
+ three-line cue survived. Walking upward from the last line, while a line is weak the last atom
1344
+ of the line above moves down onto it -- with 1.15's two guards intact (the result must still
1345
+ fit, and the line above must not itself become weak), so the line count never changes."""
1346
+ lines = list(lines)
1347
+ for i in range(len(lines) - 1, 0, -1):
1348
+ for _ in range(len(lines)):
1349
+ if not _is_weak_line(lines[i]):
1350
+ break
1351
+ prev = _atoms(lines[i - 1])
1352
+ if len(prev) < 2:
1353
+ break
1354
+ moved, _spaced = prev[-1]
1355
+ new_prev = ""
1356
+ for atom, sp in prev[:-1]:
1357
+ new_prev = _join(new_prev, atom, sp)
1358
+ new_last = _join(moved, lines[i], _break_spaced(lines[i - 1], lines[i]))
1359
+ if text_width_em(new_last) > max_em or _is_weak_line(new_prev) \
1360
+ or text_width_em(new_prev) < ORPHAN_MIN_EM:
1361
+ break
1362
+ lines[i - 1], lines[i] = new_prev, new_last
1363
+ return lines
1364
+
1365
+
1366
+ def _rebalance(lines: "List[str]", max_em: float) -> "List[str]":
1367
+ """Move each break to the one that minimises the widest line of the pair, without changing the
1368
+ line count.
1369
+
1370
+ Greedy wrapping fills line 1 to the brim and leaves line 2 short, which is what split eval 14's
1371
+ `"A third line the tool times for me"` mid-phrase. Only spaced scripts are rebalanced: a
1372
+ non-spaced script has no phrase structure in its atom list, so moving the break there only
1373
+ moves the ragged edge. A break is never placed before a punctuation-only atom."""
1374
+ if len(lines) < 2:
1375
+ return lines
1376
+ out = list(lines)
1377
+ for i in range(len(out) - 1):
1378
+ first, second = out[i], out[i + 1]
1379
+ tail_atoms = _atoms(second)
1380
+ if tail_atoms:
1381
+ tail_atoms[0] = (tail_atoms[0][0], _break_spaced(first, second))
1382
+ atoms = _atoms(first) + tail_atoms
1383
+ if not atoms or any(char_script(ch) in NO_SPACE_SCRIPTS for ch in first + second):
1384
+ continue
1385
+ best = None
1386
+ for cut in range(1, len(atoms)):
1387
+ if not atoms[cut][1]:
1388
+ continue # only break where a space stood
1389
+ if all(not ch.isalnum() for ch in atoms[cut][0]):
1390
+ continue # never strand punctuation at the start of a line
1391
+ a = b = ""
1392
+ for atom, sp in atoms[:cut]:
1393
+ a = _join(a, atom, sp)
1394
+ for atom, sp in atoms[cut:]:
1395
+ b = _join(b, atom, sp)
1396
+ wa, wb = text_width_em(a), text_width_em(b)
1397
+ if max(wa, wb) > max_em:
1398
+ continue
1399
+ key = (max(wa, wb), abs(wa - wb))
1400
+ if best is None or key < best[0]:
1401
+ best = (key, a, b)
1402
+ if best is not None:
1403
+ out[i], out[i + 1] = best[1], best[2]
1404
+ return out
1405
+
1406
+
1407
+ def _rebalance_phrase(lines: "List[str]", max_em: float, lang: "Optional[str]") -> "Tuple[List[str], int]":
1408
+ """_rebalance with R1-R4 deciding, for every script rather than spaced ones only.
1409
+
1410
+ Returns the new lines and how many breaks a phrase rule moved away from the position 1.15's
1411
+ widest-line rule alone would have chosen -- the `phrase_breaks` count in the result."""
1412
+ if len(lines) < 2:
1413
+ return list(lines), 0
1414
+ out = list(lines)
1415
+ moved = 0
1416
+ for i in range(len(out) - 1):
1417
+ first, second = out[i], out[i + 1]
1418
+ tail_atoms = _atoms(second)
1419
+ if tail_atoms:
1420
+ tail_atoms[0] = (tail_atoms[0][0], _break_spaced(first, second))
1421
+ atoms = _split_hyphens(_atoms(first) + tail_atoms)
1422
+ if len(atoms) < 2:
1423
+ continue
1424
+ cut = best_break(atoms, max_em, lang)
1425
+ if cut is None:
1426
+ continue
1427
+ a = b = ""
1428
+ for atom, sp in atoms[:cut]:
1429
+ a = _join(a, atom, sp)
1430
+ for atom, sp in atoms[cut:]:
1431
+ b = _join(b, atom, sp)
1432
+ if (a, b) != (first, second):
1433
+ moved += 1
1434
+ out[i], out[i + 1] = a, b
1435
+ return out, moved
1436
+
1437
+
1438
+ def _greedy_chunks(raw: str, max_em: float) -> "List[str]":
1439
+ """The greedy fill on its own: the line count every mode must keep."""
1440
+ current = ""
1441
+ chunk: "List[str]" = []
1442
+ for atom, spaced in _atoms(raw):
1443
+ candidate = _join(current, atom, spaced)
1444
+ if current and text_width_em(candidate) > max_em:
1445
+ chunk.append(current)
1446
+ current = atom
1447
+ else:
1448
+ current = candidate
1449
+ if current:
1450
+ chunk.append(current)
1451
+ return chunk
1452
+
1453
+
1454
+ def _balance(chunk: "List[str]", max_em: float, mode: str, lang: "Optional[str]") -> "List[str]":
1455
+ """The post-passes for one greedy chunk, in the mode's own order. Never changes the count:
1456
+ a pass that would is discarded, exactly as 1.15 did."""
1457
+ if len(chunk) < 2:
1458
+ return chunk
1459
+ if mode == "measured":
1460
+ fixed = _fix_orphans(chunk, max_em)
1461
+ rebalanced = _rebalance(fixed, max_em)
1462
+ else:
1463
+ fixed = _fix_weak_lines(_fix_orphans(chunk, max_em), max_em)
1464
+ rebalanced, _moved = _rebalance_phrase(fixed, max_em, lang)
1465
+ rebalanced = _fix_weak_lines(rebalanced, max_em)
1466
+ if len(rebalanced) == len(chunk):
1467
+ return rebalanced
1468
+ return fixed if len(fixed) == len(chunk) else chunk
1469
+
1470
+
1471
+ def wrap_text(text: str, max_em: float, *, balance: bool = True, mode: str = "phrase",
1472
+ lang: "Optional[str]" = None) -> "List[str]":
1473
+ """Wrap `text` to lines no wider than `max_em` em, keeping the manual breaks it already has.
1474
+
1475
+ An atom wider than the whole line (one very long word) is left alone on its line rather than
1476
+ cut mid-word: an over-long line is readable, a chopped word is not.
1477
+
1478
+ `mode="phrase"` (the default since 1.16) then applies the four phrase rules -- never inside a
1479
+ word or across a hyphen's wrong side (R1), no line that is a lone digit, punctuation or kana
1480
+ (R2), Japanese/Chinese breaks preferred at sentence ends and after particles, never before one
1481
+ and never inside a word (R3), and an article or preposition kept with the phrase it governs by
1482
+ preferring the break before it and avoiding the break after it (R4). `mode="measured"` is
1483
+ 1.15's behaviour exactly: no one-character orphan line, and a break chosen only to minimise the
1484
+ widest line. Neither mode ever changes the number of lines the greedy fill produced.
1485
+ """
1486
+ lines: "List[str]" = []
1487
+ for raw in text.split("\n"):
1488
+ if not raw.strip():
1489
+ continue
1490
+ chunk = _greedy_chunks(raw, max_em)
1491
+ lines.extend(_balance(chunk, max_em, mode, lang) if balance else chunk)
1492
+ return lines or [text]
1493
+ def wrap_variants(text: str, max_em: float, *, mode: str = "phrase",
1494
+ lang: "Optional[str]" = None) -> "Tuple[List[str], List[str], List[str]]":
1495
+ """`(wrapped, greedy, measured)` for one cue from a single greedy fill.
1496
+
1497
+ layout_cues needs all three -- `wrapped` is what is burnt in, `greedy` is what `rebalanced`
1498
+ counts against and `measured` what `phrase_breaks` counts against -- and used to call
1499
+ wrap_text() three times, re-running the atomiser and the greedy fill each time. The fill is
1500
+ the same for every mode, so it is done once here and only the post-passes are repeated.
1501
+ `measured` is the same list object as `wrapped` when that is already the mode.
1502
+ """
1503
+ wrapped: "List[str]" = []
1504
+ greedy: "List[str]" = []
1505
+ measured: "List[str]" = []
1506
+ for raw in text.split("\n"):
1507
+ if not raw.strip():
1508
+ continue
1509
+ chunk = _greedy_chunks(raw, max_em)
1510
+ greedy.extend(chunk)
1511
+ wrapped.extend(_balance(list(chunk), max_em, mode, lang))
1512
+ measured.extend(chunk if mode == "measured" else _balance(list(chunk), max_em, "measured", None))
1513
+ if not greedy:
1514
+ greedy = [text]
1515
+ return (wrapped or [text], greedy, measured or [text])