xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Tilo Behrmann" <T.behrm...@gmx.net>
Subject AW: Clipping SVG documents
Date Thu, 08 Jun 2006 14:21:56 GMT
Hi Dominik,

i have changed the code a little bit, but it dosent work. I get every time a
null pointer Exception.

 public SVGDocument clipMap(SVGDocument svgDocument, java.awt.Shape shape)
  {
    // first clone the document
    SVGDocument doc = (SVGDocument) svgDocument.cloneNode(true);

    // create an svg element from the clipping shape with the cloned
document
    SVGGraphics2D svgGenerator = new SVGGraphics2D(doc);
    Element svgShape = svgGenerator.getShapeConverter().toSVG(shape);

    // add the clipping element to the <defs> tag of the document
    NodeList n1 = doc.getRootElement().getElementsByTagName(SVGConstants.
        SVG_DEFS_TAG);
    Element defs = (Element) n1.item(0);
    if(defs == null)
    {
      defs = doc.createElementNS(svgNS, SVGConstants.SVG_DEFS_TAG);
      doc.getRootElement().appendChild(defs);
    }

    // create the <clip-path> tag
    Element clip = doc.createElementNS(svgNS,
SVGConstants.SVG_CLIP_PATH_TAG);
    clip.setAttributeNS(null, SVGConstants.SVG_ID_ATTRIBUTE, "clipmap");

    // append the clip shape to the clip path and the <defs> tag
    clip.appendChild(svgShape);
    defs.appendChild(clip);

    // reference the clipping from the element you want to clip
    Element el = doc.getElementById("myelement");
    el.setAttributeNS(null, SVGConstants.SVG_CLIP_PATH_ATTRIBUTE,
                      "url(#clipmap)");

    // Get a DOMImplementation
    DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();

    // Create an instance of org.w3c.dom.Document
    SVGDocument docres = (SVGDocument)impl.createDocument(svgNS, "svg",
null);

    Element root = doc.getDocumentElement();
    root.appendChild(el);
    return docres;
  }



-----Ursprüngliche Nachricht-----
Von: Steiner, Dominik [mailto:Dominik.Steiner@gigatronik.com]
Gesendet: Donnerstag, 8. Juni 2006 15:15
An: batik-users@xmlgraphics.apache.org; T.behrmann@gmx.net
Betreff: AW: Clipping SVG documents


Hi Tilo,

perhaps this one can help:

// shape is your clipping shape, so for example a polygon and
// svgDocument the document you want to clip
public void clipMap(SVGDocument svgDocument, java.awt.Shape shape)
{
	// first clone the document
	SVGDocument doc = (SVGDocument) svgDocument.cloneNode(true);

	// create an svg element from the clipping shape with the cloned
document
	SVGGraphics2D svgGenerator = new SVGGraphics2D(doc);
	Element svgShape = svgGenerator.getShapeConverter().toSVG(shape);

	// add the clipping element to the <defs> tag of the document
	NodeList n1 = doc.getRootElement().getElementsByTagName(
SVGConstants.SVG_DEFS_TAG);
	Element defs = (Element) n1.item(0);

	// create the <clip-path> tag
	Element clip = doc.createElementNS(svgNS, SVGConstants.SVG_CLIP_PATH_TAG);
	clip.setAttributeNS(null, SVGConstants.SVG_ID_ATTRIBUTE, "clipmap");

      // append the clip shape to the clip path and the <defs> tag
	clip.appendChild(svgShape);
	defs.appendChild(clip);

	// reference the clipping from the element you want to clip
	Element el = doc.getElementById("myelement");
	el.setAttributeNS(null, SVGConstants.SVG_CLIP_PATH_ATTRIBUTE,
				"url(#clipmap)");
}


Dominik Steiner
Dipl-Ing. Informationstechnik (BA)
GIGATRONIK
Gesellschaft für Automobil-
elektronikentwicklung mbH
Taunusstr. 21
80807 München
Telefon +49 (0) 89 / 353 96 80-70
Telefax +49 (0) 89 / 353 96 80-99
mailto:dominik.steiner@gigatronik.com
www.gigatronik.com

-----Ursprüngliche Nachricht-----
Von: Tilo Behrmann [mailto:T.behrmann@gmx.net]
Gesendet: Donnerstag, 8. Juni 2006 14:54
An: Batik User
Betreff: Clipping SVG documents

Hi all,

I have an SVG document and I want to generate an SVG document that contains
only a smaller part of this document - just cut a rectangle from the
original image. Does anyone have an idea about how to do this using the
Batik toolkit ?

MfG

Tilo


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org


Mime
View raw message