Frames | No Frames |
1: /* TextMeasurer.java 2: Copyright (C) 2006 Free Software Foundation, Inc. 3: 4: This file is part of GNU Classpath. 5: 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. 10: 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. 15: 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. 20: 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. 25: 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. */ 37: 38: 39: package java.awt.font; 40: 41: import java.text.AttributedCharacterIterator; 42: import java.awt.Shape; 43: 44: /** 45: * TextMeasurer is a small utility class for measuring the length of laid-out 46: * text objects. 47: * 48: * @author Sven de Marothy 49: * @since 1.3 50: */ 51: public final class TextMeasurer implements Cloneable 52: { 53: private AttributedCharacterIterator text; 54: private FontRenderContext frc; 55: private TextLayout totalLayout; 56: private int numChars; 57: 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) 64: { 65: this.text = text; 66: this.frc = frc; 67: totalLayout = new TextLayout( text, frc ); 68: numChars = totalLayout.getCharacterCount(); 69: } 70: 71: /** 72: * Clones the TextMeasurer object 73: */ 74: protected Object clone () 75: { 76: return new TextMeasurer( text, frc ); 77: } 78: 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 83: */ 84: public void deleteChar (AttributedCharacterIterator newParagraph, 85: int deletePos) 86: { 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; 92: } 93: 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 98: */ 99: public void insertChar (AttributedCharacterIterator newParagraph, 100: int insertPos) 101: { 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; 107: } 108: 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. 112: * 113: * @param start - the starting character index. 114: * @param limit - the limiting index. 115: */ 116: public float getAdvanceBetween (int start, int limit) 117: { 118: Shape s = totalLayout.getLogicalHighlightShape( start, limit ); 119: return (float)s.getBounds2D().getWidth(); 120: } 121: 122: /** 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. 127: */ 128: public TextLayout getLayout (int start, int limit) 129: { 130: if( start >= limit ) 131: throw new IllegalArgumentException("Start position must be < limit."); 132: return new TextLayout( totalLayout, start, limit ); 133: } 134: 135: /** 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.) 139: * 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. 144: */ 145: public int getLineBreakIndex (int start, float maxAdvance) 146: { 147: if( start < 0 ) 148: throw new IllegalArgumentException("Start parameter must be > 0."); 149: 150: double remainingLength = getAdvanceBetween( start, numChars ); 151: 152: int guessOffset = (int)( ( (double)maxAdvance / (double)remainingLength) 153: * ( (double)numChars - (double)start ) ); 154: guessOffset += start; 155: if( guessOffset > numChars ) 156: guessOffset = numChars; 157: 158: double guessLength = getAdvanceBetween( start, guessOffset ); 159: boolean makeSmaller = ( guessLength > maxAdvance ); 160: int inc = makeSmaller ? -1 : 1; 161: boolean keepGoing = true; 162: 163: do 164: { 165: guessOffset = guessOffset + inc; 166: if( guessOffset <= start || guessOffset > numChars ) 167: { 168: keepGoing = false; 169: } 170: else 171: { 172: guessLength = getAdvanceBetween( start, guessOffset ); 173: if( makeSmaller && ( guessLength <= maxAdvance) ) 174: keepGoing = false; 175: if( !makeSmaller && ( guessLength >= maxAdvance) ) 176: keepGoing = false; 177: } 178: } 179: while( keepGoing ); 180: 181: // Return first index that doesn't fit. 182: if( !makeSmaller ) 183: guessOffset--; 184: 185: if( guessOffset > numChars ) 186: return numChars; 187: 188: return guessOffset; 189: } 190: }