Skip to content

Commit

Permalink
fix: Offset text regions that are out of viewport (#6986)
Browse files Browse the repository at this point in the history
Fixes #3732 (we
resolve it using offset operation instead of clipping)
  • Loading branch information
avelad committed Jul 8, 2024
1 parent c9e4a0a commit e81dfe6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
23 changes: 20 additions & 3 deletions lib/text/ui_text_displayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,9 @@ shaka.text.UITextDisplayer = class {

const regionElement = shaka.util.Dom.createHTMLElement('span');

const linesUnit = shaka.text.CueRegion.units.LINES;
const percentageUnit = shaka.text.CueRegion.units.PERCENTAGE;
const pixelUnit = shaka.text.CueRegion.units.PX;
const heightUnit = region.heightUnits == percentageUnit ? '%' : 'px';
const widthUnit = region.widthUnits == percentageUnit ? '%' : 'px';
const viewportAnchorUnit =
Expand All @@ -479,12 +481,14 @@ shaka.text.UITextDisplayer = class {

regionElement.style.position = 'absolute';

const linesUnit = shaka.text.CueRegion.units.LINES;
if (region.heightUnits === linesUnit && region.widthUnits === linesUnit) {
if (region.heightUnits === linesUnit) {
regionElement.style.height = region.height * lineHeightMultiple + '%';
regionElement.style.width = region.width * lineWidthMultiple + '%';
} else {
regionElement.style.height = region.height + heightUnit;
}
if (region.widthUnits === linesUnit) {
regionElement.style.width = region.width * lineWidthMultiple + '%';
} else {
regionElement.style.width = region.width + widthUnit;
}

Expand All @@ -504,6 +508,19 @@ shaka.text.UITextDisplayer = class {
regionElement.style.left = region.viewportAnchorX -
region.regionAnchorX * region.width / 100 + viewportAnchorUnit;
}
if (region.heightUnits !== pixelUnit &&
region.widthUnits !== pixelUnit &&
region.viewportAnchorUnits !== pixelUnit) {
// Clip region
const top = parseInt(regionElement.style.top.slice(0, -1), 10) || 0;
const left = parseInt(regionElement.style.left.slice(0, -1), 10) || 0;
const height = parseInt(regionElement.style.height.slice(0, -1), 10) || 0;
const width = parseInt(regionElement.style.width.slice(0, -1), 10) || 0;
const realTop = Math.max(0, Math.min(100 - height, top));
const realLeft = Math.max(0, Math.min(100 - width, left));
regionElement.style.top = realTop + '%';
regionElement.style.left = realLeft + '%';
}

regionElement.style.display = 'flex';
regionElement.style.flexDirection = 'column';
Expand Down
3 changes: 2 additions & 1 deletion test/text/text_displayer_layout_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,8 @@ filterDescribe('Cue layout', shaka.test.TextLayoutTests.supported, () => {
const cue = new shaka.text.Cue(0, 1, '');
cue.region.id = '1';
cue.region.height = 15; // %
cue.region.viewportAnchorX = 20; // %
cue.region.width = 60; // %
cue.region.viewportAnchorX = 40; // %
cue.region.viewportAnchorY = 10; // %
// eslint-disable-next-line max-len
cue.backgroundImage = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAHAAAAA8BAMAAABMVCNvAAAAElBMVEX9/vtRldU+uUv6vAnvNjWzvsvH461AAAABvUlEQVRIx7WWwY6CMBRFYQb2Nhn22sS9o5m9icy+aPn/X5lS6r221gfTxLsC0pN7XovE6j1p9d6UcB/apYTUU/YlhT7bArCwUocUg9sCsMxVF7i2Pwd3+v93/Ty5uEbZtVVd+nacphw08oJzMUmhzxWgyYHDBHbRo9sMHmVX5bOJTENyrq0rMjB1EUG61ipMBtBEYM41qakVXLk3eVdyagMQrqNB47ProO4hGCbu+/4MMHUlp6J2F8c58pR3bRVj/B1cm94nB2IlatSDq53BW8Z1i4UEed3PueQqhxTkA781L10JBb2aNwAvGdddAj66NhKon0C6onHZtXKhKxolVx46XbtyUHLdRaaxqyXISoBxYfQOftFVBPnZuSfrSpCFcBXAYwp2HlrtSrBi1BrXlFt2RSEGXOn6DRCc6EpQgYMoXGWQIbLsSpCizAOIABQ4yZVgV2VAwfXlgPJvi6DJgnUGvMEUA0qgBTh6cOCAsmsD8LdDIQYUKiuA50FdBwy4DFqAtXCCDMAGYEtOCDYBrgYfFfn/G0ALsAInVgYruIaHnQSxcroKI1ZrU9/PuQmmq9OO43xhhUI5jR3lBX8x/RKsZNOu/wAAAABJRU5ErkJggg==';
Expand Down

0 comments on commit e81dfe6

Please sign in to comment.