forked from video-dev/hls.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cea-608-parser.ts
1463 lines (1319 loc) · 37.4 KB
/
cea-608-parser.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import OutputFilter from './output-filter';
import { logger } from '../utils/logger';
/**
*
* This code was ported from the dash.js project at:
* https://github.com/Dash-Industry-Forum/dash.js/blob/development/externals/cea608-parser.js
* https://github.com/Dash-Industry-Forum/dash.js/commit/8269b26a761e0853bb21d78780ed945144ecdd4d#diff-71bc295a2d6b6b7093a1d3290d53a4b2
*
* The original copyright appears below:
*
* The copyright in this software is being made available under the BSD License,
* included below. This software may be subject to other third party and contributor
* rights, including patent rights, and no such rights are granted under this license.
*
* Copyright (c) 2015-2016, DASH Industry Forum.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
* 2. Neither the name of Dash Industry Forum nor the names of its
* contributors may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/**
* Exceptions from regular ASCII. CodePoints are mapped to UTF-16 codes
*/
const specialCea608CharsCodes = {
0x2a: 0xe1, // lowercase a, acute accent
0x5c: 0xe9, // lowercase e, acute accent
0x5e: 0xed, // lowercase i, acute accent
0x5f: 0xf3, // lowercase o, acute accent
0x60: 0xfa, // lowercase u, acute accent
0x7b: 0xe7, // lowercase c with cedilla
0x7c: 0xf7, // division symbol
0x7d: 0xd1, // uppercase N tilde
0x7e: 0xf1, // lowercase n tilde
0x7f: 0x2588, // Full block
// THIS BLOCK INCLUDES THE 16 EXTENDED (TWO-BYTE) LINE 21 CHARACTERS
// THAT COME FROM HI BYTE=0x11 AND LOW BETWEEN 0x30 AND 0x3F
// THIS MEANS THAT \x50 MUST BE ADDED TO THE VALUES
0x80: 0xae, // Registered symbol (R)
0x81: 0xb0, // degree sign
0x82: 0xbd, // 1/2 symbol
0x83: 0xbf, // Inverted (open) question mark
0x84: 0x2122, // Trademark symbol (TM)
0x85: 0xa2, // Cents symbol
0x86: 0xa3, // Pounds sterling
0x87: 0x266a, // Music 8'th note
0x88: 0xe0, // lowercase a, grave accent
0x89: 0x20, // transparent space (regular)
0x8a: 0xe8, // lowercase e, grave accent
0x8b: 0xe2, // lowercase a, circumflex accent
0x8c: 0xea, // lowercase e, circumflex accent
0x8d: 0xee, // lowercase i, circumflex accent
0x8e: 0xf4, // lowercase o, circumflex accent
0x8f: 0xfb, // lowercase u, circumflex accent
// THIS BLOCK INCLUDES THE 32 EXTENDED (TWO-BYTE) LINE 21 CHARACTERS
// THAT COME FROM HI BYTE=0x12 AND LOW BETWEEN 0x20 AND 0x3F
0x90: 0xc1, // capital letter A with acute
0x91: 0xc9, // capital letter E with acute
0x92: 0xd3, // capital letter O with acute
0x93: 0xda, // capital letter U with acute
0x94: 0xdc, // capital letter U with diaresis
0x95: 0xfc, // lowercase letter U with diaeresis
0x96: 0x2018, // opening single quote
0x97: 0xa1, // inverted exclamation mark
0x98: 0x2a, // asterisk
0x99: 0x2019, // closing single quote
0x9a: 0x2501, // box drawings heavy horizontal
0x9b: 0xa9, // copyright sign
0x9c: 0x2120, // Service mark
0x9d: 0x2022, // (round) bullet
0x9e: 0x201c, // Left double quotation mark
0x9f: 0x201d, // Right double quotation mark
0xa0: 0xc0, // uppercase A, grave accent
0xa1: 0xc2, // uppercase A, circumflex
0xa2: 0xc7, // uppercase C with cedilla
0xa3: 0xc8, // uppercase E, grave accent
0xa4: 0xca, // uppercase E, circumflex
0xa5: 0xcb, // capital letter E with diaresis
0xa6: 0xeb, // lowercase letter e with diaresis
0xa7: 0xce, // uppercase I, circumflex
0xa8: 0xcf, // uppercase I, with diaresis
0xa9: 0xef, // lowercase i, with diaresis
0xaa: 0xd4, // uppercase O, circumflex
0xab: 0xd9, // uppercase U, grave accent
0xac: 0xf9, // lowercase u, grave accent
0xad: 0xdb, // uppercase U, circumflex
0xae: 0xab, // left-pointing double angle quotation mark
0xaf: 0xbb, // right-pointing double angle quotation mark
// THIS BLOCK INCLUDES THE 32 EXTENDED (TWO-BYTE) LINE 21 CHARACTERS
// THAT COME FROM HI BYTE=0x13 AND LOW BETWEEN 0x20 AND 0x3F
0xb0: 0xc3, // Uppercase A, tilde
0xb1: 0xe3, // Lowercase a, tilde
0xb2: 0xcd, // Uppercase I, acute accent
0xb3: 0xcc, // Uppercase I, grave accent
0xb4: 0xec, // Lowercase i, grave accent
0xb5: 0xd2, // Uppercase O, grave accent
0xb6: 0xf2, // Lowercase o, grave accent
0xb7: 0xd5, // Uppercase O, tilde
0xb8: 0xf5, // Lowercase o, tilde
0xb9: 0x7b, // Open curly brace
0xba: 0x7d, // Closing curly brace
0xbb: 0x5c, // Backslash
0xbc: 0x5e, // Caret
0xbd: 0x5f, // Underscore
0xbe: 0x7c, // Pipe (vertical line)
0xbf: 0x223c, // Tilde operator
0xc0: 0xc4, // Uppercase A, umlaut
0xc1: 0xe4, // Lowercase A, umlaut
0xc2: 0xd6, // Uppercase O, umlaut
0xc3: 0xf6, // Lowercase o, umlaut
0xc4: 0xdf, // Esszett (sharp S)
0xc5: 0xa5, // Yen symbol
0xc6: 0xa4, // Generic currency sign
0xc7: 0x2503, // Box drawings heavy vertical
0xc8: 0xc5, // Uppercase A, ring
0xc9: 0xe5, // Lowercase A, ring
0xca: 0xd8, // Uppercase O, stroke
0xcb: 0xf8, // Lowercase o, strok
0xcc: 0x250f, // Box drawings heavy down and right
0xcd: 0x2513, // Box drawings heavy down and left
0xce: 0x2517, // Box drawings heavy up and right
0xcf: 0x251b, // Box drawings heavy up and left
};
/**
* Utils
*/
const getCharForByte = function (byte: number) {
let charCode = byte;
if (specialCea608CharsCodes.hasOwnProperty(byte)) {
charCode = specialCea608CharsCodes[byte];
}
return String.fromCharCode(charCode);
};
const NR_ROWS = 15;
const NR_COLS = 100;
// Tables to look up row from PAC data
const rowsLowCh1 = {
0x11: 1,
0x12: 3,
0x15: 5,
0x16: 7,
0x17: 9,
0x10: 11,
0x13: 12,
0x14: 14,
};
const rowsHighCh1 = {
0x11: 2,
0x12: 4,
0x15: 6,
0x16: 8,
0x17: 10,
0x13: 13,
0x14: 15,
};
const rowsLowCh2 = {
0x19: 1,
0x1a: 3,
0x1d: 5,
0x1e: 7,
0x1f: 9,
0x18: 11,
0x1b: 12,
0x1c: 14,
};
const rowsHighCh2 = {
0x19: 2,
0x1a: 4,
0x1d: 6,
0x1e: 8,
0x1f: 10,
0x1b: 13,
0x1c: 15,
};
const backgroundColors = [
'white',
'green',
'blue',
'cyan',
'red',
'yellow',
'magenta',
'black',
'transparent',
];
enum VerboseLevel {
ERROR = 0,
TEXT = 1,
WARNING = 2,
INFO = 2,
DEBUG = 3,
DATA = 3,
}
class CaptionsLogger {
public time: number | null = null;
public verboseLevel: VerboseLevel = VerboseLevel.ERROR;
log(severity: VerboseLevel, msg: string): void {
if (this.verboseLevel >= severity) {
logger.log(`${this.time} [${severity}] ${msg}`);
}
}
}
const numArrayToHexArray = function (numArray: number[]): string[] {
const hexArray: string[] = [];
for (let j = 0; j < numArray.length; j++) {
hexArray.push(numArray[j].toString(16));
}
return hexArray;
};
type PenStyles = {
foreground: string | null;
underline: boolean;
italics: boolean;
background: string;
flash: boolean;
};
class PenState {
public foreground: string;
public underline: boolean;
public italics: boolean;
public background: string;
public flash: boolean;
constructor(
foreground?: string,
underline?: boolean,
italics?: boolean,
background?: string,
flash?: boolean
) {
this.foreground = foreground || 'white';
this.underline = underline || false;
this.italics = italics || false;
this.background = background || 'black';
this.flash = flash || false;
}
reset() {
this.foreground = 'white';
this.underline = false;
this.italics = false;
this.background = 'black';
this.flash = false;
}
setStyles(styles: Partial<PenStyles>) {
const attribs = [
'foreground',
'underline',
'italics',
'background',
'flash',
];
for (let i = 0; i < attribs.length; i++) {
const style = attribs[i];
if (styles.hasOwnProperty(style)) {
this[style] = styles[style];
}
}
}
isDefault() {
return (
this.foreground === 'white' &&
!this.underline &&
!this.italics &&
this.background === 'black' &&
!this.flash
);
}
equals(other: PenState) {
return (
this.foreground === other.foreground &&
this.underline === other.underline &&
this.italics === other.italics &&
this.background === other.background &&
this.flash === other.flash
);
}
copy(newPenState: PenState) {
this.foreground = newPenState.foreground;
this.underline = newPenState.underline;
this.italics = newPenState.italics;
this.background = newPenState.background;
this.flash = newPenState.flash;
}
toString(): string {
return (
'color=' +
this.foreground +
', underline=' +
this.underline +
', italics=' +
this.italics +
', background=' +
this.background +
', flash=' +
this.flash
);
}
}
/**
* Unicode character with styling and background.
* @constructor
*/
class StyledUnicodeChar {
uchar: string;
penState: PenState;
constructor(
uchar?: string,
foreground?: string,
underline?: boolean,
italics?: boolean,
background?: string,
flash?: boolean
) {
this.uchar = uchar || ' '; // unicode character
this.penState = new PenState(
foreground,
underline,
italics,
background,
flash
);
}
reset() {
this.uchar = ' ';
this.penState.reset();
}
setChar(uchar: string, newPenState: PenState) {
this.uchar = uchar;
this.penState.copy(newPenState);
}
setPenState(newPenState: PenState) {
this.penState.copy(newPenState);
}
equals(other: StyledUnicodeChar) {
return this.uchar === other.uchar && this.penState.equals(other.penState);
}
copy(newChar: StyledUnicodeChar) {
this.uchar = newChar.uchar;
this.penState.copy(newChar.penState);
}
isEmpty(): boolean {
return this.uchar === ' ' && this.penState.isDefault();
}
}
/**
* CEA-608 row consisting of NR_COLS instances of StyledUnicodeChar.
* @constructor
*/
export class Row {
public chars: StyledUnicodeChar[];
public pos: number;
public currPenState: PenState;
public cueStartTime?: number;
logger: CaptionsLogger;
constructor(logger: CaptionsLogger) {
this.chars = [];
for (let i = 0; i < NR_COLS; i++) {
this.chars.push(new StyledUnicodeChar());
}
this.logger = logger;
this.pos = 0;
this.currPenState = new PenState();
}
equals(other: Row) {
let equal = true;
for (let i = 0; i < NR_COLS; i++) {
if (!this.chars[i].equals(other.chars[i])) {
equal = false;
break;
}
}
return equal;
}
copy(other: Row) {
for (let i = 0; i < NR_COLS; i++) {
this.chars[i].copy(other.chars[i]);
}
}
isEmpty(): boolean {
let empty = true;
for (let i = 0; i < NR_COLS; i++) {
if (!this.chars[i].isEmpty()) {
empty = false;
break;
}
}
return empty;
}
/**
* Set the cursor to a valid column.
*/
setCursor(absPos: number) {
if (this.pos !== absPos) {
this.pos = absPos;
}
if (this.pos < 0) {
this.logger.log(
VerboseLevel.DEBUG,
'Negative cursor position ' + this.pos
);
this.pos = 0;
} else if (this.pos > NR_COLS) {
this.logger.log(
VerboseLevel.DEBUG,
'Too large cursor position ' + this.pos
);
this.pos = NR_COLS;
}
}
/**
* Move the cursor relative to current position.
*/
moveCursor(relPos: number) {
const newPos = this.pos + relPos;
if (relPos > 1) {
for (let i = this.pos + 1; i < newPos + 1; i++) {
this.chars[i].setPenState(this.currPenState);
}
}
this.setCursor(newPos);
}
/**
* Backspace, move one step back and clear character.
*/
backSpace() {
this.moveCursor(-1);
this.chars[this.pos].setChar(' ', this.currPenState);
}
insertChar(byte: number) {
if (byte >= 0x90) {
// Extended char
this.backSpace();
}
const char = getCharForByte(byte);
if (this.pos >= NR_COLS) {
this.logger.log(
VerboseLevel.ERROR,
'Cannot insert ' +
byte.toString(16) +
' (' +
char +
') at position ' +
this.pos +
'. Skipping it!'
);
return;
}
this.chars[this.pos].setChar(char, this.currPenState);
this.moveCursor(1);
}
clearFromPos(startPos: number) {
let i: number;
for (i = startPos; i < NR_COLS; i++) {
this.chars[i].reset();
}
}
clear() {
this.clearFromPos(0);
this.pos = 0;
this.currPenState.reset();
}
clearToEndOfRow() {
this.clearFromPos(this.pos);
}
getTextString() {
const chars: string[] = [];
let empty = true;
for (let i = 0; i < NR_COLS; i++) {
const char = this.chars[i].uchar;
if (char !== ' ') {
empty = false;
}
chars.push(char);
}
if (empty) {
return '';
} else {
return chars.join('');
}
}
setPenStyles(styles: Partial<PenStyles>) {
this.currPenState.setStyles(styles);
const currChar = this.chars[this.pos];
currChar.setPenState(this.currPenState);
}
}
/**
* Keep a CEA-608 screen of 32x15 styled characters
* @constructor
*/
export class CaptionScreen {
rows: Row[];
currRow: number;
nrRollUpRows: number | null;
lastOutputScreen: CaptionScreen | null;
logger: CaptionsLogger;
constructor(logger: CaptionsLogger) {
this.rows = [];
for (let i = 0; i < NR_ROWS; i++) {
this.rows.push(new Row(logger));
} // Note that we use zero-based numbering (0-14)
this.logger = logger;
this.currRow = NR_ROWS - 1;
this.nrRollUpRows = null;
this.lastOutputScreen = null;
this.reset();
}
reset() {
for (let i = 0; i < NR_ROWS; i++) {
this.rows[i].clear();
}
this.currRow = NR_ROWS - 1;
}
equals(other: CaptionScreen): boolean {
let equal = true;
for (let i = 0; i < NR_ROWS; i++) {
if (!this.rows[i].equals(other.rows[i])) {
equal = false;
break;
}
}
return equal;
}
copy(other: CaptionScreen) {
for (let i = 0; i < NR_ROWS; i++) {
this.rows[i].copy(other.rows[i]);
}
}
isEmpty(): boolean {
let empty = true;
for (let i = 0; i < NR_ROWS; i++) {
if (!this.rows[i].isEmpty()) {
empty = false;
break;
}
}
return empty;
}
backSpace() {
const row = this.rows[this.currRow];
row.backSpace();
}
clearToEndOfRow() {
const row = this.rows[this.currRow];
row.clearToEndOfRow();
}
/**
* Insert a character (without styling) in the current row.
*/
insertChar(char: number) {
const row = this.rows[this.currRow];
row.insertChar(char);
}
setPen(styles: Partial<PenStyles>) {
const row = this.rows[this.currRow];
row.setPenStyles(styles);
}
moveCursor(relPos: number) {
const row = this.rows[this.currRow];
row.moveCursor(relPos);
}
setCursor(absPos: number) {
this.logger.log(VerboseLevel.INFO, 'setCursor: ' + absPos);
const row = this.rows[this.currRow];
row.setCursor(absPos);
}
setPAC(pacData: PACData) {
this.logger.log(VerboseLevel.INFO, 'pacData = ' + JSON.stringify(pacData));
let newRow = pacData.row - 1;
if (this.nrRollUpRows && newRow < this.nrRollUpRows - 1) {
newRow = this.nrRollUpRows - 1;
}
// Make sure this only affects Roll-up Captions by checking this.nrRollUpRows
if (this.nrRollUpRows && this.currRow !== newRow) {
// clear all rows first
for (let i = 0; i < NR_ROWS; i++) {
this.rows[i].clear();
}
// Copy this.nrRollUpRows rows from lastOutputScreen and place it in the newRow location
// topRowIndex - the start of rows to copy (inclusive index)
const topRowIndex = this.currRow + 1 - this.nrRollUpRows;
// We only copy if the last position was already shown.
// We use the cueStartTime value to check this.
const lastOutputScreen = this.lastOutputScreen;
if (lastOutputScreen) {
const prevLineTime = lastOutputScreen.rows[topRowIndex].cueStartTime;
const time = this.logger.time;
if (prevLineTime && time !== null && prevLineTime < time) {
for (let i = 0; i < this.nrRollUpRows; i++) {
this.rows[newRow - this.nrRollUpRows + i + 1].copy(
lastOutputScreen.rows[topRowIndex + i]
);
}
}
}
}
this.currRow = newRow;
const row = this.rows[this.currRow];
if (pacData.indent !== null) {
const indent = pacData.indent;
const prevPos = Math.max(indent - 1, 0);
row.setCursor(pacData.indent);
pacData.color = row.chars[prevPos].penState.foreground;
}
const styles: PenStyles = {
foreground: pacData.color,
underline: pacData.underline,
italics: pacData.italics,
background: 'black',
flash: false,
};
this.setPen(styles);
}
/**
* Set background/extra foreground, but first do back_space, and then insert space (backwards compatibility).
*/
setBkgData(bkgData: Partial<PenStyles>) {
this.logger.log(VerboseLevel.INFO, 'bkgData = ' + JSON.stringify(bkgData));
this.backSpace();
this.setPen(bkgData);
this.insertChar(0x20); // Space
}
setRollUpRows(nrRows: number | null) {
this.nrRollUpRows = nrRows;
}
rollUp() {
if (this.nrRollUpRows === null) {
this.logger.log(
VerboseLevel.DEBUG,
'roll_up but nrRollUpRows not set yet'
);
return; // Not properly setup
}
this.logger.log(VerboseLevel.TEXT, this.getDisplayText());
const topRowIndex = this.currRow + 1 - this.nrRollUpRows;
const topRow = this.rows.splice(topRowIndex, 1)[0];
topRow.clear();
this.rows.splice(this.currRow, 0, topRow);
this.logger.log(VerboseLevel.INFO, 'Rolling up');
// this.logger.log(VerboseLevel.TEXT, this.get_display_text())
}
/**
* Get all non-empty rows with as unicode text.
*/
getDisplayText(asOneRow?: boolean) {
asOneRow = asOneRow || false;
const displayText: string[] = [];
let text = '';
let rowNr = -1;
for (let i = 0; i < NR_ROWS; i++) {
const rowText = this.rows[i].getTextString();
if (rowText) {
rowNr = i + 1;
if (asOneRow) {
displayText.push('Row ' + rowNr + ": '" + rowText + "'");
} else {
displayText.push(rowText.trim());
}
}
}
if (displayText.length > 0) {
if (asOneRow) {
text = '[' + displayText.join(' | ') + ']';
} else {
text = displayText.join('\n');
}
}
return text;
}
getTextAndFormat() {
return this.rows;
}
}
// var modes = ['MODE_ROLL-UP', 'MODE_POP-ON', 'MODE_PAINT-ON', 'MODE_TEXT'];
type CaptionModes =
| 'MODE_ROLL-UP'
| 'MODE_POP-ON'
| 'MODE_PAINT-ON'
| 'MODE_TEXT'
| null;
class Cea608Channel {
chNr: number;
outputFilter: OutputFilter;
mode: CaptionModes;
verbose: number;
displayedMemory: CaptionScreen;
nonDisplayedMemory: CaptionScreen;
lastOutputScreen: CaptionScreen;
currRollUpRow: Row;
writeScreen: CaptionScreen;
cueStartTime: number | null;
logger: CaptionsLogger;
constructor(
channelNumber: number,
outputFilter: OutputFilter,
logger: CaptionsLogger
) {
this.chNr = channelNumber;
this.outputFilter = outputFilter;
this.mode = null;
this.verbose = 0;
this.displayedMemory = new CaptionScreen(logger);
this.nonDisplayedMemory = new CaptionScreen(logger);
this.lastOutputScreen = new CaptionScreen(logger);
this.currRollUpRow = this.displayedMemory.rows[NR_ROWS - 1];
this.writeScreen = this.displayedMemory;
this.mode = null;
this.cueStartTime = null; // Keeps track of where a cue started.
this.logger = logger;
}
reset() {
this.mode = null;
this.displayedMemory.reset();
this.nonDisplayedMemory.reset();
this.lastOutputScreen.reset();
this.outputFilter.reset();
this.currRollUpRow = this.displayedMemory.rows[NR_ROWS - 1];
this.writeScreen = this.displayedMemory;
this.mode = null;
this.cueStartTime = null;
}
getHandler(): OutputFilter {
return this.outputFilter;
}
setHandler(newHandler: OutputFilter) {
this.outputFilter = newHandler;
}
setPAC(pacData: PACData) {
this.writeScreen.setPAC(pacData);
}
setBkgData(bkgData: Partial<PenStyles>) {
this.writeScreen.setBkgData(bkgData);
}
setMode(newMode: CaptionModes) {
if (newMode === this.mode) {
return;
}
this.mode = newMode;
this.logger.log(VerboseLevel.INFO, 'MODE=' + newMode);
if (this.mode === 'MODE_POP-ON') {
this.writeScreen = this.nonDisplayedMemory;
} else {
this.writeScreen = this.displayedMemory;
this.writeScreen.reset();
}
if (this.mode !== 'MODE_ROLL-UP') {
this.displayedMemory.nrRollUpRows = null;
this.nonDisplayedMemory.nrRollUpRows = null;
}
this.mode = newMode;
}
insertChars(chars: number[]) {
for (let i = 0; i < chars.length; i++) {
this.writeScreen.insertChar(chars[i]);
}
const screen =
this.writeScreen === this.displayedMemory ? 'DISP' : 'NON_DISP';
this.logger.log(
VerboseLevel.INFO,
screen + ': ' + this.writeScreen.getDisplayText(true)
);
if (this.mode === 'MODE_PAINT-ON' || this.mode === 'MODE_ROLL-UP') {
this.logger.log(
VerboseLevel.TEXT,
'DISPLAYED: ' + this.displayedMemory.getDisplayText(true)
);
this.outputDataUpdate();
}
}
ccRCL() {
// Resume Caption Loading (switch mode to Pop On)
this.logger.log(VerboseLevel.INFO, 'RCL - Resume Caption Loading');
this.setMode('MODE_POP-ON');
}
ccBS() {
// BackSpace
this.logger.log(VerboseLevel.INFO, 'BS - BackSpace');
if (this.mode === 'MODE_TEXT') {
return;
}
this.writeScreen.backSpace();
if (this.writeScreen === this.displayedMemory) {
this.outputDataUpdate();
}
}
ccAOF() {
// Reserved (formerly Alarm Off)
}
ccAON() {
// Reserved (formerly Alarm On)
}
ccDER() {
// Delete to End of Row
this.logger.log(VerboseLevel.INFO, 'DER- Delete to End of Row');
this.writeScreen.clearToEndOfRow();
this.outputDataUpdate();
}
ccRU(nrRows: number | null) {
// Roll-Up Captions-2,3,or 4 Rows
this.logger.log(VerboseLevel.INFO, 'RU(' + nrRows + ') - Roll Up');
this.writeScreen = this.displayedMemory;
this.setMode('MODE_ROLL-UP');
this.writeScreen.setRollUpRows(nrRows);
}
ccFON() {
// Flash On
this.logger.log(VerboseLevel.INFO, 'FON - Flash On');
this.writeScreen.setPen({ flash: true });
}
ccRDC() {
// Resume Direct Captioning (switch mode to PaintOn)
this.logger.log(VerboseLevel.INFO, 'RDC - Resume Direct Captioning');
this.setMode('MODE_PAINT-ON');
}
ccTR() {
// Text Restart in text mode (not supported, however)
this.logger.log(VerboseLevel.INFO, 'TR');
this.setMode('MODE_TEXT');
}
ccRTD() {
// Resume Text Display in Text mode (not supported, however)
this.logger.log(VerboseLevel.INFO, 'RTD');
this.setMode('MODE_TEXT');
}
ccEDM() {
// Erase Displayed Memory
this.logger.log(VerboseLevel.INFO, 'EDM - Erase Displayed Memory');
this.displayedMemory.reset();
this.outputDataUpdate(true);
}
ccCR() {
// Carriage Return
this.logger.log(VerboseLevel.INFO, 'CR - Carriage Return');
this.writeScreen.rollUp();
this.outputDataUpdate(true);
}
ccENM() {
// Erase Non-Displayed Memory
this.logger.log(VerboseLevel.INFO, 'ENM - Erase Non-displayed Memory');
this.nonDisplayedMemory.reset();
}
ccEOC() {
// End of Caption (Flip Memories)
this.logger.log(VerboseLevel.INFO, 'EOC - End Of Caption');
if (this.mode === 'MODE_POP-ON') {
const tmp = this.displayedMemory;
this.displayedMemory = this.nonDisplayedMemory;
this.nonDisplayedMemory = tmp;
this.writeScreen = this.nonDisplayedMemory;
this.logger.log(
VerboseLevel.TEXT,
'DISP: ' + this.displayedMemory.getDisplayText()
);
}
this.outputDataUpdate(true);
}
ccTO(nrCols: number) {
// Tab Offset 1,2, or 3 columns
this.logger.log(VerboseLevel.INFO, 'TO(' + nrCols + ') - Tab Offset');
this.writeScreen.moveCursor(nrCols);
}
ccMIDROW(secondByte: number) {
// Parse MIDROW command
const styles: Partial<PenStyles> = { flash: false };
styles.underline = secondByte % 2 === 1;
styles.italics = secondByte >= 0x2e;
if (!styles.italics) {
const colorIndex = Math.floor(secondByte / 2) - 0x10;
const colors = [
'white',
'green',
'blue',
'cyan',
'red',
'yellow',
'magenta',
];
styles.foreground = colors[colorIndex];
} else {
styles.foreground = 'white';
}
this.logger.log(VerboseLevel.INFO, 'MIDROW: ' + JSON.stringify(styles));
this.writeScreen.setPen(styles);
}