Skip to content

Commit 91a284f

Browse files
feat(text): Render bold/italics/underline on SimpleTextDisplayer (#2779)
Related to #2648, #2357, and #2776
1 parent f42ccd2 commit 91a284f

File tree

2 files changed

+73
-3
lines changed

2 files changed

+73
-3
lines changed

lib/text/simple_text_displayer.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,30 @@ shaka.text.SimpleTextDisplayer = class {
8282
// Flatten nested cue payloads recursively. If a cue has nested cues,
8383
// their contents should be combined and replace the payload of the parent.
8484
const flattenPayload = (cue) => {
85-
// TODO: If we want to support bold, italic, underline here, we would
86-
// insert markup into the payload.
85+
// Handle styles (currently bold/italics/underline).
86+
// TODO add support for color rendering.
87+
const openStyleTags = [];
88+
const bold = cue.fontWeight >= shaka.text.Cue.fontWeight.BOLD;
89+
const italics = cue.fontStyle == shaka.text.Cue.fontStyle.ITALIC;
90+
const underline = cue.textDecoration.includes(
91+
shaka.text.Cue.textDecoration.UNDERLINE);
92+
if (bold) {
93+
openStyleTags.push('b');
94+
}
95+
if (italics) {
96+
openStyleTags.push('i');
97+
}
98+
if (underline) {
99+
openStyleTags.push('u');
100+
}
101+
102+
// Prefix opens tags, suffix closes tags in reverse order of opening.
103+
const prefixStyleTags = openStyleTags.reduce((acc, tag) => {
104+
return `${acc}<${tag}>`;
105+
}, '');
106+
const suffixStyleTags = openStyleTags.reduceRight((acc, tag) => {
107+
return `${acc}</${tag}>`;
108+
}, '');
87109

88110
if (cue.spacer) {
89111
// This is a vertical spacer, so insert a newline.
@@ -92,7 +114,7 @@ shaka.text.SimpleTextDisplayer = class {
92114
return cue.nestedCues.map(flattenPayload).join('');
93115
} else {
94116
// This is a real cue.
95-
return cue.payload;
117+
return prefixStyleTags + cue.payload + suffixStyleTags;
96118
}
97119
};
98120

test/text/simple_text_displayer_unit.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,54 @@ describe('SimpleTextDisplayer', () => {
9595
[shakaCue]);
9696
});
9797

98+
it('creates style tags for cues with underline/italics/bold', () => {
99+
const shakaCue = new shaka.text.Cue(10, 20, '');
100+
101+
// First cue is underlined and italicized.
102+
const nestedCue1 = new shaka.text.Cue(10, 20, 'Test1');
103+
nestedCue1.fontStyle = shaka.text.Cue.fontStyle.ITALIC;
104+
nestedCue1.textDecoration.push(shaka.text.Cue.textDecoration.UNDERLINE);
105+
106+
// Second cue is italicized and bolded.
107+
const nestedCue2 = new shaka.text.Cue(10, 20, 'Test2');
108+
nestedCue2.fontStyle = shaka.text.Cue.fontStyle.ITALIC;
109+
nestedCue2.fontWeight = shaka.text.Cue.fontWeight.BOLD;
110+
111+
// Third cue has no bold, italics, or underline.
112+
const nestedCue3 = new shaka.text.Cue(10, 20, 'Test3');
113+
114+
// Fourth cue is only underlined.
115+
const nestedCue4 = new shaka.text.Cue(10, 20, 'Test4');
116+
nestedCue4.textDecoration.push(shaka.text.Cue.textDecoration.UNDERLINE);
117+
118+
const expectedText =
119+
'<i><u>Test1</u></i><b><i>Test2</i></b>Test3<u>Test4</u>';
120+
shakaCue.nestedCues = [nestedCue1, nestedCue2, nestedCue3, nestedCue4];
121+
verifyHelper(
122+
[
123+
{startTime: 10, endTime: 20, text: expectedText},
124+
],
125+
[shakaCue]);
126+
});
127+
128+
it('adds linebreaks when a linebreak cue is seen', () => {
129+
const shakaCue = new shaka.text.Cue(10, 20, '');
130+
const nestedCue1 = new shaka.text.Cue(10, 20, 'Test1');
131+
132+
// Second cue is a linebreak cue.
133+
const nestedCue2 = new shaka.text.Cue(10, 20, '');
134+
nestedCue2.spacer = true;
135+
136+
const nestedCue3 = new shaka.text.Cue(10, 20, 'Test2');
137+
138+
shakaCue.nestedCues = [nestedCue1, nestedCue2, nestedCue3];
139+
verifyHelper(
140+
[
141+
{startTime: 10, endTime: 20, text: 'Test1\nTest2'},
142+
],
143+
[shakaCue]);
144+
});
145+
98146
it('skips duplicate cues', () => {
99147
const cue1 = new shaka.text.Cue(10, 20, 'Test');
100148
displayer.append([cue1]);

0 commit comments

Comments
 (0)