From df1a2a1d470f9c37869ee57d1b4479d81bd87289 Mon Sep 17 00:00:00 2001 From: John Date: Thu, 25 Mar 2010 14:57:38 +0000 Subject: [PATCH] --- PROMS/Volian.Controls.Library/DisplayText.cs | 422 +++++++++++++------ 1 file changed, 295 insertions(+), 127 deletions(-) diff --git a/PROMS/Volian.Controls.Library/DisplayText.cs b/PROMS/Volian.Controls.Library/DisplayText.cs index ec7057ea..5e5bbc1f 100644 --- a/PROMS/Volian.Controls.Library/DisplayText.cs +++ b/PROMS/Volian.Controls.Library/DisplayText.cs @@ -115,7 +115,7 @@ namespace Volian.Controls.Library _FieldToEdit = fieldToEdit; _MyItemInfo = itemInfo; OriginalText = InfoText; - TextFont = GetItemFont(); + TextFont = itemInfo.GetItemFont();//GetItemFont(); string text = InfoText; // if in print mode, view mode, or non-active richtextbox do replace words. Only if in @@ -216,7 +216,8 @@ namespace Volian.Controls.Library RtfToDisplayTextElements(OriginalText); List origList = GetLinkList(DisplayTextElementList); // now get new text into displaytext elements for comparison for links: - RtfToDisplayTextElements(rtb.Rtf); + //RtfToDisplayTextElements(rtb.Rtf); + RtfToDisplayTextElements(modtext); // Compare ro/transition lists and delete or add any to the item for any ros/transitions that have been // added/deleted or modified. @@ -469,7 +470,8 @@ namespace Volian.Controls.Library if (text == null || text == "") return; - string noExtraRtfStr = RtfToDbText(text); + //string noExtraRtfStr = RtfToDbText(text); + string noExtraRtfStr = text; int startIndex = 0; int index = -1; @@ -500,7 +502,8 @@ namespace Volian.Controls.Library // Check for two links in a row & if found, add separating rtf comment // commands (these get removed in the richtextbox: - noExtraRtfStr = noExtraRtfStr.Replace(@"[END>\v0 \v \v0 \v 0) || ((TextFont.Style & E_Style.MmBold) > 0)) + // { + // retval = RemoveToken(retval, @"\\b0"); + // retval = RemoveToken(retval, @"\\b"); + // } + // if ((TextFont.Style & E_Style.Underline) > 0) + // { + // retval = RemoveToken(retval, @"\\ulnone"); + // retval = RemoveToken(retval, @"\\ul"); + // } + // if ((TextFont.Style & E_Style.Italics) > 0) + // { + // retval = RemoveToken(retval, @"\\i0"); + // retval = RemoveToken(retval, @"\\i"); + // } + // return retval; + //} + //private string RemoveToken(string str, string token) + //{ + // // if this token is preceeded by another token and followed by a space + // // leave the preceeding token the ending space + // string retval = Regex.Replace(str, @"(\\[^ \\?\r\n\t]*)" + token + " ", "$1 "); + // if (retval != str) + // Console.WriteLine("leave the preceeding token the ending space"); + // // otherwise replace the token optionally followed by a space + // retval = Regex.Replace(retval, token + " ?", ""); + // return retval; + //} + //private static string StaticRemoveToken(string str, string token) + //{ + // // if this token is preceeded by another token and followed by a space + // // leave the preceeding token the ending space + // string retval = Regex.Replace(str, @"(\\[^ \\?\r\n\t]*)" + token + " ", "$1 "); + // if (retval != str) + // Console.WriteLine("leave the preceeding token the ending space"); + // // otherwise replace the token optionally followed by a space + // retval = Regex.Replace(retval, token + " ?", ""); + // return retval; + //} + //public static string ReplaceRTFClause(Match m) + //{ + // switch (m.Value[1]) + // { + // case 'u': + // if (Regex.IsMatch(m.Value, @"\\u[0-9]+")) + // return m.Value; // Special Charcaters + // if (Regex.IsMatch(m.Value, @"\\ulnone")) + // return m.Value; + // if (Regex.IsMatch(m.Value, @"\\ul.*")) + // return m.Value; // Underline + // break; + // case '\'': // Special Character + // return m.Value; + // case 'b': // Bold + // return m.Value; + // case 's': // sub or super.... + // if (m.Value == @"\sub") return m.Value; + // if (m.Value == @"\super") return m.Value; + // break; + // case 'n': // nosubsuper... + // if (m.Value == @"\nosupersub") return m.Value; + // break; + // case 'i': // Italics + // return m.Value; + // case '{': // look for escape for curly braces: + // return m.Value; + // case '}': + // return m.Value; + // case 'v': // save link hidden info + // if (m.Value == @"\v") return m.Value; // comment part of link + // // end comment may end in space or may end in '\' if another rtf command, + // // or may end at end of string. First check for space, keep it in string + // // if it is there. + // if (Regex.IsMatch(m.Value, @"\\v0 ")) + // return m.Value; + // if (Regex.IsMatch(m.Value, @"\\v0")) + // return m.Value; + // break; + // case 'l': + // if (m.Value == @"\line") return m.Value; + // break; + // case 'p': + // if (m.Value == @"\par") return "\r\n"; + // //if (m.Value == @"\protect") + // // return m.Value; + // //if (m.Value == @"\protect0") + // // return m.Value; + // if (m.Value.Length>=6 && m.Value.Substring(0,6) == "\\par\r\n") return m.Value.Replace("\r\n", " "); + // break; + // case 'f': // handle fonts separately because they may or may not have a space after them + // if (m.Value[2]>='0' && m.Value[2]<='9')return m.Value; + // break; + // } + // return "";//Strip All + //} + public static string StaticReplaceRTFClause(Match m) { - string retval = rtf; - // remove rtf commands for any styles that were added. Note that if - // the entire item has a style, and also contains 'pieces' of text with - // the same style, the underlying rtf box removes the embedded rtf commands, - // for example, if the entire step is bolded, and 'THEN' has bold on/off - // surrounding it, the rtf box removes the bold around the 'THEN' - // These remove the command with a following space or the command alone, - // either case may exist, because if there are rtf commands following the - // style command, there will be no space character following the style command. - if (((TextFont.Style & E_Style.Bold) > 0) || ((TextFont.Style & E_Style.MmBold) > 0)) + try { - retval = Regex.Replace(retval, @"\\b0 ?", ""); - retval = Regex.Replace(retval, @"\\b ?",""); + string token = m.Groups[1].Value; + switch (token[1]) + { + case 'u': + if (Regex.IsMatch(token, @"^\\u[0-9]+$")) + return token; // Special Charcaters + if (Regex.IsMatch(token, @"^\\ulnone ?$")) + return token; + if (Regex.IsMatch(token, @"^\\ul.*$")) + return token; // Underline + break; + case '\'': // Special Character + return token; + case 'b': // Bold + return token; + case 's': // sub or super.... + if (Regex.IsMatch(token, @"^\\sub ?$")) return token; + if (Regex.IsMatch(token, @"^\\super ?$")) return token; + break; + case 'n': // nosubsuper... + if (Regex.IsMatch(token, @"^\\nosupersub ?$")) return token; + break; + case 'i': // Italics + return token; + case '{': // look for escape for curly braces: + return token; + case '}': + return token; + case 'v': // save link hidden info + if (Regex.IsMatch(token, @"^\\v0? ?$")) return token; // comment part of link + // end comment may end in space or may end in '\' if another rtf command, + // or may end at end of string. First check for space, keep it in string + // if it is there. + //if (Regex.IsMatch(token, @"\\v0 ")) + // return token; + //if (Regex.IsMatch(token, @"\\v0")) + // return token; + break; + case 'l': + if (Regex.IsMatch(token, @"^\\line ?$")) return token; + break; + case 'p': + if (Regex.IsMatch(token, @"^\\par ?$")) return "\r\n"; + //if (token == @"\protect") + // return token; + //if (token == @"\protect0") + // return token; + //if (token.Length >= 6 && token.Substring(0, 6) == "\\par\r\n") return token.Replace("\r\n", " "); + break; + //case 'f': // handle fonts separately because they may or may not have a space after them + // if (token[2] >= '0' && token[2] <= '9') return token; + // break; + } } - if ((TextFont.Style & E_Style.Underline) > 0) + catch (Exception ex) { - retval = Regex.Replace(retval, @"\\ulnone ?", ""); - retval = Regex.Replace(retval, @"\\ul ?", ""); - } - if ((TextFont.Style & E_Style.Italics) > 0) - { - retval = Regex.Replace(retval, @"\\i0 ?", ""); - retval = Regex.Replace(retval, @"\\i ?", ""); - } - return retval; - } - public string ReplaceRTFClause(Match m) - { - switch (m.Value[1]) - { - case 'u': - if (Regex.IsMatch(m.Value, @"\\u[0-9]+")) - return m.Value; // Special Charcaters - if (Regex.IsMatch(m.Value, @"\\ulnone")) - return m.Value; - if (Regex.IsMatch(m.Value, @"\\ul.*")) - return m.Value; // Underline - break; - case '\'': // Special Character - return m.Value; - case 'b': // Bold - return m.Value; - case 's': // sub or super.... - if (m.Value == @"\sub") return m.Value; - if (m.Value == @"\super") return m.Value; - break; - case 'n': // nosubsuper... - if (m.Value == @"\nosupersub") return m.Value; - break; - case 'i': // Italics - return m.Value; - case '{': // look for escape for curly braces: - return m.Value; - case '}': - return m.Value; - case 'v': // save link hidden info - if (m.Value == @"\v") return m.Value; // comment part of link - // end comment may end in space or may end in '\' if another rtf command, - // or may end at end of string. First check for space, keep it in string - // if it is there. - if (Regex.IsMatch(m.Value, @"\\v0 ")) - return m.Value; - if (Regex.IsMatch(m.Value, @"\\v0")) - return m.Value; - break; - case 'l': - if (m.Value == @"\line") return m.Value; - break; - case 'p': - if (m.Value == @"\par") return "\r\n"; - //if (m.Value == @"\protect") - // return m.Value; - //if (m.Value == @"\protect0") - // return m.Value; - if (m.Value.Length>=6 && m.Value.Substring(0,6) == "\\par\r\n") return m.Value.Replace("\r\n", " "); - break; - case 'f': // handle fonts separately because they may or may not have a space after them - if (m.Value[2]>='0' && m.Value[2]<='9')return m.Value; - break; + Console.WriteLine("{0}-{1}",ex.GetType().Name, ex.Message); } return "";//Strip All } - private string StripRtfCommands(string rtf) + public string StripRtfCommands(string rtf) { // replace \{ & \} with (![ & (!] respectively and then redo at end. The curly braces // are rtf so were getting removed and/or not handled correctly. string retval = rtf.Replace(@"\{", @"(!["); retval = retval.Replace(@"\}", @"(!]"); - + + //// remove carriage return/newlines after \par commands (these are introduced by rtb + //// for hard returns, goes into rtb as \par and comes out as \par\r\n): + //retval = Regex.Replace(retval, @"\\par\r\n([^\\.*?])", "\\par $1"); + //retval = Regex.Replace(retval, @"\\par\r\n([\\.*?])", "\\par$1"); + // remove carriage return/newlines after \par commands (these are introduced by rtb // for hard returns, goes into rtb as \par and comes out as \par\r\n): - retval = Regex.Replace(retval, @"\\par\r\n([^\\.*?])", "\\par $1"); - retval = Regex.Replace(retval, @"\\par\r\n([\\.*?])", "\\par$1"); + retval = Regex.Replace(retval, @"\\par\r\n(?!\\)", "\\par "); + + //retval = Regex.Replace(retval, @"[\r\n]", "", RegexOptions.Singleline); // Strip Carriage Returns and Newlines + //retval = Regex.Replace(retval, @"^\{(.*)\}$", "$1", RegexOptions.Singleline); // Strip Opening and Closing Braces + //retval = Regex.Replace(retval, @"\{[^{]*?\}", "", RegexOptions.Singleline); // Strip Clauses - remove anything from curly braces + //retval = Regex.Replace(retval, @"\{[^{]*?\}", "", RegexOptions.Singleline); // Strip Clauses - remove anything from curly braces + //retval = Regex.Replace(retval, @"(\\[^ \\?\r\n\t]+)\\f[0-9] ", "$1 "); // remove font command - if next to another command, keep other command and space + //// OLD: The following two lines are replaced by the more generic replace above. + ////retval = Regex.Replace(retval, @"\\v\\f[0-9] ", "\\v "); // remove font command - if next to Comment keep space + ////retval = Regex.Replace(retval, @"\\line\\f[0-9] ", "\\line "); // remove font command - if next to Line keep space + //retval = Regex.Replace(retval, @"\\f[0-9] ", ""); // remove font command with ending space + //retval = Regex.Replace(retval, @"\\f[0-9]", ""); // remove font command without ending space + //retval = Regex.Replace(retval, @"\\par ", "\r\n"); + //retval = Regex.Replace(retval, @"\\[^ \\?\r\n\t]+", new MatchEvaluator(ReplaceRTFClause)); // take backslash xyz and evaluates them + //// remove a space if there is one as the first character or the last character + //if (retval[0] == ' ') retval = retval.Remove(0, 1); + //retval = retval.TrimEnd(' '); + //// remove \r\n at end of string if the string has 2 or more characters + //if (retval.Length > 1 && retval.Substring(retval.Length - 2, 2) == "\r\n") retval = retval.Remove(retval.Length - 2, 2); + //// remove \par at end of string if the string has 4 or more characters + //if (retval.Length > 3 && retval.Substring(retval.Length - 4, 4) == @"\par") retval = retval.Remove(retval.Length - 4, 4); + //// remove a space following \r\n + //retval = Regex.Replace(retval, "\r\n ", "\r\n"); + ////if there are still spaces following \r\n, then probable in a table - we need to put the space back + //retval = Regex.Replace(retval, "\r\n ", "\r\n "); retval = Regex.Replace(retval, @"[\r\n]", "", RegexOptions.Singleline); // Strip Carriage Returns and Newlines retval = Regex.Replace(retval, @"^\{(.*)\}$", "$1", RegexOptions.Singleline); // Strip Opening and Closing Braces retval = Regex.Replace(retval, @"\{[^{]*?\}", "", RegexOptions.Singleline); // Strip Clauses - remove anything from curly braces retval = Regex.Replace(retval, @"\{[^{]*?\}", "", RegexOptions.Singleline); // Strip Clauses - remove anything from curly braces - retval = Regex.Replace(retval, @"(\\[^ \\?]+)\\f[0-9] ", "$1 "); // remove font command - if next to another command, keep other command and space - // OLD: The following two lines are replaced by the more generic replace above. - //retval = Regex.Replace(retval, @"\\v\\f[0-9] ", "\\v "); // remove font command - if next to Comment keep space - //retval = Regex.Replace(retval, @"\\line\\f[0-9] ", "\\line "); // remove font command - if next to Line keep space - retval = Regex.Replace(retval, @"\\f[0-9] ", ""); // remove font command with ending space - retval = Regex.Replace(retval, @"\\f[0-9]", ""); // remove font command without ending space - retval = Regex.Replace(retval, @"\\par ", "\r\n"); - retval = Regex.Replace(retval, @"\\[^ \\?]+", new MatchEvaluator(ReplaceRTFClause)); // take backslash xyz and evaluates them - // remove a space if there is one as the first character or the last character - if (retval[0]==' ')retval = retval.Remove(0, 1); - retval = retval.TrimEnd(' '); + retval = Regex.Replace(retval, @"(\\[^' \\?\r\n\t]+)(?=\\)", "$1 "); // add space after token if followed by token + retval = Regex.Replace(retval, @"(\\[^' \\?\r\n\t]+ )", new MatchEvaluator(StaticReplaceRTFClause)); // take backslash xyz and evaluates them + retval = Regex.Replace(retval, @"(\\[^' \\?\r\n\t]+) (?=\\)", "$1"); // remove space between tokens + retval = Regex.Replace(retval, @"(\\[^' \\?\r\n\t]+) (?=\r\n)", "$1"); // remove space before /r/n // remove \r\n at end of string if the string has 2 or more characters if (retval.Length > 1 && retval.Substring(retval.Length - 2, 2) == "\r\n") retval = retval.Remove(retval.Length - 2, 2); - // remove \par at end of string if the string has 4 or more characters - if (retval.Length > 3 && retval.Substring(retval.Length - 4, 4) == @"\par") retval = retval.Remove(retval.Length - 4, 4); if (retval.Length == 0) return ""; if (retval.Substring(retval.Length - 2, 2) == @"\v") retval = retval.Remove(retval.Length - 2, 2); - retval = RemoveRtfStyles(retval); + retval = _MyItemInfo.RemoveRtfStyles(retval); // RemoveRtfStyles(retval); retval = retval.Replace(@"(![", @"\{"); retval = retval.Replace(@"(!]", @"\}"); + retval = retval.TrimEnd(' '); + return retval; + } + //public static string StaticStripRtfCommands(string rtf, ItemInfo itmInfo) + public static string StaticStripRtfCommands(string rtf) + { + // replace \{ & \} with (![ & (!] respectively and then redo at end. The curly braces + // are rtf so were getting removed and/or not handled correctly. + string retval = rtf.Replace(@"\{", @"(!["); + retval = retval.Replace(@"\}", @"(!]"); + + // For hardspaces, the windows richtextbox does some 'quirky' things: + // A unicode representation of \u160? is sent INTO the rtb. Coming out, + // that \u160? was translated to a \~ (by the underlying windows rtb). + // Note that if the \~ is sent to the rtb, it is treated as a regular space, + // i.e. no longer a hardspace, and actually is converted to a regular space. + // SO, on the way out, convert any \~ to \u160? + retval = retval.Replace(@"\~", @"\u160?"); + + // remove carriage return/newlines after \par commands (these are introduced by rtb + // for hard returns, goes into rtb as \par and comes out as \par\r\n): + retval = Regex.Replace(retval, @"\\par\r\n(?!\\)", "\\par "); + //retval = Regex.Replace(retval, @"\\par\r\n(?=\\)", "\\par"); + + retval = Regex.Replace(retval, @"[\r\n]", "", RegexOptions.Singleline); // Strip Carriage Returns and Newlines + retval = Regex.Replace(retval, @"^\{(.*)\}$", "$1", RegexOptions.Singleline); // Strip Opening and Closing Braces + retval = Regex.Replace(retval, @"\{[^{]*?\}", "", RegexOptions.Singleline); // Strip Clauses - remove anything from curly braces + retval = Regex.Replace(retval, @"\{[^{]*?\}", "", RegexOptions.Singleline); // Strip Clauses - remove anything from curly braces + //retval = Regex.Replace(retval, @"(\\[^ \\?\r\n\t]+)\\f[0-9] ", "$1 "); // remove font command - if next to another command, keep other command and space + // OLD: The following two lines are replaced by the more generic replace above. + //retval = Regex.Replace(retval, @"\\v\\f[0-9] ", "\\v "); // remove font command - if next to Comment keep space + //retval = Regex.Replace(retval, @"\\line\\f[0-9] ", "\\line "); // remove font command - if next to Line keep space + //retval = Regex.Replace(retval, @"\\f[0-9] ", ""); // remove font command with ending space + //retval = Regex.Replace(retval, @"\\f[0-9]", ""); // remove font command without ending space + //retval = Regex.Replace(retval, @"\\par ", "\r\n"); + retval = Regex.Replace(retval, @"(\\[^' \\?\r\n\t]+)(?=\\)", "$1 "); // add space after token if followed by token + //retval = Regex.Replace(retval, @"(\\[^ \\?\r\n\t]+)(\\)", "$1 $2"); // take backslash xyz and evaluates them + retval = Regex.Replace(retval, @"(\\[^ \\?\r\n\t]+ )", new MatchEvaluator(StaticReplaceRTFClause)); // take backslash xyz and evaluates them + retval = Regex.Replace(retval, @"(\\[^ \\?\r\n\t]+) (?=\\)", "$1"); // remove space between tokens + retval = Regex.Replace(retval, @"(\\[^ \\?\r\n\t]+) (?=\r\n)", "$1"); // remove space before /r/n + // remove a space if there is one as the first character or the last character + //if (retval[0] == ' ') retval = retval.Remove(0, 1); + //retval = retval.TrimEnd(' '); + // remove \r\n at end of string if the string has 2 or more characters + if (retval.Length > 1 && retval.Substring(retval.Length - 2, 2) == "\r\n") retval = retval.Remove(retval.Length - 2, 2); + // remove \par at end of string if the string has 4 or more characters + //if (retval.Length > 3 && retval.Substring(retval.Length - 4, 4) == @"\par") retval = retval.Remove(retval.Length - 4, 4); + // remove a space following \r\n + //retval = Regex.Replace(retval, "\r\n ", "\r\n"); + ////if there are still spaces following \r\n, then probable in a table - we need to put the space back + //retval = Regex.Replace(retval, "\r\n ", "\r\n "); + + if (retval.Length == 0) return ""; + if (retval.Length > 1 && retval.Substring(retval.Length - 2, 2) == @"\v") retval = retval.Remove(retval.Length - 2, 2); + //retval = RemoveRtfStyles(retval); + //if (itmInfo != null) + // retval = StaticRemoveRtfStyles(retval, itmInfo); + + retval = retval.Replace(@"(![", @"\{"); + retval = retval.Replace(@"(!]", @"\}"); + retval = retval.TrimEnd(' '); + + // the indent character was translated in the richtextbox, change it back: + if (retval.IndexOf(@"\'05") > -1) retval = retval.Replace(@"\'05", "\x05"); return retval; } #endregion #region StyleData - private VE_Font GetItemFont() - { - VE_Font font = null; - FormatInfo format = _MyItemInfo.ActiveFormat; - int type = (int)_MyItemInfo.MyContent.Type; - switch (type/10000) - { - case 0: // procedure - font = format.PlantFormat.FormatData.Font; - break; - case 1: // section - font = format.PlantFormat.FormatData.SectData.SectionHeader.Font; - break; - case 2: // step types - int typindx = type - 20000; // what to do for other types rather than steps - font = format.PlantFormat.FormatData.StepDataList[typindx].Font; - break; - } - TextFont = font; - return font; - } + //private VE_Font GetItemFont() + //{ + // VE_Font font = null; + // FormatInfo format = _MyItemInfo.ActiveFormat; + // int type = (int)_MyItemInfo.MyContent.Type; + // switch (type/10000) + // { + // case 0: // procedure + // font = format.PlantFormat.FormatData.Font; + // break; + // case 1: // section + // font = format.PlantFormat.FormatData.SectData.SectionHeader.Font; + // break; + // case 2: // step types + // int typindx = type - 20000; // what to do for other types rather than steps + // font = format.PlantFormat.FormatData.StepDataList[typindx].Font; + // break; + // } + // TextFont = font; + // return font; + //} #endregion #region DoListElements private int FindTokenChar(string txt, int startIndex) @@ -711,13 +883,7 @@ namespace Volian.Controls.Library if (link.IndexOf("") != -1) return text; int transitionID = Convert.ToInt32(link.Split(" ".ToCharArray())[1]); // Find the transition - if (_MyItemInfo.MyContent.ContentTransitionCount <= 0) - { - // TODO: RHM 20100310 - _MyItemInfo.MyContent.RefreshContentTransitions(); - if (_MyItemInfo.MyContent.ContentTransitionCount <= 0) - return "*Resolved Transition Text*"; - } + if (_MyItemInfo.MyContent.ContentTransitionCount <= 0) return "*Resolved Transition Text*"; foreach (TransitionInfo ti in _MyItemInfo.MyContent.ContentTransitions) { if (ti.TransitionID == transitionID) @@ -752,6 +918,8 @@ namespace Volian.Controls.Library if (endLinkIndxV == -1) endLinkIndxV = text.IndexOf(@"\v0", linkIndx); // at end of string int endLinkIndxE = text.IndexOf(@"[END>", linkIndx); int endLinkIndx = (endLinkIndxV < endLinkIndxE) ? endLinkIndxV : endLinkIndxE; + if (endLinkIndx == -1) + Console.WriteLine("{0},{1},{2},'{3}'", endLinkIndx, linkIndx, text.Length,text.Substring(linkIndx)); vte.Link = text.Substring(linkIndx + 6, endLinkIndx - linkIndx - 6); // 6 for #Link: string tmptxt = null;