libbitsub 1.5.0 → 1.6.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.
package/LICENSE CHANGED
@@ -1,9 +1,3 @@
1
- Libbitsub is dual-licensed under The MIT License [1] and
2
- Apache 2.0 License [2]. Copyright (c) 2026, Jaruwit Jaiya.
3
-
4
- [1]: <http://opensource.org/licenses/MIT>, which is reproduced below:
5
-
6
- ~~~~
7
1
  MIT License
8
2
 
9
3
  Copyright (c) 2026 Jaruwit Jaiya
@@ -25,211 +19,3 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
19
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26
20
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27
21
  SOFTWARE.
28
- ~~~~
29
-
30
-
31
- [2]: <http://www.apache.org/licenses/LICENSE-2.0>, which is reproduced below:
32
-
33
- ~~~~
34
- Apache License
35
- Version 2.0, January 2004
36
- http://www.apache.org/licenses/
37
-
38
- TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
39
-
40
- 1. Definitions.
41
-
42
- "License" shall mean the terms and conditions for use, reproduction,
43
- and distribution as defined by Sections 1 through 9 of this document.
44
-
45
- "Licensor" shall mean the copyright owner or entity authorized by
46
- the copyright owner that is granting the License.
47
-
48
- "Legal Entity" shall mean the union of the acting entity and all
49
- other entities that control, are controlled by, or are under common
50
- control with that entity. For the purposes of this definition,
51
- "control" means (i) the power, direct or indirect, to cause the
52
- direction or management of such entity, whether by contract or
53
- otherwise, or (ii) ownership of fifty percent (50%) or more of the
54
- outstanding shares, or (iii) beneficial ownership of such entity.
55
-
56
- "You" (or "Your") shall mean an individual or Legal Entity
57
- exercising permissions granted by this License.
58
-
59
- "Source" form shall mean the preferred form for making modifications,
60
- including but not limited to software source code, documentation
61
- source, and configuration files.
62
-
63
- "Object" form shall mean any form resulting from mechanical
64
- transformation or translation of a Source form, including but
65
- not limited to compiled object code, generated documentation,
66
- and conversions to other media types.
67
-
68
- "Work" shall mean the work of authorship, whether in Source or
69
- Object form, made available under the License, as indicated by a
70
- copyright notice that is included in or attached to the work
71
- (an example is provided in the Appendix below).
72
-
73
- "Derivative Works" shall mean any work, whether in Source or Object
74
- form, that is based on (or derived from) the Work and for which the
75
- editorial revisions, annotations, elaborations, or other modifications
76
- represent, as a whole, an original work of authorship. For the purposes
77
- of this License, Derivative Works shall not include works that remain
78
- separable from, or merely link (or bind by name) to the interfaces of,
79
- the Work and Derivative Works thereof.
80
-
81
- "Contribution" shall mean any work of authorship, including
82
- the original version of the Work and any modifications or additions
83
- to that Work or Derivative Works thereof, that is intentionally
84
- submitted to Licensor for inclusion in the Work by the copyright owner
85
- or by an individual or Legal Entity authorized to submit on behalf of
86
- the copyright owner. For the purposes of this definition, "submitted"
87
- means any form of electronic, verbal, or written communication sent
88
- to the Licensor or its representatives, including but not limited to
89
- communication on electronic mailing lists, source code control systems,
90
- and issue tracking systems that are managed by, or on behalf of, the
91
- Licensor for the purpose of discussing and improving the Work, but
92
- excluding communication that is conspicuously marked or otherwise
93
- designated in writing by the copyright owner as "Not a Contribution."
94
-
95
- "Contributor" shall mean Licensor and any individual or Legal Entity
96
- on behalf of whom a Contribution has been received by Licensor and
97
- subsequently incorporated within the Work.
98
-
99
- 2. Grant of Copyright License. Subject to the terms and conditions of
100
- this License, each Contributor hereby grants to You a perpetual,
101
- worldwide, non-exclusive, no-charge, royalty-free, irrevocable
102
- copyright license to reproduce, prepare Derivative Works of,
103
- publicly display, publicly perform, sublicense, and distribute the
104
- Work and such Derivative Works in Source or Object form.
105
-
106
- 3. Grant of Patent License. Subject to the terms and conditions of
107
- this License, each Contributor hereby grants to You a perpetual,
108
- worldwide, non-exclusive, no-charge, royalty-free, irrevocable
109
- (except as stated in this section) patent license to make, have made,
110
- use, offer to sell, sell, import, and otherwise transfer the Work,
111
- where such license applies only to those patent claims licensable
112
- by such Contributor that are necessarily infringed by their
113
- Contribution(s) alone or by combination of their Contribution(s)
114
- with the Work to which such Contribution(s) was submitted. If You
115
- institute patent litigation against any entity (including a
116
- cross-claim or counterclaim in a lawsuit) alleging that the Work
117
- or a Contribution incorporated within the Work constitutes direct
118
- or contributory patent infringement, then any patent licenses
119
- granted to You under this License for that Work shall terminate
120
- as of the date such litigation is filed.
121
-
122
- 4. Redistribution. You may reproduce and distribute copies of the
123
- Work or Derivative Works thereof in any medium, with or without
124
- modifications, and in Source or Object form, provided that You
125
- meet the following conditions:
126
-
127
- (a) You must give any other recipients of the Work or
128
- Derivative Works a copy of this License; and
129
-
130
- (b) You must cause any modified files to carry prominent notices
131
- stating that You changed the files; and
132
-
133
- (c) You must retain, in the Source form of any Derivative Works
134
- that You distribute, all copyright, patent, trademark, and
135
- attribution notices from the Source form of the Work,
136
- excluding those notices that do not pertain to any part of
137
- the Derivative Works; and
138
-
139
- (d) If the Work includes a "NOTICE" text file as part of its
140
- distribution, then any Derivative Works that You distribute must
141
- include a readable copy of the attribution notices contained
142
- within such NOTICE file, excluding those notices that do not
143
- pertain to any part of the Derivative Works, in at least one
144
- of the following places: within a NOTICE text file distributed
145
- as part of the Derivative Works; within the Source form or
146
- documentation, if provided along with the Derivative Works; or,
147
- within a display generated by the Derivative Works, if and
148
- wherever such third-party notices normally appear. The contents
149
- of the NOTICE file are for informational purposes only and
150
- do not modify the License. You may add Your own attribution
151
- notices within Derivative Works that You distribute, alongside
152
- or as an addendum to the NOTICE text from the Work, provided
153
- that such additional attribution notices cannot be construed
154
- as modifying the License.
155
-
156
- You may add Your own copyright statement to Your modifications and
157
- may provide additional or different license terms and conditions
158
- for use, reproduction, or distribution of Your modifications, or
159
- for any such Derivative Works as a whole, provided Your use,
160
- reproduction, and distribution of the Work otherwise complies with
161
- the conditions stated in this License.
162
-
163
- 5. Submission of Contributions. Unless You explicitly state otherwise,
164
- any Contribution intentionally submitted for inclusion in the Work
165
- by You to the Licensor shall be under the terms and conditions of
166
- this License, without any additional terms or conditions.
167
- Notwithstanding the above, nothing herein shall supersede or modify
168
- the terms of any separate license agreement you may have executed
169
- with Licensor regarding such Contributions.
170
-
171
- 6. Trademarks. This License does not grant permission to use the trade
172
- names, trademarks, service marks, or product names of the Licensor,
173
- except as required for reasonable and customary use in describing the
174
- origin of the Work and reproducing the content of the NOTICE file.
175
-
176
- 7. Disclaimer of Warranty. Unless required by applicable law or
177
- agreed to in writing, Licensor provides the Work (and each
178
- Contributor provides its Contributions) on an "AS IS" BASIS,
179
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
180
- implied, including, without limitation, any warranties or conditions
181
- of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
182
- PARTICULAR PURPOSE. You are solely responsible for determining the
183
- appropriateness of using or redistributing the Work and assume any
184
- risks associated with Your exercise of permissions under this License.
185
-
186
- 8. Limitation of Liability. In no event and under no legal theory,
187
- whether in tort (including negligence), contract, or otherwise,
188
- unless required by applicable law (such as deliberate and grossly
189
- negligent acts) or agreed to in writing, shall any Contributor be
190
- liable to You for damages, including any direct, indirect, special,
191
- incidental, or consequential damages of any character arising as a
192
- result of this License or out of the use or inability to use the
193
- Work (including but not limited to damages for loss of goodwill,
194
- work stoppage, computer failure or malfunction, or any and all
195
- other commercial damages or losses), even if such Contributor
196
- has been advised of the possibility of such damages.
197
-
198
- 9. Accepting Warranty or Additional Liability. While redistributing
199
- the Work or Derivative Works thereof, You may choose to offer,
200
- and charge a fee for, acceptance of support, warranty, indemnity,
201
- or other liability obligations and/or rights consistent with this
202
- License. However, in accepting such obligations, You may act only
203
- on Your own behalf and on Your sole responsibility, not on behalf
204
- of any other Contributor, and only if You agree to indemnify,
205
- defend, and hold each Contributor harmless for any liability
206
- incurred by, or claims asserted against, such Contributor by reason
207
- of your accepting any such warranty or additional liability.
208
-
209
- END OF TERMS AND CONDITIONS
210
-
211
- APPENDIX: How to apply the Apache License to your work.
212
-
213
- To apply the Apache License to your work, attach the following
214
- boilerplate notice, with the fields enclosed by brackets "[]"
215
- replaced with your own identifying information. (Don't include
216
- the brackets!) The text should be enclosed in the appropriate
217
- comment syntax for the file format. We also recommend that a
218
- file or class name and description of purpose be included on the
219
- same "printed page" as the copyright notice for easier
220
- identification within third-party archives.
221
-
222
- Copyright [yyyy] [name of copyright owner]
223
-
224
- Licensed under the Apache License, Version 2.0 (the "License");
225
- you may not use this file except in compliance with the License.
226
- You may obtain a copy of the License at
227
-
228
- http://www.apache.org/licenses/LICENSE-2.0
229
-
230
- Unless required by applicable law or agreed to in writing, software
231
- distributed under the License is distributed on an "AS IS" BASIS,
232
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
233
- See the License for the specific language governing permissions and
234
- limitations under the License.
235
- ~~~~
package/README.md CHANGED
@@ -8,7 +8,7 @@ Started as a fork of Arcus92's [libpgs-js](https://github.com/Arcus92/libpgs-js)
8
8
 
9
9
  - **PGS (Blu-ray)** subtitle parsing and rendering
10
10
  - **VobSub (DVD)** subtitle parsing and rendering
11
- - **WebGPU rendering** GPU-accelerated rendering with automatic Canvas2D fallback
11
+ - **WebGPU / WebGL2 rendering** GPU-accelerated rendering with automatic fallback: WebGPU → WebGL2 → Canvas2D
12
12
  - **High-performance** Rust-based rendering engine compiled to WebAssembly
13
13
  - **Zero-copy** data transfer between JS and WASM where possible
14
14
  - **Caching** for decoded bitmaps to optimize repeated rendering
@@ -319,31 +319,44 @@ setInterval(() => {
319
319
  | `totalEntries` | number | Total subtitle entries/display sets in the loaded file |
320
320
  | `currentIndex` | number | Index of the currently displayed subtitle |
321
321
 
322
- ### WebGPU Rendering
322
+ ### GPU-Accelerated Rendering
323
323
 
324
- libbitsub automatically uses WebGPU for GPU-accelerated rendering when available, with automatic fallback to Canvas2D:
324
+ libbitsub automatically selects the best available GPU renderer at startup, following this fallback chain:
325
+
326
+ **WebGPU → WebGL2 → Canvas2D**
325
327
 
326
328
  ```typescript
327
- import { PgsRenderer, isWebGPUSupported } from 'libbitsub'
329
+ import { PgsRenderer, isWebGPUSupported, isWebGL2Supported } from 'libbitsub'
328
330
 
329
- // Check WebGPU support
331
+ // Check renderer support
330
332
  if (isWebGPUSupported()) {
331
- console.log('WebGPU available - GPU-accelerated rendering enabled')
333
+ console.log('WebGPU available')
334
+ } else if (isWebGL2Supported()) {
335
+ console.log('WebGL2 available')
336
+ } else {
337
+ console.log('Falling back to Canvas2D')
332
338
  }
333
339
 
334
- // Configure WebGPU preference
335
340
  const renderer = new PgsRenderer({
336
341
  video: videoElement,
337
342
  subUrl: '/subtitles/movie.sup',
338
- preferWebGPU: true, // default: true
339
- onWebGPUFallback: () => console.log('Fell back to Canvas2D')
343
+ onWebGPUFallback: () => console.log('WebGPU unavailable, trying WebGL2'),
344
+ onWebGL2Fallback: () => console.log('WebGL2 unavailable, using Canvas2D')
340
345
  })
341
346
  ```
342
347
 
343
348
  **Options:**
344
349
 
345
- - `preferWebGPU` (boolean): Enable WebGPU rendering if available. Default: `true`
346
- - `onWebGPUFallback` (function): Callback when WebGPU is unavailable and falls back to Canvas2D
350
+ - `onWebGPUFallback` (function): Callback when WebGPU initialisation fails
351
+ - `onWebGL2Fallback` (function): Callback when WebGL2 initialisation fails
352
+
353
+ **Renderer capabilities:**
354
+
355
+ | Renderer | Premultiplied alpha | Linear sampling | Browser support |
356
+ | -------- | ------------------- | --------------- | --------------- |
357
+ | WebGPU | ✅ | ✅ | Chrome 113+, Firefox 141+, Edge 113+ |
358
+ | WebGL2 | ✅ | ✅ | All modern browsers |
359
+ | Canvas2D | — | ✅ | Universal |
347
360
 
348
361
  ## Low-Level API (Programmatic Use)
349
362
 
@@ -501,11 +514,11 @@ interface VideoSubtitleOptions {
501
514
  subUrl?: string // URL to subtitle file (provide this OR subContent)
502
515
  subContent?: ArrayBuffer // Direct subtitle content (provide this OR subUrl)
503
516
  workerUrl?: string // Worker URL (for API compatibility)
504
- preferWebGPU?: boolean // Prefer WebGPU renderer if available (default: true)
505
517
  onLoading?: () => void // Called when subtitle loading starts
506
518
  onLoaded?: () => void // Called when subtitle loading completes
507
519
  onError?: (error: Error) => void // Called when subtitle loading fails
508
- onWebGPUFallback?: () => void // Called when WebGPU is unavailable
520
+ onWebGPUFallback?: () => void // Called when WebGPU init fails
521
+ onWebGL2Fallback?: () => void // Called when WebGL2 init fails
509
522
  }
510
523
  ```
511
524
 
@@ -566,7 +579,4 @@ interface SubtitleCompositionData {
566
579
 
567
580
  ## License
568
581
 
569
- Licensed under either of
570
-
571
- - Apache License, Version 2.0
572
- - MIT license
582
+ MIT
@@ -4,6 +4,7 @@
4
4
  */
5
5
  import type { SubtitleData, SubtitleDisplaySettings, VideoSubtitleOptions, VideoVobSubOptions } from './types';
6
6
  import { WebGPURenderer } from './webgpu-renderer';
7
+ import { WebGL2Renderer } from './webgl2-renderer';
7
8
  /** Performance statistics for subtitle renderer */
8
9
  export interface SubtitleRendererStats {
9
10
  /** Total frames rendered since initialization */
@@ -54,8 +55,10 @@ declare abstract class BaseVideoSubtitleRenderer {
54
55
  protected displaySettings: SubtitleDisplaySettings;
55
56
  protected webgpuRenderer: WebGPURenderer | null;
56
57
  protected useWebGPU: boolean;
57
- protected preferWebGPU: boolean;
58
58
  protected onWebGPUFallback?: () => void;
59
+ protected webgl2Renderer: WebGL2Renderer | null;
60
+ protected useWebGL2: boolean;
61
+ protected onWebGL2Fallback?: () => void;
59
62
  protected perfStats: {
60
63
  framesRendered: number;
61
64
  framesDropped: number;
@@ -83,6 +86,8 @@ declare abstract class BaseVideoSubtitleRenderer {
83
86
  protected createCanvas(): void;
84
87
  /** Initialize WebGPU renderer. */
85
88
  private initWebGPU;
89
+ /** Initialize WebGL2 renderer. */
90
+ private initWebGL2;
86
91
  /** Initialize Canvas2D renderer. */
87
92
  private initCanvas2D;
88
93
  /** Called when video seeks. */
@@ -108,6 +113,8 @@ declare abstract class BaseVideoSubtitleRenderer {
108
113
  protected renderFrame(time: number, index: number): void;
109
114
  /** Render using WebGPU. */
110
115
  private renderFrameWebGPU;
116
+ /** Render using WebGL2. */
117
+ private renderFrameWebGL2;
111
118
  /** Render using Canvas2D. */
112
119
  private renderFrameCanvas2D;
113
120
  /** Dispose of all resources. */
@@ -1 +1 @@
1
- {"version":3,"file":"renderers.d.ts","sourceRoot":"","sources":["../../src/ts/renderers.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,uBAAuB,EAAE,oBAAoB,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAA;AAK9G,OAAO,EAAE,cAAc,EAAqB,MAAM,mBAAmB,CAAA;AAQrE,mDAAmD;AACnD,MAAM,WAAW,qBAAqB;IACpC,iDAAiD;IACjD,cAAc,EAAE,MAAM,CAAA;IACtB,2CAA2C;IAC3C,aAAa,EAAE,MAAM,CAAA;IACrB,0CAA0C;IAC1C,aAAa,EAAE,MAAM,CAAA;IACrB,0CAA0C;IAC1C,aAAa,EAAE,MAAM,CAAA;IACrB,0CAA0C;IAC1C,aAAa,EAAE,MAAM,CAAA;IACrB,uCAAuC;IACvC,cAAc,EAAE,MAAM,CAAA;IACtB,uCAAuC;IACvC,SAAS,EAAE,MAAM,CAAA;IACjB,4CAA4C;IAC5C,WAAW,EAAE,OAAO,CAAA;IACpB,8BAA8B;IAC9B,YAAY,EAAE,MAAM,CAAA;IACpB,gCAAgC;IAChC,cAAc,EAAE,MAAM,CAAA;IACtB,0CAA0C;IAC1C,YAAY,EAAE,MAAM,CAAA;IACpB,6CAA6C;IAC7C,YAAY,EAAE,MAAM,CAAA;CACrB;AAED;;;GAGG;AACH,uBAAe,yBAAyB;IACtC,SAAS,CAAC,KAAK,EAAE,gBAAgB,CAAA;IACjC,SAAS,CAAC,MAAM,CAAC,EAAE,MAAM,CAAA;IACzB,SAAS,CAAC,UAAU,CAAC,EAAE,WAAW,CAAA;IAClC,SAAS,CAAC,MAAM,EAAE,iBAAiB,GAAG,IAAI,CAA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U3C,+DAA+D;IAC/D,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;IAUnC,oCAAoC;IACpC,IAAI,aAAa,IAAI,OAAO,CAE3B;IAED,OAAO,IAAI,IAAI;CAUhB"}
@@ -7,6 +7,7 @@ import { getOrCreateWorker, sendToWorker } from './worker';
7
7
  import { binarySearchTimestamp, convertFrameData, createWorkerState } from './utils';
8
8
  import { PgsParser, VobSubParserLowLevel } from './parsers';
9
9
  import { WebGPURenderer, isWebGPUSupported } from './webgpu-renderer';
10
+ import { WebGL2Renderer, isWebGL2Supported } from './webgl2-renderer';
10
11
  /** Default display settings */
11
12
  const DEFAULT_DISPLAY_SETTINGS = {
12
13
  scale: 1.0,
@@ -33,11 +34,14 @@ class BaseVideoSubtitleRenderer {
33
34
  lastRenderedData = null;
34
35
  /** Display settings for subtitle rendering */
35
36
  displaySettings = { ...DEFAULT_DISPLAY_SETTINGS };
36
- // WebGPU renderer (optional, falls back to Canvas2D if unavailable)
37
+ // WebGPU renderer (optional, falls back to WebGL2 then Canvas2D)
37
38
  webgpuRenderer = null;
38
39
  useWebGPU = false;
39
- preferWebGPU = true;
40
40
  onWebGPUFallback;
41
+ // WebGL2 renderer (optional, falls back to Canvas2D)
42
+ webgl2Renderer = null;
43
+ useWebGL2 = false;
44
+ onWebGL2Fallback;
41
45
  // Performance tracking
42
46
  perfStats = {
43
47
  framesRendered: 0,
@@ -51,8 +55,8 @@ class BaseVideoSubtitleRenderer {
51
55
  this.video = options.video;
52
56
  this.subUrl = options.subUrl;
53
57
  this.subContent = options.subContent;
54
- this.preferWebGPU = options.preferWebGPU !== false; // Default to true
55
58
  this.onWebGPUFallback = options.onWebGPUFallback;
59
+ this.onWebGL2Fallback = options.onWebGL2Fallback;
56
60
  }
57
61
  /** Get current display settings */
58
62
  getDisplaySettings() {
@@ -126,10 +130,13 @@ class BaseVideoSubtitleRenderer {
126
130
  }
127
131
  parent.appendChild(this.canvas);
128
132
  }
129
- // Try WebGPU first if preferred
130
- if (this.preferWebGPU && isWebGPUSupported()) {
133
+ // Try WebGPU first, then WebGL2, then Canvas2D
134
+ if (isWebGPUSupported()) {
131
135
  this.initWebGPU();
132
136
  }
137
+ else if (isWebGL2Supported()) {
138
+ this.initWebGL2();
139
+ }
133
140
  else {
134
141
  this.initCanvas2D();
135
142
  }
@@ -158,12 +165,41 @@ class BaseVideoSubtitleRenderer {
158
165
  console.log('[libbitsub] Using WebGPU renderer');
159
166
  }
160
167
  catch (error) {
161
- console.warn('[libbitsub] WebGPU init failed, falling back to Canvas2D:', error);
168
+ console.warn('[libbitsub] WebGPU init failed, falling back to WebGL2:', error);
162
169
  this.webgpuRenderer?.destroy();
163
170
  this.webgpuRenderer = null;
164
171
  this.useWebGPU = false;
165
- this.initCanvas2D();
166
172
  this.onWebGPUFallback?.();
173
+ // Try WebGL2 before Canvas2D
174
+ if (isWebGL2Supported()) {
175
+ this.initWebGL2();
176
+ }
177
+ else {
178
+ this.initCanvas2D();
179
+ }
180
+ }
181
+ }
182
+ /** Initialize WebGL2 renderer. */
183
+ async initWebGL2() {
184
+ try {
185
+ this.webgl2Renderer = new WebGL2Renderer();
186
+ await this.webgl2Renderer.init();
187
+ if (!this.canvas)
188
+ return;
189
+ const bounds = this.getVideoContentBounds();
190
+ const width = Math.max(1, bounds.width * window.devicePixelRatio);
191
+ const height = Math.max(1, bounds.height * window.devicePixelRatio);
192
+ await this.webgl2Renderer.setCanvas(this.canvas, width, height);
193
+ this.useWebGL2 = true;
194
+ console.log('[libbitsub] Using WebGL2 renderer');
195
+ }
196
+ catch (error) {
197
+ console.warn('[libbitsub] WebGL2 init failed, falling back to Canvas2D:', error);
198
+ this.webgl2Renderer?.destroy();
199
+ this.webgl2Renderer = null;
200
+ this.useWebGL2 = false;
201
+ this.onWebGL2Fallback?.();
202
+ this.initCanvas2D();
167
203
  }
168
204
  }
169
205
  /** Initialize Canvas2D renderer. */
@@ -172,6 +208,7 @@ class BaseVideoSubtitleRenderer {
172
208
  return;
173
209
  this.ctx = this.canvas.getContext('2d');
174
210
  this.useWebGPU = false;
211
+ this.useWebGL2 = false;
175
212
  console.log('[libbitsub] Using Canvas2D renderer');
176
213
  }
177
214
  /** Called when video seeks. */
@@ -227,10 +264,13 @@ class BaseVideoSubtitleRenderer {
227
264
  this.canvas.style.top = `${bounds.y}px`;
228
265
  this.canvas.style.width = `${bounds.width}px`;
229
266
  this.canvas.style.height = `${bounds.height}px`;
230
- // Update WebGPU renderer size if active
267
+ // Update GPU renderer size if active
231
268
  if (this.useWebGPU && this.webgpuRenderer) {
232
269
  this.webgpuRenderer.updateSize(pixelWidth, pixelHeight);
233
270
  }
271
+ else if (this.useWebGL2 && this.webgl2Renderer) {
272
+ this.webgl2Renderer.updateSize(pixelWidth, pixelHeight);
273
+ }
234
274
  this.lastRenderedIndex = -1;
235
275
  this.lastRenderedTime = -1;
236
276
  }
@@ -290,10 +330,13 @@ class BaseVideoSubtitleRenderer {
290
330
  return;
291
331
  }
292
332
  }
293
- // Use WebGPU or Canvas2D based on availability
333
+ // Use best available renderer
294
334
  if (this.useWebGPU && this.webgpuRenderer) {
295
335
  this.renderFrameWebGPU(data, index);
296
336
  }
337
+ else if (this.useWebGL2 && this.webgl2Renderer) {
338
+ this.renderFrameWebGL2(data, index);
339
+ }
297
340
  else {
298
341
  this.renderFrameCanvas2D(data, index);
299
342
  }
@@ -320,6 +363,24 @@ class BaseVideoSubtitleRenderer {
320
363
  const offsetY = (verticalOffset / 100) * this.canvas.height;
321
364
  this.webgpuRenderer.render(data.compositionData, data.width, data.height, scaleX, scaleY, offsetY);
322
365
  }
366
+ /** Render using WebGL2. */
367
+ renderFrameWebGL2(data, index) {
368
+ if (!this.webgl2Renderer || !this.canvas)
369
+ return;
370
+ if (index < 0 || !data || data.compositionData.length === 0) {
371
+ this.webgl2Renderer.clear();
372
+ this.lastRenderedData = null;
373
+ return;
374
+ }
375
+ this.lastRenderedData = data;
376
+ const baseScaleX = this.canvas.width / data.width;
377
+ const baseScaleY = this.canvas.height / data.height;
378
+ const { scale, verticalOffset } = this.displaySettings;
379
+ const scaleX = baseScaleX * scale;
380
+ const scaleY = baseScaleY * scale;
381
+ const offsetY = (verticalOffset / 100) * this.canvas.height;
382
+ this.webgl2Renderer.render(data.compositionData, data.width, data.height, scaleX, scaleY, offsetY);
383
+ }
323
384
  /** Render using Canvas2D. */
324
385
  renderFrameCanvas2D(data, index) {
325
386
  if (!this.ctx || !this.canvas)
@@ -370,11 +431,15 @@ class BaseVideoSubtitleRenderer {
370
431
  }
371
432
  this.resizeObserver?.disconnect();
372
433
  this.resizeObserver = null;
373
- // Clean up WebGPU renderer
434
+ // Clean up GPU renderers
374
435
  if (this.webgpuRenderer) {
375
436
  this.webgpuRenderer.destroy();
376
437
  this.webgpuRenderer = null;
377
438
  }
439
+ if (this.webgl2Renderer) {
440
+ this.webgl2Renderer.destroy();
441
+ this.webgl2Renderer = null;
442
+ }
378
443
  this.canvas?.parentElement?.removeChild(this.canvas);
379
444
  this.canvas = null;
380
445
  this.ctx = null;
@@ -382,6 +447,7 @@ class BaseVideoSubtitleRenderer {
382
447
  this.tempCtx = null;
383
448
  this.lastRenderedData = null;
384
449
  this.useWebGPU = false;
450
+ this.useWebGL2 = false;
385
451
  }
386
452
  }
387
453
  /**