1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sourceforge.jeuclid.elements.presentation.token;
20
21 import java.awt.Color;
22 import java.awt.Graphics2D;
23 import java.awt.font.TextLayout;
24 import java.text.AttributedCharacterIterator;
25 import java.text.AttributedString;
26 import java.util.Collections;
27 import java.util.List;
28
29 import net.sourceforge.jeuclid.LayoutContext;
30 import net.sourceforge.jeuclid.context.Parameter;
31 import net.sourceforge.jeuclid.elements.AbstractJEuclidElement;
32 import net.sourceforge.jeuclid.elements.support.text.StringUtil;
33 import net.sourceforge.jeuclid.layout.LayoutInfo;
34 import net.sourceforge.jeuclid.layout.LayoutStage;
35 import net.sourceforge.jeuclid.layout.LayoutView;
36 import net.sourceforge.jeuclid.layout.LayoutableNode;
37 import net.sourceforge.jeuclid.layout.TextObject;
38
39 import org.apache.batik.dom.AbstractDocument;
40 import org.w3c.dom.mathml.MathMLPresentationToken;
41
42
43
44
45
46
47 public abstract class AbstractTokenWithTextLayout extends
48 AbstractJEuclidElement implements MathMLPresentationToken {
49
50 private static final long serialVersionUID = 1L;
51
52
53
54
55
56
57
58
59
60 public AbstractTokenWithTextLayout(final String qname,
61 final AbstractDocument odoc) {
62 super(qname, odoc);
63 }
64
65
66 @Override
67 public void layoutStageInvariant(final LayoutView view,
68 final LayoutInfo info, final LayoutStage stage,
69 final LayoutContext context) {
70 final Graphics2D g = view.getGraphics();
71 final TextLayout t = this.produceTextLayout(g, context);
72 if (t != null) {
73 final StringUtil.TextLayoutInfo tli = StringUtil.getTextLayoutInfo(
74 t, false);
75 info.setAscentHeight(tli.getAscent(), stage);
76 info.setDescentHeight(tli.getDescent(), stage);
77 final float width = tli.getWidth();
78 info.setHorizontalCenterOffset(width / 2.0f, stage);
79 info.setWidth(width, stage);
80 info.setGraphicsObject(new TextObject(t, tli.getOffset(),
81 (Color) this.applyLocalAttributesToContext(context)
82 .getParameter(Parameter.MATHCOLOR)));
83 }
84 }
85
86 private TextLayout produceTextLayout(final Graphics2D g2d,
87 final LayoutContext context) {
88 TextLayout layout;
89 final LayoutContext now = this.applyLocalAttributesToContext(context);
90
91 final AttributedCharacterIterator aci = StringUtil
92 .textContentAsAttributedCharacterIterator(now, this, this, 1.0f);
93
94 if (aci.getBeginIndex() == aci.getEndIndex()) {
95 return null;
96 } else {
97 layout = StringUtil.createTextLayoutFromAttributedString(g2d,
98 new AttributedString(aci), now);
99 return layout;
100 }
101 }
102
103
104 @Override
105 public List<LayoutableNode> getChildrenToLayout() {
106 return Collections.emptyList();
107 }
108
109
110 @Override
111 public List<LayoutableNode> getChildrenToDraw() {
112 return Collections.emptyList();
113 }
114
115 }