private static void embedSvgGraphic ( final SVGElement svgRoot , final SVGElement newSvgRoot , final Document newDocument , final Dimension targetSize , final Double rotation ) { final String originalWidth = svgRoot . getAttributeNS ( null , "width" ) ; final String originalHeight = svgRoot . getAttributeNS ( null , "height" ) ; /* * To scale the SVG graphic and to apply the rotation, we distinguish two * cases: width and height is set on the original SVG or not. * * Case 1: Width and height is set * If width and height is set, we wrap the original SVG into 2 new SVG elements * and a container element. * * Example: * Original SVG: * <svg width="100" height="100"></svg> * * New SVG (scaled to 300x300 and rotated by 90 degree): * <svg width="300" height="300"> * <g transform="rotate(90.0 150 150)"> * <svg width="100%" height="100%" viewBox="0 0 100 100"> * <svg width="100" height="100"></svg> * </svg> * </g> * </svg> * * The requested size is set on the outermost <svg>. Then, the rotation is applied to the * <g> container and the scaling is achieved with the viewBox parameter on the 2nd <svg>. * * * Case 2: Width and height is not set * In this case the original SVG is wrapped into just one container and one new SVG element. * The rotation is set on the container, and the scaling happens automatically. * * Example: * Original SVG: * <svg viewBox="0 0 61.06 91.83"></svg> * * New SVG (scaled to 300x300 and rotated by 90 degree): * <svg width="300" height="300"> * <g transform="rotate(90.0 150 150)"> * <svg viewBox="0 0 61.06 91.83"></svg> * </g> * </svg> */ if ( ! StringUtils . isEmpty ( originalWidth ) && ! StringUtils . isEmpty ( originalHeight ) ) { Element wrapperContainer = newDocument . createElementNS ( SVG_NS , "g" ) ; wrapperContainer . setAttributeNS ( null , SVGConstants . SVG_TRANSFORM_ATTRIBUTE , getRotateTransformation ( targetSize , rotation ) ) ; newSvgRoot . appendChild ( wrapperContainer ) ; Element wrapperSvg = newDocument . createElementNS ( SVG_NS , "svg" ) ; wrapperSvg . setAttributeNS ( null , "width" , "100%" ) ; wrapperSvg . setAttributeNS ( null , "height" , "100%" ) ; wrapperSvg . setAttributeNS ( null , "viewBox" , "0 0 " + originalWidth + " " + originalHeight ) ; wrapperContainer . appendChild ( wrapperSvg ) ; Node svgRootImported = newDocument . importNode ( svgRoot , true ) ; wrapperSvg . appendChild ( svgRootImported ) ; } else if ( StringUtils . isEmpty ( originalWidth ) && StringUtils . isEmpty ( originalHeight ) ) { Element wrapperContainer = newDocument . createElementNS ( SVG_NS , "g" ) ; wrapperContainer . setAttributeNS ( null , SVGConstants . SVG_TRANSFORM_ATTRIBUTE , getRotateTransformation ( targetSize , rotation ) ) ; newSvgRoot . appendChild ( wrapperContainer ) ; Node svgRootImported = newDocument . importNode ( svgRoot , true ) ; wrapperContainer . appendChild ( svgRootImported ) ; } else { throw new IllegalArgumentException ( "Unsupported or invalid north-arrow SVG graphic: The same unit (px, em, %, ...) must be" + " " + "used for `width` and `height`." ) ; } }
Embeds the given SVG element into a new SVG element scaling the graphic to the given dimension and applying the given rotation .