Parsing SVG width/height attributes
@@ -33,9 +33,11 @@ package com.twelvemonkeys.imageio.plugins.svg;
import com.twelvemonkeys.image.ImageUtil;
import com.twelvemonkeys.imageio.ImageReaderBase;
import com.twelvemonkeys.imageio.util.IIOUtil;
+import com.twelvemonkeys.lang.StringUtil;
import org.apache.batik.anim.dom.SVGDOMImplementation;
import org.apache.batik.anim.dom.SVGOMDocument;
import org.apache.batik.bridge.*;
+import org.apache.batik.css.parser.CSSLexicalUnit;
import org.apache.batik.dom.util.DOMUtilities;
import org.apache.batik.ext.awt.image.GraphicsUtil;
import org.apache.batik.gvt.CanvasGraphicsNode;
@@ -47,6 +49,7 @@ import org.apache.batik.transcoder.*;
import org.apache.batik.transcoder.image.ImageTranscoder;
import org.apache.batik.util.ParsedURL;
import org.apache.batik.util.SVGConstants;
+import org.apache.batik.xml.LexicalUnits;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.svg.SVGSVGElement;
@@ -324,18 +327,18 @@ public class SVGImageReader extends ImageReaderBase {
}
// ----
+ SVGSVGElement rootElement = svgDoc.getRootElement();
// get the 'width' and 'height' attributes of the SVG document
- Dimension2D docSize = ctx.getDocumentSize();
- if (docSize != null) {
- defaultWidth = (float) docSize.getWidth();
- defaultHeight = (float) docSize.getHeight();
+ String widthStr = rootElement.getAttributeNS(null, SVGConstants.SVG_WIDTH_ATTRIBUTE);
+ String heightStr = rootElement.getAttributeNS(null, SVGConstants.SVG_HEIGHT_ATTRIBUTE);
+ if (!StringUtil.isEmpty(widthStr) && !StringUtil.isEmpty(heightStr)) {
+ UnitProcessor.Context uctx
+ = UnitProcessor.createContext(ctx, rootElement);
+ defaultWidth = UnitProcessor.svgToUserSpace(widthStr, SVGConstants.SVG_WIDTH_ATTRIBUTE, UnitProcessor.HORIZONTAL_LENGTH, uctx);
+ defaultHeight = UnitProcessor.svgToUserSpace(heightStr, SVGConstants.SVG_HEIGHT_ATTRIBUTE, UnitProcessor.VERTICAL_LENGTH, uctx);
}
else {
- defaultWidth = 200;
- defaultHeight = 200;
- }
- SVGSVGElement rootElement = svgDoc.getRootElement();
String viewBoxStr = rootElement.getAttributeNS
(null, SVGConstants.SVG_VIEW_BOX_ATTRIBUTE);
if (viewBoxStr.length() != 0) {
@@ -343,6 +346,11 @@ public class SVGImageReader extends ImageReaderBase {
defaultWidth = rect[2];
defaultHeight = rect[3];
}
+ else {
+ defaultWidth = 200;
+ defaultHeight = 200;
+ }
+ }
// Hack to work around exception above
if (root != null) {
