ffmpeg-skill 1.15.1 → 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.
@@ -10,9 +10,9 @@ import math
10
10
  import os
11
11
  import re
12
12
  from fractions import Fraction
13
- from typing import Any, Dict, List, Optional, Sequence
13
+ from typing import Any, Dict, List, Optional, Sequence, Tuple
14
14
  from _common.emit import die
15
- from _common.runner import STATE, require_tool, run, run_analysis
15
+ from _common.runner import STATE, dry_run_input_pending, require_tool, run, run_analysis
16
16
 
17
17
 
18
18
  def fingerprint(path: str) -> Dict[str, Any]:
@@ -380,3 +380,93 @@ def _aspect_string(w: Optional[int], h: Optional[int]) -> Optional[str]:
380
380
  return None
381
381
  f = Fraction(w, h)
382
382
  return f"{f.numerator}:{f.denominator}"
383
+
384
+
385
+ # --------------------------------------------------------------- structure detectors (1.16)
386
+ # silencedetect and scdet, lifted out of silence.py and scenes.py byte-for-byte in 1.16.0 so that
387
+ # metadata.py --auto-chapters can measure structure without importing another tool (no script in
388
+ # scripts/ imports a sibling tool; only the _-prefixed modules are shared). silence.py and
389
+ # scenes.py import them back from here, so their behaviour is unchanged.
390
+
391
+ SIL_RE = re.compile(r"silence_(start|end): ([0-9.]+)")
392
+
393
+
394
+ def detect_silences(path: str, threshold: float, min_silence: float) -> "List[Tuple[float, float]]":
395
+ if dry_run_input_pending(path):
396
+ return []
397
+ ffmpeg = require_tool("ffmpeg")
398
+ cmd = [ffmpeg, "-hide_banner", "-nostdin", "-i", path, "-vn", "-af",
399
+ f"silencedetect=noise={threshold}dB:d={min_silence}", "-f", "null", "-"]
400
+ proc = run_analysis(cmd, check=False, record=True)
401
+ if proc.returncode != 0:
402
+ die(f"silencedetect failed:\n{proc.stderr.strip()[-800:]}", kind="ffmpeg")
403
+ silences: "List[Tuple[float, float]]" = []
404
+ start = None
405
+ for kind, val in SIL_RE.findall(proc.stderr):
406
+ if kind == "start":
407
+ start = float(val)
408
+ elif start is not None:
409
+ silences.append((start, float(val)))
410
+ start = None
411
+ if start is not None: # silence runs to the end
412
+ silences.append((start, float("inf")))
413
+ return silences
414
+
415
+
416
+ SCORE_RE = re.compile(r"frame:(\d+)\s+pts:\d+\s+pts_time:([0-9.]+)")
417
+
418
+
419
+ def detect_scenes(path: str, threshold: float, min_len: float, duration: float, ratio: float = 3.0) -> "List[float]":
420
+ """Scene cuts = frames whose scdet score is above `threshold` AND stands out from its
421
+ neighbourhood (score > ratio x median of the surrounding +-12 frames). Sustained motion,
422
+ flashes and fast pans raise the score on many consecutive frames and are rejected;
423
+ a real cut is a one-frame spike. On real footage this roughly doubles precision at
424
+ equal recall compared with the raw scdet threshold."""
425
+ ffmpeg = require_tool("ffmpeg")
426
+ proc = run_analysis([ffmpeg, "-hide_banner", "-nostdin", "-i", path, "-an", "-vf",
427
+ "scale=320:-2,scdet=threshold=0,metadata=print:file=-", "-f", "null", "-"])
428
+ # No `sc_pass=1` on scdet: on FFmpeg 5.x that option means "pass only the frames whose
429
+ # score exceeds the threshold", so every truly static frame (score exactly 0 -- a title
430
+ # card, colour bars) is dropped before metadata=print and the frame numbers are re-counted
431
+ # without them. The +-12-frame neighbourhood around a real cut then fills with the moving
432
+ # segment's scores instead of the still one's zeros, the cut fails the ratio test, and a
433
+ # 4 s smptebars scene made the cuts on both sides of it disappear (found by the 5.1.1 CI
434
+ # job, #146). 6.1+ passes every frame either way. Scores are still indexed by frame number
435
+ # and any frame the filter did not report counts as 0, so a build that drops frames again
436
+ # cannot shift the neighbourhood.
437
+ by_frame: "Dict[int, Tuple[float, float]]" = {}
438
+ cur = None
439
+ for line in proc.stdout.splitlines():
440
+ m = SCORE_RE.match(line)
441
+ if m:
442
+ cur = (int(m.group(1)), float(m.group(2)))
443
+ continue
444
+ if line.startswith("lavfi.scd.score=") and cur is not None:
445
+ try:
446
+ by_frame[cur[0]] = (cur[1], float(line.split("=", 1)[1]))
447
+ except ValueError:
448
+ pass
449
+ cuts = [0.0]
450
+ if not by_frame:
451
+ return cuts
452
+ n_frames = max(by_frame) + 1
453
+ times: "List[float]" = [by_frame[i][0] if i in by_frame else -1.0 for i in range(n_frames)]
454
+ scores: "List[float]" = [by_frame[i][1] if i in by_frame else 0.0 for i in range(n_frames)]
455
+ w = 12
456
+ for i, sc in enumerate(scores):
457
+ if sc < threshold:
458
+ continue
459
+ lo, hi = max(0, i - w), min(len(scores), i + w + 1)
460
+ neigh = sorted(scores[lo:i] + scores[i + 1:hi])
461
+ med = neigh[len(neigh) // 2] if neigh else 0.0
462
+ if sc < ratio * max(med, 0.5):
463
+ continue
464
+ # keep only the local maximum inside +-2 frames
465
+ if any(scores[j] > sc for j in range(max(0, i - 2), min(len(scores), i + 3)) if j != i):
466
+ continue
467
+ t = times[i]
468
+ if t - cuts[-1] >= min_len:
469
+ cuts.append(t)
470
+ if duration - cuts[-1] < min_len and len(cuts) > 1:
471
+ cuts.pop()
472
+ return cuts
@@ -978,3 +978,538 @@ def drawtext_text_opts(text: str, tmpdir: "Optional[str]" = None) -> str:
978
978
  path = os.path.join(tmpdir, name)
979
979
  _DRAWTEXT_PENDING[path] = cleaned
980
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])