diff --git a/src/Forms/FixCommonErrors.cs b/src/Forms/FixCommonErrors.cs index c35997be0..5a22f8450 100644 --- a/src/Forms/FixCommonErrors.cs +++ b/src/Forms/FixCommonErrors.cs @@ -574,19 +574,7 @@ namespace Nikse.SubtitleEdit.Forms if (noOfShortDisplayTimes > 0) LogStatus(fixAction, string.Format(_language.XDisplayTimesProlonged, noOfShortDisplayTimes)); } - - public static int CountTagInText(string text, string tag) - { - int count = 0; - int index = text.IndexOf(tag); - while (index >= 0) - { - count++; - index = text.IndexOf(tag, index + 1); - } - return count; - } - + public void FixInvalidItalicTags() { const string beginTag = ""; @@ -598,100 +586,18 @@ namespace Nikse.SubtitleEdit.Forms string text = _subtitle.Paragraphs[i].Text.Replace(beginTag.ToUpper(), beginTag).Replace(endTag.ToUpper(), endTag); string oldText = text; - text = text.Replace("< i>", beginTag); - text = text.Replace("", beginTag); - text = text.Replace("< I>", beginTag); - text = text.Replace("", beginTag); - - text = text.Replace("< /i>", endTag); - text = text.Replace("", endTag); - text = text.Replace("< /i>", endTag); - text = text.Replace("< /I>", endTag); - text = text.Replace("", endTag); - text = text.Replace("< /I>", endTag); - - if (text.Contains(beginTag)) - text = text.Replace("", endTag); - else - text = text.Replace("", string.Empty); - - text = text.Replace(beginTag + beginTag, beginTag); - text = text.Replace(endTag + endTag, endTag); - - int italicBeginTagCount = CountTagInText(text, beginTag); - int italicEndTagCount = CountTagInText(text, endTag); - int noOfLines = CountTagInText(text, Environment.NewLine) + 1; - if (italicBeginTagCount + italicEndTagCount > 0) + text = Utilities.FixInvalidItalicTags(text); + if (text != oldText) { - if (italicBeginTagCount == 1 && italicEndTagCount == 1) + if (AllowFix(i + 1, fixAction)) { - if (text.IndexOf(beginTag) > text.IndexOf(endTag)) - { - text = text.Replace(beginTag, "___________@"); - text = text.Replace(endTag, beginTag); - text = text.Replace("___________@", endTag); - } - } - - if (italicBeginTagCount == 2 && italicEndTagCount == 0) - { - int firstIndex = text.IndexOf(beginTag); - int lastIndex = text.LastIndexOf(beginTag); - int lastIndexWithNewLine = text.LastIndexOf(Environment.NewLine+ beginTag) + Environment.NewLine.Length; - if (noOfLines == 2 && lastIndex == lastIndexWithNewLine && firstIndex < 2) - text = text.Replace(Environment.NewLine, "" + Environment.NewLine) + ""; - else if (text.Length > lastIndex + endTag.Length) - text = text.Substring(0, lastIndex) + endTag + text.Substring(lastIndex -1 + endTag.Length); - else - text = text.Substring(0, lastIndex) + endTag; - } - - if (italicBeginTagCount == 1 && italicEndTagCount == 2) - { - int firstIndex = text.IndexOf(endTag); - text = text.Substring(0, firstIndex - 1) + text.Substring(firstIndex + endTag.Length); - } - - if (italicBeginTagCount == 2 && italicEndTagCount == 1) - { - int lastIndex = text.LastIndexOf(beginTag); - if (text.Length > lastIndex + endTag.Length) - text = text.Substring(0, lastIndex) + text.Substring(lastIndex - 1 + endTag.Length); - else - text = text.Substring(0, lastIndex - 1) + endTag; - } - - if (italicBeginTagCount == 1 && italicEndTagCount == 0) - { - int lastIndexWithNewLine = text.LastIndexOf(Environment.NewLine + beginTag) + Environment.NewLine.Length; - int lastIndex = text.LastIndexOf(beginTag); - - if (text.StartsWith(beginTag)) - text += endTag; - else if (noOfLines == 2 && lastIndex == lastIndexWithNewLine) - text += endTag; - else - text = text.Replace(beginTag, string.Empty); - } - - if (italicBeginTagCount == 0 && italicEndTagCount == 1) - { - text = text.Replace(endTag, string.Empty); - } - - text = text.Replace("", string.Empty); - - if (text != oldText) - { - if (AllowFix(i + 1, fixAction)) - { - _subtitle.Paragraphs[i].Text = text; - _totalFixes++; - noOfInvalidHtmlTags++; - AddFixToListView(_subtitle.Paragraphs[i], i + 1, fixAction, oldText, text); - } + _subtitle.Paragraphs[i].Text = text; + _totalFixes++; + noOfInvalidHtmlTags++; + AddFixToListView(_subtitle.Paragraphs[i], i + 1, fixAction, oldText, text); } } + } if (noOfInvalidHtmlTags > 0) LogStatus(_language.FixInvalidItalicTags, string.Format(_language.XInvalidHtmlTagsFixed, noOfInvalidHtmlTags)); @@ -1210,8 +1116,8 @@ namespace Nikse.SubtitleEdit.Forms int noOfFixes = 0; for (int i = 0; i < _subtitle.Paragraphs.Count; i++) { - Paragraph p = _subtitle.Paragraphs[i]; - if (CountTagInText(p.Text, "\"") == 1) + Paragraph p = _subtitle.Paragraphs[i]; + if (Utilities.CountTagInText(p.Text, "\"") == 1) { string oldText = p.Text; if (p.Text.StartsWith("\"")) @@ -1956,7 +1862,7 @@ namespace Nikse.SubtitleEdit.Forms if (prev == null || !Utilities.RemoveHtmlTags(prev.Text).Trim().EndsWith("-")) { - if (CountTagInText(text, "-") == 1) + if (Utilities.CountTagInText(text, "-") == 1) { string oldText = p.Text; @@ -1992,7 +1898,7 @@ namespace Nikse.SubtitleEdit.Forms Paragraph p = _subtitle.Paragraphs[i]; string text = p.Text; - if (CountTagInText(text, Environment.NewLine) > 1) + if (Utilities.CountTagInText(text, Environment.NewLine) > 1) { string oldText = p.Text; text = Utilities.AutoBreakLine(text); diff --git a/src/Forms/Main.Designer.cs b/src/Forms/Main.Designer.cs index df0dbe9d3..77ded31e7 100644 --- a/src/Forms/Main.Designer.cs +++ b/src/Forms/Main.Designer.cs @@ -1558,7 +1558,7 @@ this.fixCommonErrorsInSelectedLinesToolStripMenuItem, this.changeCasingForSelectedLinesToolStripMenuItem}); this.contextMenuStripListview.Name = "contextMenuStripListview"; - this.contextMenuStripListview.Size = new System.Drawing.Size(276, 562); + this.contextMenuStripListview.Size = new System.Drawing.Size(276, 584); this.contextMenuStripListview.Opening += new System.ComponentModel.CancelEventHandler(this.ContextMenuStripListviewOpening); // // toolStripMenuItemDelete diff --git a/src/Forms/Main.cs b/src/Forms/Main.cs index fec11d274..795c141f4 100644 --- a/src/Forms/Main.cs +++ b/src/Forms/Main.cs @@ -2952,10 +2952,8 @@ namespace Nikse.SubtitleEdit.Forms toolStripMenuItemInsertAfter.Visible = false; toolStripMenuItemInsertSubtitle.Visible = false; splitLineToolStripMenuItem.Visible = false; - toolStripMenuItemMergeLines.Visible = false; mergeAfterToolStripMenuItem.Visible = false; mergeBeforeToolStripMenuItem.Visible = false; - toolStripMenuItemMergeLines.Visible = false; typeEffectToolStripMenuItem.Visible = false; toolStripSeparator7.Visible = false; } @@ -3575,10 +3573,95 @@ namespace Nikse.SubtitleEdit.Forms } } + private void MergeSelectedLines() + { + if (_subtitle.Paragraphs.Count > 0 && SubtitleListview1.SelectedItems.Count > 1) + { + StringBuilder sb = new StringBuilder(); + List mergeParagraphs = new List(); + List deleteIndices = new List(); + bool first = true; + int firstIndex = 0; + foreach (int index in SubtitleListview1.SelectedIndices) + { + if (first) + firstIndex = index; + else + deleteIndices.Add(index); + sb.AppendLine(_subtitle.Paragraphs[index].Text); + first = false; + } + + + if (sb.Length > 200) + return; + + SubtitleListview1.SelectedIndexChanged -= SubtitleListview1_SelectedIndexChanged; + MakeHistoryForUndo(_language.BeforeMergeLines); + + Paragraph currentParagraph = _subtitle.Paragraphs[firstIndex]; + string text = sb.ToString(); + text = Utilities.FixInvalidItalicTags(text); + text = ChangeAllLinesItalictoSingleItalic(text); + text = Utilities.AutoBreakLine(text); + currentParagraph.Text = text; + + //display time + currentParagraph.EndTime.TotalMilliseconds = currentParagraph.StartTime.TotalMilliseconds + Utilities.GetDisplayMillisecondsFromText(text); + Paragraph nextParagraph = _subtitle.GetParagraphOrDefault(_subtitle.GetIndex(currentParagraph) + 1); + if (nextParagraph != null && currentParagraph.EndTime.TotalMilliseconds > nextParagraph.StartTime.TotalMilliseconds && currentParagraph.StartTime.TotalMilliseconds < nextParagraph.StartTime.TotalMilliseconds) + { + currentParagraph.EndTime.TotalMilliseconds = nextParagraph.StartTime.TotalMilliseconds - 1; + } + + if (_networkSession != null) + { + _networkSession.TimerStop(); + _networkSession.UpdateLine(firstIndex, currentParagraph); + NetworkGetSendUpdates(deleteIndices, 0, null); + } + else + { + for (int i = deleteIndices.Count - 1; i >= 0; i--) + _subtitle.Paragraphs.RemoveAt(deleteIndices[i]); + _subtitle.Renumber(1); + SubtitleListview1.Fill(_subtitle, _subtitleAlternate); + } + ShowSource(); + ShowStatus(_language.LinesMerged); + SubtitleListview1.SelectIndexAndEnsureVisible(firstIndex); + SubtitleListview1.SelectedIndexChanged += SubtitleListview1_SelectedIndexChanged; + RefreshSelectedParagraph(); + _change = true; + } + } + + private static string ChangeAllLinesItalictoSingleItalic(string text) + { + bool allLinesStartAndEndsWithItalic = true; + foreach (string line in text.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries)) + { + if (!line.Trim().StartsWith("") || !line.Trim().EndsWith("")) + allLinesStartAndEndsWithItalic = false; + } + if (allLinesStartAndEndsWithItalic) + { + text = text.Replace("", string.Empty).Replace("", string.Empty).Trim(); + text = "" + text + ""; + } + return text; + } + private void MergeAfterToolStripMenuItemClick(object sender, EventArgs e) { if (_subtitle.Paragraphs.Count > 0 && SubtitleListview1.SelectedItems.Count > 0) { + if (SubtitleListview1.SelectedItems.Count > 2) + { + MergeSelectedLines(); + return; + } + int startNumber = _subtitle.Paragraphs[0].Number; int firstSelectedIndex = SubtitleListview1.SelectedItems[0].Index; @@ -3592,8 +3675,9 @@ namespace Nikse.SubtitleEdit.Forms currentParagraph.Text = currentParagraph.Text.Replace(Environment.NewLine, " "); currentParagraph.Text += Environment.NewLine + nextParagraph.Text.Replace(Environment.NewLine, " "); + currentParagraph.Text = ChangeAllLinesItalictoSingleItalic(currentParagraph.Text); currentParagraph.Text = Utilities.AutoBreakLine(currentParagraph.Text); - + //currentParagraph.EndTime.TotalMilliseconds = currentParagraph.EndTime.TotalMilliseconds + nextParagraph.Duration.TotalMilliseconds; //nextParagraph.EndTime; currentParagraph.EndTime.TotalMilliseconds = nextParagraph.EndTime.TotalMilliseconds; @@ -4605,7 +4689,7 @@ namespace Nikse.SubtitleEdit.Forms private void ToolStripMenuItemMergeLinesClick(object sender, EventArgs e) { - if (_subtitle.Paragraphs.Count > 0 && SubtitleListview1.SelectedItems.Count >= 1 && SubtitleListview1.SelectedItems.Count <= 2) + if (_subtitle.Paragraphs.Count > 0 && SubtitleListview1.SelectedItems.Count >= 1) MergeAfterToolStripMenuItemClick(null, null); } @@ -4750,10 +4834,13 @@ namespace Nikse.SubtitleEdit.Forms } else if (e.Modifiers == (Keys.Control | Keys.Shift) && e.KeyCode == Keys.M) { - if (_subtitle.Paragraphs.Count > 0 && SubtitleListview1.SelectedItems.Count >= 1 && SubtitleListview1.SelectedItems.Count <= 2) + if (_subtitle.Paragraphs.Count > 0 && SubtitleListview1.SelectedItems.Count >= 1) { e.SuppressKeyPress = true; - MergeAfterToolStripMenuItemClick(null, null); + if (SubtitleListview1.SelectedItems.Count == 2) + MergeAfterToolStripMenuItemClick(null, null); + else + MergeSelectedLines(); } } else if (e.Modifiers == Keys.Control && e.KeyCode == Keys.U) diff --git a/src/Logic/SubtitleFormats/SubStationAlpha.cs b/src/Logic/SubtitleFormats/SubStationAlpha.cs index 0affb83c2..78bf89e6d 100644 --- a/src/Logic/SubtitleFormats/SubStationAlpha.cs +++ b/src/Logic/SubtitleFormats/SubStationAlpha.cs @@ -127,17 +127,17 @@ Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text text = text.Replace(@"{\i1}", ""); text = text.Replace(@"{\i0}", ""); - if (FixCommonErrors.CountTagInText(text, "") > FixCommonErrors.CountTagInText(text, "")) + if (Utilities.CountTagInText(text, "") > Utilities.CountTagInText(text, "")) text += ""; text = text.Replace(@"{\u1}", ""); text = text.Replace(@"{\u0}", ""); - if (FixCommonErrors.CountTagInText(text, "") > FixCommonErrors.CountTagInText(text, "")) + if (Utilities.CountTagInText(text, "") > Utilities.CountTagInText(text, "")) text += ""; text = text.Replace(@"{\b1}", ""); text = text.Replace(@"{\b0}", ""); - if (FixCommonErrors.CountTagInText(text, "") > FixCommonErrors.CountTagInText(text, "")) + if (Utilities.CountTagInText(text, "") > Utilities.CountTagInText(text, "")) text += ""; for (int i = 0; i < 5; i++) // just look five times... diff --git a/src/Logic/Utilities.cs b/src/Logic/Utilities.cs index efac97cfd..4f11de9f2 100644 --- a/src/Logic/Utilities.cs +++ b/src/Logic/Utilities.cs @@ -1538,6 +1538,111 @@ namespace Nikse.SubtitleEdit.Logic else if (control is Button) control.Height = newHeight; } + + internal static int CountTagInText(string text, string tag) + { + int count = 0; + int index = text.IndexOf(tag); + while (index >= 0) + { + count++; + index = text.IndexOf(tag, index + 1); + } + return count; + } + + internal static string FixInvalidItalicTags(string text) + { + const string beginTag = ""; + const string endTag = ""; + + text = text.Replace("< i>", beginTag); + text = text.Replace("", beginTag); + text = text.Replace("< I>", beginTag); + text = text.Replace("", beginTag); + + text = text.Replace("< /i>", endTag); + text = text.Replace("", endTag); + text = text.Replace("< /i>", endTag); + text = text.Replace("< /I>", endTag); + text = text.Replace("", endTag); + text = text.Replace("< /I>", endTag); + + if (text.Contains(beginTag)) + text = text.Replace("", endTag); + else + text = text.Replace("", string.Empty); + + text = text.Replace(beginTag + beginTag, beginTag); + text = text.Replace(endTag + endTag, endTag); + + int italicBeginTagCount = CountTagInText(text, beginTag); + int italicEndTagCount = CountTagInText(text, endTag); + int noOfLines = CountTagInText(text, Environment.NewLine) + 1; + if (italicBeginTagCount + italicEndTagCount > 0) + { + if (italicBeginTagCount == 1 && italicEndTagCount == 1) + { + if (text.IndexOf(beginTag) > text.IndexOf(endTag)) + { + text = text.Replace(beginTag, "___________@"); + text = text.Replace(endTag, beginTag); + text = text.Replace("___________@", endTag); + } + } + + if (italicBeginTagCount == 2 && italicEndTagCount == 0) + { + int firstIndex = text.IndexOf(beginTag); + int lastIndex = text.LastIndexOf(beginTag); + int lastIndexWithNewLine = text.LastIndexOf(Environment.NewLine + beginTag) + Environment.NewLine.Length; + if (noOfLines == 2 && lastIndex == lastIndexWithNewLine && firstIndex < 2) + text = text.Replace(Environment.NewLine, "" + Environment.NewLine) + ""; + else if (text.Length > lastIndex + endTag.Length) + text = text.Substring(0, lastIndex) + endTag + text.Substring(lastIndex - 1 + endTag.Length); + else + text = text.Substring(0, lastIndex) + endTag; + } + + if (italicBeginTagCount == 1 && italicEndTagCount == 2) + { + int firstIndex = text.IndexOf(endTag); + text = text.Substring(0, firstIndex - 1) + text.Substring(firstIndex + endTag.Length); + } + + if (italicBeginTagCount == 2 && italicEndTagCount == 1) + { + int lastIndex = text.LastIndexOf(beginTag); + if (text.Length > lastIndex + endTag.Length) + text = text.Substring(0, lastIndex) + text.Substring(lastIndex - 1 + endTag.Length); + else + text = text.Substring(0, lastIndex - 1) + endTag; + } + + if (italicBeginTagCount == 1 && italicEndTagCount == 0) + { + int lastIndexWithNewLine = text.LastIndexOf(Environment.NewLine + beginTag) + Environment.NewLine.Length; + int lastIndex = text.LastIndexOf(beginTag); + + if (text.StartsWith(beginTag)) + text += endTag; + else if (noOfLines == 2 && lastIndex == lastIndexWithNewLine) + text += endTag; + else + text = text.Replace(beginTag, string.Empty); + } + + if (italicBeginTagCount == 0 && italicEndTagCount == 1) + { + text = text.Replace(endTag, string.Empty); + } + + text = text.Replace("", string.Empty); + text = text.Replace(" ", string.Empty); + text = text.Replace(" ", string.Empty); + } + return text; + } } } \ No newline at end of file