Source for java.awt.font.TextMeasurer

   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.text.AttributedString;
  43: import java.awt.Shape;
  44: 
  45: /**
  46:  * TextMeasurer is a small utility class for measuring the length of laid-out
  47:  * text objects. 
  48:  *
  49:  * @author Sven de Marothy
  50:  * @since 1.3
  51:  */
  52: public final class TextMeasurer implements Cloneable
  53: {
  54:   private AttributedCharacterIterator text;
  55:   private FontRenderContext frc;
  56:   private TextLayout totalLayout;
  57:   private int numChars;
  58: 
  59:   /**
  60:    * Creates a TextMeasurer from a given text in the form of an
  61:    * <code>AttributedCharacterIterator</code> and a 
  62:    * <code>FontRenderContext</code>.
  63:    */  
  64:   public TextMeasurer (AttributedCharacterIterator text, FontRenderContext frc)
  65:   {
  66:     this.text = text;
  67:     this.frc = frc;
  68:     totalLayout = new TextLayout( text, frc );
  69:     numChars = totalLayout.getCharacterCount();
  70:   }
  71: 
  72:   /**
  73:    * Clones the TextMeasurer object
  74:    */
  75:   protected Object clone ()
  76:   {
  77:     return new TextMeasurer( text, frc );
  78:   }
  79: 
  80:   /**
  81:    * Update the text if a character is deleted at the position deletePos
  82:    * @param newParagraph - the updated paragraph.
  83:    * @param deletePos - the deletion position
  84:    */
  85:   public void deleteChar (AttributedCharacterIterator newParagraph,
  86:                           int deletePos)
  87:   {
  88:     totalLayout = new TextLayout(newParagraph, frc);
  89:     if( deletePos < 0 || deletePos > totalLayout.getCharacterCount() )
  90:       throw new NullPointerException("Invalid deletePos:"+deletePos);
  91:     numChars = totalLayout.getCharacterCount();
  92:     text = newParagraph;
  93:   }
  94: 
  95:   /**
  96:    * Update the text if a character is inserted at the position insertPos
  97:    * @param newParagraph - the updated paragraph.
  98:    * @param insertPos - the insertion position
  99:    */
 100:   public void insertChar (AttributedCharacterIterator newParagraph,
 101:                           int insertPos)
 102:   {
 103:     totalLayout = new TextLayout(newParagraph, frc);
 104:     if( insertPos < 0 || insertPos > totalLayout.getCharacterCount() )
 105:       throw new NullPointerException("Invalid insertPos:"+insertPos);
 106:     numChars = totalLayout.getCharacterCount();
 107:     text = newParagraph;
 108:   }
 109: 
 110:   /***
 111:    * Returns the total advance between two positions in the paragraph.
 112:    * Characters from start to limit-1 (inclusive) are included in this count.
 113:    *
 114:    * @param start - the starting character index.
 115:    * @param limit - the limiting index.
 116:    */
 117:   public float getAdvanceBetween (int start, int limit)
 118:   {
 119:     Shape s = totalLayout.getLogicalHighlightShape( start, limit );
 120:     return (float)s.getBounds2D().getWidth();
 121:   }
 122: 
 123:   /**
 124:    * Returns a <code>TextLayout</code> object corresponding to the characters
 125:    * from text to limit.
 126:    * @param start - the starting character index.
 127:    * @param limit - the limiting index.
 128:    */
 129:   public TextLayout getLayout (int start, int limit)
 130:   {
 131:     if( start >= limit )
 132:       throw new IllegalArgumentException("Start position must be < limit.");
 133:     return new TextLayout( totalLayout, start, limit );
 134:   }
 135: 
 136:   /**
 137:    * Returns the line-break index from a given starting index and a maximum
 138:    * advance. The index returned is the first character outside the given
 139:    * advance (or the limit of the string, if all remaining characters fit.)
 140:    *
 141:    * @param start - the starting index.
 142:    * @param maxAdvance - the maximum advance allowed.
 143:    * @return the index of the first character beyond maxAdvance, or the 
 144:    * index of the last character + 1.
 145:    */
 146:   public int getLineBreakIndex (int start, float maxAdvance)
 147:   {   
 148:     if( start < 0 )
 149:       throw new IllegalArgumentException("Start parameter must be > 0.");
 150: 
 151:     double remainingLength = getAdvanceBetween( start, numChars );
 152:     
 153:     int guessOffset = (int)( ( (double)maxAdvance / (double)remainingLength)
 154:                  * ( (double)numChars - (double)start ) );
 155:     guessOffset += start;
 156:     if( guessOffset > numChars )
 157:       guessOffset = numChars;
 158:     
 159:     double guessLength = getAdvanceBetween( start, guessOffset );
 160:     boolean makeSmaller = ( guessLength > maxAdvance );
 161:     int inc = makeSmaller ? -1 : 1;
 162:     boolean keepGoing = true;
 163: 
 164:     do
 165:       {
 166:     guessOffset = guessOffset + inc;
 167:     if( guessOffset <= start || guessOffset > numChars )
 168:       {
 169:         keepGoing = false;
 170:       }
 171:     else
 172:       {
 173:         guessLength = getAdvanceBetween( start, guessOffset );
 174:         if( makeSmaller && ( guessLength <= maxAdvance) )      
 175:           keepGoing = false;
 176:         if( !makeSmaller && ( guessLength >= maxAdvance) )
 177:           keepGoing = false;
 178:       }
 179:       }
 180:     while( keepGoing );
 181: 
 182:     // Return first index that doesn't fit.
 183:     if( !makeSmaller )
 184:       guessOffset--;
 185: 
 186:     if( guessOffset > numChars )
 187:       return numChars;
 188: 
 189:     return guessOffset;
 190:   }
 191: }