Skip to content

Commit ea80135

Browse files
added lazyFontLoading option for picking between direct or lazy loading for filesystem
fixed blob: URI using lazy loading. added default fallback font, with possibility to override it. bumped libass to release 0.15.1
1 parent d678367 commit ea80135

File tree

11 files changed

+49
-39
lines changed

11 files changed

+49
-39
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ EMCC_COMMON_ARGS = \
331331
-s MALLOC=emmalloc \
332332
-s ALLOW_MEMORY_GROWTH=1 \
333333
-s INITIAL_MEMORY=67108864 \
334+
--embed-file assets/default.woff2 \
334335
--embed-file assets/fonts.conf \
335336
-s FORCE_FILESYSTEM=1 \
336337
-s ENVIRONMENT=web,worker \

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
SubtitlesOctopus displays subtitles in .ass format and easily integrates with HTML5 videos. It supports all SSA/ASS features and fully compatible with [libass](https://github.com/libass/libass).
1+
SubtitlesOctopus displays subtitles in .ass format and easily integrates with HTML5 videos. It supports all SSA/ASS features supported by [libass](https://github.com/libass/libass).
22

33
## Features
44

@@ -7,7 +7,7 @@ SubtitlesOctopus displays subtitles in .ass format and easily integrates with HT
77
- Works fast (because uses WebAssembly with fallback to asm.js if it's not available)
88
- Uses Web Workers thus video and interface doesn't lag even on "heavy" subtitles (working in background)
99
- Doesn't use DOM manipulations and render subtitles on single canvas
10-
- Fully compatible with [libass](https://github.com/libass/libass)
10+
- Uses [libass](https://github.com/libass/libass) natively.
1111
- Easy to use - just connect it to video element
1212

1313
## Included Libraries
@@ -27,7 +27,7 @@ To start using SubtitlesOctopus you only need to instantiate a new instance of
2727
var options = {
2828
video: document.getElementById('video'), // HTML5 video element
2929
subUrl: '/test/test.ass', // Link to subtitles
30-
fallbackFont: '/test/font-1.ttf', // Fallback font to be used in case none can be loaded / or has special characters
30+
fallbackFont: '/test/font-1.ttf', // Fallback font to be used in case subtitles have special characters
3131
fonts: ['/test/font-1.ttf', '/test/font-2.ttf'], // Links to fonts (not required, default font already included in build)
3232
workerUrl: '/subtitles-octopus-worker.js', // Link to worker file "libassjs-worker.js"
3333
};
@@ -47,7 +47,7 @@ the time the subtitles should render at yourself:
4747
var options = {
4848
canvas: document.getElementById('canvas'), // canvas element
4949
subUrl: '/test/test.ass', // Link to subtitles
50-
fallbackFont: '/test/font-1.ttf', // Fallback font to be used in case none can be loaded / or has special characters
50+
fallbackFont: '/test/font-1.ttf', // Fallback font to be used in case subtitles have special characters
5151
fonts: ['/test/font-1.ttf', '/test/font-2.ttf'], // Links to fonts
5252
workerUrl: '/subtitles-octopus-worker.js' // Link to file "libassjs-worker.js"
5353
};
@@ -100,7 +100,8 @@ When creating an instance of SubtitleOctopus, you can set the following options:
100100
- `subContent`: The content of the subtitle file to play. (Require either
101101
`subContent` or `subUrl` to be specified)
102102
- `workerUrl`: The URL of the worker. (Default: `subtitles-octopus-worker.js`)
103-
- `fallbackFont`: The URL of a fallback font to be used in case none can be loaded / or has special characters
103+
- `fallbackFont`: URL to override fallback font, for example, with a CJK one. Default fallback font is Liberation Sans (Optional)
104+
- `lazyFontLoading`: Load fonts in a lazy way. Requires Access-Control-Expose-Headers for Accept-Ranges, Content-Length, and Content-Encoding. If Content-Encoding is compressed, file will be fully fetched instead of just a HEAD request.
104105
- `fonts`: An array of links to the fonts used in the subtitle. (Optional)
105106
- `availableFonts`: Object with all available fonts - Key is font name in lower
106107
case, value is link: `{"arial": "/font1.ttf"}` (Optional)
@@ -114,7 +115,7 @@ When creating an instance of SubtitleOctopus, you can set the following options:
114115
`false`)
115116

116117
### Fast Render Mode (Lossy) (EXPERIMENTAL)
117-
The Fast Render mode has been created by @no1d as a suggestion for fix browser freezing when rendering heavy subtitles (#46), it use [createImageBitmap](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/createImageBitmap) to render the bitmap in the Worker, using Promises instead of direct render on canvas in the Main Thread. When the browser start to hang, it will not lock main thread, instead will run Async, so if the function createImageBitmap fail, it will not stop the rendering process at all and may cause some bitmap loss or simply will not draw anything in canvas, mostly on low end devices.
118+
The Fast Render mode has been created by @no1d as a suggestion for fix browser freezing when rendering heavy subtitles (#46), it uses [createImageBitmap](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/createImageBitmap) to render the bitmap in the Worker, using Promises instead of direct render on canvas in the Main Thread. When the browser start to hang, it will not lock main thread, instead will run Async, so if the function createImageBitmap fail, it will not stop the rendering process at all and may cause some bitmap loss or simply will not draw anything in canvas, mostly on low end devices.
118119

119120
**WARNING: Experimental, not stable and not working in Safari**
120121

assets/default.woff2

143 KB
Binary file not shown.

dist/js/subtitles-octopus-worker.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
354 Bytes
Binary file not shown.

dist/js/subtitles-octopus-worker.wasm.js

Lines changed: 22 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/js/subtitles-octopus.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ var SubtitlesOctopus = function (options) {
22
var self = this;
33
self.canvas = options.canvas; // HTML canvas element (optional if video specified)
44
self.renderMode = options.renderMode || (options.lossyRender ? 'fast' : (options.blendRender ? 'blend' : 'normal'));
5-
if(self.renderMode == "fast" && typeof createImageBitmap === 'undefined'){
5+
if(self.renderMode === "fast" && typeof createImageBitmap === 'undefined'){
6+
//Fallback for browsers not supporting fast renderMode (Safari)
67
self.renderMode = "normal";
78
}
89

@@ -20,7 +21,8 @@ var SubtitlesOctopus = function (options) {
2021
self.isOurCanvas = false; // (internal) we created canvas and manage it
2122
self.video = options.video; // HTML video element (optional if canvas specified)
2223
self.canvasParent = null; // (internal) HTML canvas parent element
23-
self.fallbackFont = options.fallbackFont;
24+
self.fallbackFont = options.fallbackFont || null; // Override fallback font, for example, with a CJK one. Default fallback font is Liberation Sans
25+
self.lazyFontLoading = options.lazyFontLoading || false; // Load fonts in a lazy way. Requires Access-Control-Expose-Headers for Accept-Ranges, Content-Length, and Content-Encoding. If Content-Encoding is compressed, file will be fully fetched instead of just a HEAD request.
2426
self.fonts = options.fonts || []; // Array with links to fonts used in sub (optional)
2527
self.availableFonts = options.availableFonts || []; // Object with all available fonts (optional). Key is font name in lower case, value is link: {"arial": "/font1.ttf"}
2628
self.onReadyEvent = options.onReady; // Function called when SubtitlesOctopus is ready (optional)
@@ -115,6 +117,7 @@ var SubtitlesOctopus = function (options) {
115117
subUrl: self.subUrl,
116118
subContent: self.subContent,
117119
fallbackFont: self.fallbackFont,
120+
lazyFontLoading: self.lazyFontLoading,
118121
fonts: self.fonts,
119122
availableFonts: self.availableFonts,
120123
debug: self.debug,

src/post-worker.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ self.writeFontToFS = function(font) {
9595
return;
9696
}
9797

98-
Module["FS_createLazyFile"]("/fonts", 'font' + (self.fontId++) + '-' + path.split('/').pop(), path, true, false);
98+
Module[(self.lazyFontLoading && path.indexOf("blob:") !== 0) ? "FS_createLazyFile" : "FS_createPreloadedFile"]("/fonts", 'font' + (self.fontId++) + '-' + path.split('/').pop(), path, true, false);
9999
};
100100

101101
/**
@@ -652,6 +652,7 @@ function onMessageFromMainEmscriptenThread(message) {
652652
}
653653
self.availableFonts = message.data.availableFonts;
654654
self.fallbackFont = message.data.fallbackFont;
655+
self.lazyFontLoading = message.data.lazyFontLoading;
655656
self.debug = message.data.debug;
656657
if (!hasNativeConsole && self.debug) {
657658
console = makeCustomConsole();

src/pre-worker.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,13 @@ Module["preRun"].push(function () {
115115

116116
self.subContent = null;
117117

118-
Module["FS_createLazyFile"]("/fonts", ".fallback-" + self.fallbackFont.split('/').pop(), self.fallbackFont, true, false);
118+
if(self.fallbackFont) {
119+
Module[(self.lazyFontLoading && self.fallbackFont.indexOf("blob:") !== 0) ? "FS_createLazyFile" : "FS_createPreloadedFile"]("/fonts", ".fallback-" + self.fallbackFont.split('/').pop(), self.fallbackFont, true, false);
120+
}
119121

120-
//Module["FS"].mount(Module["FS"].filesystems.IDBFS, {}, '/fonts');
121122
const fontFiles = self.fontFiles || [];
122123
for (i = 0; i < fontFiles.length; i++) {
123-
Module["FS_createLazyFile"]("/fonts", 'font' + i + '-' + fontFiles[i].split('/').pop(), fontFiles[i], true, false);
124+
Module[(self.lazyFontLoading && fontFiles[i].indexOf("blob:") !== 0) ? "FS_createLazyFile" : "FS_createPreloadedFile"]("/fonts", 'font' + i + '-' + fontFiles[i].split('/').pop(), fontFiles[i], true, false);
124125
}
125126
});
126127

@@ -132,7 +133,7 @@ Module['onRuntimeInitialized'] = function () {
132133
if(self.debug){
133134
self.octObj.setLogLevel(7);
134135
}
135-
self.octObj.initLibrary(screen.width, screen.height, "/fonts/.fallback-" + self.fallbackFont.split('/').pop());
136+
self.octObj.initLibrary(screen.width, screen.height, self.fallbackFont ? "/fonts/.fallback-" + self.fallbackFont.split('/').pop() : "/default.woff2");
136137
self.octObj.setDropAnimations(!!self.dropAllAnimations);
137138
self.octObj.createTrack("/sub.ass");
138139
self.ass_track = self.octObj.track;

0 commit comments

Comments
 (0)