Skip to content

Commit

Permalink
Introduce LanguageFeatureStyle interface to mark language features
Browse files Browse the repository at this point in the history
  • Loading branch information
dlafayet committed May 17, 2021
1 parent 49dfe66 commit e322806
Show file tree
Hide file tree
Showing 7 changed files with 262 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@
// NOTE: There's no Android layout support for this, so this span currently doesn't extend any
// styling superclasses (e.g. MetricAffectingSpan). The only way to render this styling is to
// extract the spans and do the layout manually.
public final class HorizontalTextInVerticalContextSpan {}
public final class HorizontalTextInVerticalContextSpan implements LanguageFeatureStyle {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.exoplayer2.text.span;

/**
* Marker interface to mark classes that are language features.
*/
public interface LanguageFeatureStyle {
}

Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
// extract the spans and do the layout manually.
// TODO: Consider adding support for parenthetical text to be used when rendering doesn't support
// rubies (e.g. HTML <rp> tag).
public final class RubySpan {
public final class RubySpan implements LanguageFeatureStyle {

/** The ruby text, i.e. the smaller explanatory characters. */
public final String rubyText;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
// NOTE: There's no Android layout support for text emphasis, so this span currently doesn't extend
// any styling superclasses (e.g. MetricAffectingSpan). The only way to render this emphasis is to
// extract the spans and do the layout manually.
public final class TextEmphasisSpan {
public final class TextEmphasisSpan implements LanguageFeatureStyle {

/**
* The possible mark shapes that can be used.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.text.Cue;
import com.google.android.exoplayer2.text.TextOutput;
import com.google.android.exoplayer2.text.span.LanguageFeatureStyle;
import com.google.android.exoplayer2.util.Util;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
Expand Down Expand Up @@ -374,47 +375,9 @@ private List<Cue> getCuesWithStylingPreferencesApplied() {
}
List<Cue> strippedCues = new ArrayList<>(cues.size());
for (int i = 0; i < cues.size(); i++) {
strippedCues.add(removeEmbeddedStyling(cues.get(i)));
strippedCues.add(SubtitleViewUtils
.removeEmbeddedStyling(cues.get(i), applyEmbeddedStyles, applyEmbeddedFontSizes));
}
return strippedCues;
}

private Cue removeEmbeddedStyling(Cue cue) {
@Nullable CharSequence cueText = cue.text;
if (!applyEmbeddedStyles) {
Cue.Builder strippedCue =
cue.buildUpon().setTextSize(Cue.DIMEN_UNSET, Cue.TYPE_UNSET).clearWindowColor();
if (cueText != null) {
// Remove all spans, regardless of type.
strippedCue.setText(new SpannableString(cueText.toString()));
if (cueText instanceof Spanned) {
SubtitleViewUtils
.preserveJapaneseLanguageFeatures((SpannableString)strippedCue.getText(),
(Spanned) cueText);
}
}
return strippedCue.build();
} else if (!applyEmbeddedFontSizes) {
if (cueText == null) {
return cue;
}
Cue.Builder strippedCue = cue.buildUpon().setTextSize(Cue.DIMEN_UNSET, Cue.TYPE_UNSET);
if (cueText instanceof Spanned) {
SpannableString spannable = SpannableString.valueOf(cueText);
AbsoluteSizeSpan[] absSpans =
spannable.getSpans(0, spannable.length(), AbsoluteSizeSpan.class);
for (AbsoluteSizeSpan absSpan : absSpans) {
spannable.removeSpan(absSpan);
}
RelativeSizeSpan[] relSpans =
spannable.getSpans(0, spannable.length(), RelativeSizeSpan.class);
for (RelativeSizeSpan relSpan : relSpans) {
spannable.removeSpan(relSpan);
}
strippedCue.setText(spannable);
}
return strippedCue.build();
}
return cue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@
package com.google.android.exoplayer2.ui;

import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.RelativeSizeSpan;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.text.Cue;
import com.google.android.exoplayer2.text.span.HorizontalTextInVerticalContextSpan;
import com.google.android.exoplayer2.text.span.LanguageFeatureStyle;
import com.google.android.exoplayer2.text.span.RubySpan;
import com.google.android.exoplayer2.text.span.SpanUtil;
import com.google.android.exoplayer2.text.span.TextEmphasisSpan;
Expand Down Expand Up @@ -54,26 +60,42 @@ public static float resolveTextSize(
}
}

public static void preserveJapaneseLanguageFeatures(Spannable copy, Spanned original) {
RubySpan[] absSpans =
original.getSpans(0, original.length(), RubySpan.class);
for (RubySpan rubySpan : absSpans) {
SpanUtil.addOrReplaceSpan(copy, rubySpan, original.getSpanStart(rubySpan),
original.getSpanEnd(rubySpan), original.getSpanFlags(rubySpan));
}
TextEmphasisSpan[] textEmphasisSpans =
original.getSpans(0, original.length(), TextEmphasisSpan.class);
for (TextEmphasisSpan textEmphasisSpan : textEmphasisSpans) {
SpanUtil.addOrReplaceSpan(copy, textEmphasisSpan, original.getSpanStart(textEmphasisSpan),
original.getSpanEnd(textEmphasisSpan), original.getSpanFlags(textEmphasisSpan));
}
HorizontalTextInVerticalContextSpan[] horizontalTextInVerticalContextSpans =
original.getSpans(0, original.length(), HorizontalTextInVerticalContextSpan.class);

for (HorizontalTextInVerticalContextSpan span : horizontalTextInVerticalContextSpans) {
SpanUtil.addOrReplaceSpan(copy, span, original.getSpanStart(span),
original.getSpanEnd(span), original.getSpanFlags(span));
/**
* Returns a cue object with the specified styling removed
* @param cue - Cue object that contains all the styling information
* @param applyEmbeddedStyles - if true, styles embedded within the cues should be applied
* @param applyEmbeddedFontSizes - if true, font sizes embedded within the cues should be applied.
* Only takes effect if setApplyEmbeddedStyles is true
* See {@link SubtitleView#setApplyEmbeddedStyles}
* @return New cue object with the specified styling removed
*/
@NonNull
static Cue removeEmbeddedStyling(@NonNull Cue cue, boolean applyEmbeddedStyles,
boolean applyEmbeddedFontSizes) {
@Nullable CharSequence cueText = cue.text;
if (cueText != null && (!applyEmbeddedStyles || !applyEmbeddedFontSizes)) {
Cue.Builder strippedCue = cue.buildUpon().setTextSize(Cue.DIMEN_UNSET, Cue.TYPE_UNSET);
if (!applyEmbeddedStyles) {
strippedCue.clearWindowColor();
}
if (cueText instanceof Spanned) {
SpannableString spannable = SpannableString.valueOf(cueText);
Object[] spans = spannable.getSpans(0, spannable.length(), Object.class);
for (Object span : spans) {
if (span instanceof LanguageFeatureStyle) {
continue;
}
// applyEmbeddedFontSizes should only be applied if applyEmbeddedStyles is true
if (!applyEmbeddedStyles || span instanceof AbsoluteSizeSpan
|| span instanceof RelativeSizeSpan) {
spannable.removeSpan(span);
}
}
strippedCue.setText(spannable);
}
return strippedCue.build();
}
return cue;
}

private SubtitleViewUtils() {}
Expand Down
Loading

0 comments on commit e322806

Please sign in to comment.