View Javadoc

1   /*
2    * Copyright 2007 - 2007 JEuclid, http://jeuclid.sf.net
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  /* $Id: Mglyph.java 718 2008-04-28 18:46:38Z maxberger $ */
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   * Implements the mglyph element.
35   * 
36   * @todo FontFamliy gives a "deprecated attribute" warning due to the current
37   *       design.
38   * @todo other attributes (such as italic, bold, etc.) may be inherited from
39   *       the context.
40   * @version $Revision: 718 $
41   */
42  public final class Mglyph extends AbstractTokenWithTextLayout implements
43          MathMLGlyphElement {
44  
45      /**
46       * The XML element from this class.
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       * Default constructor.
60       */
61      public Mglyph() {
62          super();
63      }
64  
65      /** {@inheritDoc} */
66      @Override
67      protected Node newNode() {
68          return new Mglyph();
69      }
70  
71      /** {@inheritDoc} */
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      /** {@inheritDoc} */
94      @Override
95      protected boolean isEmpty() {
96          return false;
97      }
98  
99      /** {@inheritDoc} */
100     public String getAlt() {
101         return this.getMathAttribute(Mglyph.ATTR_ALT);
102     }
103 
104     /** {@inheritDoc} */
105     public String getFontfamily() {
106         return this.getMathAttribute(Mglyph.ATTR_FONTFAMILY);
107     }
108 
109     /** {@inheritDoc} */
110     public int getIndex() {
111         return Integer.parseInt(this.getMathAttribute(Mglyph.ATTR_INDEX));
112     }
113 
114     /** {@inheritDoc} */
115     public void setAlt(final String alt) {
116         this.setAttribute(Mglyph.ATTR_ALT, alt);
117     }
118 
119     /** {@inheritDoc} */
120     public void setFontfamily(final String fontfamily) {
121         this.setAttribute(Mglyph.ATTR_FONTFAMILY, fontfamily);
122     }
123 
124     /** {@inheritDoc} */
125     public void setIndex(final int index) {
126         this.setAttribute(Mglyph.ATTR_INDEX, Integer.toString(index));
127     }
128 
129 }