001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-present, by David Gilbert and Contributors. 006 * 007 * Project Info: http://www.jfree.org/jfreechart/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 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 025 * Other names may be trademarks of their respective owners.] 026 * 027 */ 028 029package org.jfree.chart.text; 030 031import java.awt.Font; 032import java.awt.Graphics2D; 033import java.awt.Paint; 034import java.io.Serializable; 035import java.util.Iterator; 036import java.util.List; 037import org.jfree.chart.ui.Size2D; 038import org.jfree.chart.ui.TextAnchor; 039 040/** 041 * A sequence of {@link TextFragment} objects that together form a line of 042 * text. A sequence of text lines is managed by the {@link TextBlock} class. 043 */ 044public class TextLine implements Serializable { 045 046 /** For serialization. */ 047 private static final long serialVersionUID = 7100085690160465444L; 048 049 /** Storage for the text fragments that make up the line. */ 050 private List fragments; 051 052 /** 053 * Creates a new empty line. 054 */ 055 public TextLine() { 056 this.fragments = new java.util.ArrayList(); 057 } 058 059 /** 060 * Creates a new text line using the default font. 061 * 062 * @param text the text ({@code null} not permitted). 063 */ 064 public TextLine(String text) { 065 this(text, TextFragment.DEFAULT_FONT); 066 } 067 068 /** 069 * Creates a new text line. 070 * 071 * @param text the text ({@code null} not permitted). 072 * @param font the text font ({@code null} not permitted). 073 */ 074 public TextLine(String text, Font font) { 075 this.fragments = new java.util.ArrayList(); 076 final TextFragment fragment = new TextFragment(text, font); 077 this.fragments.add(fragment); 078 } 079 080 /** 081 * Creates a new text line. 082 * 083 * @param text the text ({@code null} not permitted). 084 * @param font the text font ({@code null} not permitted). 085 * @param paint the text color ({@code null} not permitted). 086 */ 087 public TextLine(String text, Font font, Paint paint) { 088 if (text == null) { 089 throw new IllegalArgumentException("Null 'text' argument."); 090 } 091 if (font == null) { 092 throw new IllegalArgumentException("Null 'font' argument."); 093 } 094 if (paint == null) { 095 throw new IllegalArgumentException("Null 'paint' argument."); 096 } 097 this.fragments = new java.util.ArrayList(); 098 final TextFragment fragment = new TextFragment(text, font, paint); 099 this.fragments.add(fragment); 100 } 101 102 /** 103 * Adds a text fragment to the text line. 104 * 105 * @param fragment the text fragment ({@code null} not permitted). 106 */ 107 public void addFragment(TextFragment fragment) { 108 this.fragments.add(fragment); 109 } 110 111 /** 112 * Removes a fragment from the line. 113 * 114 * @param fragment the fragment to remove. 115 */ 116 public void removeFragment(TextFragment fragment) { 117 this.fragments.remove(fragment); 118 } 119 120 /** 121 * Draws the text line. 122 * 123 * @param g2 the graphics device. 124 * @param anchorX the x-coordinate for the anchor point. 125 * @param anchorY the y-coordinate for the anchor point. 126 * @param anchor the point on the text line that is aligned to the anchor 127 * point. 128 * @param rotateX the x-coordinate for the rotation point. 129 * @param rotateY the y-coordinate for the rotation point. 130 * @param angle the rotation angle (in radians). 131 */ 132 public void draw(Graphics2D g2, float anchorX, float anchorY, 133 TextAnchor anchor, float rotateX, float rotateY, double angle) { 134 135 Size2D dim = calculateDimensions(g2); 136 float xAdj = 0.0f; 137 if (anchor.isHorizontalCenter()) { 138 xAdj = (float) -dim.getWidth() / 2.0f; 139 } 140 else if (anchor.isRight()) { 141 xAdj = (float) -dim.getWidth(); 142 } 143 float x = anchorX + xAdj; 144 final float yOffset = calculateBaselineOffset(g2, anchor); 145 final Iterator iterator = this.fragments.iterator(); 146 while (iterator.hasNext()) { 147 final TextFragment fragment = (TextFragment) iterator.next(); 148 final Size2D d = fragment.calculateDimensions(g2); 149 fragment.draw(g2, x, anchorY + yOffset, TextAnchor.BASELINE_LEFT, 150 rotateX, rotateY, angle); 151 x = x + (float) d.getWidth(); 152 } 153 154 } 155 156 /** 157 * Calculates the width and height of the text line. 158 * 159 * @param g2 the graphics device. 160 * 161 * @return The width and height. 162 */ 163 public Size2D calculateDimensions(Graphics2D g2) { 164 double width = 0.0; 165 double height = 0.0; 166 final Iterator iterator = this.fragments.iterator(); 167 while (iterator.hasNext()) { 168 final TextFragment fragment = (TextFragment) iterator.next(); 169 final Size2D dimension = fragment.calculateDimensions(g2); 170 width = width + dimension.getWidth(); 171 height = Math.max(height, dimension.getHeight()); 172 } 173 return new Size2D(width, height); 174 } 175 176 /** 177 * Returns the first text fragment in the line. 178 * 179 * @return The first text fragment in the line. 180 */ 181 public TextFragment getFirstTextFragment() { 182 TextFragment result = null; 183 if (this.fragments.size() > 0) { 184 result = (TextFragment) this.fragments.get(0); 185 } 186 return result; 187 } 188 189 /** 190 * Returns the last text fragment in the line. 191 * 192 * @return The last text fragment in the line. 193 */ 194 public TextFragment getLastTextFragment() { 195 TextFragment result = null; 196 if (this.fragments.size() > 0) { 197 result = (TextFragment) this.fragments.get(this.fragments.size() 198 - 1); 199 } 200 return result; 201 } 202 203 /** 204 * Calculate the offsets required to translate from the specified anchor 205 * position to the left baseline position. 206 * 207 * @param g2 the graphics device. 208 * @param anchor the anchor position. 209 * 210 * @return The offsets. 211 */ 212 private float calculateBaselineOffset(Graphics2D g2, TextAnchor anchor) { 213 float result = 0.0f; 214 Iterator iterator = this.fragments.iterator(); 215 while (iterator.hasNext()) { 216 TextFragment fragment = (TextFragment) iterator.next(); 217 result = Math.max(result, 218 fragment.calculateBaselineOffset(g2, anchor)); 219 } 220 return result; 221 } 222 223 /** 224 * Tests this object for equality with an arbitrary object. 225 * 226 * @param obj the object to test against ({@code null} permitted). 227 * 228 * @return A boolean. 229 */ 230 @Override 231 public boolean equals(Object obj) { 232 if (obj == null) { 233 return false; 234 } 235 if (obj == this) { 236 return true; 237 } 238 if (obj instanceof TextLine) { 239 final TextLine line = (TextLine) obj; 240 return this.fragments.equals(line.fragments); 241 } 242 return false; 243 } 244 245 /** 246 * Returns a hash code for this object. 247 * 248 * @return A hash code. 249 */ 250 @Override 251 public int hashCode() { 252 return (this.fragments != null ? this.fragments.hashCode() : 0); 253 } 254 255}