Search in sources :

Example 1 with UserAgentAdapter

use of org.apache.batik.bridge.UserAgentAdapter in project charts by vaadin.

the class PdfExportDemo method buildBatikGraphicsNode.

/**
 * Use Batik SVG Toolkit to create GraphicsNode for the target SVG.
 * <ol>
 * <li>Create SVGDocument</li>
 * <li>Create BridgeContext</li>
 * <li>Build GVT tree. Results to GraphicsNode</li>
 * </ol>
 *
 * @param svg
 *            SVG as a String
 * @return GraphicsNode
 * @throws IOException
 *             Thrown when SVG could not be read properly.
 */
private GraphicsNode buildBatikGraphicsNode(String svg) throws IOException {
    UserAgent agent = new UserAgentAdapter();
    SVGDocument svgdoc = createSVGDocument(svg, agent);
    DocumentLoader loader = new DocumentLoader(agent);
    BridgeContext bridgeContext = new BridgeContext(agent, loader);
    bridgeContext.setDynamicState(BridgeContext.STATIC);
    GVTBuilder builder = new GVTBuilder();
    GraphicsNode imageGraphics = builder.build(bridgeContext, svgdoc);
    return imageGraphics;
}
Also used : UserAgentAdapter(org.apache.batik.bridge.UserAgentAdapter) UserAgent(org.apache.batik.bridge.UserAgent) BridgeContext(org.apache.batik.bridge.BridgeContext) DocumentLoader(org.apache.batik.bridge.DocumentLoader) SVGDocument(org.w3c.dom.svg.SVGDocument) GVTBuilder(org.apache.batik.bridge.GVTBuilder) GraphicsNode(org.apache.batik.gvt.GraphicsNode)

Example 2 with UserAgentAdapter

use of org.apache.batik.bridge.UserAgentAdapter in project megameklab by MegaMek.

the class PrintRecordSheet method build.

protected GraphicsNode build() {
    GVTBuilder builder = new GVTBuilder();
    BridgeContext ctx = new BridgeContext(new UserAgentAdapter() {

        @Override
        public // rather than throwing an exception.
        SVGDocument getBrokenLinkDocument(Element e, String url, String message) {
            LogManager.getLogger().warn("Cannot render image: " + message);
            DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
            SVGDocument doc = (SVGDocument) impl.createDocument(svgNS, SVGConstants.SVG_SVG_TAG, null);
            Element text = doc.createElementNS(svgNS, SVGConstants.SVG_TEXT_TAG);
            text.setTextContent("?");
            doc.getDocumentElement().appendChild(text);
            return doc;
        }
    });
    ctx.setDynamic(true);
    return builder.build(ctx, svgDocument);
}
Also used : UserAgentAdapter(org.apache.batik.bridge.UserAgentAdapter) SVGRectElement(org.w3c.dom.svg.SVGRectElement) Element(org.w3c.dom.Element) BridgeContext(org.apache.batik.bridge.BridgeContext) DOMImplementation(org.w3c.dom.DOMImplementation) SVGDOMImplementation(org.apache.batik.anim.dom.SVGDOMImplementation) GVTBuilder(org.apache.batik.bridge.GVTBuilder) SVGDocument(org.w3c.dom.svg.SVGDocument)

Example 3 with UserAgentAdapter

use of org.apache.batik.bridge.UserAgentAdapter in project megameklab by MegaMek.

the class PrintRecordSheet method build.

protected GraphicsNode build() {
    GVTBuilder builder = new GVTBuilder();
    BridgeContext ctx = new BridgeContext(new UserAgentAdapter() {

        @Override
        public // rather than throwing an exception.
        SVGDocument getBrokenLinkDocument(Element e, String url, String message) {
            MegaMekLab.getLogger().log(PrintRecordSheet.class, "build()", LogLevel.WARNING, "Cannot render image: " + message);
            DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
            SVGDocument doc = (SVGDocument) impl.createDocument(svgNS, "svg", null);
            Element text = doc.createElementNS(svgNS, SVGConstants.SVG_TEXT_TAG);
            text.setTextContent("?");
            doc.getDocumentElement().appendChild(text);
            return doc;
        }
    });
    ctx.setDynamic(true);
    return builder.build(ctx, svgDocument);
}
Also used : UserAgentAdapter(org.apache.batik.bridge.UserAgentAdapter) SVGRectElement(org.w3c.dom.svg.SVGRectElement) SVGElement(org.w3c.dom.svg.SVGElement) Element(org.w3c.dom.Element) BridgeContext(org.apache.batik.bridge.BridgeContext) DOMImplementation(org.w3c.dom.DOMImplementation) SVGDOMImplementation(org.apache.batik.anim.dom.SVGDOMImplementation) GVTBuilder(org.apache.batik.bridge.GVTBuilder) SVGDocument(org.w3c.dom.svg.SVGDocument)

Example 4 with UserAgentAdapter

use of org.apache.batik.bridge.UserAgentAdapter in project scout.rt by eclipse.

the class SVGUtility method readSVGDocumentForGraphicalModification.

/**
 * Parses a SVG document read by the given input stream and attaches a GVT tree. An attached GVT tree is required for
 * performing CSS, text size and and bounding box operations on the SVG document.
 * <p/>
 * The resulting bridge context holds a reference to the SVG document {@link BridgeContext#getDocument()}
 * <p/>
 * <h1>Important:</h1> Callers are required to invoke {@link BridgeContext#dispose()} on the returned bridge context
 * as soon as the bridge or the document it references is not required anymore.
 * <p/>
 * If the documents needs not be manipulated, use {@link #readSVGDocument(InputStream)} instead.
 *
 * @param in
 *          input stream the SVG document is read from.
 * @return Returns a bridge context that holds references to the SVG document as well as to the GVT tree wrapping
 *         objects.
 */
public static BridgeContext readSVGDocumentForGraphicalModification(InputStream in) {
    SVGDocument doc = readSVGDocument(in);
    // add a gvt tree for text and alignment calculations
    BridgeContext bc = new BridgeContext(new UserAgentAdapter());
    bc.setDynamic(true);
    GVTBuilder builder = new DynamicGVTBuilder();
    builder.build(bc, doc);
    return bc;
}
Also used : DynamicGVTBuilder(org.apache.batik.bridge.DynamicGVTBuilder) UserAgentAdapter(org.apache.batik.bridge.UserAgentAdapter) BridgeContext(org.apache.batik.bridge.BridgeContext) SVGDocument(org.w3c.dom.svg.SVGDocument) GVTBuilder(org.apache.batik.bridge.GVTBuilder) DynamicGVTBuilder(org.apache.batik.bridge.DynamicGVTBuilder)

Aggregations

BridgeContext (org.apache.batik.bridge.BridgeContext)4 GVTBuilder (org.apache.batik.bridge.GVTBuilder)4 UserAgentAdapter (org.apache.batik.bridge.UserAgentAdapter)4 SVGDocument (org.w3c.dom.svg.SVGDocument)4 SVGDOMImplementation (org.apache.batik.anim.dom.SVGDOMImplementation)2 DOMImplementation (org.w3c.dom.DOMImplementation)2 Element (org.w3c.dom.Element)2 SVGRectElement (org.w3c.dom.svg.SVGRectElement)2 DocumentLoader (org.apache.batik.bridge.DocumentLoader)1 DynamicGVTBuilder (org.apache.batik.bridge.DynamicGVTBuilder)1 UserAgent (org.apache.batik.bridge.UserAgent)1 GraphicsNode (org.apache.batik.gvt.GraphicsNode)1 SVGElement (org.w3c.dom.svg.SVGElement)1