1 49 50 package org.jfree.text; 51 52 import java.awt.Font ; 53 import java.awt.Graphics2D ; 54 import java.awt.Paint ; 55 import java.io.Serializable ; 56 import java.util.Iterator ; 57 import java.util.List ; 58 59 import org.jfree.ui.Size2D; 60 import org.jfree.ui.TextAnchor; 61 import org.jfree.util.Log; 62 import org.jfree.util.LogContext; 63 64 70 public class TextLine implements Serializable { 71 72 73 private static final long serialVersionUID = 7100085690160465444L; 74 75 76 private List fragments; 77 78 79 protected static final LogContext logger 80 = Log.createContext(TextLine.class); 81 82 85 public TextLine() { 86 this.fragments = new java.util.ArrayList (); 87 } 88 89 94 public TextLine(final String text) { 95 this(text, TextFragment.DEFAULT_FONT); 96 } 97 98 104 public TextLine(final String text, final Font font) { 105 this.fragments = new java.util.ArrayList (); 106 final TextFragment fragment = new TextFragment(text, font); 107 this.fragments.add(fragment); 108 } 109 110 117 public TextLine(final String text, final Font font, final Paint paint) { 118 if (text == null) { 119 throw new IllegalArgumentException ("Null 'text' argument."); 120 } 121 if (font == null) { 122 throw new IllegalArgumentException ("Null 'font' argument."); 123 } 124 if (paint == null) { 125 throw new IllegalArgumentException ("Null 'paint' argument."); 126 } 127 this.fragments = new java.util.ArrayList (); 128 final TextFragment fragment = new TextFragment(text, font, paint); 129 this.fragments.add(fragment); 130 } 131 132 137 public void addFragment(final TextFragment fragment) { 138 this.fragments.add(fragment); 139 } 140 141 146 public void removeFragment(final TextFragment fragment) { 147 this.fragments.remove(fragment); 148 } 149 150 162 public void draw(final Graphics2D g2, 163 final float anchorX, final float anchorY, 164 final TextAnchor anchor, 165 final float rotateX, final float rotateY, 166 final double angle) { 167 168 float x = anchorX; 169 final float yOffset = calculateBaselineOffset(g2, anchor); 170 final Iterator iterator = this.fragments.iterator(); 171 while (iterator.hasNext()) { 172 final TextFragment fragment = (TextFragment) iterator.next(); 173 final Size2D d = fragment.calculateDimensions(g2); 174 fragment.draw( 175 g2, x, anchorY + yOffset, TextAnchor.BASELINE_LEFT, 176 rotateX, rotateY, angle 177 ); 178 x = x + (float) d.getWidth(); 179 } 180 181 } 182 183 190 public Size2D calculateDimensions(final Graphics2D g2) { 191 double width = 0.0; 192 double height = 0.0; 193 final Iterator iterator = this.fragments.iterator(); 194 while (iterator.hasNext()) { 195 final TextFragment fragment = (TextFragment) iterator.next(); 196 final Size2D dimension = fragment.calculateDimensions(g2); 197 width = width + dimension.getWidth(); 198 height = Math.max(height, dimension.getHeight()); 199 if (logger.isDebugEnabled()) { 200 logger.debug("width = " + width + ", height = " + height); 201 } 202 } 203 return new Size2D(width, height); 204 } 205 206 211 public TextFragment getFirstTextFragment() { 212 TextFragment result = null; 213 if (this.fragments.size() > 0) { 214 result = (TextFragment) this.fragments.get(0); 215 } 216 return result; 217 } 218 219 224 public TextFragment getLastTextFragment() { 225 TextFragment result = null; 226 if (this.fragments.size() > 0) { 227 result = (TextFragment) this.fragments.get(this.fragments.size() 228 - 1); 229 } 230 return result; 231 } 232 233 242 private float calculateBaselineOffset(final Graphics2D g2, 243 final TextAnchor anchor) { 244 float result = 0.0f; 245 Iterator iterator = this.fragments.iterator(); 246 while (iterator.hasNext()) { 247 TextFragment fragment = (TextFragment) iterator.next(); 248 result = Math.max(result, 249 fragment.calculateBaselineOffset(g2, anchor)); 250 } 251 return result; 252 } 253 254 261 public boolean equals(final Object obj) { 262 if (obj == null) { 263 return false; 264 } 265 if (obj == this) { 266 return true; 267 } 268 if (obj instanceof TextLine) { 269 final TextLine line = (TextLine) obj; 270 return this.fragments.equals(line.fragments); 271 } 272 return false; 273 } 274 275 280 public int hashCode() { 281 return (this.fragments != null ? this.fragments.hashCode() : 0); 282 } 283 284 } 285 | Popular Tags |