deweese 02/02/19 14:08:44
Modified: sources/org/apache/batik/extension/svg
BatikBridgeExtension.java BatikExtConstants.java
sources/org/apache/batik/swing/svg JSVGComponent.java
Added: samples/extensions multi.svg multi1.jpg multi2.jpg
multi3.jpg multi4.jpg
sources/org/apache/batik/extension/svg
BatikMultiImageElementBridge.java
sources/org/apache/batik/extension/svg/renderable
MultiResRable.java
Log:
1) New extension multi-image element that takes a list of
sub-images and switches which image is used based on the
size of the needed image for rendering (see the example)
Revision Changes Path
1.1 xml-batik/samples/extensions/multi.svg
Index: multi.svg
===================================================================
<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN"
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<!-- ====================================================================== -->
<!-- Copyright (C) The Apache Software Foundation. All rights reserved. -->
<!-- -->
<!-- This software is published under the terms of the Apache Software -->
<!-- License version 1.1, a copy of which has been included with this -->
<!-- distribution in the LICENSE file. -->
<!-- ====================================================================== -->
<!-- ====================================================================== -->
<!-- regularPoly extension tag test -->
<!-- -->
<!-- @author [EMAIL PROTECTED] -->
<!-- @version $Id: multi.svg,v 1.1 2002/02/19 22:08:43 deweese Exp $ -->
<!-- ====================================================================== -->
<!-- <?xml-stylesheet type="text/css" href="extension.css" ?> -->
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
id="body" width="450" height="500" xml:space="preserve"
viewBox="0 0 450 500"
xmlns:batikExt="http://xml.apache.org/batik/ext">
<title>Multi-Image Extension Tag</title>
<!-- ============================================================= -->
<!-- Test content -->
<!-- ============================================================= -->
<g id="testContent" class="legend" style="text-anchor:middle">
<text x="225" y="40" class="title">Multi-Image Extension Tag</text>
<g transform="translate(145, 145)">
<rect fill="#DAA" x="0" y="0" width="160" height="220"/>
<batikExt:multi-image id="img" xlink:href="multi1.jpg"
x="20" y="20" width="120" height="180"
pixel-width="480" pixel-height="720">
<batikExt:sub-image xlink:href="multi2.jpg"
pixel-width="240" pixel-height="360"/>
<batikExt:sub-image xlink:href="multi3.jpg"
pixel-width="120" pixel-height="180"/>
<batikExt:sub-image xlink:href="multi4.jpg"
pixel-width="60" pixel-height="90"/>
</batikExt:multi-image>
</g>
</g>
<!-- ============================================================= -->
<!-- Batik sample mark -->
<!-- ============================================================= -->
<!-- <use xlink:href="../batikLogo.svg#Batik_Tag_Box" /> -->
</svg>
1.1 xml-batik/samples/extensions/multi1.jpg
<<Binary file>>
1.1 xml-batik/samples/extensions/multi2.jpg
<<Binary file>>
1.1 xml-batik/samples/extensions/multi3.jpg
<<Binary file>>
1.1 xml-batik/samples/extensions/multi4.jpg
<<Binary file>>
1.5 +2 -0
xml-batik/sources/org/apache/batik/extension/svg/BatikBridgeExtension.java
Index: BatikBridgeExtension.java
===================================================================
RCS file:
/home/cvs/xml-batik/sources/org/apache/batik/extension/svg/BatikBridgeExtension.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- BatikBridgeExtension.java 17 Oct 2001 12:33:00 -0000 1.4
+++ BatikBridgeExtension.java 19 Feb 2002 22:08:43 -0000 1.5
@@ -42,6 +42,7 @@
"http://xml.apache.org/batik/ext/histogramNormalization/1.0" ,
"http://xml.apache.org/batik/ext/solidColor/1.0" ,
"http://xml.apache.org/batik/ext/colorSwitch/1.0" ,
+ "http://xml.apache.org/batik/ext/multi-image/1.0" ,
};
Vector v = new Vector(extensions.length);
for (int i=0; i<extensions.length; i++) {
@@ -94,6 +95,7 @@
ctx.putBridge(new BatikRegularPolygonElementBridge());
ctx.putBridge(new BatikStarElementBridge());
ctx.putBridge(new BatikHistogramNormalizationElementBridge());
+ ctx.putBridge(new BatikMultiImageElementBridge());
ctx.putBridge(new SolidColorBridge());
ctx.putBridge(new ColorSwitchBridge());
}
1.6 +17 -1
xml-batik/sources/org/apache/batik/extension/svg/BatikExtConstants.java
Index: BatikExtConstants.java
===================================================================
RCS file:
/home/cvs/xml-batik/sources/org/apache/batik/extension/svg/BatikExtConstants.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- BatikExtConstants.java 23 Oct 2001 13:42:29 -0000 1.5
+++ BatikExtConstants.java 19 Feb 2002 22:08:43 -0000 1.6
@@ -31,10 +31,18 @@
public static final String BATIK_EXT_COLOR_SWITCH_TAG =
"colorSwitch";
- /** Tag name for Batik's star extension. */
+ /** Tag name for Batik's histogram normalization extension. */
public static final String BATIK_EXT_HISTOGRAM_NORMALIZATION_TAG =
"histogramNormalization";
+ /** Tag name for Batik's multi-image extension. */
+ public static final String BATIK_EXT_MULTI_IMAGE_TAG =
+ "multi-image";
+
+ /** Tag name for Batik's multi-image extension. */
+ public static final String BATIK_EXT_SUB_IMAGE_TAG =
+ "sub-image";
+
/** Attribute name for sides attribute */
public static final String BATIK_EXT_SIDES_ATTRIBUTE =
"sides";
@@ -46,6 +54,14 @@
/** Attribute name for trim percent attribute */
public static final String BATIK_EXT_TRIM_ATTRIBUTE =
"trim";
+
+ /** Attribute name for pixel-width attribute */
+ public static final String BATIK_EXT_PIXEL_WIDTH_ATTRIBUTE =
+ "pixel-width";
+
+ /** Attribute name for pixel-height attribute */
+ public static final String BATIK_EXT_PIXEL_HEIGHT_ATTRIBUTE =
+ "pixel-height";
/** Attribute name for color attribute */
public static final String BATIK_EXT_SOLID_COLOR_PROPERTY =
1.1
xml-batik/sources/org/apache/batik/extension/svg/BatikMultiImageElementBridge.java
Index: BatikMultiImageElementBridge.java
===================================================================
/*****************************************************************************
* Copyright (C) The Apache Software Foundation. All rights reserved. *
* ------------------------------------------------------------------------- *
* This software is published under the terms of the Apache Software License *
* version 1.1, a copy of which has been included with this distribution in *
* the LICENSE file. *
*****************************************************************************/
package org.apache.batik.extension.svg;
import java.awt.Dimension;
import java.awt.geom.Rectangle2D;
import java.net.URL;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Iterator;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.svg.SVGDocument;
import org.apache.batik.bridge.CSSUtilities;
import org.apache.batik.bridge.SVGImageElementBridge;
import org.apache.batik.bridge.Bridge;
import org.apache.batik.bridge.BridgeContext;
import org.apache.batik.bridge.BridgeException;
import org.apache.batik.bridge.SVGUtilities;
import org.apache.batik.dom.svg.SVGOMDocument;
import org.apache.batik.dom.util.XLinkSupport;
import org.apache.batik.ext.awt.image.renderable.Filter;
import org.apache.batik.extension.svg.renderable.MultiResRable;
import org.apache.batik.gvt.GraphicsNode;
import org.apache.batik.gvt.ImageNode;
import org.apache.batik.gvt.RasterImageNode;
import org.apache.batik.util.ParsedURL;
/**
* Bridge class for the <image> element.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Thierry Kormann</a>
* @version $Id: BatikMultiImageElementBridge.java,v 1.1 2002/02/19 22:08:43 deweese
Exp $
*/
public class BatikMultiImageElementBridge extends SVGImageElementBridge
implements BatikExtConstants {
BatikMultiImageElementBridge() { }
/**
* Returns the Batik Extension namespace URI.
*/
public String getNamespaceURI() {
return BATIK_EXT_NAMESPACE_URI;
}
/**
* Returns 'multi-image'.
*/
public String getLocalName() {
return BATIK_EXT_MULTI_IMAGE_TAG;
}
/**
* Returns a new instance of this bridge.
*/
public Bridge getInstance() {
return new BatikMultiImageElementBridge();
}
/**
* Creates a graphics node using the specified BridgeContext and for the
* specified element.
*
* @param ctx the bridge context to use
* @param e the element that describes the graphics node to build
* @return a graphics node that represents the specified element
*/
public GraphicsNode createGraphicsNode(BridgeContext ctx, Element e) {
// 'requiredFeatures', 'requiredExtensions' and 'systemLanguage'
if (!SVGUtilities.matchUserAgent(e, ctx.getUserAgent())) {
return null;
}
ImageNode imageNode = (ImageNode)instantiateGraphicsNode();
// 'transform'
String s = e.getAttributeNS(null, SVG_TRANSFORM_ATTRIBUTE);
if (s.length() != 0) {
imageNode.setTransform
(SVGUtilities.convertTransform(e, SVG_TRANSFORM_ATTRIBUTE, s));
}
// 'visibility'
imageNode.setVisible(CSSUtilities.convertVisibility(e));
RasterImageNode node = new RasterImageNode();
List dims = new LinkedList();
List uris = new LinkedList();
addInfo(e, dims, uris);
for (Node n = e.getFirstChild(); n != null; n = n.getNextSibling()) {
if (n.getNodeType() != Node.ELEMENT_NODE)
continue;
Element se = (Element)n;
if (!(se.getNamespaceURI().equals(BATIK_EXT_NAMESPACE_URI)) ||
!(se.getLocalName().equals(BATIK_EXT_SUB_IMAGE_TAG)))
continue;
addInfo(se, dims, uris);
}
Dimension [] dary = new Dimension[uris.size()];
ParsedURL [] uary = new ParsedURL[uris.size()];
Iterator di = dims.iterator();
Iterator ui = uris.iterator();
int n=0;
while (di.hasNext()) {
int i;
Dimension d = (Dimension)di.next();
for (i=0; i<n; i++) {
if (d.width > dary[i].width) break;
}
for (int j=n; j>i; j--) {
dary[j] = dary[j-1];
uary[j] = uary[j-1];
}
dary[i] = d;
uary[i] = (ParsedURL)ui.next();
n++;
}
Filter f = new MultiResRable(uary, dary);
Rectangle2D imgB, b;
imgB = f.getBounds2D();
b = getImageBounds(ctx, e);
node.setImage(f);
node.setImageBounds(imgB);
float []vb = new float[4];
vb[0] = 0;
vb[1] = 0;
vb[2] = (float)imgB.getWidth();
vb[3] = (float)imgB.getHeight();
initializeViewport(ctx, e, node, vb, b);
imageNode.setImage(node);
return imageNode;
}
protected void addInfo(Element e, Collection dims, Collection uris) {
Dimension d = getElementPixelSize(e);
String uriStr = XLinkSupport.getXLinkHref(e);
if (uriStr.length() == 0) {
throw new BridgeException(e, ERR_ATTRIBUTE_MISSING,
new Object[] {"xlink:href"});
}
SVGDocument svgDoc = (SVGDocument)e.getOwnerDocument();
URL baseURL = ((SVGOMDocument)svgDoc).getURLObject();
ParsedURL purl = new ParsedURL(baseURL, uriStr);
dims.add(d);
uris.add(purl);
}
protected Dimension getElementPixelSize(Element e) {
int w=0, h=0;
String s;
s = e.getAttributeNS
(null,BATIK_EXT_PIXEL_WIDTH_ATTRIBUTE);
if (s.length() == 0)
throw new BridgeException(e, ERR_ATTRIBUTE_MISSING,
new Object[] {"pixel-width"});
try {
w = (int)SVGUtilities.convertSVGNumber(s);
} catch (NumberFormatException ex) {
throw new BridgeException
(e, ERR_ATTRIBUTE_VALUE_MALFORMED,
new Object[] {BATIK_EXT_TRIM_ATTRIBUTE, s});
}
s = e.getAttributeNS
(null,BATIK_EXT_PIXEL_HEIGHT_ATTRIBUTE);
if (s.length() == 0)
throw new BridgeException(e, ERR_ATTRIBUTE_MISSING,
new Object[] {"pixel-height"});
try {
h = (int)SVGUtilities.convertSVGNumber(s);
} catch (NumberFormatException ex) {
throw new BridgeException
(e, ERR_ATTRIBUTE_VALUE_MALFORMED,
new Object[] {BATIK_EXT_TRIM_ATTRIBUTE, s});
}
return new Dimension(w, h);
}
}
1.1
xml-batik/sources/org/apache/batik/extension/svg/renderable/MultiResRable.java
Index: MultiResRable.java
===================================================================
/*****************************************************************************
* Copyright (C) The Apache Software Foundation. All rights reserved. *
* ------------------------------------------------------------------------- *
* This software is published under the terms of the Apache Software License *
* version 1.1, a copy of which has been included with this distribution in *
* the LICENSE file. *
*****************************************************************************/
package org.apache.batik.extension.svg.renderable;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.RenderedImage;
import java.awt.image.renderable.RenderContext;
import java.lang.ref.SoftReference;
import org.apache.batik.ext.awt.image.spi.ImageTagRegistry;
import org.apache.batik.ext.awt.image.renderable.Filter;
import org.apache.batik.ext.awt.image.renderable.AbstractRable;
import org.apache.batik.util.ParsedURL;
/**
* RasterRable This is used to wrap a Rendered Image back into the
* RenderableImage world.
*
* @author <a href="mailto:[EMAIL PROTECTED]>Thomas DeWeese</a>
* @version $Id: MultiResRable.java,v 1.1 2002/02/19 22:08:44 deweese Exp $
*/
public class MultiResRable
extends AbstractRable {
SoftReference [] srcs;
ParsedURL [] srcURLs;
Dimension [] sizes;
Rectangle2D bounds;
public MultiResRable(ParsedURL []srcURLs,
Dimension [] sizes) {
super((Filter)null);
this.srcURLs = new ParsedURL[srcURLs.length];
this.sizes = new Dimension[srcURLs.length];
for (int i=0; i<srcURLs.length; i++) {
this.srcURLs[i] = srcURLs[i];
if (i < sizes.length)
this.sizes[i] = sizes[i];
// System.out.println("Sz: " + this.sizes[i]);
// System.out.println("URL: " + this.srcURLs[i]);
}
this.srcs = new SoftReference[srcURLs.length];
bounds = new Rectangle2D.Float(0, 0, sizes[0].width, sizes[0].height);
}
public Rectangle2D getBounds2D() {
return bounds;
}
public RenderedImage getImage(int idx, RenderContext rc) {
// System.out.println("Getting: " + idx);
Filter f = null;
if (srcs[idx] != null) {
Object o = srcs[idx].get();
if (o != null) f= (Filter)o;
}
if (f == null) {
// System.out.println("Reading: " + srcURLs[idx]);
f = ImageTagRegistry.getRegistry().readURL(srcURLs[idx]);
srcs[idx] = new SoftReference(f);
}
double sx = bounds.getWidth() /(double)f.getWidth();
double sy = bounds.getHeight()/(double)f.getHeight();
// System.out.println("Scale: [" + sx + ", " + sy + "]");
AffineTransform at = rc.getTransform();
at.scale(sx, sy);
rc.setTransform(at);
return f.createRendering(rc);
}
public RenderedImage createRendering(RenderContext rc) {
// get the current affine transform
AffineTransform at = rc.getTransform();
double det = Math.sqrt(at.getDeterminant());
if (det >= 1.0) return getImage(0, rc);
double w = bounds.getWidth()*det;
for (int i=1; i<sizes.length; i++) {
if (w >= sizes[i].width)
return getImage(i-1, rc);
}
return getImage(srcURLs.length-1, rc);
}
}
1.40 +5 -1 xml-batik/sources/org/apache/batik/swing/svg/JSVGComponent.java
Index: JSVGComponent.java
===================================================================
RCS file: /home/cvs/xml-batik/sources/org/apache/batik/swing/svg/JSVGComponent.java,v
retrieving revision 1.39
retrieving revision 1.40
diff -u -r1.39 -r1.40
--- JSVGComponent.java 18 Feb 2002 11:03:20 -0000 1.39
+++ JSVGComponent.java 19 Feb 2002 22:08:44 -0000 1.40
@@ -180,7 +180,7 @@
* building/rendering a document (invalid XML file, missing attributes...).</p>
*
* @author <a href="mailto:[EMAIL PROTECTED]">Stephane Hillion</a>
- * @version $Id: JSVGComponent.java,v 1.39 2002/02/18 11:03:20 hillion Exp $
+ * @version $Id: JSVGComponent.java,v 1.40 2002/02/19 22:08:44 deweese Exp $
*/
public class JSVGComponent extends JGVTComponent {
@@ -1020,6 +1020,10 @@
* Called when a rendering started.
*/
public void gvtRenderingStarted(GVTTreeRendererEvent e) {
+ super.gvtRenderingStarted(e);
+
+ // Don't know if more stuff should go here, but this works
+ // much better than not calling the base class.
}
/**
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]