Skip to content

Commit

Permalink
#60 Add support for ASS "Collisions" attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
www committed Mar 19, 2022
1 parent 2a6765f commit 32369b3
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 1 deletion.
6 changes: 6 additions & 0 deletions YTSubConverter.Shared/Enumerations.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
namespace YTSubConverter.Shared
{
public enum LineMergeType
{
MoveNew,
MoveExisting
}

public enum AnchorPoint
{
TopLeft,
Expand Down
1 change: 1 addition & 0 deletions YTSubConverter.Shared/Formats/Ass/AssDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ private void Load(TextReader reader, List<AssStyleOptions> styleOptions)

AssDocumentSection infoSection = fileSections["Script Info"];
VideoDimensions = new Size(infoSection.GetItemInt("PlayResX", 384), infoSection.GetItemInt("PlayResY", 288));
LineMergeType = infoSection.GetItemString("Collisions") == "Reverse" ? LineMergeType.MoveExisting : LineMergeType.MoveNew;

_styles = fileSections["V4+ Styles"].MapItems("Style", i => new AssStyle(i))
.ToDictionaryOverwrite(s => s.Name);
Expand Down
10 changes: 9 additions & 1 deletion YTSubConverter.Shared/Formats/SubtitleDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ public abstract class SubtitleDocument
protected SubtitleDocument()
{
VideoDimensions = new Size(1280, 720);
LineMergeType = LineMergeType.MoveNew;
}

protected SubtitleDocument(SubtitleDocument doc)
{
VideoDimensions = doc.VideoDimensions;
LineMergeType = doc.LineMergeType;
Lines.AddRange(doc.Lines.Where(l => l.Sections.Any(s => s.Text.Length > 0)));
}

Expand All @@ -32,6 +34,12 @@ public Size VideoDimensions
protected set;
}

public LineMergeType LineMergeType
{
get;
protected set;
}

public List<Line> Lines { get; } = new List<Line>();

public static bool IsExtensionSupported(string extension)
Expand Down Expand Up @@ -143,7 +151,7 @@ public void MergeSimultaneousLines()
if (firstLine.Start < secondLine.Start)
InsertConcatenedLine(lines, i, firstLine.Start, secondLine.Start, false, firstLine);

if (AnchorPointUtil.IsBottomAligned(firstLine.AnchorPoint))
if (AnchorPointUtil.IsBottomAligned(firstLine.AnchorPoint) ^ (LineMergeType == LineMergeType.MoveExisting))
InsertConcatenedLine(lines, i, secondLine.Start, TimeUtil.Min(firstLine.End, secondLine.End), false, secondLine, firstLine);
else
InsertConcatenedLine(lines, i, secondLine.Start, TimeUtil.Min(firstLine.End, secondLine.End), false, firstLine, secondLine);
Expand Down
27 changes: 27 additions & 0 deletions YTSubConverter.Tests/Ass/Files/SimultaneousReverse.ass
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[Script Info]
; Script generated by Aegisub 3.3.0
; http://www.aegisub.org/
Title: Default Aegisub file
ScriptType: v4.00+
WrapStyle: 0
ScaledBorderAndShadow: yes
PlayResX: 384
PlayResY: 288
YCbCr Matrix: None
Collisions: Reverse

[Aegisub Project Garbage]
Last Style Storage: Default
Video AR Value: 1.333333
Video Position: 129

[V4+ Styles]
Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
Style: Default,Roboto,15,&H00FFFFFF,&HFFFFFFFF,&H3C000000,&H00000000,0,0,0,0,100,100,0,0,3,2,0,2,10,10,10,1
Style: Soft shadow box,Roboto,15,&H00FFFFFF,&HFF0000FF,&H3C000000,&H00828282,0,0,0,0,100,100,0,0,3,2,2,2,10,10,10,1

[Events]
Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
Dialogue: 0,0:00:00.00,0:00:04.37,Default,,0,0,0,,Line 1
Dialogue: 0,0:00:00.60,0:00:03.70,Default,,0,0,0,,Line 2
Dialogue: 0,0:00:01.40,0:00:02.37,Default,,0,0,0,,Line 3
5 changes: 5 additions & 0 deletions YTSubConverter.Tests/Ass/Files/SimultaneousReverse.ytt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?><timedtext format="3"><head><wp id="0" ap="7" ah="0" av="0" /><wp id="1" ap="7" ah="50" av="100" /><ws id="0" ju="2" pd="0" sd="0" /><ws id="1" ju="2" pd="0" sd="0" /><pen id="0" sz="100" fc="#000000" fo="0" bo="0" /><pen id="1" sz="0" fc="#A0AAB4" fo="0" bo="0" /><pen id="2" sz="100" fc="#FEFEFE" fo="254" bc="#000000" bo="195" /></head><body><p t="1" d="616" wp="1" ws="1"><s p="1">​</s>​<s p="2">​ ​Line 1​ ​</s><s p="1">​</s></p><p t="617" d="768" wp="1" ws="1"><s p="1">​</s>​<s p="2">​ ​Line 1​ ​</s><s p="1">​
​</s><s p="2">​ ​Line 2​ ​</s><s p="1">​</s></p><p t="1385" d="1001" wp="1" ws="1"><s p="1">​</s>​<s p="2">​ ​Line 1​ ​</s><s p="1">​
​</s><s p="2">​ ​Line 2​ ​</s><s p="1">​
​</s><s p="2">​ ​Line 3​ ​</s><s p="1">​</s></p><p t="2386" d="1301" wp="1" ws="1"><s p="1">​</s>​<s p="2">​ ​Line 1​ ​</s><s p="1">​
​</s><s p="2">​ ​Line 2​ ​</s><s p="1">​</s></p><p t="3687" d="668" wp="1" ws="1"><s p="1">​</s>​<s p="2">​ ​Line 1​ ​</s><s p="1">​</s></p></body></timedtext>

0 comments on commit 32369b3

Please sign in to comment.