001 /*
002 * Copyright 2007 - 2008 JEuclid, http://jeuclid.sf.net
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 /* $Id: JEuclidXMLHandler.java,v 32023847f457 2009/03/20 14:20:15 maxberger $ */
018
019 /*
020 * Parts of the contents are heavily inspired by work done for Barcode4J by
021 * Jeremias Maerki, available at http://barcode4j.sf.net/
022 */
023 package net.sourceforge.jeuclid.fop;
024
025 import net.sourceforge.jeuclid.elements.AbstractJEuclidElement;
026 import net.sourceforge.jeuclid.xmlgraphics.Graphics2DImagePainterMathML;
027
028 import org.apache.fop.render.Graphics2DAdapter;
029 import org.apache.fop.render.Renderer;
030 import org.apache.fop.render.RendererContext;
031 import org.apache.fop.render.XMLHandler;
032 import org.apache.xmlgraphics.java2d.Graphics2DImagePainter;
033 import org.w3c.dom.Document;
034
035 /**
036 * XMLHandler which draws MathML through a fop G2DAdapter.
037 *
038 * @version $Revision: 32023847f457 $
039 */
040 public class JEuclidXMLHandler implements XMLHandler {
041
042 /** Creates a new instance of JEuclidXMLHandler. */
043 public JEuclidXMLHandler() {
044 }
045
046 /** {@inheritDoc} */
047 public void handleXML(final RendererContext rendererContext,
048 final Document document, final String ns) throws Exception {
049 final Graphics2DAdapter g2dAdapter = rendererContext.getRenderer()
050 .getGraphics2DAdapter();
051
052 if (g2dAdapter != null) {
053 final Graphics2DImagePainter painter = Graphics2DImagePainterMathML
054 .createGraphics2DImagePainter(document);
055 g2dAdapter.paintImage(painter, rendererContext,
056 ((Integer) rendererContext.getProperty("xpos"))
057 .intValue(), ((Integer) rendererContext
058 .getProperty("ypos")).intValue(),
059 ((Integer) rendererContext.getProperty("width"))
060 .intValue(), ((Integer) rendererContext
061 .getProperty("height")).intValue());
062
063 }
064 }
065
066 /** {@inheritDoc} */
067 public boolean supportsRenderer(final Renderer renderer) {
068 return renderer.getGraphics2DAdapter() != null;
069 }
070
071 /** {@inheritDoc} */
072 public String getNamespace() {
073 return AbstractJEuclidElement.URI;
074 }
075
076 }