Skip to content

Commit e9fc7c5

Browse files
JustAMandmitrylyzoTheOneric
committed
Add options to scale canvas relative to video
JSO struggles on complex subtitles and higher resolutions, scaling the sub-canvas down if the canvas gets larger than a size deemed good enough can be used to somewhat work around this at the expense of introducing some blurriness. Scaling up is also possible but probably less useful. This was ported from jellyfin's fork and jellyfin-web uses this on at least some devices. However the option names were changed to better match their actual function. [Vasily <just.one.man@yandex.ru>] Original implementation together with a hard canvas height limit in jellyfin@345d701 jellyfin@bcf4b5f [Dmitry Lyzo <ashephard0@gmail.com>] Rebased for upstream; removed default hardHeightLimit and added docs [Oneric <oneric@oneric.stub>] Simplified scaling logic (identical results for positive values); fixed zero and negativity bugs in scaling logic; changed option names for clarity; split prescaling from hard height limit. Co-Authored-By: Dmitry Lyzo <ashephard0@gmail.com> Co-Authored-By: Oneric <oneric@oneric.stub>
1 parent 29be5f9 commit e9fc7c5

2 files changed

Lines changed: 31 additions & 2 deletions

File tree

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ When creating an instance of SubtitleOctopus, you can set the following options:
132132
(Default: `0` - no limit)
133133
- `libassGlyphLimit`: libass glyph cache memory limit in MiB (approximate)
134134
(Default: `0` - no limit)
135+
- `prescaleFactor`: Scale down (`< 1.0`) the subtitles canvas to improve
136+
performance at the expense of quality, or scale it up (`> 1.0`).
137+
(Default: `1.0` - no scaling; must be a number > 0)
138+
- `prescaleHeightLimit`: The height beyond which the subtitles canvas won't be prescaled.
139+
(Default: `1080`)
135140

136141
### Rendering Modes
137142
#### JS Blending

src/subtitles-octopus.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ var SubtitlesOctopus = function (options) {
1717
self.libassMemoryLimit = options.libassMemoryLimit || 0;
1818
self.libassGlyphLimit = options.libassGlyphLimit || 0;
1919
self.targetFps = options.targetFps || 24;
20+
self.prescaleFactor = options.prescaleFactor || 1.0;
21+
self.prescaleHeightLimit = options.prescaleHeightLimit || 1080;
2022
self.isOurCanvas = false; // (internal) we created canvas and manage it
2123
self.video = options.video; // HTML video element (optional if canvas specified)
2224
self.canvasParent = null; // (internal) HTML canvas parent element
@@ -404,14 +406,36 @@ var SubtitlesOctopus = function (options) {
404406
}
405407
};
406408

409+
function _computeCanvasSize(width, height) {
410+
var scalefactor = self.prescaleFactor <= 0 ? 1.0 : self.prescaleFactor;
411+
412+
if (height <= 0 || width <= 0) {
413+
width = 0;
414+
height = 0;
415+
} else {
416+
var sgn = scalefactor < 1 ? -1 : 1;
417+
var newH = height;
418+
if (sgn * newH * scalefactor <= sgn * self.prescaleHeightLimit)
419+
newH *= scalefactor;
420+
else if (sgn * newH < sgn * self.prescaleHeightLimit)
421+
newH = self.prescaleHeightLimit;
422+
423+
width *= newH / height;
424+
height = newH;
425+
}
426+
427+
return {'width': width, 'height': height};
428+
}
429+
407430
self.resize = function (width, height, top, left) {
408431
var videoSize = null;
409432
top = top || 0;
410433
left = left || 0;
411434
if ((!width || !height) && self.video) {
412435
videoSize = self.getVideoPosition();
413-
width = videoSize.width * self.pixelRatio;
414-
height = videoSize.height * self.pixelRatio;
436+
var newSize = _computeCanvasSize(videoSize.width * self.pixelRatio, videoSize.height * self.pixelRatio);
437+
width = newSize.width;
438+
height = newSize.height;
415439
var offset = self.canvasParent.getBoundingClientRect().top - self.video.getBoundingClientRect().top;
416440
top = videoSize.y - offset;
417441
left = videoSize.x;

0 commit comments

Comments
 (0)