mirror of
https://github.com/haraldk/TwelveMonkeys.git
synced 2025-08-04 03:55:28 -04:00
When rescaling SVGs use the ViewBox, if defined, for default sizes
This commit is contained in:
parent
c1ce4b066d
commit
24a0786d64
@ -46,6 +46,7 @@ import org.apache.batik.gvt.renderer.ImageRendererFactory;
|
|||||||
import org.apache.batik.transcoder.*;
|
import org.apache.batik.transcoder.*;
|
||||||
import org.apache.batik.transcoder.image.ImageTranscoder;
|
import org.apache.batik.transcoder.image.ImageTranscoder;
|
||||||
import org.apache.batik.util.ParsedURL;
|
import org.apache.batik.util.ParsedURL;
|
||||||
|
import org.apache.batik.util.SVGConstants;
|
||||||
import org.w3c.dom.DOMImplementation;
|
import org.w3c.dom.DOMImplementation;
|
||||||
import org.w3c.dom.Document;
|
import org.w3c.dom.Document;
|
||||||
import org.w3c.dom.svg.SVGSVGElement;
|
import org.w3c.dom.svg.SVGSVGElement;
|
||||||
@ -334,6 +335,14 @@ public class SVGImageReader extends ImageReaderBase {
|
|||||||
defaultWidth = 200;
|
defaultWidth = 200;
|
||||||
defaultHeight = 200;
|
defaultHeight = 200;
|
||||||
}
|
}
|
||||||
|
SVGSVGElement rootElement = svgDoc.getRootElement();
|
||||||
|
String viewBoxStr = rootElement.getAttributeNS
|
||||||
|
(null, SVGConstants.SVG_VIEW_BOX_ATTRIBUTE);
|
||||||
|
if (viewBoxStr.length() != 0) {
|
||||||
|
float[] rect = ViewBox.parseViewBoxAttribute(rootElement, viewBoxStr, null);
|
||||||
|
defaultWidth = rect[2];
|
||||||
|
defaultHeight = rect[3];
|
||||||
|
}
|
||||||
|
|
||||||
// Hack to work around exception above
|
// Hack to work around exception above
|
||||||
if (root != null) {
|
if (root != null) {
|
||||||
|
@ -35,6 +35,7 @@ import org.junit.Ignore;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import javax.imageio.IIOException;
|
import javax.imageio.IIOException;
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
import javax.imageio.ImageReadParam;
|
import javax.imageio.ImageReadParam;
|
||||||
import javax.imageio.ImageReader;
|
import javax.imageio.ImageReader;
|
||||||
import javax.imageio.event.IIOReadWarningListener;
|
import javax.imageio.event.IIOReadWarningListener;
|
||||||
@ -43,9 +44,12 @@ import javax.imageio.stream.ImageInputStream;
|
|||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.awt.image.ImagingOpException;
|
import java.awt.image.ImagingOpException;
|
||||||
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
import java.nio.Buffer;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -99,6 +103,57 @@ public class SVGImageReaderTest extends ImageReaderAbstractTest<SVGImageReader>
|
|||||||
return Collections.singletonList("image/svg+xml");
|
return Collections.singletonList("image/svg+xml");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testScaleViewBox() throws IOException {
|
||||||
|
URL svgUrl = getClassLoaderResource("/svg/quadrants.svg");
|
||||||
|
|
||||||
|
File tmpDir = new File(System.getProperty("java.io.tmpdir"));
|
||||||
|
|
||||||
|
SVGImageReader reader = createReader();
|
||||||
|
SVGReadParam param = new SVGReadParam();
|
||||||
|
|
||||||
|
int[] sizes = new int[]{16, 32, 64, 128};
|
||||||
|
for (int size : sizes) {
|
||||||
|
try (InputStream svgStream = svgUrl.openStream(); ImageInputStream iis = ImageIO.createImageInputStream(svgStream)) {
|
||||||
|
reader.reset();
|
||||||
|
reader.setInput(iis);
|
||||||
|
|
||||||
|
|
||||||
|
param.setSourceRenderSize(new Dimension(size, size));
|
||||||
|
BufferedImage image = reader.read(0, param);
|
||||||
|
checkQuadrantColors(image);
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
reader.dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void checkQuadrantColors(BufferedImage image) {
|
||||||
|
int quadPoint = image.getWidth() / 2;
|
||||||
|
for (int x = 0; x < image.getWidth(); x++) {
|
||||||
|
for (int y = 0; y < image.getHeight(); y++) {
|
||||||
|
int current = image.getRGB(x, y);
|
||||||
|
if (x < quadPoint) {
|
||||||
|
if (y < quadPoint) {
|
||||||
|
assertEquals("x=" + x + " y=" + y + " q=" + quadPoint, 0xFF0000FF, current);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
assertEquals("x=" + x + " y=" + y + " q=" + quadPoint, 0xFFFF0000, current);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (y < quadPoint) {
|
||||||
|
assertEquals("x=" + x + " y=" + y + " q=" + quadPoint, 0xFF00FF00, current);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
assertEquals("x=" + x + " y=" + y + " q=" + quadPoint, 0xFF000000, current);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Override
|
@Override
|
||||||
public void testReadWithSizeParam() {
|
public void testReadWithSizeParam() {
|
||||||
|
13
imageio/imageio-batik/src/test/resources/svg/quadrants.svg
Normal file
13
imageio/imageio-batik/src/test/resources/svg/quadrants.svg
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||||
|
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" id="blue-square" version="1.1">
|
||||||
|
<g id="layer1">
|
||||||
|
<rect id="rect2985" width="50" height="50" x="0" y="0"
|
||||||
|
style="color:#000000;fill:#0000ff;fill-opacity:1;fill-rule:nonzero;stroke:none;stroke-width:1;marker:none;visibility:visible;display:inline;overflow:visible;enable-background:accumulate" />
|
||||||
|
<rect id="rect2986" width="50" height="50" x="50" y="0"
|
||||||
|
style="color:#000000;fill:#00ff00;fill-opacity:1;fill-rule:nonzero;stroke:none;stroke-width:1;marker:none;visibility:visible;display:inline;overflow:visible;enable-background:accumulate" />
|
||||||
|
<rect id="rect2987" width="50" height="50" x="0" y="50"
|
||||||
|
style="color:#000000;fill:#ff0000;fill-opacity:1;fill-rule:nonzero;stroke:none;stroke-width:1;marker:none;visibility:visible;display:inline;overflow:visible;enable-background:accumulate" />
|
||||||
|
<rect id="rect2988" width="50" height="50" x="50" y="50"
|
||||||
|
style="color:#000000;fill:#000000;fill-opacity:1;fill-rule:nonzero;stroke:none;stroke-width:1;marker:none;visibility:visible;display:inline;overflow:visible;enable-background:accumulate" />
|
||||||
|
</g>
|
||||||
|
</svg>
|
After Width: | Height: | Size: 1.2 KiB |
Loading…
x
Reference in New Issue
Block a user