1 /* ========================================================================
2  * JCommon : a free general purpose class library for the Java(tm) platform
3  * ========================================================================
4  *
5  * (C) Copyright 2000-2013, by Object Refinery Limited and Contributors.
6  *
7  * Project Info:  http://www.jfree.org/jcommon/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * -------------
28  * TextLine.java
29  * -------------
30  * (C) Copyright 2003-2013, by Object Refinery Limited and Contributors.
31  *
32  * Original Author:  David Gilbert (for Object Refinery Limited);
33  * Contributor(s):   -;
34  *
35  * $Id: TextLine.java,v 1.13 2007/11/02 17:50:35 taqua Exp $
36  *
37  * Changes
38  * -------
39  * 07-Nov-2003 : Version 1 (DG);
40  * 22-Dec-2003 : Added workaround for Java bug 4245442 (DG);
41  * 29-Jan-2004 : Added new constructor (DG);
42  * 22-Mar-2004 : Added equals() method and implemented Serializable (DG);
43  * 01-Apr-2004 : Changed java.awt.geom.Dimension2D to org.jfree.ui.Size2D
44  *               because of JDK bug 4976448 which persists on JDK 1.3.1 (DG);
45  * 03-Sep-2004 : Added a method to remove a fragment (DG);
46  * 08-Jul-2005 : Fixed bug in calculateBaselineOffset() (DG);
47  * 01-Sep-2013 : Updated draw() method to take into account the textAnchor (DG);
48  *
49  */
50 
51 package org.jfree.text;
52 
53 import java.awt.Font;
54 import java.awt.Graphics2D;
55 import java.awt.Paint;
56 import java.io.Serializable;
57 import java.util.Iterator;
58 import java.util.List;
59 
60 import org.jfree.ui.Size2D;
61 import org.jfree.ui.TextAnchor;
62 
63 /**
64  * A sequence of {@link TextFragment} objects that together form a line of
65  * text.  A sequence of text lines is managed by the {@link TextBlock} class.
66  *
67  * @author David Gilbert
68  */
69 public class TextLine implements Serializable {
70 
71     /** For serialization. */
72     private static final long serialVersionUID = 7100085690160465444L;
73 
74     /** Storage for the text fragments that make up the line. */
75     private List fragments;
76 
77     /**
78      * Creates a new empty line.
79      */
TextLine()80     public TextLine() {
81         this.fragments = new java.util.ArrayList();
82     }
83 
84     /**
85      * Creates a new text line using the default font.
86      *
87      * @param text  the text (<code>null</code> not permitted).
88      */
TextLine(final String text)89     public TextLine(final String text) {
90         this(text, TextFragment.DEFAULT_FONT);
91     }
92 
93     /**
94      * Creates a new text line.
95      *
96      * @param text  the text (<code>null</code> not permitted).
97      * @param font  the text font (<code>null</code> not permitted).
98      */
TextLine(final String text, final Font font)99     public TextLine(final String text, final Font font) {
100         this.fragments = new java.util.ArrayList();
101         final TextFragment fragment = new TextFragment(text, font);
102         this.fragments.add(fragment);
103     }
104 
105     /**
106      * Creates a new text line.
107      *
108      * @param text  the text (<code>null</code> not permitted).
109      * @param font  the text font (<code>null</code> not permitted).
110      * @param paint  the text color (<code>null</code> not permitted).
111      */
TextLine(final String text, final Font font, final Paint paint)112     public TextLine(final String text, final Font font, final Paint paint) {
113         if (text == null) {
114             throw new IllegalArgumentException("Null 'text' argument.");
115         }
116         if (font == null) {
117             throw new IllegalArgumentException("Null 'font' argument.");
118         }
119         if (paint == null) {
120             throw new IllegalArgumentException("Null 'paint' argument.");
121         }
122         this.fragments = new java.util.ArrayList();
123         final TextFragment fragment = new TextFragment(text, font, paint);
124         this.fragments.add(fragment);
125     }
126 
127     /**
128      * Adds a text fragment to the text line.
129      *
130      * @param fragment  the text fragment (<code>null</code> not permitted).
131      */
addFragment(final TextFragment fragment)132     public void addFragment(final TextFragment fragment) {
133         this.fragments.add(fragment);
134     }
135 
136     /**
137      * Removes a fragment from the line.
138      *
139      * @param fragment  the fragment to remove.
140      */
removeFragment(final TextFragment fragment)141     public void removeFragment(final TextFragment fragment) {
142         this.fragments.remove(fragment);
143     }
144 
145     /**
146      * Draws the text line.
147      *
148      * @param g2  the graphics device.
149      * @param anchorX  the x-coordinate for the anchor point.
150      * @param anchorY  the y-coordinate for the anchor point.
151      * @param anchor  the point on the text line that is aligned to the anchor
152      *                point.
153      * @param rotateX  the x-coordinate for the rotation point.
154      * @param rotateY  the y-coordinate for the rotation point.
155      * @param angle  the rotation angle (in radians).
156      */
draw(Graphics2D g2, float anchorX, float anchorY, TextAnchor anchor, float rotateX, float rotateY, double angle)157     public void draw(Graphics2D g2, float anchorX, float anchorY,
158             TextAnchor anchor, float rotateX,  float rotateY, double angle) {
159 
160         Size2D dim = calculateDimensions(g2);
161         float xAdj = 0.0f;
162         if (anchor.isHorizontalCenter()) {
163             xAdj = (float) -dim.getWidth() / 2.0f;
164         }
165         else if (anchor.isRight()) {
166             xAdj = (float) -dim.getWidth();
167         }
168         float x = anchorX + xAdj;
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(g2, x, anchorY + yOffset, TextAnchor.BASELINE_LEFT,
175                     rotateX, rotateY, angle);
176             x = x + (float) d.getWidth();
177         }
178 
179     }
180 
181     /**
182      * Calculates the width and height of the text line.
183      *
184      * @param g2  the graphics device.
185      *
186      * @return The width and height.
187      */
calculateDimensions(final Graphics2D g2)188     public Size2D calculateDimensions(final Graphics2D g2) {
189         double width = 0.0;
190         double height = 0.0;
191         final Iterator iterator = this.fragments.iterator();
192         while (iterator.hasNext()) {
193             final TextFragment fragment = (TextFragment) iterator.next();
194             final Size2D dimension = fragment.calculateDimensions(g2);
195             width = width + dimension.getWidth();
196             height = Math.max(height, dimension.getHeight());
197         }
198         return new Size2D(width, height);
199     }
200 
201     /**
202      * Returns the first text fragment in the line.
203      *
204      * @return The first text fragment in the line.
205      */
getFirstTextFragment()206     public TextFragment getFirstTextFragment() {
207         TextFragment result = null;
208         if (this.fragments.size() > 0) {
209             result = (TextFragment) this.fragments.get(0);
210         }
211         return result;
212     }
213 
214     /**
215      * Returns the last text fragment in the line.
216      *
217      * @return The last text fragment in the line.
218      */
getLastTextFragment()219     public TextFragment getLastTextFragment() {
220         TextFragment result = null;
221         if (this.fragments.size() > 0) {
222             result = (TextFragment) this.fragments.get(this.fragments.size()
223                     - 1);
224         }
225         return result;
226     }
227 
228     /**
229      * Calculate the offsets required to translate from the specified anchor
230      * position to the left baseline position.
231      *
232      * @param g2  the graphics device.
233      * @param anchor  the anchor position.
234      *
235      * @return The offsets.
236      */
calculateBaselineOffset(final Graphics2D g2, final TextAnchor anchor)237     private float calculateBaselineOffset(final Graphics2D g2,
238                                           final TextAnchor anchor) {
239         float result = 0.0f;
240         Iterator iterator = this.fragments.iterator();
241         while (iterator.hasNext()) {
242             TextFragment fragment = (TextFragment) iterator.next();
243             result = Math.max(result,
244                     fragment.calculateBaselineOffset(g2, anchor));
245         }
246         return result;
247     }
248 
249     /**
250      * Tests this object for equality with an arbitrary object.
251      *
252      * @param obj  the object to test against (<code>null</code> permitted).
253      *
254      * @return A boolean.
255      */
equals(final Object obj)256     public boolean equals(final Object obj) {
257         if (obj == null) {
258             return false;
259         }
260         if (obj == this) {
261             return true;
262         }
263         if (obj instanceof TextLine) {
264             final TextLine line = (TextLine) obj;
265             return this.fragments.equals(line.fragments);
266         }
267         return false;
268     }
269 
270     /**
271      * Returns a hash code for this object.
272      *
273      * @return A hash code.
274      */
hashCode()275     public int hashCode() {
276         return (this.fragments != null ? this.fragments.hashCode() : 0);
277     }
278 
279 }
280