001    /*
002     * Copyright 2002 - 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: Graphics2DImagePainterMathML.java,v 32023847f457 2009/03/20 14:20:15 maxberger $ */
018    
019    package net.sourceforge.jeuclid.xmlgraphics;
020    
021    import java.awt.Dimension;
022    import java.awt.Graphics2D;
023    import java.awt.Image;
024    import java.awt.geom.Rectangle2D;
025    import java.awt.image.BufferedImage;
026    
027    import net.sourceforge.jeuclid.MutableLayoutContext;
028    import net.sourceforge.jeuclid.context.LayoutContextImpl;
029    import net.sourceforge.jeuclid.layout.JEuclidView;
030    
031    import org.apache.xmlgraphics.java2d.Graphics2DImagePainter;
032    import org.w3c.dom.Document;
033    
034    /**
035     * Actually draw an JEuclidView.
036     * 
037     * @version $Revision: 32023847f457 $
038     */
039    public class Graphics2DImagePainterMathML implements Graphics2DImagePainter {
040    
041        private final Dimension dimension;
042    
043        private final JEuclidView view;
044    
045        private final float ascent;
046    
047        /**
048         * Default Constructor.
049         * 
050         * @param theView
051         *            {@link JEuclidView} to paint.
052         * @param dim
053         *            Dimension of the view.
054         * @param asc
055         *            Ascent of the view.
056         */
057        public Graphics2DImagePainterMathML(final JEuclidView theView,
058                final Dimension dim, final float asc) {
059            this.view = theView;
060            this.dimension = dim;
061            this.ascent = asc;
062        }
063    
064        /**
065         * Create a new {@link Graphics2DImagePainter} for the given Document.
066         * 
067         * @param document
068         *            A MathML DOM Document.
069         * @return a {@link Graphics2DImagePainter}.
070         */
071        public static Graphics2DImagePainter createGraphics2DImagePainter(
072                final Document document) {
073            final MutableLayoutContext layoutContext = new LayoutContextImpl(
074                    LayoutContextImpl.getDefaultLayoutContext());
075    
076            final Image tempimage = new BufferedImage(1, 1,
077                    BufferedImage.TYPE_INT_ARGB);
078            final Graphics2D tempg = (Graphics2D) tempimage.getGraphics();
079            final JEuclidView view = new JEuclidView(document, layoutContext,
080                    tempg);
081            final int w = (int) Math.ceil(view.getWidth()
082                    * PreloaderMathML.MPT_FACTOR);
083            final float ascent = view.getAscentHeight();
084            final int h = (int) Math.ceil((ascent + view.getDescentHeight())
085                    * PreloaderMathML.MPT_FACTOR);
086            return new Graphics2DImagePainterMathML(view, new Dimension(w, h),
087                    ascent);
088        }
089    
090        /** {@inheritDoc} */
091        public Dimension getImageSize() {
092            return this.dimension;
093        }
094    
095        /** {@inheritDoc} */
096        public void paint(final Graphics2D graphics2d,
097                final Rectangle2D rectangle2d) {
098            this.view.draw(graphics2d, 0, this.ascent);
099        }
100    }