xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Karel Vervaeke <ka...@outerthought.org>
Subject Create SVG with <svg:a xlink:href> via Java2D graphics.
Date Fri, 24 Jun 2011 11:15:47 GMT
Hi, I am trying insert text links in an SVG document while using
SVGGraphics2D.
The end result should look something like this:

<svg:a xlink:href="...."><svg:text...>Hello world</svg:text></svg:a>

Currently I do this by using an extension handler to put a custom attribute
on the svg:text element.
svg:text elements with this attribute are wrapped in a <svg:a> using xslt,
but I would rather do it in one step.

Is this possible?
Regards,
Karel

ps: here's how I currently add the attribute on the svg:text element:

svgGraphicsContext.setExtensionHandler(new LinkPaintHandler());
svgGraphics2d.setPaint(new LinkPaint("http://www.google.com"));
svgGraphics2D.drawString("Hello world", 100, 200);

public class LinkPaint extends Paint {
  private String href;
  public LinkPaint(String href) {
    this.href = href;
  }
  // getHref, setHref...
}

public class LinkPaintHandler extends DefaultExtensionHandler {
    public SVGPaintDescriptor handlePaint(Paint paint,
            SVGGeneratorContext generatorContext) {
        if (paint instanceof LinkPaint) {
            Element def =
generatorContext.getDOMFactory().createElementNS(SVGUtil.SVG_NS, "def");
            String id =
generatorContext.getIDGenerator().generateID("paintdef");
            def.setAttribute("id", id);
            return new SVGLinkPaintDescriptor(((LinkPaint)paint).getHref());
        }
        return null;
    }
}

public class SVGLinkPaintDescriptor extends SVGPaintDescriptor {
    private String href;
    public SVGLinkPaintDescriptor(String href) {
        super("#0000ff", SVG_OPAQUE_VALUE);
        this.href = href;
    }

    @Override
    public Map getAttributeMap(Map attrMap) {
        Map result = super.getAttributeMap(attrMap);

        if (result == null)
            result = new HashMap();

        if (href != null)
            result.put(XLINK_HREF_QNAME, href);

        return result;
    }
}



-- 
Karel Vervaeke
http://outerthought.org/
Open Source Content Applications
Makers of Kauri, Daisy CMS and Lily

Mime
View raw message