home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / awt / font / TextMeasurer.java < prev   
Encoding:
Java Source  |  1998-03-20  |  5.2 KB  |  145 lines

  1. /*
  2.  * @(#)TextMeasurer.java    1.7 98/03/18
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. /*
  16.  * (C) Copyright Taligent, Inc. 1996 - 1997, All Rights Reserved
  17.  * (C) Copyright IBM Corp. 1996 - 1998, All Rights Reserved
  18.  *
  19.  * The original version of this source code and documentation is
  20.  * copyrighted and owned by Taligent, Inc., a wholly-owned subsidiary
  21.  * of IBM. These materials are provided under terms of a License
  22.  * Agreement between Taligent and Sun. This technology is protected
  23.  * by multiple US and International patents.
  24.  *
  25.  * This notice and attribution to Taligent may not be removed.
  26.  * Taligent is a registered trademark of Taligent, Inc.
  27.  *
  28.  */
  29.  
  30. package java.awt.font;
  31.  
  32. import java.text.CharacterIterator;
  33. import java.text.AttributedCharacterIterator;
  34.  
  35. /**
  36.  * This class implements the 'primitive' operations needed for line
  37.  * break: measuring up to a given advance, determining the advance of
  38.  * a range of characters, and generating a TextLayout for a range of
  39.  * characters. It also provides optimizations for incremental editing
  40.  * of paragraphs.
  41.  *
  42.  * Most clients will use the more convenient LineBreakMeasurer, which
  43.  * implements the standard line break policy (placing as many words as
  44.  * will fit on each line).
  45.  *
  46.  * @see LineBreakMeasurer
  47.  */
  48. class TextMeasurer {
  49.     private int fStart;
  50.     private TextLayout fLayout;
  51.  
  52.     /*
  53.      * This version is currently implemented with TextLayout.  Eventually,
  54.      * the 'measurer' implementation will be removed from TextLayout and
  55.      * implemented directly in this class.
  56.      */
  57.  
  58.     /**
  59.      * Construct a TextMeasurer from the source text.  The source text
  60.      * should be a single entire paragraph.
  61.      * @param text the source paragraph
  62.      */
  63.     public TextMeasurer(AttributedCharacterIterator text) {
  64.         fStart = text.getBeginIndex();
  65.         fLayout = new TextLayout(text);
  66.     }
  67.  
  68.     /**
  69.      * Accumulate the advances of the characters at and after start,
  70.      * until a character is reached whose advance would equal or
  71.      * exceed maxAdvance. Return the index of that character.
  72.      * @param start the character index at which to start measuring.
  73.      * This is the absolute index, not the relative index within the
  74.      * paragraph.
  75.      * @param maxAdvance the maximumAdvance
  76.      * @return the index of the character whose advance exceeded
  77.      * maxAdvance.
  78.      */
  79.     public int getLineBreakIndex(int start, float maxAdvance) {
  80.         return fLayout.getLineBreakIndex(start-fStart, maxAdvance) + fStart;
  81.     }
  82.  
  83.     /**
  84.      * Return the sum of the advances for the characters
  85.      * from start up to limit.
  86.      *
  87.      * @param start the character index at which to start measuring
  88.      * @param limit the character index at which to stop measuring
  89.      * @return the sum of the advances of the range of characters.
  90.      */
  91.     public float getAdvanceBetween(int start, int limit) {
  92.         // REMIND jk df (need a real implementation)
  93.         TextLayout hack = fLayout.sublayout(start-fStart, limit-fStart);
  94.         return hack.getAdvance();
  95.     }
  96.  
  97.     /**
  98.      * Return a layout that represents the characters.  The number
  99.      * of characters must be >= 1.  The returned layout will apply the
  100.      * bidi 'line reordering' rules to the text.
  101.      *
  102.      * @param start the index of the first character to use
  103.      * @param limit the index past the last character to use
  104.      * @return a new layout
  105.      */
  106.     public TextLayout getLayout(int start, int limit) {
  107.         return fLayout.sublayout(start-fStart, limit-fStart);
  108.     }
  109.  
  110.     /**
  111.      * An optimization to facilitate inserting single characters from
  112.      * a paragraph. Clients can then remeasure and reextract layouts as
  113.      * required.  In general, clients can always begin remeasuring from
  114.      * the layout preceeding the one that contains the new character,
  115.      * and stop measuring once the start of a subsequent layout matches
  116.      * up with the start of a layout the client has cached (plus one
  117.      * for the inserted character).
  118.      *
  119.      * @param newParagraph the text of the paragraph after performing
  120.      * the insertion.
  121.      * @param insertPos the position in the text
  122.      * at which the single character was inserted.
  123.      * @see #deleteChar
  124.      */
  125.     public void insertChar(AttributedCharacterIterator newParagraph, int insertPos) {
  126.         fStart = newParagraph.getBeginIndex();
  127.         fLayout = fLayout.insertChar(newParagraph, insertPos);
  128.     }
  129.     
  130.     /**
  131.      * An optimization to facilitate deleting single characters from
  132.      * a paragraph.
  133.      *
  134.      * @param newParagraph the text of the paragraph after performing
  135.      * the deletion.
  136.      * @param deletePos the position in the text
  137.      * at which the single character was deleted.
  138.      * @see #insertChar
  139.      */
  140.     public void deleteChar(AttributedCharacterIterator newParagraph, int deletePos) {
  141.         fStart = newParagraph.getBeginIndex();
  142.         fLayout = fLayout.deleteChar(newParagraph, deletePos);
  143.     }
  144. }
  145.