Skip to content

Commit

Permalink
Add graphic subtitles support
Browse files Browse the repository at this point in the history
  • Loading branch information
tresvecesseis committed Sep 15, 2016
1 parent ab49425 commit 530977d
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 20 deletions.
26 changes: 26 additions & 0 deletions library/src/main/java/com/google/android/exoplayer2/text/Cue.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.google.android.exoplayer2.text;

import android.graphics.Bitmap;
import android.text.Layout.Alignment;

/**
Expand Down Expand Up @@ -117,6 +118,13 @@ public class Cue {
*/
public final float size;

/**
* Graphical content
*/
public final Bitmap bitmap;

public final boolean isGraphic;

/**
* Constructs a cue whose {@link #textAlignment} is null, whose type parameters are set to
* {@link #TYPE_UNSET} and whose dimension parameters are set to {@link #DIMEN_UNSET}.
Expand Down Expand Up @@ -147,6 +155,24 @@ public Cue(CharSequence text, Alignment textAlignment, float line, int lineType,
this.position = position;
this.positionAnchor = positionAnchor;
this.size = size;

this.bitmap = null;
this.isGraphic = false;
}

public Cue(Bitmap data) {

this.text = null;
this.textAlignment = null;
this.line = DIMEN_UNSET;
this.lineType = TYPE_UNSET;
this.lineAnchor = TYPE_UNSET;
this.position = DIMEN_UNSET;
this.positionAnchor = TYPE_UNSET;
this.size = DIMEN_UNSET;

this.bitmap = data;
this.isGraphic = true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
Expand Down Expand Up @@ -221,29 +225,44 @@ public void setBottomPaddingFraction(float bottomPaddingFraction) {
@Override
public void dispatchDraw(Canvas canvas) {
int cueCount = (cues == null) ? 0 : cues.size();
int rawTop = getTop();
int rawBottom = getBottom();

// Calculate the bounds after padding is taken into account.
int left = getLeft() + getPaddingLeft();
int top = rawTop + getPaddingTop();
int right = getRight() + getPaddingRight();
int bottom = rawBottom - getPaddingBottom();
if (bottom <= top || right <= left) {
// No space to draw subtitles.
return;
}
if (cueCount > 0) {
Cue cue;
cue = cues.get(0);
if (cue.isGraphic) {
Bitmap subsBitmap = cue.bitmap;
if (subsBitmap == null) {
return;
}
Paint paint = new Paint(); // fixme default paint
Rect src = new Rect(0, 0, subsBitmap.getWidth(), subsBitmap.getHeight());
RectF dst = new RectF(0, 0, getWidth(), getHeight());
canvas.drawBitmap(subsBitmap, src, dst, paint);
} else {
int rawTop = getTop();
int rawBottom = getBottom();

float textSizePx = textSizeType == ABSOLUTE ? textSize
: textSize * (textSizeType == FRACTIONAL ? (bottom - top) : (rawBottom - rawTop));
if (textSizePx <= 0) {
// Text has no height.
return;
}
// Calculate the bounds after padding is taken into account.
int left = getLeft() + getPaddingLeft();
int top = rawTop + getPaddingTop();
int right = getRight() + getPaddingRight();
int bottom = rawBottom - getPaddingBottom();
if (bottom <= top || right <= left) {
// No space to draw subtitles.
return;
}
float textSizePx = textSizeType == ABSOLUTE ? textSize
: textSize * (textSizeType == FRACTIONAL ? (bottom - top) : (rawBottom - rawTop));
if (textSizePx <= 0) {
// Text has no height.
return;
}

for (int i = 0; i < cueCount; i++) {
painters.get(i).draw(cues.get(i), applyEmbeddedStyles, style, textSizePx,
bottomPaddingFraction, canvas, left, top, right, bottom);
for (int i = 0; i < cueCount; i++) {
painters.get(i).draw(cues.get(i), applyEmbeddedStyles, style, textSizePx,
bottomPaddingFraction, canvas, left, top, right, bottom);
}
}
}
}

Expand Down

0 comments on commit 530977d

Please sign in to comment.