@@ -74,34 +74,47 @@ public ImageData rasterizeSVG(InputStream inputStream, int zoom) {
74
74
if (zoom < 0 ) {
75
75
SWT .error (SWT .ERROR_INVALID_ARGUMENT );
76
76
}
77
- SVGDocument svgDocument = loadSVG (inputStream );
78
- if (svgDocument == null ) {
79
- SWT .error (SWT .ERROR_INVALID_IMAGE );
80
- }
77
+ SVGDocument svgDocument = loadAndValidateSVG (inputStream );
81
78
BufferedImage rasterizedImage = renderSVG (svgDocument , zoom );
82
79
return convertToSWTImageData (rasterizedImage );
83
80
}
84
81
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 ;
87
95
}
88
96
89
97
private BufferedImage renderSVG (SVGDocument svgDocument , int zoom ) {
98
+ FloatSize sourceImageSize = svgDocument .size ();
90
99
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 );
93
113
svgDocument .render (null , g );
94
114
g .dispose ();
95
115
return image ;
96
116
}
97
117
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
-
105
118
private int calculateTargetWidth (float scalingFactor , FloatSize sourceImageSize ) {
106
119
double sourceImageWidth = sourceImageSize .getWidth ();
107
120
return (int ) Math .round (sourceImageWidth * scalingFactor );
@@ -112,10 +125,11 @@ private int calculateTargetHeight(float scalingFactor, FloatSize sourceImageSize
112
125
return (int ) Math .round (sourceImageHeight * scalingFactor );
113
126
}
114
127
115
- private Graphics2D configureRenderingOptions (float scalingFactor , BufferedImage image ) {
128
+ private Graphics2D configureRenderingOptions (float widthScalingFactor , float heightScalingFactor ,
129
+ BufferedImage image ) {
116
130
Graphics2D g = image .createGraphics ();
117
131
g .setRenderingHints (RENDERING_HINTS );
118
- g .scale (scalingFactor , scalingFactor );
132
+ g .scale (widthScalingFactor , heightScalingFactor );
119
133
return g ;
120
134
}
121
135
0 commit comments