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