35
35
import java .awt .RenderingHints .Key ;
36
36
import java .awt .image .BufferedImage ;
37
37
import java .awt .image .DataBufferInt ;
38
- import java .io .IOException ;
39
38
import java .io .InputStream ;
40
39
import java .util .Map ;
41
40
@@ -71,35 +70,48 @@ public class JSVGRasterizer implements SVGRasterizer {
71
70
);
72
71
73
72
@ 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 );
79
75
BufferedImage rasterizedImage = renderSVG (svgDocument , zoom );
80
76
return convertToSWTImageData (rasterizedImage );
81
77
}
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 ;
85
92
}
86
93
87
94
private BufferedImage renderSVG (SVGDocument svgDocument , int zoom ) {
95
+ FloatSize sourceImageSize = svgDocument .size ();
88
96
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 );
91
110
svgDocument .render (null , g );
92
111
g .dispose ();
93
112
return image ;
94
113
}
95
114
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
115
private int calculateTargetWidth (float scalingFactor , FloatSize sourceImageSize ) {
104
116
double sourceImageWidth = sourceImageSize .getWidth ();
105
117
return (int ) Math .round (sourceImageWidth * scalingFactor );
@@ -110,10 +122,10 @@ private int calculateTargetHeight(float scalingFactor, FloatSize sourceImageSize
110
122
return (int ) Math .round (sourceImageHeight * scalingFactor );
111
123
}
112
124
113
- private Graphics2D configureRenderingOptions (float scalingFactor , BufferedImage image ) {
125
+ private Graphics2D configureRenderingOptions (float widthScalingFactor , float heightScalingFactor , BufferedImage image ) {
114
126
Graphics2D g = image .createGraphics ();
115
127
g .setRenderingHints (RENDERING_HINTS );
116
- g .scale (scalingFactor , scalingFactor );
128
+ g .scale (widthScalingFactor , heightScalingFactor );
117
129
return g ;
118
130
}
119
131
0 commit comments