| 1 |
/* JLaTeXMathElement.java |
| 2 |
* ========================================================================= |
| 3 |
* This file is part of the JLaTeXMath Library - http://forge.scilab.org/jlatexmath |
| 4 |
* |
| 5 |
* Copyright (C) 2010 DENIZET Calixte |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or (at |
| 10 |
* your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, but |
| 13 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 |
* General Public License for more details. |
| 16 |
* |
| 17 |
* A copy of the GNU General Public License can be found in the file |
| 18 |
* LICENSE.txt provided with the source distribution of this program (see |
| 19 |
* the META-INF directory in the source jar). This license can also be |
| 20 |
* found on the GNU website at http://www.gnu.org/licenses/gpl.html. |
| 21 |
* |
| 22 |
* If you did not receive a copy of the GNU General Public License along |
| 23 |
* with this program, contact the lead developer, or write to the Free |
| 24 |
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 25 |
* 02110-1301, USA. |
| 26 |
* |
| 27 |
* Linking this library statically or dynamically with other modules |
| 28 |
* is making a combined work based on this library. Thus, the terms |
| 29 |
* and conditions of the GNU General Public License cover the whole |
| 30 |
* combination. |
| 31 |
* |
| 32 |
* As a special exception, the copyright holders of this library give you |
| 33 |
* permission to link this library with independent modules to produce |
| 34 |
* an executable, regardless of the license terms of these independent |
| 35 |
* modules, and to copy and distribute the resulting executable under terms |
| 36 |
* of your choice, provided that you also meet, for each linked independent |
| 37 |
* module, the terms and conditions of the license of that module. |
| 38 |
* An independent module is a module which is not derived from or based |
| 39 |
* on this library. If you modify this library, you may extend this exception |
| 40 |
* to your version of the library, but you are not obliged to do so. |
| 41 |
* If you do not wish to do so, delete this exception statement from your |
| 42 |
* version. |
| 43 |
* |
| 44 |
*/ |
| 45 |
|
| 46 |
/* This file is largely inspired by files wrote by Jeremias Maerki, |
| 47 |
* for the fop plugin of barcode4j available at |
| 48 |
* http://barcode4j.sourceforge.net/ |
| 49 |
*/ |
| 50 |
|
| 51 |
package org.scilab.forge.jlatexmath.fop; |
| 52 |
|
| 53 |
import java.awt.Color; |
| 54 |
import java.awt.geom.Point2D; |
| 55 |
import java.util.HashMap; |
| 56 |
import java.util.Map; |
| 57 |
import java.util.StringTokenizer; |
| 58 |
|
| 59 |
import org.apache.fop.apps.FOPException; |
| 60 |
import org.apache.fop.apps.FOUserAgent; |
| 61 |
import org.apache.fop.datatypes.Length; |
| 62 |
import org.apache.fop.fo.FOEventHandler; |
| 63 |
import org.apache.fop.fo.FONode; |
| 64 |
import org.apache.fop.fo.PropertyList; |
| 65 |
import org.apache.fop.fo.properties.CommonFont; |
| 66 |
import org.apache.fop.fo.properties.FixedLength; |
| 67 |
import org.apache.fop.fo.properties.Property; |
| 68 |
import org.scilab.forge.jlatexmath.TeXConstants; |
| 69 |
import org.scilab.forge.jlatexmath.TeXFormula; |
| 70 |
import org.scilab.forge.jlatexmath.TeXIcon; |
| 71 |
import org.w3c.dom.Document; |
| 72 |
import org.w3c.dom.Element; |
| 73 |
import org.w3c.dom.NamedNodeMap; |
| 74 |
import org.xml.sax.Attributes; |
| 75 |
import org.xml.sax.Locator; |
| 76 |
|
| 77 |
public class JLaTeXMathElement extends JLaTeXMathObj { |
| 78 |
|
| 79 |
private float size; |
| 80 |
private Color fg; |
| 81 |
private TeXIcon icon = null; |
| 82 |
private boolean bool = true; |
| 83 |
private String PR_COLOR = "PR_COLOR"; |
| 84 |
|
| 85 |
public JLaTeXMathElement(FONode parent) { |
| 86 |
super(parent); |
| 87 |
} |
| 88 |
|
| 89 |
public void processNode(final String elementName, final Locator locator, |
| 90 |
final Attributes attlist, final PropertyList propertyList) |
| 91 |
throws FOPException { |
| 92 |
super.processNode(elementName, locator, attlist, propertyList); |
| 93 |
Element e = createBasicDocument().getDocumentElement(); |
| 94 |
e.setAttribute("size", "" + size); |
| 95 |
e.setAttribute("fg", "" + fg.getRGB()); |
| 96 |
} |
| 97 |
|
| 98 |
public Point2D getDimension(Point2D p) { |
| 99 |
if (icon == null) { |
| 100 |
icon = calculate(doc, size); |
| 101 |
} |
| 102 |
return new Point2D.Float(icon.getTrueIconWidth(), icon.getTrueIconHeight()); |
| 103 |
} |
| 104 |
|
| 105 |
public Length getIntrinsicAlignmentAdjust() { |
| 106 |
if (icon == null) { |
| 107 |
icon = calculate(doc, size); |
| 108 |
} |
| 109 |
return FixedLength.getInstance(-icon.getTrueIconDepth(), "px"); |
| 110 |
} |
| 111 |
|
| 112 |
public static float getFWidth(String str) { |
| 113 |
StringTokenizer tok = new StringTokenizer(str, ","); |
| 114 |
int sum = 0; |
| 115 |
while (tok.hasMoreTokens()) { |
| 116 |
int i = 0; |
| 117 |
String s = tok.nextToken(); |
| 118 |
for (; i < s.length() && !Character.isLetter(s.charAt(i)); i++); |
| 119 |
double w = 0; |
| 120 |
try { |
| 121 |
w = Double.parseDouble(s.substring(0, i)); |
| 122 |
} catch (NumberFormatException e) { |
| 123 |
return 0.0f; |
| 124 |
} |
| 125 |
|
| 126 |
String unit = "px"; |
| 127 |
if (i != s.length()) { |
| 128 |
unit = s.substring(i).toLowerCase(); |
| 129 |
} |
| 130 |
|
| 131 |
sum += FixedLength.getInstance(w, unit).getValue(); |
| 132 |
} |
| 133 |
|
| 134 |
return (float) (sum / 1000f); |
| 135 |
} |
| 136 |
|
| 137 |
public static TeXIcon calculate(Document doc, float size) { |
| 138 |
TeXIcon icon; |
| 139 |
Element e = doc.getDocumentElement(); |
| 140 |
String code = e.getTextContent(); |
| 141 |
String style = e.getAttribute("style"); |
| 142 |
int st = TeXConstants.STYLE_DISPLAY; |
| 143 |
if ("text".equals(style)) { |
| 144 |
st = TeXConstants.STYLE_TEXT; |
| 145 |
} else if ("script".equals(style)) { |
| 146 |
st = TeXConstants.STYLE_SCRIPT; |
| 147 |
} else if ("script_script".equals(style)) { |
| 148 |
st = TeXConstants.STYLE_SCRIPT_SCRIPT; |
| 149 |
} |
| 150 |
|
| 151 |
NamedNodeMap attributes = e.getAttributes(); |
| 152 |
int len = attributes.getLength(); |
| 153 |
Map<String, String> map = new HashMap<String, String>(); |
| 154 |
for (int i = 0; i < len; i++) { |
| 155 |
map.put(attributes.item(i).getNodeName(), attributes.item(i).getNodeValue()); |
| 156 |
} |
| 157 |
|
| 158 |
String stfw = e.getAttribute("fwidth"); |
| 159 |
if (stfw.length() != 0) { |
| 160 |
icon = new TeXFormula(code, map).createTeXIcon(st, size, TeXConstants.UNIT_PIXEL, getFWidth(stfw), TeXConstants.ALIGN_CENTER); |
| 161 |
} else { |
| 162 |
icon = new TeXFormula(code, map).createTeXIcon(st, size, true); |
| 163 |
} |
| 164 |
|
| 165 |
return icon; |
| 166 |
} |
| 167 |
|
| 168 |
protected PropertyList createPropertyList(final PropertyList pList, |
| 169 |
final FOEventHandler foEventHandler) throws FOPException { |
| 170 |
FOUserAgent userAgent = this.getUserAgent(); |
| 171 |
CommonFont commonFont = pList.getFontProps(); |
| 172 |
this.size = (float) commonFont.fontSize.getNumericValue() / 1000; |
| 173 |
|
| 174 |
int n = org.apache.fop.fo.Constants.PR_COLOR; |
| 175 |
try { |
| 176 |
n = org.apache.fop.fo.Constants.class.getDeclaredField(PR_COLOR).getInt(null); |
| 177 |
} catch (Exception e) { |
| 178 |
System.err.println("Error in getting field:\n" + e); |
| 179 |
} |
| 180 |
|
| 181 |
Property colorProp = pList.get(n); |
| 182 |
|
| 183 |
this.fg = colorProp != null ? colorProp.getColor(userAgent) : null; |
| 184 |
|
| 185 |
return super.createPropertyList(pList, foEventHandler); |
| 186 |
} |
| 187 |
} |