Skip to content

Commit

Permalink
Add support for SSA OutlineColour (only background)
Browse files Browse the repository at this point in the history
OutlineColour should be treated as the background color if BorderStyle=3. Since currently BorderStyle is ignored, we can always treat OutlineColor as the background color.
  • Loading branch information
egor-n committed Apr 3, 2022
1 parent cf0a680 commit 388b8d1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import android.graphics.Typeface;
import android.text.Layout;
import android.text.SpannableString;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.StyleSpan;
Expand Down Expand Up @@ -319,6 +320,13 @@ private static Cue createCue(
/* end= */ spannableText.length(),
SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
}
if (style.outlineColor != null) {
spannableText.setSpan(
new BackgroundColorSpan(style.outlineColor),
/* start= */ 0,
/* end= */ spannableText.length(),
SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
}
if (style.fontSize != Cue.DIMEN_UNSET && screenHeight != Cue.DIMEN_UNSET) {
cue.setTextSize(
style.fontSize / screenHeight, Cue.TEXT_SIZE_TYPE_FRACTIONAL_IGNORE_PADDING);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
public final String name;
public final @SsaAlignment int alignment;
@Nullable @ColorInt public final Integer primaryColor;
@Nullable @ColorInt public final Integer outlineColor;
public final float fontSize;
public final boolean bold;
public final boolean italic;
Expand All @@ -105,6 +106,7 @@ private SsaStyle(
String name,
@SsaAlignment int alignment,
@Nullable @ColorInt Integer primaryColor,
@Nullable @ColorInt Integer outlineColor,
float fontSize,
boolean bold,
boolean italic,
Expand All @@ -113,6 +115,7 @@ private SsaStyle(
this.name = name;
this.alignment = alignment;
this.primaryColor = primaryColor;
this.outlineColor = outlineColor;
this.fontSize = fontSize;
this.bold = bold;
this.italic = italic;
Expand Down Expand Up @@ -141,6 +144,9 @@ public static SsaStyle fromStyleLine(String styleLine, Format format) {
format.primaryColorIndex != C.INDEX_UNSET
? parseColor(styleValues[format.primaryColorIndex].trim())
: null,
format.outlineColorIndex != C.INDEX_UNSET
? parseColor(styleValues[format.outlineColorIndex].trim())
: null,
format.fontSizeIndex != C.INDEX_UNSET
? parseFontSize(styleValues[format.fontSizeIndex].trim())
: Cue.DIMEN_UNSET,
Expand Down Expand Up @@ -257,6 +263,7 @@ private static boolean parseBooleanValue(String booleanValue) {
public final int nameIndex;
public final int alignmentIndex;
public final int primaryColorIndex;
public final int outlineColorIndex;
public final int fontSizeIndex;
public final int boldIndex;
public final int italicIndex;
Expand All @@ -268,6 +275,7 @@ private Format(
int nameIndex,
int alignmentIndex,
int primaryColorIndex,
int outlineColorIndex,
int fontSizeIndex,
int boldIndex,
int italicIndex,
Expand All @@ -277,6 +285,7 @@ private Format(
this.nameIndex = nameIndex;
this.alignmentIndex = alignmentIndex;
this.primaryColorIndex = primaryColorIndex;
this.outlineColorIndex = outlineColorIndex;
this.fontSizeIndex = fontSizeIndex;
this.boldIndex = boldIndex;
this.italicIndex = italicIndex;
Expand All @@ -295,6 +304,7 @@ public static Format fromFormatLine(String styleFormatLine) {
int nameIndex = C.INDEX_UNSET;
int alignmentIndex = C.INDEX_UNSET;
int primaryColorIndex = C.INDEX_UNSET;
int outlineColorIndex = C.INDEX_UNSET;
int fontSizeIndex = C.INDEX_UNSET;
int boldIndex = C.INDEX_UNSET;
int italicIndex = C.INDEX_UNSET;
Expand All @@ -313,6 +323,9 @@ public static Format fromFormatLine(String styleFormatLine) {
case "primarycolour":
primaryColorIndex = i;
break;
case "outlinecolour":
outlineColorIndex = i;
break;
case "fontsize":
fontSizeIndex = i;
break;
Expand All @@ -335,6 +348,7 @@ public static Format fromFormatLine(String styleFormatLine) {
nameIndex,
alignmentIndex,
primaryColorIndex,
outlineColorIndex,
fontSizeIndex,
boldIndex,
italicIndex,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ public void decodeColors() throws IOException {
SsaDecoder decoder = new SsaDecoder();
byte[] bytes = TestUtil.getByteArray(ApplicationProvider.getApplicationContext(), STYLE_COLORS);
Subtitle subtitle = decoder.decode(bytes, bytes.length, false);
assertThat(subtitle.getEventTimeCount()).isEqualTo(14);
assertThat(subtitle.getEventTimeCount()).isEqualTo(16);
// &H000000FF (AABBGGRR) -> #FFFF0000 (AARRGGBB)
Spanned firstCueText =
(Spanned) Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(0))).text;
Expand Down Expand Up @@ -342,6 +342,11 @@ public void decodeColors() throws IOException {
(Spanned) Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(12))).text;
SpannedSubject.assertThat(seventhCueText)
.hasNoForegroundColorSpanBetween(0, seventhCueText.length());
Spanned eighthCueText =
(Spanned) Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(14))).text;
SpannedSubject.assertThat(eighthCueText)
.hasBackgroundColorSpanBetween(0, eighthCueText.length())
.withColor(Color.BLUE);
}

@Test
Expand Down
18 changes: 10 additions & 8 deletions testdata/src/test/assets/media/ssa/style_colors
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ PlayResX: 1280
PlayResY: 720

[V4+ Styles]
Format: Name ,PrimaryColour
Style: PrimaryColourStyleHexRed ,&H000000FF
Style: PrimaryColourStyleHexYellow ,&H0000FFFF
Style: PrimaryColourStyleHexGreen ,&HFF00
Style: PrimaryColourStyleHexAlpha ,&HA00000FF
Style: PrimaryColourStyleDecimal ,16711680
Style: PrimaryColourStyleDecimalAlpha,2164195328
Style: PrimaryColourStyleInvalid ,blue
Format: Name ,PrimaryColour,OutlineColour
Style: PrimaryColourStyleHexRed ,&H000000FF ,&H00000000
Style: PrimaryColourStyleHexYellow ,&H0000FFFF ,&H00000000
Style: PrimaryColourStyleHexGreen ,&HFF00 ,&H00000000
Style: PrimaryColourStyleHexAlpha ,&HA00000FF ,&H00000000
Style: PrimaryColourStyleDecimal ,16711680 ,&H00000000
Style: PrimaryColourStyleDecimalAlpha,2164195328 ,&H00000000
Style: PrimaryColourStyleInvalid ,blue ,&H00000000
Style: OutlineColourStyleBlue ,&H00000000 ,&H00FF0000


[Events]
Expand All @@ -24,3 +25,4 @@ Dialogue: 0:00:07.00,0:00:08.00,PrimaryColourStyleHexAlpha ,Fourth line in RE
Dialogue: 0:00:09.00,0:00:10.00,PrimaryColourStyleDecimal ,Fifth line in BLUE (16711680).
Dialogue: 0:00:11.00,0:00:12.00,PrimaryColourStyleDecimalAlpha,Sixth line in BLUE with alpha (2164195328).
Dialogue: 0:00:13.00,0:00:14.00,PrimaryColourInvalid ,Seventh line with invalid color.
Dialogue: 0:00:15.00,0:00:16.00,OutlineColourStyleBlue ,Eighth line with BLUE (&H00FF0000) outline.

0 comments on commit 388b8d1

Please sign in to comment.