Source code: jaxe/equations/element/MathSubSup.java
1 /*
2 Jaxe - Editeur XML en Java
3
4 Copyright (C) 2003 Observatoire de Paris-Meudon
5
6 Ce programme est un logiciel libre ; vous pouvez le redistribuer et/ou le modifier conformément aux dispositions de la Licence Publique Générale GNU, telle que publiée par la Free Software Foundation ; version 2 de la licence, ou encore (à votre choix) toute version ultérieure.
7
8 Ce programme est distribué dans l'espoir qu'il sera utile, mais SANS AUCUNE GARANTIE ; sans même la garantie implicite de COMMERCIALISATION ou D'ADAPTATION A UN OBJET PARTICULIER. Pour plus de détail, voir la Licence Publique Générale GNU .
9
10 Vous devez avoir reçu un exemplaire de la Licence Publique Générale GNU en même temps que ce programme ; si ce n'est pas le cas, écrivez à la Free Software Foundation Inc., 675 Mass Ave, Cambridge, MA 02139, Etats-Unis.
11 */
12
13 package jaxe.equations.element;
14
15 import java.awt.*;
16 import java.util.Vector;
17 import java.util.Enumeration;
18
19 /**
20 * This class arange a element lower, and a other
21 * elements upper to an element
22 *
23 * @author <a href="mailto:stephan@vern.chem.tu-berlin.de">Stephan Michels</a>
24 * @author <a href="mailto:sielaff@vern.chem.tu-berlin.de">Marco Sielaff</a>
25 * @version %I%, %G%
26 */
27 public class MathSubSup extends MathElement
28 {
29
30 /** The XML element from this class */
31 public final static String ELEMENT = "msubsup";
32
33 /**
34 * Add a math element as a child
35 *
36 * @param child Math element
37 */
38 public void addMathElement(MathElement child)
39 {
40 super.addMathElement(child);
41 if (child != null)
42 {
43 if ((getMathElementCount() == 2) || (getMathElementCount() == 3))
44 child.setFontSize(getFontSize() - 2);
45 else
46 child.setFontSize(getFontSize());
47 }
48 }
49
50 /**
51 * Sets the font size for this component
52 *
53 * @param fontsize Font size
54 */
55 public void setFontSize(int fontsize)
56 {
57 super.setFontSize(fontsize);
58 if (getMathElement(1)!=null)
59 getMathElement(1).setFontSize(getFontSize()-2);
60 if (getMathElement(2)!=null)
61 getMathElement(2).setFontSize(getFontSize()-2);
62 }
63
64 /**
65 * Paints this element
66 *
67 * @param g The graphics context to use for painting
68 * @param posX The first left position for painting
69 * @param posY The position of the baseline
70 */
71 public void paint(Graphics g, int posX, int posY)
72 {
73 MathElement e1 = getMathElement(0);
74 MathElement e2 = getMathElement(1);
75 MathElement e3 = getMathElement(2);
76
77 // int middleshift = getMiddleShift();
78 int childmiddleshift = e2.getMiddleShift();
79 // int posY1 = Math.max(posY+e2.getAscentHeight(true)-middleshift,
80 // posY+e1.getDescentHeight(true)+middleshiftchild);
81 // int posY2 = Math.min(posY-(e3.getAscentHeight(true)+middleshift),
82 // posY-e1.getAscentHeight(true)+middleshiftchild);
83 int posY1 = posY + e1.getDescentHeight(true) + childmiddleshift / 2;
84 int posY2 = posY - e1.getAscentHeight(true) + childmiddleshift;
85
86 e1.paint(g, posX, posY);
87 e2.paint(g, posX + e1.getWidth(true), posY1);
88 e3.paint(g, posX + e1.getWidth(true), posY2);
89 }
90
91 /**
92 * Return the current width of this element
93 *
94 * @param dynamicParts Should be true, if the calculation consider the elements,
95 * which has not fixed sizes
96 *
97 * @return Width of this element
98 */
99 public int getWidth(boolean dynamicParts)
100 {
101 return getMathElement(0).getWidth(dynamicParts)
102 + Math.max(getMathElement(1).getWidth(dynamicParts),
103 getMathElement(2).getWidth(dynamicParts));
104 }
105
106 /**
107 * Return the current height of this element
108 *
109 * @param dynamicParts Should be true, if the calculation consider the elements,
110 * which has not fixed sizes
111 *
112 * @return Height of this element
113 */
114 public int getHeight(boolean dynamicParts)
115 {
116 return getAscentHeight(true) + getDescentHeight(true);
117 }
118
119 /**
120 * Return the current height of the upper part
121 * of this component from the baseline
122 *
123 * @param dynamicParts Should be true, if the calculation consider the elements,
124 * which has not fixed sizes
125 *
126 * @return Height of the upper part
127 */
128 public int getAscentHeight(boolean dynamicParts)
129 {
130 return Math.max(getMathElement(0).getAscentHeight(true),
131 getMathElement(2).getHeight(true) + getMiddleShift());
132 }
133
134 /**
135 * Return the current height of the lower part
136 * of this component from the baseline
137 *
138 * @param dynamicParts Should be true, if the calculation consider the elements,
139 * which has not fixed sizes
140 *
141 * @return Height of the lower part
142 */
143 public int getDescentHeight(boolean dynamicParts)
144 {
145 return Math.max(getMathElement(0).getDescentHeight(true),
146 getMathElement(1).getHeight(true) - getMiddleShift());
147 }
148 }