Skip to content

Commit 11a2cc5

Browse files
authored
feat(CEA): Support alignment in CEA-608 (#7022)
Closes #2940
1 parent 230f6e0 commit 11a2cc5

File tree

2 files changed

+68
-8
lines changed

2 files changed

+68
-8
lines changed

lib/cea/cea608_data_channel.js

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,19 +123,26 @@ shaka.cea.Cea608DataChannel = class {
123123
controlPac_(b1, b2) {
124124
const row = this.pacToRow_(b1, b2);
125125

126-
// Get attribute bits (4 bits)
127-
const attr = (b2 & 0x1E) >> 1;
128-
129126
// Set up the defaults.
130127
let textColor = shaka.cea.CeaUtils.DEFAULT_TXT_COLOR;
131128
let italics = false;
129+
let indent = null;
130+
131+
// Get PAC index;
132+
let pacIndex;
133+
if (b2 > 0x5f) {
134+
pacIndex = b2 - 0x60;
135+
} else {
136+
pacIndex = b2 - 0x40;
137+
}
132138

133-
134-
// Attributes < 7 are colors, = 7 is white w/ italics, and >7 are indents
135-
if (attr < 7) {
136-
textColor = shaka.cea.Cea608DataChannel.TEXT_COLORS[attr];
137-
} else if (attr === 7) {
139+
if (pacIndex <= 0xd) {
140+
const colorIndex = Math.floor(pacIndex / 2);
141+
textColor = shaka.cea.Cea608DataChannel.TEXT_COLORS[colorIndex];
142+
} else if (pacIndex <= 0xf) {
138143
italics = true; // color stays white
144+
} else {
145+
indent = Math.floor((pacIndex - 0x10) / 2);
139146
}
140147

141148
// PACs toggle underline on the last bit of b2.
@@ -168,6 +175,7 @@ shaka.cea.Cea608DataChannel = class {
168175
buf.setUnderline(underline);
169176
buf.setItalics(italics);
170177
buf.setTextColor(textColor);
178+
buf.setIndent(indent);
171179

172180
// Clear the background color, since new row (PAC) should reset ALL styles.
173181
buf.setBackgroundColor(shaka.cea.CeaUtils.DEFAULT_BG_COLOR);
@@ -507,6 +515,16 @@ shaka.cea.Cea608DataChannel = class {
507515
this.curbuf_.addChar(charSet, b2);
508516
}
509517

518+
/**
519+
* Handles a tab offset.
520+
*
521+
* @param {number} offset
522+
* @private
523+
*/
524+
handleOffset_(offset) {
525+
this.curbuf_.setOffset(offset);
526+
}
527+
510528
/**
511529
* Decodes control code.
512530
* Three types of control codes:
@@ -543,6 +561,9 @@ shaka.cea.Cea608DataChannel = class {
543561
this.handleExtendedWesternEuropeanChar_(b1, b2);
544562
} else if (this.isMiscellaneous_(b1, b2)) {
545563
return this.controlMiscellaneous_(ccPacket);
564+
} else if (this.isOffset_(b1, b2)) {
565+
const offset = b2 - 0x20;
566+
this.handleOffset_(offset);
546567
}
547568
return null;
548569
}
@@ -561,6 +582,17 @@ shaka.cea.Cea608DataChannel = class {
561582
return ((b1 & 0xf6) === 0x14) && ((b2 & 0xf0) === 0x20);
562583
}
563584

585+
/**
586+
* Checks if this is a offset control code.
587+
* @param {number} b1 Byte 1.
588+
* @param {number} b2 Byte 2.
589+
* @return {boolean}
590+
* @private
591+
*/
592+
isOffset_(b1, b2) {
593+
return (b1 == 0x17 || b1 == 0x1f) && b2 >= 0x21 && b2 <= 0x23;
594+
}
595+
564596
/**
565597
* Checks if this is a PAC control code.
566598
* @param {number} b1 Byte 1.

lib/cea/cea608_memory.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,16 @@ shaka.cea.Cea608Memory = class {
6969
*/
7070
this.backgroundColor_ = shaka.cea.CeaUtils.DEFAULT_BG_COLOR;
7171

72+
/**
73+
* @private {?number}
74+
*/
75+
this.offset_ = null;
76+
77+
/**
78+
* @private {?number}
79+
*/
80+
this.indent_ = null;
81+
7282
this.reset();
7383
}
7484

@@ -89,6 +99,10 @@ shaka.cea.Cea608Memory = class {
8999
if (line) {
90100
topLevelCue.line = line;
91101
}
102+
if (this.indent_ != null && this.offset_ != null) {
103+
topLevelCue.position = 10 + Math.min(70, this.indent_ * 10) +
104+
this.offset_ * 2.5;
105+
}
92106
const ret = shaka.cea.CeaUtils.getParsedCaption(
93107
topLevelCue, stream, this.rows_, startTime, endTime);
94108
// If the text and its lines are larger than what we can show on the
@@ -270,6 +284,20 @@ shaka.cea.Cea608Memory = class {
270284
setBackgroundColor(color) {
271285
this.backgroundColor_ = color;
272286
}
287+
288+
/**
289+
* @param {number} offset
290+
*/
291+
setOffset(offset) {
292+
this.offset_ = offset;
293+
}
294+
295+
/**
296+
* @param {?number} indent
297+
*/
298+
setIndent(indent) {
299+
this.indent_ = indent;
300+
}
273301
};
274302

275303
/**

0 commit comments

Comments
 (0)