View Javadoc

1   /*
2    * Copyright 2007 - 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: JEuclidElement.java,v 38db1a498ca5 2009/09/25 20:17:03 max $ */
18  
19  /*
20   * Please note: This file was originally taken from the Apache FOP project,
21   * available at http://xmlgraphics.apache.org/fop/ It is therefore
22   * partially copyright (c) 1999-2007 The Apache Software Foundation.
23   * 
24   * Parts of the contents are heavily inspired by work done for Barcode4J by
25   * Jeremias Maerki, available at http://barcode4j.sf.net/
26   */
27  
28  package net.sourceforge.jeuclid.fop;
29  
30  import java.awt.Color;
31  import java.awt.Graphics2D;
32  import java.awt.Image;
33  import java.awt.geom.Point2D;
34  import java.awt.image.BufferedImage;
35  import java.util.ArrayList;
36  import java.util.List;
37  
38  import net.sourceforge.jeuclid.Constants;
39  import net.sourceforge.jeuclid.MutableLayoutContext;
40  import net.sourceforge.jeuclid.context.LayoutContextImpl;
41  import net.sourceforge.jeuclid.context.Parameter;
42  import net.sourceforge.jeuclid.layout.JEuclidView;
43  import net.sourceforge.jeuclid.xmlgraphics.PreloaderMathML;
44  
45  import org.apache.fop.apps.FOPException;
46  import org.apache.fop.apps.FOUserAgent;
47  import org.apache.fop.datatypes.Length;
48  import org.apache.fop.fo.FOEventHandler;
49  import org.apache.fop.fo.FONode;
50  import org.apache.fop.fo.PropertyList;
51  import org.apache.fop.fo.properties.CommonFont;
52  import org.apache.fop.fo.properties.FixedLength;
53  import org.apache.fop.fo.properties.Property;
54  import org.apache.fop.fonts.FontInfo;
55  import org.apache.fop.fonts.FontTriplet;
56  import org.w3c.dom.Document;
57  import org.w3c.dom.Element;
58  import org.xml.sax.Attributes;
59  import org.xml.sax.Locator;
60  
61  /**
62   * Defines the top-level element for MathML.
63   * 
64   * @version $Revision: 38db1a498ca5 $
65   */
66  public class JEuclidElement extends JEuclidObj {
67  
68      private Point2D size;
69  
70      private Length baseline;
71  
72      private final MutableLayoutContext layoutContext;
73  
74      /**
75       * Default constructor.
76       * 
77       * @param parent
78       *            Parent Node in the FO tree.
79       */
80      public JEuclidElement(final FONode parent) {
81          super(parent);
82          this.layoutContext = new LayoutContextImpl(LayoutContextImpl
83                  .getDefaultLayoutContext());
84      }
85  
86      /** {@inheritDoc} */
87      @Override
88      public void processNode(final String elementName, final Locator locator,
89              final Attributes attlist, final PropertyList propertyList)
90              throws FOPException {
91          super.processNode(elementName, locator, attlist, propertyList);
92          final Document d = this.createBasicDocument();
93          final Element e = d.getDocumentElement();
94          for (final Parameter p : Parameter.values()) {
95              final String localName = p.getOptionName();
96              final String attrName = "jeuclid:" + localName;
97              final String isSet = e.getAttributeNS(Constants.NS_JEUCLID_EXT,
98                      localName);
99              if ((isSet == null) || (isSet.length() == 0)) {
100                 e.setAttributeNS(Constants.NS_JEUCLID_EXT, attrName, p
101                         .toString(this.layoutContext.getParameter(p)));
102             }
103         }
104     }
105 
106     private void calculate() {
107         final Image tempimage = new BufferedImage(1, 1,
108                 BufferedImage.TYPE_INT_ARGB);
109         final Graphics2D tempg = (Graphics2D) tempimage.getGraphics();
110         final JEuclidView view = new JEuclidView(this.doc, this.layoutContext,
111                 tempg);
112         final float descent = view.getDescentHeight();
113         this.size = new Point2D.Float(view.getWidth(), view.getAscentHeight()
114                 + descent);
115         this.baseline = FixedLength.getInstance(-descent, "pt");
116     }
117 
118     /** {@inheritDoc} */
119     @Override
120     public Point2D getDimension(final Point2D view) {
121         if (this.size == null) {
122             this.calculate();
123         }
124         return this.size;
125     }
126 
127     /** {@inheritDoc} */
128     @Override
129     public Length getIntrinsicAlignmentAdjust() {
130         if (this.baseline == null) {
131             this.calculate();
132         }
133         return this.baseline;
134     }
135 
136     /** {@inheritDoc} */
137     @SuppressWarnings("unchecked")
138     @Override
139     protected PropertyList createPropertyList(final PropertyList pList,
140             final FOEventHandler foEventHandler) throws FOPException {
141         final FOUserAgent userAgent = this.getUserAgent();
142         final CommonFont commonFont = pList.getFontProps();
143         final float msize = (float) (commonFont.fontSize.getNumericValue() / PreloaderMathML.MPT_FACTOR);
144         final Property colorProp = pList
145                 .get(org.apache.fop.fo.Constants.PR_COLOR);
146         if (colorProp != null) {
147             final Color color = colorProp.getColor(userAgent);
148             this.layoutContext.setParameter(Parameter.MATHCOLOR, color);
149         }
150         final Property bcolorProp = pList
151                 .get(org.apache.fop.fo.Constants.PR_BACKGROUND_COLOR);
152         if (bcolorProp != null) {
153             final Color bcolor = bcolorProp.getColor(userAgent);
154             this.layoutContext.setParameter(Parameter.MATHBACKGROUND, bcolor);
155         }
156         final FontInfo fi = this.getFOEventHandler().getFontInfo();
157         final FontTriplet[] fontkeys = commonFont.getFontState(fi);
158 
159         this.layoutContext.setParameter(Parameter.MATHSIZE, msize);
160         final List<String> defaultFonts = (List<String>) this.layoutContext
161                 .getParameter(Parameter.FONTS_SERIF);
162         final List<String> newFonts = new ArrayList<String>(fontkeys.length
163                 + defaultFonts.size());
164         for (final FontTriplet t : fontkeys) {
165             newFonts.add(t.getName());
166         }
167         newFonts.addAll(defaultFonts);
168         this.layoutContext.setParameter(Parameter.FONTS_SERIF, newFonts);
169         return super.createPropertyList(pList, foEventHandler);
170     }
171 }