public virtual void ProcessWhiteSpaceBreakLine() { //Create tree TextSvgBranchRenderer root = new TextSvgBranchRenderer(); TextLeafSvgNodeRenderer textBefore = new TextLeafSvgNodeRenderer(); textBefore.SetAttribute(SvgConstants.Attributes.TEXT_CONTENT, "\n" + " text\n" + " " ); root.AddChild(textBefore); TextSvgBranchRenderer span = new TextSvgBranchRenderer(); TextLeafSvgNodeRenderer textInSpan = new TextLeafSvgNodeRenderer(); textInSpan.SetAttribute(SvgConstants.Attributes.TEXT_CONTENT, "\n" + " tspan text\n" + " " ); span.AddChild(textInSpan); root.AddChild(span); TextLeafSvgNodeRenderer textAfter = new TextLeafSvgNodeRenderer(); textAfter.SetAttribute(SvgConstants.Attributes.TEXT_CONTENT, "\n" + " after text\n" + " " ); root.AddChild(textAfter); //Run SvgTextUtil.ProcessWhiteSpace(root, true); root.GetChildren()[0].GetAttribute(SvgConstants.Attributes.TEXT_CONTENT); //Create result array String[] actual = new String[] { root.GetChildren()[0].GetAttribute(SvgConstants.Attributes.TEXT_CONTENT), ((TextSvgBranchRenderer)root.GetChildren()[1]).GetChildren()[0].GetAttribute(SvgConstants.Attributes.TEXT_CONTENT ), root.GetChildren()[2].GetAttribute(SvgConstants.Attributes.TEXT_CONTENT) }; //Create expected String[] expected = new String[] { "text", " tspan text", " after text" }; NUnit.Framework.Assert.AreEqual(expected, actual); }
/// <summary>Process the whitespace inside the Text Tree.</summary> /// <remarks> /// Process the whitespace inside the Text Tree. /// Whitespace is collapsed and new lines are handled /// A leading element in each subtree is handled different: the preceding whitespace is trimmed instead of kept /// </remarks> /// <param name="root">root of the text-renderer subtree</param> /// <param name="isLeadingElement">true if this element is a leading element(either the first child or the first element after an absolute position change) /// </param> public static void ProcessWhiteSpace(TextSvgBranchRenderer root, bool isLeadingElement) { // when svg is parsed by jsoup it leaves all whitespace in text element as is. Meaning that // tab/space indented xml files will retain their tabs and spaces. // The following regex replaces all whitespace with a single space. bool performLeadingTrim = isLeadingElement; foreach (ISvgTextNodeRenderer child in root.GetChildren()) { //If leaf, process contents, if branch, call function again if (child is TextSvgBranchRenderer) { //Branch processing ProcessWhiteSpace((TextSvgBranchRenderer)child, child.ContainsAbsolutePositionChange()); ((TextSvgBranchRenderer)child).MarkWhiteSpaceProcessed(); } if (child is TextLeafSvgNodeRenderer) { //Leaf processing TextLeafSvgNodeRenderer leafRend = (TextLeafSvgNodeRenderer)child; //Process text String toProcess = leafRend.GetAttribute(SvgConstants.Attributes.TEXT_CONTENT); toProcess = iText.IO.Util.StringUtil.ReplaceAll(toProcess, "\\s+", " "); toProcess = WhiteSpaceUtil.CollapseConsecutiveSpaces(toProcess); if (performLeadingTrim) { //Trim leading white spaces toProcess = TrimLeadingWhitespace(toProcess); toProcess = TrimTrailingWhitespace(toProcess); performLeadingTrim = false; } else { //only collapse whitespace toProcess = TrimTrailingWhitespace(toProcess); } leafRend.SetAttribute(SvgConstants.Attributes.TEXT_CONTENT, toProcess); } } }