@@ -74,34 +74,47 @@ public ImageData rasterizeSVG(InputStream inputStream, int zoom) {
7474 if (zoom < 0 ) {
7575 SWT .error (SWT .ERROR_INVALID_ARGUMENT );
7676 }
77- SVGDocument svgDocument = loadSVG (inputStream );
78- if (svgDocument == null ) {
79- SWT .error (SWT .ERROR_INVALID_IMAGE );
80- }
77+ SVGDocument svgDocument = loadAndValidateSVG (inputStream );
8178 BufferedImage rasterizedImage = renderSVG (svgDocument , zoom );
8279 return convertToSWTImageData (rasterizedImage );
8380 }
8481
85- private SVGDocument loadSVG (InputStream inputStream ) {
86- return SVG_LOADER .load (inputStream , null , LoaderContext .createDefault ());
82+ @ Override
83+ public ImageData rasterizeSVG (InputStream inputStream , int width , int height ) {
84+ SVGDocument svgDocument = loadAndValidateSVG (inputStream );
85+ BufferedImage rasterizedImage = renderSVG (svgDocument , width , height );
86+ return convertToSWTImageData (rasterizedImage );
87+ }
88+
89+ private SVGDocument loadAndValidateSVG (InputStream inputStream ) {
90+ SVGDocument svgDocument = SVG_LOADER .load (inputStream , null , LoaderContext .createDefault ());
91+ if (svgDocument == null ) {
92+ SWT .error (SWT .ERROR_INVALID_IMAGE );
93+ }
94+ return svgDocument ;
8795 }
8896
8997 private BufferedImage renderSVG (SVGDocument svgDocument , int zoom ) {
98+ FloatSize sourceImageSize = svgDocument .size ();
9099 float scalingFactor = zoom / 100.0f ;
91- BufferedImage image = createImageBase (svgDocument , scalingFactor );
92- Graphics2D g = configureRenderingOptions (scalingFactor , image );
100+ int targetImageWidth = calculateTargetWidth (scalingFactor , sourceImageSize );
101+ int targetImageHeight = calculateTargetHeight (scalingFactor , sourceImageSize );
102+ return renderSVG (svgDocument , targetImageWidth , targetImageHeight );
103+ }
104+
105+ private BufferedImage renderSVG (SVGDocument svgDocument , int width , int height ) {
106+ if (width <= 0 || height <= 0 ) {
107+ SWT .error (SWT .ERROR_INVALID_ARGUMENT );
108+ }
109+ BufferedImage image = new BufferedImage (width , height , BufferedImage .TYPE_INT_ARGB );
110+ float widthScalingFactor = width / svgDocument .size ().width ;
111+ float heightScalingFactor = height / svgDocument .size ().height ;
112+ Graphics2D g = configureRenderingOptions (widthScalingFactor , heightScalingFactor , image );
93113 svgDocument .render (null , g );
94114 g .dispose ();
95115 return image ;
96116 }
97117
98- private BufferedImage createImageBase (SVGDocument svgDocument , float scalingFactor ) {
99- FloatSize sourceImageSize = svgDocument .size ();
100- int targetImageWidth = calculateTargetWidth (scalingFactor , sourceImageSize );
101- int targetImageHeight = calculateTargetHeight (scalingFactor , sourceImageSize );
102- return new BufferedImage (targetImageWidth , targetImageHeight , BufferedImage .TYPE_INT_ARGB );
103- }
104-
105118 private int calculateTargetWidth (float scalingFactor , FloatSize sourceImageSize ) {
106119 double sourceImageWidth = sourceImageSize .getWidth ();
107120 return (int ) Math .round (sourceImageWidth * scalingFactor );
@@ -112,10 +125,11 @@ private int calculateTargetHeight(float scalingFactor, FloatSize sourceImageSize
112125 return (int ) Math .round (sourceImageHeight * scalingFactor );
113126 }
114127
115- private Graphics2D configureRenderingOptions (float scalingFactor , BufferedImage image ) {
128+ private Graphics2D configureRenderingOptions (float widthScalingFactor , float heightScalingFactor ,
129+ BufferedImage image ) {
116130 Graphics2D g = image .createGraphics ();
117131 g .setRenderingHints (RENDERING_HINTS );
118- g .scale (scalingFactor , scalingFactor );
132+ g .scale (widthScalingFactor , heightScalingFactor );
119133 return g ;
120134 }
121135
0 commit comments