View Javadoc

1   /*
2    * Copyright 2002 - 2008 JEuclid, http://jeuclid.sf.net
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  /* $Id: Graphics2DImagePainterMathML.java,v 32023847f457 2009/03/20 14:20:15 maxberger $ */
18  
19  package net.sourceforge.jeuclid.xmlgraphics;
20  
21  import java.awt.Dimension;
22  import java.awt.Graphics2D;
23  import java.awt.Image;
24  import java.awt.geom.Rectangle2D;
25  import java.awt.image.BufferedImage;
26  
27  import net.sourceforge.jeuclid.MutableLayoutContext;
28  import net.sourceforge.jeuclid.context.LayoutContextImpl;
29  import net.sourceforge.jeuclid.layout.JEuclidView;
30  
31  import org.apache.xmlgraphics.java2d.Graphics2DImagePainter;
32  import org.w3c.dom.Document;
33  
34  /**
35   * Actually draw an JEuclidView.
36   * 
37   * @version $Revision: 32023847f457 $
38   */
39  public class Graphics2DImagePainterMathML implements Graphics2DImagePainter {
40  
41      private final Dimension dimension;
42  
43      private final JEuclidView view;
44  
45      private final float ascent;
46  
47      /**
48       * Default Constructor.
49       * 
50       * @param theView
51       *            {@link JEuclidView} to paint.
52       * @param dim
53       *            Dimension of the view.
54       * @param asc
55       *            Ascent of the view.
56       */
57      public Graphics2DImagePainterMathML(final JEuclidView theView,
58              final Dimension dim, final float asc) {
59          this.view = theView;
60          this.dimension = dim;
61          this.ascent = asc;
62      }
63  
64      /**
65       * Create a new {@link Graphics2DImagePainter} for the given Document.
66       * 
67       * @param document
68       *            A MathML DOM Document.
69       * @return a {@link Graphics2DImagePainter}.
70       */
71      public static Graphics2DImagePainter createGraphics2DImagePainter(
72              final Document document) {
73          final MutableLayoutContext layoutContext = new LayoutContextImpl(
74                  LayoutContextImpl.getDefaultLayoutContext());
75  
76          final Image tempimage = new BufferedImage(1, 1,
77                  BufferedImage.TYPE_INT_ARGB);
78          final Graphics2D tempg = (Graphics2D) tempimage.getGraphics();
79          final JEuclidView view = new JEuclidView(document, layoutContext,
80                  tempg);
81          final int w = (int) Math.ceil(view.getWidth()
82                  * PreloaderMathML.MPT_FACTOR);
83          final float ascent = view.getAscentHeight();
84          final int h = (int) Math.ceil((ascent + view.getDescentHeight())
85                  * PreloaderMathML.MPT_FACTOR);
86          return new Graphics2DImagePainterMathML(view, new Dimension(w, h),
87                  ascent);
88      }
89  
90      /** {@inheritDoc} */
91      public Dimension getImageSize() {
92          return this.dimension;
93      }
94  
95      /** {@inheritDoc} */
96      public void paint(final Graphics2D graphics2d,
97              final Rectangle2D rectangle2d) {
98          this.view.draw(graphics2d, 0, this.ascent);
99      }
100 }