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.Font;
22 import java.awt.font.TextAttribute;
23 import java.text.AttributedCharacterIterator;
24 import java.text.AttributedString;
25
26 import net.sourceforge.jeuclid.LayoutContext;
27 import net.sourceforge.jeuclid.elements.support.GraphicsSupport;
28 import net.sourceforge.jeuclid.elements.support.text.StringUtil;
29 import net.sourceforge.jeuclid.elements.support.text.TextContentModifier;
30 import net.sourceforge.jeuclid.font.FontFactory;
31
32 import org.apache.batik.dom.AbstractDocument;
33 import org.w3c.dom.Node;
34 import org.w3c.dom.mathml.MathMLGlyphElement;
35
36
37
38
39
40
41
42
43
44
45
46
47
48 public final class Mglyph extends AbstractTokenWithTextLayout implements
49 MathMLGlyphElement, TextContentModifier {
50
51
52
53
54 public static final String ELEMENT = "mglyph";
55
56 private static final String ATTR_ALT = "alt";
57
58 private static final String ATTR_FONTFAMILY = "fontfamily";
59
60 private static final String ATTR_INDEX = "index";
61
62 private static final long serialVersionUID = 1L;
63
64
65
66
67
68
69
70
71
72 public Mglyph(final String qname, final AbstractDocument odoc) {
73 super(qname, odoc);
74 }
75
76
77 @Override
78 protected Node newNode() {
79 return new Mglyph(this.nodeName, this.ownerDocument);
80 }
81
82
83
84 public AttributedCharacterIterator modifyTextContent(
85 final AttributedCharacterIterator aci, final LayoutContext now) {
86 final AttributedString retVal;
87 final String ffamily = this.getFontfamily();
88 final String fontFamily;
89 if (ffamily == null) {
90 fontFamily = "serif";
91 } else {
92 fontFamily = ffamily.trim();
93 }
94 final Font font = FontFactory.getInstance().getFont(fontFamily,
95 Font.PLAIN, GraphicsSupport.getFontsizeInPoint(now));
96 final int codePoint = this.getIndex();
97 if ((codePoint > 0) && (font.getFamily().equalsIgnoreCase(fontFamily))
98 && (font.canDisplay(codePoint))) {
99 retVal = new AttributedString(new String(new int[] { codePoint },
100 0, 1));
101 retVal.addAttribute(TextAttribute.FONT, font);
102 } else {
103 retVal = StringUtil.convertStringtoAttributedString(this.getAlt(),
104 this.getMathvariantAsVariant(), GraphicsSupport
105 .getFontsizeInPoint(now), now);
106 }
107 return retVal.getIterator();
108 }
109
110
111 public String getAlt() {
112 return this.getMathAttribute(Mglyph.ATTR_ALT);
113 }
114
115
116 public String getFontfamily() {
117 return this.getMathAttribute(Mglyph.ATTR_FONTFAMILY);
118 }
119
120
121 public int getIndex() {
122 int retVal = 0;
123 final String indexStr = this.getMathAttribute(Mglyph.ATTR_INDEX);
124 try {
125 if (indexStr != null) {
126 retVal = Integer.parseInt(indexStr);
127 }
128 } catch (final NumberFormatException e) {
129 retVal = 0;
130 }
131 return retVal;
132 }
133
134
135 public void setAlt(final String alt) {
136 this.setAttribute(Mglyph.ATTR_ALT, alt);
137 }
138
139
140 public void setFontfamily(final String fontfamily) {
141 this.setAttribute(Mglyph.ATTR_FONTFAMILY, fontfamily);
142 }
143
144
145 public void setIndex(final int index) {
146 this.setAttribute(Mglyph.ATTR_INDEX, Integer.toString(index));
147 }
148
149 }