@@ -72,34 +72,44 @@ public class JSVGRasterizer implements SVGRasterizer {
72
72
73
73
@ Override
74
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
- }
75
+ SVGDocument svgDocument = loadAndValidateSVG (inputStream );
79
76
BufferedImage rasterizedImage = renderSVG (svgDocument , zoom );
80
77
return convertToSWTImageData (rasterizedImage );
81
78
}
82
-
83
- private SVGDocument loadSVG (InputStream inputStream ) {
84
- return SVG_LOADER .load (inputStream , null , LoaderContext .createDefault ());
79
+
80
+ @ Override
81
+ public ImageData rasterizeSVG (InputStream inputStream , int width , int height ) throws IOException {
82
+ SVGDocument svgDocument = loadAndValidateSVG (inputStream );
83
+ BufferedImage rasterizedImage = renderSVG (svgDocument , width , height );
84
+ return convertToSWTImageData (rasterizedImage );
85
+ }
86
+
87
+ private SVGDocument loadAndValidateSVG (InputStream inputStream ) throws IOException {
88
+ SVGDocument svgDocument = SVG_LOADER .load (inputStream , null , LoaderContext .createDefault ());
89
+ if (svgDocument == null ) {
90
+ SWT .error (SWT .ERROR_INVALID_IMAGE );
91
+ }
92
+ return svgDocument ;
85
93
}
86
94
87
95
private BufferedImage renderSVG (SVGDocument svgDocument , int zoom ) {
96
+ FloatSize sourceImageSize = svgDocument .size ();
88
97
float scalingFactor = zoom / 100.0f ;
89
- BufferedImage image = createImageBase (svgDocument , scalingFactor );
90
- Graphics2D g = configureRenderingOptions (scalingFactor , image );
98
+ int targetImageWidth = calculateTargetWidth (scalingFactor , sourceImageSize );
99
+ int targetImageHeight = calculateTargetHeight (scalingFactor , sourceImageSize );
100
+ return renderSVG (svgDocument , targetImageWidth , targetImageHeight );
101
+ }
102
+
103
+ private BufferedImage renderSVG (SVGDocument svgDocument , int width , int height ) {
104
+ BufferedImage image = new BufferedImage (width , height , BufferedImage .TYPE_INT_ARGB );
105
+ float widthScalingFactor = width / svgDocument .size ().width ;
106
+ float heightScalingFactor = height / svgDocument .size ().height ;
107
+ Graphics2D g = configureRenderingOptions (widthScalingFactor , heightScalingFactor , image );
91
108
svgDocument .render (null , g );
92
109
g .dispose ();
93
110
return image ;
94
111
}
95
112
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
-
103
113
private int calculateTargetWidth (float scalingFactor , FloatSize sourceImageSize ) {
104
114
double sourceImageWidth = sourceImageSize .getWidth ();
105
115
return (int ) Math .round (sourceImageWidth * scalingFactor );
@@ -110,10 +120,10 @@ private int calculateTargetHeight(float scalingFactor, FloatSize sourceImageSize
110
120
return (int ) Math .round (sourceImageHeight * scalingFactor );
111
121
}
112
122
113
- private Graphics2D configureRenderingOptions (float scalingFactor , BufferedImage image ) {
123
+ private Graphics2D configureRenderingOptions (float widthScalingFactor , float heightScalingFactor , BufferedImage image ) {
114
124
Graphics2D g = image .createGraphics ();
115
125
g .setRenderingHints (RENDERING_HINTS );
116
- g .scale (scalingFactor , scalingFactor );
126
+ g .scale (widthScalingFactor , heightScalingFactor );
117
127
return g ;
118
128
}
119
129
0 commit comments