Skip to content

Commit

Permalink
fix(CEA): Fix multi byte language support in CEA-708 (#7929)
Browse files Browse the repository at this point in the history
Fixes #7926
  • Loading branch information
avelad committed Jan 23, 2025
1 parent 1c3b66a commit 0f84e02
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 19 deletions.
26 changes: 9 additions & 17 deletions lib/cea/cea708_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,24 +169,16 @@ shaka.cea.Cea708Service = class {
const firstByte = dtvccPacket.readByte().value;
const secondByte = dtvccPacket.readByte().value;

const isTextBlock = (b) => {
return (b >= 0x20 && b <= 0x7f) || (b >= 0xa0 && b <= 0xff);
const toHexString = (byteArray) => {
return byteArray.map((byte) => {
return ('0' + (byte & 0xFF).toString(16)).slice(-2);
}).join('');
};

if (isTextBlock(firstByte) && isTextBlock(secondByte)) {
const toHexString = (byteArray) => {
return byteArray.map((byte) => {
return ('0' + (byte & 0xFF).toString(16)).slice(-2);
}).join('');
};
const unicode = toHexString([firstByte, secondByte]);
// Takes a unicode hex string and creates a single character.
const char = String.fromCharCode(parseInt(unicode, 16));
this.currentWindow_.setCharacter(char);
return null;
} else {
dtvccPacket.rewind(2);
}
const unicode = toHexString([firstByte, secondByte]);
// Takes a unicode hex string and creates a single character.
const char = String.fromCharCode(parseInt(unicode, 16));
this.currentWindow_.setCharacter(char);
return null;
}

const window = this.currentWindow_;
Expand Down
31 changes: 29 additions & 2 deletions test/cea/cea708_service_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,7 @@ describe('Cea708Service', () => {
expect(captions).toEqual(expectedCaptions);
});


it('decodes multibyte unstyled caption text', () => {
it('decodes multibyte unstyled caption text (Korean)', () => {
const controlCodes = [
...defineWindow,
// Series of C0 control codes that add multi-byte text.
Expand Down Expand Up @@ -150,6 +149,34 @@ describe('Cea708Service', () => {
expect(captions).toEqual(expectedCaptions);
});

it('decodes multibyte unstyled caption text (Polish)', () => {
const controlCodes = [
...defineWindow,
// Series of C0 control codes that add multi-byte text.
0x18, 0x01, 0x7c, 0xF3, 0x18, 0x01, 0x42, 0x18, 0x01, 0x07, // ż, ó, ł, ć
];

const packet1 = createCea708PacketFromBytes(controlCodes, startTime);
const packet2 = createCea708PacketFromBytes(hideWindow, endTime);

const text = 'żółć'; // cspell:ignore żółć
const topLevelCue = CeaUtils.createWindowedCue(startTime, endTime, '',
serviceNumber, windowId, rowCount, colCount, anchorId);
topLevelCue.nestedCues = [
CeaUtils.createDefaultCue(startTime, endTime, /* payload= */ text),
];

const expectedCaptions = [
{
stream,
cue: topLevelCue,
},
];

const captions = getCaptionsFromPackets(service, packet1, packet2);
expect(captions).toEqual(expectedCaptions);
});

it('setPenLocation sets the pen location correctly', () => {
const controlCodes = [
...defineWindow,
Expand Down

0 comments on commit 0f84e02

Please sign in to comment.