kasaframe 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (338) hide show
  1. package/LICENSE +21 -0
  2. package/NOTICE +146 -0
  3. package/README.md +77 -2
  4. package/app/package.json +12 -0
  5. package/app/server/engine.ts +415 -0
  6. package/app/server/main.ts +205 -0
  7. package/app/web/env.d.ts +2 -0
  8. package/app/web/icons.ts +31 -0
  9. package/app/web/index.html +21 -0
  10. package/app/web/main.ts +1013 -0
  11. package/app/web/style.css +430 -0
  12. package/bin/hooks.mjs +51 -0
  13. package/bin/kf.mjs +14 -0
  14. package/docs/AGENTS.md +238 -0
  15. package/music/README.md +13 -0
  16. package/package.json +44 -4
  17. package/packages/adapters/package.json +36 -0
  18. package/packages/adapters/src/css.ts +11 -0
  19. package/packages/adapters/src/gsap.ts +26 -0
  20. package/packages/adapters/src/index.ts +9 -0
  21. package/packages/adapters/src/lottie.ts +22 -0
  22. package/packages/adapters/src/media.ts +59 -0
  23. package/packages/adapters/src/page.ts +229 -0
  24. package/packages/adapters/src/project.ts +214 -0
  25. package/packages/adapters/src/react.ts +36 -0
  26. package/packages/adapters/src/svelte.ts +22 -0
  27. package/packages/adapters/src/three.ts +18 -0
  28. package/packages/adapters/src/vue.ts +27 -0
  29. package/packages/check/package.json +19 -0
  30. package/packages/check/src/check.ts +273 -0
  31. package/packages/check/src/cli.ts +70 -0
  32. package/packages/check/src/image.ts +146 -0
  33. package/packages/check/src/index.ts +9 -0
  34. package/packages/check/src/ocr.swift +37 -0
  35. package/packages/check/src/probe.ts +328 -0
  36. package/packages/check/src/report.ts +44 -0
  37. package/packages/check/src/rules/assets.ts +42 -0
  38. package/packages/check/src/rules/beats.ts +212 -0
  39. package/packages/check/src/rules/camera.ts +217 -0
  40. package/packages/check/src/rules/captions.ts +49 -0
  41. package/packages/check/src/rules/flash.ts +235 -0
  42. package/packages/check/src/rules/frames.ts +135 -0
  43. package/packages/check/src/rules/layout.ts +257 -0
  44. package/packages/check/src/rules/page.ts +87 -0
  45. package/packages/check/src/rules/runs.ts +20 -0
  46. package/packages/check/src/rules/text.ts +326 -0
  47. package/packages/check/src/scan.ts +240 -0
  48. package/packages/check/src/serve.ts +67 -0
  49. package/packages/check/src/types.ts +80 -0
  50. package/packages/check/src/video.ts +137 -0
  51. package/packages/cli/package.json +29 -0
  52. package/packages/cli/src/blur.ts +68 -0
  53. package/packages/cli/src/browser.ts +203 -0
  54. package/packages/cli/src/command.ts +44 -0
  55. package/packages/cli/src/commands/app.ts +25 -0
  56. package/packages/cli/src/commands/beats.ts +36 -0
  57. package/packages/cli/src/commands/blender.ts +270 -0
  58. package/packages/cli/src/commands/check.ts +25 -0
  59. package/packages/cli/src/commands/doctor.ts +22 -0
  60. package/packages/cli/src/commands/gen.ts +173 -0
  61. package/packages/cli/src/commands/look.ts +39 -0
  62. package/packages/cli/src/commands/lyrics.ts +111 -0
  63. package/packages/cli/src/commands/make.ts +542 -0
  64. package/packages/cli/src/commands/new.ts +176 -0
  65. package/packages/cli/src/commands/preview.ts +43 -0
  66. package/packages/cli/src/commands/render.ts +216 -0
  67. package/packages/cli/src/commands/setup.ts +63 -0
  68. package/packages/cli/src/commands/sheet.ts +114 -0
  69. package/packages/cli/src/commands/skill.ts +43 -0
  70. package/packages/cli/src/commands/still.ts +40 -0
  71. package/packages/cli/src/commands/theme.ts +85 -0
  72. package/packages/cli/src/commands/transitions.ts +40 -0
  73. package/packages/cli/src/deps.ts +122 -0
  74. package/packages/cli/src/ffmpeg.ts +81 -0
  75. package/packages/cli/src/index.ts +8 -0
  76. package/packages/cli/src/kf.ts +53 -0
  77. package/packages/cli/src/lock.ts +72 -0
  78. package/packages/cli/src/out.ts +13 -0
  79. package/packages/cli/src/serve.ts +124 -0
  80. package/packages/clock/package.json +14 -0
  81. package/packages/clock/src/index.ts +25 -0
  82. package/packages/clock/src/install.ts +351 -0
  83. package/packages/clock/src/types.ts +45 -0
  84. package/packages/core/package.json +9 -0
  85. package/packages/core/src/beats.ts +283 -0
  86. package/packages/core/src/ease.ts +87 -0
  87. package/packages/core/src/frame.ts +99 -0
  88. package/packages/core/src/index.ts +27 -0
  89. package/packages/core/src/project.ts +146 -0
  90. package/packages/core/src/rand.ts +56 -0
  91. package/packages/core/src/spring.ts +52 -0
  92. package/packages/core/src/style.ts +100 -0
  93. package/packages/core/src/time.ts +88 -0
  94. package/packages/gen/PROVIDERS.md +55 -0
  95. package/packages/gen/package.json +9 -0
  96. package/packages/gen/src/generate.ts +204 -0
  97. package/packages/gen/src/image/og.ts +133 -0
  98. package/packages/gen/src/index.ts +18 -0
  99. package/packages/gen/src/keys.ts +60 -0
  100. package/packages/gen/src/media.ts +150 -0
  101. package/packages/gen/src/plan.ts +106 -0
  102. package/packages/gen/src/registry.ts +51 -0
  103. package/packages/gen/src/seam.ts +103 -0
  104. package/packages/gen/src/types.ts +86 -0
  105. package/packages/gen/src/video/fal.ts +140 -0
  106. package/packages/gen/src/video/h3.ts +107 -0
  107. package/packages/gen/src/video/http.ts +44 -0
  108. package/packages/gen/src/video/ltx.ts +60 -0
  109. package/packages/gen/src/video/minimax.ts +55 -0
  110. package/packages/gen/src/video/veo.ts +71 -0
  111. package/packages/jizura/package.json +16 -0
  112. package/packages/jizura/src/deny.ts +73 -0
  113. package/packages/jizura/src/index.ts +7 -0
  114. package/packages/jizura/src/install.ts +226 -0
  115. package/packages/jizura/src/layer.ts +282 -0
  116. package/packages/jizura/src/lyrics.ts +81 -0
  117. package/packages/jizura/src/pin.ts +5 -0
  118. package/packages/jizura/src/vite.ts +36 -0
  119. package/packages/motion/README.md +142 -0
  120. package/packages/motion/package.json +29 -0
  121. package/packages/motion/src/camera.ts +97 -0
  122. package/packages/motion/src/captions.ts +479 -0
  123. package/packages/motion/src/enter.ts +128 -0
  124. package/packages/motion/src/hangul.ts +129 -0
  125. package/packages/motion/src/impact.ts +100 -0
  126. package/packages/motion/src/index.ts +17 -0
  127. package/packages/motion/src/moves/brushWipe.ts +90 -0
  128. package/packages/motion/src/moves/cloneArray.ts +111 -0
  129. package/packages/motion/src/moves/curtains.ts +81 -0
  130. package/packages/motion/src/moves/echoStack.ts +70 -0
  131. package/packages/motion/src/moves/fit.ts +34 -0
  132. package/packages/motion/src/moves/flood.ts +61 -0
  133. package/packages/motion/src/moves/ghostStack.ts +79 -0
  134. package/packages/motion/src/moves/gridFloor.ts +100 -0
  135. package/packages/motion/src/moves/imageSwarm.ts +109 -0
  136. package/packages/motion/src/moves/index.ts +19 -0
  137. package/packages/motion/src/moves/irisBlades.ts +113 -0
  138. package/packages/motion/src/moves/karaokeLine.ts +94 -0
  139. package/packages/motion/src/moves/ledBoard.ts +94 -0
  140. package/packages/motion/src/moves/liquidTab.ts +72 -0
  141. package/packages/motion/src/moves/markerWrite.ts +63 -0
  142. package/packages/motion/src/moves/morphBox.ts +96 -0
  143. package/packages/motion/src/moves/neonSign.ts +77 -0
  144. package/packages/motion/src/moves/panelPop.ts +78 -0
  145. package/packages/motion/src/moves/paperTear.ts +75 -0
  146. package/packages/motion/src/moves/path.ts +64 -0
  147. package/packages/motion/src/moves/pathText.ts +107 -0
  148. package/packages/motion/src/moves/pointSphere.ts +109 -0
  149. package/packages/motion/src/moves/ransomLetters.ts +73 -0
  150. package/packages/motion/src/moves/shapeMorph.ts +143 -0
  151. package/packages/motion/src/moves/slamWords.ts +89 -0
  152. package/packages/motion/src/moves/sparkTrail.ts +114 -0
  153. package/packages/motion/src/moves/speedLines.ts +91 -0
  154. package/packages/motion/src/moves/splitFlap.ts +147 -0
  155. package/packages/motion/src/moves/stepWords.ts +117 -0
  156. package/packages/motion/src/moves/strikeOut.ts +98 -0
  157. package/packages/motion/src/moves/sunburst.ts +58 -0
  158. package/packages/motion/src/moves/tickerRows.ts +87 -0
  159. package/packages/motion/src/moves/tokenPicker.ts +114 -0
  160. package/packages/motion/src/moves/truchet.ts +85 -0
  161. package/packages/motion/src/moves/wordWall.ts +76 -0
  162. package/packages/motion/src/moves/worldRoll.ts +31 -0
  163. package/packages/motion/src/particles.ts +102 -0
  164. package/packages/motion/src/react/index.tsx +21 -0
  165. package/packages/motion/src/react/parts.tsx +431 -0
  166. package/packages/motion/src/react/time.tsx +97 -0
  167. package/packages/motion/src/text.ts +103 -0
  168. package/packages/motion/src/transition.ts +82 -0
  169. package/packages/motion/src/transition3d/cards.ts +65 -0
  170. package/packages/motion/src/transition3d/chain.ts +84 -0
  171. package/packages/motion/src/transition3d/dive.ts +181 -0
  172. package/packages/motion/src/transition3d/fold.ts +110 -0
  173. package/packages/motion/src/transition3d/gl.ts +219 -0
  174. package/packages/motion/src/transition3d/index.ts +228 -0
  175. package/packages/motion/src/transition3d/live.ts +70 -0
  176. package/packages/motion/src/transition3d/portal.ts +115 -0
  177. package/packages/motion/src/transition3d/react.tsx +29 -0
  178. package/packages/motion/src/transition3d/rig.ts +502 -0
  179. package/packages/motion/src/transition3d/shatter.ts +101 -0
  180. package/packages/motion/src/transition3d/whip.ts +65 -0
  181. package/packages/motion/src/transition3d/world.ts +683 -0
  182. package/packages/motion/src/transition3d/zoomOut.ts +114 -0
  183. package/packages/motion/src/transitions2d/catalog.ts +127 -0
  184. package/packages/motion/src/transitions2d/css.d.ts +2 -0
  185. package/packages/motion/src/transitions2d/cult-ui/LICENSE.md +9 -0
  186. package/packages/motion/src/transitions2d/cult-ui/RollingNumber.tsx +43 -0
  187. package/packages/motion/src/transitions2d/gl-transitions/LICENSE +25 -0
  188. package/packages/motion/src/transitions2d/gl-transitions/shaders.ts +1931 -0
  189. package/packages/motion/src/transitions2d/index.ts +2 -0
  190. package/packages/motion/src/transitions2d/kokonutui/LICENSE +21 -0
  191. package/packages/motion/src/transitions2d/kokonutui/MatrixText.css +16 -0
  192. package/packages/motion/src/transitions2d/kokonutui/MatrixText.tsx +94 -0
  193. package/packages/motion/src/transitions2d/magicui/HyperText.css +15 -0
  194. package/packages/motion/src/transitions2d/magicui/HyperText.tsx +115 -0
  195. package/packages/motion/src/transitions2d/magicui/LICENSE.md +21 -0
  196. package/packages/motion/src/transitions2d/magicui/MorphingText.css +25 -0
  197. package/packages/motion/src/transitions2d/magicui/MorphingText.tsx +130 -0
  198. package/packages/motion/src/transitions2d/magicui/WordRotate.tsx +42 -0
  199. package/packages/motion/src/transitions2d/react.ts +13 -0
  200. package/packages/motion/src/transitions2d/skiper-ui/ClipDiv.css +39 -0
  201. package/packages/motion/src/transitions2d/skiper-ui/ClipDiv.tsx +32 -0
  202. package/packages/motion/src/transitions2d/smoothui/LICENSE +21 -0
  203. package/packages/motion/src/transitions2d/smoothui/SharedAxisY.tsx +50 -0
  204. package/packages/motion/src/transitions2d/smoothui/TextMorph.tsx +90 -0
  205. package/packages/motion/src/transitions2d/smoothui/shaders.ts +223 -0
  206. package/packages/motion/src/transitions2d/unlumen/LICENSE +21 -0
  207. package/packages/motion/src/transitions2d/unlumen/ScrambleText.tsx +114 -0
  208. package/packages/motion/src/transitions2d/vengeance-ui/LICENSE +21 -0
  209. package/packages/motion/src/transitions2d/vengeance-ui/MorphText.css +69 -0
  210. package/packages/motion/src/transitions2d/vengeance-ui/MorphText.tsx +45 -0
  211. package/packages/motion/src/type.ts +131 -0
  212. package/packages/motion/src/ui.ts +172 -0
  213. package/packages/plan/package.json +15 -0
  214. package/packages/plan/src/brief.ts +254 -0
  215. package/packages/plan/src/captions.ts +75 -0
  216. package/packages/plan/src/detect.ts +93 -0
  217. package/packages/plan/src/index.ts +23 -0
  218. package/packages/plan/src/layout.ts +176 -0
  219. package/packages/plan/src/llm.ts +110 -0
  220. package/packages/plan/src/look.ts +220 -0
  221. package/packages/plan/src/plan.ts +125 -0
  222. package/packages/plan/src/request.ts +55 -0
  223. package/packages/plan/src/studio.ts +53 -0
  224. package/packages/plan/src/templates.ts +42 -0
  225. package/packages/plan/src/themes.ts +48 -0
  226. package/packages/plan/src/types.ts +106 -0
  227. package/packages/studio/package.json +12 -0
  228. package/packages/studio/src/index.ts +1 -0
  229. package/packages/studio/src/plugin.ts +236 -0
  230. package/packages/studio/src/ui/main.ts +503 -0
  231. package/packages/studio/src/ui/studio.css +165 -0
  232. package/packages/studio/src/ui/timeline.ts +317 -0
  233. package/packages/themes/kit/THEME-MAKE.md +68 -0
  234. package/packages/themes/kit/THEME.md +30 -0
  235. package/packages/themes/kit/briefs/og-intro-15s/brief.json +16 -0
  236. package/packages/themes/kit/briefs/og-intro-15s/facts.ts +29 -0
  237. package/packages/themes/kit/briefs/og-intro-15s/logo.svg +12 -0
  238. package/packages/themes/kit/effects.ts +5 -0
  239. package/packages/themes/kit/sample/index.html +11 -0
  240. package/packages/themes/kit/sample/main.ts +9 -0
  241. package/packages/themes/kit/sample/scenes/scene.ts +15 -0
  242. package/packages/themes/package.json +16 -0
  243. package/packages/themes/src/factory.ts +296 -0
  244. package/packages/themes/src/fx.ts +14 -0
  245. package/packages/themes/src/index.ts +105 -0
  246. package/packages/themes/src/stage3d.ts +177 -0
  247. package/packages/themes/src/types.ts +58 -0
  248. package/presets/kawaii-pastel.json +94 -0
  249. package/presets/neutral-dark.json +82 -0
  250. package/presets/og.json +98 -0
  251. package/skills/kasaframe/SKILL.md +32 -0
  252. package/templates/_shared/project/index.html +11 -0
  253. package/templates/_shared/project/lib/react.ts +22 -0
  254. package/templates/_shared/project/lib/stage.ts +272 -0
  255. package/templates/_shared/project/main.ts +9 -0
  256. package/templates/_shared/scenes/default.ts +18 -0
  257. package/templates/_shared/scenes/drop.ts +30 -0
  258. package/templates/_shared/scenes/end.ts +23 -0
  259. package/templates/_shared/scenes/hook.ts +16 -0
  260. package/templates/_shared/scenes/seam.ts +41 -0
  261. package/templates/_shared/scenes/step.ts +40 -0
  262. package/templates/ad/preview.png +0 -0
  263. package/templates/ad/template.json +132 -0
  264. package/templates/demo/preview.png +0 -0
  265. package/templates/demo/template.json +130 -0
  266. package/templates/explainer/preview.png +0 -0
  267. package/templates/explainer/template.json +142 -0
  268. package/templates/launch/preview.png +0 -0
  269. package/templates/launch/template.json +130 -0
  270. package/templates/mv/preview.png +0 -0
  271. package/templates/mv/scenes/default.ts +95 -0
  272. package/templates/mv/template.json +176 -0
  273. package/templates/promo/preview.png +0 -0
  274. package/templates/promo/template.json +144 -0
  275. package/templates/shorts/preview.png +0 -0
  276. package/templates/shorts/scenes/hook.ts +34 -0
  277. package/templates/shorts/template.json +127 -0
  278. package/templates/square/preview.png +0 -0
  279. package/templates/square/template.json +113 -0
  280. package/themes/debug-hud/THEME.md +39 -0
  281. package/themes/debug-hud/effects.ts +389 -0
  282. package/themes/debug-hud/sample/assets/og/logo.svg +12 -0
  283. package/themes/debug-hud/sample/beats.json +1 -0
  284. package/themes/debug-hud/sample/index.html +11 -0
  285. package/themes/debug-hud/sample/kf.json +59 -0
  286. package/themes/debug-hud/sample/lib/hudkit.ts +49 -0
  287. package/themes/debug-hud/sample/lib/og.ts +29 -0
  288. package/themes/debug-hud/sample/main.ts +9 -0
  289. package/themes/debug-hud/sample/scenes/end.ts +62 -0
  290. package/themes/debug-hud/sample/scenes/gather.ts +67 -0
  291. package/themes/debug-hud/sample/scenes/models.ts +70 -0
  292. package/themes/debug-hud/sample/scenes/price.ts +79 -0
  293. package/themes/debug-hud/sample/style.json +91 -0
  294. package/themes/debug-hud/still.png +0 -0
  295. package/themes/debug-hud/style.json +91 -0
  296. package/themes/debug-hud/theme.json +68 -0
  297. package/themes/og-measure/THEME.md +47 -0
  298. package/themes/og-measure/effects.ts +432 -0
  299. package/themes/og-measure/sample/assets/brand/og-logo-full.svg +12 -0
  300. package/themes/og-measure/sample/assets/brand/og-logo.svg +12 -0
  301. package/themes/og-measure/sample/assets/data/measure.json +353 -0
  302. package/themes/og-measure/sample/beats.json +1 -0
  303. package/themes/og-measure/sample/index.html +11 -0
  304. package/themes/og-measure/sample/kf.json +66 -0
  305. package/themes/og-measure/sample/lib/data.ts +62 -0
  306. package/themes/og-measure/sample/main.ts +9 -0
  307. package/themes/og-measure/sample/measure.ts +78 -0
  308. package/themes/og-measure/sample/scenes/end.ts +47 -0
  309. package/themes/og-measure/sample/scenes/gather.ts +51 -0
  310. package/themes/og-measure/sample/scenes/models.ts +34 -0
  311. package/themes/og-measure/sample/scenes/one.ts +39 -0
  312. package/themes/og-measure/sample/scenes/price.ts +56 -0
  313. package/themes/og-measure/sample/style.json +97 -0
  314. package/themes/og-measure/still.png +0 -0
  315. package/themes/og-measure/style.json +97 -0
  316. package/themes/og-measure/theme.json +79 -0
  317. package/themes/riso-zine/THEME.md +43 -0
  318. package/themes/riso-zine/effects.ts +321 -0
  319. package/themes/riso-zine/sample/assets/og/logo.svg +12 -0
  320. package/themes/riso-zine/sample/beats.json +1 -0
  321. package/themes/riso-zine/sample/index.html +11 -0
  322. package/themes/riso-zine/sample/kf.json +59 -0
  323. package/themes/riso-zine/sample/lib/og.ts +29 -0
  324. package/themes/riso-zine/sample/lib/shot.ts +86 -0
  325. package/themes/riso-zine/sample/lib/zine.ts +42 -0
  326. package/themes/riso-zine/sample/main.ts +9 -0
  327. package/themes/riso-zine/sample/scenes/end.ts +61 -0
  328. package/themes/riso-zine/sample/scenes/gather.ts +62 -0
  329. package/themes/riso-zine/sample/scenes/models.ts +52 -0
  330. package/themes/riso-zine/sample/scenes/price.ts +82 -0
  331. package/themes/riso-zine/sample/style.json +99 -0
  332. package/themes/riso-zine/still.png +0 -0
  333. package/themes/riso-zine/style.json +99 -0
  334. package/themes/riso-zine/theme.json +75 -0
  335. package/tools/beats/beats.py +372 -0
  336. package/tools/beats/requirements.txt +3 -0
  337. package/tools/lyrics/align.py +164 -0
  338. package/tools/lyrics/requirements.txt +9 -0
@@ -0,0 +1,257 @@
1
+ import type { Thumb } from "../image.ts";
2
+ import type { Finding, SafeArea } from "../types.ts";
3
+
4
+ // 멈춤·빈 화면 검사를 다 통과하고도 「화면이 비었다」가 나온다 — 9:16 에 그림·글이 가운데 20% 만 차지하거나,
5
+ // 흐르는 바탕 위에 같은 구도가 9초씩 가는 경우. 둘 다 엄지만 한 썸네일(160px 회색조)로 잰다.
6
+
7
+ export interface SceneSpan {
8
+ id: string;
9
+ from: number;
10
+ dur: number;
11
+ }
12
+
13
+ interface Thumbish extends Thumb {
14
+ width?: number;
15
+ height?: number;
16
+ }
17
+
18
+ const THUMB_W = 160;
19
+
20
+ function dims(t: Thumbish, aspect: number): { w: number; h: number } {
21
+ const w = t.width ?? THUMB_W;
22
+ return { w, h: t.height ?? (Math.round(t.luma.length / w) || Math.round(w / aspect)) };
23
+ }
24
+
25
+ // 가장 흔한 밝기(±3)를 바탕으로 본다 — 생성 그림처럼 바탕이 고르지 않으면 바탕 몫이 작아 「꽉 참」으로 나온다
26
+ function background(l: Uint8Array): { value: number; share: number } {
27
+ const hist = new Uint32Array(256);
28
+ for (const v of l) hist[v]++;
29
+ let best = 0;
30
+ let value = 0;
31
+ for (let v = 0; v < 256; v++) {
32
+ let n = 0;
33
+ for (let d = -3; d <= 3; d++) n += hist[v + d] ?? 0;
34
+ if (n > best) {
35
+ best = n;
36
+ value = v;
37
+ }
38
+ }
39
+ return { value, share: best / l.length };
40
+ }
41
+
42
+ // 내용 상자 — 바탕과 다른 픽셀의 가로·세로 분포에서 양 끝 3% 씩 떼어 낸 범위(구석의 작은 로고 하나가 상자를 키우지 않게)
43
+ export function contentBox(l: Uint8Array, w: number, h: number, safe: { x0: number; y0: number; x1: number; y1: number }): { wFrac: number; hFrac: number; area: number } {
44
+ const bg = background(l);
45
+ if (bg.share < 0.45) return { wFrac: 1, hFrac: 1, area: 1 };
46
+ const cols = new Float64Array(w);
47
+ const rows = new Float64Array(h);
48
+ let total = 0;
49
+ for (let y = safe.y0; y < safe.y1; y++) {
50
+ for (let x = safe.x0; x < safe.x1; x++) {
51
+ if (Math.abs(l[y * w + x] - bg.value) > 14) {
52
+ cols[x]++;
53
+ rows[y]++;
54
+ total++;
55
+ }
56
+ }
57
+ }
58
+ const sw = safe.x1 - safe.x0;
59
+ const sh = safe.y1 - safe.y0;
60
+ if (total < 4) return { wFrac: 0, hFrac: 0, area: 0 };
61
+ const span = (arr: Float64Array, lo: number, hi: number) => {
62
+ let acc = 0;
63
+ let a = lo;
64
+ while (a < hi && acc + arr[a] <= total * 0.03) acc += arr[a++];
65
+ acc = 0;
66
+ let b = hi - 1;
67
+ while (b > a && acc + arr[b] <= total * 0.03) acc += arr[b--];
68
+ return b - a + 1;
69
+ };
70
+ const wFrac = span(cols, safe.x0, safe.x1) / sw;
71
+ const hFrac = span(rows, safe.y0, safe.y1) / sh;
72
+ return { wFrac, hFrac, area: wFrac * hFrac };
73
+ }
74
+
75
+ export interface FillOptions {
76
+ width: number;
77
+ height: number;
78
+ safe?: SafeArea;
79
+ // 세로 화면은 주 요소가 안전 영역 폭의 이만큼은 차야 한다
80
+ minWidthVertical?: number;
81
+ minArea?: number;
82
+ }
83
+
84
+ // 시작이 t 이하인 마지막 장면 — kf.json 시각은 넷째 자리에서 반올림돼 경계에 0.0001초 틈이 날 수 있다
85
+ function sceneOf(t: number, scenes: SceneSpan[]): SceneSpan | undefined {
86
+ let hit: SceneSpan | undefined;
87
+ for (const s of scenes) if (s.from <= t + 1e-3) hit = s;
88
+ return hit;
89
+ }
90
+
91
+ function median(xs: number[]): number {
92
+ const s = [...xs].sort((a, b) => a - b);
93
+ return s.length ? s[Math.floor(s.length / 2)] : 0;
94
+ }
95
+
96
+ // 화면 채움 — 장면마다 내용 상자의 가운데값. 세로 화면은 폭 70% 아래면 경고, 넓이 12% 아래면 오류
97
+ export function fill(thumbs: Thumbish[], scenes: SceneSpan[], o: FillOptions): Finding[] {
98
+ if (!thumbs.length) return [];
99
+ const aspect = o.width / o.height;
100
+ const vertical = o.height > o.width;
101
+ const minW = o.minWidthVertical ?? 0.7;
102
+ const minArea = o.minArea ?? 0.12;
103
+ const groups = new Map<string, { span: SceneSpan; w: number[]; a: number[] }>();
104
+ for (const t of thumbs) {
105
+ const s = sceneOf(t.t, scenes) ?? { id: "영상", from: 0, dur: Infinity };
106
+ const { w, h } = dims(t, aspect);
107
+ const k = w / o.width;
108
+ const sa = o.safe ?? { top: 0, bottom: 0, left: 0, right: 0 };
109
+ const box = contentBox(t.luma, w, h, {
110
+ x0: Math.max(0, Math.round(sa.left * k)),
111
+ y0: Math.max(0, Math.round(sa.top * k)),
112
+ x1: Math.min(w, w - Math.round(sa.right * k)),
113
+ y1: Math.min(h, h - Math.round(sa.bottom * k)),
114
+ });
115
+ const g = groups.get(s.id) ?? { span: s, w: [], a: [] };
116
+ g.w.push(box.wFrac);
117
+ g.a.push(box.area);
118
+ groups.set(s.id, g);
119
+ }
120
+ const out: Finding[] = [];
121
+ for (const { span, w, a } of groups.values()) {
122
+ const mw = median(w);
123
+ const ma = median(a);
124
+ const to = Number.isFinite(span.dur) ? span.from + span.dur : thumbs.at(-1)!.t;
125
+ if (ma < minArea) {
126
+ out.push({
127
+ rule: "layout.fill",
128
+ severity: "error",
129
+ from: span.from,
130
+ to,
131
+ message: `장면 ${span.id} 의 내용이 안전 영역의 ${(ma * 100).toFixed(0)}% 만 차지한다 — 화면이 비어 보인다. 주 요소를 키우거나(큰 글자 단계·도식 확대) 화면을 채우는 바탕·요소를 더한다`,
132
+ detail: { scene: span.id, area: Math.round(ma * 1000) / 1000, width: Math.round(mw * 1000) / 1000 },
133
+ });
134
+ } else if (vertical && mw < minW) {
135
+ out.push({
136
+ rule: "layout.fill",
137
+ severity: "warn",
138
+ from: span.from,
139
+ to,
140
+ message: `장면 ${span.id} 의 주 요소 폭이 안전 영역의 ${(mw * 100).toFixed(0)}% — 세로 화면은 ${Math.round(minW * 100)}% 넘게 채운다`,
141
+ detail: { scene: span.id, area: Math.round(ma * 1000) / 1000, width: Math.round(mw * 1000) / 1000 },
142
+ });
143
+ }
144
+ }
145
+ return out;
146
+ }
147
+
148
+ // 구도 — 썸네일을 16×N 칸으로 줄인 평균 밝기. 흐르는 점무늬처럼 잔 움직임은 칸 평균에 묻히고, 카메라·확대·새 요소는 칸을 바꾼다
149
+ function layoutSig(t: Thumbish, aspect: number, cols = 16): Float32Array {
150
+ const { w, h } = dims(t, aspect);
151
+ const rows = Math.max(4, Math.round((cols * h) / w));
152
+ const sig = new Float32Array(cols * rows);
153
+ const cnt = new Uint32Array(cols * rows);
154
+ for (let y = 0; y < h; y++) {
155
+ const r = Math.min(rows - 1, Math.floor((y * rows) / h));
156
+ for (let x = 0; x < w; x++) {
157
+ const c = Math.min(cols - 1, Math.floor((x * cols) / w));
158
+ sig[r * cols + c] += t.luma[y * w + x];
159
+ cnt[r * cols + c]++;
160
+ }
161
+ }
162
+ for (let i = 0; i < sig.length; i++) sig[i] /= cnt[i] || 1;
163
+ return sig;
164
+ }
165
+
166
+ function sigDiff(a: Float32Array, b: Float32Array): number {
167
+ let s = 0;
168
+ for (let i = 0; i < a.length; i++) s += Math.abs(a[i] - b[i]);
169
+ return s / a.length;
170
+ }
171
+
172
+ // 화면 안 내용 — 칸마다 센 가장자리(이웃 픽셀 밝기 차 합이 24 넘는 픽셀) 수와 평균 밝기. 숫자가 구르고 칸이 차고 줄이 붙으면 이 지도가 바뀐다.
173
+ // 칸 평균 밝기만 보면 이런 변화가 묻혀 모델이 카메라 샷을 박아 넘겼다. 빛 방울·모눈처럼 부드럽거나 가는 바탕 흐름은 센 가장자리를 거의 안 만든다
174
+ interface ContentSig {
175
+ edges: Uint16Array;
176
+ mean: Float32Array;
177
+ }
178
+
179
+ export function contentSig(t: Thumbish, aspect: number, cols = 32): ContentSig {
180
+ const { w, h } = dims(t, aspect);
181
+ const rows = Math.max(4, Math.round((cols * h) / w));
182
+ const edges = new Uint16Array(cols * rows);
183
+ const mean = new Float32Array(cols * rows);
184
+ const cnt = new Uint16Array(cols * rows);
185
+ const l = t.luma;
186
+ for (let y = 0; y < h; y++) {
187
+ const r = Math.min(rows - 1, Math.floor((y * rows) / h));
188
+ for (let x = 0; x < w; x++) {
189
+ const k = r * cols + Math.min(cols - 1, Math.floor((x * cols) / w));
190
+ const i = y * w + x;
191
+ mean[k] += l[i];
192
+ cnt[k]++;
193
+ if (x < w - 1 && y < h - 1 && Math.abs(l[i] - l[i + 1]) + Math.abs(l[i] - l[i + w]) > 24) edges[k]++;
194
+ }
195
+ }
196
+ for (let k = 0; k < mean.length; k++) mean[k] /= cnt[k] || 1;
197
+ return { edges, mean };
198
+ }
199
+
200
+ // 바뀐 칸의 몫 — 가장자리 수가 3 넘게 달라졌거나, 가장자리가 있는 칸(글·칸·막대)의 밝기가 20 넘게 달라졌다(회색 칸이 검게 차는 것).
201
+ // 가장자리 없는 칸의 밝기 변화(부드러운 바탕 흐름)는 세지 않는다
202
+ export function contentChange(a: ContentSig, b: ContentSig): number {
203
+ let n = 0;
204
+ for (let i = 0; i < a.edges.length; i++) {
205
+ if (Math.abs(a.edges[i] - b.edges[i]) > 3 || ((a.edges[i] > 3 || b.edges[i] > 3) && Math.abs(a.mean[i] - b.mean[i]) > 20)) n++;
206
+ }
207
+ return n / a.edges.length;
208
+ }
209
+
210
+ export interface StaticOptions {
211
+ width: number;
212
+ height: number;
213
+ warnSec?: number;
214
+ errorSec?: number;
215
+ // 칸 평균 밝기 차(0..255). 이보다 덜 바뀌고
216
+ threshold?: number;
217
+ // 화면 안 내용이 바뀐 칸의 몫도 이보다 작으면 같은 구도로 본다
218
+ contentThreshold?: number;
219
+ }
220
+
221
+ // 장면 안 변화량 — 같은 구도가 1.5초 넘게 가면 경고, 3초 넘으면 오류. 칸 평균 밝기(컷·확대·새 면)와 화면 안 내용(숫자·칸·줄)
222
+ // 둘 다 닻 프레임에서 안 바뀌어야 같은 구도다 — 카메라 컷 없이도 내용이 움직이면 넘는다. 카메라로 메우면 camera.wander
223
+ export function staticLayout(thumbs: Thumbish[], scenes: SceneSpan[], o: StaticOptions): Finding[] {
224
+ const aspect = o.width / o.height;
225
+ const warnSec = o.warnSec ?? 1.5;
226
+ const errorSec = o.errorSec ?? 3;
227
+ const thr = o.threshold ?? 2.5;
228
+ const cthr = o.contentThreshold ?? 0.015;
229
+ const out: Finding[] = [];
230
+ const spans = scenes.length ? scenes : [{ id: "영상", from: 0, dur: Infinity }];
231
+ for (const s of spans) {
232
+ const ts = thumbs.filter((t) => sceneOf(t.t, spans) === s);
233
+ if (ts.length < 2) continue;
234
+ const sigs = ts.map((t) => layoutSig(t, aspect));
235
+ const content = ts.map((t) => contentSig(t, aspect));
236
+ let a = 0;
237
+ while (a < ts.length - 1) {
238
+ let j = a + 1;
239
+ while (j < ts.length && sigDiff(sigs[a], sigs[j]) < thr && contentChange(content[a], content[j]) < cthr) j++;
240
+ const end = j - 1;
241
+ const len = ts[end].t - ts[a].t;
242
+ if (len > warnSec + 1e-6) {
243
+ const severity = len > errorSec + 1e-6 ? "error" : "warn";
244
+ out.push({
245
+ rule: "layout.static",
246
+ severity,
247
+ from: ts[a].t,
248
+ to: ts[end].t,
249
+ message: `장면 ${s.id} 에서 같은 구도가 ${len.toFixed(1)}초 간다 — ${warnSec}초 안에 새 요소·칸 채우기·숫자 키우기·컷·전환으로 구도를 바꾼다(카메라를 이유 없이 옮겨 메우지 않는다)`,
250
+ detail: { scene: s.id, seconds: Math.round(len * 10) / 10 },
251
+ });
252
+ }
253
+ a = Math.max(end, a + 1);
254
+ }
255
+ }
256
+ return out;
257
+ }
@@ -0,0 +1,87 @@
1
+ import type { ContrastSample, RepeatSample } from "../scan.ts";
2
+ import type { Finding } from "../types.ts";
3
+ import { roundBox } from "./text.ts";
4
+ import { runs } from "./runs.ts";
5
+
6
+ // 페이드 중인 글은 낮은 대비가 당연하다. 같은 글이 minSec 넘게 낮은 대비로 머물 때만 말한다.
7
+ // 장식으로 흐리게 깐 작은 글은 일부러일 수 있어 경고에 그친다. 읽으라고 크게 쓴 글이 거의 안 보이면 오류.
8
+ export function contrast(samples: ContrastSample[], o: { shotFps: number; minSec?: number; warnBelow?: number; errorBelow?: number; errorMinPx?: number }): Finding[] {
9
+ const minSec = o.minSec ?? 0.9;
10
+ const warnBelow = o.warnBelow ?? 3;
11
+ const errorBelow = o.errorBelow ?? 1.5;
12
+ const errorMinPx = o.errorMinPx ?? 24;
13
+ const byText = new Map<string, ContrastSample[]>();
14
+ for (const s of samples) {
15
+ if (s.ratio >= warnBelow) continue;
16
+ const key = s.text;
17
+ if (!byText.has(key)) byText.set(key, []);
18
+ byText.get(key)!.push(s);
19
+ }
20
+ const each: { from: number; to: number; text: string; worst: ContrastSample; severe: boolean }[] = [];
21
+ for (const [text, list] of byText) {
22
+ for (const r of runs(list.map((s) => s.t), 1.5 / o.shotFps)) {
23
+ if (r.to - r.from + 1 / o.shotFps < minSec) continue;
24
+ const inRun = list.filter((s) => s.t >= r.from && s.t <= r.to);
25
+ const worst = inRun.reduce((a, b) => (b.ratio < a.ratio ? b : a));
26
+ each.push({ from: r.from, to: r.to + 1 / o.shotFps, text, worst, severe: worst.ratio < errorBelow && worst.sizePx >= errorMinPx });
27
+ }
28
+ }
29
+ // 한 화면에 흐린 글이 여럿이면 한 줄로 — 고칠 곳은 대개 그 화면의 바탕이나 덮개 하나다.
30
+ each.sort((a, b) => a.from - b.from);
31
+ const groups: (typeof each)[] = [];
32
+ for (const e of each) {
33
+ const g = groups.find((x) => x[0].severe === e.severe && e.from < Math.max(...x.map((y) => y.to)) && e.to > x[0].from);
34
+ if (g) g.push(e);
35
+ else groups.push([e]);
36
+ }
37
+ return groups.map((g) => {
38
+ const worst = g.reduce((a, b) => (b.worst.ratio < a.worst.ratio ? b : a)).worst;
39
+ const severe = g[0].severe;
40
+ const names = g.slice(0, 4).map((x) => `"${x.text.trim()}"`).join(" · ");
41
+ return {
42
+ rule: "text.contrast",
43
+ severity: severe ? ("error" as const) : ("warn" as const),
44
+ from: Math.min(...g.map((x) => x.from)),
45
+ to: Math.max(...g.map((x) => x.to)),
46
+ message: `글자 대비가 낮다 — 가장 낮은 ${worst.ratio}:1 (${worst.sizePx}px) · 글 ${g.length}개: ${names}${g.length > 4 ? " …" : ""}${severe ? " — 거의 안 보인다" : " — 바탕과 3:1 이상 벌린다"}`,
47
+ detail: { ratio: worst.ratio, sizePx: worst.sizePx, box: roundBox(worst.box), texts: g.map((x) => ({ text: x.text, ratio: x.worst.ratio, from: x.from, to: x.to })) },
48
+ };
49
+ });
50
+ }
51
+
52
+ export function repeatable(samples: RepeatSample[]): Finding[] {
53
+ const bad = samples.filter((s) => s.diff > 0.5 || !s.textSame);
54
+ return bad.map((s) => ({
55
+ rule: "frame.nondeterministic",
56
+ severity: "error" as const,
57
+ from: s.t,
58
+ to: s.t,
59
+ message: `같은 시각을 두 번 그렸더니 달랐다(차 ${s.diff}${s.textSame ? "" : ", 글도 다름"}) — Math.random·Date.now·누적 상태를 t 의 함수로 바꾼다`,
60
+ detail: { diff: s.diff, textSame: s.textSame },
61
+ }));
62
+ }
63
+
64
+ export function pageErrors(errors: { t: number; text: string }[], fps: number): Finding[] {
65
+ const byText = new Map<string, number[]>();
66
+ for (const e of errors) {
67
+ // 시각만 다른 같은 오류(「@7.300」 따위)는 한데 묶는다.
68
+ const key = e.text.replace(/@?\d+\.\d+/g, "#").slice(0, 300);
69
+ if (!byText.has(key)) byText.set(key, []);
70
+ byText.get(key)!.push(e.t);
71
+ }
72
+ const out: Finding[] = [];
73
+ for (const [key, times] of byText) {
74
+ const first = errors.find((e) => e.text.replace(/@?\d+\.\d+/g, "#").slice(0, 300) === key)!;
75
+ const valid = times.filter((t) => t >= 0);
76
+ const rs = valid.length ? runs(valid, 1.5 / fps) : [{ from: 0, to: 0, count: times.length }];
77
+ out.push({
78
+ rule: "page.error",
79
+ severity: "error",
80
+ from: rs[0].from,
81
+ to: rs[rs.length - 1].to,
82
+ message: `페이지 오류 ${times.length}번${valid.length ? "" : "(불러올 때)"} — ${first.text.slice(0, 200)}`,
83
+ detail: { count: times.length, spans: rs.length },
84
+ });
85
+ }
86
+ return out;
87
+ }
@@ -0,0 +1,20 @@
1
+ export interface Run {
2
+ from: number;
3
+ to: number;
4
+ count: number;
5
+ }
6
+
7
+ // 정렬된 시각들을 maxGap 보다 가까운 것끼리 묶는다 — 「연속 프레임 묶음 = 한 번」.
8
+ export function runs(times: number[], maxGap: number): Run[] {
9
+ const out: Run[] = [];
10
+ for (const t of [...times].sort((a, b) => a - b)) {
11
+ const last = out[out.length - 1];
12
+ if (last && t - last.to <= maxGap) {
13
+ last.to = t;
14
+ last.count++;
15
+ } else {
16
+ out.push({ from: t, to: t, count: 1 });
17
+ }
18
+ }
19
+ return out;
20
+ }
@@ -0,0 +1,326 @@
1
+ import type { Box, Finding, FrameText, SafeArea, TextHit } from "../types.ts";
2
+ import { runs } from "./runs.ts";
3
+
4
+ export interface Line {
5
+ text: string;
6
+ box?: Box;
7
+ sizePx: number;
8
+ alpha: number;
9
+ hits: TextHit[];
10
+ }
11
+
12
+ // 터미널 셀 렌더러처럼 글자를 한 자씩 그리는 화면이 많다. 같은 줄에 놓인 조각을 x 순서로 이어 붙여야
13
+ // 「u」「n」「d」… 로 흩어진 undefined 가 한 낱말로 보인다.
14
+ export function assembleLines(hits: TextHit[]): Line[] {
15
+ const placed = hits.filter((h) => h.box && h.alpha > 0.05);
16
+ const lines: Line[] = hits.filter((h) => !h.box).map((h) => ({ text: h.text, sizePx: h.sizePx, alpha: h.alpha, hits: [h] }));
17
+ // 가운데 높이로 묶으면 g·y 처럼 아래로 내려가는 글자가 딴 줄로 빠진다. 세로로 겹치는 정도로 묶는다.
18
+ const rows: { y0: number; y1: number; hits: TextHit[] }[] = [];
19
+ const size = (h: TextHit) => Math.max(h.sizePx, 1);
20
+ for (const h of [...placed].sort((a, b) => a.box!.y0 - b.box!.y0)) {
21
+ const b = h.box!;
22
+ const row = rows.find((r) => {
23
+ const ref = r.hits[0];
24
+ const overlap = Math.min(r.y1, b.y1) - Math.max(r.y0, b.y0);
25
+ return h.via === ref.via && !!h.lyrics === !!ref.lyrics && overlap >= 0.5 * Math.min(r.y1 - r.y0, b.y1 - b.y0) && Math.max(size(h), size(ref)) / Math.min(size(h), size(ref)) < 1.35;
26
+ });
27
+ if (row) {
28
+ row.hits.push(h);
29
+ row.y0 = Math.min(row.y0, b.y0);
30
+ row.y1 = Math.max(row.y1, b.y1);
31
+ } else rows.push({ y0: b.y0, y1: b.y1, hits: [h] });
32
+ }
33
+ for (const { hits: row } of rows) {
34
+ row.sort((a, b) => a.box!.x0 - b.box!.x0);
35
+ let cur: TextHit[] = [row[0]];
36
+ let text = row[0].text;
37
+ const flush = () => {
38
+ const b = cur.map((h) => h.box!);
39
+ lines.push({
40
+ text,
41
+ box: { x0: Math.min(...b.map((x) => x.x0)), y0: Math.min(...b.map((x) => x.y0)), x1: Math.max(...b.map((x) => x.x1)), y1: Math.max(...b.map((x) => x.y1)) },
42
+ sizePx: Math.max(...cur.map(size)),
43
+ alpha: Math.max(...cur.map((h) => h.alpha)),
44
+ hits: cur,
45
+ });
46
+ };
47
+ for (let i = 1; i < row.length; i++) {
48
+ const prev = cur[cur.length - 1];
49
+ const h = row[i];
50
+ const gap = h.box!.x0 - prev.box!.x1;
51
+ // 다른 덩어리(옆 pane·다른 칸)면 끊는다. 한 칸쯤 벌어졌으면 띄어쓰기로 본다.
52
+ if (gap > 2.2 * size(prev)) {
53
+ flush();
54
+ cur = [h];
55
+ text = h.text;
56
+ continue;
57
+ }
58
+ text += gap > 0.45 * size(prev) ? " " + h.text : h.text;
59
+ cur.push(h);
60
+ }
61
+ flush();
62
+ }
63
+ return lines;
64
+ }
65
+
66
+ const TOKENS: { name: string; re: RegExp }[] = [
67
+ { name: "undefined", re: /undefined/ },
68
+ { name: "NaN", re: /(^|[^A-Za-z])NaN([^A-Za-z]|$)/ },
69
+ { name: "[object Object]", re: /\[object [A-Za-z]+\]/ },
70
+ // 터미널 화면의 2>/dev/null 같은 경로는 빼고
71
+ { name: "null", re: /(^|[^A-Za-z/._-])null([^A-Za-z/._-]|$)/ },
72
+ { name: "Infinity", re: /(^|[^A-Za-z])-?Infinity([^A-Za-z]|$)/ },
73
+ ];
74
+
75
+ export interface TextRuleOptions {
76
+ // 코드 화면처럼 일부러 null 을 보이는 영상이면 여기 넣는다(kf.json check.allowText)
77
+ allow?: string[];
78
+ fps: number;
79
+ // OCR 로 읽은 글은 겹친 글자가 뭉개져 「DuHdefined」처럼 나온다. 긴 낱말은 두 글자까지 틀려도 잡는다.
80
+ fuzzy?: boolean;
81
+ }
82
+
83
+ function editDistance(a: string, b: string): number {
84
+ const d = Array.from({ length: b.length + 1 }, (_, j) => j);
85
+ for (let i = 1; i <= a.length; i++) {
86
+ let prev = d[0];
87
+ d[0] = i;
88
+ for (let j = 1; j <= b.length; j++) {
89
+ const cur = d[j];
90
+ d[j] = Math.min(d[j] + 1, d[j - 1] + 1, prev + (a[i - 1] === b[j - 1] ? 0 : 1));
91
+ prev = cur;
92
+ }
93
+ }
94
+ return d[b.length];
95
+ }
96
+
97
+ export function fuzzyHas(text: string, word: string, maxDist: number): boolean {
98
+ const s = text.toLowerCase().replace(/\s+/g, "");
99
+ const w = word.toLowerCase();
100
+ for (let len = w.length - 1; len <= w.length + 1; len++) {
101
+ for (let i = 0; i + len <= s.length; i++) if (editDistance(s.slice(i, i + len), w) <= maxDist) return true;
102
+ }
103
+ return false;
104
+ }
105
+
106
+ const FUZZY: { name: string; word: string; dist: number }[] = [
107
+ { name: "undefined", word: "undefined", dist: 2 },
108
+ { name: "[object Object]", word: "objectobject", dist: 2 },
109
+ ];
110
+
111
+ export function badText(frames: FrameText[], o: TextRuleOptions): Finding[] {
112
+ const allow = new Set(o.allow ?? []);
113
+ const hitsAt = new Map<string, { t: number; line: string; where?: string; argType?: string }[]>();
114
+ for (const f of frames) {
115
+ const found = new Map<string, { line: string; where?: string; argType?: string }>();
116
+ for (const h of f.hits) {
117
+ if (h.argType && h.argType !== "number") {
118
+ const key = `argType:${h.argType}`;
119
+ if (!found.has(key)) found.set(key, { line: h.text, where: h.where, argType: h.argType });
120
+ }
121
+ }
122
+ for (const line of assembleLines(f.hits)) {
123
+ const names = TOKENS.filter((tok) => tok.re.test(line.text)).map((tok) => tok.name);
124
+ if (o.fuzzy) for (const z of FUZZY) if (!names.includes(z.name) && fuzzyHas(line.text, z.word, z.dist)) names.push(z.name);
125
+ for (const name of names) {
126
+ const key = `token:${name}`;
127
+ if (allow.has(name) || found.has(key)) continue;
128
+ found.set(key, { line: line.text.slice(0, 120), where: line.hits.find((h) => h.where)?.where });
129
+ }
130
+ }
131
+ for (const [key, v] of found) {
132
+ if (!hitsAt.has(key)) hitsAt.set(key, []);
133
+ hitsAt.get(key)!.push({ t: f.t, ...v });
134
+ }
135
+ }
136
+ const out: Finding[] = [];
137
+ for (const [key, list] of hitsAt) {
138
+ for (const r of runs(list.map((x) => x.t), 1.5 / o.fps)) {
139
+ const first = list.find((x) => x.t >= r.from)!;
140
+ const [kind, name] = key.split(":");
141
+ out.push({
142
+ rule: "text.bad",
143
+ severity: "error",
144
+ from: r.from,
145
+ to: r.to + 1 / o.fps,
146
+ message:
147
+ kind === "argType"
148
+ ? `화면 글에 문자열이 아닌 값(${name})이 들어갔다 — "${first.line}"`
149
+ : `화면 글에 「${name}」 — "${first.line}"`,
150
+ detail: { line: first.line, where: first.where, frames: r.count },
151
+ });
152
+ }
153
+ }
154
+ return out;
155
+ }
156
+
157
+ function primaryFamily(font: string): string {
158
+ const m = /\d(?:\.\d+)?px(?:\/\S+)?\s+("[^"]+"|'[^']+'|[^,]+)/.exec(font);
159
+ return (m?.[1] ?? font).trim().replace(/^["']|["']$/g, "");
160
+ }
161
+
162
+ // 글꼴 크기·굵기만 다른 것은 한 줄로 묶는다 — 고칠 곳은 글꼴 이름 하나다.
163
+ export function glyphProblems(frames: FrameText[], glyphs: Record<string, string>, fps: number): Finding[] {
164
+ const groups = new Map<string, { status: string; family: string; chars: Set<string>; times: number[] }>();
165
+ for (const f of frames) {
166
+ for (const h of f.hits) {
167
+ for (const ch of new Set(h.text)) {
168
+ const st = glyphs[h.font + "\u0000" + ch];
169
+ if (!st || st === "ok" || st === "stack") continue;
170
+ const status = st === "blank" ? "tofu" : st;
171
+ const family = primaryFamily(h.font);
172
+ const key = `${status}\u0000${family}`;
173
+ let g = groups.get(key);
174
+ if (!g) groups.set(key, (g = { status, family, chars: new Set(), times: [] }));
175
+ g.chars.add(ch);
176
+ if (g.times[g.times.length - 1] !== f.t) g.times.push(f.t);
177
+ }
178
+ }
179
+ }
180
+ const out: Finding[] = [];
181
+ for (const g of groups.values()) {
182
+ const chars = [...g.chars];
183
+ const shown = chars.slice(0, 24).join("");
184
+ const span = runs(g.times, 1.5 / fps);
185
+ const from = span[0]?.from ?? 0;
186
+ const to = (span[span.length - 1]?.to ?? 0) + 1 / fps;
187
+ if (g.status === "tofu") {
188
+ out.push({
189
+ rule: "text.tofu",
190
+ severity: "error",
191
+ from,
192
+ to,
193
+ // 두부 글자는 화면에 찍어도 안 보이니 코드 포인트로 알린다.
194
+ message: `어떤 글꼴에도 없는 글자가 두부(빈 네모)로 나온다 — 글꼴 ${g.family} · ${chars
195
+ .slice(0, 12)
196
+ .map((c) => `U+${c.codePointAt(0)!.toString(16).toUpperCase().padStart(4, "0")}`)
197
+ .join(" ")}`,
198
+ detail: { family: g.family, chars },
199
+ });
200
+ continue;
201
+ }
202
+ const latin = g.status === "missing-font";
203
+ const hangul = chars.some((c) => /[\u3131-\u318E\uAC00-\uD7A3]/.test(c));
204
+ out.push({
205
+ rule: "text.fallback",
206
+ severity: "warn",
207
+ from,
208
+ to,
209
+ message: latin
210
+ ? `글꼴 「${g.family}」이 안 불러와져 다른 글꼴로 그렸다 — 이름이 틀렸거나 @font-face·글꼴 파일이 없다 (글자 ${chars.length}개)`
211
+ : `글꼴 「${g.family}」에 없는 글자를 다른 글꼴이 대신 그린다${hangul ? " — 한글이 서는 글꼴을 쓴다" : ""} · 「${shown}」`,
212
+ detail: { family: g.family, chars, notLoaded: latin },
213
+ });
214
+ }
215
+ return out;
216
+ }
217
+
218
+ export interface OffscreenOptions {
219
+ width: number;
220
+ height: number;
221
+ fps: number;
222
+ safe?: SafeArea;
223
+ minSec?: number;
224
+ }
225
+
226
+ // 들어오고 나가는 글은 잠깐 걸쳐도 된다. 같은 글이 minSec 넘게 걸쳐 있으면 잘린 채 머무는 것.
227
+ export function offscreenText(frames: FrameText[], o: OffscreenOptions): Finding[] {
228
+ const minSec = o.minSec ?? 0.5;
229
+ const pad = 2;
230
+ const safe = o.safe && (o.safe.top || o.safe.bottom || o.safe.left || o.safe.right) ? o.safe : null;
231
+ const seen = new Map<string, { kind: "clip" | "safe"; times: number[]; text: string; box: Box; lyrics: boolean }>();
232
+ for (const f of frames) {
233
+ for (const line of assembleLines(f.hits)) {
234
+ const b = line.box;
235
+ if (!b || line.alpha < 0.3) continue;
236
+ const fullyOut = b.x1 <= 0 || b.y1 <= 0 || b.x0 >= o.width || b.y0 >= o.height;
237
+ if (fullyOut) continue;
238
+ const clipped = b.x0 < -pad || b.y0 < -pad || b.x1 > o.width + pad || b.y1 > o.height + pad;
239
+ const unsafe =
240
+ !clipped &&
241
+ safe !== null &&
242
+ (b.x0 < safe.left - pad || b.y0 < safe.top - pad || b.x1 > o.width - safe.right + pad || b.y1 > o.height - safe.bottom + pad);
243
+ if (!clipped && !unsafe) continue;
244
+ const kind = clipped ? "clip" : "safe";
245
+ const lyrics = line.hits.every((h) => h.lyrics);
246
+ const key = `${kind}\u0000${lyrics ? "lyrics" : ""}\u0000${line.text}`;
247
+ let s = seen.get(key);
248
+ if (!s) seen.set(key, (s = { kind, times: [], text: line.text, box: b, lyrics }));
249
+ s.times.push(f.t);
250
+ }
251
+ }
252
+ const out: Finding[] = [];
253
+ for (const s of seen.values()) {
254
+ for (const r of runs(s.times, 1.5 / o.fps)) {
255
+ if (r.to - r.from + 1 / o.fps < minSec) continue;
256
+ // 가사 층의 마퀴 띠·배경 큰 글자는 일부러 화면 끝을 넘긴다 — 잘림은 경고로만(글 내용·두부·대비 판정은 그대로)
257
+ out.push({
258
+ rule: s.kind === "clip" ? "text.offscreen" : "text.safe-area",
259
+ severity: s.kind === "clip" && !s.lyrics ? "error" : "warn",
260
+ from: r.from,
261
+ to: r.to + 1 / o.fps,
262
+ message:
263
+ s.kind === "clip"
264
+ ? `${s.lyrics ? "가사 층 글" : "글"}이 화면 밖으로 잘린 채 ${(r.to - r.from).toFixed(1)}초 머문다 — "${s.text.slice(0, 60)}"${s.lyrics ? " — 띠·배경 장식이면 둬도 된다. 읽혀야 할 가사 줄이면 화면 안으로" : ""}`
265
+ : `글이 안전 영역(플랫폼 UI 가 가리는 자리)에 걸친다 — "${s.text.slice(0, 60)}"`,
266
+ detail: { text: s.text.slice(0, 120), box: roundBox(s.box) },
267
+ });
268
+ }
269
+ }
270
+ return out;
271
+ }
272
+
273
+ export function roundBox(b: Box): Box {
274
+ return { x0: Math.round(b.x0), y0: Math.round(b.y0), x1: Math.round(b.x1), y1: Math.round(b.y1) };
275
+ }
276
+
277
+ export interface TextPolicy {
278
+ minPx?: number;
279
+ maxOnScreen?: number;
280
+ }
281
+
282
+ // 틀(template.json text)이 정한 글 규칙. 한 화면에 글 덩어리가 너무 많거나 작으면 영상에서 안 읽힌다.
283
+ export function textPolicy(frames: FrameText[], policy: TextPolicy, o: { fps: number; minSec?: number }): Finding[] {
284
+ const minSec = o.minSec ?? 1;
285
+ const small = new Map<string, number[]>();
286
+ const crowded: number[] = [];
287
+ let crowdMax = 0;
288
+ for (const f of frames) {
289
+ const lines = assembleLines(f.hits).filter((l) => l.box && l.alpha > 0.5 && /[\p{L}\p{N}]/u.test(l.text));
290
+ // 자막 판은 틀의 화면 글 수(장면이 쓰는 글)와 따로 센다 — 자막을 켰다고 장면 글이 많아진 게 아니다
291
+ const scene = lines.filter((l) => l.hits[0]?.via !== "caption").length;
292
+ if (policy.maxOnScreen && scene > policy.maxOnScreen) {
293
+ crowded.push(f.t);
294
+ crowdMax = Math.max(crowdMax, scene);
295
+ }
296
+ if (policy.minPx) for (const l of lines) if (l.sizePx < policy.minPx * 0.95) (small.get(l.text) ?? small.set(l.text, []).get(l.text)!).push(f.t);
297
+ }
298
+ const out: Finding[] = [];
299
+ for (const r of runs(crowded, 1.5 / o.fps)) {
300
+ if (r.to - r.from < minSec) continue;
301
+ out.push({
302
+ rule: "text.crowded",
303
+ severity: "warn",
304
+ from: r.from,
305
+ to: r.to + 1 / o.fps,
306
+ message: `한 화면에 글 덩어리가 최대 ${crowdMax}개 — 틀은 ${policy.maxOnScreen}개까지 (터미널·코드 화면이면 그대로 둬도 된다)`,
307
+ });
308
+ }
309
+ const smallRuns: { from: number; to: number; text: string }[] = [];
310
+ for (const [text, times] of small) for (const r of runs(times, 1.5 / o.fps)) if (r.to - r.from >= minSec) smallRuns.push({ from: r.from, to: r.to + 1 / o.fps, text });
311
+ if (smallRuns.length) {
312
+ smallRuns.sort((a, b) => a.from - b.from);
313
+ out.push({
314
+ rule: "text.small",
315
+ severity: "warn",
316
+ from: smallRuns[0].from,
317
+ to: Math.max(...smallRuns.map((x) => x.to)),
318
+ message: `틀의 최소 글 크기 ${policy.minPx}px 보다 작은 글 ${smallRuns.length}개 — 예: ${smallRuns
319
+ .slice(0, 4)
320
+ .map((x) => `"${x.text.trim().slice(0, 30)}"`)
321
+ .join(" · ")}`,
322
+ detail: { texts: smallRuns.slice(0, 50).map((x) => ({ text: x.text.slice(0, 60), from: x.from, to: x.to })) },
323
+ });
324
+ }
325
+ return out;
326
+ }