-
Notifications
You must be signed in to change notification settings - Fork 5
/
CaptionMerge.cs
165 lines (137 loc) · 3.07 KB
/
CaptionMerge.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using System;
namespace Subindex
{
public enum MergeType {ByTime,ByBlock,BySerial};
public class CaptionMerge
{
public CaptionMerge()
{
}
public bool isRepeatHead;
public bool isSecondTime;
public bool isSecondFront;
public MergeType MergeMode;
public TimeSpan TimeIgnore;
private Caption cap1;
public Caption Caption1
{
get{return cap1;}
set{cap1=value;}
}
private Caption cap2;
public Caption Caption2
{
get{return cap2;}
set{cap2=value;}
}
public void Switch()
{
Caption capTemp=cap1;
cap1=cap2;
cap2=capTemp;
}
public override string ToString()
{
string result="";
if (cap1!=null&&cap2!=null)
{
switch (MergeMode)
{
case MergeType.ByTime:
result=TimeMerge();
break;
case MergeType.ByBlock:
result=BlockMerge();
break;
case MergeType.BySerial:
result=SerialMerge();
break;
}
}
return result;//isSerialMerge?SerialMerge(isByBlock):CompoMerge(isByBlock);
}
private string SerialMerge()
{
return cap1.ToString() + cap2.ToString(cap1.Count,cap1.GetTimeLenth());
}
private string TimeMerge()
{
Caption ct=new Caption();
ct.AutoWrap=false;
int index1,index2;
int lockType;//0 no, 1 cap1, 2 cap2
lockType=index1=index2=0;
DateTime capTime1=new DateTime(),capTime2=new DateTime();
while (index1<cap1.Count||index2<cap2.Count)
{
Section st=new Section();
if (lockType!=1&&index1<cap1.Count) capTime1=cap1[index1].GetBeginTime();
if (lockType!=2&&index2<cap2.Count) capTime2=cap2[index2].GetBeginTime();
lockType=0;
if (capTime1>capTime2&&capTime1>capTime2.Add(TimeIgnore)&&index2<cap2.Count||index1==cap1.Count) lockType=1;
if (capTime2>capTime1&&capTime2>capTime1.Add(TimeIgnore)&&index1<cap1.Count||index2==cap2.Count) lockType=2;
if (lockType!=1)
{
st=cap1[index1++].Copy();
if (this.isRepeatHead||lockType==2) ct.Add(st);
}
if (lockType!=2)
{
addText(ref st,cap2[index2]);
ct.Add(st);
index2++;
}
}
return ct.ToString();
}
private void addText(ref Section st,Section sec)
{
if (this.isRepeatHead||st.Items.Count==0)
st=sec.Copy();
else
{
if (isSecondTime)
{
st.BeginTime=sec.BeginTime;
st.EndTime=sec.EndTime;
st.TimeBaseLine=sec.TimeBaseLine;
}
if (isSecondFront)
{
if (Pub.AutoWrap)
st.Items.Insert(0,sec.Text(true,false));
else
st.Items.InsertRange(0,sec.Items);
}
else
{
if (Pub.AutoWrap)
st.Items.Add(sec.Text(true,false));
else
st.Items.AddRange(sec.Items);
}
}
}
private string BlockMerge()
{
Caption ct=new Caption();
ct.AutoWrap=false;
int max=cap1.Count>cap2.Count?cap1.Count:cap2.Count;
for (int i=0;i<max;i++)
{
Section st=new Section();
if (i<cap1.Count)
{
st=cap1[i].Copy();
if (this.isRepeatHead||i>=cap2.Count) ct.Add(st);
}
if (i<cap2.Count)
{
addText(ref st,cap2[i]);
ct.Add(st);
}
}
return ct.ToString();
}
}
}