From 3e4bfbb9cd60b99729bec92119d0553de9533ac9 Mon Sep 17 00:00:00 2001 From: Nikolaj Olsson Date: Fri, 15 Dec 2023 20:13:56 +0100 Subject: [PATCH] Toggle music symbols, now toggles via first text - thx Leon :) Related to #7386 --- SubtitleEdit.sln.DotSettings | 1 + src/libse/Common/Utilities.cs | 93 ++++++++++++++++++++++++++++------- src/ui/Forms/Main.cs | 38 +++++++++++--- 3 files changed, 109 insertions(+), 23 deletions(-) diff --git a/SubtitleEdit.sln.DotSettings b/SubtitleEdit.sln.DotSettings index 5ea2ab6fd..b878fbaf2 100644 --- a/SubtitleEdit.sln.DotSettings +++ b/SubtitleEdit.sln.DotSettings @@ -41,6 +41,7 @@ True True True + True True True True diff --git a/src/libse/Common/Utilities.cs b/src/libse/Common/Utilities.cs index 404b529e4..16eaf2515 100644 --- a/src/libse/Common/Utilities.cs +++ b/src/libse/Common/Utilities.cs @@ -856,7 +856,7 @@ namespace Nikse.SubtitleEdit.Core.Common var excludes = new HashSet(); var languages = Configuration.Settings.General.DefaultLanguages ?? string.Empty; - var languageList = useFilter ? languages.Split(new []{ ';' }, StringSplitOptions.RemoveEmptyEntries) : Array.Empty(); + var languageList = useFilter ? languages.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries) : Array.Empty(); foreach (var ci in CultureInfo.GetCultures(CultureTypes.NeutralCultures)) { @@ -1048,9 +1048,9 @@ namespace Nikse.SubtitleEdit.Core.Common if (word.Length > 0) { var userWordsXmlFileName = DictionaryFolder + languageName + "_user.xml"; - if (!File.Exists(userWordsXmlFileName) && languageName != null && languageName.Length> 2) + if (!File.Exists(userWordsXmlFileName) && languageName != null && languageName.Length > 2) { - var newFileName = DictionaryFolder + languageName.Substring(0,2).ToLowerInvariant() + "_user.xml"; + var newFileName = DictionaryFolder + languageName.Substring(0, 2).ToLowerInvariant() + "_user.xml"; if (File.Exists(newFileName)) { userWordsXmlFileName = newFileName; @@ -1831,7 +1831,7 @@ namespace Nikse.SubtitleEdit.Core.Common var color = text.Substring(start, end - start).TrimStart('\\').TrimStart('1').TrimStart('c'); color = color.RemoveChar('&').TrimStart('H'); color = color.PadLeft(6, '0'); - var c= AdvancedSubStationAlpha.GetSsaColor("h" + color, defaultColor); + var c = AdvancedSubStationAlpha.GetSsaColor("h" + color, defaultColor); // alpha @@ -2971,7 +2971,7 @@ namespace Nikse.SubtitleEdit.Core.Common return true; } - var isLineContinuation = s.EndsWith("...", StringComparison.Ordinal) || + var isLineContinuation = s.EndsWith("...", StringComparison.Ordinal) || (AllLetters + "…,-$%").Contains(s.Substring(s.Length - 1)) || CalcCjk.IsCjk(s[s.Length - 1]); @@ -3124,7 +3124,55 @@ namespace Nikse.SubtitleEdit.Core.Common } } - public static string ToggleSymbols(string tag, string text, string endTag = null) + public static string ToggleSymbols(string tag, string text, string endTag, out bool added) + { + var pre = string.Empty; + var post = string.Empty; + text = SplitStartTags(text, ref pre); + text = SplitEndTags(text, ref post); + + if (!string.IsNullOrEmpty(tag) && text.Contains(tag) || string.IsNullOrEmpty(tag) && !string.IsNullOrEmpty(endTag) && text.Contains(endTag)) + { + if (!string.IsNullOrEmpty(endTag) && !string.IsNullOrEmpty(tag)) + { + text = pre + text.Replace(tag, string.Empty).Replace(endTag, string.Empty).Replace(Environment.NewLine + " ", Environment.NewLine).Replace(" " + Environment.NewLine, Environment.NewLine).Trim() + post; + } + else if (string.IsNullOrEmpty(endTag) && !string.IsNullOrEmpty(tag)) + { + text = pre + text.Replace(tag, string.Empty).Replace(Environment.NewLine + " ", Environment.NewLine).Replace(" " + Environment.NewLine, Environment.NewLine).Trim() + post; + } + else if (!string.IsNullOrEmpty(endTag)) + { + text = pre + text.Replace(endTag, string.Empty).Replace(Environment.NewLine + " ", Environment.NewLine).Replace(" " + Environment.NewLine, Environment.NewLine).Trim() + post; + } + + added = false; + } + else + { + if (tag == Configuration.Settings.Tools.MusicSymbol) + { + if (Configuration.Settings.Tools.MusicSymbolStyle.Equals("single", StringComparison.OrdinalIgnoreCase)) + { + text = string.Format("{0}{1} {2}{3}", pre, tag, text.Replace(Environment.NewLine, Environment.NewLine + tag + " "), post); + } + else + { + text = string.Format("{0}{1} {2} {1}{3}", pre, tag, text.Replace(Environment.NewLine, " " + tag + Environment.NewLine + tag + " "), post); + } + } + else + { + text = string.Format("{0}{1}{2}{3}{4}", pre, tag, text, endTag ?? tag, post); + } + + added = true; + } + + return text; + } + + public static string RemoveSymbols(string tag, string text, string endTag) { var pre = string.Empty; var post = string.Empty; @@ -3146,24 +3194,35 @@ namespace Nikse.SubtitleEdit.Core.Common text = pre + text.Replace(endTag, string.Empty).Replace(Environment.NewLine + " ", Environment.NewLine).Replace(" " + Environment.NewLine, Environment.NewLine).Trim() + post; } } - else + + return text; + } + + + public static string AddSymbols(string tag, string text, string endTag) + { + text = RemoveSymbols(tag, text, endTag); + + var pre = string.Empty; + var post = string.Empty; + text = SplitStartTags(text, ref pre); + text = SplitEndTags(text, ref post); + + if (tag == Configuration.Settings.Tools.MusicSymbol) { - if (tag == Configuration.Settings.Tools.MusicSymbol) + if (Configuration.Settings.Tools.MusicSymbolStyle.Equals("single", StringComparison.OrdinalIgnoreCase)) { - if (Configuration.Settings.Tools.MusicSymbolStyle.Equals("single", StringComparison.OrdinalIgnoreCase)) - { - text = string.Format("{0}{1} {2}{3}", pre, tag, text.Replace(Environment.NewLine, Environment.NewLine + tag + " "), post); - } - else - { - text = string.Format("{0}{1} {2} {1}{3}", pre, tag, text.Replace(Environment.NewLine, " " + tag + Environment.NewLine + tag + " "), post); - } + text = string.Format("{0}{1} {2}{3}", pre, tag, text.Replace(Environment.NewLine, Environment.NewLine + tag + " "), post); } else { - text = string.Format("{0}{1}{2}{3}{4}", pre, tag, text, endTag ?? tag, post); + text = string.Format("{0}{1} {2} {1}{3}", pre, tag, text.Replace(Environment.NewLine, " " + tag + Environment.NewLine + tag + " "), post); } } + else + { + text = string.Format("{0}{1}{2}{3}{4}", pre, tag, text, endTag ?? tag, post); + } return text; } diff --git a/src/ui/Forms/Main.cs b/src/ui/Forms/Main.cs index 37b9df4f1..c6331de4b 100644 --- a/src/ui/Forms/Main.cs +++ b/src/ui/Forms/Main.cs @@ -16625,7 +16625,7 @@ namespace Nikse.SubtitleEdit.Forms // do not check for shortcuts if text is being entered and a textbox is focused var textBoxTypes = new List { "AdvancedTextBox", "SimpleTextBox", "SETextBox", "TextBox", "RichTextBox" }; if (textBoxTypes.Contains(typeName) && - ((e.KeyCode >= Keys.A && e.KeyCode <= Keys.Z) || + ((e.KeyCode >= Keys.A && e.KeyCode <= Keys.Z) || (e.KeyCode >= Keys.OemSemicolon && e.KeyCode <= Keys.OemBackslash) || e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9 || e.KeyCode == Keys.Multiply || @@ -31230,7 +31230,7 @@ namespace Nikse.SubtitleEdit.Forms } int selectionStart = tb.SelectionStart; - text = Utilities.ToggleSymbols(tag, text, endTag); + text = Utilities.ToggleSymbols(tag, text, endTag, out var _); tb.SelectedText = text; tb.SelectionStart = selectionStart; tb.SelectionLength = text.Length; @@ -31254,20 +31254,46 @@ namespace Nikse.SubtitleEdit.Forms } SubtitleListview1.BeginUpdate(); + var first = true; + var addTags = true; foreach (int i in indices) { + if (first) + { + _subtitle.Paragraphs[i].Text = Utilities.ToggleSymbols(tag, _subtitle.Paragraphs[i].Text, endTag, out var added); + addTags = added; + first = false; + } + + + if (addTags) + { + _subtitle.Paragraphs[i].Text = Utilities.AddSymbols(tag, _subtitle.Paragraphs[i].Text, endTag); + } + else + { + _subtitle.Paragraphs[i].Text = Utilities.RemoveSymbols(tag, _subtitle.Paragraphs[i].Text, endTag); + } + + SubtitleListview1.SetText(i, _subtitle.Paragraphs[i].Text); + if (IsOriginalEditable) { var original = Utilities.GetOriginalParagraph(i, _subtitle.Paragraphs[i], _subtitleOriginal.Paragraphs); if (original != null) { - original.Text = Utilities.ToggleSymbols(tag, original.Text, endTag); + if (addTags) + { + original.Text = Utilities.AddSymbols(tag, original.Text, endTag); + } + else + { + original.Text = Utilities.RemoveSymbols(tag, original.Text, endTag); + } + SubtitleListview1.SetOriginalText(i, original.Text); } } - - _subtitle.Paragraphs[i].Text = Utilities.ToggleSymbols(tag, _subtitle.Paragraphs[i].Text, endTag); - SubtitleListview1.SetText(i, _subtitle.Paragraphs[i].Text); } SubtitleListview1.EndUpdate();