Skip to content

Commit

Permalink
feat(CEA): Support alignment in CEA-608 (#7022)
Browse files Browse the repository at this point in the history
Closes #2940
  • Loading branch information
avelad authored Jul 12, 2024
1 parent 230f6e0 commit 11a2cc5
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 8 deletions.
48 changes: 40 additions & 8 deletions lib/cea/cea608_data_channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,19 +123,26 @@ shaka.cea.Cea608DataChannel = class {
controlPac_(b1, b2) {
const row = this.pacToRow_(b1, b2);

// Get attribute bits (4 bits)
const attr = (b2 & 0x1E) >> 1;

// Set up the defaults.
let textColor = shaka.cea.CeaUtils.DEFAULT_TXT_COLOR;
let italics = false;
let indent = null;

// Get PAC index;
let pacIndex;
if (b2 > 0x5f) {
pacIndex = b2 - 0x60;
} else {
pacIndex = b2 - 0x40;
}


// Attributes < 7 are colors, = 7 is white w/ italics, and >7 are indents
if (attr < 7) {
textColor = shaka.cea.Cea608DataChannel.TEXT_COLORS[attr];
} else if (attr === 7) {
if (pacIndex <= 0xd) {
const colorIndex = Math.floor(pacIndex / 2);
textColor = shaka.cea.Cea608DataChannel.TEXT_COLORS[colorIndex];
} else if (pacIndex <= 0xf) {
italics = true; // color stays white
} else {
indent = Math.floor((pacIndex - 0x10) / 2);
}

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

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

/**
* Handles a tab offset.
*
* @param {number} offset
* @private
*/
handleOffset_(offset) {
this.curbuf_.setOffset(offset);
}

/**
* Decodes control code.
* Three types of control codes:
Expand Down Expand Up @@ -543,6 +561,9 @@ shaka.cea.Cea608DataChannel = class {
this.handleExtendedWesternEuropeanChar_(b1, b2);
} else if (this.isMiscellaneous_(b1, b2)) {
return this.controlMiscellaneous_(ccPacket);
} else if (this.isOffset_(b1, b2)) {
const offset = b2 - 0x20;
this.handleOffset_(offset);
}
return null;
}
Expand All @@ -561,6 +582,17 @@ shaka.cea.Cea608DataChannel = class {
return ((b1 & 0xf6) === 0x14) && ((b2 & 0xf0) === 0x20);
}

/**
* Checks if this is a offset control code.
* @param {number} b1 Byte 1.
* @param {number} b2 Byte 2.
* @return {boolean}
* @private
*/
isOffset_(b1, b2) {
return (b1 == 0x17 || b1 == 0x1f) && b2 >= 0x21 && b2 <= 0x23;
}

/**
* Checks if this is a PAC control code.
* @param {number} b1 Byte 1.
Expand Down
28 changes: 28 additions & 0 deletions lib/cea/cea608_memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ shaka.cea.Cea608Memory = class {
*/
this.backgroundColor_ = shaka.cea.CeaUtils.DEFAULT_BG_COLOR;

/**
* @private {?number}
*/
this.offset_ = null;

/**
* @private {?number}
*/
this.indent_ = null;

this.reset();
}

Expand All @@ -89,6 +99,10 @@ shaka.cea.Cea608Memory = class {
if (line) {
topLevelCue.line = line;
}
if (this.indent_ != null && this.offset_ != null) {
topLevelCue.position = 10 + Math.min(70, this.indent_ * 10) +
this.offset_ * 2.5;
}
const ret = shaka.cea.CeaUtils.getParsedCaption(
topLevelCue, stream, this.rows_, startTime, endTime);
// If the text and its lines are larger than what we can show on the
Expand Down Expand Up @@ -270,6 +284,20 @@ shaka.cea.Cea608Memory = class {
setBackgroundColor(color) {
this.backgroundColor_ = color;
}

/**
* @param {number} offset
*/
setOffset(offset) {
this.offset_ = offset;
}

/**
* @param {?number} indent
*/
setIndent(indent) {
this.indent_ = indent;
}
};

/**
Expand Down

0 comments on commit 11a2cc5

Please sign in to comment.