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,v 923658a2b249 2009/09/11 09:57:31 max $ */
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   * Implements the mglyph element.
38   * 
39   * <p>
40   * TODO: FontFamliy gives a "deprecated attribute" warning due to the current
41   * design.
42   * <p>
43   * TODO: other attributes (such as italic, bold, etc.) may be inherited from the
44   * context.
45   * 
46   * @version $Revision: 923658a2b249 $
47   */
48  public final class Mglyph extends AbstractTokenWithTextLayout implements
49          MathMLGlyphElement, TextContentModifier {
50  
51      /**
52       * The XML element from this class.
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       * Default constructor. Sets MathML Namespace.
66       * 
67       * @param qname
68       *            Qualified name.
69       * @param odoc
70       *            Owner Document.
71       */
72      public Mglyph(final String qname, final AbstractDocument odoc) {
73          super(qname, odoc);
74      }
75  
76      /** {@inheritDoc} */
77      @Override
78      protected Node newNode() {
79          return new Mglyph(this.nodeName, this.ownerDocument);
80      }
81  
82      /** {@inheritDoc} */
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     /** {@inheritDoc} */
111     public String getAlt() {
112         return this.getMathAttribute(Mglyph.ATTR_ALT);
113     }
114 
115     /** {@inheritDoc} */
116     public String getFontfamily() {
117         return this.getMathAttribute(Mglyph.ATTR_FONTFAMILY);
118     }
119 
120     /** {@inheritDoc} */
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     /** {@inheritDoc} */
135     public void setAlt(final String alt) {
136         this.setAttribute(Mglyph.ATTR_ALT, alt);
137     }
138 
139     /** {@inheritDoc} */
140     public void setFontfamily(final String fontfamily) {
141         this.setAttribute(Mglyph.ATTR_FONTFAMILY, fontfamily);
142     }
143 
144     /** {@inheritDoc} */
145     public void setIndex(final int index) {
146         this.setAttribute(Mglyph.ATTR_INDEX, Integer.toString(index));
147     }
148 
149 }