VerticalTextIcon.java |
1 package gate.swing; 2 3 import java.awt.*; 4 import java.beans.PropertyChangeEvent; 5 import java.beans.PropertyChangeListener; 6 7 import javax.swing.Icon; 8 9 /** 10 * VTextIcon is an Icon implementation which draws a short string vertically. 11 * It's useful for JTabbedPanes with LEFT or RIGHT tabs but can be used in any 12 * component which supports Icons, such as JLabel or JButton 13 * You can provide a hint to indicate whether to rotate the string 14 * to the left or right, or not at all, and it checks to make sure 15 * that the rotation is legal for the given string 16 * (for example, Chinese/Japanese/Korean scripts have special rules when 17 * drawn vertically and should never be rotated) 18 * <hr> 19 * NOTE: This class was taken from <br> 20 * <a href="http://www.macdevcenter.com/pub/a/mac/2002/03/22/vertical_text.html"> 21 * this location</a>. 22 * <br> 23 * The author specifies:<br> 24 * "Anybody can use the code for any purpose; I don't want any 25 * compensation, nor do I accept any liability." 26 *<hr> 27 * @author Lee Ann Rucker <LRucker@mac.com> 28 * 29 */ 30 public class VerticalTextIcon implements Icon, PropertyChangeListener { 31 String fLabel; 32 String[] fCharStrings; // for efficiency, break the fLabel into one-char strings to be passed to drawString 33 int[] fCharWidths; // Roman characters should be centered when not rotated (Japanese fonts are monospaced) 34 int[] fPosition; // Japanese half-height characters need to be shifted when drawn vertically 35 int fWidth, fHeight, fCharHeight, fDescent; // Cached for speed 36 int fRotation; 37 Component fComponent; 38 39 static final int POSITION_NORMAL = 0; 40 static final int POSITION_TOP_RIGHT = 1; 41 static final int POSITION_FAR_TOP_RIGHT = 2; 42 43 public static final int ROTATE_DEFAULT = 0x00; 44 public static final int ROTATE_NONE = 0x01; 45 public static final int ROTATE_LEFT = 0x02; 46 public static final int ROTATE_RIGHT = 0x04; 47 48 /** 49 * Creates a <code>VTextIcon</code> for the specified <code>component</code> 50 * with the specified <code>label</code>. 51 * It sets the orientation to the default for the string 52 * @see #verifyRotation 53 */ 54 public VerticalTextIcon(Component component, String label) { 55 this(component, label, ROTATE_DEFAULT); 56 } 57 58 /** 59 * Creates a <code>VTextIcon</code> for the specified <code>component</code> 60 * with the specified <code>label</code>. 61 * It sets the orientation to the provided value if it's legal for the string 62 * @see #verifyRotation 63 */ 64 public VerticalTextIcon(Component component, String label, int rotateHint) { 65 fComponent = component; 66 fLabel = label; 67 fRotation = verifyRotation(label, rotateHint); 68 calcDimensions(); 69 fComponent.addPropertyChangeListener(this); 70 } 71 72 /** 73 * sets the label to the given string, updating the orientation as needed 74 * and invalidating the layout if the size changes 75 * @see #verifyRotation 76 */ 77 public void setLabel(String label) { 78 fLabel = label; 79 fRotation = verifyRotation(label, fRotation); // Make sure the current rotation is still legal 80 recalcDimensions(); 81 } 82 83 /** 84 * Checks for changes to the font on the fComponent 85 * so that it can invalidate the layout if the size changes 86 */ 87 public void propertyChange(PropertyChangeEvent e) { 88 String prop = e.getPropertyName(); 89 if("font".equals(prop)) { 90 recalcDimensions(); 91 } 92 } 93 94 /** 95 * Calculates the dimensions. If they've changed, 96 * invalidates the component 97 */ 98 void recalcDimensions() { 99 int wOld = getIconWidth(); 100 int hOld = getIconHeight(); 101 calcDimensions(); 102 if (wOld != getIconWidth() || hOld != getIconHeight()) 103 fComponent.invalidate(); 104 } 105 106 void calcDimensions() { 107 FontMetrics fm = fComponent.getFontMetrics(fComponent.getFont()); 108 fCharHeight = fm.getAscent() + fm.getDescent(); 109 fDescent = fm.getDescent(); 110 if (fRotation == ROTATE_NONE) { 111 int len = fLabel.length(); 112 char data[] = new char[len]; 113 fLabel.getChars(0, len, data, 0); 114 // if not rotated, width is that of the widest char in the string 115 fWidth = 0; 116 // we need an array of one-char strings for drawString 117 fCharStrings = new String[len]; 118 fCharWidths = new int[len]; 119 fPosition = new int[len]; 120 char ch; 121 for (int i = 0; i < len; i++) { 122 ch = data[i]; 123 fCharWidths[i] = fm.charWidth(ch); 124 if (fCharWidths[i] > fWidth) 125 fWidth = fCharWidths[i]; 126 fCharStrings[i] = new String(data, i, 1); 127 // small kana and punctuation 128 if (sDrawsInTopRight.indexOf(ch) >= 0) // if ch is in sDrawsInTopRight 129 fPosition[i] = POSITION_TOP_RIGHT; 130 else if (sDrawsInFarTopRight.indexOf(ch) >= 0) 131 fPosition[i] = POSITION_FAR_TOP_RIGHT; 132 else 133 fPosition[i] = POSITION_NORMAL; 134 } 135 // and height is the font height * the char count, + one extra leading at the bottom 136 fHeight = fCharHeight * len + fDescent; 137 } 138 else { 139 // if rotated, width is the height of the string 140 fWidth = fCharHeight; 141 // and height is the width, plus some buffer space 142 fHeight = fm.stringWidth(fLabel) + 2*kBufferSpace; 143 } 144 } 145 146 /** 147 * Draw the icon at the specified location. Icon implementations 148 * may use the Component argument to get properties useful for 149 * painting, e.g. the foreground or background color. 150 */ 151 public void paintIcon(Component c, Graphics g, int x, int y) { 152 // We don't insist that it be on the same Component 153 g.setColor(c.getForeground()); 154 g.setFont(c.getFont()); 155 if (fRotation == ROTATE_NONE) { 156 int yPos = y + fCharHeight; 157 for (int i = 0; i < fCharStrings.length; i++) { 158 // Special rules for Japanese - "half-height" characters (like ya, yu, yo in combinations) 159 // should draw in the top-right quadrant when drawn vertically 160 // - they draw in the bottom-left normally 161 int tweak; 162 switch (fPosition[i]) { 163 case POSITION_NORMAL: 164 // Roman fonts should be centered. Japanese fonts are always monospaced. 165 g.drawString(fCharStrings[i], x+((fWidth-fCharWidths[i])/2), yPos); 166 break; 167 case POSITION_TOP_RIGHT: 168 tweak = fCharHeight/3; // Should be 2, but they aren't actually half-height 169 g.drawString(fCharStrings[i], x+(tweak/2), yPos-tweak); 170 break; 171 case POSITION_FAR_TOP_RIGHT: 172 tweak = fCharHeight - fCharHeight/3; 173 g.drawString(fCharStrings[i], x+(tweak/2), yPos-tweak); 174 break; 175 } 176 yPos += fCharHeight; 177 } 178 } 179 else if (fRotation == ROTATE_LEFT) { 180 g.translate(x+fWidth,y+fHeight); 181 ((Graphics2D)g).rotate(-NINETY_DEGREES); 182 g.drawString(fLabel, kBufferSpace, -fDescent); 183 ((Graphics2D)g).rotate(NINETY_DEGREES); 184 g.translate(-(x+fWidth),-(y+fHeight)); 185 } 186 else if (fRotation == ROTATE_RIGHT) { 187 g.translate(x,y); 188 ((Graphics2D)g).rotate(NINETY_DEGREES); 189 g.drawString(fLabel, kBufferSpace, -fDescent); 190 ((Graphics2D)g).rotate(-NINETY_DEGREES); 191 g.translate(-x,-y); 192 } 193 194 } 195 196 /** 197 * Returns the icon's width. 198 * 199 * @return an int specifying the fixed width of the icon. 200 */ 201 public int getIconWidth() { 202 return fWidth; 203 } 204 205 /** 206 * Returns the icon's height. 207 * 208 * @return an int specifying the fixed height of the icon. 209 */ 210 public int getIconHeight() { 211 return fHeight; 212 } 213 214 /** 215 verifyRotation 216 217 returns the best rotation for the string (ROTATE_NONE, ROTATE_LEFT, ROTATE_RIGHT) 218 219 This is public static so you can use it to test a string without creating a VTextIcon 220 221 from http://www.unicode.org/unicode/reports/tr9/tr9-3.html 222 When setting text using the Arabic script in vertical lines, 223 it is more common to employ a horizontal baseline that 224 is rotated by 90� counterclockwise so that the characters 225 are ordered from top to bottom. Latin text and numbers 226 may be rotated 90� clockwise so that the characters 227 are also ordered from top to bottom. 228 229 Rotation rules 230 - Roman can rotate left, right, or none - default right (counterclockwise) 231 - CJK can't rotate 232 - Arabic must rotate - default left (clockwise) 233 234 from the online edition of _The Unicode Standard, Version 3.0_, file ch10.pdf page 4 235 Ideographs are found in three blocks of the Unicode Standard... 236 U+4E00-U+9FFF, U+3400-U+4DFF, U+F900-U+FAFF 237 238 Hiragana is U+3040-U+309F, katakana is U+30A0-U+30FF 239 240 from http://www.unicode.org/unicode/faq/writingdirections.html 241 East Asian scripts are frequently written in vertical lines 242 which run from top-to-bottom and are arrange columns either 243 from left-to-right (Mongolian) or right-to-left (other scripts). 244 Most characters use the same shape and orientation when displayed 245 horizontally or vertically, but many punctuation characters 246 will change their shape when displayed vertically. 247 248 Letters and words from other scripts are generally rotated through 249 ninety degree angles so that they, too, will read from top to bottom. 250 That is, letters from left-to-right scripts will be rotated clockwise 251 and letters from right-to-left scripts counterclockwise, both 252 through ninety degree angles. 253 254 Unlike the bidirectional case, the choice of vertical layout 255 is usually treated as a formatting style; therefore, 256 the Unicode Standard does not define default rendering behavior 257 for vertical text nor provide directionality controls designed to override such behavior 258 259 */ 260 public static int verifyRotation(String label, int rotateHint) { 261 boolean hasCJK = false; 262 boolean hasMustRotate = false; // Arabic, etc 263 264 int len = label.length(); 265 char data[] = new char[len]; 266 char ch; 267 label.getChars(0, len, data, 0); 268 for (int i = 0; i < len; i++) { 269 ch = data[i]; 270 if ((ch >= '\u4E00' && ch <= '\u9FFF') || 271 (ch >= '\u3400' && ch <= '\u4DFF') || 272 (ch >= '\uF900' && ch <= '\uFAFF') || 273 (ch >= '\u3040' && ch <= '\u309F') || 274 (ch >= '\u30A0' && ch <= '\u30FF') ) 275 hasCJK = true; 276 if ((ch >= '\u0590' && ch <= '\u05FF') || // Hebrew 277 (ch >= '\u0600' && ch <= '\u06FF') || // Arabic 278 (ch >= '\u0700' && ch <= '\u074F') ) // Syriac 279 hasMustRotate = true; 280 } 281 // If you mix Arabic with Chinese, you're on your own 282 if (hasCJK) 283 return DEFAULT_CJK; 284 285 int legal = hasMustRotate ? LEGAL_MUST_ROTATE : LEGAL_ROMAN; 286 if ((rotateHint & legal) > 0) 287 return rotateHint; 288 289 // The hint wasn't legal, or it was zero 290 return hasMustRotate ? DEFAULT_MUST_ROTATE : DEFAULT_ROMAN; 291 } 292 293 // The small kana characters and Japanese punctuation that draw in the top right quadrant: 294 // small a, i, u, e, o, tsu, ya, yu, yo, wa (katakana only) ka ke 295 static final String sDrawsInTopRight = 296 "\u3041\u3043\u3045\u3047\u3049\u3063\u3083\u3085\u3087\u308E" + // hiragana 297 "\u30A1\u30A3\u30A5\u30A7\u30A9\u30C3\u30E3\u30E5\u30E7\u30EE\u30F5\u30F6"; // katakana 298 static final String sDrawsInFarTopRight = "\u3001\u3002"; // comma, full stop 299 300 static final int DEFAULT_CJK = ROTATE_NONE; 301 static final int LEGAL_ROMAN = ROTATE_NONE | ROTATE_LEFT | ROTATE_RIGHT; 302 static final int DEFAULT_ROMAN = ROTATE_RIGHT; 303 static final int LEGAL_MUST_ROTATE = ROTATE_LEFT | ROTATE_RIGHT; 304 static final int DEFAULT_MUST_ROTATE = ROTATE_LEFT; 305 306 static final double NINETY_DEGREES = Math.toRadians(90.0); 307 static final int kBufferSpace = 5; 308 }