Skip to content

Commit

Permalink
add support and unit tests for styling on SimpleTextDisplayer (shaka-…
Browse files Browse the repository at this point in the history
  • Loading branch information
muhammadharis committed Aug 11, 2020
1 parent 4499149 commit 2407734
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
23 changes: 20 additions & 3 deletions lib/text/simple_text_displayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,25 @@ shaka.text.SimpleTextDisplayer = class {
// Flatten nested cue payloads recursively. If a cue has nested cues,
// their contents should be combined and replace the payload of the parent.
const flattenPayload = (cue) => {
// TODO: If we want to support bold, italic, underline here, we would
// insert markup into the payload.
// Handle styles (currently underlines/italics). TODO support for color.
const openStyleTags = [];
const italics = cue.fontStyle == shaka.text.Cue.fontStyle.ITALIC;
const underline = cue.textDecoration.includes(
shaka.text.Cue.textDecoration.UNDERLINE);
if (italics) {
openStyleTags.push('i');
}
if (underline) {
openStyleTags.push('u');
}

// Prefix opens tags, suffix closes tags in reverse order of opening.
const prefixStyleTags = openStyleTags.reduce((acc, tag) => {
return `${acc}<${tag}>`;
}, '');
const suffixStyleTags = openStyleTags.reduceRight((acc, tag) => {
return `${acc}</${tag}>`;
}, '');

if (cue.spacer) {
// This is a vertical spacer, so insert a newline.
Expand All @@ -92,7 +109,7 @@ shaka.text.SimpleTextDisplayer = class {
return cue.nestedCues.map(flattenPayload).join('');
} else {
// This is a real cue.
return cue.payload;
return prefixStyleTags+cue.payload+suffixStyleTags;
}
};

Expand Down
46 changes: 46 additions & 0 deletions test/text/simple_text_displayer_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,52 @@ describe('SimpleTextDisplayer', () => {
[shakaCue]);
});

it('creates style tags for cues with underline/italics properties', () => {
const shakaCue = new shaka.text.Cue(10, 20, '');

// First cue is underlined and italicized.
const nestedCue1 = new shaka.text.Cue(10, 20, 'Test1');
nestedCue1.fontStyle = shaka.text.Cue.fontStyle.ITALIC;
nestedCue1.textDecoration.push(shaka.text.Cue.textDecoration.UNDERLINE);

// Second cue is only italicized.
const nestedCue2 = new shaka.text.Cue(10, 20, 'Test2');
nestedCue2.fontStyle = shaka.text.Cue.fontStyle.ITALIC;

// Third cue has no underline or italics.
const nestedCue3 = new shaka.text.Cue(10, 20, 'Test3');

// Fourth cue is only underlined.
const nestedCue4 = new shaka.text.Cue(10, 20, 'Test4');
nestedCue4.textDecoration.push(shaka.text.Cue.textDecoration.UNDERLINE);

const expectedText = '<i><u>Test1</u></i><i>Test2</i>Test3<u>Test4</u>';
shakaCue.nestedCues = [nestedCue1, nestedCue2, nestedCue3, nestedCue4];
verifyHelper(
[
{startTime: 10, endTime: 20, text: expectedText},
],
[shakaCue]);
});

it('adds linebreaks when a linebreak cue is seen', () => {
const shakaCue = new shaka.text.Cue(10, 20, '');
const nestedCue1 = new shaka.text.Cue(10, 20, 'Test1');

// Second cue is a linebreak cue.
const nestedCue2 = new shaka.text.Cue(10, 20, '');
nestedCue2.spacer = true;

const nestedCue3 = new shaka.text.Cue(10, 20, 'Test2');

shakaCue.nestedCues = [nestedCue1, nestedCue2, nestedCue3];
verifyHelper(
[
{startTime: 10, endTime: 20, text: 'Test1\nTest2'},
],
[shakaCue]);
});

it('skips duplicate cues', () => {
const cue1 = new shaka.text.Cue(10, 20, 'Test');
displayer.append([cue1]);
Expand Down

0 comments on commit 2407734

Please sign in to comment.