Merge with trank @ 137446
[official-gcc.git] / libjava / classpath / java / awt / font / TextMeasurer.java
blobf4430bf6dfaf3f7561c054fb831bdd78fa0afc28
1 /* TextMeasurer.java
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.awt.font;
41 import java.text.AttributedCharacterIterator;
42 import java.awt.Shape;
44 /**
45 * TextMeasurer is a small utility class for measuring the length of laid-out
46 * text objects.
48 * @author Sven de Marothy
49 * @since 1.3
51 public final class TextMeasurer implements Cloneable
53 private AttributedCharacterIterator text;
54 private FontRenderContext frc;
55 private TextLayout totalLayout;
56 private int numChars;
58 /**
59 * Creates a TextMeasurer from a given text in the form of an
60 * <code>AttributedCharacterIterator</code> and a
61 * <code>FontRenderContext</code>.
62 */
63 public TextMeasurer (AttributedCharacterIterator text, FontRenderContext frc)
65 this.text = text;
66 this.frc = frc;
67 totalLayout = new TextLayout( text, frc );
68 numChars = totalLayout.getCharacterCount();
71 /**
72 * Clones the TextMeasurer object
74 protected Object clone ()
76 return new TextMeasurer( text, frc );
79 /**
80 * Update the text if a character is deleted at the position deletePos
81 * @param newParagraph - the updated paragraph.
82 * @param deletePos - the deletion position
84 public void deleteChar (AttributedCharacterIterator newParagraph,
85 int deletePos)
87 totalLayout = new TextLayout(newParagraph, frc);
88 if( deletePos < 0 || deletePos > totalLayout.getCharacterCount() )
89 throw new NullPointerException("Invalid deletePos:"+deletePos);
90 numChars = totalLayout.getCharacterCount();
91 text = newParagraph;
94 /**
95 * Update the text if a character is inserted at the position insertPos
96 * @param newParagraph - the updated paragraph.
97 * @param insertPos - the insertion position
99 public void insertChar (AttributedCharacterIterator newParagraph,
100 int insertPos)
102 totalLayout = new TextLayout(newParagraph, frc);
103 if( insertPos < 0 || insertPos > totalLayout.getCharacterCount() )
104 throw new NullPointerException("Invalid insertPos:"+insertPos);
105 numChars = totalLayout.getCharacterCount();
106 text = newParagraph;
109 /***
110 * Returns the total advance between two positions in the paragraph.
111 * Characters from start to limit-1 (inclusive) are included in this count.
113 * @param start - the starting character index.
114 * @param limit - the limiting index.
116 public float getAdvanceBetween (int start, int limit)
118 Shape s = totalLayout.getLogicalHighlightShape( start, limit );
119 return (float)s.getBounds2D().getWidth();
123 * Returns a <code>TextLayout</code> object corresponding to the characters
124 * from text to limit.
125 * @param start - the starting character index.
126 * @param limit - the limiting index.
128 public TextLayout getLayout (int start, int limit)
130 if( start >= limit )
131 throw new IllegalArgumentException("Start position must be < limit.");
132 return new TextLayout( totalLayout, start, limit );
136 * Returns the line-break index from a given starting index and a maximum
137 * advance. The index returned is the first character outside the given
138 * advance (or the limit of the string, if all remaining characters fit.)
140 * @param start - the starting index.
141 * @param maxAdvance - the maximum advance allowed.
142 * @return the index of the first character beyond maxAdvance, or the
143 * index of the last character + 1.
145 public int getLineBreakIndex (int start, float maxAdvance)
147 if( start < 0 )
148 throw new IllegalArgumentException("Start parameter must be > 0.");
150 double remainingLength = getAdvanceBetween( start, numChars );
152 int guessOffset = (int)( ( (double)maxAdvance / (double)remainingLength)
153 * ( (double)numChars - (double)start ) );
154 guessOffset += start;
155 if( guessOffset > numChars )
156 guessOffset = numChars;
158 double guessLength = getAdvanceBetween( start, guessOffset );
159 boolean makeSmaller = ( guessLength > maxAdvance );
160 int inc = makeSmaller ? -1 : 1;
161 boolean keepGoing = true;
165 guessOffset = guessOffset + inc;
166 if( guessOffset <= start || guessOffset > numChars )
168 keepGoing = false;
170 else
172 guessLength = getAdvanceBetween( start, guessOffset );
173 if( makeSmaller && ( guessLength <= maxAdvance) )
174 keepGoing = false;
175 if( !makeSmaller && ( guessLength >= maxAdvance) )
176 keepGoing = false;
179 while( keepGoing );
181 // Return first index that doesn't fit.
182 if( !makeSmaller )
183 guessOffset--;
185 if( guessOffset > numChars )
186 return numChars;
188 return guessOffset;