home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-20 | 5.2 KB | 145 lines |
- /*
- * @(#)TextMeasurer.java 1.7 98/03/18
- *
- * Copyright 1997, 1998 by Sun Microsystems, Inc.,
- * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
- * All rights reserved.
- *
- * This software is the confidential and proprietary information
- * of Sun Microsystems, Inc. ("Confidential Information"). You
- * shall not disclose such Confidential Information and shall use
- * it only in accordance with the terms of the license agreement
- * you entered into with Sun.
- */
-
- /*
- * (C) Copyright Taligent, Inc. 1996 - 1997, All Rights Reserved
- * (C) Copyright IBM Corp. 1996 - 1998, All Rights Reserved
- *
- * The original version of this source code and documentation is
- * copyrighted and owned by Taligent, Inc., a wholly-owned subsidiary
- * of IBM. These materials are provided under terms of a License
- * Agreement between Taligent and Sun. This technology is protected
- * by multiple US and International patents.
- *
- * This notice and attribution to Taligent may not be removed.
- * Taligent is a registered trademark of Taligent, Inc.
- *
- */
-
- package java.awt.font;
-
- import java.text.CharacterIterator;
- import java.text.AttributedCharacterIterator;
-
- /**
- * This class implements the 'primitive' operations needed for line
- * break: measuring up to a given advance, determining the advance of
- * a range of characters, and generating a TextLayout for a range of
- * characters. It also provides optimizations for incremental editing
- * of paragraphs.
- *
- * Most clients will use the more convenient LineBreakMeasurer, which
- * implements the standard line break policy (placing as many words as
- * will fit on each line).
- *
- * @see LineBreakMeasurer
- */
- class TextMeasurer {
- private int fStart;
- private TextLayout fLayout;
-
- /*
- * This version is currently implemented with TextLayout. Eventually,
- * the 'measurer' implementation will be removed from TextLayout and
- * implemented directly in this class.
- */
-
- /**
- * Construct a TextMeasurer from the source text. The source text
- * should be a single entire paragraph.
- * @param text the source paragraph
- */
- public TextMeasurer(AttributedCharacterIterator text) {
- fStart = text.getBeginIndex();
- fLayout = new TextLayout(text);
- }
-
- /**
- * Accumulate the advances of the characters at and after start,
- * until a character is reached whose advance would equal or
- * exceed maxAdvance. Return the index of that character.
- * @param start the character index at which to start measuring.
- * This is the absolute index, not the relative index within the
- * paragraph.
- * @param maxAdvance the maximumAdvance
- * @return the index of the character whose advance exceeded
- * maxAdvance.
- */
- public int getLineBreakIndex(int start, float maxAdvance) {
- return fLayout.getLineBreakIndex(start-fStart, maxAdvance) + fStart;
- }
-
- /**
- * Return the sum of the advances for the characters
- * from start up to limit.
- *
- * @param start the character index at which to start measuring
- * @param limit the character index at which to stop measuring
- * @return the sum of the advances of the range of characters.
- */
- public float getAdvanceBetween(int start, int limit) {
- // REMIND jk df (need a real implementation)
- TextLayout hack = fLayout.sublayout(start-fStart, limit-fStart);
- return hack.getAdvance();
- }
-
- /**
- * Return a layout that represents the characters. The number
- * of characters must be >= 1. The returned layout will apply the
- * bidi 'line reordering' rules to the text.
- *
- * @param start the index of the first character to use
- * @param limit the index past the last character to use
- * @return a new layout
- */
- public TextLayout getLayout(int start, int limit) {
- return fLayout.sublayout(start-fStart, limit-fStart);
- }
-
- /**
- * An optimization to facilitate inserting single characters from
- * a paragraph. Clients can then remeasure and reextract layouts as
- * required. In general, clients can always begin remeasuring from
- * the layout preceeding the one that contains the new character,
- * and stop measuring once the start of a subsequent layout matches
- * up with the start of a layout the client has cached (plus one
- * for the inserted character).
- *
- * @param newParagraph the text of the paragraph after performing
- * the insertion.
- * @param insertPos the position in the text
- * at which the single character was inserted.
- * @see #deleteChar
- */
- public void insertChar(AttributedCharacterIterator newParagraph, int insertPos) {
- fStart = newParagraph.getBeginIndex();
- fLayout = fLayout.insertChar(newParagraph, insertPos);
- }
-
- /**
- * An optimization to facilitate deleting single characters from
- * a paragraph.
- *
- * @param newParagraph the text of the paragraph after performing
- * the deletion.
- * @param deletePos the position in the text
- * at which the single character was deleted.
- * @see #insertChar
- */
- public void deleteChar(AttributedCharacterIterator newParagraph, int deletePos) {
- fStart = newParagraph.getBeginIndex();
- fLayout = fLayout.deleteChar(newParagraph, deletePos);
- }
- }
-