001/* ======================================================================== 002 * JCommon : a free general purpose class library for the Java(tm) platform 003 * ======================================================================== 004 * 005 * (C) Copyright 2000-2013, by Object Refinery Limited and Contributors. 006 * 007 * Project Info: http://www.jfree.org/jcommon/index.html 008 * 009 * This library is free software; you can redistribute it and/or modify it 010 * under the terms of the GNU Lesser General Public License as published by 011 * the Free Software Foundation; either version 2.1 of the License, or 012 * (at your option) any later version. 013 * 014 * This library is distributed in the hope that it will be useful, but 015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 017 * License for more details. 018 * 019 * You should have received a copy of the GNU Lesser General Public 020 * License along with this library; if not, write to the Free Software 021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 022 * USA. 023 * 024 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 025 * in the United States and other countries.] 026 * 027 * ----------------- 028 * TextFragment.java 029 * ----------------- 030 * (C) Copyright 2003-2013, by Object Refinery Limited and Contributors. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): -; 034 * 035 * $Id: TextFragment.java,v 1.13 2007/03/16 10:25:58 mungady Exp $ 036 * 037 * Changes 038 * ------- 039 * 07-Nov-2003 : Version 1 (DG); 040 * 25-Nov-2003 : Fixed bug in the dimension calculation (DG); 041 * 22-Dec-2003 : Added workaround for Java bug 4245442 (DG); 042 * 29-Jan-2004 : Added paint attribute (DG); 043 * 22-Mar-2004 : Added equals() method and implemented Serializable (DG); 044 * 01-Apr-2004 : Changed java.awt.geom.Dimension2D to org.jfree.ui.Size2D 045 * because of JDK bug 4976448 which persists on JDK 1.3.1 (DG); 046 * 30-Sep-2004 : Moved drawRotatedString() from RefineryUtilities 047 * --> TextUtilities (DG); 048 * 16-Mar-2007 : Fixed serialization for GradientPaint (DG); 049 * 01-Sep-2013 : Take into account all TextAnchor offsets (DG); 050 * 051 */ 052 053package org.jfree.text; 054 055import java.awt.Color; 056import java.awt.Font; 057import java.awt.FontMetrics; 058import java.awt.Graphics2D; 059import java.awt.Paint; 060import java.awt.font.LineMetrics; 061import java.awt.geom.Rectangle2D; 062import java.io.IOException; 063import java.io.ObjectInputStream; 064import java.io.ObjectOutputStream; 065import java.io.Serializable; 066 067import org.jfree.io.SerialUtilities; 068import org.jfree.ui.Size2D; 069import org.jfree.ui.TextAnchor; 070import org.jfree.util.Log; 071import org.jfree.util.LogContext; 072 073/** 074 * A text item, with an associated font, that fits on a single line (see 075 * {@link TextLine}). Instances of the class are immutable. 076 */ 077public class TextFragment implements Serializable { 078 079 /** For serialization. */ 080 private static final long serialVersionUID = 4465945952903143262L; 081 082 /** The default font. */ 083 public static final Font DEFAULT_FONT = new Font("Serif", Font.PLAIN, 12); 084 085 /** The default text color. */ 086 public static final Paint DEFAULT_PAINT = Color.black; 087 088 /** The text. */ 089 private String text; 090 091 /** The font. */ 092 private Font font; 093 094 /** The text color. */ 095 private transient Paint paint; 096 097 /** 098 * The baseline offset (can be used to simulate subscripts and 099 * superscripts). 100 */ 101 private float baselineOffset; 102 103 /** Access to logging facilities. */ 104 protected static final LogContext logger = Log.createContext( 105 TextFragment.class); 106 107 /** 108 * Creates a new text fragment. 109 * 110 * @param text the text (<code>null</code> not permitted). 111 */ 112 public TextFragment(final String text) { 113 this(text, DEFAULT_FONT, DEFAULT_PAINT); 114 } 115 116 /** 117 * Creates a new text fragment. 118 * 119 * @param text the text (<code>null</code> not permitted). 120 * @param font the font (<code>null</code> not permitted). 121 */ 122 public TextFragment(final String text, final Font font) { 123 this(text, font, DEFAULT_PAINT); 124 } 125 126 /** 127 * Creates a new text fragment. 128 * 129 * @param text the text (<code>null</code> not permitted). 130 * @param font the font (<code>null</code> not permitted). 131 * @param paint the text color (<code>null</code> not permitted). 132 */ 133 public TextFragment(final String text, final Font font, final Paint paint) { 134 this(text, font, paint, 0.0f); 135 } 136 137 /** 138 * Creates a new text fragment. 139 * 140 * @param text the text (<code>null</code> not permitted). 141 * @param font the font (<code>null</code> not permitted). 142 * @param paint the text color (<code>null</code> not permitted). 143 * @param baselineOffset the baseline offset. 144 */ 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 /** 163 * Returns the text. 164 * 165 * @return The text (possibly <code>null</code>). 166 */ 167 public String getText() { 168 return this.text; 169 } 170 171 /** 172 * Returns the font. 173 * 174 * @return The font (never <code>null</code>). 175 */ 176 public Font getFont() { 177 return this.font; 178 } 179 180 /** 181 * Returns the text paint. 182 * 183 * @return The text paint (never <code>null</code>). 184 */ 185 public Paint getPaint() { 186 return this.paint; 187 } 188 189 /** 190 * Returns the baseline offset. 191 * 192 * @return The baseline offset. 193 */ 194 public float getBaselineOffset() { 195 return this.baselineOffset; 196 } 197 198 /** 199 * Draws the text fragment. 200 * 201 * @param g2 the graphics device. 202 * @param anchorX the x-coordinate of the anchor point. 203 * @param anchorY the y-coordinate of the anchor point. 204 * @param anchor the location of the text that is aligned to the anchor 205 * point. 206 * @param rotateX the x-coordinate of the rotation point. 207 * @param rotateY the y-coordinate of the rotation point. 208 * @param angle the angle. 209 */ 210 public void draw(final Graphics2D g2, final float anchorX, 211 final float anchorY, final TextAnchor anchor, 212 final float rotateX, final float rotateY, 213 final double angle) { 214 215 g2.setFont(this.font); 216 g2.setPaint(this.paint); 217 TextUtilities.drawRotatedString(this.text, g2, anchorX, anchorY 218 + this.baselineOffset, anchor, angle, rotateX, rotateY); 219 220 } 221 222 /** 223 * Calculates the dimensions of the text fragment. 224 * 225 * @param g2 the graphics device. 226 * 227 * @return The width and height of the text. 228 */ 229 public Size2D calculateDimensions(final Graphics2D g2) { 230 final FontMetrics fm = g2.getFontMetrics(this.font); 231 final Rectangle2D bounds = TextUtilities.getTextBounds(this.text, g2, 232 fm); 233 final Size2D result = new Size2D(bounds.getWidth(), bounds.getHeight()); 234 return result; 235 } 236 237 /** 238 * Calculates the vertical offset between the baseline and the specified 239 * text anchor. 240 * 241 * @param g2 the graphics device. 242 * @param anchor the anchor. 243 * 244 * @return the offset. 245 */ 246 public float calculateBaselineOffset(Graphics2D g2, TextAnchor anchor) { 247 float result = 0.0f; 248 final FontMetrics fm = g2.getFontMetrics(this.font); 249 final LineMetrics lm = fm.getLineMetrics("ABCxyz", g2); 250 if (anchor.isTop()) { 251 result = lm.getAscent(); 252 } 253 else if (anchor.isHalfAscent()) { 254 result = lm.getAscent() / 2.0f; 255 } 256 else if (anchor.isVerticalCenter()) { 257 result = lm.getAscent() / 2.0f - lm.getDescent() / 2.0f; 258 } 259 else if (anchor.isBottom()) { 260 result = -lm.getDescent() - lm.getLeading(); 261 } 262 return result; 263 } 264 265 /** 266 * Tests this instance for equality with an arbitrary object. 267 * 268 * @param obj the object to test against (<code>null</code> permitted). 269 * 270 * @return A boolean. 271 */ 272 public boolean equals(final Object obj) { 273 if (obj == null) { 274 return false; 275 } 276 if (obj == this) { 277 return true; 278 } 279 if (obj instanceof TextFragment) { 280 final TextFragment tf = (TextFragment) obj; 281 if (!this.text.equals(tf.text)) { 282 return false; 283 } 284 if (!this.font.equals(tf.font)) { 285 return false; 286 } 287 if (!this.paint.equals(tf.paint)) { 288 return false; 289 } 290 return true; 291 } 292 return false; 293 } 294 295 /** 296 * Returns a hash code for this object. 297 * 298 * @return A hash code. 299 */ 300 public int hashCode() { 301 int result; 302 result = (this.text != null ? this.text.hashCode() : 0); 303 result = 29 * result + (this.font != null ? this.font.hashCode() : 0); 304 result = 29 * result + (this.paint != null ? this.paint.hashCode() : 0); 305 return result; 306 } 307 308 /** 309 * Provides serialization support. 310 * 311 * @param stream the output stream. 312 * 313 * @throws IOException if there is an I/O error. 314 */ 315 private void writeObject(final ObjectOutputStream stream) 316 throws IOException { 317 stream.defaultWriteObject(); 318 SerialUtilities.writePaint(this.paint, stream); 319 } 320 321 /** 322 * Provides serialization support. 323 * 324 * @param stream the input stream. 325 * 326 * @throws IOException if there is an I/O error. 327 * @throws ClassNotFoundException if there is a classpath problem. 328 */ 329 private void readObject(final ObjectInputStream stream) 330 throws IOException, ClassNotFoundException { 331 stream.defaultReadObject(); 332 this.paint = SerialUtilities.readPaint(stream); 333 } 334 335}