Commit ef2d7c7f by Julian Cable

tests pass.

parent b692d9fa
...@@ -13,17 +13,21 @@ import java.text.SimpleDateFormat; ...@@ -13,17 +13,21 @@ import java.text.SimpleDateFormat;
public class SSATests extends InstrumentationTestCase { public class SSATests extends InstrumentationTestCase {
private static final String TYPICAL_FILE = "ssa/typical"; private static final String TYPICAL_FILE = "ssa/typical";
private static SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");
public void testTimeCodeConvert() throws IOException {
assertEquals("0:00:04.230", SSADecoder.formatTimeCode(SSADecoder.parseTimecode("0:00:04.23")));
}
public void testDecodeTypical() throws IOException { public void testDecodeTypical() throws IOException {
SSADecoder decoder = new SSADecoder(); SSADecoder decoder = new SSADecoder();
byte[] bytes = TestUtil.getByteArray(getInstrumentation(), TYPICAL_FILE); byte[] bytes = TestUtil.getByteArray(getInstrumentation(), TYPICAL_FILE);
SSASubtitle subtitle = decoder.decodeFile(bytes, bytes.length); SSASubtitle subtitle = decoder.decodeFile(bytes, bytes.length);
int n = subtitle.getEventTimeCount(); int n = subtitle.getEventTimeCount();
assertEquals(462, n); assertEquals(924, n); // includes end events
assertTypicalCue1(subtitle, 0); assertTypicalCue1(subtitle, 0);
assertTypicalCue2(subtitle, 2); assertTypicalCue2(subtitle, 2);
assertTypicalCue3(subtitle, 4); assertTypicalCue3(subtitle, 4);
assertTypicalCue4(subtitle, 6);
} }
/* /*
...@@ -34,27 +38,35 @@ public class SSATests extends InstrumentationTestCase { ...@@ -34,27 +38,35 @@ public class SSATests extends InstrumentationTestCase {
*/ */
private static void assertTypicalCue1(SSASubtitle subtitle, int eventIndex) { private static void assertTypicalCue1(SSASubtitle subtitle, int eventIndex) {
assertEquals("00:00:04.230", sdf.format(new java.util.Date(subtitle.getEventTime(eventIndex)))); assertEquals("0:00:04.230", SSADecoder.formatTimeCode(subtitle.getEventTime(eventIndex)));
assertEquals("The prince should be with the princess.", assertEquals("The prince should be with the princess.",
subtitle.getCues(subtitle.getEventTime(eventIndex)).get(0).text.toString()); subtitle.getCues(subtitle.getEventTime(eventIndex)).get(0).text.toString());
assertEquals("00:00:09.610", sdf.format(new java.util.Date(subtitle.getEventTime(eventIndex+1)))); assertEquals("0:00:06.900", SSADecoder.formatTimeCode(subtitle.getEventTime(eventIndex+1)));
} }
private static void assertTypicalCue2(SSASubtitle subtitle, int eventIndex) { private static void assertTypicalCue2(SSASubtitle subtitle, int eventIndex) {
assertEquals("00:00:33.010", sdf.format(new java.util.Date(subtitle.getEventTime(eventIndex)))); assertEquals("0:00:09.610", SSADecoder.formatTimeCode(subtitle.getEventTime(eventIndex)));
assertEquals("Kiss Him, Not Me", assertEquals("Who was the one who decided that?",
subtitle.getCues(subtitle.getEventTime(eventIndex)).get(0).text.toString()); subtitle.getCues(subtitle.getEventTime(eventIndex)).get(0).text.toString());
assertEquals("00:01:48.870", sdf.format(new java.util.Date(subtitle.getEventTime(eventIndex+1)))); assertEquals("0:00:13.200", SSADecoder.formatTimeCode(subtitle.getEventTime(eventIndex+1)));
} }
private static void assertTypicalCue3(SSASubtitle subtitle, int eventIndex) { private static void assertTypicalCue3(SSASubtitle subtitle, int eventIndex) {
String s1 = sdf.format(new java.util.Date(subtitle.getEventTime(eventIndex))); assertEquals("0:00:33.010", SSADecoder.formatTimeCode(subtitle.getEventTime(eventIndex)));
String s2 = sdf.format(new java.util.Date(subtitle.getEventTime(eventIndex+1))); assertEquals("Kiss Him, Not Me",
subtitle.getCues(subtitle.getEventTime(eventIndex)).get(0).text.toString());
assertEquals("0:00:41.770", SSADecoder.formatTimeCode(subtitle.getEventTime(eventIndex+1)));
}
private static void assertTypicalCue4(SSASubtitle subtitle, int eventIndex) {
String s1 = SSADecoder.formatTimeCode(subtitle.getEventTime(eventIndex));
String s2 = SSADecoder.formatTimeCode(subtitle.getEventTime(eventIndex+1));
String s3 = String s3 =
subtitle.getCues(subtitle.getEventTime(eventIndex)).get(0).text.toString(); subtitle.getCues(subtitle.getEventTime(eventIndex)).get(0).text.toString();
assertEquals("00:01:59.610", sdf.format(new java.util.Date(subtitle.getEventTime(eventIndex)))); assertEquals("0:01:48.870", SSADecoder.formatTimeCode(subtitle.getEventTime(eventIndex)));
assertEquals("Nice one, Igarashi!", assertEquals("Can She Do It? A Real Life Otome Game",
subtitle.getCues(subtitle.getEventTime(eventIndex)).get(0).text.toString()); subtitle.getCues(subtitle.getEventTime(eventIndex)).get(0).text.toString());
assertEquals("00:02:01.220", sdf.format(new java.util.Date(subtitle.getEventTime(eventIndex+1)))); assertEquals("0:01:54.380", SSADecoder.formatTimeCode(subtitle.getEventTime(eventIndex+1)));
} }
} }
...@@ -11,6 +11,8 @@ import java.util.HashMap; ...@@ -11,6 +11,8 @@ import java.util.HashMap;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import static android.R.attr.subtitle;
/** /**
* Created by cablej01 on 26/12/2016. * Created by cablej01 on 26/12/2016.
*/ */
...@@ -59,7 +61,7 @@ import java.util.Map; ...@@ -59,7 +61,7 @@ import java.util.Map;
public class SSADecoder extends SimpleSubtitleDecoder { public class SSADecoder extends SimpleSubtitleDecoder {
private static final String TAG = "SSADecoder"; private static final String TAG = "SSADecoder";
private static String defaultDialogueFormat = "Start, End, , Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text"; private static String defaultDialogueFormat = "Start, End, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text";
private static String defaultStyleFormat = "Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding"; private static String defaultStyleFormat = "Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding";
private String[] dialogueFormat; private String[] dialogueFormat;
private String[] styleFormat; private String[] styleFormat;
...@@ -99,21 +101,18 @@ public class SSADecoder extends SimpleSubtitleDecoder { ...@@ -99,21 +101,18 @@ public class SSADecoder extends SimpleSubtitleDecoder {
decodeHeader(data); decodeHeader(data);
String currentLine; String currentLine;
while ((currentLine = data.readLine()) != null) { while ((currentLine = data.readLine()) != null) {
while(true) { if(currentLine==null)
currentLine = data.readLine(); break;
if(currentLine==null) Log.i(TAG, currentLine);
break; if(!currentLine.contains(":"))
Log.i(TAG, currentLine); break;
if(!currentLine.contains(":")) String p[] = currentLine.split(":",2);
break; if(p[0].equals("Format")) {
String p[] = currentLine.split(":",2); dialogueFormat = parseKeys(p[1]);
if(p[0].equals("Format")) { }
dialogueFormat = parseKeys(p[1]); else if(p[0].equals("Dialogue")) {
} Map<String,String> ev = parseLine(dialogueFormat, p[1].trim());
else if(p[0].equals("Dialogue")) { subtitle.addEvent(ev, styles);
Map<String,String> ev = parseLine(dialogueFormat, p[1].trim());
subtitle.addEvent(ev, styles);
}
} }
} }
return subtitle; return subtitle;
...@@ -176,7 +175,7 @@ public class SSADecoder extends SimpleSubtitleDecoder { ...@@ -176,7 +175,7 @@ public class SSADecoder extends SimpleSubtitleDecoder {
public static Map<String,String> parseLine(String[] keys, String event) { public static Map<String,String> parseLine(String[] keys, String event) {
Map<String,String> result = new HashMap<>(); Map<String,String> result = new HashMap<>();
String fields[] = event.split(", *", keys.length); String fields[] = event.split(", *", keys.length);
for(int i=0; i<keys.length; i++) { for(int i=0; i<fields.length; i++) {
String k = keys[i]; String k = keys[i];
String v = fields[i].trim(); String v = fields[i].trim();
result.put(k, v); result.put(k, v);
......
...@@ -61,10 +61,18 @@ public class SSASubtitle implements Subtitle { ...@@ -61,10 +61,18 @@ public class SSASubtitle implements Subtitle {
} }
protected void addEvent(Map<String,String> ev, Map<String,Style> styles) { protected void addEvent(Map<String,String> ev, Map<String,Style> styles) {
// int readOrder = Integer.parseInt(ev.get("readorder")); ? not needed int marginL = 0;
int marginL = Integer.parseInt(ev.get("marginl")); String m = ev.get("marginl");
int marginR = Integer.parseInt(ev.get("marginr")); if(!m.equals(""))
int marginV = Integer.parseInt(ev.get("marginv")); marginL = Integer.parseInt(m);
int marginR = 0;
m = ev.get("marginr");
if(!m.equals(""))
marginR = Integer.parseInt(m);
int marginV = 0;
m = ev.get("marginv");
if(!m.equals(""))
marginV = Integer.parseInt(m);
String styleName = ev.get("style"); String styleName = ev.get("style");
Style style = styles.get(styleName); Style style = styles.get(styleName);
if(marginL!=0 || marginR!=0 || marginV !=0) { if(marginL!=0 || marginR!=0 || marginV !=0) {
......
...@@ -2,6 +2,8 @@ package com.google.android.exoplayer2.text.ssa; ...@@ -2,6 +2,8 @@ package com.google.android.exoplayer2.text.ssa;
import java.util.Map; import java.util.Map;
import static android.R.attr.angle;
/** /**
* Created by cablej01 on 27/12/2016. * Created by cablej01 on 27/12/2016.
*/ */
...@@ -12,9 +14,11 @@ public class Style { ...@@ -12,9 +14,11 @@ public class Style {
private int fontSize; private int fontSize;
private int primaryColour, secondaryColour, outlineColour, backColour; private int primaryColour, secondaryColour, outlineColour, backColour;
private boolean bold, italic, underline, strikeOut; private boolean bold, italic, underline, strikeOut;
private int scaleX, scaleY, spacing, angle; private int scaleX, scaleY, spacing;
private float angle;
private int borderStyle; private int borderStyle;
private int outline, shadow, alignment, marginL, marginR, marginV; private float outline;
private int shadow, alignment, marginL, marginR, marginV;
private int alphaLevel=0; private int alphaLevel=0;
private int encoding; private int encoding;
...@@ -30,16 +34,16 @@ public class Style { ...@@ -30,16 +34,16 @@ public class Style {
secondaryColour = parseColour(init.get("secondarycolour")); secondaryColour = parseColour(init.get("secondarycolour"));
outlineColour = parseColour(init.get("outlinecolour")); outlineColour = parseColour(init.get("outlinecolour"));
backColour = parseColour(init.get("backcolour")); backColour = parseColour(init.get("backcolour"));
bold = init.get("bold").equals("0")?false:true; bold = !init.get("bold").equals("0");
italic = init.get("italic").equals("0")?false:true; italic = !init.get("italic").equals("0");
underline = init.get("underline").equals("0")?false:true; underline = !init.get("underline").equals("0");
strikeOut = init.get("strikeout").equals("0")?false:true; strikeOut = !init.get("strikeout").equals("0");
scaleX = Integer.parseInt(init.get("scalex")); scaleX = Integer.parseInt(init.get("scalex"));
scaleY = Integer.parseInt(init.get("scaley")); scaleY = Integer.parseInt(init.get("scaley"));
spacing = Integer.parseInt(init.get("spacing")); spacing = Integer.parseInt(init.get("spacing"));
angle = Integer.parseInt(init.get("angle")); angle = Float.parseFloat(init.get("angle"));
borderStyle = Integer.parseInt(init.get("borderstyle")); borderStyle = Integer.parseInt(init.get("borderstyle"));
outline = Integer.parseInt(init.get("outline")); outline = Float.parseFloat(init.get("outline"));
shadow = Integer.parseInt(init.get("shadow")); shadow = Integer.parseInt(init.get("shadow"));
alignment = Integer.parseInt(init.get("alignment")); alignment = Integer.parseInt(init.get("alignment"));
marginL = Integer.parseInt(init.get("marginl")); marginL = Integer.parseInt(init.get("marginl"));
...@@ -197,11 +201,11 @@ public class Style { ...@@ -197,11 +201,11 @@ public class Style {
this.spacing = spacing; this.spacing = spacing;
} }
public int getAngle() { public float getAngle() {
return angle; return angle;
} }
public void setAngle(int angle) { public void setAngle(float angle) {
this.angle = angle; this.angle = angle;
} }
...@@ -213,11 +217,11 @@ public class Style { ...@@ -213,11 +217,11 @@ public class Style {
this.borderStyle = borderStyle; this.borderStyle = borderStyle;
} }
public int getOutline() { public float getOutline() {
return outline; return outline;
} }
public void setOutline(int outline) { public void setOutline(float outline) {
this.outline = outline; this.outline = outline;
} }
......
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