From ef2d7c7fd50334dbbfe70049a1be379f386533f4 Mon Sep 17 00:00:00 2001 From: Julian Cable Date: Mon, 2 Jan 2017 15:49:30 +0000 Subject: [PATCH] tests pass. --- .../android/exoplayer2/text/ssa/SSATests.java | 36 ++++++++++++------- .../exoplayer2/text/ssa/SSADecoder.java | 33 +++++++++-------- .../exoplayer2/text/ssa/SSASubtitle.java | 16 ++++++--- .../android/exoplayer2/text/ssa/Style.java | 28 ++++++++------- 4 files changed, 68 insertions(+), 45 deletions(-) diff --git a/library/src/androidTest/java/com/google/android/exoplayer2/text/ssa/SSATests.java b/library/src/androidTest/java/com/google/android/exoplayer2/text/ssa/SSATests.java index d15bdbe07e..a245d2710d 100644 --- a/library/src/androidTest/java/com/google/android/exoplayer2/text/ssa/SSATests.java +++ b/library/src/androidTest/java/com/google/android/exoplayer2/text/ssa/SSATests.java @@ -13,17 +13,21 @@ import java.text.SimpleDateFormat; public class SSATests extends InstrumentationTestCase { 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 { SSADecoder decoder = new SSADecoder(); byte[] bytes = TestUtil.getByteArray(getInstrumentation(), TYPICAL_FILE); SSASubtitle subtitle = decoder.decodeFile(bytes, bytes.length); int n = subtitle.getEventTimeCount(); - assertEquals(462, n); + assertEquals(924, n); // includes end events assertTypicalCue1(subtitle, 0); assertTypicalCue2(subtitle, 2); assertTypicalCue3(subtitle, 4); + assertTypicalCue4(subtitle, 6); } /* @@ -34,27 +38,35 @@ public class SSATests extends InstrumentationTestCase { */ 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.", 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) { - assertEquals("00:00:33.010", sdf.format(new java.util.Date(subtitle.getEventTime(eventIndex)))); - assertEquals("Kiss Him, Not Me", + assertEquals("0:00:09.610", SSADecoder.formatTimeCode(subtitle.getEventTime(eventIndex))); + assertEquals("Who was the one who decided that?", 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) { - String s1 = sdf.format(new java.util.Date(subtitle.getEventTime(eventIndex))); - String s2 = sdf.format(new java.util.Date(subtitle.getEventTime(eventIndex+1))); + assertEquals("0:00:33.010", SSADecoder.formatTimeCode(subtitle.getEventTime(eventIndex))); + 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 = subtitle.getCues(subtitle.getEventTime(eventIndex)).get(0).text.toString(); - assertEquals("00:01:59.610", sdf.format(new java.util.Date(subtitle.getEventTime(eventIndex)))); - assertEquals("Nice one, Igarashi!", + assertEquals("0:01:48.870", SSADecoder.formatTimeCode(subtitle.getEventTime(eventIndex))); + assertEquals("Can She Do It? A Real Life Otome Game", 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))); } } diff --git a/library/src/main/java/com/google/android/exoplayer2/text/ssa/SSADecoder.java b/library/src/main/java/com/google/android/exoplayer2/text/ssa/SSADecoder.java index 542516ce7a..5a76f0c91c 100644 --- a/library/src/main/java/com/google/android/exoplayer2/text/ssa/SSADecoder.java +++ b/library/src/main/java/com/google/android/exoplayer2/text/ssa/SSADecoder.java @@ -11,6 +11,8 @@ import java.util.HashMap; import java.util.Locale; import java.util.Map; +import static android.R.attr.subtitle; + /** * Created by cablej01 on 26/12/2016. */ @@ -59,7 +61,7 @@ import java.util.Map; public class SSADecoder extends SimpleSubtitleDecoder { 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 String[] dialogueFormat; private String[] styleFormat; @@ -99,21 +101,18 @@ public class SSADecoder extends SimpleSubtitleDecoder { decodeHeader(data); String currentLine; while ((currentLine = data.readLine()) != null) { - while(true) { - currentLine = data.readLine(); - if(currentLine==null) - break; - Log.i(TAG, currentLine); - if(!currentLine.contains(":")) - break; - String p[] = currentLine.split(":",2); - if(p[0].equals("Format")) { - dialogueFormat = parseKeys(p[1]); - } - else if(p[0].equals("Dialogue")) { - Map ev = parseLine(dialogueFormat, p[1].trim()); - subtitle.addEvent(ev, styles); - } + if(currentLine==null) + break; + Log.i(TAG, currentLine); + if(!currentLine.contains(":")) + break; + String p[] = currentLine.split(":",2); + if(p[0].equals("Format")) { + dialogueFormat = parseKeys(p[1]); + } + else if(p[0].equals("Dialogue")) { + Map ev = parseLine(dialogueFormat, p[1].trim()); + subtitle.addEvent(ev, styles); } } return subtitle; @@ -176,7 +175,7 @@ public class SSADecoder extends SimpleSubtitleDecoder { public static Map parseLine(String[] keys, String event) { Map result = new HashMap<>(); String fields[] = event.split(", *", keys.length); - for(int i=0; i ev, Map styles) { - // int readOrder = Integer.parseInt(ev.get("readorder")); ? not needed - int marginL = Integer.parseInt(ev.get("marginl")); - int marginR = Integer.parseInt(ev.get("marginr")); - int marginV = Integer.parseInt(ev.get("marginv")); + int marginL = 0; + String m = ev.get("marginl"); + if(!m.equals("")) + 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"); Style style = styles.get(styleName); if(marginL!=0 || marginR!=0 || marginV !=0) { diff --git a/library/src/main/java/com/google/android/exoplayer2/text/ssa/Style.java b/library/src/main/java/com/google/android/exoplayer2/text/ssa/Style.java index 2cb7415918..9f4839a349 100644 --- a/library/src/main/java/com/google/android/exoplayer2/text/ssa/Style.java +++ b/library/src/main/java/com/google/android/exoplayer2/text/ssa/Style.java @@ -2,6 +2,8 @@ package com.google.android.exoplayer2.text.ssa; import java.util.Map; +import static android.R.attr.angle; + /** * Created by cablej01 on 27/12/2016. */ @@ -12,9 +14,11 @@ public class Style { private int fontSize; private int primaryColour, secondaryColour, outlineColour, backColour; 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 outline, shadow, alignment, marginL, marginR, marginV; + private float outline; + private int shadow, alignment, marginL, marginR, marginV; private int alphaLevel=0; private int encoding; @@ -30,16 +34,16 @@ public class Style { secondaryColour = parseColour(init.get("secondarycolour")); outlineColour = parseColour(init.get("outlinecolour")); backColour = parseColour(init.get("backcolour")); - bold = init.get("bold").equals("0")?false:true; - italic = init.get("italic").equals("0")?false:true; - underline = init.get("underline").equals("0")?false:true; - strikeOut = init.get("strikeout").equals("0")?false:true; + bold = !init.get("bold").equals("0"); + italic = !init.get("italic").equals("0"); + underline = !init.get("underline").equals("0"); + strikeOut = !init.get("strikeout").equals("0"); scaleX = Integer.parseInt(init.get("scalex")); scaleY = Integer.parseInt(init.get("scaley")); spacing = Integer.parseInt(init.get("spacing")); - angle = Integer.parseInt(init.get("angle")); + angle = Float.parseFloat(init.get("angle")); borderStyle = Integer.parseInt(init.get("borderstyle")); - outline = Integer.parseInt(init.get("outline")); + outline = Float.parseFloat(init.get("outline")); shadow = Integer.parseInt(init.get("shadow")); alignment = Integer.parseInt(init.get("alignment")); marginL = Integer.parseInt(init.get("marginl")); @@ -197,11 +201,11 @@ public class Style { this.spacing = spacing; } - public int getAngle() { + public float getAngle() { return angle; } - public void setAngle(int angle) { + public void setAngle(float angle) { this.angle = angle; } @@ -213,11 +217,11 @@ public class Style { this.borderStyle = borderStyle; } - public int getOutline() { + public float getOutline() { return outline; } - public void setOutline(int outline) { + public void setOutline(float outline) { this.outline = outline; }