SvgTextUtil { public static void processWhiteSpace(TextSvgBranchRenderer root, boolean isLeadingElement) { boolean performLeadingTrim = isLeadingElement; for (ISvgTextNodeRenderer child : root.getChildren()) { if (child instanceof TextSvgBranchRenderer) { processWhiteSpace((TextSvgBranchRenderer) child, child.containsAbsolutePositionChange()); ((TextSvgBranchRenderer) child).markWhiteSpaceProcessed(); } if (child instanceof TextLeafSvgNodeRenderer) { TextLeafSvgNodeRenderer leafRend = (TextLeafSvgNodeRenderer) child; String toProcess = leafRend.getAttribute(SvgConstants.Attributes.TEXT_CONTENT); toProcess = toProcess.replaceAll("\\s+", " "); toProcess = WhiteSpaceUtil.collapseConsecutiveSpaces(toProcess); if (performLeadingTrim) { toProcess = trimLeadingWhitespace(toProcess); toProcess = trimTrailingWhitespace(toProcess); performLeadingTrim = false; } else { toProcess = trimTrailingWhitespace(toProcess); } leafRend.setAttribute(SvgConstants.Attributes.TEXT_CONTENT, toProcess); } } } private  SvgTextUtil(); static String trimLeadingWhitespace(String toTrim); static String trimTrailingWhitespace(String toTrim); static void processWhiteSpace(TextSvgBranchRenderer root, boolean isLeadingElement); static boolean isOnlyWhiteSpace(String s); static float resolveFontSize(ISvgTextNodeRenderer renderer, float parentFontSize); static String filterReferenceValue(String name);  }
@Test public void processWhiteSpaceBreakLine() { 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); SvgTextUtil.processWhiteSpace(root, true); root.getChildren().get(0).getAttribute(SvgConstants.Attributes.TEXT_CONTENT); String[] actual = new String[]{ root.getChildren().get(0).getAttribute(SvgConstants.Attributes.TEXT_CONTENT), ((TextSvgBranchRenderer) root.getChildren().get(1)).getChildren().get(0).getAttribute(SvgConstants.Attributes.TEXT_CONTENT), root.getChildren().get(2).getAttribute(SvgConstants.Attributes.TEXT_CONTENT) }; String[] expected = new String[]{"text", " tspan text", " after text"}; Assert.assertArrayEquals(expected, actual); }
@Test public void processWhiteSpaceAbsPositionChange() { TextSvgBranchRenderer root = new TextSvgBranchRenderer(); TextLeafSvgNodeRenderer textBefore = new TextLeafSvgNodeRenderer(); textBefore.setAttribute(SvgConstants.Attributes.TEXT_CONTENT, "\n" + " text\n" + " "); root.addChild(textBefore); TextSvgBranchRenderer span = new TextSvgBranchRenderer(); span.setAttribute(SvgConstants.Attributes.X, "10"); span.setAttribute(SvgConstants.Attributes.Y, "20"); 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); SvgTextUtil.processWhiteSpace(root, true); root.getChildren().get(0).getAttribute(SvgConstants.Attributes.TEXT_CONTENT); String[] actual = new String[]{ root.getChildren().get(0).getAttribute(SvgConstants.Attributes.TEXT_CONTENT), ((TextSvgBranchRenderer) root.getChildren().get(1)).getChildren().get(0).getAttribute(SvgConstants.Attributes.TEXT_CONTENT), root.getChildren().get(2).getAttribute(SvgConstants.Attributes.TEXT_CONTENT) }; String[] expected = new String[]{"text", "tspan text", " after text"}; Assert.assertArrayEquals(expected, actual); }