jassub 1.5.13 → 1.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/jassub.es.js CHANGED
@@ -1,440 +1,475 @@
1
- var u = Object.defineProperty;
2
- var f = (d, e, t) => e in d ? u(d, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : d[e] = t;
3
- var v = (d, e, t) => (f(d, typeof e != "symbol" ? e + "" : e, t), t);
4
- !("requestVideoFrameCallback" in HTMLVideoElement.prototype) && "getVideoPlaybackQuality" in HTMLVideoElement.prototype && (HTMLVideoElement.prototype._rvfcpolyfillmap = {}, HTMLVideoElement.prototype.requestVideoFrameCallback = function(d) {
5
- const e = this.getVideoPlaybackQuality(), t = this.mozPresentedFrames || this.mozPaintedFrames || e.totalVideoFrames - e.droppedVideoFrames, s = (r, i) => {
6
- const o = this.getVideoPlaybackQuality(), c = this.mozPresentedFrames || this.mozPaintedFrames || o.totalVideoFrames - o.droppedVideoFrames;
7
- if (c > t) {
8
- const l = this.mozFrameDelay || o.totalFrameDelay - e.totalFrameDelay || 0, m = i - r;
9
- d(i, {
10
- presentationTime: i + l * 1e3,
11
- expectedDisplayTime: i + m,
12
- width: this.videoWidth,
13
- height: this.videoHeight,
14
- mediaTime: Math.max(0, this.currentTime || 0) + m / 1e3,
15
- presentedFrames: c,
16
- processingDuration: l
17
- }), delete this._rvfcpolyfillmap[a];
18
- } else
19
- this._rvfcpolyfillmap[a] = requestAnimationFrame((l) => s(i, l));
20
- }, a = Date.now(), n = performance.now();
21
- return this._rvfcpolyfillmap[a] = requestAnimationFrame((r) => s(n, r)), a;
22
- }, HTMLVideoElement.prototype.cancelVideoFrameCallback = function(d) {
23
- cancelAnimationFrame(this._rvfcpolyfillmap[d]), delete this._rvfcpolyfillmap[d];
24
- });
25
- const h = class extends EventTarget {
26
- /**
27
- * @param {Object} options Settings object.
28
- * @param {HTMLVideoElement} options.video Video to use as target for rendering and event listeners. Optional if canvas is specified instead.
29
- * @param {HTMLCanvasElement} [options.canvas=HTMLCanvasElement] Canvas to use for manual handling. Not required if video is specified.
30
- * @param {'js'|'wasm'} [options.blendMode='js'] Which image blending mode to use. WASM will perform better on lower end devices, JS will perform better if the device and browser supports hardware acceleration.
31
- * @param {Boolean} [options.asyncRender=true] Whether or not to use async rendering, which offloads the CPU by creating image bitmaps on the GPU.
32
- * @param {Boolean} [options.offscreenRender=true] Whether or not to render things fully on the worker, greatly reduces CPU usage.
33
- * @param {Boolean} [options.onDemandRender=true] Whether or not to render subtitles as the video player decodes renders, rather than predicting which frame the player is on using events.
34
- * @param {Number} [options.targetFps=24] Target FPS to render subtitles at. Ignored when onDemandRender is enabled.
35
- * @param {Number} [options.timeOffset=0] Subtitle time offset in seconds.
36
- * @param {Boolean} [options.debug=false] Whether or not to print debug information.
37
- * @param {Number} [options.prescaleFactor=1.0] Scale down (< 1.0) the subtitles canvas to improve performance at the expense of quality, or scale it up (> 1.0).
38
- * @param {Number} [options.prescaleHeightLimit=1080] The height in pixels beyond which the subtitles canvas won't be prescaled.
39
- * @param {Number} [options.maxRenderHeight=0] The maximum rendering height in pixels of the subtitles canvas. Beyond this subtitles will be upscaled by the browser.
40
- * @param {Boolean} [options.dropAllAnimations=false] Attempt to discard all animated tags. Enabling this may severly mangle complex subtitles and should only be considered as an last ditch effort of uncertain success for hardware otherwise incapable of displaing anything. Will not reliably work with manually edited or allocated events.
41
- * @param {String} [options.workerUrl='jassub-worker.js'] The URL of the worker.
42
- * @param {String} [options.legacyWorkerUrl='jassub-worker-legacy.js'] The URL of the legacy worker. Only loaded if the browser doesn't support WASM.
43
- * @param {String} [options.subUrl=options.subContent] The URL of the subtitle file to play.
44
- * @param {String} [options.subContent=options.subUrl] The content of the subtitle file to play.
45
- * @param {String[]|Uint8Array[]} [options.fonts] An array of links or Uint8Arrays to the fonts used in the subtitle. If Uint8Array is used the array is copied, not referenced. This forces all the fonts in this array to be loaded by the renderer, regardless of if they are used.
46
- * @param {Object} [options.availableFonts={'liberation sans': './default.woff2'}] Object with all available fonts - Key is font family in lower case, value is link or Uint8Array: { arial: '/font1.ttf' }. These fonts are selectively loaded if detected as used in the current subtitle track.
47
- * @param {String} [options.fallbackFont='liberation sans'] The font family key of the fallback font in availableFonts to use if the other font for the style is missing special glyphs or unicode.
48
- * @param {Boolean} [options.useLocalFonts=false] If the Local Font Access API is enabled [chrome://flags/#font-access], the library will query for permissions to use local fonts and use them if any are missing. The permission can be queried beforehand using navigator.permissions.request({ name: 'local-fonts' }).
49
- * @param {Number} [options.libassMemoryLimit] libass bitmap cache memory limit in MiB (approximate).
50
- * @param {Number} [options.libassGlyphLimit] libass glyph cache memory limit in MiB (approximate).
51
- */
52
- constructor(e = {}) {
53
- super(), globalThis.Worker || this.destroy("Worker not supported"), h._test();
54
- const t = e.blendMode || "js", s = typeof createImageBitmap < "u" && (e.asyncRender ?? !0), a = typeof OffscreenCanvas < "u" && (e.offscreenRender ?? !0);
55
- this._onDemandRender = "requestVideoFrameCallback" in HTMLVideoElement.prototype && (e.onDemandRender ?? !0), this.timeOffset = e.timeOffset || 0, this._video = e.video, this._videoHeight = 0, this._videoWidth = 0, this._canvas = e.canvas, this._video && !this._canvas ? (this._canvasParent = document.createElement("div"), this._canvasParent.className = "JASSUB", this._canvasParent.style.position = "relative", this._canvas = document.createElement("canvas"), this._canvas.style.display = "block", this._canvas.style.position = "absolute", this._canvas.style.pointerEvents = "none", this._canvasParent.appendChild(this._canvas), this._video.nextSibling ? this._video.parentNode.insertBefore(this._canvasParent, this._video.nextSibling) : this._video.parentNode.appendChild(this._canvasParent)) : this._canvas || this.destroy("Don't know where to render: you should give video or canvas in options."), this._bufferCanvas = document.createElement("canvas"), this._bufferCtx = this._bufferCanvas.getContext("2d", { desynchronized: !0, willReadFrequently: !0 }), this._canvasctrl = a ? this._canvas.transferControlToOffscreen() : this._canvas, this._ctx = !a && this._canvasctrl.getContext("2d", { desynchronized: !0 }), this._lastRenderTime = 0, this.debug = !!e.debug, this.prescaleFactor = e.prescaleFactor || 1, this.prescaleHeightLimit = e.prescaleHeightLimit || 1080, this.maxRenderHeight = e.maxRenderHeight || 0, this._worker = new Worker(h._supportsWebAssembly ? e.workerUrl || "jassub-worker.js" : e.legacyWorkerUrl || "jassub-worker-legacy.js"), this._worker.onmessage = (n) => this._onmessage(n), this._worker.onerror = (n) => this._error(n), this._loaded = new Promise((n) => {
56
- this._init = () => {
57
- var r;
58
- this._destroyed || (this._worker.postMessage({
59
- target: "init",
60
- asyncRender: s,
61
- onDemandRender: this._onDemandRender,
62
- width: this._canvasctrl.width || 0,
63
- height: this._canvasctrl.height || 0,
64
- preMain: !0,
65
- blendMode: t,
66
- subUrl: e.subUrl,
67
- subContent: e.subContent || null,
68
- fonts: e.fonts || [],
69
- availableFonts: e.availableFonts || { "liberation sans": "./default.woff2" },
70
- fallbackFont: e.fallbackFont || "liberation sans",
71
- debug: this.debug,
72
- targetFps: e.targetFps || 24,
73
- dropAllAnimations: e.dropAllAnimations,
74
- libassMemoryLimit: e.libassMemoryLimit || 0,
75
- libassGlyphLimit: e.libassGlyphLimit || 0,
76
- hasAlphaBug: h._hasAlphaBug,
77
- useLocalFonts: "queryLocalFonts" in self && (e.useLocalFonts ?? !0)
78
- }), a === !0 && this.sendMessage("offscreenCanvas", null, [this._canvasctrl]), this._boundResize = this.resize.bind(this), this._boundTimeUpdate = this._timeupdate.bind(this), this._boundSetRate = this.setRate.bind(this), this._video && this.setVideo(e.video), this._onDemandRender && (this.busy = !1, this._lastDemandTime = null, (r = this._video) == null || r.requestVideoFrameCallback(this._handleRVFC.bind(this))), n());
79
- };
80
- });
81
- }
82
- static _test() {
83
- if (h._supportsWebAssembly !== null)
84
- return null;
85
- const e = document.createElement("canvas"), t = e.getContext("2d", { willReadFrequently: !0 });
86
- if (typeof ImageData.prototype.constructor == "function")
87
- try {
88
- new ImageData(new Uint8ClampedArray([0, 0, 0, 0]), 1, 1);
89
- } catch {
90
- console.log("Detected that ImageData is not constructable despite browser saying so"), self.ImageData = function(o, c, l) {
91
- const m = t.createImageData(c, l);
92
- return o && m.data.set(o), m;
93
- };
94
- }
95
- try {
96
- if (typeof WebAssembly == "object" && typeof WebAssembly.instantiate == "function") {
97
- const i = new WebAssembly.Module(Uint8Array.of(0, 97, 115, 109, 1, 0, 0, 0));
98
- i instanceof WebAssembly.Module && (h._supportsWebAssembly = new WebAssembly.Instance(i) instanceof WebAssembly.Instance);
99
- }
100
- } catch {
101
- h._supportsWebAssembly = !1;
102
- }
103
- const s = document.createElement("canvas"), a = s.getContext("2d", { willReadFrequently: !0 });
104
- e.width = s.width = 1, e.height = s.height = 1, t.clearRect(0, 0, 1, 1), a.clearRect(0, 0, 1, 1);
105
- const n = a.getImageData(0, 0, 1, 1).data;
106
- t.putImageData(new ImageData(new Uint8ClampedArray([0, 255, 0, 0]), 1, 1), 0, 0), a.drawImage(e, 0, 0);
107
- const r = a.getImageData(0, 0, 1, 1).data;
108
- h._hasAlphaBug = n[1] !== r[1], h._hasAlphaBug && console.log("Detected a browser having issue with transparent pixels, applying workaround"), e.remove(), s.remove();
109
- }
110
- /**
111
- * Resize the canvas to given parameters. Auto-generated if values are ommited.
112
- * @param {Number} [width=0]
113
- * @param {Number} [height=0]
114
- * @param {Number} [top=0]
115
- * @param {Number} [left=0]
116
- * @param {Boolean} [force=false]
117
- */
118
- resize(e = 0, t = 0, s = 0, a = 0, n = ((r) => (r = this._video) == null ? void 0 : r.paused)()) {
119
- if ((!e || !t) && this._video) {
120
- const i = this._getVideoPosition();
121
- let o = null;
122
- if (this._videoWidth) {
123
- const c = this._video.videoWidth / this._videoWidth, l = this._video.videoHeight / this._videoHeight;
124
- o = this._computeCanvasSize((i.width || 0) / c, (i.height || 0) / l);
125
- } else
126
- o = this._computeCanvasSize(i.width || 0, i.height || 0);
127
- e = o.width, t = o.height, this._canvasParent && (s = i.y - (this._canvasParent.getBoundingClientRect().top - this._video.getBoundingClientRect().top), a = i.x), this._canvas.style.width = i.width + "px", this._canvas.style.height = i.height + "px";
128
- }
129
- this._canvas.style.top = s + "px", this._canvas.style.left = a + "px", this.sendMessage("canvas", { width: e, height: t, force: n && this.busy === !1 });
130
- }
131
- _getVideoPosition(e = this._video.videoWidth, t = this._video.videoHeight) {
132
- const s = e / t, { offsetWidth: a, offsetHeight: n } = this._video, r = a / n;
133
- e = a, t = n, r > s ? e = Math.floor(n * s) : t = Math.floor(a / s);
134
- const i = (a - e) / 2, o = (n - t) / 2;
135
- return { width: e, height: t, x: i, y: o };
136
- }
137
- _computeCanvasSize(e = 0, t = 0) {
138
- const s = this.prescaleFactor <= 0 ? 1 : this.prescaleFactor, a = self.devicePixelRatio || 1;
139
- if (e = e * a, t = t * a, t <= 0 || e <= 0)
140
- e = 0, t = 0;
141
- else {
142
- const n = s < 1 ? -1 : 1;
143
- let r = t * a;
144
- n * r * s <= n * this.prescaleHeightLimit ? r *= s : n * r < n * this.prescaleHeightLimit && (r = this.prescaleHeightLimit), this.maxRenderHeight > 0 && r > this.maxRenderHeight && (r = this.maxRenderHeight), e *= r / t, t = r;
145
- }
146
- return { width: e, height: t };
147
- }
148
- _timeupdate({ type: e }) {
149
- const s = {
150
- seeking: !0,
151
- waiting: !0,
152
- playing: !1
153
- }[e];
154
- s != null && (this._playstate = s), this.setCurrentTime(this._video.paused || this._playstate, this._video.currentTime + this.timeOffset);
155
- }
156
- /**
157
- * Change the video to use as target for event listeners.
158
- * @param {HTMLVideoElement} video
159
- */
160
- setVideo(e) {
161
- e instanceof HTMLVideoElement ? (this._removeListeners(), this._video = e, this._onDemandRender ? this._video.requestVideoFrameCallback(this._handleRVFC.bind(this)) : (this._playstate = e.paused, e.addEventListener("timeupdate", this._boundTimeUpdate, !1), e.addEventListener("progress", this._boundTimeUpdate, !1), e.addEventListener("waiting", this._boundTimeUpdate, !1), e.addEventListener("seeking", this._boundTimeUpdate, !1), e.addEventListener("playing", this._boundTimeUpdate, !1), e.addEventListener("ratechange", this._boundSetRate, !1), e.addEventListener("resize", this._boundResize)), e.videoWidth > 0 && this.resize(), typeof ResizeObserver < "u" && (this._ro || (this._ro = new ResizeObserver(() => this.resize())), this._ro.observe(e))) : this._error("Video element invalid!");
162
- }
163
- runBenchmark() {
164
- this.sendMessage("runBenchmark");
165
- }
166
- /**
167
- * Overwrites the current subtitle content.
168
- * @param {String} url URL to load subtitles from.
169
- */
170
- setTrackByUrl(e) {
171
- this.sendMessage("setTrackByUrl", { url: e });
172
- }
173
- /**
174
- * Overwrites the current subtitle content.
175
- * @param {String} content Content of the ASS file.
176
- */
177
- setTrack(e) {
178
- this.sendMessage("setTrack", { content: e });
179
- }
180
- /**
181
- * Free currently used subtitle track.
182
- */
183
- freeTrack() {
184
- this.sendMessage("freeTrack");
185
- }
186
- /**
187
- * Sets the playback state of the media.
188
- * @param {Boolean} isPaused Pause/Play subtitle playback.
189
- */
190
- setIsPaused(e) {
191
- this.sendMessage("video", { isPaused: e });
192
- }
193
- /**
194
- * Sets the playback rate of the media [speed multiplier].
195
- * @param {Number} rate Playback rate.
196
- */
197
- setRate(e) {
198
- this.sendMessage("video", { rate: e });
199
- }
200
- /**
201
- * Sets the current time, playback state and rate of the subtitles.
202
- * @param {Boolean} [isPaused] Pause/Play subtitle playback.
203
- * @param {Number} [currentTime] Time in seconds.
204
- * @param {Number} [rate] Playback rate.
205
- */
206
- setCurrentTime(e, t, s) {
207
- this.sendMessage("video", { isPaused: e, currentTime: t, rate: s });
208
- }
209
- /**
210
- * @typedef {Object} ASS_Event
211
- * @property {Number} Start Start Time of the Event, in 0:00:00:00 format ie. Hrs:Mins:Secs:hundredths. This is the time elapsed during script playback at which the text will appear onscreen. Note that there is a single digit for the hours!
212
- * @property {Number} Duration End Time of the Event, in 0:00:00:00 format ie. Hrs:Mins:Secs:hundredths. This is the time elapsed during script playback at which the text will disappear offscreen. Note that there is a single digit for the hours!
213
- * @property {String} Style Style name. If it is "Default", then your own *Default style will be subtituted.
214
- * @property {String} Name Character name. This is the name of the character who speaks the dialogue. It is for information only, to make the script is easier to follow when editing/timing.
215
- * @property {Number} MarginL 4-figure Left Margin override. The values are in pixels. All zeroes means the default margins defined by the style are used.
216
- * @property {Number} MarginR 4-figure Right Margin override. The values are in pixels. All zeroes means the default margins defined by the style are used.
217
- * @property {Number} MarginV 4-figure Bottom Margin override. The values are in pixels. All zeroes means the default margins defined by the style are used.
218
- * @property {String} Effect Transition Effect. This is either empty, or contains information for one of the three transition effects implemented in SSA v4.x
219
- * @property {String} Text Subtitle Text. This is the actual text which will be displayed as a subtitle onscreen. Everything after the 9th comma is treated as the subtitle text, so it can include commas.
220
- * @property {Number} ReadOrder Number in order of which to read this event.
221
- * @property {Number} Layer Z-index overlap in which to render this event.
222
- * @property {Number} _index (Internal) index of the event.
223
- */
224
- /**
225
- * Create a new ASS event directly.
226
- * @param {ASS_Event} event
227
- */
228
- createEvent(e) {
229
- this.sendMessage("createEvent", { event: e });
230
- }
231
- /**
232
- * Overwrite the data of the event with the specified index.
233
- * @param {ASS_Event} event
234
- * @param {Number} index
235
- */
236
- setEvent(e, t) {
237
- this.sendMessage("setEvent", { event: e, index: t });
238
- }
239
- /**
240
- * Remove the event with the specified index.
241
- * @param {Number} index
242
- */
243
- removeEvent(e) {
244
- this.sendMessage("removeEvent", { index: e });
245
- }
246
- /**
247
- * Get all ASS events.
248
- * @param {function(Error|null, ASS_Event)} callback Function to callback when worker returns the events.
249
- */
250
- getEvents(e) {
251
- this._fetchFromWorker({
252
- target: "getEvents"
253
- }, (t, { events: s }) => {
254
- e(t, s);
255
- });
256
- }
257
- /**
258
- * @typedef {Object} ASS_Style
259
- * @property {String} Name The name of the Style. Case sensitive. Cannot include commas.
260
- * @property {String} FontName The fontname as used by Windows. Case-sensitive.
261
- * @property {Number} FontSize Font size.
262
- * @property {Number} PrimaryColour A long integer BGR (blue-green-red) value. ie. the byte order in the hexadecimal equivelent of this number is BBGGRR
263
- * @property {Number} SecondaryColour A long integer BGR (blue-green-red) value. ie. the byte order in the hexadecimal equivelent of this number is BBGGRR
264
- * @property {Number} OutlineColour A long integer BGR (blue-green-red) value. ie. the byte order in the hexadecimal equivelent of this number is BBGGRR
265
- * @property {Number} BackColour This is the colour of the subtitle outline or shadow, if these are used. A long integer BGR (blue-green-red) value. ie. the byte order in the hexadecimal equivelent of this number is BBGGRR.
266
- * @property {Number} Bold This defines whether text is bold (true) or not (false). -1 is True, 0 is False. This is independant of the Italic attribute - you can have have text which is both bold and italic.
267
- * @property {Number} Italic Italic. This defines whether text is italic (true) or not (false). -1 is True, 0 is False. This is independant of the bold attribute - you can have have text which is both bold and italic.
268
- * @property {Number} Underline -1 or 0
269
- * @property {Number} StrikeOut -1 or 0
270
- * @property {Number} ScaleX Modifies the width of the font. [percent]
271
- * @property {Number} ScaleY Modifies the height of the font. [percent]
272
- * @property {Number} Spacing Extra space between characters. [pixels]
273
- * @property {Number} Angle The origin of the rotation is defined by the alignment. Can be a floating point number. [degrees]
274
- * @property {Number} BorderStyle 1=Outline + drop shadow, 3=Opaque box
275
- * @property {Number} Outline If BorderStyle is 1, then this specifies the width of the outline around the text, in pixels. Values may be 0, 1, 2, 3 or 4.
276
- * @property {Number} Shadow If BorderStyle is 1, then this specifies the depth of the drop shadow behind the text, in pixels. Values may be 0, 1, 2, 3 or 4. Drop shadow is always used in addition to an outline - SSA will force an outline of 1 pixel if no outline width is given.
277
- * @property {Number} Alignment This sets how text is "justified" within the Left/Right onscreen margins, and also the vertical placing. Values may be 1=Left, 2=Centered, 3=Right. Add 4 to the value for a "Toptitle". Add 8 to the value for a "Midtitle". eg. 5 = left-justified toptitle
278
- * @property {Number} MarginL This defines the Left Margin in pixels. It is the distance from the left-hand edge of the screen.The three onscreen margins (MarginL, MarginR, MarginV) define areas in which the subtitle text will be displayed.
279
- * @property {Number} MarginR This defines the Right Margin in pixels. It is the distance from the right-hand edge of the screen. The three onscreen margins (MarginL, MarginR, MarginV) define areas in which the subtitle text will be displayed.
280
- * @property {Number} MarginV This defines the vertical Left Margin in pixels. For a subtitle, it is the distance from the bottom of the screen. For a toptitle, it is the distance from the top of the screen. For a midtitle, the value is ignored - the text will be vertically centred.
281
- * @property {Number} Encoding This specifies the font character set or encoding and on multi-lingual Windows installations it provides access to characters used in multiple than one languages. It is usually 0 (zero) for English (Western, ANSI) Windows.
282
- * @property {Number} treat_fontname_as_pattern
283
- * @property {Number} Blur
284
- * @property {Number} Justify
285
- */
286
- /**
287
- * Create a new ASS style directly.
288
- * @param {ASS_Style} event
289
- */
290
- createStyle(e) {
291
- this.sendMessage("createStyle", { style: e });
292
- }
293
- /**
294
- * Overwrite the data of the style with the specified index.
295
- * @param {ASS_Style} event
296
- * @param {Number} index
297
- */
298
- setStyle(e, t) {
299
- this.sendMessage("setStyle", { event: e, index: t });
300
- }
301
- /**
302
- * Remove the style with the specified index.
303
- * @param {Number} index
304
- */
305
- removeStyle(e) {
306
- this.sendMessage("removeStyle", { index: e });
307
- }
308
- /**
309
- * Get all ASS styles.
310
- * @param {function(Error|null, ASS_Style)} callback Function to callback when worker returns the styles.
311
- */
312
- getStyles(e) {
313
- this._fetchFromWorker({
314
- target: "getStyles"
315
- }, (t, { styles: s }) => {
316
- e(t, s);
317
- });
318
- }
319
- /**
320
- * Adds a font to the renderer.
321
- * @param {String|Uint8Array} font Font to add.
322
- */
323
- addFont(e) {
324
- this.sendMessage("addFont", { font: e });
325
- }
326
- _sendLocalFont(e) {
327
- try {
328
- queryLocalFonts().then((t) => {
329
- const s = t == null ? void 0 : t.find((a) => a.fullName.toLowerCase() === e);
330
- s && s.blob().then((a) => {
331
- a.arrayBuffer().then((n) => {
332
- this.addFont(new Uint8Array(n));
333
- });
334
- });
335
- });
336
- } catch (t) {
337
- console.warn("Local fonts API:", t);
338
- }
339
- }
340
- _getLocalFont({ font: e }) {
341
- var t;
342
- try {
343
- (t = navigator == null ? void 0 : navigator.permissions) != null && t.query ? navigator.permissions.query({ name: "local-fonts" }).then((s) => {
344
- s.state === "granted" && this._sendLocalFont(e);
345
- }) : this._sendLocalFont(e);
346
- } catch (s) {
347
- console.warn("Local fonts API:", s);
348
- }
349
- }
350
- _unbusy() {
351
- this._lastDemandTime ? this._demandRender(this._lastDemandTime) : this.busy = !1;
352
- }
353
- _handleRVFC(e, { mediaTime: t, width: s, height: a }) {
354
- if (this._destroyed)
355
- return null;
356
- this.busy ? this._lastDemandTime = { mediaTime: t, width: s, height: a } : (this.busy = !0, this._demandRender({ mediaTime: t, width: s, height: a })), this._video.requestVideoFrameCallback(this._handleRVFC.bind(this));
357
- }
358
- _demandRender({ mediaTime: e, width: t, height: s }) {
359
- this._lastDemandTime = null, (t !== this._videoWidth || s !== this._videoHeight) && (this._videoWidth = t, this._videoHeight = s, this.resize()), this.sendMessage("demand", { time: e + this.timeOffset });
360
- }
361
- _render({ images: e, async: t, times: s, width: a, height: n }) {
362
- this._unbusy();
363
- const r = Date.now();
364
- (this._canvasctrl.width !== a || this._canvasctrl.height !== n) && (this._canvasctrl.width = a, this._canvasctrl.height = n), this._ctx.clearRect(0, 0, this._canvasctrl.width, this._canvasctrl.height);
365
- for (const i of e)
366
- i.image && (t ? (this._ctx.drawImage(i.image, i.x, i.y), i.image.close()) : (this._bufferCanvas.width = i.w, this._bufferCanvas.height = i.h, this._bufferCtx.putImageData(new ImageData(this._fixAlpha(new Uint8ClampedArray(i.image)), i.w, i.h), 0, 0), this._ctx.drawImage(this._bufferCanvas, i.x, i.y)));
367
- if (this.debug) {
368
- s.drawTime = Date.now() - r;
369
- let i = 0;
370
- for (const o in s)
371
- i += s[o];
372
- console.log("Bitmaps: " + e.length + " Total: " + Math.round(i) + "ms", s);
373
- }
374
- }
375
- _fixAlpha(e) {
376
- if (h._hasAlphaBug)
377
- for (let t = 3; t < e.length; t += 4)
378
- e[t] = e[t] > 1 ? e[t] : 1;
379
- return e;
380
- }
381
- _ready() {
382
- this._init(), this.dispatchEvent(new CustomEvent("ready"));
383
- }
384
- /**
385
- * Send data and execute function in the worker.
386
- * @param {String} target Target function.
387
- * @param {Object} [data] Data for function.
388
- * @param {Transferable[]} [transferable] Array of transferables.
389
- */
390
- async sendMessage(e, t = {}, s) {
391
- await this._loaded, s ? this._worker.postMessage({
392
- target: e,
393
- transferable: s,
394
- ...t
395
- }, [...s]) : this._worker.postMessage({
396
- target: e,
397
- ...t
398
- });
399
- }
400
- _fetchFromWorker(e, t) {
401
- try {
402
- const s = e.target, a = setTimeout(() => {
403
- r(new Error("Error: Timeout while try to fetch " + s));
404
- }, 5e3), n = ({ data: i }) => {
405
- i.target === s && (t(null, i), this._worker.removeEventListener("message", n), this._worker.removeEventListener("error", r), clearTimeout(a));
406
- }, r = (i) => {
407
- t(i), this._worker.removeEventListener("message", n), this._worker.removeEventListener("error", r), clearTimeout(a);
408
- };
409
- this._worker.addEventListener("message", n), this._worker.addEventListener("error", r), this._worker.postMessage(e);
410
- } catch (s) {
411
- this._error(s);
412
- }
413
- }
414
- _console({ content: e, command: t }) {
415
- console[t].apply(console, JSON.parse(e));
416
- }
417
- _onmessage({ data: e }) {
418
- this["_" + e.target] && this["_" + e.target](e);
419
- }
420
- _error(e) {
421
- const t = e instanceof Error ? e : e instanceof ErrorEvent ? e.error : new Error(e), s = e instanceof Event ? new ErrorEvent(e.type, e) : new ErrorEvent("error", { error: t });
422
- this.dispatchEvent(s), console.error(t);
423
- }
424
- _removeListeners() {
425
- this._video && (this._ro && this._ro.unobserve(this._video), this._video.removeEventListener("timeupdate", this._boundTimeUpdate), this._video.removeEventListener("progress", this._boundTimeUpdate), this._video.removeEventListener("waiting", this._boundTimeUpdate), this._video.removeEventListener("seeking", this._boundTimeUpdate), this._video.removeEventListener("playing", this._boundTimeUpdate), this._video.removeEventListener("ratechange", this._boundSetRate), this._video.removeEventListener("resize", this._boundResize));
426
- }
427
- /**
428
- * Destroy the object, worker, listeners and all data.
429
- * @param {String} [err] Error to throw when destroying.
430
- */
431
- destroy(e) {
432
- e && this._error(e), this._video && this._canvasParent && this._video.parentNode.removeChild(this._canvasParent), this._destroyed = !0, this._removeListeners(), this.sendMessage("destroy"), this._worker.terminate();
433
- }
434
- };
435
- let _ = h;
436
- // test support for WASM, ImageData, alphaBug, but only once, on init so it doesn't run when first running the page
437
- v(_, "_supportsWebAssembly", null), v(_, "_hasAlphaBug", null);
438
- export {
439
- _ as default
440
- };
1
+ var _ = Object.defineProperty;
2
+ var u = (d, e, t) => e in d ? _(d, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : d[e] = t;
3
+ var f = (d, e, t) => (u(d, typeof e != "symbol" ? e + "" : e, t), t);
4
+ !("requestVideoFrameCallback" in HTMLVideoElement.prototype) && "getVideoPlaybackQuality" in HTMLVideoElement.prototype && (HTMLVideoElement.prototype._rvfcpolyfillmap = {}, HTMLVideoElement.prototype.requestVideoFrameCallback = function(d) {
5
+ const e = this.getVideoPlaybackQuality(), t = this.mozPresentedFrames || this.mozPaintedFrames || e.totalVideoFrames - e.droppedVideoFrames, s = (n, r) => {
6
+ const a = this.getVideoPlaybackQuality(), h = this.mozPresentedFrames || this.mozPaintedFrames || a.totalVideoFrames - a.droppedVideoFrames;
7
+ if (h > t) {
8
+ const c = this.mozFrameDelay || a.totalFrameDelay - e.totalFrameDelay || 0, m = r - n;
9
+ d(r, {
10
+ presentationTime: r + c * 1e3,
11
+ expectedDisplayTime: r + m,
12
+ width: this.videoWidth,
13
+ height: this.videoHeight,
14
+ mediaTime: Math.max(0, this.currentTime || 0) + m / 1e3,
15
+ presentedFrames: h,
16
+ processingDuration: c
17
+ }), delete this._rvfcpolyfillmap[i];
18
+ } else
19
+ this._rvfcpolyfillmap[i] = requestAnimationFrame((c) => s(r, c));
20
+ }, i = Date.now(), o = performance.now();
21
+ return this._rvfcpolyfillmap[i] = requestAnimationFrame((n) => s(o, n)), i;
22
+ }, HTMLVideoElement.prototype.cancelVideoFrameCallback = function(d) {
23
+ cancelAnimationFrame(this._rvfcpolyfillmap[d]), delete this._rvfcpolyfillmap[d];
24
+ });
25
+ const g = {
26
+ bt709: "BT.709",
27
+ // these might not be exactly correct? oops?
28
+ bt470bg: "BT.601",
29
+ // alias BT.601 PAL... whats the difference?
30
+ smpte170m: "BT.601"
31
+ // alias BT.601 NTSC... whats the difference?
32
+ }, p = {
33
+ "BT.601": {
34
+ "BT.709": `url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg'><filter id='f'><feColorMatrix type='matrix' values='1.0863 -0.0723 -0.014 0 0 0.0965 0.8451 0.0584 0 0 -0.0141 -0.0277 1.0418 0 0 0 0 0 1 0'/></filter></svg>#f")`
35
+ },
36
+ "BT.709": {
37
+ "BT.601": `url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg'><filter id='f'><feColorMatrix type='matrix' values='0.9137 0.0784 0.0079 0 0 -0.1049 1.1722 -0.0671 0 0 0.0096 0.0322 0.9582 0 0 0 0 0 1 0'/></filter></svg>#f")`
38
+ },
39
+ FCC: {
40
+ "BT.709": `url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg'><filter id='f'><feColorMatrix type='matrix' values='1.0873 -0.0736 -0.0137 0 0 0.0974 0.8494 0.0531 0 0 -0.0127 -0.0251
41
+ 1.0378 0 0 0 0 0 1 0'/></filter></svg>#f")`,
42
+ "BT.601": `url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg'><filter id='f'><feColorMatrix type='matrix' values='1.001 -0.0008 -0.0002 0 0 0.0009 1.005 -0.006 0 0 0.0013 0.0027 0.996 0 0 0 0 0 1 0'/></filter></svg>#f")`
43
+ },
44
+ SMPTE240M: {
45
+ "BT.709": `url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg'><filter id='f'><feColorMatrix type='matrix' values='0.9993 0.0006 0.0001 0 0 -0.0004 0.9812 0.0192 0 0 -0.0034 -0.0114 1.0148 0 0 0 0 0 1 0'/></filter></svg>#f")`,
46
+ "BT.601": `url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg'><filter id='f'><feColorMatrix type='matrix' values='0.913 0.0774 0.0096 0 0 -0.1051 1.1508 -0.0456 0 0 0.0063 0.0207 0.973 0 0 0 0 0 1 0'/></filter></svg>#f")`
47
+ }
48
+ }, l = class extends EventTarget {
49
+ /**
50
+ * @param {Object} options Settings object.
51
+ * @param {HTMLVideoElement} options.video Video to use as target for rendering and event listeners. Optional if canvas is specified instead.
52
+ * @param {HTMLCanvasElement} [options.canvas=HTMLCanvasElement] Canvas to use for manual handling. Not required if video is specified.
53
+ * @param {'js'|'wasm'} [options.blendMode='js'] Which image blending mode to use. WASM will perform better on lower end devices, JS will perform better if the device and browser supports hardware acceleration.
54
+ * @param {Boolean} [options.asyncRender=true] Whether or not to use async rendering, which offloads the CPU by creating image bitmaps on the GPU.
55
+ * @param {Boolean} [options.offscreenRender=true] Whether or not to render things fully on the worker, greatly reduces CPU usage.
56
+ * @param {Boolean} [options.onDemandRender=true] Whether or not to render subtitles as the video player decodes renders, rather than predicting which frame the player is on using events.
57
+ * @param {Number} [options.targetFps=24] Target FPS to render subtitles at. Ignored when onDemandRender is enabled.
58
+ * @param {Number} [options.timeOffset=0] Subtitle time offset in seconds.
59
+ * @param {Boolean} [options.debug=false] Whether or not to print debug information.
60
+ * @param {Number} [options.prescaleFactor=1.0] Scale down (< 1.0) the subtitles canvas to improve performance at the expense of quality, or scale it up (> 1.0).
61
+ * @param {Number} [options.prescaleHeightLimit=1080] The height in pixels beyond which the subtitles canvas won't be prescaled.
62
+ * @param {Number} [options.maxRenderHeight=0] The maximum rendering height in pixels of the subtitles canvas. Beyond this subtitles will be upscaled by the browser.
63
+ * @param {Boolean} [options.dropAllAnimations=false] Attempt to discard all animated tags. Enabling this may severly mangle complex subtitles and should only be considered as an last ditch effort of uncertain success for hardware otherwise incapable of displaing anything. Will not reliably work with manually edited or allocated events.
64
+ * @param {String} [options.workerUrl='jassub-worker.js'] The URL of the worker.
65
+ * @param {String} [options.legacyWorkerUrl='jassub-worker-legacy.js'] The URL of the legacy worker. Only loaded if the browser doesn't support WASM.
66
+ * @param {String} [options.subUrl=options.subContent] The URL of the subtitle file to play.
67
+ * @param {String} [options.subContent=options.subUrl] The content of the subtitle file to play.
68
+ * @param {String[]|Uint8Array[]} [options.fonts] An array of links or Uint8Arrays to the fonts used in the subtitle. If Uint8Array is used the array is copied, not referenced. This forces all the fonts in this array to be loaded by the renderer, regardless of if they are used.
69
+ * @param {Object} [options.availableFonts={'liberation sans': './default.woff2'}] Object with all available fonts - Key is font family in lower case, value is link or Uint8Array: { arial: '/font1.ttf' }. These fonts are selectively loaded if detected as used in the current subtitle track.
70
+ * @param {String} [options.fallbackFont='liberation sans'] The font family key of the fallback font in availableFonts to use if the other font for the style is missing special glyphs or unicode.
71
+ * @param {Boolean} [options.useLocalFonts=false] If the Local Font Access API is enabled [chrome://flags/#font-access], the library will query for permissions to use local fonts and use them if any are missing. The permission can be queried beforehand using navigator.permissions.request({ name: 'local-fonts' }).
72
+ * @param {Number} [options.libassMemoryLimit] libass bitmap cache memory limit in MiB (approximate).
73
+ * @param {Number} [options.libassGlyphLimit] libass glyph cache memory limit in MiB (approximate).
74
+ */
75
+ constructor(e = {}) {
76
+ super(), globalThis.Worker || this.destroy("Worker not supported"), l._test(), this._onDemandRender = "requestVideoFrameCallback" in HTMLVideoElement.prototype && (e.onDemandRender ?? !0), this.timeOffset = e.timeOffset || 0, this._video = e.video, this._videoHeight = 0, this._videoWidth = 0, this._videoColorSpace = null, this._canvas = e.canvas, this._video && !this._canvas ? (this._canvasParent = document.createElement("div"), this._canvasParent.className = "JASSUB", this._canvasParent.style.position = "relative", this._canvas = document.createElement("canvas"), this._canvas.style.display = "block", this._canvas.style.position = "absolute", this._canvas.style.pointerEvents = "none", this._canvasParent.appendChild(this._canvas), this._video.nextSibling ? this._video.parentNode.insertBefore(this._canvasParent, this._video.nextSibling) : this._video.parentNode.appendChild(this._canvasParent)) : this._canvas || this.destroy("Don't know where to render: you should give video or canvas in options."), this._bufferCanvas = document.createElement("canvas"), this._bufferCtx = this._bufferCanvas.getContext("2d"), this._canvasctrl = this._canvas, this._ctx = this._canvasctrl.getContext("2d"), this._lastRenderTime = 0, this.debug = !!e.debug, this.prescaleFactor = e.prescaleFactor || 1, this.prescaleHeightLimit = e.prescaleHeightLimit || 1080, this.maxRenderHeight = e.maxRenderHeight || 0, this._worker = new Worker(l._supportsWebAssembly ? e.workerUrl || "jassub-worker.js" : e.legacyWorkerUrl || "jassub-worker-legacy.js"), this._worker.onmessage = (t) => this._onmessage(t), this._worker.onerror = (t) => this._error(t), this._loaded = new Promise((t) => {
77
+ this._init = () => {
78
+ this._destroyed || (this._worker.postMessage({
79
+ target: "init",
80
+ asyncRender: typeof createImageBitmap < "u" && (e.asyncRender ?? !0),
81
+ onDemandRender: this._onDemandRender,
82
+ width: this._canvasctrl.width || 0,
83
+ height: this._canvasctrl.height || 0,
84
+ preMain: !0,
85
+ blendMode: e.blendMode || "js",
86
+ subUrl: e.subUrl,
87
+ subContent: e.subContent || null,
88
+ fonts: e.fonts || [],
89
+ availableFonts: e.availableFonts || { "liberation sans": "./default.woff2" },
90
+ fallbackFont: e.fallbackFont || "liberation sans",
91
+ debug: this.debug,
92
+ targetFps: e.targetFps || 24,
93
+ dropAllAnimations: e.dropAllAnimations,
94
+ libassMemoryLimit: e.libassMemoryLimit || 0,
95
+ libassGlyphLimit: e.libassGlyphLimit || 0,
96
+ hasAlphaBug: l._hasAlphaBug,
97
+ offscreenRender: typeof OffscreenCanvas < "u" && (e.offscreenRender ?? !0),
98
+ useLocalFonts: "queryLocalFonts" in self && (e.useLocalFonts ?? !0)
99
+ }), this._boundResize = this.resize.bind(this), this._boundTimeUpdate = this._timeupdate.bind(this), this._boundSetRate = this.setRate.bind(this), this._boundUpdateColorSpace = this._updateColorSpace.bind(this), this._video && this.setVideo(e.video), this._onDemandRender && (this.busy = !1, this._lastDemandTime = null), t());
100
+ };
101
+ });
102
+ }
103
+ static _test() {
104
+ if (l._supportsWebAssembly !== null)
105
+ return null;
106
+ const e = document.createElement("canvas"), t = e.getContext("2d", { willReadFrequently: !0 });
107
+ if (typeof ImageData.prototype.constructor == "function")
108
+ try {
109
+ new ImageData(new Uint8ClampedArray([0, 0, 0, 0]), 1, 1);
110
+ } catch {
111
+ console.log("Detected that ImageData is not constructable despite browser saying so"), self.ImageData = function(a, h, c) {
112
+ const m = t.createImageData(h, c);
113
+ return a && m.data.set(a), m;
114
+ };
115
+ }
116
+ try {
117
+ if (typeof WebAssembly == "object" && typeof WebAssembly.instantiate == "function") {
118
+ const r = new WebAssembly.Module(Uint8Array.of(0, 97, 115, 109, 1, 0, 0, 0));
119
+ r instanceof WebAssembly.Module && (l._supportsWebAssembly = new WebAssembly.Instance(r) instanceof WebAssembly.Instance);
120
+ }
121
+ } catch {
122
+ l._supportsWebAssembly = !1;
123
+ }
124
+ const s = document.createElement("canvas"), i = s.getContext("2d", { willReadFrequently: !0 });
125
+ e.width = s.width = 1, e.height = s.height = 1, t.clearRect(0, 0, 1, 1), i.clearRect(0, 0, 1, 1);
126
+ const o = i.getImageData(0, 0, 1, 1).data;
127
+ t.putImageData(new ImageData(new Uint8ClampedArray([0, 255, 0, 0]), 1, 1), 0, 0), i.drawImage(e, 0, 0);
128
+ const n = i.getImageData(0, 0, 1, 1).data;
129
+ l._hasAlphaBug = o[1] !== n[1], l._hasAlphaBug && console.log("Detected a browser having issue with transparent pixels, applying workaround"), e.remove(), s.remove();
130
+ }
131
+ /**
132
+ * Resize the canvas to given parameters. Auto-generated if values are ommited.
133
+ * @param {Number} [width=0]
134
+ * @param {Number} [height=0]
135
+ * @param {Number} [top=0]
136
+ * @param {Number} [left=0]
137
+ * @param {Boolean} [force=false]
138
+ */
139
+ resize(e = 0, t = 0, s = 0, i = 0, o = ((n) => (n = this._video) == null ? void 0 : n.paused)()) {
140
+ if ((!e || !t) && this._video) {
141
+ const r = this._getVideoPosition();
142
+ let a = null;
143
+ if (this._videoWidth) {
144
+ const h = this._video.videoWidth / this._videoWidth, c = this._video.videoHeight / this._videoHeight;
145
+ a = this._computeCanvasSize((r.width || 0) / h, (r.height || 0) / c);
146
+ } else
147
+ a = this._computeCanvasSize(r.width || 0, r.height || 0);
148
+ e = a.width, t = a.height, this._canvasParent && (s = r.y - (this._canvasParent.getBoundingClientRect().top - this._video.getBoundingClientRect().top), i = r.x), this._canvas.style.width = r.width + "px", this._canvas.style.height = r.height + "px";
149
+ }
150
+ this._canvas.style.top = s + "px", this._canvas.style.left = i + "px", this.sendMessage("canvas", { width: e, height: t, force: o && this.busy === !1 });
151
+ }
152
+ _getVideoPosition(e = this._video.videoWidth, t = this._video.videoHeight) {
153
+ const s = e / t, { offsetWidth: i, offsetHeight: o } = this._video, n = i / o;
154
+ e = i, t = o, n > s ? e = Math.floor(o * s) : t = Math.floor(i / s);
155
+ const r = (i - e) / 2, a = (o - t) / 2;
156
+ return { width: e, height: t, x: r, y: a };
157
+ }
158
+ _computeCanvasSize(e = 0, t = 0) {
159
+ const s = this.prescaleFactor <= 0 ? 1 : this.prescaleFactor, i = self.devicePixelRatio || 1;
160
+ if (e = e * i, t = t * i, t <= 0 || e <= 0)
161
+ e = 0, t = 0;
162
+ else {
163
+ const o = s < 1 ? -1 : 1;
164
+ let n = t * i;
165
+ o * n * s <= o * this.prescaleHeightLimit ? n *= s : o * n < o * this.prescaleHeightLimit && (n = this.prescaleHeightLimit), this.maxRenderHeight > 0 && n > this.maxRenderHeight && (n = this.maxRenderHeight), e *= n / t, t = n;
166
+ }
167
+ return { width: e, height: t };
168
+ }
169
+ _timeupdate({ type: e }) {
170
+ const s = {
171
+ seeking: !0,
172
+ waiting: !0,
173
+ playing: !1
174
+ }[e];
175
+ s != null && (this._playstate = s), this.setCurrentTime(this._video.paused || this._playstate, this._video.currentTime + this.timeOffset);
176
+ }
177
+ _updateColorSpace() {
178
+ this._video.requestVideoFrameCallback(() => {
179
+ const e = new VideoFrame(this._video);
180
+ this._videoColorSpace = g[e.colorSpace.matrix], e.close();
181
+ });
182
+ }
183
+ /**
184
+ * Change the video to use as target for event listeners.
185
+ * @param {HTMLVideoElement} video
186
+ */
187
+ setVideo(e) {
188
+ e instanceof HTMLVideoElement ? (this._removeListeners(), this._video = e, this._onDemandRender ? this._video.requestVideoFrameCallback(this._handleRVFC.bind(this)) : (this._playstate = e.paused, e.addEventListener("timeupdate", this._boundTimeUpdate, !1), e.addEventListener("progress", this._boundTimeUpdate, !1), e.addEventListener("waiting", this._boundTimeUpdate, !1), e.addEventListener("seeking", this._boundTimeUpdate, !1), e.addEventListener("playing", this._boundTimeUpdate, !1), e.addEventListener("ratechange", this._boundSetRate, !1), e.addEventListener("resize", this._boundResize, !1)), "VideoFrame" in window && (e.addEventListener("loadedmetadata", this._boundUpdateColorSpace, !1), e.readyState > 2 && this._updateColorSpace()), e.videoWidth > 0 && this.resize(), typeof ResizeObserver < "u" && (this._ro || (this._ro = new ResizeObserver(() => this.resize())), this._ro.observe(e))) : this._error("Video element invalid!");
189
+ }
190
+ runBenchmark() {
191
+ this.sendMessage("runBenchmark");
192
+ }
193
+ /**
194
+ * Overwrites the current subtitle content.
195
+ * @param {String} url URL to load subtitles from.
196
+ */
197
+ setTrackByUrl(e) {
198
+ this.sendMessage("setTrackByUrl", { url: e });
199
+ }
200
+ /**
201
+ * Overwrites the current subtitle content.
202
+ * @param {String} content Content of the ASS file.
203
+ */
204
+ setTrack(e) {
205
+ this.sendMessage("setTrack", { content: e });
206
+ }
207
+ /**
208
+ * Free currently used subtitle track.
209
+ */
210
+ freeTrack() {
211
+ this.sendMessage("freeTrack");
212
+ }
213
+ /**
214
+ * Sets the playback state of the media.
215
+ * @param {Boolean} isPaused Pause/Play subtitle playback.
216
+ */
217
+ setIsPaused(e) {
218
+ this.sendMessage("video", { isPaused: e });
219
+ }
220
+ /**
221
+ * Sets the playback rate of the media [speed multiplier].
222
+ * @param {Number} rate Playback rate.
223
+ */
224
+ setRate(e) {
225
+ this.sendMessage("video", { rate: e });
226
+ }
227
+ /**
228
+ * Sets the current time, playback state and rate of the subtitles.
229
+ * @param {Boolean} [isPaused] Pause/Play subtitle playback.
230
+ * @param {Number} [currentTime] Time in seconds.
231
+ * @param {Number} [rate] Playback rate.
232
+ */
233
+ setCurrentTime(e, t, s) {
234
+ this.sendMessage("video", { isPaused: e, currentTime: t, rate: s });
235
+ }
236
+ /**
237
+ * @typedef {Object} ASS_Event
238
+ * @property {Number} Start Start Time of the Event, in 0:00:00:00 format ie. Hrs:Mins:Secs:hundredths. This is the time elapsed during script playback at which the text will appear onscreen. Note that there is a single digit for the hours!
239
+ * @property {Number} Duration End Time of the Event, in 0:00:00:00 format ie. Hrs:Mins:Secs:hundredths. This is the time elapsed during script playback at which the text will disappear offscreen. Note that there is a single digit for the hours!
240
+ * @property {String} Style Style name. If it is "Default", then your own *Default style will be subtituted.
241
+ * @property {String} Name Character name. This is the name of the character who speaks the dialogue. It is for information only, to make the script is easier to follow when editing/timing.
242
+ * @property {Number} MarginL 4-figure Left Margin override. The values are in pixels. All zeroes means the default margins defined by the style are used.
243
+ * @property {Number} MarginR 4-figure Right Margin override. The values are in pixels. All zeroes means the default margins defined by the style are used.
244
+ * @property {Number} MarginV 4-figure Bottom Margin override. The values are in pixels. All zeroes means the default margins defined by the style are used.
245
+ * @property {String} Effect Transition Effect. This is either empty, or contains information for one of the three transition effects implemented in SSA v4.x
246
+ * @property {String} Text Subtitle Text. This is the actual text which will be displayed as a subtitle onscreen. Everything after the 9th comma is treated as the subtitle text, so it can include commas.
247
+ * @property {Number} ReadOrder Number in order of which to read this event.
248
+ * @property {Number} Layer Z-index overlap in which to render this event.
249
+ * @property {Number} _index (Internal) index of the event.
250
+ */
251
+ /**
252
+ * Create a new ASS event directly.
253
+ * @param {ASS_Event} event
254
+ */
255
+ createEvent(e) {
256
+ this.sendMessage("createEvent", { event: e });
257
+ }
258
+ /**
259
+ * Overwrite the data of the event with the specified index.
260
+ * @param {ASS_Event} event
261
+ * @param {Number} index
262
+ */
263
+ setEvent(e, t) {
264
+ this.sendMessage("setEvent", { event: e, index: t });
265
+ }
266
+ /**
267
+ * Remove the event with the specified index.
268
+ * @param {Number} index
269
+ */
270
+ removeEvent(e) {
271
+ this.sendMessage("removeEvent", { index: e });
272
+ }
273
+ /**
274
+ * Get all ASS events.
275
+ * @param {function(Error|null, ASS_Event)} callback Function to callback when worker returns the events.
276
+ */
277
+ getEvents(e) {
278
+ this._fetchFromWorker({
279
+ target: "getEvents"
280
+ }, (t, { events: s }) => {
281
+ e(t, s);
282
+ });
283
+ }
284
+ /**
285
+ * @typedef {Object} ASS_Style
286
+ * @property {String} Name The name of the Style. Case sensitive. Cannot include commas.
287
+ * @property {String} FontName The fontname as used by Windows. Case-sensitive.
288
+ * @property {Number} FontSize Font size.
289
+ * @property {Number} PrimaryColour A long integer BGR (blue-green-red) value. ie. the byte order in the hexadecimal equivelent of this number is BBGGRR
290
+ * @property {Number} SecondaryColour A long integer BGR (blue-green-red) value. ie. the byte order in the hexadecimal equivelent of this number is BBGGRR
291
+ * @property {Number} OutlineColour A long integer BGR (blue-green-red) value. ie. the byte order in the hexadecimal equivelent of this number is BBGGRR
292
+ * @property {Number} BackColour This is the colour of the subtitle outline or shadow, if these are used. A long integer BGR (blue-green-red) value. ie. the byte order in the hexadecimal equivelent of this number is BBGGRR.
293
+ * @property {Number} Bold This defines whether text is bold (true) or not (false). -1 is True, 0 is False. This is independant of the Italic attribute - you can have have text which is both bold and italic.
294
+ * @property {Number} Italic Italic. This defines whether text is italic (true) or not (false). -1 is True, 0 is False. This is independant of the bold attribute - you can have have text which is both bold and italic.
295
+ * @property {Number} Underline -1 or 0
296
+ * @property {Number} StrikeOut -1 or 0
297
+ * @property {Number} ScaleX Modifies the width of the font. [percent]
298
+ * @property {Number} ScaleY Modifies the height of the font. [percent]
299
+ * @property {Number} Spacing Extra space between characters. [pixels]
300
+ * @property {Number} Angle The origin of the rotation is defined by the alignment. Can be a floating point number. [degrees]
301
+ * @property {Number} BorderStyle 1=Outline + drop shadow, 3=Opaque box
302
+ * @property {Number} Outline If BorderStyle is 1, then this specifies the width of the outline around the text, in pixels. Values may be 0, 1, 2, 3 or 4.
303
+ * @property {Number} Shadow If BorderStyle is 1, then this specifies the depth of the drop shadow behind the text, in pixels. Values may be 0, 1, 2, 3 or 4. Drop shadow is always used in addition to an outline - SSA will force an outline of 1 pixel if no outline width is given.
304
+ * @property {Number} Alignment This sets how text is "justified" within the Left/Right onscreen margins, and also the vertical placing. Values may be 1=Left, 2=Centered, 3=Right. Add 4 to the value for a "Toptitle". Add 8 to the value for a "Midtitle". eg. 5 = left-justified toptitle
305
+ * @property {Number} MarginL This defines the Left Margin in pixels. It is the distance from the left-hand edge of the screen.The three onscreen margins (MarginL, MarginR, MarginV) define areas in which the subtitle text will be displayed.
306
+ * @property {Number} MarginR This defines the Right Margin in pixels. It is the distance from the right-hand edge of the screen. The three onscreen margins (MarginL, MarginR, MarginV) define areas in which the subtitle text will be displayed.
307
+ * @property {Number} MarginV This defines the vertical Left Margin in pixels. For a subtitle, it is the distance from the bottom of the screen. For a toptitle, it is the distance from the top of the screen. For a midtitle, the value is ignored - the text will be vertically centred.
308
+ * @property {Number} Encoding This specifies the font character set or encoding and on multi-lingual Windows installations it provides access to characters used in multiple than one languages. It is usually 0 (zero) for English (Western, ANSI) Windows.
309
+ * @property {Number} treat_fontname_as_pattern
310
+ * @property {Number} Blur
311
+ * @property {Number} Justify
312
+ */
313
+ /**
314
+ * Create a new ASS style directly.
315
+ * @param {ASS_Style} event
316
+ */
317
+ createStyle(e) {
318
+ this.sendMessage("createStyle", { style: e });
319
+ }
320
+ /**
321
+ * Overwrite the data of the style with the specified index.
322
+ * @param {ASS_Style} event
323
+ * @param {Number} index
324
+ */
325
+ setStyle(e, t) {
326
+ this.sendMessage("setStyle", { event: e, index: t });
327
+ }
328
+ /**
329
+ * Remove the style with the specified index.
330
+ * @param {Number} index
331
+ */
332
+ removeStyle(e) {
333
+ this.sendMessage("removeStyle", { index: e });
334
+ }
335
+ /**
336
+ * Get all ASS styles.
337
+ * @param {function(Error|null, ASS_Style)} callback Function to callback when worker returns the styles.
338
+ */
339
+ getStyles(e) {
340
+ this._fetchFromWorker({
341
+ target: "getStyles"
342
+ }, (t, { styles: s }) => {
343
+ e(t, s);
344
+ });
345
+ }
346
+ /**
347
+ * Adds a font to the renderer.
348
+ * @param {String|Uint8Array} font Font to add.
349
+ */
350
+ addFont(e) {
351
+ this.sendMessage("addFont", { font: e });
352
+ }
353
+ _sendLocalFont(e) {
354
+ try {
355
+ queryLocalFonts().then((t) => {
356
+ const s = t == null ? void 0 : t.find((i) => i.fullName.toLowerCase() === e);
357
+ s && s.blob().then((i) => {
358
+ i.arrayBuffer().then((o) => {
359
+ this.addFont(new Uint8Array(o));
360
+ });
361
+ });
362
+ });
363
+ } catch (t) {
364
+ console.warn("Local fonts API:", t);
365
+ }
366
+ }
367
+ _getLocalFont({ font: e }) {
368
+ var t;
369
+ try {
370
+ (t = navigator == null ? void 0 : navigator.permissions) != null && t.query ? navigator.permissions.query({ name: "local-fonts" }).then((s) => {
371
+ s.state === "granted" && this._sendLocalFont(e);
372
+ }) : this._sendLocalFont(e);
373
+ } catch (s) {
374
+ console.warn("Local fonts API:", s);
375
+ }
376
+ }
377
+ _unbusy() {
378
+ this._lastDemandTime ? this._demandRender(this._lastDemandTime) : this.busy = !1;
379
+ }
380
+ _handleRVFC(e, { mediaTime: t, width: s, height: i }) {
381
+ if (this._destroyed)
382
+ return null;
383
+ this.busy ? this._lastDemandTime = { mediaTime: t, width: s, height: i } : (this.busy = !0, this._demandRender({ mediaTime: t, width: s, height: i })), this._video.requestVideoFrameCallback(this._handleRVFC.bind(this));
384
+ }
385
+ _demandRender({ mediaTime: e, width: t, height: s }) {
386
+ this._lastDemandTime = null, (t !== this._videoWidth || s !== this._videoHeight) && (this._videoWidth = t, this._videoHeight = s, this.resize()), this.sendMessage("demand", { time: e + this.timeOffset });
387
+ }
388
+ /**
389
+ * Veryify the color spaces for subtitles and videos, then apply filters to correct the color of subtitles.
390
+ * @param {String} subtitleColorSpace Subtitle color space. One of: BT.601 BT.709 SMPTE240M FCC
391
+ * @param {String} videoColorSpace Video color space. One of: BT.601 BT.709
392
+ */
393
+ verifyColorSpace(e, t = this._videoColorSpace) {
394
+ !e || !t || e !== t && (this._ctx.filter = p[e][t]);
395
+ }
396
+ _render({ images: e, async: t, times: s, width: i, height: o, colorSpace: n }) {
397
+ this._unbusy();
398
+ const r = Date.now();
399
+ (this._canvasctrl.width !== i || this._canvasctrl.height !== o) && (this._canvasctrl.width = i, this._canvasctrl.height = o, this.verifyColorSpace(n)), this._ctx.clearRect(0, 0, this._canvasctrl.width, this._canvasctrl.height);
400
+ for (const a of e)
401
+ a.image && (t ? (this._ctx.drawImage(a.image, a.x, a.y), a.image.close()) : (this._bufferCanvas.width = a.w, this._bufferCanvas.height = a.h, this._bufferCtx.putImageData(new ImageData(this._fixAlpha(new Uint8ClampedArray(a.image)), a.w, a.h), 0, 0), this._ctx.drawImage(this._bufferCanvas, a.x, a.y)));
402
+ if (this.debug) {
403
+ s.drawTime = Date.now() - r;
404
+ let a = 0;
405
+ for (const h in s)
406
+ a += s[h];
407
+ console.log("Bitmaps: " + e.length + " Total: " + Math.round(a) + "ms", s);
408
+ }
409
+ }
410
+ _fixAlpha(e) {
411
+ if (l._hasAlphaBug)
412
+ for (let t = 3; t < e.length; t += 4)
413
+ e[t] = e[t] > 1 ? e[t] : 1;
414
+ return e;
415
+ }
416
+ _ready() {
417
+ this._init(), this.dispatchEvent(new CustomEvent("ready"));
418
+ }
419
+ /**
420
+ * Send data and execute function in the worker.
421
+ * @param {String} target Target function.
422
+ * @param {Object} [data] Data for function.
423
+ * @param {Transferable[]} [transferable] Array of transferables.
424
+ */
425
+ async sendMessage(e, t = {}, s) {
426
+ await this._loaded, s ? this._worker.postMessage({
427
+ target: e,
428
+ transferable: s,
429
+ ...t
430
+ }, [...s]) : this._worker.postMessage({
431
+ target: e,
432
+ ...t
433
+ });
434
+ }
435
+ _fetchFromWorker(e, t) {
436
+ try {
437
+ const s = e.target, i = setTimeout(() => {
438
+ n(new Error("Error: Timeout while try to fetch " + s));
439
+ }, 5e3), o = ({ data: r }) => {
440
+ r.target === s && (t(null, r), this._worker.removeEventListener("message", o), this._worker.removeEventListener("error", n), clearTimeout(i));
441
+ }, n = (r) => {
442
+ t(r), this._worker.removeEventListener("message", o), this._worker.removeEventListener("error", n), clearTimeout(i);
443
+ };
444
+ this._worker.addEventListener("message", o), this._worker.addEventListener("error", n), this._worker.postMessage(e);
445
+ } catch (s) {
446
+ this._error(s);
447
+ }
448
+ }
449
+ _console({ content: e, command: t }) {
450
+ console[t].apply(console, JSON.parse(e));
451
+ }
452
+ _onmessage({ data: e }) {
453
+ this["_" + e.target] && this["_" + e.target](e);
454
+ }
455
+ _error(e) {
456
+ const t = e instanceof Error ? e : e instanceof ErrorEvent ? e.error : new Error(e), s = e instanceof Event ? new ErrorEvent(e.type, e) : new ErrorEvent("error", { error: t });
457
+ this.dispatchEvent(s), console.error(t);
458
+ }
459
+ _removeListeners() {
460
+ this._video && (this._ro && this._ro.unobserve(this._video), this._ctx.filter = "none", this._video.removeEventListener("timeupdate", this._boundTimeUpdate), this._video.removeEventListener("progress", this._boundTimeUpdate), this._video.removeEventListener("waiting", this._boundTimeUpdate), this._video.removeEventListener("seeking", this._boundTimeUpdate), this._video.removeEventListener("playing", this._boundTimeUpdate), this._video.removeEventListener("ratechange", this._boundSetRate), this._video.removeEventListener("resize", this._boundResize), this._video.removeEventListener("loadedmetadata", this._boundUpdateColorSpace));
461
+ }
462
+ /**
463
+ * Destroy the object, worker, listeners and all data.
464
+ * @param {String} [err] Error to throw when destroying.
465
+ */
466
+ destroy(e) {
467
+ e && this._error(e), this._video && this._canvasParent && this._video.parentNode.removeChild(this._canvasParent), this._destroyed = !0, this._removeListeners(), this.sendMessage("destroy"), this._worker.terminate();
468
+ }
469
+ };
470
+ let v = l;
471
+ // test support for WASM, ImageData, alphaBug, but only once, on init so it doesn't run when first running the page
472
+ f(v, "_supportsWebAssembly", null), f(v, "_hasAlphaBug", null);
473
+ export {
474
+ v as default
475
+ };