Tx3gDecoder extends SimpleSubtitleDecoder { @Override protected Subtitle decode(byte[] bytes, int length, boolean reset) throws SubtitleDecoderException { parsableByteArray.reset(bytes, length); String cueTextString = readSubtitleText(parsableByteArray); if (cueTextString.isEmpty()) { return Tx3gSubtitle.EMPTY; } SpannableStringBuilder cueText = new SpannableStringBuilder(cueTextString); attachFontFace(cueText, defaultFontFace, DEFAULT_FONT_FACE, 0, cueText.length(), SPAN_PRIORITY_LOW); attachColor(cueText, defaultColorRgba, DEFAULT_COLOR, 0, cueText.length(), SPAN_PRIORITY_LOW); attachFontFamily(cueText, defaultFontFamily, DEFAULT_FONT_FAMILY, 0, cueText.length(), SPAN_PRIORITY_LOW); float verticalPlacement = defaultVerticalPlacement; while (parsableByteArray.bytesLeft() >= SIZE_ATOM_HEADER) { int position = parsableByteArray.getPosition(); int atomSize = parsableByteArray.readInt(); int atomType = parsableByteArray.readInt(); if (atomType == TYPE_STYL) { assertTrue(parsableByteArray.bytesLeft() >= SIZE_SHORT); int styleRecordCount = parsableByteArray.readUnsignedShort(); for (int i = 0; i < styleRecordCount; i++) { applyStyleRecord(parsableByteArray, cueText); } } else if (atomType == TYPE_TBOX && customVerticalPlacement) { assertTrue(parsableByteArray.bytesLeft() >= SIZE_SHORT); int requestedVerticalPlacement = parsableByteArray.readUnsignedShort(); verticalPlacement = (float) requestedVerticalPlacement / calculatedVideoTrackHeight; verticalPlacement = Util.constrainValue(verticalPlacement, 0.0f, 0.95f); } parsableByteArray.setPosition(position + atomSize); } return new Tx3gSubtitle( new Cue( cueText, null, verticalPlacement, Cue.LINE_TYPE_FRACTION, Cue.ANCHOR_TYPE_START, Cue.DIMEN_UNSET, Cue.TYPE_UNSET, Cue.DIMEN_UNSET)); } Tx3gDecoder(List<byte[]> initializationData);   }
@Test public void testDecodeNoSubtitle() throws IOException, SubtitleDecoderException { Tx3gDecoder decoder = new Tx3gDecoder(Collections.emptyList()); byte[] bytes = TestUtil.getByteArray(ApplicationProvider.getApplicationContext(), NO_SUBTITLE); Subtitle subtitle = decoder.decode(bytes, bytes.length, false); assertThat(subtitle.getCues(0)).isEmpty(); }
@Test public void testDecodeJustText() throws IOException, SubtitleDecoderException { Tx3gDecoder decoder = new Tx3gDecoder(Collections.emptyList()); byte[] bytes = TestUtil.getByteArray(ApplicationProvider.getApplicationContext(), SAMPLE_JUST_TEXT); Subtitle subtitle = decoder.decode(bytes, bytes.length, false); SpannedString text = new SpannedString(subtitle.getCues(0).get(0).text); assertThat(text.toString()).isEqualTo("CC Test"); assertThat(text.getSpans(0, text.length(), Object.class)).hasLength(0); assertFractionalLinePosition(subtitle.getCues(0).get(0), 0.85f); }
@Test public void testDecodeWithStyl() throws IOException, SubtitleDecoderException { Tx3gDecoder decoder = new Tx3gDecoder(Collections.emptyList()); byte[] bytes = TestUtil.getByteArray(ApplicationProvider.getApplicationContext(), SAMPLE_WITH_STYL); Subtitle subtitle = decoder.decode(bytes, bytes.length, false); SpannedString text = new SpannedString(subtitle.getCues(0).get(0).text); assertThat(text.toString()).isEqualTo("CC Test"); assertThat(text.getSpans(0, text.length(), Object.class)).hasLength(3); StyleSpan styleSpan = findSpan(text, 0, 6, StyleSpan.class); assertThat(styleSpan.getStyle()).isEqualTo(Typeface.BOLD_ITALIC); findSpan(text, 0, 6, UnderlineSpan.class); ForegroundColorSpan colorSpan = findSpan(text, 0, 6, ForegroundColorSpan.class); assertThat(colorSpan.getForegroundColor()).isEqualTo(Color.GREEN); assertFractionalLinePosition(subtitle.getCues(0).get(0), 0.85f); }
@Test public void testDecodeWithStylAllDefaults() throws IOException, SubtitleDecoderException { Tx3gDecoder decoder = new Tx3gDecoder(Collections.emptyList()); byte[] bytes = TestUtil.getByteArray( ApplicationProvider.getApplicationContext(), SAMPLE_WITH_STYL_ALL_DEFAULTS); Subtitle subtitle = decoder.decode(bytes, bytes.length, false); SpannedString text = new SpannedString(subtitle.getCues(0).get(0).text); assertThat(text.toString()).isEqualTo("CC Test"); assertThat(text.getSpans(0, text.length(), Object.class)).hasLength(0); assertFractionalLinePosition(subtitle.getCues(0).get(0), 0.85f); }
@Test public void testDecodeUtf16BeNoStyl() throws IOException, SubtitleDecoderException { Tx3gDecoder decoder = new Tx3gDecoder(Collections.emptyList()); byte[] bytes = TestUtil.getByteArray(ApplicationProvider.getApplicationContext(), SAMPLE_UTF16_BE_NO_STYL); Subtitle subtitle = decoder.decode(bytes, bytes.length, false); SpannedString text = new SpannedString(subtitle.getCues(0).get(0).text); assertThat(text.toString()).isEqualTo("你好"); assertThat(text.getSpans(0, text.length(), Object.class)).hasLength(0); assertFractionalLinePosition(subtitle.getCues(0).get(0), 0.85f); }
@Test public void testDecodeUtf16LeNoStyl() throws IOException, SubtitleDecoderException { Tx3gDecoder decoder = new Tx3gDecoder(Collections.emptyList()); byte[] bytes = TestUtil.getByteArray(ApplicationProvider.getApplicationContext(), SAMPLE_UTF16_LE_NO_STYL); Subtitle subtitle = decoder.decode(bytes, bytes.length, false); SpannedString text = new SpannedString(subtitle.getCues(0).get(0).text); assertThat(text.toString()).isEqualTo("你好"); assertThat(text.getSpans(0, text.length(), Object.class)).hasLength(0); assertFractionalLinePosition(subtitle.getCues(0).get(0), 0.85f); }
@Test public void testDecodeWithMultipleStyl() throws IOException, SubtitleDecoderException { Tx3gDecoder decoder = new Tx3gDecoder(Collections.emptyList()); byte[] bytes = TestUtil.getByteArray( ApplicationProvider.getApplicationContext(), SAMPLE_WITH_MULTIPLE_STYL); Subtitle subtitle = decoder.decode(bytes, bytes.length, false); SpannedString text = new SpannedString(subtitle.getCues(0).get(0).text); assertThat(text.toString()).isEqualTo("Line 2\nLine 3"); assertThat(text.getSpans(0, text.length(), Object.class)).hasLength(4); StyleSpan styleSpan = findSpan(text, 0, 5, StyleSpan.class); assertThat(styleSpan.getStyle()).isEqualTo(Typeface.ITALIC); findSpan(text, 7, 12, UnderlineSpan.class); ForegroundColorSpan colorSpan = findSpan(text, 0, 5, ForegroundColorSpan.class); assertThat(colorSpan.getForegroundColor()).isEqualTo(Color.GREEN); colorSpan = findSpan(text, 7, 12, ForegroundColorSpan.class); assertThat(colorSpan.getForegroundColor()).isEqualTo(Color.GREEN); assertFractionalLinePosition(subtitle.getCues(0).get(0), 0.85f); }
@Test public void testDecodeWithOtherExtension() throws IOException, SubtitleDecoderException { Tx3gDecoder decoder = new Tx3gDecoder(Collections.emptyList()); byte[] bytes = TestUtil.getByteArray( ApplicationProvider.getApplicationContext(), SAMPLE_WITH_OTHER_EXTENSION); Subtitle subtitle = decoder.decode(bytes, bytes.length, false); SpannedString text = new SpannedString(subtitle.getCues(0).get(0).text); assertThat(text.toString()).isEqualTo("CC Test"); assertThat(text.getSpans(0, text.length(), Object.class)).hasLength(2); StyleSpan styleSpan = findSpan(text, 0, 6, StyleSpan.class); assertThat(styleSpan.getStyle()).isEqualTo(Typeface.BOLD); ForegroundColorSpan colorSpan = findSpan(text, 0, 6, ForegroundColorSpan.class); assertThat(colorSpan.getForegroundColor()).isEqualTo(Color.GREEN); assertFractionalLinePosition(subtitle.getCues(0).get(0), 0.85f); }
@Test public void testInitializationDecodeWithStyl() throws IOException, SubtitleDecoderException { byte[] initBytes = TestUtil.getByteArray(ApplicationProvider.getApplicationContext(), INITIALIZATION); Tx3gDecoder decoder = new Tx3gDecoder(Collections.singletonList(initBytes)); byte[] bytes = TestUtil.getByteArray(ApplicationProvider.getApplicationContext(), SAMPLE_WITH_STYL); Subtitle subtitle = decoder.decode(bytes, bytes.length, false); SpannedString text = new SpannedString(subtitle.getCues(0).get(0).text); assertThat(text.toString()).isEqualTo("CC Test"); assertThat(text.getSpans(0, text.length(), Object.class)).hasLength(5); StyleSpan styleSpan = findSpan(text, 0, text.length(), StyleSpan.class); assertThat(styleSpan.getStyle()).isEqualTo(Typeface.BOLD_ITALIC); findSpan(text, 0, text.length(), UnderlineSpan.class); TypefaceSpan typefaceSpan = findSpan(text, 0, text.length(), TypefaceSpan.class); assertThat(typefaceSpan.getFamily()).isEqualTo(C.SERIF_NAME); ForegroundColorSpan colorSpan = findSpan(text, 0, text.length(), ForegroundColorSpan.class); assertThat(colorSpan.getForegroundColor()).isEqualTo(Color.RED); colorSpan = findSpan(text, 0, 6, ForegroundColorSpan.class); assertThat(colorSpan.getForegroundColor()).isEqualTo(Color.GREEN); assertFractionalLinePosition(subtitle.getCues(0).get(0), 0.1f); }
@Test public void testInitializationDecodeWithTbox() throws IOException, SubtitleDecoderException { byte[] initBytes = TestUtil.getByteArray(ApplicationProvider.getApplicationContext(), INITIALIZATION); Tx3gDecoder decoder = new Tx3gDecoder(Collections.singletonList(initBytes)); byte[] bytes = TestUtil.getByteArray(ApplicationProvider.getApplicationContext(), SAMPLE_WITH_TBOX); Subtitle subtitle = decoder.decode(bytes, bytes.length, false); SpannedString text = new SpannedString(subtitle.getCues(0).get(0).text); assertThat(text.toString()).isEqualTo("CC Test"); assertThat(text.getSpans(0, text.length(), Object.class)).hasLength(4); StyleSpan styleSpan = findSpan(text, 0, text.length(), StyleSpan.class); assertThat(styleSpan.getStyle()).isEqualTo(Typeface.BOLD_ITALIC); findSpan(text, 0, text.length(), UnderlineSpan.class); TypefaceSpan typefaceSpan = findSpan(text, 0, text.length(), TypefaceSpan.class); assertThat(typefaceSpan.getFamily()).isEqualTo(C.SERIF_NAME); ForegroundColorSpan colorSpan = findSpan(text, 0, text.length(), ForegroundColorSpan.class); assertThat(colorSpan.getForegroundColor()).isEqualTo(Color.RED); assertFractionalLinePosition(subtitle.getCues(0).get(0), 0.1875f); }
@Test public void testInitializationAllDefaultsDecodeWithStyl() throws IOException, SubtitleDecoderException { byte[] initBytes = TestUtil.getByteArray( ApplicationProvider.getApplicationContext(), INITIALIZATION_ALL_DEFAULTS); Tx3gDecoder decoder = new Tx3gDecoder(Collections.singletonList(initBytes)); byte[] bytes = TestUtil.getByteArray(ApplicationProvider.getApplicationContext(), SAMPLE_WITH_STYL); Subtitle subtitle = decoder.decode(bytes, bytes.length, false); SpannedString text = new SpannedString(subtitle.getCues(0).get(0).text); assertThat(text.toString()).isEqualTo("CC Test"); assertThat(text.getSpans(0, text.length(), Object.class)).hasLength(3); StyleSpan styleSpan = findSpan(text, 0, 6, StyleSpan.class); assertThat(styleSpan.getStyle()).isEqualTo(Typeface.BOLD_ITALIC); findSpan(text, 0, 6, UnderlineSpan.class); ForegroundColorSpan colorSpan = findSpan(text, 0, 6, ForegroundColorSpan.class); assertThat(colorSpan.getForegroundColor()).isEqualTo(Color.GREEN); assertFractionalLinePosition(subtitle.getCues(0).get(0), 0.85f); }