public Subtitle SplitLongLinesInSubtitle(Subtitle subtitle, List <int> splittedIndexes, List <int> autoBreakedIndexes, out int numberOfSplits, int totalLineMaxCharacters, int singleLineMaxCharacters, bool clearFixes) { listViewFixes.ItemChecked -= listViewFixes_ItemChecked; if (clearFixes) { listViewFixes.Items.Clear(); } numberOfSplits = 0; string language = LanguageAutoDetect.AutoDetectGoogleLanguage(subtitle); var splittedSubtitle = new Subtitle(); string[] expectedPunctuations = { ". -", "! -", "? -" }; for (int i = 0; i < subtitle.Paragraphs.Count; i++) { bool added = false; var p = subtitle.Paragraphs[i]; if (p != null && p.Text != null) { if (SplitLongLinesHelper.QualifiesForSplit(p.Text, singleLineMaxCharacters, totalLineMaxCharacters) && IsFixAllowed(p)) { string oldText = HtmlUtil.RemoveHtmlTags(p.Text); bool isDialog = false; string dialogText = string.Empty; if (p.Text.Contains('-')) { dialogText = Utilities.AutoBreakLine(p.Text, 5, 1, language); var tempText = p.Text.Replace(Environment.NewLine, " ").Replace(" ", " "); if (Utilities.CountTagInText(tempText, '-') == 2 && (p.Text.StartsWith('-') || p.Text.StartsWith("<i>-", StringComparison.Ordinal))) { int idx = tempText.IndexOfAny(expectedPunctuations, StringComparison.Ordinal); if (idx > 1) { dialogText = tempText.Remove(idx + 1, 1).Insert(idx + 1, Environment.NewLine); } } var arr = dialogText.SplitToLines(); if (arr.Length == 2 && (arr[0].StartsWith('-') || arr[0].StartsWith("<i>-", StringComparison.Ordinal)) && (arr[1].StartsWith('-') || arr[1].StartsWith("<i>-", StringComparison.Ordinal))) { isDialog = true; } } if (!isDialog && !SplitLongLinesHelper.QualifiesForSplit(Utilities.AutoBreakLine(p.Text, language), singleLineMaxCharacters, totalLineMaxCharacters)) { var newParagraph = new Paragraph(p) { Text = Utilities.AutoBreakLine(p.Text, language) }; if (clearFixes) { AddToListView(p, (splittedSubtitle.Paragraphs.Count + 1).ToString(CultureInfo.InvariantCulture), oldText); } autoBreakedIndexes.Add(splittedSubtitle.Paragraphs.Count); splittedSubtitle.Paragraphs.Add(newParagraph); added = true; numberOfSplits++; } else { string text = Utilities.AutoBreakLine(p.Text, language); if (isDialog) { text = dialogText; } if (isDialog || text.Contains(Environment.NewLine)) { var arr = text.SplitToLines(); if (arr.Length == 2) { int spacing1 = Configuration.Settings.General.MinimumMillisecondsBetweenLines / 2; int spacing2 = Configuration.Settings.General.MinimumMillisecondsBetweenLines / 2; if (Configuration.Settings.General.MinimumMillisecondsBetweenLines % 2 == 1) { spacing2++; } var newParagraph1 = new Paragraph(p); var newParagraph2 = new Paragraph(p); newParagraph1.Text = Utilities.AutoBreakLine(arr[0], language); double middle = p.StartTime.TotalMilliseconds + (p.Duration.TotalMilliseconds / 2); if (!string.IsNullOrWhiteSpace(oldText)) { var startFactor = (double)HtmlUtil.RemoveHtmlTags(newParagraph1.Text).Length / oldText.Length; if (startFactor < 0.25) { startFactor = 0.25; } if (startFactor > 0.75) { startFactor = 0.75; } middle = p.StartTime.TotalMilliseconds + (p.Duration.TotalMilliseconds * startFactor); } newParagraph1.EndTime.TotalMilliseconds = middle - spacing1; newParagraph2.Text = Utilities.AutoBreakLine(arr[1], language); newParagraph2.StartTime.TotalMilliseconds = newParagraph1.EndTime.TotalMilliseconds + spacing2; if (clearFixes) { AddToListView(p, (splittedSubtitle.Paragraphs.Count + 1).ToString(CultureInfo.InvariantCulture), oldText); } splittedIndexes.Add(splittedSubtitle.Paragraphs.Count); splittedIndexes.Add(splittedSubtitle.Paragraphs.Count + 1); string p1 = HtmlUtil.RemoveHtmlTags(newParagraph1.Text).TrimEnd(); if (p1.EndsWith('.') || p1.EndsWith('!') || p1.EndsWith('?') || p1.EndsWith(':') || p1.EndsWith(')') || p1.EndsWith(']') || p1.EndsWith('♪')) { if (newParagraph1.Text.StartsWith('-') && newParagraph2.Text.StartsWith('-')) { newParagraph1.Text = newParagraph1.Text.Remove(0, 1).Trim(); newParagraph2.Text = newParagraph2.Text.Remove(0, 1).Trim(); } else if (newParagraph1.Text.StartsWith("<i>-", StringComparison.Ordinal) && newParagraph2.Text.StartsWith('-')) { newParagraph1.Text = newParagraph1.Text.Remove(3, 1).Trim(); if (newParagraph1.Text.StartsWith("<i> ", StringComparison.Ordinal)) { newParagraph1.Text = newParagraph1.Text.Remove(3, 1).Trim(); } newParagraph2.Text = newParagraph2.Text.Remove(0, 1).Trim(); } } else { bool endsWithComma = newParagraph1.Text.EndsWith(',') || newParagraph1.Text.EndsWith(",</i>", StringComparison.Ordinal); string post = string.Empty; if (newParagraph1.Text.EndsWith("</i>", StringComparison.Ordinal)) { post = "</i>"; newParagraph1.Text = newParagraph1.Text.Remove(newParagraph1.Text.Length - post.Length); } if (endsWithComma) { newParagraph1.Text += post; } else { newParagraph1.Text += comboBoxLineContinuationEnd.Text.TrimEnd() + post; } string pre = string.Empty; if (newParagraph2.Text.StartsWith("<i>", StringComparison.Ordinal)) { pre = "<i>"; newParagraph2.Text = newParagraph2.Text.Remove(0, pre.Length); } if (endsWithComma) { newParagraph2.Text = pre + newParagraph2.Text; } else { newParagraph2.Text = pre + comboBoxLineContinuationBegin.Text + newParagraph2.Text; } } var italicStart1 = newParagraph1.Text.IndexOf("<i>", StringComparison.Ordinal); if (italicStart1 >= 0 && italicStart1 < 10 && newParagraph1.Text.IndexOf("</i>", StringComparison.Ordinal) < 0 && newParagraph2.Text.Contains("</i>") && newParagraph2.Text.IndexOf("<i>", StringComparison.Ordinal) < 0) { newParagraph1.Text += "</i>"; newParagraph2.Text = "<i>" + newParagraph2.Text; } splittedSubtitle.Paragraphs.Add(newParagraph1); splittedSubtitle.Paragraphs.Add(newParagraph2); added = true; numberOfSplits++; } } } } if (!added) { splittedSubtitle.Paragraphs.Add(new Paragraph(p)); } } } listViewFixes.ItemChecked += listViewFixes_ItemChecked; splittedSubtitle.Renumber(); return(splittedSubtitle); }
public Subtitle SplitLongLinesInSubtitle(Subtitle subtitle, List <int> splittedIndexes, List <int> autoBreakedIndexes, out int numberOfSplits, int totalLineMaxCharacters, int singleLineMaxCharacters, bool clearFixes) { listViewFixes.ItemChecked -= listViewFixes_ItemChecked; if (clearFixes) { listViewFixes.Items.Clear(); } numberOfSplits = 0; string language = Utilities.AutoDetectGoogleLanguage(subtitle); Subtitle splittedSubtitle = new Subtitle(); Paragraph p = null; for (int i = 0; i < subtitle.Paragraphs.Count; i++) { bool added = false; p = subtitle.GetParagraphOrDefault(i); if (p != null && p.Text != null) { string oldText = Utilities.RemoveHtmlTags(p.Text); if (SplitLongLinesHelper.QualifiesForSplit(p.Text, singleLineMaxCharacters, totalLineMaxCharacters) && IsFixAllowed(p)) { bool isDialog = false; string dialogText = string.Empty; if (p.Text.Contains("-")) { dialogText = Utilities.AutoBreakLine(p.Text, 5, 100, 1, language); string[] arr = dialogText.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries); if (arr.Length == 2 && (arr[0].StartsWith("-") || arr[0].StartsWith("<i>-")) && (arr[1].StartsWith("-") || arr[1].StartsWith("<i>-"))) { isDialog = true; } } if (!isDialog && !SplitLongLinesHelper.QualifiesForSplit(Utilities.AutoBreakLine(p.Text, language), singleLineMaxCharacters, totalLineMaxCharacters)) { Paragraph newParagraph = new Paragraph(p); newParagraph.Text = Utilities.AutoBreakLine(p.Text, language); if (clearFixes) { AddToListView(p, (splittedSubtitle.Paragraphs.Count + 1).ToString(), oldText); } autoBreakedIndexes.Add(splittedSubtitle.Paragraphs.Count); splittedSubtitle.Paragraphs.Add(newParagraph); added = true; numberOfSplits++; } else { string text = Utilities.AutoBreakLine(p.Text, language); if (isDialog) { text = dialogText; } if (text.Contains(Environment.NewLine)) { string[] arr = text.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries); if (arr.Length == 2) { int spacing1 = Configuration.Settings.General.MininumMillisecondsBetweenLines / 2; int spacing2 = Configuration.Settings.General.MininumMillisecondsBetweenLines / 2; if (Configuration.Settings.General.MininumMillisecondsBetweenLines % 2 == 1) { spacing2++; } Paragraph newParagraph1 = new Paragraph(p); Paragraph newParagraph2 = new Paragraph(p); newParagraph1.Text = Utilities.AutoBreakLine(arr[0], language); double startFactor = 0; double middle = p.StartTime.TotalMilliseconds + (p.Duration.TotalMilliseconds / 2); if (Utilities.RemoveHtmlTags(oldText).Trim().Length > 0) { startFactor = (double)Utilities.RemoveHtmlTags(newParagraph1.Text).Length / Utilities.RemoveHtmlTags(oldText).Length; if (startFactor < 0.25) { startFactor = 0.25; } if (startFactor > 0.75) { startFactor = 0.75; } middle = p.StartTime.TotalMilliseconds + (p.Duration.TotalMilliseconds * startFactor); } newParagraph1.EndTime.TotalMilliseconds = middle - spacing1; newParagraph2.Text = Utilities.AutoBreakLine(arr[1], language); newParagraph2.StartTime.TotalMilliseconds = newParagraph1.EndTime.TotalMilliseconds + spacing2; if (clearFixes) { AddToListView(p, (splittedSubtitle.Paragraphs.Count + 1).ToString(), oldText); } splittedIndexes.Add(splittedSubtitle.Paragraphs.Count); splittedIndexes.Add(splittedSubtitle.Paragraphs.Count + 1); string p1 = Utilities.RemoveHtmlTags(newParagraph1.Text); bool p1Ends = false; if (p1.EndsWith(".") || p1.EndsWith("!") || p1.EndsWith("?") || p1.EndsWith(":") || p1.EndsWith(")") || p1.EndsWith("]") || p1.EndsWith("♪")) { p1Ends = true; } if (p1Ends) { if (newParagraph1.Text.StartsWith("-") && newParagraph2.Text.StartsWith("-")) { newParagraph1.Text = newParagraph1.Text.Remove(0, 1).Trim(); newParagraph2.Text = newParagraph2.Text.Remove(0, 1).Trim(); } else if (newParagraph1.Text.StartsWith("<i>-") && newParagraph2.Text.StartsWith("-")) { newParagraph1.Text = newParagraph1.Text.Remove(3, 1).Trim(); if (newParagraph1.Text.StartsWith("<i> ")) { newParagraph1.Text = newParagraph1.Text.Remove(3, 1).Trim(); } newParagraph2.Text = newParagraph2.Text.Remove(0, 1).Trim(); } } else { string post = string.Empty; if (newParagraph1.Text.EndsWith("</i>")) { post = "</i>"; newParagraph1.Text = newParagraph1.Text.Remove(newParagraph1.Text.Length - post.Length); } newParagraph1.Text += comboBoxLineContinuationEnd.Text.TrimEnd() + post; string pre = string.Empty; if (newParagraph2.Text.StartsWith("<i>")) { pre = "<i>"; newParagraph2.Text = newParagraph2.Text.Remove(0, pre.Length); } newParagraph2.Text = pre + comboBoxLineContinuationBegin.Text + newParagraph2.Text; } if (newParagraph1.Text.IndexOf("<i>") >= 0 && newParagraph1.Text.IndexOf("<i>") < 10 & newParagraph1.Text.IndexOf("</i>") < 0 && newParagraph2.Text.Contains("</i>") && newParagraph2.Text.IndexOf("<i>") < 0) { newParagraph1.Text += "</i>"; newParagraph2.Text = "<i>" + newParagraph2.Text; } splittedSubtitle.Paragraphs.Add(newParagraph1); splittedSubtitle.Paragraphs.Add(newParagraph2); added = true; numberOfSplits++; } } } } } if (!added) { splittedSubtitle.Paragraphs.Add(new Paragraph(p)); } } listViewFixes.ItemChecked += listViewFixes_ItemChecked; splittedSubtitle.Renumber(1); return(splittedSubtitle); }