1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
63
64
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
76
77
78
79
80 public JEuclidElement(final FONode parent) {
81 super(parent);
82 this.layoutContext = new LayoutContextImpl(LayoutContextImpl
83 .getDefaultLayoutContext());
84 }
85
86
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
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
128 @Override
129 public Length getIntrinsicAlignmentAdjust() {
130 if (this.baseline == null) {
131 this.calculate();
132 }
133 return this.baseline;
134 }
135
136
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 }