1 50 51 package org.jfree.text; 52 53 import java.awt.Color ; 54 import java.awt.Font ; 55 import java.awt.FontMetrics ; 56 import java.awt.Graphics2D ; 57 import java.awt.Paint ; 58 import java.awt.font.LineMetrics ; 59 import java.awt.geom.Rectangle2D ; 60 import java.io.IOException ; 61 import java.io.ObjectInputStream ; 62 import java.io.ObjectOutputStream ; 63 import java.io.Serializable ; 64 65 import org.jfree.io.SerialUtilities; 66 import org.jfree.ui.Size2D; 67 import org.jfree.ui.TextAnchor; 68 import org.jfree.util.Log; 69 import org.jfree.util.LogContext; 70 71 77 public class TextFragment implements Serializable { 78 79 80 private static final long serialVersionUID = 4465945952903143262L; 81 82 83 public static final Font DEFAULT_FONT = new Font ("Serif", Font.PLAIN, 12); 84 85 86 public static final Paint DEFAULT_PAINT = Color.black; 87 88 89 private String text; 90 91 92 private Font font; 93 94 95 private Paint paint; 96 97 101 private float baselineOffset; 102 103 104 protected static final LogContext logger 105 = Log.createContext(TextFragment.class); 106 107 112 public TextFragment(final String text) { 113 this(text, DEFAULT_FONT, DEFAULT_PAINT); 114 } 115 116 122 public TextFragment(final String text, final Font font) { 123 this(text, font, DEFAULT_PAINT); 124 } 125 126 133 public TextFragment(final String text, final Font font, final Paint paint) { 134 this(text, font, paint, 0.0f); 135 } 136 137 145 public TextFragment(final String text, final Font font, final Paint paint, 146 final float baselineOffset) { 147 if (text == null) { 148 throw new IllegalArgumentException ("Null 'text' argument."); 149 } 150 if (font == null) { 151 throw new IllegalArgumentException ("Null 'font' argument."); 152 } 153 if (paint == null) { 154 throw new IllegalArgumentException ("Null 'paint' argument."); 155 } 156 this.text = text; 157 this.font = font; 158 this.paint = paint; 159 this.baselineOffset = baselineOffset; 160 } 161 162 167 public String getText() { 168 return this.text; 169 } 170 171 176 public Font getFont() { 177 return this.font; 178 } 179 180 185 public Paint getPaint() { 186 return this.paint; 187 } 188 189 public float getBaselineOffset() { 190 return this.baselineOffset; 191 } 192 193 205 public void draw(final Graphics2D g2, final float anchorX, 206 final float anchorY, final TextAnchor anchor, 207 final float rotateX, final float rotateY, 208 final double angle) { 209 210 g2.setFont(this.font); 211 g2.setPaint(this.paint); 212 TextUtilities.drawRotatedString( 213 this.text, g2, anchorX, anchorY + this.baselineOffset, anchor, 214 angle, rotateX, rotateY 215 ); 216 217 } 218 219 226 public Size2D calculateDimensions(final Graphics2D g2) { 227 final FontMetrics fm = g2.getFontMetrics(this.font); 228 final Rectangle2D bounds = TextUtilities.getTextBounds(this.text, g2, 229 fm); 230 final Size2D result = new Size2D(bounds.getWidth(), bounds.getHeight()); 231 return result; 232 } 233 234 243 public float calculateBaselineOffset(final Graphics2D g2, 244 final TextAnchor anchor) { 245 float result = 0.0f; 246 final FontMetrics fm = g2.getFontMetrics(this.font); 247 final LineMetrics lm = fm.getLineMetrics("ABCxyz", g2); 248 if (anchor == TextAnchor.TOP_LEFT || anchor == TextAnchor.TOP_CENTER 249 || anchor == TextAnchor.TOP_RIGHT) { 250 result = lm.getAscent(); 251 } 252 else if (anchor == TextAnchor.BOTTOM_LEFT 253 || anchor == TextAnchor.BOTTOM_CENTER 254 || anchor == TextAnchor.BOTTOM_RIGHT) { 255 result = -lm.getDescent() - lm.getLeading(); 256 } 257 return result; 258 } 259 260 267 public boolean equals(final Object obj) { 268 if (obj == null) { 269 return false; 270 } 271 if (obj == this) { 272 return true; 273 } 274 if (obj instanceof TextFragment) { 275 final TextFragment tf = (TextFragment) obj; 276 if (!this.text.equals(tf.text)) { 277 return false; 278 } 279 if (!this.font.equals(tf.font)) { 280 return false; 281 } 282 if (!this.paint.equals(tf.paint)) { 283 return false; 284 } 285 return true; 286 } 287 return false; 288 } 289 290 295 public int hashCode() { 296 int result; 297 result = (this.text != null ? this.text.hashCode() : 0); 298 result = 29 * result + (this.font != null ? this.font.hashCode() : 0); 299 result = 29 * result + (this.paint != null ? this.paint.hashCode() : 0); 300 return result; 301 } 302 303 310 private void writeObject(final ObjectOutputStream stream) 311 throws IOException { 312 stream.defaultWriteObject(); 313 SerialUtilities.writePaint(this.paint, stream); 314 } 315 316 324 private void readObject(final ObjectInputStream stream) 325 throws IOException , ClassNotFoundException { 326 stream.defaultReadObject(); 327 this.paint = SerialUtilities.readPaint(stream); 328 } 329 330 } 331 | Popular Tags |