Commit 37908dd4 by ibaker Committed by Ian Baker

Remove TTML package from null-checking blacklist

PiperOrigin-RevId: 290629644
parent 8a2a5271
......@@ -31,6 +31,7 @@ import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import java.util.TreeSet;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
/**
* A package internal representation of TTML node.
......@@ -93,7 +94,7 @@ import java.util.TreeSet;
private final HashMap<String, Integer> nodeStartsByRegion;
private final HashMap<String, Integer> nodeEndsByRegion;
private List<TtmlNode> children;
@MonotonicNonNull private List<TtmlNode> children;
public static TtmlNode buildTextNode(String text) {
return new TtmlNode(
......@@ -196,6 +197,7 @@ import java.util.TreeSet;
}
}
@Nullable
public String[] getStyleIds() {
return styleIds;
}
......@@ -217,7 +219,7 @@ import java.util.TreeSet;
// Create image based cues.
for (Pair<String, String> regionImagePair : regionImageOutputs) {
String encodedBitmapData = imageMap.get(regionImagePair.second);
@Nullable String encodedBitmapData = imageMap.get(regionImagePair.second);
if (encodedBitmapData == null) {
// Image reference points to an invalid image. Do nothing.
continue;
......@@ -225,7 +227,7 @@ import java.util.TreeSet;
byte[] bitmapData = Base64.decode(encodedBitmapData, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapData, /* offset= */ 0, bitmapData.length);
TtmlRegion region = regionMap.get(regionImagePair.first);
TtmlRegion region = Assertions.checkNotNull(regionMap.get(regionImagePair.first));
cues.add(
new Cue.Builder()
......@@ -241,7 +243,7 @@ import java.util.TreeSet;
// Create text based cues.
for (Entry<String, SpannableStringBuilder> entry : regionTextOutputs.entrySet()) {
TtmlRegion region = regionMap.get(entry.getKey());
TtmlRegion region = Assertions.checkNotNull(regionMap.get(entry.getKey()));
cues.add(
new Cue(
cleanUpText(entry.getValue()),
......@@ -286,7 +288,7 @@ import java.util.TreeSet;
String resolvedRegionId = ANONYMOUS_REGION_ID.equals(regionId) ? inheritedRegion : regionId;
if (isTextNode && descendsPNode) {
getRegionOutput(resolvedRegionId, regionOutputs).append(text);
getRegionOutput(resolvedRegionId, regionOutputs).append(Assertions.checkNotNull(text));
} else if (TAG_BR.equals(tag) && descendsPNode) {
getRegionOutput(resolvedRegionId, regionOutputs).append('\n');
} else if (isActive(timeUs)) {
......@@ -330,7 +332,7 @@ import java.util.TreeSet;
int start = nodeStartsByRegion.containsKey(regionId) ? nodeStartsByRegion.get(regionId) : 0;
int end = entry.getValue();
if (start != end) {
SpannableStringBuilder regionOutput = regionOutputs.get(regionId);
SpannableStringBuilder regionOutput = Assertions.checkNotNull(regionOutputs.get(regionId));
applyStyleToOutput(globalStyles, regionOutput, start, end);
}
}
......@@ -344,7 +346,7 @@ import java.util.TreeSet;
SpannableStringBuilder regionOutput,
int start,
int end) {
TtmlStyle resolvedStyle = TtmlRenderUtil.resolveStyle(style, styleIds, globalStyles);
@Nullable TtmlStyle resolvedStyle = TtmlRenderUtil.resolveStyle(style, styleIds, globalStyles);
if (resolvedStyle != null) {
TtmlRenderUtil.applyStylesToSpan(regionOutput, start, end, resolvedStyle);
}
......
......@@ -15,6 +15,7 @@
*/
package com.google.android.exoplayer2.text.ttml;
import android.text.Layout.Alignment;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
......@@ -26,6 +27,7 @@ import android.text.style.StrikethroughSpan;
import android.text.style.StyleSpan;
import android.text.style.TypefaceSpan;
import android.text.style.UnderlineSpan;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.text.SpanUtil;
import java.util.Map;
......@@ -34,30 +36,35 @@ import java.util.Map;
*/
/* package */ final class TtmlRenderUtil {
public static TtmlStyle resolveStyle(TtmlStyle style, String[] styleIds,
Map<String, TtmlStyle> globalStyles) {
if (style == null && styleIds == null) {
// No styles at all.
return null;
} else if (style == null && styleIds.length == 1) {
// Only one single referential style present.
return globalStyles.get(styleIds[0]);
} else if (style == null && styleIds.length > 1) {
// Only multiple referential styles present.
TtmlStyle chainedStyle = new TtmlStyle();
for (String id : styleIds) {
chainedStyle.chain(globalStyles.get(id));
@Nullable
public static TtmlStyle resolveStyle(
@Nullable TtmlStyle style, @Nullable String[] styleIds, Map<String, TtmlStyle> globalStyles) {
if (style == null) {
if (styleIds == null) {
// No styles at all.
return null;
} else if (styleIds.length == 1) {
// Only one single referential style present.
return globalStyles.get(styleIds[0]);
} else if (styleIds.length > 1) {
// Only multiple referential styles present.
TtmlStyle chainedStyle = new TtmlStyle();
for (String id : styleIds) {
chainedStyle.chain(globalStyles.get(id));
}
return chainedStyle;
}
return chainedStyle;
} else if (style != null && styleIds != null && styleIds.length == 1) {
// Merge a single referential style into inline style.
return style.chain(globalStyles.get(styleIds[0]));
} else if (style != null && styleIds != null && styleIds.length > 1) {
// Merge multiple referential styles into inline style.
for (String id : styleIds) {
style.chain(globalStyles.get(id));
} else /* style != null */ {
if (styleIds != null && styleIds.length == 1) {
// Merge a single referential style into inline style.
return style.chain(globalStyles.get(styleIds[0]));
} else if (styleIds != null && styleIds.length > 1) {
// Merge multiple referential styles into inline style.
for (String id : styleIds) {
style.chain(globalStyles.get(id));
}
return style;
}
return style;
}
// Only inline styles available.
return style;
......@@ -100,10 +107,11 @@ import java.util.Map;
end,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
if (style.getTextAlign() != null) {
@Nullable Alignment textAlign = style.getTextAlign();
if (textAlign != null) {
SpanUtil.addOrReplaceSpan(
builder,
new AlignmentSpan.Standard(style.getTextAlign()),
new AlignmentSpan.Standard(textAlign),
start,
end,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
......
......@@ -18,9 +18,11 @@ package com.google.android.exoplayer2.text.ttml;
import android.graphics.Typeface;
import android.text.Layout;
import androidx.annotation.IntDef;
import androidx.annotation.Nullable;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
/**
* Style object of a <code>TtmlNode</code>
......@@ -58,7 +60,7 @@ import java.lang.annotation.RetentionPolicy;
private static final int OFF = 0;
private static final int ON = 1;
private String fontFamily;
private @MonotonicNonNull String fontFamily;
private int fontColor;
private boolean hasFontColor;
private int backgroundColor;
......@@ -69,8 +71,8 @@ import java.lang.annotation.RetentionPolicy;
@OptionalBoolean private int italic;
@FontSizeUnit private int fontSizeUnit;
private float fontSize;
private String id;
private Layout.Alignment textAlign;
private @MonotonicNonNull String id;
private Layout.@MonotonicNonNull Alignment textAlign;
public TtmlStyle() {
linethrough = UNSPECIFIED;
......@@ -122,6 +124,7 @@ import java.lang.annotation.RetentionPolicy;
return this;
}
@Nullable
public String getFontFamily() {
return fontFamily;
}
......@@ -171,7 +174,7 @@ import java.lang.annotation.RetentionPolicy;
*
* @param ancestor the referential style to inherit from
*/
public TtmlStyle chain(TtmlStyle ancestor) {
public TtmlStyle chain(@Nullable TtmlStyle ancestor) {
return inherit(ancestor, true);
}
......@@ -182,11 +185,11 @@ import java.lang.annotation.RetentionPolicy;
*
* @param ancestor the ancestor style to inherit from
*/
public TtmlStyle inherit(TtmlStyle ancestor) {
public TtmlStyle inherit(@Nullable TtmlStyle ancestor) {
return inherit(ancestor, false);
}
private TtmlStyle inherit(TtmlStyle ancestor, boolean chaining) {
private TtmlStyle inherit(@Nullable TtmlStyle ancestor, boolean chaining) {
if (ancestor != null) {
if (!hasFontColor && ancestor.hasFontColor) {
setFontColor(ancestor.fontColor);
......@@ -197,7 +200,7 @@ import java.lang.annotation.RetentionPolicy;
if (italic == UNSPECIFIED) {
italic = ancestor.italic;
}
if (fontFamily == null) {
if (fontFamily == null && ancestor.fontFamily != null) {
fontFamily = ancestor.fontFamily;
}
if (linethrough == UNSPECIFIED) {
......@@ -206,7 +209,7 @@ import java.lang.annotation.RetentionPolicy;
if (underline == UNSPECIFIED) {
underline = ancestor.underline;
}
if (textAlign == null) {
if (textAlign == null && ancestor.textAlign != null) {
textAlign = ancestor.textAlign;
}
if (fontSizeUnit == UNSPECIFIED) {
......@@ -226,10 +229,12 @@ import java.lang.annotation.RetentionPolicy;
return this;
}
@Nullable
public String getId() {
return id;
}
@Nullable
public Layout.Alignment getTextAlign() {
return textAlign;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment