|
| 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$ */ |
| 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.geom.Point2D; |
| 32 | +import java.util.ArrayList; |
| 33 | +import java.util.List; |
| 34 | + |
| 35 | +import net.sourceforge.jeuclid.Constants; |
| 36 | +import net.sourceforge.jeuclid.MutableLayoutContext; |
| 37 | +import net.sourceforge.jeuclid.context.LayoutContextImpl; |
| 38 | +import net.sourceforge.jeuclid.context.Parameter; |
| 39 | +import net.sourceforge.jeuclid.layout.JEuclidView; |
| 40 | +import net.sourceforge.jeuclid.xmlgraphics.PreloaderMathML; |
| 41 | + |
| 42 | +import org.apache.fop.apps.FOPException; |
| 43 | +import org.apache.fop.apps.FOUserAgent; |
| 44 | +import org.apache.fop.datatypes.Length; |
| 45 | +import org.apache.fop.fo.FOEventHandler; |
| 46 | +import org.apache.fop.fo.FONode; |
| 47 | +import org.apache.fop.fo.PropertyList; |
| 48 | +import org.apache.fop.fo.properties.CommonFont; |
| 49 | +import org.apache.fop.fo.properties.FixedLength; |
| 50 | +import org.apache.fop.fo.properties.Property; |
| 51 | +import org.apache.fop.fonts.FontInfo; |
| 52 | +import org.apache.fop.fonts.FontTriplet; |
| 53 | +import org.w3c.dom.Document; |
| 54 | +import org.w3c.dom.Element; |
| 55 | +import org.xml.sax.Attributes; |
| 56 | +import org.xml.sax.Locator; |
| 57 | + |
| 58 | +/** |
| 59 | + * Defines the top-level element for MathML. |
| 60 | + * |
| 61 | + * @version $Revision$ |
| 62 | + */ |
| 63 | +public class JEuclidElement extends JEuclidObj { |
| 64 | + |
| 65 | + private Point2D size; |
| 66 | + |
| 67 | + private Length baseline; |
| 68 | + |
| 69 | + private final MutableLayoutContext layoutContext; |
| 70 | + |
| 71 | + /** |
| 72 | + * Default constructor. |
| 73 | + * |
| 74 | + * @param parent |
| 75 | + * Parent Node in the FO tree. |
| 76 | + */ |
| 77 | + public JEuclidElement(final FONode parent) { |
| 78 | + super(parent); |
| 79 | + this.layoutContext = new LayoutContextImpl(LayoutContextImpl |
| 80 | + .getDefaultLayoutContext()); |
| 81 | + } |
| 82 | + |
| 83 | + /** {@inheritDoc} */ |
| 84 | + @Override |
| 85 | + public void processNode(final String elementName, final Locator locator, |
| 86 | + final Attributes attlist, final PropertyList propertyList) |
| 87 | + throws FOPException { |
| 88 | + super.processNode(elementName, locator, attlist, propertyList); |
| 89 | + final Document d = this.createBasicDocument(); |
| 90 | + final Element e = d.getDocumentElement(); |
| 91 | + for (final Parameter p : Parameter.values()) { |
| 92 | + final String localName = p.getOptionName(); |
| 93 | + final String attrName = "jeuclid:" + localName; |
| 94 | + final String isSet = e.getAttributeNS(Constants.NS_JEUCLID_EXT, |
| 95 | + localName); |
| 96 | + if ((isSet == null) || (isSet.length() == 0)) { |
| 97 | + e.setAttributeNS(Constants.NS_JEUCLID_EXT, attrName, p |
| 98 | + .toString(this.layoutContext.getParameter(p))); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private void calculate() { |
| 104 | + final JEuclidView view = new JEuclidView(this.doc, this.layoutContext, |
| 105 | + null); |
| 106 | + final float descent = view.getDescentHeight(); |
| 107 | + this.size = new Point2D.Float(view.getWidth(), view.getAscentHeight() |
| 108 | + + descent); |
| 109 | + this.baseline = FixedLength.getInstance(-descent, "pt"); |
| 110 | + } |
| 111 | + |
| 112 | + /** {@inheritDoc} */ |
| 113 | + @Override |
| 114 | + public Point2D getDimension(final Point2D view) { |
| 115 | + if (this.size == null) { |
| 116 | + this.calculate(); |
| 117 | + } |
| 118 | + return this.size; |
| 119 | + } |
| 120 | + |
| 121 | + /** {@inheritDoc} */ |
| 122 | + @Override |
| 123 | + public Length getIntrinsicAlignmentAdjust() { |
| 124 | + if (this.baseline == null) { |
| 125 | + this.calculate(); |
| 126 | + } |
| 127 | + return this.baseline; |
| 128 | + } |
| 129 | + |
| 130 | + /** {@inheritDoc} */ |
| 131 | + @SuppressWarnings("unchecked") |
| 132 | + @Override |
| 133 | + protected PropertyList createPropertyList(final PropertyList pList, |
| 134 | + final FOEventHandler foEventHandler) throws FOPException { |
| 135 | + final FOUserAgent userAgent = this.getUserAgent(); |
| 136 | + final CommonFont commonFont = pList.getFontProps(); |
| 137 | + final float msize = (float) (commonFont.fontSize.getNumericValue() / PreloaderMathML.MPT_FACTOR); |
| 138 | + final Property colorProp = pList |
| 139 | + .get(org.apache.fop.fo.Constants.PR_COLOR); |
| 140 | + if (colorProp != null) { |
| 141 | + final Color color = colorProp.getColor(userAgent); |
| 142 | + this.layoutContext.setParameter(Parameter.MATHCOLOR, color); |
| 143 | + } |
| 144 | + final Property bcolorProp = pList |
| 145 | + .get(org.apache.fop.fo.Constants.PR_BACKGROUND_COLOR); |
| 146 | + if (bcolorProp != null) { |
| 147 | + final Color bcolor = bcolorProp.getColor(userAgent); |
| 148 | + this.layoutContext.setParameter(Parameter.MATHBACKGROUND, bcolor); |
| 149 | + } |
| 150 | + final FontInfo fi = this.getFOEventHandler().getFontInfo(); |
| 151 | + final FontTriplet[] fontkeys = commonFont.getFontState(fi); |
| 152 | + |
| 153 | + this.layoutContext.setParameter(Parameter.MATHSIZE, msize); |
| 154 | + final List<String> defaultFonts = (List<String>) this.layoutContext |
| 155 | + .getParameter(Parameter.FONTS_SERIF); |
| 156 | + final List<String> newFonts = new ArrayList<String>(fontkeys.length |
| 157 | + + defaultFonts.size()); |
| 158 | + for (final FontTriplet t : fontkeys) { |
| 159 | + newFonts.add(t.getName()); |
| 160 | + } |
| 161 | + newFonts.addAll(defaultFonts); |
| 162 | + this.layoutContext.setParameter(Parameter.FONTS_SERIF, newFonts); |
| 163 | + return super.createPropertyList(pList, foEventHandler); |
| 164 | + } |
| 165 | +} |
0 commit comments