3535import java .awt .RenderingHints .Key ;
3636import java .awt .image .BufferedImage ;
3737import java .awt .image .DataBufferInt ;
38- import java .io .IOException ;
3938import java .io .InputStream ;
4039import java .util .Map ;
4140
@@ -71,35 +70,48 @@ public class JSVGRasterizer implements SVGRasterizer {
7170 );
7271
7372 @ Override
74- public ImageData rasterizeSVG (InputStream inputStream , int zoom ) throws IOException {
75- SVGDocument svgDocument = loadSVG (inputStream );
76- if (svgDocument == null ) {
77- SWT .error (SWT .ERROR_INVALID_IMAGE );
78- }
73+ public ImageData rasterizeSVG (InputStream inputStream , int zoom ){
74+ SVGDocument svgDocument = loadAndValidateSVG (inputStream );
7975 BufferedImage rasterizedImage = renderSVG (svgDocument , zoom );
8076 return convertToSWTImageData (rasterizedImage );
8177 }
82-
83- private SVGDocument loadSVG (InputStream inputStream ) {
84- return SVG_LOADER .load (inputStream , null , LoaderContext .createDefault ());
78+
79+ @ Override
80+ public ImageData rasterizeSVG (InputStream inputStream , int width , int height ){
81+ SVGDocument svgDocument = loadAndValidateSVG (inputStream );
82+ BufferedImage rasterizedImage = renderSVG (svgDocument , width , height );
83+ return convertToSWTImageData (rasterizedImage );
84+ }
85+
86+ private SVGDocument loadAndValidateSVG (InputStream inputStream ) {
87+ SVGDocument svgDocument = SVG_LOADER .load (inputStream , null , LoaderContext .createDefault ());
88+ if (svgDocument == null ) {
89+ SWT .error (SWT .ERROR_INVALID_IMAGE );
90+ }
91+ return svgDocument ;
8592 }
8693
8794 private BufferedImage renderSVG (SVGDocument svgDocument , int zoom ) {
95+ FloatSize sourceImageSize = svgDocument .size ();
8896 float scalingFactor = zoom / 100.0f ;
89- BufferedImage image = createImageBase (svgDocument , scalingFactor );
90- Graphics2D g = configureRenderingOptions (scalingFactor , image );
97+ int targetImageWidth = calculateTargetWidth (scalingFactor , sourceImageSize );
98+ int targetImageHeight = calculateTargetHeight (scalingFactor , sourceImageSize );
99+ return renderSVG (svgDocument , targetImageWidth , targetImageHeight );
100+ }
101+
102+ private BufferedImage renderSVG (SVGDocument svgDocument , int width , int height ) {
103+ if (width <= 0 || height <= 0 ) {
104+ SWT .error (SWT .ERROR_INVALID_ARGUMENT );
105+ }
106+ BufferedImage image = new BufferedImage (width , height , BufferedImage .TYPE_INT_ARGB );
107+ float widthScalingFactor = width / svgDocument .size ().width ;
108+ float heightScalingFactor = height / svgDocument .size ().height ;
109+ Graphics2D g = configureRenderingOptions (widthScalingFactor , heightScalingFactor , image );
91110 svgDocument .render (null , g );
92111 g .dispose ();
93112 return image ;
94113 }
95114
96- private BufferedImage createImageBase (SVGDocument svgDocument , float scalingFactor ) {
97- FloatSize sourceImageSize = svgDocument .size ();
98- int targetImageWidth = calculateTargetWidth (scalingFactor , sourceImageSize );
99- int targetImageHeight = calculateTargetHeight (scalingFactor , sourceImageSize );
100- return new BufferedImage (targetImageWidth , targetImageHeight , BufferedImage .TYPE_INT_ARGB );
101- }
102-
103115 private int calculateTargetWidth (float scalingFactor , FloatSize sourceImageSize ) {
104116 double sourceImageWidth = sourceImageSize .getWidth ();
105117 return (int ) Math .round (sourceImageWidth * scalingFactor );
@@ -110,10 +122,10 @@ private int calculateTargetHeight(float scalingFactor, FloatSize sourceImageSize
110122 return (int ) Math .round (sourceImageHeight * scalingFactor );
111123 }
112124
113- private Graphics2D configureRenderingOptions (float scalingFactor , BufferedImage image ) {
125+ private Graphics2D configureRenderingOptions (float widthScalingFactor , float heightScalingFactor , BufferedImage image ) {
114126 Graphics2D g = image .createGraphics ();
115127 g .setRenderingHints (RENDERING_HINTS );
116- g .scale (scalingFactor , scalingFactor );
128+ g .scale (widthScalingFactor , heightScalingFactor );
117129 return g ;
118130 }
119131
0 commit comments