@meframe/core 0.6.12 → 0.7.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.
Files changed (199) hide show
  1. package/CHANGELOG.md +55 -0
  2. package/README.md +17 -5
  3. package/dist/Meframe.d.ts +14 -3
  4. package/dist/Meframe.d.ts.map +1 -1
  5. package/dist/Meframe.js +22 -12
  6. package/dist/Meframe.js.map +1 -1
  7. package/dist/cache/CacheManager.d.ts +2 -0
  8. package/dist/cache/CacheManager.d.ts.map +1 -1
  9. package/dist/cache/CacheManager.js +5 -0
  10. package/dist/cache/CacheManager.js.map +1 -1
  11. package/dist/cache/l1/VideoL1Cache.d.ts +2 -0
  12. package/dist/cache/l1/VideoL1Cache.d.ts.map +1 -1
  13. package/dist/cache/l1/VideoL1Cache.js +13 -7
  14. package/dist/cache/l1/VideoL1Cache.js.map +1 -1
  15. package/dist/cache/resource/ResourceCache.d.ts +0 -12
  16. package/dist/cache/resource/ResourceCache.d.ts.map +1 -1
  17. package/dist/cache/resource/ResourceCache.js +0 -24
  18. package/dist/cache/resource/ResourceCache.js.map +1 -1
  19. package/dist/config/defaults.d.ts.map +1 -1
  20. package/dist/config/defaults.js +4 -1
  21. package/dist/config/defaults.js.map +1 -1
  22. package/dist/config/index.d.ts +1 -1
  23. package/dist/config/index.d.ts.map +1 -1
  24. package/dist/config/types.d.ts +29 -0
  25. package/dist/config/types.d.ts.map +1 -1
  26. package/dist/config/validation.d.ts.map +1 -1
  27. package/dist/config/validation.js +3 -0
  28. package/dist/config/validation.js.map +1 -1
  29. package/dist/controllers/PlaybackController.d.ts +38 -9
  30. package/dist/controllers/PlaybackController.d.ts.map +1 -1
  31. package/dist/controllers/PlaybackController.js +269 -154
  32. package/dist/controllers/PlaybackController.js.map +1 -1
  33. package/dist/controllers/PlaybackStateMachine.d.ts.map +1 -1
  34. package/dist/controllers/PlaybackStateMachine.js +2 -1
  35. package/dist/controllers/PlaybackStateMachine.js.map +1 -1
  36. package/dist/controllers/types.d.ts +10 -2
  37. package/dist/controllers/types.d.ts.map +1 -1
  38. package/dist/controllers/types.js.map +1 -1
  39. package/dist/event/events.d.ts +2 -0
  40. package/dist/event/events.d.ts.map +1 -1
  41. package/dist/event/events.js.map +1 -1
  42. package/dist/index.d.ts +2 -2
  43. package/dist/index.d.ts.map +1 -1
  44. package/dist/index.js +1 -1
  45. package/dist/index.js.map +1 -1
  46. package/dist/model/CompositionModel.d.ts +0 -1
  47. package/dist/model/CompositionModel.d.ts.map +1 -1
  48. package/dist/model/CompositionModel.js +0 -6
  49. package/dist/model/CompositionModel.js.map +1 -1
  50. package/dist/model/patch.js +12 -12
  51. package/dist/model/patch.js.map +1 -1
  52. package/dist/node_modules/.pnpm/{mp4-muxer@5.2.2 → mp4-muxer@5.2.2_patch_hash_452653ac44da4ef32d244694e500e34d084eaa3c4232515ddec962eacc5f30ea}/node_modules/mp4-muxer/build/mp4-muxer.js +17 -3
  53. package/dist/node_modules/.pnpm/mp4-muxer@5.2.2_patch_hash_452653ac44da4ef32d244694e500e34d084eaa3c4232515ddec962eacc5f30ea/node_modules/mp4-muxer/build/mp4-muxer.js.map +1 -0
  54. package/dist/node_modules/.pnpm/mp4box@0.5.4/node_modules/mp4box/dist/mp4box.all.js +36 -12
  55. package/dist/node_modules/.pnpm/mp4box@0.5.4/node_modules/mp4box/dist/mp4box.all.js.map +1 -1
  56. package/dist/orchestrator/AudioExportSession.d.ts +28 -3
  57. package/dist/orchestrator/AudioExportSession.d.ts.map +1 -1
  58. package/dist/orchestrator/AudioExportSession.js +131 -15
  59. package/dist/orchestrator/AudioExportSession.js.map +1 -1
  60. package/dist/orchestrator/AudioPreviewSession.d.ts +6 -1
  61. package/dist/orchestrator/AudioPreviewSession.d.ts.map +1 -1
  62. package/dist/orchestrator/AudioPreviewSession.js +57 -18
  63. package/dist/orchestrator/AudioPreviewSession.js.map +1 -1
  64. package/dist/orchestrator/AudioWindowPreparer.d.ts +9 -7
  65. package/dist/orchestrator/AudioWindowPreparer.d.ts.map +1 -1
  66. package/dist/orchestrator/AudioWindowPreparer.js +111 -36
  67. package/dist/orchestrator/AudioWindowPreparer.js.map +1 -1
  68. package/dist/orchestrator/CompositionPlanner.d.ts +5 -2
  69. package/dist/orchestrator/CompositionPlanner.d.ts.map +1 -1
  70. package/dist/orchestrator/CompositionPlanner.js +8 -3
  71. package/dist/orchestrator/CompositionPlanner.js.map +1 -1
  72. package/dist/orchestrator/ExportScheduler.d.ts +1 -0
  73. package/dist/orchestrator/ExportScheduler.d.ts.map +1 -1
  74. package/dist/orchestrator/ExportScheduler.js +177 -106
  75. package/dist/orchestrator/ExportScheduler.js.map +1 -1
  76. package/dist/orchestrator/Orchestrator.d.ts +30 -4
  77. package/dist/orchestrator/Orchestrator.d.ts.map +1 -1
  78. package/dist/orchestrator/Orchestrator.js +264 -111
  79. package/dist/orchestrator/Orchestrator.js.map +1 -1
  80. package/dist/orchestrator/PreviewFrameScheduler.d.ts +21 -1
  81. package/dist/orchestrator/PreviewFrameScheduler.d.ts.map +1 -1
  82. package/dist/orchestrator/PreviewFrameScheduler.js +129 -30
  83. package/dist/orchestrator/PreviewFrameScheduler.js.map +1 -1
  84. package/dist/orchestrator/VideoWindowDecodeSession.d.ts +22 -1
  85. package/dist/orchestrator/VideoWindowDecodeSession.d.ts.map +1 -1
  86. package/dist/orchestrator/VideoWindowDecodeSession.js +426 -198
  87. package/dist/orchestrator/VideoWindowDecodeSession.js.map +1 -1
  88. package/dist/orchestrator/aac-window-timeline.d.ts +7 -2
  89. package/dist/orchestrator/aac-window-timeline.d.ts.map +1 -1
  90. package/dist/orchestrator/aac-window-timeline.js +19 -9
  91. package/dist/orchestrator/aac-window-timeline.js.map +1 -1
  92. package/dist/orchestrator/types.d.ts +5 -1
  93. package/dist/orchestrator/types.d.ts.map +1 -1
  94. package/dist/stages/compose/FrameRateConverter.d.ts +2 -1
  95. package/dist/stages/compose/FrameRateConverter.d.ts.map +1 -1
  96. package/dist/stages/compose/LayerRenderPolicy.d.ts +14 -0
  97. package/dist/stages/compose/LayerRenderPolicy.d.ts.map +1 -0
  98. package/dist/stages/compose/LayerRenderPolicy.js +18 -0
  99. package/dist/stages/compose/LayerRenderPolicy.js.map +1 -0
  100. package/dist/stages/compose/OfflineAudioMixer.d.ts +2 -0
  101. package/dist/stages/compose/OfflineAudioMixer.d.ts.map +1 -1
  102. package/dist/stages/compose/OfflineAudioMixer.js +14 -2
  103. package/dist/stages/compose/OfflineAudioMixer.js.map +1 -1
  104. package/dist/stages/compose/OwnedFrameTransform.d.ts +52 -0
  105. package/dist/stages/compose/OwnedFrameTransform.d.ts.map +1 -0
  106. package/dist/stages/compose/OwnedFrameTransform.js +154 -0
  107. package/dist/stages/compose/OwnedFrameTransform.js.map +1 -0
  108. package/dist/stages/compose/TransitionProcessor.js +0 -1
  109. package/dist/stages/compose/TransitionProcessor.js.map +1 -1
  110. package/dist/stages/compose/VideoComposer.d.ts +12 -2
  111. package/dist/stages/compose/VideoComposer.d.ts.map +1 -1
  112. package/dist/stages/compose/VideoComposer.js +100 -92
  113. package/dist/stages/compose/VideoComposer.js.map +1 -1
  114. package/dist/stages/compose/text-renderers/basic-text-renderer.d.ts.map +1 -1
  115. package/dist/stages/compose/text-renderers/basic-text-renderer.js +24 -21
  116. package/dist/stages/compose/text-renderers/basic-text-renderer.js.map +1 -1
  117. package/dist/stages/compose/text-renderers/caption-stagger-entrance-renderer.d.ts.map +1 -1
  118. package/dist/stages/compose/text-renderers/caption-stagger-entrance-renderer.js +110 -99
  119. package/dist/stages/compose/text-renderers/caption-stagger-entrance-renderer.js.map +1 -1
  120. package/dist/stages/compose/text-renderers/character-ktv-renderer.d.ts.map +1 -1
  121. package/dist/stages/compose/text-renderers/character-ktv-renderer.js +41 -38
  122. package/dist/stages/compose/text-renderers/character-ktv-renderer.js.map +1 -1
  123. package/dist/stages/compose/text-renderers/word-by-word-renderer.d.ts.map +1 -1
  124. package/dist/stages/compose/text-renderers/word-by-word-renderer.js +47 -39
  125. package/dist/stages/compose/text-renderers/word-by-word-renderer.js.map +1 -1
  126. package/dist/stages/compose/text-renderers/word-fancy-renderer.d.ts.map +1 -1
  127. package/dist/stages/compose/text-renderers/word-fancy-renderer.js +54 -48
  128. package/dist/stages/compose/text-renderers/word-fancy-renderer.js.map +1 -1
  129. package/dist/stages/compose/text-utils/text-metrics.d.ts.map +1 -1
  130. package/dist/stages/compose/text-utils/text-metrics.js +6 -4
  131. package/dist/stages/compose/text-utils/text-metrics.js.map +1 -1
  132. package/dist/stages/compose/types.d.ts +12 -0
  133. package/dist/stages/compose/types.d.ts.map +1 -1
  134. package/dist/stages/decode/AudioChunkDecoder.d.ts.map +1 -1
  135. package/dist/stages/decode/AudioChunkDecoder.js +2 -1
  136. package/dist/stages/decode/AudioChunkDecoder.js.map +1 -1
  137. package/dist/stages/decode/BaseDecoder.d.ts +5 -0
  138. package/dist/stages/decode/BaseDecoder.d.ts.map +1 -1
  139. package/dist/stages/decode/BaseDecoder.js +52 -10
  140. package/dist/stages/decode/BaseDecoder.js.map +1 -1
  141. package/dist/stages/decode/preview-video-decoder.d.ts +48 -2
  142. package/dist/stages/decode/preview-video-decoder.d.ts.map +1 -1
  143. package/dist/stages/decode/preview-video-decoder.js +241 -71
  144. package/dist/stages/decode/preview-video-decoder.js.map +1 -1
  145. package/dist/stages/decode/video-decoder.d.ts +2 -11
  146. package/dist/stages/decode/video-decoder.d.ts.map +1 -1
  147. package/dist/stages/decode/video-decoder.js +60 -61
  148. package/dist/stages/decode/video-decoder.js.map +1 -1
  149. package/dist/stages/demux/MP4Demuxer.d.ts +1 -0
  150. package/dist/stages/demux/MP4Demuxer.d.ts.map +1 -1
  151. package/dist/stages/demux/MP4Demuxer.js +4 -1
  152. package/dist/stages/demux/MP4Demuxer.js.map +1 -1
  153. package/dist/stages/demux/MP4IndexParser.d.ts.map +1 -1
  154. package/dist/stages/demux/MP4IndexParser.js +11 -3
  155. package/dist/stages/demux/MP4IndexParser.js.map +1 -1
  156. package/dist/stages/encode/BaseEncoder.d.ts +9 -2
  157. package/dist/stages/encode/BaseEncoder.d.ts.map +1 -1
  158. package/dist/stages/encode/BaseEncoder.js +90 -59
  159. package/dist/stages/encode/BaseEncoder.js.map +1 -1
  160. package/dist/stages/load/ResourceLoader.d.ts +69 -8
  161. package/dist/stages/load/ResourceLoader.d.ts.map +1 -1
  162. package/dist/stages/load/ResourceLoader.js +248 -67
  163. package/dist/stages/load/ResourceLoader.js.map +1 -1
  164. package/dist/stages/load/TaskManager.d.ts.map +1 -1
  165. package/dist/stages/load/TaskManager.js +9 -3
  166. package/dist/stages/load/TaskManager.js.map +1 -1
  167. package/dist/stages/load/types.d.ts +2 -2
  168. package/dist/stages/load/types.d.ts.map +1 -1
  169. package/dist/stages/mux/MP4Muxer.d.ts +7 -1
  170. package/dist/stages/mux/MP4Muxer.d.ts.map +1 -1
  171. package/dist/stages/mux/MP4Muxer.js +8 -10
  172. package/dist/stages/mux/MP4Muxer.js.map +1 -1
  173. package/dist/stages/mux/MuxManager.d.ts +7 -0
  174. package/dist/stages/mux/MuxManager.d.ts.map +1 -1
  175. package/dist/stages/mux/MuxManager.js +9 -0
  176. package/dist/stages/mux/MuxManager.js.map +1 -1
  177. package/dist/types.d.ts +1 -1
  178. package/dist/types.d.ts.map +1 -1
  179. package/dist/utils/errors.d.ts +2 -0
  180. package/dist/utils/errors.d.ts.map +1 -1
  181. package/dist/utils/errors.js +10 -1
  182. package/dist/utils/errors.js.map +1 -1
  183. package/dist/utils/mp4box.js +2 -2
  184. package/dist/utils/mp4box.js.map +1 -1
  185. package/dist/utils/until-aborted.d.ts +8 -0
  186. package/dist/utils/until-aborted.d.ts.map +1 -0
  187. package/dist/utils/until-aborted.js +33 -0
  188. package/dist/utils/until-aborted.js.map +1 -0
  189. package/dist/worker/WorkerChannel.d.ts.map +1 -1
  190. package/dist/worker/WorkerChannel.js +45 -10
  191. package/dist/worker/WorkerChannel.js.map +1 -1
  192. package/dist/worker/worker-manifest.js +1 -1
  193. package/dist/worker/worker-manifest.js.map +1 -1
  194. package/dist/workers/stages/export/{export.worker.BepPfuIx.js → export.worker.BG8HA7dY.js} +745 -450
  195. package/dist/workers/stages/export/export.worker.BG8HA7dY.js.map +1 -0
  196. package/dist/workers/worker-manifest.json +1 -1
  197. package/package.json +22 -23
  198. package/dist/node_modules/.pnpm/mp4-muxer@5.2.2/node_modules/mp4-muxer/build/mp4-muxer.js.map +0 -1
  199. package/dist/workers/stages/export/export.worker.BepPfuIx.js.map +0 -1
@@ -91,28 +91,31 @@ function buildCharSlots(ctx, layer, canvasWidth, canvasHeight) {
91
91
  layer.fontConfig?.globalPosition
92
92
  );
93
93
  ctx.save();
94
- ctx.font = `${fontWeight} ${fontSize}px ${fontFamily}`;
95
- const slots = [];
96
- let globalIndex = 0;
97
- for (let lineIndex = 0; lineIndex < lines.length; lineIndex++) {
98
- const line = lines[lineIndex];
99
- const y = startY + lineIndex * fontSize * lineHeight + fontSize / 2;
100
- const lineWidth = measureTextWidth(ctx, line, fontSize, fontFamily, fontWeight);
101
- let cx = canvasWidth / 2 - lineWidth / 2;
102
- for (const ch of Array.from(line)) {
103
- const cw = measureTextWidth(ctx, ch, fontSize, fontFamily, fontWeight);
104
- slots.push({
105
- ch,
106
- x: cx + cw / 2,
107
- y,
108
- globalIndex
109
- });
110
- globalIndex++;
111
- cx += cw;
94
+ try {
95
+ ctx.font = `${fontWeight} ${fontSize}px ${fontFamily}`;
96
+ const slots = [];
97
+ let globalIndex = 0;
98
+ for (let lineIndex = 0; lineIndex < lines.length; lineIndex++) {
99
+ const line = lines[lineIndex];
100
+ const y = startY + lineIndex * fontSize * lineHeight + fontSize / 2;
101
+ const lineWidth = measureTextWidth(ctx, line, fontSize, fontFamily, fontWeight);
102
+ let cx = canvasWidth / 2 - lineWidth / 2;
103
+ for (const ch of Array.from(line)) {
104
+ const cw = measureTextWidth(ctx, ch, fontSize, fontFamily, fontWeight);
105
+ slots.push({
106
+ ch,
107
+ x: cx + cw / 2,
108
+ y,
109
+ globalIndex
110
+ });
111
+ globalIndex++;
112
+ cx += cw;
113
+ }
112
114
  }
115
+ return { slots, fontSize, lineHeight };
116
+ } finally {
117
+ ctx.restore();
113
118
  }
114
- ctx.restore();
115
- return { slots, fontSize, lineHeight };
116
119
  }
117
120
  function getCachedCharSlots(ctx, layer, canvasWidth, canvasHeight) {
118
121
  const key = layoutCacheKey(layer, canvasWidth, canvasHeight);
@@ -138,88 +141,96 @@ function drawStaggerEntranceFrame(ctx, layer, built, relativeFrame, fps, preset,
138
141
  const staggerMs = DEFAULT_STAGGER_MS * scale;
139
142
  const charDurationMs = DEFAULT_DURATION_MS * scale;
140
143
  ctx.save();
141
- ctx.font = `${fontWeight} ${fontSize}px ${fontFamily}`;
142
- ctx.textAlign = "center";
143
- ctx.textBaseline = "middle";
144
- ctx.lineJoin = "round";
145
- ctx.lineCap = "round";
146
- const tMsGlobal = relativeFrame / fps * 1e3;
147
- for (const slot of slots) {
148
- let p;
149
- if (forceComplete) {
150
- p = 1;
151
- } else if (preset === "typewriter") {
152
- const startMs = slot.globalIndex * staggerMs;
153
- p = tMsGlobal >= startMs ? 1 : 0;
154
- } else {
155
- p = charProgress(relativeFrame, fps, slot.globalIndex, staggerMs, charDurationMs);
156
- }
157
- if (p <= 0 && preset !== "blur") continue;
158
- const slidePx = SLIDE_BASE_PX * scalePx;
159
- const spreadPx = LETTER_SPREAD_BASE_PX * scalePx;
160
- ctx.save();
161
- let opacity = p;
162
- let tx = 0;
163
- let ty = 0;
164
- let rot = 0;
165
- let sc = 1;
166
- let sy = 1;
167
- let blurPx = 0;
168
- switch (preset) {
169
- case "fade":
170
- opacity = p;
171
- break;
172
- case "slideUp":
173
- opacity = p;
174
- ty = (1 - p) * slidePx;
175
- break;
176
- case "scale":
177
- opacity = p;
178
- sc = Math.max(0.04, p);
179
- break;
180
- case "rotateScale":
181
- opacity = p;
182
- rot = (1 - p) * 45 * Math.PI / 180;
183
- sc = 0.5 + p * 0.5;
184
- break;
185
- case "blur":
186
- opacity = p;
187
- blurPx = (1 - p) * BLUR_START_PX;
188
- break;
189
- case "flip3d":
190
- opacity = p;
191
- sy = Math.max(0.04, p);
192
- sc = 1;
193
- break;
194
- case "typewriter":
195
- opacity = p >= 1 ? 1 : p;
196
- break;
197
- case "letterSpread":
198
- opacity = p;
199
- tx = (1 - p) * spreadPx * slot.globalIndex;
200
- break;
201
- }
202
- ctx.globalAlpha = opacity;
203
- if (blurPx > 0.01) {
204
- ctx.filter = `blur(${blurPx}px)`;
205
- }
206
- ctx.translate(slot.x + tx, slot.y + ty);
207
- ctx.rotate(rot);
208
- if (preset === "flip3d") {
209
- ctx.scale(1, sy);
210
- } else {
211
- ctx.scale(sc, sc);
212
- }
213
- if (strokeColor && strokeWidth > 0) {
214
- ctx.strokeStyle = strokeColor;
215
- ctx.lineWidth = strokeWidth;
216
- ctx.strokeText(slot.ch, 0, 0);
144
+ try {
145
+ ctx.font = `${fontWeight} ${fontSize}px ${fontFamily}`;
146
+ ctx.textAlign = "center";
147
+ ctx.textBaseline = "middle";
148
+ ctx.lineJoin = "round";
149
+ ctx.lineCap = "round";
150
+ const tMsGlobal = relativeFrame / fps * 1e3;
151
+ for (const slot of slots) {
152
+ let p;
153
+ if (forceComplete) {
154
+ p = 1;
155
+ } else if (preset === "typewriter") {
156
+ const startMs = slot.globalIndex * staggerMs;
157
+ p = tMsGlobal >= startMs ? 1 : 0;
158
+ } else {
159
+ p = charProgress(relativeFrame, fps, slot.globalIndex, staggerMs, charDurationMs);
160
+ }
161
+ if (p <= 0 && preset !== "blur") continue;
162
+ const slidePx = SLIDE_BASE_PX * scalePx;
163
+ const spreadPx = LETTER_SPREAD_BASE_PX * scalePx;
164
+ ctx.save();
165
+ try {
166
+ let opacity = p;
167
+ let tx = 0;
168
+ let ty = 0;
169
+ let rot = 0;
170
+ let sc = 1;
171
+ let sy = 1;
172
+ let blurPx = 0;
173
+ switch (preset) {
174
+ case "fade":
175
+ opacity = p;
176
+ break;
177
+ case "slideUp":
178
+ opacity = p;
179
+ ty = (1 - p) * slidePx;
180
+ break;
181
+ case "scale":
182
+ opacity = p;
183
+ sc = Math.max(0.04, p);
184
+ break;
185
+ case "rotateScale":
186
+ opacity = p;
187
+ rot = (1 - p) * 45 * Math.PI / 180;
188
+ sc = 0.5 + p * 0.5;
189
+ break;
190
+ case "blur":
191
+ opacity = p;
192
+ blurPx = (1 - p) * BLUR_START_PX;
193
+ break;
194
+ case "flip3d":
195
+ opacity = p;
196
+ sy = Math.max(0.04, p);
197
+ sc = 1;
198
+ break;
199
+ case "typewriter":
200
+ opacity = p >= 1 ? 1 : p;
201
+ break;
202
+ case "letterSpread":
203
+ opacity = p;
204
+ tx = (1 - p) * spreadPx * slot.globalIndex;
205
+ break;
206
+ default:
207
+ break;
208
+ }
209
+ ctx.globalAlpha = opacity;
210
+ if (blurPx > 0.01) {
211
+ ctx.filter = `blur(${blurPx}px)`;
212
+ }
213
+ ctx.translate(slot.x + tx, slot.y + ty);
214
+ ctx.rotate(rot);
215
+ if (preset === "flip3d") {
216
+ ctx.scale(1, sy);
217
+ } else {
218
+ ctx.scale(sc, sc);
219
+ }
220
+ if (strokeColor && strokeWidth > 0) {
221
+ ctx.strokeStyle = strokeColor;
222
+ ctx.lineWidth = strokeWidth;
223
+ ctx.strokeText(slot.ch, 0, 0);
224
+ }
225
+ ctx.fillStyle = fill;
226
+ ctx.fillText(slot.ch, 0, 0);
227
+ } finally {
228
+ ctx.restore();
229
+ }
217
230
  }
218
- ctx.fillStyle = fill;
219
- ctx.fillText(slot.ch, 0, 0);
231
+ } finally {
220
232
  ctx.restore();
221
233
  }
222
- ctx.restore();
223
234
  }
224
235
  function clearCaptionStaggerCache() {
225
236
  charSlotsCache.clear();
@@ -1 +1 @@
1
- {"version":3,"file":"caption-stagger-entrance-renderer.js","sources":["../../../../src/stages/compose/text-renderers/caption-stagger-entrance-renderer.ts"],"sourcesContent":["import type { TextLayer } from '../types';\nimport { getCachedEvenLinesWithWords, getCachedWrapText } from '../text-utils/text-layout-cache';\nimport { getLetterCaseText, measureTextWidth } from '../text-utils/text-metrics';\nimport { needsSpaceBetweenWords } from '../text-utils/locale-detector';\nimport { calculateCaptionStartY } from '../text-utils/caption-position';\n\n/** Matches medeo-web caption preview (`caption-anime-effects` + anime.js defaults). */\nconst DEFAULT_DURATION_MS = 800;\nconst DEFAULT_STAGGER_MS = 50;\n/** Keep the caption still & readable for at least this long after the entrance ends. */\nconst CAPTION_STATIC_TAIL_MS = 500;\n\n/**\n * Scale factor to fit the whole stagger entrance within (captionDurationMs - tail).\n * Returns 1 when it already fits (or duration unknown) — only compress when overrunning.\n * Must stay byte-for-byte identical to medeo-fe caption-cover-lib computeStaggerScale.\n */\nexport function computeStaggerScale(\n rawEndMs: number,\n captionDurationMs: number | undefined\n): number {\n if (captionDurationMs == null || !Number.isFinite(captionDurationMs)) return 1;\n if (rawEndMs <= 0) return 1;\n const budget = Math.max(0, captionDurationMs - CAPTION_STATIC_TAIL_MS);\n if (rawEndMs <= budget) return 1;\n return budget / rawEndMs;\n}\nconst SLIDE_BASE_PX = 50;\nconst LETTER_SPREAD_BASE_PX = 20;\nconst BLUR_START_PX = 10;\nconst FONT_REF_PX = 40;\n\nfunction easeOutExpo(t: number): number {\n if (t <= 0) return 0;\n if (t >= 1) return 1;\n return 1 - Math.pow(2, -10 * t);\n}\n\nexport function charProgress(\n relativeFrame: number,\n fps: number,\n charIndex: number,\n staggerMs: number,\n durationMs: number\n): number {\n const tMs = (relativeFrame / fps) * 1000;\n const startMs = charIndex * staggerMs;\n if (tMs <= startMs) return 0;\n const raw = (tMs - startMs) / durationMs;\n return easeOutExpo(Math.min(1, raw));\n}\n\nexport type CaptionStaggerPreset =\n 'fade' | 'slideUp' | 'scale' | 'rotateScale' | 'blur' | 'flip3d' | 'typewriter' | 'letterSpread';\n\ninterface CharSlot {\n ch: string;\n x: number;\n y: number;\n globalIndex: number;\n}\n\ninterface CharSlotsLayout {\n slots: CharSlot[];\n fontSize: number;\n lineHeight: number;\n}\n\nconst charSlotsCache = new Map<string, CharSlotsLayout>();\nconst staggerFinalRasterCache = new Map<string, ImageBitmap>();\n\nfunction layoutCacheKey(layer: TextLayer, canvasWidth: number, canvasHeight: number): string {\n const ts = layer.fontConfig?.textStyle;\n return [\n layer.id,\n layer.text,\n layer.letterCase ?? '',\n canvasWidth,\n canvasHeight,\n ts?.fontSize,\n ts?.fontFamily,\n ts?.fontWeight,\n ts?.fill,\n ts?.strokeColor,\n ts?.strokeWidth,\n ts?.lineHeight,\n JSON.stringify(layer.fontConfig?.globalPosition ?? null),\n ].join('\\x1f');\n}\n\nfunction staggerRasterKey(\n layer: TextLayer,\n canvasWidth: number,\n canvasHeight: number,\n preset: CaptionStaggerPreset\n): string {\n return `${layoutCacheKey(layer, canvasWidth, canvasHeight)}\\x1f${preset}`;\n}\n\n/** Raw (uncompressed) entrance duration by char count. */\nexport function staggerEntranceRawEndMs(slotCount: number, preset: CaptionStaggerPreset): number {\n if (slotCount <= 0) return 0;\n const lastIndex = slotCount - 1;\n if (preset === 'typewriter') {\n return lastIndex * DEFAULT_STAGGER_MS + DEFAULT_STAGGER_MS;\n }\n return lastIndex * DEFAULT_STAGGER_MS + DEFAULT_DURATION_MS;\n}\n\n/** Effective entrance duration after compressing to fit captionDurationMs - tail. */\nexport function staggerEntranceEndMs(\n slotCount: number,\n preset: CaptionStaggerPreset,\n captionDurationMs?: number\n): number {\n const raw = staggerEntranceRawEndMs(slotCount, preset);\n return raw * computeStaggerScale(raw, captionDurationMs);\n}\n\nfunction buildCharSlots(\n ctx: OffscreenCanvasRenderingContext2D | CanvasRenderingContext2D,\n layer: TextLayer,\n canvasWidth: number,\n canvasHeight: number\n): CharSlotsLayout | null {\n const fontConfig = layer.fontConfig?.textStyle;\n if (!fontConfig) return null;\n\n const fontSize = fontConfig.fontSize;\n const fontFamily = fontConfig.fontFamily;\n const fontWeight = fontConfig.fontWeight;\n const lineHeight = fontConfig.lineHeight || 1.2;\n const maxWidth = canvasWidth * 0.64;\n const text = getLetterCaseText(layer.text, layer.letterCase);\n\n let lines: string[];\n if (layer.wordTimings && layer.wordTimings.length > 0) {\n const needsSpace = needsSpaceBetweenWords(layer.localeCode || 'en-US', text);\n const words = needsSpace ? text.split(/\\s+/) : Array.from(text);\n lines = getCachedEvenLinesWithWords(\n ctx,\n words,\n maxWidth,\n fontSize,\n needsSpace,\n fontFamily,\n fontWeight\n );\n } else {\n lines = getCachedWrapText(ctx, text, maxWidth, fontSize, fontFamily, fontWeight);\n }\n\n const totalHeight = lines.length * fontSize * lineHeight;\n const startY = calculateCaptionStartY(\n canvasHeight,\n totalHeight,\n layer.fontConfig?.globalPosition\n );\n\n ctx.save();\n ctx.font = `${fontWeight} ${fontSize}px ${fontFamily}`;\n\n const slots: CharSlot[] = [];\n let globalIndex = 0;\n\n for (let lineIndex = 0; lineIndex < lines.length; lineIndex++) {\n const line = lines[lineIndex]!;\n const y = startY + lineIndex * fontSize * lineHeight + fontSize / 2;\n const lineWidth = measureTextWidth(ctx, line, fontSize, fontFamily, fontWeight);\n let cx = canvasWidth / 2 - lineWidth / 2;\n\n for (const ch of Array.from(line)) {\n const cw = measureTextWidth(ctx, ch, fontSize, fontFamily, fontWeight);\n slots.push({\n ch,\n x: cx + cw / 2,\n y,\n globalIndex,\n });\n globalIndex++;\n cx += cw;\n }\n }\n\n ctx.restore();\n return { slots, fontSize, lineHeight };\n}\n\nfunction getCachedCharSlots(\n ctx: OffscreenCanvasRenderingContext2D | CanvasRenderingContext2D,\n layer: TextLayer,\n canvasWidth: number,\n canvasHeight: number\n): CharSlotsLayout | null {\n const key = layoutCacheKey(layer, canvasWidth, canvasHeight);\n const cached = charSlotsCache.get(key);\n if (cached) {\n return cached;\n }\n const built = buildCharSlots(ctx, layer, canvasWidth, canvasHeight);\n if (built) {\n charSlotsCache.set(key, built);\n }\n return built;\n}\n\nfunction drawStaggerEntranceFrame(\n ctx: OffscreenCanvasRenderingContext2D | CanvasRenderingContext2D,\n layer: TextLayer,\n built: CharSlotsLayout,\n relativeFrame: number,\n fps: number,\n preset: CaptionStaggerPreset,\n scale: number,\n // Final frame forces every char to progress=1 instead of computing from time:\n // when scale=0 (duration <= tail) endFrame=0, so a time-based progress would be 0\n // and render a blank frame (and poison the cache). The static end state must be explicit.\n forceComplete = false\n): void {\n const { slots, fontSize } = built;\n const fontConfig = layer.fontConfig!.textStyle!;\n const fontFamily = fontConfig.fontFamily;\n const fontWeight = fontConfig.fontWeight;\n const fill = fontConfig.fill;\n const strokeColor = fontConfig.strokeColor;\n const strokeWidth = fontConfig.strokeWidth || 0;\n\n const scalePx = fontSize / FONT_REF_PX;\n // Compress delay & per-char duration equally so the whole entrance fits the budget.\n const staggerMs = DEFAULT_STAGGER_MS * scale;\n const charDurationMs = DEFAULT_DURATION_MS * scale;\n\n ctx.save();\n ctx.font = `${fontWeight} ${fontSize}px ${fontFamily}`;\n ctx.textAlign = 'center';\n ctx.textBaseline = 'middle';\n ctx.lineJoin = 'round';\n ctx.lineCap = 'round';\n\n const tMsGlobal = (relativeFrame / fps) * 1000;\n\n for (const slot of slots) {\n let p: number;\n if (forceComplete) {\n p = 1;\n } else if (preset === 'typewriter') {\n const startMs = slot.globalIndex * staggerMs;\n p = tMsGlobal >= startMs ? 1 : 0;\n } else {\n p = charProgress(relativeFrame, fps, slot.globalIndex, staggerMs, charDurationMs);\n }\n if (p <= 0 && preset !== 'blur') continue;\n\n const slidePx = SLIDE_BASE_PX * scalePx;\n const spreadPx = LETTER_SPREAD_BASE_PX * scalePx;\n\n ctx.save();\n\n let opacity = p;\n let tx = 0;\n let ty = 0;\n let rot = 0;\n let sc = 1;\n let sy = 1;\n let blurPx = 0;\n\n switch (preset) {\n case 'fade':\n opacity = p;\n break;\n case 'slideUp':\n opacity = p;\n ty = (1 - p) * slidePx;\n break;\n case 'scale':\n opacity = p;\n sc = Math.max(0.04, p);\n break;\n case 'rotateScale':\n opacity = p;\n rot = ((1 - p) * 45 * Math.PI) / 180;\n sc = 0.5 + p * 0.5;\n break;\n case 'blur':\n opacity = p;\n blurPx = (1 - p) * BLUR_START_PX;\n break;\n case 'flip3d':\n opacity = p;\n sy = Math.max(0.04, p);\n sc = 1;\n break;\n case 'typewriter':\n opacity = p >= 1 ? 1 : p;\n break;\n case 'letterSpread':\n opacity = p;\n tx = (1 - p) * spreadPx * slot.globalIndex;\n break;\n default:\n break;\n }\n\n ctx.globalAlpha = opacity;\n if (blurPx > 0.01) {\n ctx.filter = `blur(${blurPx}px)`;\n }\n\n ctx.translate(slot.x + tx, slot.y + ty);\n ctx.rotate(rot);\n if (preset === 'flip3d') {\n ctx.scale(1, sy);\n } else {\n ctx.scale(sc, sc);\n }\n\n if (strokeColor && strokeWidth > 0) {\n ctx.strokeStyle = strokeColor;\n ctx.lineWidth = strokeWidth;\n ctx.strokeText(slot.ch, 0, 0);\n }\n ctx.fillStyle = fill;\n ctx.fillText(slot.ch, 0, 0);\n\n ctx.restore();\n }\n\n ctx.restore();\n}\n\nexport function clearCaptionStaggerCache(): void {\n charSlotsCache.clear();\n for (const bitmap of staggerFinalRasterCache.values()) {\n bitmap.close();\n }\n staggerFinalRasterCache.clear();\n}\n\n/**\n * Per-character stagger entrance aligned with medeo-web preview (anime.js easeOutExpo, 800ms, 50ms stagger).\n */\nexport function renderCaptionStaggerEntrance(\n ctx: OffscreenCanvasRenderingContext2D | CanvasRenderingContext2D,\n layer: TextLayer,\n canvasWidth: number,\n canvasHeight: number,\n relativeFrame: number,\n fps: number,\n preset: CaptionStaggerPreset\n): void {\n const built = getCachedCharSlots(ctx, layer, canvasWidth, canvasHeight);\n if (!built) return;\n\n // Compress the entrance to fit (captionDurationMs - tail); scale=1 when it fits or duration unknown.\n const captionDurationMs =\n layer.activeDurationUs != null ? layer.activeDurationUs / 1000 : undefined;\n const rawEndMs = staggerEntranceRawEndMs(built.slots.length, preset);\n const scale = computeStaggerScale(rawEndMs, captionDurationMs);\n const endMs = rawEndMs * scale;\n const endFrame = Math.ceil((endMs / 1000) * fps);\n\n if (relativeFrame >= endFrame) {\n const rasterKey = staggerRasterKey(layer, canvasWidth, canvasHeight, preset);\n let bitmap = staggerFinalRasterCache.get(rasterKey);\n if (!bitmap) {\n const offscreen = new OffscreenCanvas(canvasWidth, canvasHeight);\n const offCtx = offscreen.getContext('2d');\n if (!offCtx) {\n drawStaggerEntranceFrame(ctx, layer, built, endFrame, fps, preset, scale, true);\n return;\n }\n drawStaggerEntranceFrame(offCtx, layer, built, endFrame, fps, preset, scale, true);\n bitmap = offscreen.transferToImageBitmap();\n staggerFinalRasterCache.set(rasterKey, bitmap);\n }\n ctx.drawImage(bitmap, 0, 0);\n return;\n }\n\n drawStaggerEntranceFrame(ctx, layer, built, relativeFrame, fps, preset, scale);\n}\n"],"names":[],"mappings":";;;;AAOA,MAAM,sBAAsB;AAC5B,MAAM,qBAAqB;AAE3B,MAAM,yBAAyB;AAOxB,SAAS,oBACd,UACA,mBACQ;AACR,MAAI,qBAAqB,QAAQ,CAAC,OAAO,SAAS,iBAAiB,EAAG,QAAO;AAC7E,MAAI,YAAY,EAAG,QAAO;AAC1B,QAAM,SAAS,KAAK,IAAI,GAAG,oBAAoB,sBAAsB;AACrE,MAAI,YAAY,OAAQ,QAAO;AAC/B,SAAO,SAAS;AAClB;AACA,MAAM,gBAAgB;AACtB,MAAM,wBAAwB;AAC9B,MAAM,gBAAgB;AACtB,MAAM,cAAc;AAEpB,SAAS,YAAY,GAAmB;AACtC,MAAI,KAAK,EAAG,QAAO;AACnB,MAAI,KAAK,EAAG,QAAO;AACnB,SAAO,IAAI,KAAK,IAAI,GAAG,MAAM,CAAC;AAChC;AAEO,SAAS,aACd,eACA,KACA,WACA,WACA,YACQ;AACR,QAAM,MAAO,gBAAgB,MAAO;AACpC,QAAM,UAAU,YAAY;AAC5B,MAAI,OAAO,QAAS,QAAO;AAC3B,QAAM,OAAO,MAAM,WAAW;AAC9B,SAAO,YAAY,KAAK,IAAI,GAAG,GAAG,CAAC;AACrC;AAkBA,M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AQ,MAAQ,GAAG;AAE/C,MAAI,iBAAiB,UAAU;AAC7B,UAAM,YAAY,iBAAiB,OAAO,aAAa,cAAc,MAAM;AAC3E,QAAI,SAAS,wBAAwB,IAAI,SAAS;AAClD,QAAI,CAAC,QAAQ;AACX,YAAM,YAAY,IAAI,gBAAgB,aAAa,YAAY;AAC/D,YAAM,SAAS,UAAU,WAAW,IAAI;AACxC,UAAI,CAAC,QAAQ;AACX,iCAAyB,KAAK,OAAO,OAAO,UAAU,KAAK,QAAQ,OAAO,IAAI;AAC9E;AAAA,MACF;AACA,+BAAyB,QAAQ,OAAO,OAAO,UAAU,KAAK,QAAQ,OAAO,IAAI;AACjF,eAAS,UAAU,sBAAA;AACnB,8BAAwB,IAAI,WAAW,MAAM;AAAA,IAC/C;AACA,QAAI,UAAU,QAAQ,GAAG,CAAC;AAC1B;AAAA,EACF;AAEA,2BAAyB,KAAK,OAAO,OAAO,eAAe,KAAK,QAAQ,KAAK;AAC/E;"}
1
+ {"version":3,"file":"caption-stagger-entrance-renderer.js","sources":["../../../../src/stages/compose/text-renderers/caption-stagger-entrance-renderer.ts"],"sourcesContent":["import type { TextLayer } from '../types';\nimport { getCachedEvenLinesWithWords, getCachedWrapText } from '../text-utils/text-layout-cache';\nimport { getLetterCaseText, measureTextWidth } from '../text-utils/text-metrics';\nimport { needsSpaceBetweenWords } from '../text-utils/locale-detector';\nimport { calculateCaptionStartY } from '../text-utils/caption-position';\n\n/** Matches medeo-web caption preview (`caption-anime-effects` + anime.js defaults). */\nconst DEFAULT_DURATION_MS = 800;\nconst DEFAULT_STAGGER_MS = 50;\n/** Keep the caption still & readable for at least this long after the entrance ends. */\nconst CAPTION_STATIC_TAIL_MS = 500;\n\n/**\n * Scale factor to fit the whole stagger entrance within (captionDurationMs - tail).\n * Returns 1 when it already fits (or duration unknown) — only compress when overrunning.\n * Must stay byte-for-byte identical to medeo-fe caption-cover-lib computeStaggerScale.\n */\nexport function computeStaggerScale(\n rawEndMs: number,\n captionDurationMs: number | undefined\n): number {\n if (captionDurationMs == null || !Number.isFinite(captionDurationMs)) return 1;\n if (rawEndMs <= 0) return 1;\n const budget = Math.max(0, captionDurationMs - CAPTION_STATIC_TAIL_MS);\n if (rawEndMs <= budget) return 1;\n return budget / rawEndMs;\n}\nconst SLIDE_BASE_PX = 50;\nconst LETTER_SPREAD_BASE_PX = 20;\nconst BLUR_START_PX = 10;\nconst FONT_REF_PX = 40;\n\nfunction easeOutExpo(t: number): number {\n if (t <= 0) return 0;\n if (t >= 1) return 1;\n return 1 - Math.pow(2, -10 * t);\n}\n\nexport function charProgress(\n relativeFrame: number,\n fps: number,\n charIndex: number,\n staggerMs: number,\n durationMs: number\n): number {\n const tMs = (relativeFrame / fps) * 1000;\n const startMs = charIndex * staggerMs;\n if (tMs <= startMs) return 0;\n const raw = (tMs - startMs) / durationMs;\n return easeOutExpo(Math.min(1, raw));\n}\n\nexport type CaptionStaggerPreset =\n 'fade' | 'slideUp' | 'scale' | 'rotateScale' | 'blur' | 'flip3d' | 'typewriter' | 'letterSpread';\n\ninterface CharSlot {\n ch: string;\n x: number;\n y: number;\n globalIndex: number;\n}\n\ninterface CharSlotsLayout {\n slots: CharSlot[];\n fontSize: number;\n lineHeight: number;\n}\n\nconst charSlotsCache = new Map<string, CharSlotsLayout>();\nconst staggerFinalRasterCache = new Map<string, ImageBitmap>();\n\nfunction layoutCacheKey(layer: TextLayer, canvasWidth: number, canvasHeight: number): string {\n const ts = layer.fontConfig?.textStyle;\n return [\n layer.id,\n layer.text,\n layer.letterCase ?? '',\n canvasWidth,\n canvasHeight,\n ts?.fontSize,\n ts?.fontFamily,\n ts?.fontWeight,\n ts?.fill,\n ts?.strokeColor,\n ts?.strokeWidth,\n ts?.lineHeight,\n JSON.stringify(layer.fontConfig?.globalPosition ?? null),\n ].join('\\x1f');\n}\n\nfunction staggerRasterKey(\n layer: TextLayer,\n canvasWidth: number,\n canvasHeight: number,\n preset: CaptionStaggerPreset\n): string {\n return `${layoutCacheKey(layer, canvasWidth, canvasHeight)}\\x1f${preset}`;\n}\n\n/** Raw (uncompressed) entrance duration by char count. */\nexport function staggerEntranceRawEndMs(slotCount: number, preset: CaptionStaggerPreset): number {\n if (slotCount <= 0) return 0;\n const lastIndex = slotCount - 1;\n if (preset === 'typewriter') {\n return lastIndex * DEFAULT_STAGGER_MS + DEFAULT_STAGGER_MS;\n }\n return lastIndex * DEFAULT_STAGGER_MS + DEFAULT_DURATION_MS;\n}\n\n/** Effective entrance duration after compressing to fit captionDurationMs - tail. */\nexport function staggerEntranceEndMs(\n slotCount: number,\n preset: CaptionStaggerPreset,\n captionDurationMs?: number\n): number {\n const raw = staggerEntranceRawEndMs(slotCount, preset);\n return raw * computeStaggerScale(raw, captionDurationMs);\n}\n\nfunction buildCharSlots(\n ctx: OffscreenCanvasRenderingContext2D | CanvasRenderingContext2D,\n layer: TextLayer,\n canvasWidth: number,\n canvasHeight: number\n): CharSlotsLayout | null {\n const fontConfig = layer.fontConfig?.textStyle;\n if (!fontConfig) return null;\n\n const fontSize = fontConfig.fontSize;\n const fontFamily = fontConfig.fontFamily;\n const fontWeight = fontConfig.fontWeight;\n const lineHeight = fontConfig.lineHeight || 1.2;\n const maxWidth = canvasWidth * 0.64;\n const text = getLetterCaseText(layer.text, layer.letterCase);\n\n let lines: string[];\n if (layer.wordTimings && layer.wordTimings.length > 0) {\n const needsSpace = needsSpaceBetweenWords(layer.localeCode || 'en-US', text);\n const words = needsSpace ? text.split(/\\s+/) : Array.from(text);\n lines = getCachedEvenLinesWithWords(\n ctx,\n words,\n maxWidth,\n fontSize,\n needsSpace,\n fontFamily,\n fontWeight\n );\n } else {\n lines = getCachedWrapText(ctx, text, maxWidth, fontSize, fontFamily, fontWeight);\n }\n\n const totalHeight = lines.length * fontSize * lineHeight;\n const startY = calculateCaptionStartY(\n canvasHeight,\n totalHeight,\n layer.fontConfig?.globalPosition\n );\n\n ctx.save();\n try {\n ctx.font = `${fontWeight} ${fontSize}px ${fontFamily}`;\n\n const slots: CharSlot[] = [];\n let globalIndex = 0;\n\n for (let lineIndex = 0; lineIndex < lines.length; lineIndex++) {\n const line = lines[lineIndex]!;\n const y = startY + lineIndex * fontSize * lineHeight + fontSize / 2;\n const lineWidth = measureTextWidth(ctx, line, fontSize, fontFamily, fontWeight);\n let cx = canvasWidth / 2 - lineWidth / 2;\n\n for (const ch of Array.from(line)) {\n const cw = measureTextWidth(ctx, ch, fontSize, fontFamily, fontWeight);\n slots.push({\n ch,\n x: cx + cw / 2,\n y,\n globalIndex,\n });\n globalIndex++;\n cx += cw;\n }\n }\n\n return { slots, fontSize, lineHeight };\n } finally {\n ctx.restore();\n }\n}\n\nfunction getCachedCharSlots(\n ctx: OffscreenCanvasRenderingContext2D | CanvasRenderingContext2D,\n layer: TextLayer,\n canvasWidth: number,\n canvasHeight: number\n): CharSlotsLayout | null {\n const key = layoutCacheKey(layer, canvasWidth, canvasHeight);\n const cached = charSlotsCache.get(key);\n if (cached) {\n return cached;\n }\n const built = buildCharSlots(ctx, layer, canvasWidth, canvasHeight);\n if (built) {\n charSlotsCache.set(key, built);\n }\n return built;\n}\n\nfunction drawStaggerEntranceFrame(\n ctx: OffscreenCanvasRenderingContext2D | CanvasRenderingContext2D,\n layer: TextLayer,\n built: CharSlotsLayout,\n relativeFrame: number,\n fps: number,\n preset: CaptionStaggerPreset,\n scale: number,\n // Final frame forces every char to progress=1 instead of computing from time:\n // when scale=0 (duration <= tail) endFrame=0, so a time-based progress would be 0\n // and render a blank frame (and poison the cache). The static end state must be explicit.\n forceComplete = false\n): void {\n const { slots, fontSize } = built;\n const fontConfig = layer.fontConfig!.textStyle!;\n const fontFamily = fontConfig.fontFamily;\n const fontWeight = fontConfig.fontWeight;\n const fill = fontConfig.fill;\n const strokeColor = fontConfig.strokeColor;\n const strokeWidth = fontConfig.strokeWidth || 0;\n\n const scalePx = fontSize / FONT_REF_PX;\n // Compress delay & per-char duration equally so the whole entrance fits the budget.\n const staggerMs = DEFAULT_STAGGER_MS * scale;\n const charDurationMs = DEFAULT_DURATION_MS * scale;\n\n ctx.save();\n try {\n ctx.font = `${fontWeight} ${fontSize}px ${fontFamily}`;\n ctx.textAlign = 'center';\n ctx.textBaseline = 'middle';\n ctx.lineJoin = 'round';\n ctx.lineCap = 'round';\n\n const tMsGlobal = (relativeFrame / fps) * 1000;\n\n for (const slot of slots) {\n let p: number;\n if (forceComplete) {\n p = 1;\n } else if (preset === 'typewriter') {\n const startMs = slot.globalIndex * staggerMs;\n p = tMsGlobal >= startMs ? 1 : 0;\n } else {\n p = charProgress(relativeFrame, fps, slot.globalIndex, staggerMs, charDurationMs);\n }\n if (p <= 0 && preset !== 'blur') continue;\n\n const slidePx = SLIDE_BASE_PX * scalePx;\n const spreadPx = LETTER_SPREAD_BASE_PX * scalePx;\n\n ctx.save();\n try {\n let opacity = p;\n let tx = 0;\n let ty = 0;\n let rot = 0;\n let sc = 1;\n let sy = 1;\n let blurPx = 0;\n\n switch (preset) {\n case 'fade':\n opacity = p;\n break;\n case 'slideUp':\n opacity = p;\n ty = (1 - p) * slidePx;\n break;\n case 'scale':\n opacity = p;\n sc = Math.max(0.04, p);\n break;\n case 'rotateScale':\n opacity = p;\n rot = ((1 - p) * 45 * Math.PI) / 180;\n sc = 0.5 + p * 0.5;\n break;\n case 'blur':\n opacity = p;\n blurPx = (1 - p) * BLUR_START_PX;\n break;\n case 'flip3d':\n opacity = p;\n sy = Math.max(0.04, p);\n sc = 1;\n break;\n case 'typewriter':\n opacity = p >= 1 ? 1 : p;\n break;\n case 'letterSpread':\n opacity = p;\n tx = (1 - p) * spreadPx * slot.globalIndex;\n break;\n default:\n break;\n }\n\n ctx.globalAlpha = opacity;\n if (blurPx > 0.01) {\n ctx.filter = `blur(${blurPx}px)`;\n }\n\n ctx.translate(slot.x + tx, slot.y + ty);\n ctx.rotate(rot);\n if (preset === 'flip3d') {\n ctx.scale(1, sy);\n } else {\n ctx.scale(sc, sc);\n }\n\n if (strokeColor && strokeWidth > 0) {\n ctx.strokeStyle = strokeColor;\n ctx.lineWidth = strokeWidth;\n ctx.strokeText(slot.ch, 0, 0);\n }\n ctx.fillStyle = fill;\n ctx.fillText(slot.ch, 0, 0);\n } finally {\n ctx.restore();\n }\n }\n } finally {\n ctx.restore();\n }\n}\n\nexport function clearCaptionStaggerCache(): void {\n charSlotsCache.clear();\n for (const bitmap of staggerFinalRasterCache.values()) {\n bitmap.close();\n }\n staggerFinalRasterCache.clear();\n}\n\n/**\n * Per-character stagger entrance aligned with medeo-web preview (anime.js easeOutExpo, 800ms, 50ms stagger).\n */\nexport function renderCaptionStaggerEntrance(\n ctx: OffscreenCanvasRenderingContext2D | CanvasRenderingContext2D,\n layer: TextLayer,\n canvasWidth: number,\n canvasHeight: number,\n relativeFrame: number,\n fps: number,\n preset: CaptionStaggerPreset\n): void {\n const built = getCachedCharSlots(ctx, layer, canvasWidth, canvasHeight);\n if (!built) return;\n\n // Compress the entrance to fit (captionDurationMs - tail); scale=1 when it fits or duration unknown.\n const captionDurationMs =\n layer.activeDurationUs != null ? layer.activeDurationUs / 1000 : undefined;\n const rawEndMs = staggerEntranceRawEndMs(built.slots.length, preset);\n const scale = computeStaggerScale(rawEndMs, captionDurationMs);\n const endMs = rawEndMs * scale;\n const endFrame = Math.ceil((endMs / 1000) * fps);\n\n if (relativeFrame >= endFrame) {\n const rasterKey = staggerRasterKey(layer, canvasWidth, canvasHeight, preset);\n let bitmap = staggerFinalRasterCache.get(rasterKey);\n if (!bitmap) {\n const offscreen = new OffscreenCanvas(canvasWidth, canvasHeight);\n const offCtx = offscreen.getContext('2d');\n if (!offCtx) {\n drawStaggerEntranceFrame(ctx, layer, built, endFrame, fps, preset, scale, true);\n return;\n }\n drawStaggerEntranceFrame(offCtx, layer, built, endFrame, fps, preset, scale, true);\n bitmap = offscreen.transferToImageBitmap();\n staggerFinalRasterCache.set(rasterKey, bitmap);\n }\n ctx.drawImage(bitmap, 0, 0);\n return;\n }\n\n drawStaggerEntranceFrame(ctx, layer, built, relative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AAK,QAAQ,OAAO,IAAI;AAC9E;AAAA,MACF;AACA,+BAAyB,QAAQ,OAAO,OAAO,UAAU,KAAK,QAAQ,OAAO,IAAI;AACjF,eAAS,UAAU,sBAAA;AACnB,8BAAwB,IAAI,WAAW,MAAM;AAAA,IAC/C;AACA,QAAI,UAAU,QAAQ,GAAG,CAAC;AAC1B;AAAA,EACF;AAEA,2BAAyB,KAAK,OAAO,OAAO,eAAe,KAAK,QAAQ,KAAK;AAC/E;"}
@@ -1 +1 @@
1
- {"version":3,"file":"character-ktv-renderer.d.ts","sourceRoot":"","sources":["../../../../src/stages/compose/text-renderers/character-ktv-renderer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAgB1C,wBAAgB,kBAAkB,CAChC,GAAG,EAAE,iCAAiC,GAAG,wBAAwB,EACjE,KAAK,EAAE,SAAS,EAChB,WAAW,EAAE,MAAM,EACnB,YAAY,EAAE,MAAM,EACpB,aAAa,EAAE,MAAM,EACrB,GAAG,GAAE,MAAW,GACf,IAAI,CAmIN"}
1
+ {"version":3,"file":"character-ktv-renderer.d.ts","sourceRoot":"","sources":["../../../../src/stages/compose/text-renderers/character-ktv-renderer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAgB1C,wBAAgB,kBAAkB,CAChC,GAAG,EAAE,iCAAiC,GAAG,wBAAwB,EACjE,KAAK,EAAE,SAAS,EAChB,WAAW,EAAE,MAAM,EACnB,YAAY,EAAE,MAAM,EACpB,aAAa,EAAE,MAAM,EACrB,GAAG,GAAE,MAAW,GACf,IAAI,CAqIN"}
@@ -70,49 +70,52 @@ function renderCharacterKTV(ctx, layer, canvasWidth, canvasHeight, relativeFrame
70
70
  const fullText = characterTimings.map((ct) => ct.char).join("");
71
71
  const fullTextLines = wrapText(ctx, fullText, maxWidth, fontSize, fontFamily, fontWeight);
72
72
  ctx.save();
73
- ctx.font = `${fontWeight} ${fontSize}px ${fontFamily}`;
74
- ctx.textAlign = "left";
75
- ctx.textBaseline = "middle";
76
- ctx.lineJoin = "round";
77
- ctx.lineCap = "round";
78
- const totalHeight = fullTextLines.length * fontSize * lineHeight;
79
- const startY = calculateCaptionStartY(
80
- canvasHeight,
81
- totalHeight,
82
- layer.fontConfig?.globalPosition
83
- );
84
- let charIndexInText = 0;
85
- for (let lineIndex = 0; lineIndex < fullTextLines.length; lineIndex++) {
86
- const line = fullTextLines[lineIndex];
87
- const y = startY + lineIndex * fontSize * lineHeight + fontSize / 2;
88
- const lineWidth = measureTextWidth(ctx, line, fontSize, fontFamily, fontWeight);
89
- let currentX = canvasWidth / 2 - lineWidth / 2;
90
- for (let i = 0; i < line.length; i++) {
91
- const char = line[i];
92
- const timing = characterTimings[charIndexInText];
93
- if (timing) {
94
- const hasScanned = relativeFrame >= timing.startFrame;
95
- const isCurrentlySinging = hasScanned && relativeFrame < timing.startFrame + transitionFrames;
96
- ctx.fillStyle = hasScanned ? highlightFill : baseFill;
97
- if (isCurrentlySinging) {
98
- ctx.shadowColor = glowColor;
99
- ctx.shadowBlur = glowIntensity * 10;
100
- } else {
73
+ try {
74
+ ctx.font = `${fontWeight} ${fontSize}px ${fontFamily}`;
75
+ ctx.textAlign = "left";
76
+ ctx.textBaseline = "middle";
77
+ ctx.lineJoin = "round";
78
+ ctx.lineCap = "round";
79
+ const totalHeight = fullTextLines.length * fontSize * lineHeight;
80
+ const startY = calculateCaptionStartY(
81
+ canvasHeight,
82
+ totalHeight,
83
+ layer.fontConfig?.globalPosition
84
+ );
85
+ let charIndexInText = 0;
86
+ for (let lineIndex = 0; lineIndex < fullTextLines.length; lineIndex++) {
87
+ const line = fullTextLines[lineIndex];
88
+ const y = startY + lineIndex * fontSize * lineHeight + fontSize / 2;
89
+ const lineWidth = measureTextWidth(ctx, line, fontSize, fontFamily, fontWeight);
90
+ let currentX = canvasWidth / 2 - lineWidth / 2;
91
+ for (let i = 0; i < line.length; i++) {
92
+ const char = line[i];
93
+ const timing = characterTimings[charIndexInText];
94
+ if (timing) {
95
+ const hasScanned = relativeFrame >= timing.startFrame;
96
+ const isCurrentlySinging = hasScanned && relativeFrame < timing.startFrame + transitionFrames;
97
+ ctx.fillStyle = hasScanned ? highlightFill : baseFill;
98
+ if (isCurrentlySinging) {
99
+ ctx.shadowColor = glowColor;
100
+ ctx.shadowBlur = glowIntensity * 10;
101
+ } else {
102
+ ctx.shadowBlur = 0;
103
+ }
104
+ if (strokeColor && strokeWidth > 0) {
105
+ ctx.strokeStyle = strokeColor;
106
+ ctx.lineWidth = strokeWidth;
107
+ ctx.strokeText(char, currentX, y);
108
+ }
109
+ ctx.fillText(char, currentX, y);
101
110
  ctx.shadowBlur = 0;
102
111
  }
103
- if (strokeColor && strokeWidth > 0) {
104
- ctx.strokeStyle = strokeColor;
105
- ctx.lineWidth = strokeWidth;
106
- ctx.strokeText(char, currentX, y);
107
- }
108
- ctx.fillText(char, currentX, y);
109
- ctx.shadowBlur = 0;
112
+ currentX += measureTextWidth(ctx, char, fontSize, fontFamily, fontWeight);
113
+ charIndexInText++;
110
114
  }
111
- currentX += measureTextWidth(ctx, char, fontSize, fontFamily, fontWeight);
112
- charIndexInText++;
113
115
  }
116
+ } finally {
117
+ ctx.restore();
114
118
  }
115
- ctx.restore();
116
119
  }
117
120
  export {
118
121
  renderCharacterKTV
@@ -1 +1 @@
1
- {"version":3,"file":"character-ktv-renderer.js","sources":["../../../../src/stages/compose/text-renderers/character-ktv-renderer.ts"],"sourcesContent":["import type { TextLayer } from '../types';\nimport { wrapText } from '../text-utils/text-wrapper';\nimport { getLetterCaseText, measureTextWidth } from '../text-utils/text-metrics';\nimport { needsSpaceBetweenWords } from '../text-utils/locale-detector';\nimport { calculateCaptionStartY } from '../text-utils/caption-position';\n\ninterface CharacterTiming {\n char: string;\n index: number;\n startFrame: number;\n}\n\nfunction usToFrame(us: number, fps: number): number {\n return Math.floor(us / (1000000 / fps));\n}\n\nexport function renderCharacterKTV(\n ctx: OffscreenCanvasRenderingContext2D | CanvasRenderingContext2D,\n layer: TextLayer,\n canvasWidth: number,\n canvasHeight: number,\n relativeFrame: number,\n fps: number = 30\n): void {\n const fontConfig = layer.fontConfig?.textStyle;\n if (!fontConfig) return;\n\n const fontSize = fontConfig.fontSize;\n const fontFamily = fontConfig.fontFamily;\n const fontWeight = fontConfig.fontWeight;\n const baseFill = fontConfig.fill;\n const strokeColor = fontConfig.strokeColor;\n const strokeWidth = fontConfig.strokeWidth || 0;\n const lineHeight = fontConfig.lineHeight || 1.2;\n\n const highlightFill = layer.animation?.highlightTextStyle?.fill || 'rgb(255, 215, 0)';\n const glowColor = layer.animation?.glowColor || '#ffffff';\n const glowIntensity = layer.animation?.glowIntensity || 3;\n const transitionFrames = layer.animation?.transitionFrames || 10;\n\n const maxWidth = canvasWidth * 0.9;\n const text = getLetterCaseText(layer.text, layer.letterCase);\n const needsSpace = needsSpaceBetweenWords(layer.localeCode || 'en-US', text);\n const characterTimings: CharacterTiming[] = [];\n\n if (layer.wordTimings && layer.wordTimings.length > 0) {\n let charIndex = 0;\n\n for (let wordIndex = 0; wordIndex < layer.wordTimings.length; wordIndex++) {\n const word = layer.wordTimings[wordIndex]!;\n const wordChars = word.text.split('');\n const wordStartFrame = usToFrame(word.startUs, fps);\n const wordEndFrame = usToFrame(word.endUs, fps);\n const framesPerChar =\n wordChars.length > 0 ? (wordEndFrame - wordStartFrame) / wordChars.length : 0;\n\n for (let i = 0; i < wordChars.length; i++) {\n const charStartFrame = Math.floor(wordStartFrame + i * framesPerChar);\n characterTimings.push({\n char: wordChars[i]!,\n index: charIndex,\n startFrame: charStartFrame,\n });\n charIndex++;\n }\n\n if (needsSpace && wordIndex < layer.wordTimings.length - 1) {\n const nextWord = layer.wordTimings[wordIndex + 1];\n const nextWordFirstChar = nextWord?.text?.[0] || '';\n const isPunctuation = /[.,!?;:)]/.test(nextWordFirstChar);\n\n if (!isPunctuation) {\n const spaceStartFrame = characterTimings[characterTimings.length - 1]?.startFrame || 0;\n characterTimings.push({\n char: ' ',\n index: charIndex,\n startFrame: spaceStartFrame,\n });\n charIndex++;\n }\n }\n }\n } else {\n const totalFrames = 100;\n const chars = text.split('');\n const framesPerChar = chars.length > 0 ? totalFrames / chars.length : 0;\n\n for (let i = 0; i < chars.length; i++) {\n characterTimings.push({\n char: chars[i]!,\n index: i,\n startFrame: Math.floor(i * framesPerChar),\n });\n }\n }\n\n const fullText = characterTimings.map((ct) => ct.char).join('');\n const fullTextLines = wrapText(ctx, fullText, maxWidth, fontSize, fontFamily, fontWeight);\n\n ctx.save();\n ctx.font = `${fontWeight} ${fontSize}px ${fontFamily}`;\n ctx.textAlign = 'left';\n ctx.textBaseline = 'middle';\n ctx.lineJoin = 'round';\n ctx.lineCap = 'round';\n\n const totalHeight = fullTextLines.length * fontSize * lineHeight;\n const startY = calculateCaptionStartY(\n canvasHeight,\n totalHeight,\n layer.fontConfig?.globalPosition\n );\n\n let charIndexInText = 0;\n\n for (let lineIndex = 0; lineIndex < fullTextLines.length; lineIndex++) {\n const line = fullTextLines[lineIndex]!;\n const y = startY + lineIndex * fontSize * lineHeight + fontSize / 2;\n const lineWidth = measureTextWidth(ctx, line, fontSize, fontFamily, fontWeight);\n let currentX = canvasWidth / 2 - lineWidth / 2;\n\n for (let i = 0; i < line.length; i++) {\n const char = line[i]!;\n const timing = characterTimings[charIndexInText];\n\n if (timing) {\n const hasScanned = relativeFrame >= timing.startFrame;\n const isCurrentlySinging =\n hasScanned && relativeFrame < timing.startFrame + transitionFrames;\n\n ctx.fillStyle = hasScanned ? highlightFill : baseFill;\n\n if (isCurrentlySinging) {\n ctx.shadowColor = glowColor;\n ctx.shadowBlur = glowIntensity * 10;\n } else {\n ctx.shadowBlur = 0;\n }\n\n if (strokeColor && strokeWidth > 0) {\n ctx.strokeStyle = strokeColor;\n ctx.lineWidth = strokeWidth;\n ctx.strokeText(char, currentX, y);\n }\n ctx.fillText(char, currentX, y);\n ctx.shadowBlur = 0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IACA,MAAM,YAAY;AAAA,EAAA;AAGpB,MAAI,kBAAkB;AAEtB,WAAS,YAAY,GAAG,YAAY,cAAc,QAAQ,aAAa;AACrE,UAAM,OAAO,cAAc,SAAS;AACpC,UAAM,IAAI,SAAS,YAAY,WAAW,aAAa,WAAW;AAClE,UAAM,YAAY,iBAAiB,KAAK,MAAM,UAAU,YAAY,UAAU;AAC9E,QAAI,WAAW,cAAc,IAAI,YAAY;AAE7C,aAAS,IAAI,GAAG,IAAI,KAAK,QAAQ,KAAK;AACpC,YAAM,OAAO,KAAK,CAAC;AACnB,YAAM,SAAS,iBAAiB,eAAe;AAE/C,UAAI,QAAQ;AACV,cAAM,aAAa,iBAAiB,OAAO;AAC3C,cAAM,qBACJ,cAAc,gBAAgB,OAAO,aAAa;AAEpD,YAAI,YAAY,aAAa,gBAAgB;AAE7C,YAAI,oBAAoB;AACtB,cAAI,cAAc;AAClB,cAAI,aAAa,gBAAgB;AAAA,QACnC,OAAO;AACL,cAAI,aAAa;AAAA,QACnB;AAEA,YAAI,eAAe,cAAc,GAAG;AAClC,cAAI,cAAc;AAClB,cAAI,YAAY;AAChB,cAAI,WAAW,MAAM,UAAU,CAAC;AAAA,QAClC;AACA,YAAI,SAAS,MAAM,UAAU,CAAC;AAC9B,YAAI,aAAa;AAAA,MACnB;AAEA,kBAAY,iBAAiB,KAAK,MAAM,UAAU,YAAY,UAAU;AACxE;AAAA,IACF;AAAA,EACF;AAEA,MAAI,QAAA;AACN;"}
1
+ {"version":3,"file":"character-ktv-renderer.js","sources":["../../../../src/stages/compose/text-renderers/character-ktv-renderer.ts"],"sourcesContent":["import type { TextLayer } from '../types';\nimport { wrapText } from '../text-utils/text-wrapper';\nimport { getLetterCaseText, measureTextWidth } from '../text-utils/text-metrics';\nimport { needsSpaceBetweenWords } from '../text-utils/locale-detector';\nimport { calculateCaptionStartY } from '../text-utils/caption-position';\n\ninterface CharacterTiming {\n char: string;\n index: number;\n startFrame: number;\n}\n\nfunction usToFrame(us: number, fps: number): number {\n return Math.floor(us / (1000000 / fps));\n}\n\nexport function renderCharacterKTV(\n ctx: OffscreenCanvasRenderingContext2D | CanvasRenderingContext2D,\n layer: TextLayer,\n canvasWidth: number,\n canvasHeight: number,\n relativeFrame: number,\n fps: number = 30\n): void {\n const fontConfig = layer.fontConfig?.textStyle;\n if (!fontConfig) return;\n\n const fontSize = fontConfig.fontSize;\n const fontFamily = fontConfig.fontFamily;\n const fontWeight = fontConfig.fontWeight;\n const baseFill = fontConfig.fill;\n const strokeColor = fontConfig.strokeColor;\n const strokeWidth = fontConfig.strokeWidth || 0;\n const lineHeight = fontConfig.lineHeight || 1.2;\n\n const highlightFill = layer.animation?.highlightTextStyle?.fill || 'rgb(255, 215, 0)';\n const glowColor = layer.animation?.glowColor || '#ffffff';\n const glowIntensity = layer.animation?.glowIntensity || 3;\n const transitionFrames = layer.animation?.transitionFrames || 10;\n\n const maxWidth = canvasWidth * 0.9;\n const text = getLetterCaseText(layer.text, layer.letterCase);\n const needsSpace = needsSpaceBetweenWords(layer.localeCode || 'en-US', text);\n const characterTimings: CharacterTiming[] = [];\n\n if (layer.wordTimings && layer.wordTimings.length > 0) {\n let charIndex = 0;\n\n for (let wordIndex = 0; wordIndex < layer.wordTimings.length; wordIndex++) {\n const word = layer.wordTimings[wordIndex]!;\n const wordChars = word.text.split('');\n const wordStartFrame = usToFrame(word.startUs, fps);\n const wordEndFrame = usToFrame(word.endUs, fps);\n const framesPerChar =\n wordChars.length > 0 ? (wordEndFrame - wordStartFrame) / wordChars.length : 0;\n\n for (let i = 0; i < wordChars.length; i++) {\n const charStartFrame = Math.floor(wordStartFrame + i * framesPerChar);\n characterTimings.push({\n char: wordChars[i]!,\n index: charIndex,\n startFrame: charStartFrame,\n });\n charIndex++;\n }\n\n if (needsSpace && wordIndex < layer.wordTimings.length - 1) {\n const nextWord = layer.wordTimings[wordIndex + 1];\n const nextWordFirstChar = nextWord?.text?.[0] || '';\n const isPunctuation = /[.,!?;:)]/.test(nextWordFirstChar);\n\n if (!isPunctuation) {\n const spaceStartFrame = characterTimings[characterTimings.length - 1]?.startFrame || 0;\n characterTimings.push({\n char: ' ',\n index: charIndex,\n startFrame: spaceStartFrame,\n });\n charIndex++;\n }\n }\n }\n } else {\n const totalFrames = 100;\n const chars = text.split('');\n const framesPerChar = chars.length > 0 ? totalFrames / chars.length : 0;\n\n for (let i = 0; i < chars.length; i++) {\n characterTimings.push({\n char: chars[i]!,\n index: i,\n startFrame: Math.floor(i * framesPerChar),\n });\n }\n }\n\n const fullText = characterTimings.map((ct) => ct.char).join('');\n const fullTextLines = wrapText(ctx, fullText, maxWidth, fontSize, fontFamily, fontWeight);\n\n ctx.save();\n try {\n ctx.font = `${fontWeight} ${fontSize}px ${fontFamily}`;\n ctx.textAlign = 'left';\n ctx.textBaseline = 'middle';\n ctx.lineJoin = 'round';\n ctx.lineCap = 'round';\n\n const totalHeight = fullTextLines.length * fontSize * lineHeight;\n const startY = calculateCaptionStartY(\n canvasHeight,\n totalHeight,\n layer.fontConfig?.globalPosition\n );\n\n let charIndexInText = 0;\n\n for (let lineIndex = 0; lineIndex < fullTextLines.length; lineIndex++) {\n const line = fullTextLines[lineIndex]!;\n const y = startY + lineIndex * fontSize * lineHeight + fontSize / 2;\n const lineWidth = measureTextWidth(ctx, line, fontSize, fontFamily, fontWeight);\n let currentX = canvasWidth / 2 - lineWidth / 2;\n\n for (let i = 0; i < line.length; i++) {\n const char = line[i]!;\n const timing = characterTimings[charIndexInText];\n\n if (timing) {\n const hasScanned = relativeFrame >= timing.startFrame;\n const isCurrentlySinging =\n hasScanned && relativeFrame < timing.startFrame + transitionFrames;\n\n ctx.fillStyle = hasScanned ? highlightFill : baseFill;\n\n if (isCurrentlySinging) {\n ctx.shadowColor = glowColor;\n ctx.shadowBlur = glowIntensity * 10;\n } else {\n ctx.shadowBlur = 0;\n }\n\n if (strokeColor && strokeWidth > 0) {\n ctx.strokeStyle = strokeColor;\n ctx.lineWidth = strokeWidth;\n ctx.strokeText(char, currentX, y);\n }\n ctx.fillText(char, currentX, y);\n ctx.shadowBlur = 0;\n }\n\n currentX += measureTextWidth(ctx, char, fontSize, fontFamily, fontWeight);\n charIndexInText++;\n }\n }\n } finally {\n ctx.restore();\n }\n}\n"],"names":[],"mappings":";;;;AAYA,SAAS,UAAU,IAAY,KAAqB;AAClD,SAAO,KAAK,MAAM,MAAM,MAAU,IAAI;AACxC;AAEO,SAAS,mBACd,KACA,OACA,aACA,cACA,eACA,MAAc,IACR;AACN,QAAM,aAAa,MAAM,YAAY;AACrC,MAAI,CAAC,WAAY;AAEjB,QAAM,WAAW,WAAW;AAC5B,QAAM,aAAa,WAAW;AAC9B,QAAM,aAAa,WAAW;AAC9B,QAAM,WAAW,WAAW;AAC5B,QAAM,cAAc,WAAW;AAC/B,QAAM,cAAc,WAAW,eAAe;AAC9C,QAAM,aAAa,WAAW,cAAc;AAE5C,QAAM,gBAAgB,MAAM,WAAW,oBAAoB,QAAQ;AACnE,QAAM,YAAY,MAAM,WAAW,aAAa;AAChD,QAAM,gBAAgB,MAAM,WAAW,iBAAiB;AACxD,QAAM,mBAAmB,MAAM,WAAW,oBAAoB;AAE9D,QAAM,WAAW,cAAc;AAC/B,QAAM,OAAO,kBAAkB,MAAM,MAAM,MAAM,UAAU;AAC3D,QAAM,aAAa,uBAAuB,MAAM,cAAc,SAAS,IAAI;AAC3E,QAAM,mBAAsC,CAAA;AAE5C,MAAI,MAAM,eAAe,MAAM,YAAY,SAAS,GAAG;AACrD,QAAI,YAAY;AAEhB,aAAS,YAAY,GAAG,YAAY,MAAM,YAAY,QAAQ,aAAa;AACzE,YAAM,OAAO,MAAM,YAAY,SAAS;AACxC,YAAM,YAAY,KAAK,KAAK,MAAM,EAAE;AACpC,YAAM,iBAAiB,UAAU,KAAK,SAAS,GAAG;AAClD,YAAM,eAAe,UAAU,KAAK,OAAO,GAAG;AAC9C,YAAM,gBACJ,UAAU,SAAS,KAAK,eAAe,kBAAkB,UAAU,SAAS;AAE9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,UAAM,cAAc,cAAc,SAAS,WAAW;AACtD,UAAM,SAAS;AAAA,MACb;AAAA,MACA;AAAA,MACA,MAAM,YAAY;AAAA,IAAA;AAGpB,QAAI,kBAAkB;AAEtB,aAAS,YAAY,GAAG,YAAY,cAAc,QAAQ,aAAa;AACrE,YAAM,OAAO,cAAc,SAAS;AACpC,YAAM,IAAI,SAAS,YAAY,WAAW,aAAa,WAAW;AAClE,YAAM,YAAY,iBAAiB,KAAK,MAAM,UAAU,YAAY,UAAU;AAC9E,UAAI,WAAW,cAAc,IAAI,YAAY;AAE7C,eAAS,IAAI,GAAG,IAAI,KAAK,QAAQ,KAAK;AACpC,cAAM,OAAO,KAAK,CAAC;AACnB,cAAM,SAAS,iBAAiB,eAAe;AAE/C,YAAI,QAAQ;AACV,gBAAM,aAAa,iBAAiB,OAAO;AAC3C,gBAAM,qBACJ,cAAc,gBAAgB,OAAO,aAAa;AAEpD,cAAI,YAAY,aAAa,gBAAgB;AAE7C,cAAI,oBAAoB;AACtB,gBAAI,cAAc;AAClB,gBAAI,aAAa,gBAAgB;AAAA,UACnC,OAAO;AACL,gBAAI,aAAa;AAAA,UACnB;AAEA,cAAI,eAAe,cAAc,GAAG;AAClC,gBAAI,cAAc;AAClB,gBAAI,YAAY;AAChB,gBAAI,WAAW,MAAM,UAAU,CAAC;AAAA,UAClC;AACA,cAAI,SAAS,MAAM,UAAU,CAAC;AAC9B,cAAI,aAAa;AAAA,QACnB;AAEA,oBAAY,iBAAiB,KAAK,MAAM,UAAU,YAAY,UAAU;AACxE;AAAA,MACF;AAAA,IACF;AAAA,EACF,UAAA;AACE,QAAI,QAAA;AAAA,EACN;AACF;"}
@@ -1 +1 @@
1
- {"version":3,"file":"word-by-word-renderer.d.ts","sourceRoot":"","sources":["../../../../src/stages/compose/text-renderers/word-by-word-renderer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAoB1C,wBAAgB,gBAAgB,CAC9B,GAAG,EAAE,iCAAiC,GAAG,wBAAwB,EACjE,KAAK,EAAE,SAAS,EAChB,WAAW,EAAE,MAAM,EACnB,YAAY,EAAE,MAAM,EACpB,aAAa,EAAE,MAAM,EACrB,GAAG,GAAE,MAAW,GACf,IAAI,CA6HN"}
1
+ {"version":3,"file":"word-by-word-renderer.d.ts","sourceRoot":"","sources":["../../../../src/stages/compose/text-renderers/word-by-word-renderer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAoB1C,wBAAgB,gBAAgB,CAC9B,GAAG,EAAE,iCAAiC,GAAG,wBAAwB,EACjE,KAAK,EAAE,SAAS,EAChB,WAAW,EAAE,MAAM,EACnB,YAAY,EAAE,MAAM,EACpB,aAAa,EAAE,MAAM,EACrB,GAAG,GAAE,MAAW,GACf,IAAI,CAoIN"}
@@ -65,50 +65,58 @@ function renderWordByWord(ctx, layer, canvasWidth, canvasHeight, relativeFrame,
65
65
  }
66
66
  }
67
67
  ctx.save();
68
- ctx.font = `${fontWeight} ${fontSize}px ${fontFamily}`;
69
- ctx.textAlign = "center";
70
- ctx.textBaseline = "middle";
71
- ctx.lineJoin = "round";
72
- ctx.lineCap = "round";
73
- for (const wordPos of wordPositions) {
74
- let currentFill = fill;
75
- let currentStroke = strokeColor;
76
- if (wordPos.timing) {
77
- const { startFrame, endFrame } = wordPos.timing;
78
- if (relativeFrame >= startFrame && relativeFrame <= endFrame) {
79
- const transitionProgressIn = interpolate(
80
- relativeFrame,
81
- [startFrame, startFrame + 3],
82
- [0, 1],
83
- {
84
- extrapolateLeft: "clamp",
85
- extrapolateRight: "clamp"
68
+ try {
69
+ ctx.font = `${fontWeight} ${fontSize}px ${fontFamily}`;
70
+ ctx.textAlign = "center";
71
+ ctx.textBaseline = "middle";
72
+ ctx.lineJoin = "round";
73
+ ctx.lineCap = "round";
74
+ for (const wordPos of wordPositions) {
75
+ let currentFill = fill;
76
+ let currentStroke = strokeColor;
77
+ if (wordPos.timing) {
78
+ const { startFrame, endFrame } = wordPos.timing;
79
+ if (relativeFrame >= startFrame && relativeFrame <= endFrame) {
80
+ const transitionProgressIn = interpolate(
81
+ relativeFrame,
82
+ [startFrame, startFrame + 3],
83
+ [0, 1],
84
+ {
85
+ extrapolateLeft: "clamp",
86
+ extrapolateRight: "clamp"
87
+ }
88
+ );
89
+ currentFill = interpolateColor(fill, highlightFill, transitionProgressIn);
90
+ if (strokeColor && highlightStroke) {
91
+ currentStroke = interpolateColor(strokeColor, highlightStroke, transitionProgressIn);
92
+ }
93
+ } else if (relativeFrame > endFrame) {
94
+ const transitionProgressOut = interpolate(
95
+ relativeFrame,
96
+ [endFrame, endFrame + 3],
97
+ [1, 0],
98
+ {
99
+ extrapolateLeft: "clamp",
100
+ extrapolateRight: "clamp"
101
+ }
102
+ );
103
+ currentFill = interpolateColor(fill, highlightFill, transitionProgressOut);
104
+ if (strokeColor && highlightStroke) {
105
+ currentStroke = interpolateColor(strokeColor, highlightStroke, transitionProgressOut);
86
106
  }
87
- );
88
- currentFill = interpolateColor(fill, highlightFill, transitionProgressIn);
89
- if (strokeColor && highlightStroke) {
90
- currentStroke = interpolateColor(strokeColor, highlightStroke, transitionProgressIn);
91
- }
92
- } else if (relativeFrame > endFrame) {
93
- const transitionProgressOut = interpolate(relativeFrame, [endFrame, endFrame + 3], [1, 0], {
94
- extrapolateLeft: "clamp",
95
- extrapolateRight: "clamp"
96
- });
97
- currentFill = interpolateColor(fill, highlightFill, transitionProgressOut);
98
- if (strokeColor && highlightStroke) {
99
- currentStroke = interpolateColor(strokeColor, highlightStroke, transitionProgressOut);
100
107
  }
101
108
  }
109
+ if (currentStroke && strokeWidth > 0) {
110
+ ctx.strokeStyle = currentStroke;
111
+ ctx.lineWidth = strokeWidth;
112
+ ctx.strokeText(wordPos.text, wordPos.x, wordPos.y);
113
+ }
114
+ ctx.fillStyle = currentFill;
115
+ ctx.fillText(wordPos.text, wordPos.x, wordPos.y);
102
116
  }
103
- if (currentStroke && strokeWidth > 0) {
104
- ctx.strokeStyle = currentStroke;
105
- ctx.lineWidth = strokeWidth;
106
- ctx.strokeText(wordPos.text, wordPos.x, wordPos.y);
107
- }
108
- ctx.fillStyle = currentFill;
109
- ctx.fillText(wordPos.text, wordPos.x, wordPos.y);
117
+ } finally {
118
+ ctx.restore();
110
119
  }
111
- ctx.restore();
112
120
  }
113
121
  export {
114
122
  renderWordByWord
@@ -1 +1 @@
1
- {"version":3,"file":"word-by-word-renderer.js","sources":["../../../../src/stages/compose/text-renderers/word-by-word-renderer.ts"],"sourcesContent":["import type { TextLayer } from '../types';\nimport { formEvenLinesWithWords } from '../text-utils/text-wrapper';\nimport { getLetterCaseText, measureTextWidth } from '../text-utils/text-metrics';\nimport { interpolate, interpolateColor } from './animation-utils';\nimport { needsSpaceBetweenWords } from '../text-utils/locale-detector';\nimport { calculateCaptionStartY } from '../text-utils/caption-position';\n\ninterface WordPosition {\n text: string;\n x: number;\n y: number;\n lineIndex: number;\n wordIndex: number;\n timing?: { startFrame: number; endFrame: number };\n}\n\nfunction usToFrame(us: number, fps: number): number {\n return Math.floor(us / (1000000 / fps));\n}\n\nexport function renderWordByWord(\n ctx: OffscreenCanvasRenderingContext2D | CanvasRenderingContext2D,\n layer: TextLayer,\n canvasWidth: number,\n canvasHeight: number,\n relativeFrame: number,\n fps: number = 30\n): void {\n const fontConfig = layer.fontConfig?.textStyle;\n if (!fontConfig) return;\n\n const fontSize = fontConfig.fontSize;\n const fontFamily = fontConfig.fontFamily;\n const fontWeight = fontConfig.fontWeight;\n const fill = fontConfig.fill;\n const strokeColor = fontConfig.strokeColor;\n const strokeWidth = fontConfig.strokeWidth || 0;\n const lineHeight = fontConfig.lineHeight || 1.2;\n\n const highlightFill = layer.animation?.highlightTextStyle?.fill || 'rgb(255, 215, 0)';\n const highlightStroke = layer.animation?.highlightTextStyle?.strokeColor || strokeColor;\n\n const maxWidth = canvasWidth * 0.64;\n const text = getLetterCaseText(layer.text, layer.letterCase);\n const needsSpace = needsSpaceBetweenWords(layer.localeCode || 'en-US', text);\n const words = text.split(needsSpace ? /\\s+/ : '');\n const lines = formEvenLinesWithWords(\n ctx,\n words,\n maxWidth,\n fontSize,\n needsSpace,\n fontFamily,\n fontWeight\n );\n\n const wordPositions: WordPosition[] = [];\n let wordIndex = 0;\n\n const totalHeight = lines.length * fontSize * lineHeight;\n const startY = calculateCaptionStartY(\n canvasHeight,\n totalHeight,\n layer.fontConfig?.globalPosition\n );\n\n for (let lineIndex = 0; lineIndex < lines.length; lineIndex++) {\n const line = lines[lineIndex]!;\n const lineWords = line.split(needsSpace ? /\\s+/ : '');\n const y = startY + lineIndex * fontSize * lineHeight + fontSize / 2;\n\n const lineWidth = measureTextWidth(ctx, line, fontSize, fontFamily, fontWeight);\n let currentX = canvasWidth / 2 - lineWidth / 2;\n\n for (const word of lineWords) {\n const wordWidth = measureTextWidth(ctx, word, fontSize, fontFamily, fontWeight);\n const wordTimingUs = layer.wordTimings?.[wordIndex];\n\n const wordTiming = wordTimingUs\n ? {\n startFrame: usToFrame(wordTimingUs.startUs, fps),\n endFrame: usToFrame(wordTimingUs.endUs, fps),\n }\n : undefined;\n\n wordPositions.push({\n text: word,\n x: currentX + wordWidth / 2,\n y,\n lineIndex,\n wordIndex,\n timing: wordTiming,\n });\n\n currentX +=\n wordWidth + (needsSpace ? measureTextWidth(ctx, ' ', fontSize, fontFamily, fontWeight) : 0);\n wordIndex++;\n }\n }\n\n ctx.save();\n ctx.font = `${fontWeight} ${fontSize}px ${fontFamily}`;\n ctx.textAlign = 'center';\n ctx.textBaseline = 'middle';\n ctx.lineJoin = 'round';\n ctx.lineCap = 'round';\n\n for (const wordPos of wordPositions) {\n let currentFill = fill;\n let currentStroke = strokeColor;\n\n if (wordPos.timing) {\n const { startFrame, endFrame } = wordPos.timing;\n\n if (relativeFrame >= startFrame && relativeFrame <= endFrame) {\n const transitionProgressIn = interpolate(\n relativeFrame,\n [startFrame, startFrame + 3],\n [0, 1],\n {\n extrapolateLeft: 'clamp',\n extrapolateRight: 'clamp',\n }\n );\n\n currentFill = interpolateColor(fill, highlightFill, transitionProgressIn);\n if (strokeColor && highlightStroke) {\n currentStroke = interpolateColor(strokeColor, highlightStroke, transitionProgressIn);\n }\n } else if (relativeFrame > endFrame) {\n const transitionProgressOut = interpolate(relativeFrame, [endFrame, endFrame + 3], [1, 0], {\n extrapolateLeft: 'clamp',\n extrapolateRight: 'clamp',\n });\n\n currentFill = interpolateColor(fill, highlightFill, transitionProgressOut);\n if (strokeColor && highlightStroke) {\n currentStroke = interpolateColor(strokeColor, highlightStroke, transitionProgressOut);\n }\n }\n }\n\n if (currentStroke && strokeWidth > 0) {\n ctx.strokeStyle = currentStroke;\n ctx.lineWidth = strokeWidth;\n ctx.strokeText(wordPos.text, wordPos.x, wordPos.y);\n }\n ctx.fillStyle = currentFill;\n ctx.fillText(wordPos.text, wordPos.x, wordPos.y);\n }\n\n ctx.restore();\n}\n"],"names":[],"mappings":";;;;;AAgBA,SAAS,UAAU,IAAY,KAAqB;AAClD,SAAO,KAAK,MAAM,MAAM,MAAU,IAAI;AACxC;AAEO,SAAS,iBACd,KACA,OACA,aACA,cACA,eACA,MAAc,IACR;AACN,QAAM,aAAa,MAAM,YAAY;AACrC,MAAI,CAAC,WAAY;AAEjB,QAAM,WAAW,WAAW;AAC5B,QAAM,aAAa,WAAW;AAC9B,QAAM,aAAa,WAAW;AAC9B,QAAM,OAAO,WAAW;AACxB,QAAM,cAAc,WAAW;AAC/B,QAAM,cAAc,WAAW,eAAe;AAC9C,QAAM,aAAa,WAAW,cAAc;AAE5C,QAAM,gBAAgB,MAAM,WAAW,oBAAoB,QAAQ;AACnE,QAAM,kBAAkB,MAAM,WAAW,oBAAoB,eAAe;AAE5E,QAAM,WAAW,cAAc;AAC/B,QAAM,OAAO,kBAAkB,MAAM,MAAM,MAAM,UAAU;AAC3D,QAAM,aAAa,uBAAuB,MAAM,cAAc,SAAS,IAAI;AAC3E,QAAM,QAAQ,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,cAAc;AAClB,QAAI,gBAAgB;AAEpB,QAAI,QAAQ,QAAQ;AAClB,YAAM,EAAE,YAAY,SAAA,IAAa,QAAQ;AAEzC,UAAI,iBAAiB,cAAc,iBAAiB,UAAU;AAC5D,cAAM,uBAAuB;AAAA,UAC3B;AAAA,UACA,CAAC,YAAY,aAAa,CAAC;AAAA,UAC3B,CAAC,GAAG,CAAC;AAAA,UACL;AAAA,YACE,iBAAiB;AAAA,YACjB,kBAAkB;AAAA,UAAA;AAAA,QACpB;AAGF,sBAAc,iBAAiB,MAAM,eAAe,oBAAoB;AACxE,YAAI,eAAe,iBAAiB;AAClC,0BAAgB,iBAAiB,aAAa,iBAAiB,oBAAoB;AAAA,QACrF;AAAA,MACF,WAAW,gBAAgB,UAAU;AACnC,cAAM,wBAAwB,YAAY,eAAe,CAAC,UAAU,WAAW,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG;AAAA,UACzF,iBAAiB;AAAA,UACjB,kBAAkB;AAAA,QAAA,CACnB;AAED,sBAAc,iBAAiB,MAAM,eAAe,qBAAqB;AACzE,YAAI,eAAe,iBAAiB;AAClC,0BAAgB,iBAAiB,aAAa,iBAAiB,qBAAqB;AAAA,QACtF;AAAA,MACF;AAAA,IACF;AAEA,QAAI,iBAAiB,cAAc,GAAG;AACpC,UAAI,cAAc;AAClB,UAAI,YAAY;AAChB,UAAI,WAAW,QAAQ,MAAM,QAAQ,GAAG,QAAQ,CAAC;AAAA,IACnD;AACA,QAAI,YAAY;AAChB,QAAI,SAAS,QAAQ,MAAM,QAAQ,GAAG,QAAQ,CAAC;AAAA,EACjD;AAEA,MAAI,QAAA;AACN;"}
1
+ {"version":3,"file":"word-by-word-renderer.js","sources":["../../../../src/stages/compose/text-renderers/word-by-word-renderer.ts"],"sourcesContent":["import type { TextLayer } from '../types';\nimport { formEvenLinesWithWords } from '../text-utils/text-wrapper';\nimport { getLetterCaseText, measureTextWidth } from '../text-utils/text-metrics';\nimport { interpolate, interpolateColor } from './animation-utils';\nimport { needsSpaceBetweenWords } from '../text-utils/locale-detector';\nimport { calculateCaptionStartY } from '../text-utils/caption-position';\n\ninterface WordPosition {\n text: string;\n x: number;\n y: number;\n lineIndex: number;\n wordIndex: number;\n timing?: { startFrame: number; endFrame: number };\n}\n\nfunction usToFrame(us: number, fps: number): number {\n return Math.floor(us / (1000000 / fps));\n}\n\nexport function renderWordByWord(\n ctx: OffscreenCanvasRenderingContext2D | CanvasRenderingContext2D,\n layer: TextLayer,\n canvasWidth: number,\n canvasHeight: number,\n relativeFrame: number,\n fps: number = 30\n): void {\n const fontConfig = layer.fontConfig?.textStyle;\n if (!fontConfig) return;\n\n const fontSize = fontConfig.fontSize;\n const fontFamily = fontConfig.fontFamily;\n const fontWeight = fontConfig.fontWeight;\n const fill = fontConfig.fill;\n const strokeColor = fontConfig.strokeColor;\n const strokeWidth = fontConfig.strokeWidth || 0;\n const lineHeight = fontConfig.lineHeight || 1.2;\n\n const highlightFill = layer.animation?.highlightTextStyle?.fill || 'rgb(255, 215, 0)';\n const highlightStroke = layer.animation?.highlightTextStyle?.strokeColor || strokeColor;\n\n const maxWidth = canvasWidth * 0.64;\n const text = getLetterCaseText(layer.text, layer.letterCase);\n const needsSpace = needsSpaceBetweenWords(layer.localeCode || 'en-US', text);\n const words = text.split(needsSpace ? /\\s+/ : '');\n const lines = formEvenLinesWithWords(\n ctx,\n words,\n maxWidth,\n fontSize,\n needsSpace,\n fontFamily,\n fontWeight\n );\n\n const wordPositions: WordPosition[] = [];\n let wordIndex = 0;\n\n const totalHeight = lines.length * fontSize * lineHeight;\n const startY = calculateCaptionStartY(\n canvasHeight,\n totalHeight,\n layer.fontConfig?.globalPosition\n );\n\n for (let lineIndex = 0; lineIndex < lines.length; lineIndex++) {\n const line = lines[lineIndex]!;\n const lineWords = line.split(needsSpace ? /\\s+/ : '');\n const y = startY + lineIndex * fontSize * lineHeight + fontSize / 2;\n\n const lineWidth = measureTextWidth(ctx, line, fontSize, fontFamily, fontWeight);\n let currentX = canvasWidth / 2 - lineWidth / 2;\n\n for (const word of lineWords) {\n const wordWidth = measureTextWidth(ctx, word, fontSize, fontFamily, fontWeight);\n const wordTimingUs = layer.wordTimings?.[wordIndex];\n\n const wordTiming = wordTimingUs\n ? {\n startFrame: usToFrame(wordTimingUs.startUs, fps),\n endFrame: usToFrame(wordTimingUs.endUs, fps),\n }\n : undefined;\n\n wordPositions.push({\n text: word,\n x: currentX + wordWidth / 2,\n y,\n lineIndex,\n wordIndex,\n timing: wordTiming,\n });\n\n currentX +=\n wordWidth + (needsSpace ? measureTextWidth(ctx, ' ', fontSize, fontFamily, fontWeight) : 0);\n wordIndex++;\n }\n }\n\n ctx.save();\n try {\n ctx.font = `${fontWeight} ${fontSize}px ${fontFamily}`;\n ctx.textAlign = 'center';\n ctx.textBaseline = 'middle';\n ctx.lineJoin = 'round';\n ctx.lineCap = 'round';\n\n for (const wordPos of wordPositions) {\n let currentFill = fill;\n let currentStroke = strokeColor;\n\n if (wordPos.timing) {\n const { startFrame, endFrame } = wordPos.timing;\n\n if (relativeFrame >= startFrame && relativeFrame <= endFrame) {\n const transitionProgressIn = interpolate(\n relativeFrame,\n [startFrame, startFrame + 3],\n [0, 1],\n {\n extrapolateLeft: 'clamp',\n extrapolateRight: 'clamp',\n }\n );\n\n currentFill = interpolateColor(fill, highlightFill, transitionProgressIn);\n if (strokeColor && highlightStroke) {\n currentStroke = interpolateColor(strokeColor, highlightStroke, transitionProgressIn);\n }\n } else if (relativeFrame > endFrame) {\n const transitionProgressOut = interpolate(\n relativeFrame,\n [endFrame, endFrame + 3],\n [1, 0],\n {\n extrapolateLeft: 'clamp',\n extrapolateRight: 'clamp',\n }\n );\n\n currentFill = interpolateColor(fill, highlightFill, transitionProgressOut);\n if (strokeColor && highlightStroke) {\n currentStroke = interpolateColor(strokeColor, highlightStroke, transitionProgressOut);\n }\n }\n }\n\n if (currentStroke && strokeWidth > 0) {\n ctx.strokeStyle = currentStroke;\n ctx.lineWidth = strokeWidth;\n ctx.strokeText(wordPos.text, wordPos.x, wordPos.y);\n }\n ctx.fillStyle = currentFill;\n ctx.fillText(wordPos.text, wordPos.x, wordPos.y);\n }\n } finally {\n ctx.restore();\n }\n}\n"],"names":[],"mappings":";;;;;AAgBA,SAAS,UAAU,IAAY,KAAqB;AAClD,SAAO,KAAK,MAAM,MAAM,MAAU,IAAI;AACxC;AAEO,SAAS,iBACd,KACA,OACA,aACA,cACA,eACA,MAAc,IACR;AACN,QAAM,aAAa,MAAM,YAAY;AACrC,MAAI,CAAC,WAAY;AAEjB,QAAM,WAAW,WAAW;AAC5B,QAAM,aAAa,WAAW;AAC9B,QAAM,aAAa,WAAW;AAC9B,QAAM,OAAO,WAAW;AACxB,QAAM,cAAc,WAAW;AAC/B,QAAM,cAAc,WAAW,eAAe;AAC9C,QAAM,aAAa,WAAW,cAAc;AAE5C,QAAM,gBAAgB,MAAM,WAAW,oBAAoB,QAAQ;AACnE,QAAM,kBAAkB,MAAM,WAAW,oBAAoB,eAAe;AAE5E,QAAM,WAAW,cAAc;AAC/B,QAAM,OAAO,kBAAkB,MAAM,MAAM,MAAM,UAAU;AAC3D,QAAM,aAAa,uBAAuB,MAAM,cAAc,SAAS,IAAI;AAC3E,QAAM,QAAQ,KAAK,MAAM,aAAa,QAAQ,EAAE;AAChD,QAAM,QAAQ;AAAA,IACZ;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EAAA;AAGF,QAAM,gBAAgC,CAAA;AACtC,MAAI,YAAY;AAEhB,QAAM,cAAc,MAAM,SAAS,WAAW;AAC9C,QAAM,SAAS;AAAA,IACb;AAAA,IACA;AAAA,IACA,MAAM,YAAY;AAAA,EAAA;AAGpB,WAAS,YAAY,GAAG,YAAY,MAAM,QAAQ,aAAa;AAC7D,UAAM,OAAO,MAAM,SAAS;AAC5B,UAAM,YAAY,KAAK,MAAM,aAAa,QAAQ,EAAE;AACpD,UAAM,IAAI,SAAS,YAAY,WAAW,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WAAW,gBAAgB,UAAU;AACnC,gBAAM,wBAAwB;AAAA,YAC5B;AAAA,YACA,CAAC,UAAU,WAAW,CAAC;AAAA,YACvB,CAAC,GAAG,CAAC;AAAA,YACL;AAAA,cACE,iBAAiB;AAAA,cACjB,kBAAkB;AAAA,YAAA;AAAA,UACpB;AAGF,wBAAc,iBAAiB,MAAM,eAAe,qBAAqB;AACzE,cAAI,eAAe,iBAAiB;AAClC,4BAAgB,iBAAiB,aAAa,iBAAiB,qBAAqB;AAAA,UACtF;AAAA,QACF;AAAA,MACF;AAEA,UAAI,iBAAiB,cAAc,GAAG;AACpC,YAAI,cAAc;AAClB,YAAI,YAAY;AAChB,YAAI,WAAW,QAAQ,MAAM,QAAQ,GAAG,QAAQ,CAAC;AAAA,MACnD;AACA,UAAI,YAAY;AAChB,UAAI,SAAS,QAAQ,MAAM,QAAQ,GAAG,QAAQ,CAAC;AAAA,IACjD;AAAA,EACF,UAAA;AACE,QAAI,QAAA;AAAA,EACN;AACF;"}
@@ -1 +1 @@
1
- {"version":3,"file":"word-fancy-renderer.d.ts","sourceRoot":"","sources":["../../../../src/stages/compose/text-renderers/word-fancy-renderer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAqB1C,wBAAgB,qBAAqB,CACnC,GAAG,EAAE,iCAAiC,GAAG,wBAAwB,EACjE,KAAK,EAAE,SAAS,EAChB,WAAW,EAAE,MAAM,EACnB,YAAY,EAAE,MAAM,EACpB,aAAa,EAAE,MAAM,EACrB,GAAG,GAAE,MAAW,GACf,IAAI,CAsIN"}
1
+ {"version":3,"file":"word-fancy-renderer.d.ts","sourceRoot":"","sources":["../../../../src/stages/compose/text-renderers/word-fancy-renderer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAqB1C,wBAAgB,qBAAqB,CACnC,GAAG,EAAE,iCAAiC,GAAG,wBAAwB,EACjE,KAAK,EAAE,SAAS,EAChB,WAAW,EAAE,MAAM,EACnB,YAAY,EAAE,MAAM,EACpB,aAAa,EAAE,MAAM,EACrB,GAAG,GAAE,MAAW,GACf,IAAI,CA2IN"}