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