From a3963cc60cf76b8ae75c8670b13266c06ea45c45 Mon Sep 17 00:00:00 2001 From: Nikolaj Olsson Date: Sat, 24 Apr 2021 09:53:22 +0200 Subject: [PATCH] Fix for crash due to color in karoke/typewriter effect - thx PM :) --- Changelog.txt | 3 +- src/libse/Common/HtmlUtil.cs | 24 ++++++++++ .../AdvancedSubStationAlpha.cs | 46 ++----------------- src/ui/Forms/EffectKaraoke.cs | 2 +- src/ui/Forms/EffectTypewriter.cs | 16 +------ 5 files changed, 31 insertions(+), 60 deletions(-) diff --git a/Changelog.txt b/Changelog.txt index 5df80138f..975252e0f 100644 --- a/Changelog.txt +++ b/Changelog.txt @@ -37,7 +37,7 @@ * Make "load second subtitle" work with more subtitle formats - thx PM * Improve compare colors for dark theme - thx OmrSi * Minor UI improvements for nOCR character inspect - * Add "Clear" to File - Recent files - thx Dominiquini/OmrSi + * Add "Clear" to "File - Recent files" - thx Dominiquini/OmrSi * Make bd sup edit work better with full frame images - thx peter-qgd * Point sync now also works on original subtitle - thx Rene * FIXED: @@ -52,6 +52,7 @@ * Some fixes for editing source view - thx PreetM07 * Fix bug converting vtt two line color text to srt * Fix FCE issue w missing spaces/French language - thx kyrpasto/OmrSi + * Fix crash in karoke/typewriter effect due to invalid color - thx PM 3.6.0 (18th February 2021) diff --git a/src/libse/Common/HtmlUtil.cs b/src/libse/Common/HtmlUtil.cs index 7e501ad46..f96d20231 100644 --- a/src/libse/Common/HtmlUtil.cs +++ b/src/libse/Common/HtmlUtil.cs @@ -1,4 +1,5 @@ using System; +using System.Drawing; using System.Linq; using System.Text; using System.Text.RegularExpressions; @@ -909,5 +910,28 @@ namespace Nikse.SubtitleEdit.Core.Common return text; } + public static Color GetColorFromString(string color) + { + Color c = Color.White; + try + { + if (color.StartsWith("rgb(", StringComparison.Ordinal)) + { + string[] arr = color.Remove(0, 4).TrimEnd(')').Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); + c = Color.FromArgb(int.Parse(arr[0]), int.Parse(arr[1]), int.Parse(arr[2])); + } + else + { + c = ColorTranslator.FromHtml(color); + } + } + catch + { + // ignored + } + + return c; + } + } } diff --git a/src/libse/SubtitleFormats/AdvancedSubStationAlpha.cs b/src/libse/SubtitleFormats/AdvancedSubStationAlpha.cs index 8b6d6bee9..9993f0f0c 100644 --- a/src/libse/SubtitleFormats/AdvancedSubStationAlpha.cs +++ b/src/libse/SubtitleFormats/AdvancedSubStationAlpha.cs @@ -442,23 +442,7 @@ Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text" color = node.Attributes["tts:color"].Value.Trim(); } - Color c = Color.White; - try - { - if (color.StartsWith("rgb(", StringComparison.Ordinal)) - { - string[] arr = color.Remove(0, 4).TrimEnd(')').Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); - c = Color.FromArgb(int.Parse(arr[0]), int.Parse(arr[1]), int.Parse(arr[2])); - } - else - { - c = ColorTranslator.FromHtml(color); - } - } - catch - { - // ignored - } + var c = HtmlUtil.GetColorFromString(color); string fontSize = "20"; if (node.Attributes["tts:fontSize"] != null) @@ -589,23 +573,7 @@ Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text" color = node.Attributes["tts:color"].Value.Trim(); } - Color c = Color.White; - try - { - if (color.StartsWith("rgb(", StringComparison.Ordinal)) - { - string[] arr = color.Remove(0, 4).TrimEnd(')').Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); - c = Color.FromArgb(int.Parse(arr[0]), int.Parse(arr[1]), int.Parse(arr[2])); - } - else - { - c = ColorTranslator.FromHtml(color); - } - } - catch - { - // ignored - } + var c = HtmlUtil.GetColorFromString(color); string fontSize = "20"; if (node.Attributes["tts:fontSize"] != null) @@ -802,15 +770,7 @@ Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text" string subTag = fontTag.Substring(fontStart + tag.Length, fontEnd - (fontStart + tag.Length)); if (tag.Contains("color")) { - Color c; - try - { - c = ColorTranslator.FromHtml(subTag); - } - catch - { - c = Color.White; - } + var c = HtmlUtil.GetColorFromString(subTag); subTag = (c.B.ToString("X2") + c.G.ToString("X2") + c.R.ToString("X2")).ToLowerInvariant(); // use bbggrr } fontTag = fontTag.Remove(fontStart, fontEnd - fontStart + 1); diff --git a/src/ui/Forms/EffectKaraoke.cs b/src/ui/Forms/EffectKaraoke.cs index 47eb86914..a3b316470 100644 --- a/src/ui/Forms/EffectKaraoke.cs +++ b/src/ui/Forms/EffectKaraoke.cs @@ -194,7 +194,7 @@ namespace Nikse.SubtitleEdit.Forms { int length = rtb.Text.Length; richTextBoxPreview.Text += text; - _colorList.Add(new ColorEntry { Start = length, Length = text.Length, Color = string.IsNullOrWhiteSpace(color) ? Color.White : ColorTranslator.FromHtml(color) }); + _colorList.Add(new ColorEntry { Start = length, Length = text.Length, Color = HtmlUtil.GetColorFromString(color) }); var fontStyle = new FontStyle(); if (underline) diff --git a/src/ui/Forms/EffectTypewriter.cs b/src/ui/Forms/EffectTypewriter.cs index 622423a3a..9ec9ab541 100644 --- a/src/ui/Forms/EffectTypewriter.cs +++ b/src/ui/Forms/EffectTypewriter.cs @@ -176,21 +176,7 @@ namespace Nikse.SubtitleEdit.Forms var c = Color.White; if (!string.IsNullOrWhiteSpace(color)) { - try - { - c = ColorTranslator.FromHtml(color); - } - catch - { - try - { - c = ColorTranslator.FromHtml("#" + color.Trim('#', ' ', '"', '\'')); - } - catch - { - c = Color.White; - } - } + c = HtmlUtil.GetColorFromString(color); } _colorList.Add(new EffectKaraoke.ColorEntry { Start = length, Length = text.Length, Color = c });