Surf commited on
Commit
1092e04
·
1 Parent(s): b33d80e

字幕视觉系统:编辑式层级与金句动画(推送 #11)

Browse files
src/film/core/film.ts CHANGED
@@ -72,7 +72,8 @@ export class Film {
72
  const t0 = this.vs + (tm ? tm[0] : lastT)
73
  const t1 = this.vs + (tm ? tm[1] : lastT + 0.12)
74
  lastT = tm ? tm[1] : lastT + 0.12
75
- const c: CharT = { ch, i, t0, t1, clause: ci, key: false }
 
76
  chars.push(c)
77
  this.chars.push(c)
78
  }
 
72
  const t0 = this.vs + (tm ? tm[0] : lastT)
73
  const t1 = this.vs + (tm ? tm[1] : lastT + 0.12)
74
  lastT = tm ? tm[1] : lastT + 0.12
75
+ // 关键词强调只作用于视觉层,不改变原文与配音时间轴。
76
+ const c: CharT = { ch, i, t0, t1, clause: ci, key: /[我你他它生活人生路梦钱饭车香蕉刹车命运撑住放下拉扛累难走冲]/.test(ch) }
77
  chars.push(c)
78
  this.chars.push(c)
79
  }
src/film/core/renderer.ts CHANGED
@@ -3,10 +3,13 @@ import type { CharT, Film, PageT, Program } from './film'
3
  import { Post, defaultPost, type PostParams } from './post'
4
  import { FONT } from './assets'
5
  import { drawVideo, type Rect } from './fx'
6
- import { adv, breakClause } from './typo'
7
  import { clamp, ease, pad2, smooth } from './util'
8
 
9
- const SUB = '#F5F2EA'
 
 
 
10
 
11
  /** 一页字幕 → 若干行(同一行的两句之间留一个全角空格) */
12
  function pageLines(p: PageT, perLine: number): CharT[][] {
@@ -22,6 +25,152 @@ function pageLines(p: PageT, perLine: number): CharT[][] {
22
  return p.clauses.flatMap((c) => breakClause(c.chars, perLine))
23
  }
24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  export function videoRect(w: number, h: number): Rect {
26
  if (w >= h) return { x: 0, y: 0, w, h }
27
  const vh = (w * 9) / 16
@@ -29,114 +178,26 @@ export function videoRect(w: number, h: number): Rect {
29
  }
30
 
31
  export function drawFilmFrame(g: CanvasRenderingContext2D, w: number, h: number, f: Film, t: number, video: HTMLVideoElement | null, post: PostParams) {
32
- const P = h > w
33
  const S = Math.min(w, h)
34
  const u = S / 1080
35
  const vr = videoRect(w, h)
36
- g.fillStyle = '#000'
37
  g.fillRect(0, 0, w, h)
38
  drawVideo(g, video, vr, { zoom: 1 })
39
 
40
- // 横屏:底部轻微压暗,托住字幕
41
- if (!P) {
42
- const gr = g.createLinearGradient(0, h * 0.58, 0, h)
43
- gr.addColorStop(0, 'rgba(0,0,0,0)')
44
- gr.addColorStop(1, 'rgba(0,0,0,0.5)')
45
- g.fillStyle = gr
46
- g.fillRect(0, h * 0.58, w, h * 0.42)
47
- }
48
-
49
- g.textBaseline = 'middle'
50
- // 角标:奥德彪语录 · №07
51
- const ba = smooth((t - 0.5) / 0.9) * 0.9
52
- if (ba > 0) {
53
- g.save()
54
- g.globalAlpha = ba
55
- g.fillStyle = '#fff'
56
- g.shadowColor = 'rgba(0,0,0,0.55)'
57
- g.shadowBlur = 12 * u
58
- const bs = S * (P ? 0.034 : 0.024)
59
- const title = '奥德彪语录'
60
- const tr = 0.42
61
- g.font = `500 ${bs}px ${FONT.serif}`
62
- const tw = [...title].reduce((s, c) => s + adv(g, FONT.serif, 500, c) * bs, 0) + tr * bs * (title.length - 1)
63
- const x0 = P ? w / 2 - tw / 2 : w * 0.045
64
- const y0 = P ? vr.y - S * 0.2 : h * 0.075
65
- g.textAlign = 'left'
66
- let x = x0
67
- for (const c of title) { g.fillText(c, x, y0); x += adv(g, FONT.serif, 500, c) * bs + tr * bs }
68
- g.globalAlpha = ba * 0.7
69
- g.fillRect(P ? w / 2 - 14 * u : x0, y0 + bs * 1.05, 28 * u, Math.max(1, 1.2 * u))
70
- g.font = `400 ${bs * 0.86}px ${FONT.latin}`
71
- g.textAlign = P ? 'center' : 'left'
72
- g.fillText(`№ ${pad2(f.id)}`, P ? w / 2 : x0, y0 + bs * 1.9)
73
- g.restore()
74
- }
75
-
76
- // 字幕
77
- const page = f.pageAt(t)
78
- const size = P ? S * 0.062 : S * 0.052
79
- const track = 0.12
80
- const lineH = 1.55
81
- const baseY = P ? vr.y + vr.h + S * 0.2 : h * 0.86
82
- let blockBottom = baseY
83
- if (page) {
84
- const a = page.last ? smooth((t - page.t0) / 0.35) : Math.min(smooth((t - page.t0) / 0.35), smooth((page.t1 - t) / 0.28))
85
- if (a > 0) {
86
- const lines = pageLines(page, P ? 12 : 18)
87
- const fam = FONT.serif
88
- const wt = 500
89
- const widths = lines.map((l) => l.reduce((s, c) => s + adv(g, fam, wt, c.ch), 0) + track * (l.length - 1))
90
- const fs = Math.min(size, (w * (P ? 0.86 : 0.82)) / Math.max(...widths))
91
- const n = lines.length
92
- const top = P ? baseY - (fs * lineH * (n - 1)) / 2 : baseY - fs * lineH * (n - 1)
93
- const rise = (1 - ease.outCubic(clamp((t - page.t0) / 0.5))) * fs * 0.18
94
- g.save()
95
- g.globalAlpha = a
96
- g.font = `${wt} ${fs}px ${fam}`
97
- g.fillStyle = SUB
98
- g.textAlign = 'left'
99
- g.shadowColor = 'rgba(0,0,0,0.65)'
100
- g.shadowBlur = 14 * u
101
- g.shadowOffsetY = 1.5 * u
102
- lines.forEach((l, li) => {
103
- let x = w / 2 - (widths[li] * fs) / 2
104
- const y = top + li * fs * lineH + rise
105
- for (const c of l) {
106
- if (c.ch !== ' ') g.fillText(c.ch, x, y)
107
- x += adv(g, fam, wt, c.ch) * fs + track * fs
108
- }
109
- })
110
- g.restore()
111
- blockBottom = top + (n - 1) * fs * lineH + fs * 0.5
112
- }
113
- }
114
 
115
- // 英文译文(配音结束后浮现)
116
- const ea = smooth((t - f.ve - 0.3) / 0.9)
117
- if (f.en && ea > 0) {
118
- const es = S * (P ? 0.036 : 0.028)
119
- g.save()
120
- g.globalAlpha = ea * 0.8
121
- g.font = `italic 400 ${es}px ${FONT.latinItalic}`
122
- g.fillStyle = SUB
123
- g.textAlign = 'center'
124
- g.shadowColor = 'rgba(0,0,0,0.6)'
125
- g.shadowBlur = 10 * u
126
- const maxW = w * (P ? 0.84 : 0.7)
127
- const lines: string[] = []
128
- let cur = ''
129
- for (const wd of f.en.split(' ')) {
130
- const test = cur ? cur + ' ' + wd : wd
131
- if (g.measureText(test).width > maxW && cur) { lines.push(cur); cur = wd } else cur = test
132
- }
133
- if (cur) lines.push(cur)
134
- const y0 = P ? blockBottom + es * 1.6 : Math.min(blockBottom + es * 1.5, h - es * 1.2 * lines.length)
135
- lines.slice(0, 3).forEach((l, i) => g.fillText(l, w / 2, y0 + i * es * 1.35))
136
- g.restore()
137
- }
138
 
139
- // 淡入淡出
140
  const fin = smooth(t / 0.8)
141
  const fout = smooth((f.T - t) / 0.9)
142
  post.fade = 1 - Math.min(fin, fout)
 
3
  import { Post, defaultPost, type PostParams } from './post'
4
  import { FONT } from './assets'
5
  import { drawVideo, type Rect } from './fx'
6
+ import { adv, breakClause, drawChar, placeTrackedLine, txt } from './typo'
7
  import { clamp, ease, pad2, smooth } from './util'
8
 
9
+ const SUB = '#F6F0E6'
10
+ const GOLD = '#F5C518'
11
+ const MUTED = '#C7BDAA'
12
+ const INK = '#15110D'
13
 
14
  /** 一页字幕 → 若干行(同一行的两句之间留一个全角空格) */
15
  function pageLines(p: PageT, perLine: number): CharT[][] {
 
25
  return p.clauses.flatMap((c) => breakClause(c.chars, perLine))
26
  }
27
 
28
+ function rule(g: CanvasRenderingContext2D, x: number, y: number, w: number, p: number, color: string, width: number) {
29
+ g.save()
30
+ g.globalAlpha *= clamp(p)
31
+ g.strokeStyle = color
32
+ g.lineWidth = width
33
+ g.beginPath()
34
+ g.moveTo(x, y)
35
+ g.lineTo(x + w * ease.outCubic(p), y)
36
+ g.stroke()
37
+ g.restore()
38
+ }
39
+
40
+ /** 纪录片式角标:用留白、细线和两套字体建立字幕的编辑系统。 */
41
+ function drawEditorialMeta(g: CanvasRenderingContext2D, w: number, h: number, f: Film, t: number, portrait: boolean, u: number) {
42
+ const a = smooth((t - 0.35) / 0.75)
43
+ if (a <= 0) return
44
+ const x = portrait ? w * 0.1 : w * 0.055
45
+ const y = portrait ? h * 0.075 : h * 0.09
46
+ const s = Math.min(w, h)
47
+ g.save()
48
+ g.globalAlpha = a
49
+ g.shadowColor = 'rgba(0,0,0,0.5)'
50
+ g.shadowBlur = 12 * u
51
+ g.fillStyle = SUB
52
+ g.textAlign = 'left'
53
+ txt(g, 'ODEBIAO / FIELD NOTES', x, y - s * 0.018, `400 ${s * (portrait ? 0.018 : 0.014)}px ${FONT.mono}`, MUTED, 'left', 0.08)
54
+ txt(g, f.chapter, x, y + s * 0.024, `500 ${s * (portrait ? 0.032 : 0.026)}px ${FONT.serif}`, SUB, 'left', 0.12)
55
+ rule(g, x, y + s * 0.062, s * (portrait ? 0.19 : 0.12), 1, GOLD, Math.max(1, u * 1.5))
56
+ txt(g, `№ ${pad2(f.id)} / 43`, w - (portrait ? w * 0.1 : w * 0.055), y, `400 ${s * (portrait ? 0.019 : 0.014)}px ${FONT.mono}`, MUTED, 'right', 0.08)
57
+ g.restore()
58
+ }
59
+
60
+ /** 绘制一页“编辑设计”字幕:主句、关键词、细框与章节信息各自有层级。 */
61
+ function drawEditorialSubtitle(g: CanvasRenderingContext2D, w: number, h: number, f: Film, t: number, portrait: boolean, u: number): number {
62
+ const page = f.pageAt(t)
63
+ if (!page) return portrait ? h * 0.82 : h * 0.84
64
+ const S = Math.min(w, h)
65
+ const lines = pageLines(page, portrait ? 11 : 18)
66
+ const fam = FONT.serif
67
+ const wt = 500
68
+ const track = portrait ? 0.075 : 0.09
69
+ const maxW = w * (portrait ? 0.79 : 0.68)
70
+ const initial = S * (portrait ? 0.058 : 0.054)
71
+ const widths = lines.map((l) => l.reduce((sum, c) => sum + adv(g, fam, wt, c.ch), 0) + track * Math.max(0, l.length - 1))
72
+ const fs = Math.max(S * (portrait ? 0.035 : 0.032), Math.min(initial, maxW / Math.max(...widths)))
73
+ const lh = portrait ? 1.38 : 1.34
74
+ const left = portrait ? w * 0.1 : w * 0.105
75
+ const baseY = portrait ? h * 0.83 : h * 0.845
76
+ const blockH = fs * (1 + (lines.length - 1) * lh)
77
+ const top = baseY - blockH + fs * 0.5
78
+ const a = page.last ? smooth((t - page.t0) / 0.34) : Math.min(smooth((t - page.t0) / 0.34), smooth((page.t1 - t) / 0.34))
79
+ if (a <= 0) return baseY
80
+
81
+ const punch = page.last
82
+ const frameX = left - fs * 0.9
83
+ const frameY = top - fs * 1.3
84
+ const frameW = Math.min(maxW + fs * 1.8, w - frameX - (portrait ? w * 0.08 : w * 0.07))
85
+ const frameH = blockH + fs * 1.65
86
+ const frameP = punch ? ease.outCubic((t - page.t0) / 0.75) : 0
87
+
88
+ g.save()
89
+ g.globalAlpha = a
90
+ // 一根永远比色块更轻的“编辑线”,把字幕从画面里托起来。
91
+ g.strokeStyle = GOLD
92
+ g.lineWidth = Math.max(1, u * 2)
93
+ g.beginPath()
94
+ g.moveTo(frameX, frameY)
95
+ g.lineTo(frameX, frameY + frameH * (punch ? frameP : 0.7))
96
+ g.stroke()
97
+ if (punch) {
98
+ g.globalAlpha = a * 0.72
99
+ g.strokeStyle = `rgba(245,197,24,${0.68 * frameP})`
100
+ g.lineWidth = Math.max(1, u)
101
+ g.strokeRect(frameX + fs * 0.55, frameY + fs * 0.38, frameW - fs * 0.55, frameH - fs * 0.62)
102
+ g.globalAlpha = a * 0.16
103
+ g.fillStyle = GOLD
104
+ g.font = `italic 400 ${fs * 2.8}px ${FONT.latinItalic}`
105
+ g.textAlign = 'left'
106
+ g.fillText('“', frameX + fs * 0.12, frameY + fs * 0.42)
107
+ g.textAlign = 'right'
108
+ g.fillText('”', frameX + frameW - fs * 0.04, frameY + frameH - fs * 0.1)
109
+ }
110
+ g.restore()
111
+
112
+ // 章节名是“字幕卡”的题头,不与主句抢重量。
113
+ g.save()
114
+ g.globalAlpha = a * 0.82
115
+ txt(g, punch ? 'THE LINE / 金句' : 'VOICE / 旁白', left, top - fs * 0.72, `400 ${Math.max(10, fs * 0.27)}px ${FONT.mono}`, punch ? GOLD : MUTED, 'left', 0.12)
116
+ txt(g, `0${page.index + 1}`, left + fs * 3.7, top - fs * 0.72, `400 ${Math.max(10, fs * 0.27)}px ${FONT.mono}`, MUTED, 'left', 0.08)
117
+ rule(g, left + fs * 5.15, top - fs * 0.72, Math.min(fs * 4.5, maxW * 0.25), (t - page.t0) / 0.7, punch ? GOLD : MUTED, Math.max(1, u))
118
+ g.restore()
119
+
120
+ g.save()
121
+ g.globalAlpha = a
122
+ lines.forEach((line, li) => {
123
+ const y = top + fs * li * lh
124
+ const placed = placeTrackedLine(g, fam, wt, line, fs, track, left, y, 'left', li)
125
+ placed.forEach((p) => {
126
+ drawChar(g, p, t, punch ? 'gold' : 'blur', {
127
+ family: fam,
128
+ weight: wt,
129
+ fill: SUB,
130
+ keyFill: GOLD,
131
+ shadow: 'rgba(0,0,0,0.65)',
132
+ shadowBlur: 13 * u,
133
+ dur: punch ? 0.32 : 0.4,
134
+ lead: 0.04,
135
+ })
136
+ if (p.c.key && p.c.ch !== ' ') {
137
+ const k = clamp((t - p.c.t0 + 0.04) / (punch ? 0.42 : 0.55))
138
+ if (k > 0.72) rule(g, p.x, y + fs * 0.47, p.w, (k - 0.72) / 0.28, GOLD, Math.max(1, u * 1.2))
139
+ }
140
+ })
141
+ })
142
+ g.restore()
143
+
144
+ return top + blockH + fs * 0.18
145
+ }
146
+
147
+ function drawEnglishAfterglow(g: CanvasRenderingContext2D, w: number, h: number, f: Film, t: number, bottom: number, portrait: boolean, u: number) {
148
+ const a = smooth((t - f.ve - 0.35) / 0.9)
149
+ if (!f.en || a <= 0) return
150
+ const S = Math.min(w, h)
151
+ const es = S * (portrait ? 0.027 : 0.022)
152
+ const x = portrait ? w * 0.1 : w * 0.105
153
+ const maxW = w * (portrait ? 0.79 : 0.68)
154
+ g.save()
155
+ g.globalAlpha = a * 0.76
156
+ g.fillStyle = MUTED
157
+ g.shadowColor = 'rgba(0,0,0,0.55)'
158
+ g.shadowBlur = 10 * u
159
+ rule(g, x, bottom + es * 0.72, Math.min(maxW, S * 0.18), a, GOLD, Math.max(1, u))
160
+ g.font = `italic 400 ${es}px ${FONT.latinItalic}`
161
+ g.textAlign = 'left'
162
+ const lines: string[] = []
163
+ let cur = ''
164
+ for (const word of f.en.split(' ')) {
165
+ const test = cur ? `${cur} ${word}` : word
166
+ if (g.measureText(test).width > maxW && cur) { lines.push(cur); cur = word } else cur = test
167
+ }
168
+ if (cur) lines.push(cur)
169
+ const y = Math.min(bottom + es * 1.45, h - es * Math.min(1.2 * lines.length, 2.7))
170
+ lines.slice(0, 3).forEach((line, i) => g.fillText(line, x, y + i * es * 1.35))
171
+ g.restore()
172
+ }
173
+
174
  export function videoRect(w: number, h: number): Rect {
175
  if (w >= h) return { x: 0, y: 0, w, h }
176
  const vh = (w * 9) / 16
 
178
  }
179
 
180
  export function drawFilmFrame(g: CanvasRenderingContext2D, w: number, h: number, f: Film, t: number, video: HTMLVideoElement | null, post: PostParams) {
181
+ const portrait = h > w
182
  const S = Math.min(w, h)
183
  const u = S / 1080
184
  const vr = videoRect(w, h)
185
+ g.fillStyle = INK
186
  g.fillRect(0, 0, w, h)
187
  drawVideo(g, video, vr, { zoom: 1 })
188
 
189
+ // 画面下方的柔性暗部让字幕“落”在画面上,而不是贴在画面之上。
190
+ const shade = g.createLinearGradient(0, portrait ? h * 0.52 : h * 0.54, 0, h)
191
+ shade.addColorStop(0, 'rgba(0,0,0,0)')
192
+ shade.addColorStop(1, portrait ? 'rgba(7,5,3,0.72)' : 'rgba(7,5,3,0.62)')
193
+ g.fillStyle = shade
194
+ g.fillRect(0, portrait ? h * 0.52 : h * 0.54, w, h * 0.48)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
195
 
196
+ drawEditorialMeta(g, w, h, f, t, portrait, u)
197
+ const bottom = drawEditorialSubtitle(g, w, h, f, t, portrait, u)
198
+ drawEnglishAfterglow(g, w, h, f, t, bottom, portrait, u)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
199
 
200
+ // 淡入淡出仍然由同一条时间线控制,导出与播放保持一致。
201
  const fin = smooth(t / 0.8)
202
  const fout = smooth((f.T - t) / 0.9)
203
  post.fade = 1 - Math.min(fin, fout)
src/film/core/typo.ts CHANGED
@@ -51,6 +51,29 @@ export function lineWidth(g: CanvasRenderingContext2D, family: string, weight: n
51
  return w
52
  }
53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  export interface LayoutOpts {
55
  family: string
56
  weight: number
 
51
  return w
52
  }
53
 
54
+ /** 把一行按编辑排版的 tracking 放入安全区,供播放与导出共用。 */
55
+ export function placeTrackedLine(
56
+ g: CanvasRenderingContext2D,
57
+ family: string,
58
+ weight: number,
59
+ line: CharT[],
60
+ size: number,
61
+ tracking: number,
62
+ x: number,
63
+ y: number,
64
+ align: 'left' | 'center' = 'left',
65
+ lineNo = 0,
66
+ ): Placed[] {
67
+ const total = lineWidth(g, family, weight, line, size, tracking)
68
+ let xx = align === 'center' ? x - total / 2 : x
69
+ return line.map((c) => {
70
+ const w = adv(g, family, weight, c.ch) * size
71
+ const p: Placed = { c, x: xx, y, w, size, line: lineNo }
72
+ xx += w + tracking * size
73
+ return p
74
+ })
75
+ }
76
+
77
  export interface LayoutOpts {
78
  family: string
79
  weight: number