Skip to content

Commit

Permalink
Merge branch 'main' into feat/DORIS-2023-migrate-media-1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
guoen21 committed Feb 18, 2024
2 parents be08784 + 3ae15e7 commit 87384ee
Show file tree
Hide file tree
Showing 7 changed files with 215 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,11 @@ public final class Cue implements Bundleable {
*/
public final float shearDegrees;

/**
* The shadow to be applied to this clue.
*/
@Nullable public final TextShadow textShadow;

private Cue(
@Nullable CharSequence text,
@Nullable Alignment textAlignment,
Expand All @@ -320,7 +325,8 @@ private Cue(
boolean windowColorSet,
int windowColor,
@VerticalType int verticalType,
float shearDegrees) {
float shearDegrees,
@Nullable TextShadow textShadow) {
// Exactly one of text or bitmap should be set.
if (text == null) {
Assertions.checkNotNull(bitmap);
Expand Down Expand Up @@ -350,6 +356,7 @@ private Cue(
this.textSize = textSize;
this.verticalType = verticalType;
this.shearDegrees = shearDegrees;
this.textShadow = textShadow;
}

/** Returns a new {@link Cue.Builder} initialized with the same values as this Cue. */
Expand Down Expand Up @@ -385,7 +392,8 @@ public boolean equals(@Nullable Object obj) {
&& textSizeType == that.textSizeType
&& textSize == that.textSize
&& verticalType == that.verticalType
&& shearDegrees == that.shearDegrees;
&& shearDegrees == that.shearDegrees
&& textShadow == that.textShadow;
}

@Override
Expand All @@ -407,7 +415,8 @@ public int hashCode() {
textSizeType,
textSize,
verticalType,
shearDegrees);
shearDegrees,
textShadow);
}

/** A builder for {@link Cue} objects. */
Expand All @@ -430,6 +439,7 @@ public static final class Builder {
@ColorInt private int windowColor;
private @VerticalType int verticalType;
private float shearDegrees;
@Nullable private TextShadow textShadow;

public Builder() {
text = null;
Expand All @@ -448,6 +458,7 @@ public Builder() {
windowColorSet = false;
windowColor = Color.BLACK;
verticalType = TYPE_UNSET;
textShadow = null;
}

private Builder(Cue cue) {
Expand All @@ -468,6 +479,7 @@ private Builder(Cue cue) {
windowColor = cue.windowColor;
verticalType = cue.verticalType;
shearDegrees = cue.shearDegrees;
textShadow = cue.textShadow;
}

/**
Expand Down Expand Up @@ -790,6 +802,24 @@ public Builder setShearDegrees(float shearDegrees) {
return this;
}

/** Sets shadow for this Cue. */
@CanIgnoreReturnValue
public Builder setTextShadow(@Nullable TextShadow textShadow) {
this.textShadow = textShadow;
return this;
}

/**
* Gets the shadow of the cue text.
*
* @see Cue#textShadow
*/
@Pure
@Nullable
public TextShadow getShadow() {
return textShadow;
}

/**
* Gets the vertical formatting for this Cue.
*
Expand Down Expand Up @@ -819,7 +849,8 @@ public Cue build() {
windowColorSet,
windowColor,
verticalType,
shearDegrees);
shearDegrees,
textShadow);
}
}

Expand All @@ -842,6 +873,7 @@ public Cue build() {
private static final String FIELD_WINDOW_COLOR_SET = Util.intToStringMaxRadix(14);
private static final String FIELD_VERTICAL_TYPE = Util.intToStringMaxRadix(15);
private static final String FIELD_SHEAR_DEGREES = Util.intToStringMaxRadix(16);
private static final String FIELD_SHADOW = Util.intToStringMaxRadix(17);

@UnstableApi
@Override
Expand All @@ -868,6 +900,7 @@ public Bundle toBundle() {
bundle.putInt(FIELD_WINDOW_COLOR, windowColor);
bundle.putInt(FIELD_VERTICAL_TYPE, verticalType);
bundle.putFloat(FIELD_SHEAR_DEGREES, shearDegrees);
bundle.putSerializable(FIELD_SHADOW, textShadow);
return bundle;
}

Expand Down Expand Up @@ -925,6 +958,11 @@ private static final Cue fromBundle(Bundle bundle) {
if (bundle.containsKey(FIELD_SHEAR_DEGREES)) {
builder.setShearDegrees(bundle.getFloat(FIELD_SHEAR_DEGREES));
}
@Nullable
TextShadow textShadow = (TextShadow) bundle.getSerializable(FIELD_SHADOW);
if (textShadow != null) {
builder.setTextShadow(textShadow);
}
return builder.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package androidx.media3.common.text;

import androidx.annotation.NonNull;
import java.io.Serializable;
import java.util.List;

public class TextShadow implements Serializable {

@NonNull
private final List<Component> components;

public TextShadow(@NonNull List<Component> components) {
this.components = components;
}

public List<Component> getComponents() {
return components;
}

public static class Component implements Serializable {

public final int dx;
public final int dy;
public final int radius;
public final int color;

public Component(int dx, int dy, int radius, int color) {
this.dx = dx;
this.dy = dy;
this.radius = radius;
this.color = color;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package androidx.media3.common.text;

import android.text.TextPaint;
import android.text.style.CharacterStyle;
import androidx.annotation.Nullable;

/**
* A span that contains the text shadow but does not modifies the text as we can't apply multiple
* shadows with span, instead the {@link TextShadow} will be applied in the {@link SubtitlePainter}.
*/
public class TextShadowSpan extends CharacterStyle {

@Nullable
private TextShadow textShadow;

public TextShadowSpan(@Nullable TextShadow textShadow) {
super();
this.textShadow = textShadow;
}

@Nullable
public TextShadow getTextShadow() {
return textShadow;
}

@Override
public void updateDrawState(TextPaint tp) {
// Do nothing.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import android.text.TextUtils;
import androidx.annotation.Nullable;
import androidx.media3.common.text.TextAnnotation;
import androidx.media3.common.text.TextShadow;
import androidx.media3.common.util.Assertions;
import androidx.media3.common.util.ColorParser;
import androidx.media3.common.util.Log;
Expand All @@ -41,6 +42,8 @@
private static final String RULE_END = "}";
private static final String PROPERTY_COLOR = "color";
private static final String PROPERTY_BGCOLOR = "background-color";
private static final String PROPERTY_BG = "background";
private static final String PROPERTY_TEXT_SHADOW = "text-shadow";
private static final String PROPERTY_FONT_FAMILY = "font-family";
private static final String PROPERTY_FONT_WEIGHT = "font-weight";
private static final String PROPERTY_FONT_SIZE = "font-size";
Expand Down Expand Up @@ -175,10 +178,12 @@ private static void parseStyleDeclaration(
return;
}
skipWhitespaceAndComments(input);
int propertyValueStartIndex = input.getPosition();
String value = parsePropertyValue(input, stringBuilder);
if (value == null || "".equals(value)) {
return;
}
int propertyValueEndIndex = input.getPosition();
int position = input.getPosition();
String token = parseNextToken(input, stringBuilder);
if (";".equals(token)) {
Expand All @@ -198,12 +203,25 @@ private static void parseStyleDeclaration(
} catch (IllegalArgumentException ex) {
// Ignore exception.
}
} else if (PROPERTY_BGCOLOR.equals(property)) {
} else if (PROPERTY_BG.equals(property) || PROPERTY_BGCOLOR.equals(property)) {
try {
style.setBackgroundColor(ColorParser.parseCssColor(value));
} catch (IllegalArgumentException ex) {
// Ignore exception.
}
} else if (PROPERTY_TEXT_SHADOW.equals(property)) {
try {
// Save original position.
int originalPosition = input.getPosition();
// Set back position to the value's start index to read it with whitespaces.
int length = propertyValueEndIndex - propertyValueStartIndex;
input.setPosition(propertyValueStartIndex);
style.setTextShadow(parseTextShadow(input.readString(length)));
// Set back original position.
input.setPosition(originalPosition);
} catch (IllegalArgumentException ex) {
// Ignore exception.
}
} else if (PROPERTY_RUBY_POSITION.equals(property)) {
if (VALUE_OVER.equals(value)) {
style.setRubyPosition(TextAnnotation.POSITION_BEFORE);
Expand Down Expand Up @@ -405,4 +423,41 @@ private void applySelectorToStyle(WebvttCssStyle style, String selector) {
style.setTargetClasses(Util.nullSafeArrayCopyOfRange(classDivision, 1, classDivision.length));
}
}

private static TextShadow parseTextShadow(@Nullable String value) {
if (value == null) {
throw new IllegalArgumentException();
}
List<TextShadow.Component> shadowComponents = new ArrayList<>();
// Split multiple shadows "1px 1px black, 1px 1px black" -> ["1px 1px black", "1px 1px black"]
String[] shadows = value.split(",");
Pattern pattern = Pattern.compile("-?\\d+");
for (String shadow : shadows) {
// Split into components "1px 1px black" -> ["1px", "1px", "black"]
String[] components = shadow.trim().split(" ");
for (int i = 0; i < components.length; i++) {
Matcher matcher = pattern.matcher(components[i]);
if (matcher.find()) {
// Extract numbers ("1px" -> "1")
components[i] = matcher.group();
}
}

if (components.length >= 3) {
int dx = Integer.parseInt(components[0]);
int dy = Integer.parseInt(components[1]);
int blur = 0;
String colorComponent = components[components.length - 1];

if (components.length == 4) {
blur = Integer.parseInt(components[2]);
}

shadowComponents.add(
new TextShadow.Component(dx, dy, blur, ColorParser.parseCssColor(colorComponent)));
}
}
return new TextShadow(shadowComponents);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import androidx.annotation.ColorInt;
import androidx.annotation.IntDef;
import androidx.annotation.Nullable;
import androidx.media3.common.text.TextShadow;
import androidx.media3.common.text.TextAnnotation;
import androidx.media3.common.util.UnstableApi;
import com.google.common.base.Ascii;
Expand Down Expand Up @@ -98,6 +99,7 @@ public final class WebvttCssStyle {
private boolean hasFontColor;
private int backgroundColor;
private boolean hasBackgroundColor;
@Nullable private TextShadow textShadow;
private @OptionalBoolean int linethrough;
private @OptionalBoolean int underline;
private @OptionalBoolean int bold;
Expand All @@ -115,6 +117,7 @@ public WebvttCssStyle() {
fontFamily = null;
hasFontColor = false;
hasBackgroundColor = false;
textShadow = null;
linethrough = UNSPECIFIED;
underline = UNSPECIFIED;
bold = UNSPECIFIED;
Expand Down Expand Up @@ -274,6 +277,17 @@ public boolean hasBackgroundColor() {
return hasBackgroundColor;
}

@CanIgnoreReturnValue
public WebvttCssStyle setTextShadow(TextShadow textShadow) {
this.textShadow = textShadow;
return this;
}

@Nullable
public TextShadow getTextShadow() {
return textShadow;
}

@CanIgnoreReturnValue
public WebvttCssStyle setFontSize(float fontSize) {
this.fontSize = fontSize;
Expand Down
Loading

0 comments on commit 87384ee

Please sign in to comment.