From 85d0e02a4f349ffb1d86cd55b84dc47c8f2e21d8 Mon Sep 17 00:00:00 2001 From: Kathy Date: Tue, 14 May 2013 12:44:03 +0000 Subject: [PATCH] Handle embedded text in xyplot for CPLS --- .../Extension/DocumentExt.cs | 91 ++++++++++++++++++- 1 file changed, 88 insertions(+), 3 deletions(-) diff --git a/PROMS/VEPROMS.CSLA.Library/Extension/DocumentExt.cs b/PROMS/VEPROMS.CSLA.Library/Extension/DocumentExt.cs index 885533eb..1d206a71 100644 --- a/PROMS/VEPROMS.CSLA.Library/Extension/DocumentExt.cs +++ b/PROMS/VEPROMS.CSLA.Library/Extension/DocumentExt.cs @@ -656,9 +656,21 @@ namespace VEPROMS.CSLA.Library LBRange myRange = selxy.Paragraphs.First.Range; float yTop = (float)myRange.get_Information(LBWdInformation.wdVerticalPositionRelativeToPage); float yTop1 = (float)myRange.get_Information(LBWdInformation.wdVerticalPositionRelativeToTextBoundary); - - selxy.Text = ""; - if (cnt>0)for (int icnt = 0; icnt < cnt; icnt++) selxy.Text = selxy.Text + "\r"; + + // some data had regular text embedded in the xyplot definition. If so, the plain text must + // be left in the file. Otherwise, we can replace with empty text. + string resXyPlot = xyplot; + string txt = FindEmbeddedText(selxy.Text, ref resXyPlot); + if (txt != null) + { + selxy.Text = txt; + xyplot = resXyPlot.Replace(">\r>", ">>\r"); + } + else + { + selxy.Text = ""; + if (cnt > 0) for (int icnt = 0; icnt < cnt; icnt++) selxy.Text = selxy.Text + "\r"; + } pngFile = VlnSettings.TemporaryFolder + @"\XYPlot" + filecount.ToString() + @".png"; //@"C:\Temp\XYPlot1.png"; filecount++; @@ -811,6 +823,79 @@ namespace VEPROMS.CSLA.Library return fileName; } } + private static string FindEmbeddedText(string p, ref string resXyPlot) + { + StringBuilder sb = new StringBuilder(); // contains embedded text + StringBuilder xy = new StringBuilder(); // contains xyplot without embedded text. + string origXy = p; + bool findEmbeddedText = false; + // get past opening command: + int indxCmd = p.IndexOf("<"); + indxCmd = p.IndexOf("<", indxCmd + 1); + xy.Append(p.Substring(0, indxCmd)); + // While there are xyplot commands, check for beginning/ending. + // Any text not in a command, save. + while (indxCmd > 0) + { + // find end of command. Look for another beginning of command, newline, + // regular text, or end of graph and handle each of these cases. + int indxClose = GetNonStringCmdClose(p, indxCmd + 1); // p.IndexOf(">", indxCmd + 1); + int nxtIndxCmd = p.IndexOf("<", indxCmd + 1); + xy.Append(p.Substring(indxCmd, indxClose - indxCmd+1)); + xy.Append("\r"); // the xyplot code assumes a return exists. + // get substrings between end index & start of next and if any + // non white space text, add it to return string and flag that we have + // embedded text. If there's a newline and just other white space put a + // newline in return string. + if (nxtIndxCmd > 0) + { + string mydebug = p.Substring(indxClose + 1); + for (int i = indxClose + 1; i < nxtIndxCmd; i++) + { + if (!System.Char.IsWhiteSpace(p[i])) + { + sb.Append(p[i]); + findEmbeddedText = true; + } + if (p[i] == '\r' || p[i] == ' ') sb.Append(p[i]); + } + } + indxCmd = nxtIndxCmd; + } + xy.Append(">"); + if (findEmbeddedText) + { + resXyPlot = xy.ToString(); + return sb.ToString(); + } + resXyPlot = origXy; + return null; + } + + private static int GetNonStringCmdClose(string p, int indxCmd) + { + int indxClose = p.IndexOf(">", indxCmd); + if (indxClose >= 0) + { + // if there are any quotes between the open of command and close, + // need to see if the close command is quoted. + if (p.IndexOf('"', indxCmd, indxClose - indxCmd) > -1) + { + // see how many quotes between open/close command, if + // an odd number, the close command char '>' is in quotes. + int countQuotes = 0; + for (int i = indxCmd; i < indxClose; i++) if (p[i] == '"') countQuotes++; + if ((countQuotes % 2) != 0) + { + // find quote after close, and then get next close. + int closeQuote = p.IndexOf('"', indxClose); + if (closeQuote < 0) return -1; // this is error, so return end not found. + return p.IndexOf(">", closeQuote + 1); + } + } + } + return indxClose; + } public static void AddPrecedingText(LBSelection sel, string val) { if (val.IndexOf("_") == -1) // Is some of the text underlined?