Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add outlineWidth to Cue #1840

Open
wants to merge 5 commits into
base: release
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,13 @@
* <li>{@link #RUBY}
* <li>{@link #TEXT_EMPHASIS}
* <li>{@link #HORIZONTAL_TEXT_IN_VERTICAL_CONTEXT}
* <li>{@link #OUTLINE_WIDTH}
* </ul>
*/
@Documented
@Retention(RetentionPolicy.SOURCE)
@Target({TYPE_USE})
@IntDef({UNKNOWN, RUBY, TEXT_EMPHASIS, HORIZONTAL_TEXT_IN_VERTICAL_CONTEXT})
@IntDef({UNKNOWN, RUBY, TEXT_EMPHASIS, HORIZONTAL_TEXT_IN_VERTICAL_CONTEXT, OUTLINE_WIDTH})
private @interface CustomSpanType {}

private static final int UNKNOWN = -1;
Expand All @@ -69,6 +70,8 @@

private static final int HORIZONTAL_TEXT_IN_VERTICAL_CONTEXT = 3;

private static final int OUTLINE_WIDTH = 4;

private static final String FIELD_START_INDEX = Util.intToStringMaxRadix(0);
private static final String FIELD_END_INDEX = Util.intToStringMaxRadix(1);
private static final String FIELD_FLAGS = Util.intToStringMaxRadix(2);
Expand All @@ -94,6 +97,11 @@ public static ArrayList<Bundle> bundleCustomSpans(Spanned text) {
text, span, /* spanType= */ HORIZONTAL_TEXT_IN_VERTICAL_CONTEXT, /* params= */ null);
bundledCustomSpans.add(bundle);
}
for (OutlineSpan span : text.getSpans(0, text.length(), OutlineSpan.class)) {
Bundle bundle =
spanToBundle(text, span, /* spanType= */ OUTLINE_WIDTH, /* params= */ span.toBundle());
bundledCustomSpans.add(bundle);
}
return bundledCustomSpans;
}

Expand All @@ -113,6 +121,8 @@ public static void unbundleAndApplyCustomSpan(Bundle customSpanBundle, Spannable
case HORIZONTAL_TEXT_IN_VERTICAL_CONTEXT:
text.setSpan(new HorizontalTextInVerticalContextSpan(), start, end, flags);
break;
case OUTLINE_WIDTH:
text.setSpan(OutlineSpan.fromBundle(checkNotNull(span)),start,end,flags);
default:
break;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package androidx.media3.common.text;

import android.os.Bundle;
import android.text.TextPaint;
import android.text.style.CharacterStyle;
import androidx.annotation.Px;
import androidx.media3.common.util.Util;

/**
* A styling span for outline width on subtitle text.
*/
public class OutlineSpan extends CharacterStyle {

/**
* The outline size in pixels.
*/
@Px
public final float outlineWidth;
private static final String FIELD_OUTLINE_WIDTH = Util.intToStringMaxRadix(0);

public OutlineSpan(@Px float outlineWidth) {
this.outlineWidth = outlineWidth;
}

public Bundle toBundle() {
Bundle bundle = new Bundle();
bundle.putFloat(FIELD_OUTLINE_WIDTH, outlineWidth);
return bundle;
}

public static OutlineSpan fromBundle(Bundle bundle) {
return new OutlineSpan(
/* outlineWidth= */ bundle.getFloat(FIELD_OUTLINE_WIDTH)
);
}

@Override
public void updateDrawState(TextPaint tp) {
tp.setStrokeWidth(outlineWidth);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import android.util.SparseArray;
import androidx.annotation.Nullable;
import androidx.media3.common.text.HorizontalTextInVerticalContextSpan;
import androidx.media3.common.text.OutlineSpan;
import androidx.media3.common.text.RubySpan;
import androidx.media3.common.text.TextAnnotation;
import androidx.media3.common.text.TextEmphasisSpan;
Expand Down Expand Up @@ -207,6 +208,12 @@ private static String getOpeningTag(Object span, float displayDensity) {
+ "-webkit-text-emphasis-position:%2$s;text-emphasis-position:%2$s;"
+ "display:inline-block;'>",
style, position);
} else if (span instanceof OutlineSpan) {
OutlineSpan outline = ((OutlineSpan) span);
return Util.formatInvariant(
"<span style='-webkit-text-stroke-width: %1$spx;'>",
outline.outlineWidth
);
} else {
return null;
}
Expand All @@ -220,7 +227,8 @@ private static String getClosingTag(Object span) {
|| span instanceof HorizontalTextInVerticalContextSpan
|| span instanceof AbsoluteSizeSpan
|| span instanceof RelativeSizeSpan
|| span instanceof TextEmphasisSpan) {
|| span instanceof TextEmphasisSpan
|| span instanceof OutlineSpan) {
return "</span>";
} else if (span instanceof TypefaceSpan) {
@Nullable String fontFamily = ((TypefaceSpan) span).getFamily();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,13 @@ private void updateWebView() {
+ "font-size:%s;"
+ "line-height:%.2f;"
+ "text-shadow:%s;"
+ "-webkit-text-stroke-color: %s;"
+ "'>",
HtmlUtils.toCssRgba(style.foregroundColor),
convertTextSizeToCss(defaultTextSizeType, defaultTextSize),
CSS_LINE_HEIGHT,
convertCaptionStyleToCssTextShadow(style)));
convertCaptionStyleToCssTextShadow(style),
HtmlUtils.toCssRgba(style.edgeColor)));

Map<String, String> cssRuleSets = new HashMap<>();
cssRuleSets.put(
Expand Down