Correcting default sizing of bitmaps, code review adjustments

This commit is contained in:
Arnold Szabo 2018-11-12 23:11:44 +02:00
parent d159050eeb
commit 78ef90f61c
3 changed files with 42 additions and 23 deletions

View File

@ -70,7 +70,6 @@ public final class TtmlDecoder extends SimpleSubtitleDecoder {
private static final String ATTR_REGION = "region";
private static final String ATTR_IMAGE = "backgroundImage";
private static final Pattern CLOCK_TIME =
Pattern.compile("^([0-9][0-9]+):([0-9][0-9]):([0-9][0-9])"
+ "(?:(\\.[0-9]+)|:([0-9][0-9])(?:\\.([0-9]+))?)?$");
@ -130,7 +129,7 @@ public final class TtmlDecoder extends SimpleSubtitleDecoder {
Log.i(TAG, "Ignoring unsupported tag: " + xmlParser.getName());
unsupportedNodeDepth++;
} else if (TtmlNode.TAG_HEAD.equals(name)) {
parseHeader(xmlParser, globalStyles, regionMap, cellResolution, imageMap);
parseHeader(xmlParser, globalStyles, cellResolution, regionMap, imageMap);
} else {
try {
TtmlNode node = parseNode(xmlParser, parent, regionMap, frameAndTickRate);
@ -232,8 +231,8 @@ public final class TtmlDecoder extends SimpleSubtitleDecoder {
private Map<String, TtmlStyle> parseHeader(
XmlPullParser xmlParser,
Map<String, TtmlStyle> globalStyles,
Map<String, TtmlRegion> globalRegions,
CellResolution cellResolution,
Map<String, TtmlRegion> globalRegions,
Map<String, String> imageMap)
throws IOException, XmlPullParserException {
do {
@ -255,25 +254,23 @@ public final class TtmlDecoder extends SimpleSubtitleDecoder {
globalRegions.put(ttmlRegion.id, ttmlRegion);
}
} else if(XmlPullParserUtil.isStartTag(xmlParser, TtmlNode.TAG_METADATA)){
parseMetaData(xmlParser, imageMap);
parseMetadata(xmlParser, imageMap);
}
} while (!XmlPullParserUtil.isEndTag(xmlParser, TtmlNode.TAG_HEAD));
return globalStyles;
}
public void parseMetaData(XmlPullParser xmlParser, Map<String, String> imageMap) throws IOException, XmlPullParserException {
private void parseMetadata(XmlPullParser xmlParser, Map<String, String> imageMap) throws IOException, XmlPullParserException {
do {
xmlParser.next();
if (XmlPullParserUtil.isStartTag(xmlParser, TtmlNode.TAG_SMPTE_IMAGE)) {
for (int i = 0; i < xmlParser.getAttributeCount(); i++) {
String id = XmlPullParserUtil.getAttributeValue(xmlParser, "id");
if(id != null){
if (id != null) {
String base64 = xmlParser.nextText();
imageMap.put(id, base64);
}
}
}
} while (!XmlPullParserUtil.isEndTag(xmlParser, TtmlNode.TAG_METADATA));
}

View File

@ -183,18 +183,24 @@ import java.util.TreeSet;
public List<Cue> getCues(long timeUs, Map<String, TtmlStyle> globalStyles,
Map<String, TtmlRegion> regionMap, Map<String, String> imageMap) {
TreeMap<String, SpannableStringBuilder> regionOutputs = new TreeMap<>();
List<Pair<String, String>> regionImageList = new ArrayList<>();
TreeMap<String, SpannableStringBuilder> regionTextOutputs = new TreeMap<>();
List<Pair<String, String>> regionImageOutputs = new ArrayList<>();
traverseForText(timeUs, false, regionId, regionOutputs);
traverseForStyle(timeUs, globalStyles, regionOutputs);
traverseForImage(timeUs, regionId, regionImageList);
traverseForText(timeUs, false, regionId, regionTextOutputs);
traverseForStyle(timeUs, globalStyles, regionTextOutputs);
traverseForImage(timeUs, regionId, regionImageOutputs);
List<Cue> cues = new ArrayList<>();
// Create image based cues
for (Pair<String, String> regionImagePair : regionImageList) {
for (Pair<String, String> regionImagePair : regionImageOutputs) {
String base64 = imageMap.get(regionImagePair.second);
if (base64 == null) {
// Image ref points to invalid image, do nothing
continue;
}
byte[] decodedString = Base64.decode(base64, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
TtmlRegion region = regionMap.get(regionImagePair.first);
@ -206,13 +212,13 @@ import java.util.TreeSet;
region.line,
region.lineAnchor,
region.width,
Cue.DIMEN_UNSET
/* height= */ Cue.DIMEN_UNSET
)
);
}
// Create text based cues
for (Entry<String, SpannableStringBuilder> entry : regionOutputs.entrySet()) {
for (Entry<String, SpannableStringBuilder> entry : regionTextOutputs.entrySet()) {
TtmlRegion region = regionMap.get(entry.getKey());
cues.add(
new Cue(

View File

@ -355,8 +355,12 @@ import com.google.android.exoplayer2.util.Util;
}
private void setupBitmapLayout() {
int parentWidth = parentRight - parentLeft;
int parentHeight = parentBottom - parentTop;
// Default position
if (cuePosition == Cue.DIMEN_UNSET) {
cuePositionAnchor = Cue.ANCHOR_TYPE_MIDDLE;
cuePosition = 0.5f;
}
@ -366,13 +370,25 @@ import com.google.android.exoplayer2.util.Util;
cueLine = 0.85f;
}
// Default width
// Default width and height
if (cueSize == Cue.DIMEN_UNSET) {
// Scale up by height to be 10% of the parent's height
cueBitmapHeight = 0.1f;
float heightInParent = parentHeight * cueBitmapHeight;
float widthInParent = heightInParent * ((float) cueBitmap.getWidth() / cueBitmap.getHeight());
cueSize = widthInParent / parentWidth;
// If by the previous scaling the width exceeds 50% of the parent's width
// then scale back to 50% by width and adjust its height to keep the aspect ratio
if (cueSize > 0.5f) {
cueSize = 0.5f;
widthInParent = parentWidth * cueSize;
heightInParent = widthInParent * ((float) cueBitmap.getHeight() / cueBitmap.getWidth());
cueBitmapHeight = heightInParent / parentHeight;
}
}
int parentWidth = parentRight - parentLeft;
int parentHeight = parentBottom - parentTop;
float anchorX = parentLeft + (parentWidth * cuePosition);
float anchorY = parentTop + (parentHeight * cueLine);
int width = Math.round(parentWidth * cueSize);