Refact - add more braces

This commit is contained in:
Nikolaj Olsson 2019-01-13 01:08:28 +01:00
parent 08a6a45777
commit f23f4e742c
2 changed files with 216 additions and 22 deletions

View File

@ -58,25 +58,33 @@ namespace Nikse.SubtitleEdit.Core.Dictionaries
foreach (var kp in LoadReplaceList(userDoc, "RemovedWholeWords")) foreach (var kp in LoadReplaceList(userDoc, "RemovedWholeWords"))
{ {
if (WordReplaceList.ContainsKey(kp.Key)) if (WordReplaceList.ContainsKey(kp.Key))
{
WordReplaceList.Remove(kp.Key); WordReplaceList.Remove(kp.Key);
} }
}
foreach (var kp in LoadReplaceList(userDoc, "WholeWords")) foreach (var kp in LoadReplaceList(userDoc, "WholeWords"))
{ {
if (!WordReplaceList.ContainsKey(kp.Key)) if (!WordReplaceList.ContainsKey(kp.Key))
{
WordReplaceList.Add(kp.Key, kp.Value); WordReplaceList.Add(kp.Key, kp.Value);
} }
}
foreach (var kp in LoadReplaceList(userDoc, "RemovedPartialLines")) foreach (var kp in LoadReplaceList(userDoc, "RemovedPartialLines"))
{ {
if (PartialLineWordBoundaryReplaceList.ContainsKey(kp.Key)) if (PartialLineWordBoundaryReplaceList.ContainsKey(kp.Key))
{
PartialLineWordBoundaryReplaceList.Remove(kp.Key); PartialLineWordBoundaryReplaceList.Remove(kp.Key);
} }
}
foreach (var kp in LoadReplaceList(userDoc, "PartialLines")) foreach (var kp in LoadReplaceList(userDoc, "PartialLines"))
{ {
if (!PartialLineWordBoundaryReplaceList.ContainsKey(kp.Key)) if (!PartialLineWordBoundaryReplaceList.ContainsKey(kp.Key))
{
PartialLineWordBoundaryReplaceList.Add(kp.Key, kp.Value); PartialLineWordBoundaryReplaceList.Add(kp.Key, kp.Value);
} }
} }
}
public static OcrFixReplaceList FromLanguageId(string languageId) public static OcrFixReplaceList FromLanguageId(string languageId)
{ {
@ -87,7 +95,10 @@ namespace Nikse.SubtitleEdit.Core.Dictionaries
{ {
var list = new Dictionary<string, string>(); var list = new Dictionary<string, string>();
if (!IsValidXmlDocument(doc, name)) if (!IsValidXmlDocument(doc, name))
{
return list; return list;
}
var node = doc.DocumentElement?.SelectSingleNode(name); var node = doc.DocumentElement?.SelectSingleNode(name);
if (node != null) if (node != null)
{ {
@ -98,10 +109,12 @@ namespace Nikse.SubtitleEdit.Core.Dictionaries
string to = item.Attributes["to"].Value; string to = item.Attributes["to"].Value;
string from = item.Attributes["from"].Value; string from = item.Attributes["from"].Value;
if (!list.ContainsKey(from)) if (!list.ContainsKey(from))
{
list.Add(from, to); list.Add(from, to);
} }
} }
} }
}
return list; return list;
} }
@ -110,7 +123,10 @@ namespace Nikse.SubtitleEdit.Core.Dictionaries
{ {
var list = new Dictionary<string, string>(); var list = new Dictionary<string, string>();
if (!IsValidXmlDocument(doc, name)) if (!IsValidXmlDocument(doc, name))
{
return list; return list;
}
var node = doc.DocumentElement?.SelectSingleNode(name); var node = doc.DocumentElement?.SelectSingleNode(name);
if (node != null) if (node != null)
{ {
@ -121,25 +137,28 @@ namespace Nikse.SubtitleEdit.Core.Dictionaries
string to = item.Attributes["replaceWith"].Value; string to = item.Attributes["replaceWith"].Value;
string from = item.Attributes["find"].Value; string from = item.Attributes["find"].Value;
if (!list.ContainsKey(from)) if (!list.ContainsKey(from))
{
list.Add(from, to); list.Add(from, to);
} }
} }
} }
}
return list; return list;
} }
private static bool IsValidXmlDocument(XmlDocument doc, string elementName) private static bool IsValidXmlDocument(XmlDocument doc, string elementName)
{ {
if (doc.DocumentElement?.SelectSingleNode(elementName) == null) return doc.DocumentElement?.SelectSingleNode(elementName) != null;
return false;
return true;
} }
private static bool HasValidAttributes(XmlNode node, bool isRegex) private static bool HasValidAttributes(XmlNode node, bool isRegex)
{ {
if (node?.Attributes == null) if (node?.Attributes == null)
{
return false; return false;
}
if (isRegex) if (isRegex)
{ {
if (node.Attributes["find"] != null && node.Attributes["replaceWith"] != null) if (node.Attributes["find"] != null && node.Attributes["replaceWith"] != null)
@ -163,8 +182,10 @@ namespace Nikse.SubtitleEdit.Core.Dictionaries
foreach (string from in _wholeLineReplaceList.Keys) foreach (string from in _wholeLineReplaceList.Keys)
{ {
if (input == from) if (input == from)
{
return _wholeLineReplaceList[from]; return _wholeLineReplaceList[from];
} }
}
string newText = input; string newText = input;
string pre = string.Empty; string pre = string.Empty;
@ -196,14 +217,18 @@ namespace Nikse.SubtitleEdit.Core.Dictionaries
{ {
string with = _beginLineReplaceList[from]; string with = _beginLineReplaceList[from];
if (s.StartsWith(from, StringComparison.Ordinal)) if (s.StartsWith(from, StringComparison.Ordinal))
{
s = s.Remove(0, from.Length).Insert(0, with); s = s.Remove(0, from.Length).Insert(0, with);
}
s = s.Replace(". " + from, ". " + with); s = s.Replace(". " + from, ". " + with);
s = s.Replace("! " + from, "! " + with); s = s.Replace("! " + from, "! " + with);
s = s.Replace("? " + from, "? " + with); s = s.Replace("? " + from, "? " + with);
if (s.StartsWith("\"" + from, StringComparison.Ordinal) && !from.StartsWith('"')) if (s.StartsWith("\"" + from, StringComparison.Ordinal) && !from.StartsWith('"'))
{
s = s.Replace("\"" + from, "\"" + with); s = s.Replace("\"" + from, "\"" + with);
} }
} }
}
sb.AppendLine(s); sb.AppendLine(s);
} }
newText = pre + sb.ToString().TrimEnd(Utilities.NewLineChars); newText = pre + sb.ToString().TrimEnd(Utilities.NewLineChars);
@ -228,14 +253,18 @@ namespace Nikse.SubtitleEdit.Core.Dictionaries
foreach (string from in PartialLineWordBoundaryReplaceList.Keys) foreach (string from in PartialLineWordBoundaryReplaceList.Keys)
{ {
if (newText.FastIndexOf(from) >= 0) if (newText.FastIndexOf(from) >= 0)
{
newText = ReplaceWord(newText, from, PartialLineWordBoundaryReplaceList[from]); newText = ReplaceWord(newText, from, PartialLineWordBoundaryReplaceList[from]);
} }
}
foreach (string from in _partialLineAlwaysReplaceList.Keys) foreach (string from in _partialLineAlwaysReplaceList.Keys)
{ {
if (newText.FastIndexOf(from) >= 0) if (newText.FastIndexOf(from) >= 0)
{
newText = newText.Replace(from, _partialLineAlwaysReplaceList[from]); newText = newText.Replace(from, _partialLineAlwaysReplaceList[from]);
} }
}
foreach (string findWhat in _regExList.Keys) foreach (string findWhat in _regExList.Keys)
{ {
@ -248,16 +277,24 @@ namespace Nikse.SubtitleEdit.Core.Dictionaries
private static string AddToGuessList(List<string> list, string word, int index, string letter, string replaceLetters) private static string AddToGuessList(List<string> list, string word, int index, string letter, string replaceLetters)
{ {
if (string.IsNullOrEmpty(word) || index < 0 || index + letter.Length - 1 >= word.Length) if (string.IsNullOrEmpty(word) || index < 0 || index + letter.Length - 1 >= word.Length)
{
return word; return word;
}
string s = word.Remove(index, letter.Length); string s = word.Remove(index, letter.Length);
if (index >= s.Length) if (index >= s.Length)
{
s += replaceLetters; s += replaceLetters;
}
else else
{
s = s.Insert(index, replaceLetters); s = s.Insert(index, replaceLetters);
}
if (!list.Contains(s)) if (!list.Contains(s))
{
list.Add(s); list.Add(s);
}
return s; return s;
} }
@ -301,26 +338,34 @@ namespace Nikse.SubtitleEdit.Core.Dictionaries
word = word.Replace("ffi", "ffi"); word = word.Replace("ffi", "ffi");
word = word.Replace("ffl", "ffl"); word = word.Replace("ffl", "ffl");
if (!_replaceListXmlFileName.Contains("\\ell" + ReplaceListFileNamePostFix)) if (!_replaceListXmlFileName.Contains("\\ell" + ReplaceListFileNamePostFix))
{
word = word.Replace('ν', 'v'); // first 'v' is U+03BD GREEK SMALL LETTER NU word = word.Replace('ν', 'v'); // first 'v' is U+03BD GREEK SMALL LETTER NU
}
word = word.Replace('', '\''); word = word.Replace('', '\'');
word = word.Replace('`', '\''); word = word.Replace('`', '\'');
word = word.Replace('´', '\''); word = word.Replace('´', '\'');
word = word.Replace('', '\''); word = word.Replace('', '\'');
word = word.Replace('—', '-'); word = word.Replace('—', '-');
while (word.Contains("--")) while (word.Contains("--"))
{
word = word.Replace("--", "-"); word = word.Replace("--", "-");
}
word = word.Replace('|', 'l'); word = word.Replace('|', 'l');
word = word.Replace("vx/", "w"); word = word.Replace("vx/", "w");
if (word.Contains('¤')) if (word.Contains('¤'))
{ {
if (Regex.IsMatch(word, "[A-ZÆØÅÄÖÉÈÀÙÂÊÎÔÛËÏa-zæøåäöéèàùâêîôûëï]¤")) if (Regex.IsMatch(word, "[A-ZÆØÅÄÖÉÈÀÙÂÊÎÔÛËÏa-zæøåäöéèàùâêîôûëï]¤"))
{
word = word.Replace('¤', 'o'); word = word.Replace('¤', 'o');
} }
} }
}
//always replace list //always replace list
foreach (string letter in _partialWordReplaceListAlways.Keys) foreach (string letter in _partialWordReplaceListAlways.Keys)
{
word = word.Replace(letter, _partialWordReplaceListAlways[letter]); word = word.Replace(letter, _partialWordReplaceListAlways[letter]);
}
string pre = string.Empty; string pre = string.Empty;
string post = string.Empty; string post = string.Empty;
@ -403,14 +448,18 @@ namespace Nikse.SubtitleEdit.Core.Dictionaries
} }
string preWordPost = pre + word + post; string preWordPost = pre + word + post;
if (word.Length == 0) if (word.Length == 0)
{
return preWordPost; return preWordPost;
}
if (word.Contains('?')) if (word.Contains('?'))
{ {
var match = RegExQuestion.Match(word); var match = RegExQuestion.Match(word);
if (match.Success) if (match.Success)
{
word = word.Insert(match.Index + 2, " "); word = word.Insert(match.Index + 2, " ");
} }
}
if (!string.IsNullOrEmpty(pre) || !string.IsNullOrEmpty(post)) if (!string.IsNullOrEmpty(pre) || !string.IsNullOrEmpty(post))
{ {
@ -420,13 +469,17 @@ namespace Nikse.SubtitleEdit.Core.Dictionaries
if (word.Length == from.Length) if (word.Length == from.Length)
{ {
if (word == from) if (word == from)
{
return pre + WordReplaceList[from] + post; return pre + WordReplaceList[from] + post;
} }
}
else if (wordPlusPost.Length == from.Length) else if (wordPlusPost.Length == from.Length)
{ {
if (string.CompareOrdinal(wordPlusPost, from) == 0) if (string.CompareOrdinal(wordPlusPost, from) == 0)
{
return pre + WordReplaceList[from]; return pre + WordReplaceList[from];
} }
}
if (preWordPost.Length == from.Length && string.CompareOrdinal(preWordPost, from) == 0) if (preWordPost.Length == from.Length && string.CompareOrdinal(preWordPost, from) == 0)
{ {
return WordReplaceList[from]; return WordReplaceList[from];
@ -438,9 +491,11 @@ namespace Nikse.SubtitleEdit.Core.Dictionaries
foreach (string from in WordReplaceList.Keys) foreach (string from in WordReplaceList.Keys)
{ {
if (word.Length == from.Length && word == from) if (word.Length == from.Length && word == from)
{
return pre + WordReplaceList[from] + post; return pre + WordReplaceList[from] + post;
} }
} }
}
var oldWord = word; var oldWord = word;
if (Configuration.Settings.Tools.OcrFixUseHardcodedRules) if (Configuration.Settings.Tools.OcrFixUseHardcodedRules)
@ -469,13 +524,17 @@ namespace Nikse.SubtitleEdit.Core.Dictionaries
if (word.Length == from.Length) if (word.Length == from.Length)
{ {
if (string.CompareOrdinal(word, from) == 0) if (string.CompareOrdinal(word, from) == 0)
{
return pre + WordReplaceList[from] + post; return pre + WordReplaceList[from] + post;
} }
}
else if (wordPlusPost.Length == from.Length) else if (wordPlusPost.Length == from.Length)
{ {
if (string.CompareOrdinal(wordPlusPost, from) == 0) if (string.CompareOrdinal(wordPlusPost, from) == 0)
{
return pre + WordReplaceList[from]; return pre + WordReplaceList[from];
} }
}
if (preWordPost.Length == from.Length && string.CompareOrdinal(preWordPost, from) == 0) if (preWordPost.Length == from.Length && string.CompareOrdinal(preWordPost, from) == 0)
{ {
return WordReplaceList[from]; return WordReplaceList[from];
@ -487,10 +546,12 @@ namespace Nikse.SubtitleEdit.Core.Dictionaries
foreach (string from in WordReplaceList.Keys) foreach (string from in WordReplaceList.Keys)
{ {
if (word.Length == from.Length && string.CompareOrdinal(word, from) == 0) if (word.Length == from.Length && string.CompareOrdinal(word, from) == 0)
{
return pre + WordReplaceList[from] + post; return pre + WordReplaceList[from] + post;
} }
} }
} }
}
return preWordPost; return preWordPost;
} }
@ -510,13 +571,19 @@ namespace Nikse.SubtitleEdit.Core.Dictionaries
public static string FixIor1InsideLowerCaseWord(string word) public static string FixIor1InsideLowerCaseWord(string word)
{ {
if (StartsAndEndsWithNumber.IsMatch(word)) if (StartsAndEndsWithNumber.IsMatch(word))
{
return word; return word;
}
if (word.Contains(new[] { '2', '3', '4', '5', '6', '7', '8', '9' })) if (word.Contains(new[] { '2', '3', '4', '5', '6', '7', '8', '9' }))
{
return word; return word;
}
if (HexNumber.IsMatch(word)) if (HexNumber.IsMatch(word))
{
return word; return word;
}
if (word.LastIndexOf('I') > 0 || word.LastIndexOf('1') > 0) if (word.LastIndexOf('I') > 0 || word.LastIndexOf('1') > 0)
{ {
@ -527,16 +594,20 @@ namespace Nikse.SubtitleEdit.Core.Dictionaries
{ {
bool doFix = word[match.Index + 1] != 'I' && match.Index >= 1 && word.Substring(match.Index - 1).StartsWith("Mc", StringComparison.Ordinal); bool doFix = word[match.Index + 1] != 'I' && match.Index >= 1 && word.Substring(match.Index - 1).StartsWith("Mc", StringComparison.Ordinal);
if (word[match.Index + 1] == 'I' && match.Index >= 2 && word.Substring(match.Index - 2).StartsWith("Mac", StringComparison.Ordinal)) if (word[match.Index + 1] == 'I' && match.Index >= 2 && word.Substring(match.Index - 2).StartsWith("Mac", StringComparison.Ordinal))
{
doFix = false; doFix = false;
}
if (doFix) if (doFix)
{ {
string oldText = word; string oldText = word;
word = word.Substring(0, match.Index + 1) + "l"; word = word.Substring(0, match.Index + 1) + "l";
if (match.Index + 2 < oldText.Length) if (match.Index + 2 < oldText.Length)
{
word += oldText.Substring(match.Index + 2); word += oldText.Substring(match.Index + 2);
} }
} }
}
match = RegExIandZero.Match(word, match.Index + 1); match = RegExIandZero.Match(word, match.Index + 1);
} }
} }
@ -546,17 +617,23 @@ namespace Nikse.SubtitleEdit.Core.Dictionaries
public static string Fix0InsideLowerCaseWord(string word) public static string Fix0InsideLowerCaseWord(string word)
{ {
if (StartsAndEndsWithNumber.IsMatch(word)) if (StartsAndEndsWithNumber.IsMatch(word))
{
return word; return word;
}
if (word.Contains(new[] { '1', '2', '3', '4', '5', '6', '7', '8', '9' }) || if (word.Contains(new[] { '1', '2', '3', '4', '5', '6', '7', '8', '9' }) ||
word.EndsWith("a.m", StringComparison.Ordinal) || word.EndsWith("a.m", StringComparison.Ordinal) ||
word.EndsWith("p.m", StringComparison.Ordinal) || word.EndsWith("p.m", StringComparison.Ordinal) ||
word.EndsWith("am", StringComparison.Ordinal) || word.EndsWith("am", StringComparison.Ordinal) ||
word.EndsWith("pm", StringComparison.Ordinal)) word.EndsWith("pm", StringComparison.Ordinal))
{
return word; return word;
}
if (HexNumber.IsMatch(word)) if (HexNumber.IsMatch(word))
{
return word; return word;
}
if (word.LastIndexOf('0') > 0) if (word.LastIndexOf('0') > 0)
{ {
@ -568,8 +645,10 @@ namespace Nikse.SubtitleEdit.Core.Dictionaries
string oldText = word; string oldText = word;
word = word.Substring(0, match.Index + 1) + "o"; word = word.Substring(0, match.Index + 1) + "o";
if (match.Index + 2 < oldText.Length) if (match.Index + 2 < oldText.Length)
{
word += oldText.Substring(match.Index + 2); word += oldText.Substring(match.Index + 2);
} }
}
match = RegExTime1.Match(word); match = RegExTime1.Match(word);
} }
@ -584,9 +663,11 @@ namespace Nikse.SubtitleEdit.Core.Dictionaries
string oldText = word; string oldText = word;
word = word.Substring(0, match.Index) + "o"; word = word.Substring(0, match.Index) + "o";
if (match.Index + 1 < oldText.Length) if (match.Index + 1 < oldText.Length)
{
word += oldText.Substring(match.Index + 1); word += oldText.Substring(match.Index + 1);
} }
} }
}
match = RegExTime2.Match(word, match.Index + 1); match = RegExTime2.Match(word, match.Index + 1);
} }
} }
@ -597,7 +678,9 @@ namespace Nikse.SubtitleEdit.Core.Dictionaries
{ {
//always replace list //always replace list
foreach (string letter in _partialWordReplaceListAlways.Keys) foreach (string letter in _partialWordReplaceListAlways.Keys)
{
word = word.Replace(letter, _partialWordReplaceListAlways[letter]); word = word.Replace(letter, _partialWordReplaceListAlways[letter]);
}
string pre = string.Empty; string pre = string.Empty;
string post = string.Empty; string post = string.Empty;
@ -681,7 +764,9 @@ namespace Nikse.SubtitleEdit.Core.Dictionaries
string preWordPost = pre + word + post; string preWordPost = pre + word + post;
if (word.Length == 0) if (word.Length == 0)
{
return preWordPost; return preWordPost;
}
if (!string.IsNullOrEmpty(pre) || !string.IsNullOrEmpty(post)) if (!string.IsNullOrEmpty(pre) || !string.IsNullOrEmpty(post))
{ {
@ -691,13 +776,17 @@ namespace Nikse.SubtitleEdit.Core.Dictionaries
if (word.Length == from.Length) if (word.Length == from.Length)
{ {
if (string.CompareOrdinal(word, from) == 0) if (string.CompareOrdinal(word, from) == 0)
{
return pre + WordReplaceList[from] + post; return pre + WordReplaceList[from] + post;
} }
}
else if (wordPlusPost.Length == from.Length) else if (wordPlusPost.Length == from.Length)
{ {
if (string.CompareOrdinal(wordPlusPost, from) == 0) if (string.CompareOrdinal(wordPlusPost, from) == 0)
{
return pre + WordReplaceList[from]; return pre + WordReplaceList[from];
} }
}
if (pre.Length + word.Length + post.Length == from.Length && string.CompareOrdinal(preWordPost, from) == 0) if (pre.Length + word.Length + post.Length == from.Length && string.CompareOrdinal(preWordPost, from) == 0)
{ {
return WordReplaceList[from]; return WordReplaceList[from];
@ -709,9 +798,11 @@ namespace Nikse.SubtitleEdit.Core.Dictionaries
foreach (string from in WordReplaceList.Keys) foreach (string from in WordReplaceList.Keys)
{ {
if (word.Length == from.Length && word == from) if (word.Length == from.Length && word == from)
{
return pre + WordReplaceList[from] + post; return pre + WordReplaceList[from] + post;
} }
} }
}
return preWordPost; return preWordPost;
} }
@ -722,7 +813,10 @@ namespace Nikse.SubtitleEdit.Core.Dictionaries
if (DeletePartialLineFromWordList(word)) if (DeletePartialLineFromWordList(word))
{ {
if (PartialLineWordBoundaryReplaceList.ContainsKey(word)) if (PartialLineWordBoundaryReplaceList.ContainsKey(word))
{
PartialLineWordBoundaryReplaceList.Remove(word); PartialLineWordBoundaryReplaceList.Remove(word);
}
return true; return true;
} }
return false; return false;
@ -730,7 +824,10 @@ namespace Nikse.SubtitleEdit.Core.Dictionaries
if (DeleteWordFromWordList(word)) if (DeleteWordFromWordList(word))
{ {
if (WordReplaceList.ContainsKey(word)) if (WordReplaceList.ContainsKey(word))
{
WordReplaceList.Remove(word); WordReplaceList.Remove(word);
}
return true; return true;
} }
return false; return false;
@ -765,9 +862,13 @@ namespace Nikse.SubtitleEdit.Core.Dictionaries
private bool DeleteFromList(string word, XmlDocument userDoc, string replaceListName, string elementName, Dictionary<string, string> dictionary, Dictionary<string, string> userDictionary) private bool DeleteFromList(string word, XmlDocument userDoc, string replaceListName, string elementName, Dictionary<string, string> dictionary, Dictionary<string, string> userDictionary)
{ {
if (dictionary == null) if (dictionary == null)
{
throw new ArgumentNullException(nameof(dictionary)); throw new ArgumentNullException(nameof(dictionary));
}
if (userDictionary == null) if (userDictionary == null)
{
throw new ArgumentNullException(nameof(userDictionary)); throw new ArgumentNullException(nameof(userDictionary));
}
bool removed = false; bool removed = false;
if (userDictionary.ContainsKey((word))) if (userDictionary.ContainsKey((word)))
@ -871,7 +972,9 @@ namespace Nikse.SubtitleEdit.Core.Dictionaries
if (SavePartialLineToWordList(fromWord, toWord)) if (SavePartialLineToWordList(fromWord, toWord))
{ {
if (!PartialLineWordBoundaryReplaceList.ContainsKey(fromWord)) if (!PartialLineWordBoundaryReplaceList.ContainsKey(fromWord))
{
PartialLineWordBoundaryReplaceList.Add(fromWord, toWord); PartialLineWordBoundaryReplaceList.Add(fromWord, toWord);
}
return true; return true;
} }
return false; return false;
@ -879,7 +982,9 @@ namespace Nikse.SubtitleEdit.Core.Dictionaries
if (SaveWordToWordList(fromWord, toWord)) if (SaveWordToWordList(fromWord, toWord))
{ {
if (!WordReplaceList.ContainsKey(fromWord)) if (!WordReplaceList.ContainsKey(fromWord))
{
WordReplaceList.Add(fromWord, toWord); WordReplaceList.Add(fromWord, toWord);
}
return true; return true;
} }
return false; return false;
@ -945,7 +1050,9 @@ namespace Nikse.SubtitleEdit.Core.Dictionaries
{ {
var userDocument = LoadXmlReplaceListUserDocument(); var userDocument = LoadXmlReplaceListUserDocument();
if (!_wholeLineReplaceList.ContainsKey(fromLine)) if (!_wholeLineReplaceList.ContainsKey(fromLine))
{
_wholeLineReplaceList.Add(fromLine, toLine); _wholeLineReplaceList.Add(fromLine, toLine);
}
XmlNode wholeWordsNode = userDocument.DocumentElement?.SelectSingleNode("WholeLines"); XmlNode wholeWordsNode = userDocument.DocumentElement?.SelectSingleNode("WholeLines");
if (wholeWordsNode != null) if (wholeWordsNode != null)
{ {
@ -967,7 +1074,9 @@ namespace Nikse.SubtitleEdit.Core.Dictionaries
public static string ReplaceWord(string text, string word, string newWord) public static string ReplaceWord(string text, string word, string newWord)
{ {
if (string.IsNullOrEmpty(text) || string.IsNullOrEmpty(word)) if (string.IsNullOrEmpty(text) || string.IsNullOrEmpty(word))
{
return text; return text;
}
var sb = new StringBuilder(text.Length); var sb = new StringBuilder(text.Length);
if (text.Contains(word)) if (text.Contains(word))
@ -980,16 +1089,24 @@ namespace Nikse.SubtitleEdit.Core.Dictionaries
{ {
bool startOk = i == 0; bool startOk = i == 0;
if (!startOk) if (!startOk)
{
startOk = (startChars + Environment.NewLine).Contains(text[i - 1]); startOk = (startChars + Environment.NewLine).Contains(text[i - 1]);
}
if (!startOk && word.StartsWith(' ')) if (!startOk && word.StartsWith(' '))
{
startOk = true; startOk = true;
}
if (startOk) if (startOk)
{ {
bool endOk = (i + word.Length == text.Length); bool endOk = (i + word.Length == text.Length);
if (!endOk) if (!endOk)
{
endOk = (startChars + Environment.NewLine).Contains(text[i + word.Length]); endOk = (startChars + Environment.NewLine).Contains(text[i + word.Length]);
}
if (!endOk) if (!endOk)
{
endOk = newWord.EndsWith(' '); endOk = newWord.EndsWith(' ');
}
if (endOk) if (endOk)
{ {
sb.Append(newWord); sb.Append(newWord);
@ -998,9 +1115,11 @@ namespace Nikse.SubtitleEdit.Core.Dictionaries
} }
} }
if (i >= appendFrom) if (i >= appendFrom)
{
sb.Append(text[i]); sb.Append(text[i]);
} }
} }
}
return sb.ToString(); return sb.ToString();
} }

View File

@ -12,24 +12,23 @@ namespace Nikse.SubtitleEdit.Forms
{ {
public sealed partial class Beamer : Form public sealed partial class Beamer : Form
{ {
private Subtitle _subtitle; private readonly Subtitle _subtitle;
private int _index; private int _index;
private bool _fullscreen; private bool _fullscreen;
private Color _subtitleColor = Color.White; private Color _subtitleColor;
private string _subtitleFontName = "Verdana"; private string _subtitleFontName;
private float _subtitleFontSize = 75.0f; private float _subtitleFontSize;
private Color _borderColor = Color.Black; private Color _borderColor;
private float _borderWidth = 2.0f; private float _borderWidth;
private bool _isLoading = true; private readonly bool _isLoading;
private int _marginLeft; private int _marginLeft;
private int _marginBottom = 25; private int _marginBottom = 25;
private int _showIndex = -2; private int _showIndex = -2;
private double _millisecondsFactor = 1.0; private double _millisecondsFactor = 1.0;
private Main _main; private readonly Main _main;
private bool _noTimerAction; private bool _noTimerAction;
private long _videoStartTick; private long _videoStartTick;
//Keys _mainGeneralGoToNextSubtitle = UiUtil.GetKeys(Configuration.Settings.Shortcuts.GeneralGoToNextSubtitle); private readonly Keys _mainGeneralGoToPrevSubtitle = UiUtil.GetKeys(Configuration.Settings.Shortcuts.GeneralGoToPrevSubtitle);
private Keys _mainGeneralGoToPrevSubtitle = UiUtil.GetKeys(Configuration.Settings.Shortcuts.GeneralGoToPrevSubtitle);
public Beamer(Main main, Subtitle subtitle, int index) public Beamer(Main main, Subtitle subtitle, int index)
{ {
@ -39,6 +38,12 @@ namespace Nikse.SubtitleEdit.Forms
_main = main; _main = main;
_subtitle = subtitle; _subtitle = subtitle;
_index = index; _index = index;
_isLoading = true;
_subtitleColor = Color.White;
_subtitleFontName = "Verdana";
_subtitleFontSize = 75.0f;
_borderColor = Color.Black;
_borderWidth = 2.0f;
LanguageStructure.Beamer language = Configuration.Settings.Language.Beamer; LanguageStructure.Beamer language = Configuration.Settings.Language.Beamer;
Text = language.Title; Text = language.Title;
@ -52,7 +57,9 @@ namespace Nikse.SubtitleEdit.Forms
_subtitleFontName = Configuration.Settings.SubtitleBeaming.FontName; _subtitleFontName = Configuration.Settings.SubtitleBeaming.FontName;
_subtitleFontSize = Configuration.Settings.SubtitleBeaming.FontSize; _subtitleFontSize = Configuration.Settings.SubtitleBeaming.FontSize;
if (_subtitleFontSize > 100 || _subtitleFontSize < 10) if (_subtitleFontSize > 100 || _subtitleFontSize < 10)
{
_subtitleFontSize = 60; _subtitleFontSize = 60;
}
_subtitleColor = Configuration.Settings.SubtitleBeaming.FontColor; _subtitleColor = Configuration.Settings.SubtitleBeaming.FontColor;
_borderColor = Configuration.Settings.SubtitleBeaming.BorderColor; _borderColor = Configuration.Settings.SubtitleBeaming.BorderColor;
_borderWidth = Configuration.Settings.SubtitleBeaming.BorderWidth; _borderWidth = Configuration.Settings.SubtitleBeaming.BorderWidth;
@ -61,9 +68,13 @@ namespace Nikse.SubtitleEdit.Forms
panelBorderColor.BackColor = _borderColor; panelBorderColor.BackColor = _borderColor;
if (Configuration.Settings.SubtitleBeaming.BorderWidth > 0 && Configuration.Settings.SubtitleBeaming.BorderWidth < 5) if (Configuration.Settings.SubtitleBeaming.BorderWidth > 0 && Configuration.Settings.SubtitleBeaming.BorderWidth < 5)
{
comboBoxBorderWidth.SelectedIndex = (int)_borderWidth; comboBoxBorderWidth.SelectedIndex = (int)_borderWidth;
}
else else
{
comboBoxBorderWidth.SelectedIndex = 2; comboBoxBorderWidth.SelectedIndex = 2;
}
comboBoxHAlign.SelectedIndexChanged -= ComboBoxHAlignSelectedIndexChanged; comboBoxHAlign.SelectedIndexChanged -= ComboBoxHAlignSelectedIndexChanged;
comboBoxHAlign.SelectedIndex = 1; comboBoxHAlign.SelectedIndex = 1;
@ -74,11 +85,12 @@ namespace Nikse.SubtitleEdit.Forms
{ {
comboBoxSubtitleFont.Items.Add(x.Name); comboBoxSubtitleFont.Items.Add(x.Name);
if (x.Name.Equals(_subtitleFontName, StringComparison.OrdinalIgnoreCase)) if (x.Name.Equals(_subtitleFontName, StringComparison.OrdinalIgnoreCase))
{
comboBoxSubtitleFont.SelectedIndex = comboBoxSubtitleFont.Items.Count - 1; comboBoxSubtitleFont.SelectedIndex = comboBoxSubtitleFont.Items.Count - 1;
} }
}
comboBoxSubtitleFont.SelectedIndexChanged += ComboBoxSubtitleFontSizeSelectedIndexChanged; comboBoxSubtitleFont.SelectedIndexChanged += ComboBoxSubtitleFontSizeSelectedIndexChanged;
// Index 0 = Value: 10; Index 90 = Value: 100;
comboBoxSubtitleFontSize.SelectedIndex = (_subtitleFontSize >= 10 && _subtitleFontSize <= 100) ? (int)(_subtitleFontSize - 10) : 40; comboBoxSubtitleFontSize.SelectedIndex = (_subtitleFontSize >= 10 && _subtitleFontSize <= 100) ? (int)(_subtitleFontSize - 10) : 40;
_isLoading = false; _isLoading = false;
ShowCurrent(); ShowCurrent();
@ -174,7 +186,9 @@ namespace Nikse.SubtitleEdit.Forms
private void SetupImageParameters() private void SetupImageParameters()
{ {
if (_isLoading) if (_isLoading)
{
return; return;
}
_subtitleColor = panelColor.BackColor; _subtitleColor = panelColor.BackColor;
_borderColor = panelBorderColor.BackColor; _borderColor = panelBorderColor.BackColor;
@ -221,9 +235,13 @@ namespace Nikse.SubtitleEdit.Forms
int sizeX = (int)(textSize.Width * 0.8) + 40; int sizeX = (int)(textSize.Width * 0.8) + 40;
int sizeY = (int)(textSize.Height * 0.8) + 30; int sizeY = (int)(textSize.Height * 0.8) + 30;
if (sizeX < 1) if (sizeX < 1)
{
sizeX = 1; sizeX = 1;
}
if (sizeY < 1) if (sizeY < 1)
{
sizeY = 1; sizeY = 1;
}
bmp = new Bitmap(sizeX, sizeY); bmp = new Bitmap(sizeX, sizeY);
g = Graphics.FromImage(bmp); g = Graphics.FromImage(bmp);
@ -231,18 +249,25 @@ namespace Nikse.SubtitleEdit.Forms
foreach (var line in HtmlUtil.RemoveOpenCloseTags(text, HtmlUtil.TagItalic, HtmlUtil.TagFont).SplitToLines()) foreach (var line in HtmlUtil.RemoveOpenCloseTags(text, HtmlUtil.TagItalic, HtmlUtil.TagFont).SplitToLines())
{ {
if (subtitleAlignLeft) if (subtitleAlignLeft)
{
lefts.Add(5); lefts.Add(5);
}
else else
{
lefts.Add((float)(bmp.Width - g.MeasureString(line, font).Width * 0.8 + 15) / 2); lefts.Add((float)(bmp.Width - g.MeasureString(line, font).Width * 0.8 + 15) / 2);
} }
}
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
g.SmoothingMode = SmoothingMode.AntiAlias; g.SmoothingMode = SmoothingMode.AntiAlias;
g.CompositingQuality = CompositingQuality.HighQuality; g.CompositingQuality = CompositingQuality.HighQuality;
var sf = new StringFormat(); var sf = new StringFormat
sf.Alignment = StringAlignment.Near; {
sf.LineAlignment = StringAlignment.Near;// draw the text to a path Alignment = StringAlignment.Near,
LineAlignment = StringAlignment.Near
};
// draw the text to a path
var path = new GraphicsPath(); var path = new GraphicsPath();
// display italic // display italic
@ -251,7 +276,9 @@ namespace Nikse.SubtitleEdit.Forms
bool isItalic = false; bool isItalic = false;
float left = 5; float left = 5;
if (lefts.Count > 0) if (lefts.Count > 0)
{
left = lefts[0]; left = lefts[0];
}
float top = 5; float top = 5;
bool newLine = false; bool newLine = false;
int lineNumber = 0; int lineNumber = 0;
@ -267,7 +294,9 @@ namespace Nikse.SubtitleEdit.Forms
float addLeft = 0; float addLeft = 0;
int oldPathPointIndex = path.PointCount; int oldPathPointIndex = path.PointCount;
if (oldPathPointIndex < 0) if (oldPathPointIndex < 0)
{
oldPathPointIndex = 0; oldPathPointIndex = 0;
}
if (sb.Length > 0) if (sb.Length > 0)
{ {
@ -279,11 +308,15 @@ namespace Nikse.SubtitleEdit.Forms
for (int k = oldPathPointIndex; k < list.Length; k++) for (int k = oldPathPointIndex; k < list.Length; k++)
{ {
if (list[k].X > addLeft) if (list[k].X > addLeft)
{
addLeft = list[k].X; addLeft = list[k].X;
} }
} }
if (addLeft == 0) }
if (Math.Abs(addLeft) < 0.001)
{
addLeft = left + 2; addLeft = left + 2;
}
left = addLeft; left = addLeft;
if (_borderWidth > 0) if (_borderWidth > 0)
@ -344,7 +377,9 @@ namespace Nikse.SubtitleEdit.Forms
float addLeft = 0; float addLeft = 0;
int oldPathPointIndex = path.PointCount - 1; int oldPathPointIndex = path.PointCount - 1;
if (oldPathPointIndex < 0) if (oldPathPointIndex < 0)
{
oldPathPointIndex = 0; oldPathPointIndex = 0;
}
if (sb.Length > 0) if (sb.Length > 0)
{ {
TextDraw.DrawText(font, sf, path, sb, isItalic, subtitleFontBold, false, left, top, ref newLine, leftMargin, ref newLinePathPoint); TextDraw.DrawText(font, sf, path, sb, isItalic, subtitleFontBold, false, left, top, ref newLine, leftMargin, ref newLinePathPoint);
@ -355,21 +390,29 @@ namespace Nikse.SubtitleEdit.Forms
for (int k = oldPathPointIndex; k < list.Length; k++) for (int k = oldPathPointIndex; k < list.Length; k++)
{ {
if (list[k].X > addLeft) if (list[k].X > addLeft)
{
addLeft = list[k].X; addLeft = list[k].X;
} }
} }
if (addLeft == 0) }
if (Math.Abs(addLeft) < 0.001)
{
addLeft = left + 2; addLeft = left + 2;
}
left = addLeft; left = addLeft;
if (_borderWidth > 0) if (_borderWidth > 0)
{
g.DrawPath(new Pen(_borderColor, _borderWidth), path); g.DrawPath(new Pen(_borderColor, _borderWidth), path);
}
g.FillPath(new SolidBrush(c), path); g.FillPath(new SolidBrush(c), path);
path.Reset(); path.Reset();
sb.Clear(); sb.Clear();
if (colorStack.Count > 0) if (colorStack.Count > 0)
{
c = colorStack.Pop(); c = colorStack.Pop();
} }
}
i += 6; i += 6;
} }
else if (text.Substring(i).StartsWith("<i>", StringComparison.OrdinalIgnoreCase)) else if (text.Substring(i).StartsWith("<i>", StringComparison.OrdinalIgnoreCase))
@ -397,7 +440,6 @@ namespace Nikse.SubtitleEdit.Forms
else if (text.Substring(i).StartsWith(Environment.NewLine, StringComparison.Ordinal)) else if (text.Substring(i).StartsWith(Environment.NewLine, StringComparison.Ordinal))
{ {
TextDraw.DrawText(font, sf, path, sb, isItalic, subtitleFontBold, false, left, top, ref newLine, leftMargin, ref newLinePathPoint); TextDraw.DrawText(font, sf, path, sb, isItalic, subtitleFontBold, false, left, top, ref newLine, leftMargin, ref newLinePathPoint);
top += lineHeight; top += lineHeight;
newLine = true; newLine = true;
i += Environment.NewLine.Length - 1; i += Environment.NewLine.Length - 1;
@ -415,11 +457,15 @@ namespace Nikse.SubtitleEdit.Forms
i++; i++;
} }
if (sb.Length > 0) if (sb.Length > 0)
{
TextDraw.DrawText(font, sf, path, sb, isItalic, subtitleFontBold, false, left, top, ref newLine, leftMargin, ref newLinePathPoint); TextDraw.DrawText(font, sf, path, sb, isItalic, subtitleFontBold, false, left, top, ref newLine, leftMargin, ref newLinePathPoint);
}
sf.Dispose(); sf.Dispose();
if (_borderWidth > 0) if (_borderWidth > 0)
{
g.DrawPath(new Pen(_borderColor, _borderWidth), path); g.DrawPath(new Pen(_borderColor, _borderWidth), path);
}
g.FillPath(new SolidBrush(c), path); g.FillPath(new SolidBrush(c), path);
g.Dispose(); g.Dispose();
var nbmp = new NikseBitmap(bmp); var nbmp = new NikseBitmap(bmp);
@ -430,7 +476,9 @@ namespace Nikse.SubtitleEdit.Forms
private void Timer1Tick(object sender, EventArgs e) private void Timer1Tick(object sender, EventArgs e)
{ {
if (_noTimerAction) if (_noTimerAction)
{
return; return;
}
double positionInMilliseconds = (DateTime.Now.Ticks - _videoStartTick) / 10000.0D; // 10,000 ticks = 1 millisecond double positionInMilliseconds = (DateTime.Now.Ticks - _videoStartTick) / 10000.0D; // 10,000 ticks = 1 millisecond
positionInMilliseconds *= _millisecondsFactor; positionInMilliseconds *= _millisecondsFactor;
@ -445,8 +493,9 @@ namespace Nikse.SubtitleEdit.Forms
index++; index++;
} }
if (index == _subtitle.Paragraphs.Count) if (index == _subtitle.Paragraphs.Count)
{
index = -1; index = -1;
}
if (index == -1) if (index == -1)
{ {
pictureBox1.Image = null; pictureBox1.Image = null;
@ -508,32 +557,44 @@ namespace Nikse.SubtitleEdit.Forms
else if (e.KeyCode == Keys.Space || (e.KeyCode == Keys.Down && e.Modifiers == Keys.Alt) || _mainGeneralGoToPrevSubtitle == e.KeyData) else if (e.KeyCode == Keys.Space || (e.KeyCode == Keys.Down && e.Modifiers == Keys.Alt) || _mainGeneralGoToPrevSubtitle == e.KeyData)
{ {
if (_index < _subtitle.Paragraphs.Count - 1) if (_index < _subtitle.Paragraphs.Count - 1)
{
_index++; _index++;
}
ShowCurrent(); ShowCurrent();
e.Handled = true; e.Handled = true;
} }
else if (_mainGeneralGoToPrevSubtitle == e.KeyData || (e.KeyCode == Keys.Up && e.Modifiers == Keys.Alt)) else if (_mainGeneralGoToPrevSubtitle == e.KeyData || (e.KeyCode == Keys.Up && e.Modifiers == Keys.Alt))
{ {
if (_index > 0) if (_index > 0)
{
_index--; _index--;
}
ShowCurrent(); ShowCurrent();
e.Handled = true; e.Handled = true;
} }
else if (e.Modifiers == Keys.None && e.KeyCode == Keys.PageDown) else if (e.Modifiers == Keys.None && e.KeyCode == Keys.PageDown)
{ {
if (_index < _subtitle.Paragraphs.Count - 21) if (_index < _subtitle.Paragraphs.Count - 21)
{
_index += 20; _index += 20;
}
else else
{
_index = _subtitle.Paragraphs.Count - 1; _index = _subtitle.Paragraphs.Count - 1;
}
ShowCurrent(); ShowCurrent();
e.Handled = true; e.Handled = true;
} }
else if (e.Modifiers == Keys.None && e.KeyCode == Keys.PageUp) else if (e.Modifiers == Keys.None && e.KeyCode == Keys.PageUp)
{ {
if (_index > 20) if (_index > 20)
{
_index -= 20; _index -= 20;
}
else else
{
_index = 0; _index = 0;
}
ShowCurrent(); ShowCurrent();
e.Handled = true; e.Handled = true;
} }
@ -565,14 +626,17 @@ namespace Nikse.SubtitleEdit.Forms
timer1.Enabled = false; timer1.Enabled = false;
System.Threading.Thread.Sleep(100); System.Threading.Thread.Sleep(100);
if (_index < _subtitle.Paragraphs.Count - 1) if (_index < _subtitle.Paragraphs.Count - 1)
{
_index++; _index++;
}
_videoStartTick = DateTime.Now.Ticks - ((long)(_subtitle.Paragraphs[_index].StartTime.TotalMilliseconds) * 10000); //10,000 ticks = 1 millisecond _videoStartTick = DateTime.Now.Ticks - ((long)(_subtitle.Paragraphs[_index].StartTime.TotalMilliseconds) * 10000); //10,000 ticks = 1 millisecond
ShowCurrent(); ShowCurrent();
_noTimerAction = false; _noTimerAction = false;
if (timer1Enabled || _fullscreen) if (timer1Enabled || _fullscreen)
{
timer1.Start(); timer1.Start();
}
e.Handled = true; e.Handled = true;
} }
@ -582,7 +646,9 @@ namespace Nikse.SubtitleEdit.Forms
timer1.Enabled = false; timer1.Enabled = false;
System.Threading.Thread.Sleep(100); System.Threading.Thread.Sleep(100);
if (_index > 0) if (_index > 0)
{
_index--; _index--;
}
_videoStartTick = DateTime.Now.Ticks - ((long)(_subtitle.Paragraphs[_index].StartTime.TotalMilliseconds) * 10000); //10,000 ticks = 1 millisecond _videoStartTick = DateTime.Now.Ticks - ((long)(_subtitle.Paragraphs[_index].StartTime.TotalMilliseconds) * 10000); //10,000 ticks = 1 millisecond
ShowCurrent(); ShowCurrent();
if (timer1Enabled) if (timer1Enabled)
@ -591,18 +657,26 @@ namespace Nikse.SubtitleEdit.Forms
else if (e.Modifiers == Keys.None && e.KeyCode == Keys.PageDown) else if (e.Modifiers == Keys.None && e.KeyCode == Keys.PageDown)
{ {
if (_index < _subtitle.Paragraphs.Count - 21) if (_index < _subtitle.Paragraphs.Count - 21)
{
_index += 20; _index += 20;
}
else else
{
_index = _subtitle.Paragraphs.Count - 1; _index = _subtitle.Paragraphs.Count - 1;
}
ShowCurrent(); ShowCurrent();
e.Handled = true; e.Handled = true;
} }
else if (e.Modifiers == Keys.None && e.KeyCode == Keys.PageUp) else if (e.Modifiers == Keys.None && e.KeyCode == Keys.PageUp)
{ {
if (_index > 20) if (_index > 20)
{
_index -= 20; _index -= 20;
}
else else
{
_index = 0; _index = 0;
}
ShowCurrent(); ShowCurrent();
e.Handled = true; e.Handled = true;
} }
@ -669,6 +743,7 @@ namespace Nikse.SubtitleEdit.Forms
private void BeamerFormClosing(object sender, FormClosingEventArgs e) private void BeamerFormClosing(object sender, FormClosingEventArgs e)
{ {
Cursor.Show(); Cursor.Show();
// Save user-configurations. // Save user-configurations.
Configuration.Settings.SubtitleBeaming.FontName = _subtitleFontName; Configuration.Settings.SubtitleBeaming.FontName = _subtitleFontName;
Configuration.Settings.SubtitleBeaming.FontSize = (int)_subtitleFontSize; Configuration.Settings.SubtitleBeaming.FontSize = (int)_subtitleFontSize;