Fix typewriter effect with tags - thx OmrSi :)

Fix #3037
This commit is contained in:
Nikolaj Olsson 2018-08-19 20:12:03 +02:00
parent 54164b9e76
commit af313c78c1

View File

@ -11,7 +11,6 @@ namespace Nikse.SubtitleEdit.Forms
public sealed partial class EffectTypewriter : Form
{
private Paragraph _paragraph;
private List<Paragraph> _animation;
private int _timerCount;
public EffectTypewriter()
@ -29,13 +28,7 @@ namespace Nikse.SubtitleEdit.Forms
UiUtil.FixLargeFonts(this, buttonOK);
}
public List<Paragraph> TypewriterParagraphs
{
get
{
return _animation;
}
}
public List<Paragraph> TypewriterParagraphs { get; private set; }
private void FormEffectTypewriter_KeyDown(object sender, KeyEventArgs e)
{
@ -50,7 +43,7 @@ namespace Nikse.SubtitleEdit.Forms
AddToPreview(richTextBoxPreview, paragraph.Text);
RefreshPreview();
labelTotalMilliseconds.Text = string.Format("{0:#,##0.000}", paragraph.Duration.TotalMilliseconds / TimeCode.BaseUnit);
labelTotalMilliseconds.Text = $"{paragraph.Duration.TotalMilliseconds / TimeCode.BaseUnit:#,##0.000}";
numericUpDownDelay.Maximum = (decimal)((paragraph.Duration.TotalMilliseconds - 500) / TimeCode.BaseUnit);
numericUpDownDelay.Minimum = 0;
@ -207,13 +200,26 @@ namespace Nikse.SubtitleEdit.Forms
private static double CalculateStepLength(string text, double duration)
{
if (text.StartsWith("{\\", StringComparison.Ordinal) && text.IndexOf('}') > 2)
{
int i = 0;
while (i < text.Length &&
text.Substring(i).StartsWith("{\\", StringComparison.Ordinal) &&
text.Substring(i).IndexOf('}', i) > 2)
{
int idx = text.IndexOf('}', i);
i = idx + 1;
}
text = text.Remove(0, i);
}
text = HtmlUtil.RemoveHtmlTags(text);
return duration / text.Length;
}
private void MakeAnimation()
{
_animation = new List<Paragraph>();
TypewriterParagraphs = new List<Paragraph>();
double duration = _paragraph.Duration.TotalMilliseconds - ((double)numericUpDownDelay.Value * TimeCode.BaseUnit);
double stepsLength = CalculateStepLength(_paragraph.Text, duration);
@ -232,9 +238,16 @@ namespace Nikse.SubtitleEdit.Forms
{
if (i == 0 && _paragraph.Text.StartsWith("{\\", StringComparison.Ordinal) && _paragraph.Text.IndexOf('}') > 2)
{
int idx = _paragraph.Text.IndexOf('}');
alignment = _paragraph.Text.Substring(0, idx + 1);
i = idx;
int j = i;
while (j < _paragraph.Text.Length &&
_paragraph.Text.Substring(j).StartsWith("{\\", StringComparison.Ordinal) &&
_paragraph.Text.Substring(j).IndexOf('}', j) > 2)
{
int idx = _paragraph.Text.IndexOf('}', j);
i = idx;
j = i + 1;
}
alignment = _paragraph.Text.Substring(0, j);
}
else if (tagOn)
{
@ -273,11 +286,11 @@ namespace Nikse.SubtitleEdit.Forms
startMilliseconds = index * stepsLength;
startMilliseconds += _paragraph.StartTime.TotalMilliseconds;
endMilliseconds = ((index + 1) * stepsLength) - 1;
endMilliseconds = (index + 1) * stepsLength - 1;
endMilliseconds += _paragraph.StartTime.TotalMilliseconds;
start = new TimeCode(startMilliseconds);
end = new TimeCode(endMilliseconds);
_animation.Add(new Paragraph(start, end, alignment + text + beforeEndTag));
TypewriterParagraphs.Add(new Paragraph(start, end, alignment + text + beforeEndTag));
index++;
}
i++;
@ -290,11 +303,11 @@ namespace Nikse.SubtitleEdit.Forms
endMilliseconds = _paragraph.EndTime.TotalMilliseconds;
start = new TimeCode(startMilliseconds);
end = new TimeCode(endMilliseconds);
_animation.Add(new Paragraph(start, end, _paragraph.Text));
TypewriterParagraphs.Add(new Paragraph(start, end, _paragraph.Text));
}
else if (_animation.Count > 0)
else if (TypewriterParagraphs.Count > 0)
{
_animation[_animation.Count - 1].EndTime.TotalMilliseconds = _paragraph.EndTime.TotalMilliseconds;
TypewriterParagraphs[TypewriterParagraphs.Count - 1].EndTime.TotalMilliseconds = _paragraph.EndTime.TotalMilliseconds;
}
}
@ -302,7 +315,7 @@ namespace Nikse.SubtitleEdit.Forms
{
_timerCount += timer1.Interval;
string s = GetText(_timerCount, _animation);
string s = GetText(_timerCount, TypewriterParagraphs);
ClearPreview();
AddToPreview(richTextBoxPreview, s);
RefreshPreview();