From 0dcb118064f0477343f2d278def28f5648f31b40 Mon Sep 17 00:00:00 2001 From: Nikolaj Olsson Date: Fri, 27 Sep 2024 07:54:16 +0200 Subject: [PATCH] Fix new issue with OCR/Italic - thx Zoltan :) --- .../Ocr/MatchesToItalicStringConverterTest.cs | 32 +++++++++++++++++++ .../Ocr/MatchesToItalicStringConverter.cs | 20 ++++++++++-- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/src/Test/Logic/Ocr/MatchesToItalicStringConverterTest.cs b/src/Test/Logic/Ocr/MatchesToItalicStringConverterTest.cs index 489fe2ef4..2817375bd 100644 --- a/src/Test/Logic/Ocr/MatchesToItalicStringConverterTest.cs +++ b/src/Test/Logic/Ocr/MatchesToItalicStringConverterTest.cs @@ -138,6 +138,38 @@ namespace Test.Logic.Ocr Assert.AreEqual("CAESAR: I live here.", result); } + [TestMethod] + public void TestNonItalicAndParentheses() + { + var matches = new List + { + new VobSubOcr.CompareMatch("-(", false, 0, Guid.NewGuid().ToString()), + new VobSubOcr.CompareMatch("L", false, 0, Guid.NewGuid().ToString()), + new VobSubOcr.CompareMatch("A", false, 0, Guid.NewGuid().ToString()), + new VobSubOcr.CompareMatch("U", false, 0, Guid.NewGuid().ToString()), + new VobSubOcr.CompareMatch("G", false, 0, Guid.NewGuid().ToString()), + new VobSubOcr.CompareMatch("H", false, 0, Guid.NewGuid().ToString()), + new VobSubOcr.CompareMatch("I", false, 0, Guid.NewGuid().ToString()), + new VobSubOcr.CompareMatch("N", false, 0, Guid.NewGuid().ToString()), + new VobSubOcr.CompareMatch("G", false, 0, Guid.NewGuid().ToString()), + new VobSubOcr.CompareMatch(")", false, 0, Guid.NewGuid().ToString()), + new VobSubOcr.CompareMatch(Environment.NewLine, false, 0, Guid.NewGuid().ToString()) { }, + new VobSubOcr.CompareMatch("-", false, 0, Guid.NewGuid().ToString()), + new VobSubOcr.CompareMatch("(", false, 0, Guid.NewGuid().ToString()), + new VobSubOcr.CompareMatch("B", false, 0, Guid.NewGuid().ToString()), + new VobSubOcr.CompareMatch("Y", false, 0, Guid.NewGuid().ToString()), + new VobSubOcr.CompareMatch("E", false, 0, Guid.NewGuid().ToString()), + new VobSubOcr.CompareMatch(" ", false, 0, Guid.NewGuid().ToString()), + new VobSubOcr.CompareMatch("B", false, 0, Guid.NewGuid().ToString()), + new VobSubOcr.CompareMatch("Y", false, 0, Guid.NewGuid().ToString()), + new VobSubOcr.CompareMatch("E", false, 0, Guid.NewGuid().ToString()), + new VobSubOcr.CompareMatch(")", false, 0, Guid.NewGuid().ToString()), + }; + + var result = MatchesToItalicStringConverter.GetStringWithItalicTags(matches); + Assert.AreEqual("-(LAUGHING)" + Environment.NewLine + "-(BYE BYE)", result); + } + [TestMethod] public void TestItalicAndBrackets() { diff --git a/src/ui/Logic/Ocr/MatchesToItalicStringConverter.cs b/src/ui/Logic/Ocr/MatchesToItalicStringConverter.cs index c1ff4dda1..af467f6fd 100644 --- a/src/ui/Logic/Ocr/MatchesToItalicStringConverter.cs +++ b/src/ui/Logic/Ocr/MatchesToItalicStringConverter.cs @@ -194,9 +194,17 @@ namespace Nikse.SubtitleEdit.Logic.Ocr { var result = new List(); var line = new List(); + var skipNext = false; - foreach (var t in matches) + for (var index = 0; index < matches.Count; index++) { + var t = matches[index]; + if (skipNext) + { + skipNext = false; + continue; + } + if (t.Text == Environment.NewLine) { if (line.Count > 0) @@ -210,8 +218,16 @@ namespace Nikse.SubtitleEdit.Logic.Ocr if (line.Count > 0) { line.Add(t); + if (index < matches.Count - 1 && matches[index + 1].Text == Environment.NewLine) + { + result.Add(new SplitItem { Matches = line, Separator = Environment.NewLine }); + skipNext = true; + } + else + { + result.Add(new SplitItem { Matches = line, Separator = string.Empty }); + } - result.Add(new SplitItem { Matches = line, Separator = string.Empty }); line = new List(); } }