Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix TTML extent and origin percentage calculations #3029 #3044

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 24 additions & 18 deletions lib/text/ttml_text_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,16 +368,18 @@ shaka.text.TtmlTextParser = class {
percentage = TtmlTextParser.percentValues_.exec(extent);
results = percentage || TtmlTextParser.pixelValues_.exec(extent);
if (results != null) {
if (globalWidth != null) {
region.width = Number(results[1]) * 100 / globalWidth;
} else {
region.width = Number(results[1]);
}
if (globalHeight != null) {
region.height = Number(results[2]) * 100 / globalHeight;
} else {
region.height = Number(results[2]);
region.width = Number(results[1]);
region.height = Number(results[2]);

if (!percentage) {
if (globalWidth != null) {
region.width = region.width * 100 / globalWidth;
}
if (globalHeight != null) {
region.height = region.height * 100 / globalHeight;
}
}

region.widthUnits = percentage || globalWidth != null ?
shaka.text.CueRegion.units.PERCENTAGE :
shaka.text.CueRegion.units.PX;
Expand All @@ -394,16 +396,20 @@ shaka.text.TtmlTextParser = class {
percentage = TtmlTextParser.percentValues_.exec(origin);
results = percentage || TtmlTextParser.pixelValues_.exec(origin);
if (results != null) {
if (globalWidth != null) {
region.viewportAnchorX = Number(results[1]) * 100 / globalWidth;
} else {
region.viewportAnchorX = Number(results[1]);
}
if (globalHeight != null) {
region.viewportAnchorY = Number(results[2]) * 100 / globalHeight;
} else {
region.viewportAnchorY = Number(results[2]);
region.viewportAnchorX = Number(results[1]);
region.viewportAnchorY = Number(results[2]);

if (!percentage) {
if (globalHeight != null) {
region.viewportAnchorY = region.viewportAnchorY * 100 /
globalHeight;
}
if (globalWidth != null) {
region.viewportAnchorX = region.viewportAnchorX * 100 /
globalWidth;
}
}

region.viewportAnchorUnits = percentage || globalWidth != null ?
shaka.text.CueRegion.units.PERCENTAGE :
shaka.text.CueRegion.units.PX;
Expand Down
135 changes: 133 additions & 2 deletions test/text/ttml_text_parser_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ describe('TtmlTextParser', () => {
{periodStart: 0, segmentStart: 0, segmentEnd: 0});
});

it('supports region settings in pixels', () => {
it('supports region settings in pixels (origin)', () => {
verifyHelper(
[
{
Expand Down Expand Up @@ -595,7 +595,9 @@ describe('TtmlTextParser', () => {
'<p begin="01:02.05" end="01:02:03.200">Test</p>' +
'</div></body></tt>',
{periodStart: 0, segmentStart: 0, segmentEnd: 0});
});

it('supports region settings in pixels (extent)', () => {
verifyHelper(
[
{
Expand All @@ -619,7 +621,136 @@ describe('TtmlTextParser', () => {
],
'<tt xmlns:tts="http://www.w3.org/ns/ttml#styling">' +
'<layout>' +
'<region xml:id="subtitleArea" tts:extent="50px 16px" ' +
'<region xml:id="subtitleArea" tts:extent="50px 16px"/>' +
'</layout>' +
'<body region="subtitleArea"><div>' +
'<p begin="01:02.05" end="01:02:03.200">Test</p>' +
'</div></body></tt>',
{periodStart: 0, segmentStart: 0, segmentEnd: 0});
});

it('supports region settings in pixels: origin (with global extent)', () => {
verifyHelper(
[
{
startTime: 62.05,
endTime: 3723.2,
payload: 'Test',
region: {
id: 'subtitleArea',
viewportAnchorX: 10,
viewportAnchorY: 10,
regionAnchorX: 0,
regionAnchorY: 0,
width: 100,
height: 100,
heightUnits: CueRegion.units.PERCENTAGE,
widthUnits: CueRegion.units.PERCENTAGE,
viewportAnchorUnits: CueRegion.units.PERCENTAGE,
scroll: CueRegion.scrollMode.NONE,
},
},
],
'<tt xmlns:tts="http://www.w3.org/ns/ttml#styling" tts:extent="1920px 1080px">' +
'<layout>' +
'<region xml:id="subtitleArea" tts:origin="108px 192px"/>' +
'</layout>' +
'<body region="subtitleArea"><div>' +
'<p begin="01:02.05" end="01:02:03.200">Test</p>' +
'</div></body></tt>',
{periodStart: 0, segmentStart: 0, segmentEnd: 0});
});

it('supports region settings in pixels: extent (with global extent)', () => {
verifyHelper(
[
{
startTime: 62.05,
endTime: 3723.2,
payload: 'Test',
region: {
id: 'subtitleArea',
viewportAnchorX: 0,
viewportAnchorY: 0,
regionAnchorX: 0,
regionAnchorY: 0,
width: 30,
height: 30,
heightUnits: CueRegion.units.PERCENTAGE,
widthUnits: CueRegion.units.PERCENTAGE,
viewportAnchorUnits: CueRegion.units.PERCENTAGE,
scroll: CueRegion.scrollMode.NONE,
},
},
],
'<tt xmlns:tts="http://www.w3.org/ns/ttml#styling" tts:extent="1920px 1080px">' +
'<layout>' +
'<region xml:id="subtitleArea" tts:extent="576px 324px" ' +
'tts:writingMode="lrtb" />' +
'</layout>' +
'<body region="subtitleArea"><div>' +
'<p begin="01:02.05" end="01:02:03.200">Test</p>' +
'</div></body></tt>',
{periodStart: 0, segmentStart: 0, segmentEnd: 0});
});

it('supports region settings in percentage (origin)', () => {
verifyHelper(
[
{
startTime: 62.05,
endTime: 3723.2,
payload: 'Test',
region: {
id: 'subtitleArea',
viewportAnchorX: 50,
viewportAnchorY: 16,
regionAnchorX: 0,
regionAnchorY: 0,
width: 100,
height: 100,
heightUnits: CueRegion.units.PERCENTAGE,
widthUnits: CueRegion.units.PERCENTAGE,
viewportAnchorUnits: CueRegion.units.PERCENTAGE,
scroll: CueRegion.scrollMode.NONE,
},
},
],
'<tt xmlns:tts="http://www.w3.org/ns/ttml#styling" tts:extent="1920px 1080px">' +
'<layout>' +
'<region xml:id="subtitleArea" tts:origin="50% 16%"/>' +
'</layout>' +
'<body region="subtitleArea"><div>' +
'<p begin="01:02.05" end="01:02:03.200">Test</p>' +
'</div></body></tt>',
{periodStart: 0, segmentStart: 0, segmentEnd: 0});
});

it('supports region settings in percentage (extent)', () => {
verifyHelper(
[
{
startTime: 62.05,
endTime: 3723.2,
payload: 'Test',
region: {
id: 'subtitleArea',
viewportAnchorX: 0,
viewportAnchorY: 0,
regionAnchorX: 0,
regionAnchorY: 0,
width: 50,
height: 16,
heightUnits: CueRegion.units.PERCENTAGE,
widthUnits: CueRegion.units.PERCENTAGE,
viewportAnchorUnits: CueRegion.units.PERCENTAGE,
scroll: CueRegion.scrollMode.NONE,
},
},
],
'<tt xmlns:tts="http://www.w3.org/ns/ttml#styling" tts:extent="1920px 1080px">' +
'<layout>' +
'<region xml:id="subtitleArea" tts:extent="50% 16%" ' +
'tts:writingMode="lrtb" />' +
'</layout>' +
'<body region="subtitleArea"><div>' +
Expand Down