0N/A/*
2362N/A * Copyright (c) 1998, 2001, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * (C) Copyright Taligent, Inc. 1996 - 1997, All Rights Reserved
0N/A * (C) Copyright IBM Corp. 1996 - 1998, All Rights Reserved
0N/A *
0N/A * The original version of this source code and documentation is
0N/A * copyrighted and owned by Taligent, Inc., a wholly-owned subsidiary
0N/A * of IBM. These materials are provided under terms of a License
0N/A * Agreement between Taligent and Sun. This technology is protected
0N/A * by multiple US and International patents.
0N/A *
0N/A * This notice and attribution to Taligent may not be removed.
0N/A * Taligent is a registered trademark of Taligent, Inc.
0N/A *
0N/A */
0N/A
0N/Apackage java.awt.font;
0N/A
0N/Aimport java.text.BreakIterator;
0N/Aimport java.text.CharacterIterator;
0N/Aimport java.text.AttributedCharacterIterator;
0N/Aimport java.awt.font.FontRenderContext;
0N/A
0N/A/**
0N/A * The <code>LineBreakMeasurer</code> class allows styled text to be
0N/A * broken into lines (or segments) that fit within a particular visual
0N/A * advance. This is useful for clients who wish to display a paragraph of
0N/A * text that fits within a specific width, called the <b>wrapping
0N/A * width</b>.
0N/A * <p>
0N/A * <code>LineBreakMeasurer</code> is constructed with an iterator over
0N/A * styled text. The iterator's range should be a single paragraph in the
0N/A * text.
0N/A * <code>LineBreakMeasurer</code> maintains a position in the text for the
0N/A * start of the next text segment. Initially, this position is the
0N/A * start of text. Paragraphs are assigned an overall direction (either
0N/A * left-to-right or right-to-left) according to the bidirectional
0N/A * formatting rules. All segments obtained from a paragraph have the
0N/A * same direction as the paragraph.
0N/A * <p>
0N/A * Segments of text are obtained by calling the method
0N/A * <code>nextLayout</code>, which returns a {@link TextLayout}
0N/A * representing the text that fits within the wrapping width.
0N/A * The <code>nextLayout</code> method moves the current position
0N/A * to the end of the layout returned from <code>nextLayout</code>.
0N/A * <p>
0N/A * <code>LineBreakMeasurer</code> implements the most commonly used
0N/A * line-breaking policy: Every word that fits within the wrapping
0N/A * width is placed on the line. If the first word does not fit, then all
0N/A * of the characters that fit within the wrapping width are placed on the
0N/A * line. At least one character is placed on each line.
0N/A * <p>
0N/A * The <code>TextLayout</code> instances returned by
0N/A * <code>LineBreakMeasurer</code> treat tabs like 0-width spaces. Clients
0N/A * who wish to obtain tab-delimited segments for positioning should use
0N/A * the overload of <code>nextLayout</code> which takes a limiting offset
0N/A * in the text.
0N/A * The limiting offset should be the first character after the tab.
0N/A * The <code>TextLayout</code> objects returned from this method end
0N/A * at the limit provided (or before, if the text between the current
0N/A * position and the limit won't fit entirely within the wrapping
0N/A * width).
0N/A * <p>
0N/A * Clients who are laying out tab-delimited text need a slightly
0N/A * different line-breaking policy after the first segment has been
0N/A * placed on a line. Instead of fitting partial words in the
0N/A * remaining space, they should place words which don't fit in the
0N/A * remaining space entirely on the next line. This change of policy
0N/A * can be requested in the overload of <code>nextLayout</code> which
0N/A * takes a <code>boolean</code> parameter. If this parameter is
0N/A * <code>true</code>, <code>nextLayout</code> returns
0N/A * <code>null</code> if the first word won't fit in
0N/A * the given space. See the tab sample below.
0N/A * <p>
0N/A * In general, if the text used to construct the
0N/A * <code>LineBreakMeasurer</code> changes, a new
0N/A * <code>LineBreakMeasurer</code> must be constructed to reflect
0N/A * the change. (The old <code>LineBreakMeasurer</code> continues to
0N/A * function properly, but it won't be aware of the text change.)
0N/A * Nevertheless, if the text change is the insertion or deletion of a
0N/A * single character, an existing <code>LineBreakMeasurer</code> can be
0N/A * 'updated' by calling <code>insertChar</code> or
0N/A * <code>deleteChar</code>. Updating an existing
0N/A * <code>LineBreakMeasurer</code> is much faster than creating a new one.
0N/A * Clients who modify text based on user typing should take advantage
0N/A * of these methods.
0N/A * <p>
0N/A * <strong>Examples</strong>:<p>
0N/A * Rendering a paragraph in a component
0N/A * <blockquote>
0N/A * <pre>
0N/A * public void paint(Graphics graphics) {
0N/A *
0N/A * Point2D pen = new Point2D(10, 20);
0N/A * Graphics2D g2d = (Graphics2D)graphics;
0N/A * FontRenderContext frc = g2d.getFontRenderContext();
0N/A *
0N/A * // let styledText be an AttributedCharacterIterator containing at least
0N/A * // one character
0N/A *
0N/A * LineBreakMeasurer measurer = new LineBreakMeasurer(styledText, frc);
0N/A * float wrappingWidth = getSize().width - 15;
0N/A *
0N/A * while (measurer.getPosition() < fStyledText.length()) {
0N/A *
0N/A * TextLayout layout = measurer.nextLayout(wrappingWidth);
0N/A *
0N/A * pen.y += (layout.getAscent());
0N/A * float dx = layout.isLeftToRight() ?
0N/A * 0 : (wrappingWidth - layout.getAdvance());
0N/A *
0N/A * layout.draw(graphics, pen.x + dx, pen.y);
0N/A * pen.y += layout.getDescent() + layout.getLeading();
0N/A * }
0N/A * }
0N/A * </pre>
0N/A * </blockquote>
0N/A * <p>
0N/A * Rendering text with tabs. For simplicity, the overall text
0N/A * direction is assumed to be left-to-right
0N/A * <blockquote>
0N/A * <pre>
0N/A * public void paint(Graphics graphics) {
0N/A *
0N/A * float leftMargin = 10, rightMargin = 310;
0N/A * float[] tabStops = { 100, 250 };
0N/A *
0N/A * // assume styledText is an AttributedCharacterIterator, and the number
0N/A * // of tabs in styledText is tabCount
0N/A *
0N/A * int[] tabLocations = new int[tabCount+1];
0N/A *
0N/A * int i = 0;
0N/A * for (char c = styledText.first(); c != styledText.DONE; c = styledText.next()) {
0N/A * if (c == '\t') {
0N/A * tabLocations[i++] = styledText.getIndex();
0N/A * }
0N/A * }
0N/A * tabLocations[tabCount] = styledText.getEndIndex() - 1;
0N/A *
0N/A * // Now tabLocations has an entry for every tab's offset in
0N/A * // the text. For convenience, the last entry is tabLocations
0N/A * // is the offset of the last character in the text.
0N/A *
0N/A * LineBreakMeasurer measurer = new LineBreakMeasurer(styledText);
0N/A * int currentTab = 0;
0N/A * float verticalPos = 20;
0N/A *
0N/A * while (measurer.getPosition() < styledText.getEndIndex()) {
0N/A *
0N/A * // Lay out and draw each line. All segments on a line
0N/A * // must be computed before any drawing can occur, since
0N/A * // we must know the largest ascent on the line.
0N/A * // TextLayouts are computed and stored in a Vector;
0N/A * // their horizontal positions are stored in a parallel
0N/A * // Vector.
0N/A *
0N/A * // lineContainsText is true after first segment is drawn
0N/A * boolean lineContainsText = false;
0N/A * boolean lineComplete = false;
0N/A * float maxAscent = 0, maxDescent = 0;
0N/A * float horizontalPos = leftMargin;
0N/A * Vector layouts = new Vector(1);
0N/A * Vector penPositions = new Vector(1);
0N/A *
0N/A * while (!lineComplete) {
0N/A * float wrappingWidth = rightMargin - horizontalPos;
0N/A * TextLayout layout =
0N/A * measurer.nextLayout(wrappingWidth,
0N/A * tabLocations[currentTab]+1,
0N/A * lineContainsText);
0N/A *
0N/A * // layout can be null if lineContainsText is true
0N/A * if (layout != null) {
0N/A * layouts.addElement(layout);
0N/A * penPositions.addElement(new Float(horizontalPos));
0N/A * horizontalPos += layout.getAdvance();
0N/A * maxAscent = Math.max(maxAscent, layout.getAscent());
0N/A * maxDescent = Math.max(maxDescent,
0N/A * layout.getDescent() + layout.getLeading());
0N/A * } else {
0N/A * lineComplete = true;
0N/A * }
0N/A *
0N/A * lineContainsText = true;
0N/A *
0N/A * if (measurer.getPosition() == tabLocations[currentTab]+1) {
0N/A * currentTab++;
0N/A * }
0N/A *
0N/A * if (measurer.getPosition() == styledText.getEndIndex())
0N/A * lineComplete = true;
0N/A * else if (horizontalPos >= tabStops[tabStops.length-1])
0N/A * lineComplete = true;
0N/A *
0N/A * if (!lineComplete) {
0N/A * // move to next tab stop
0N/A * int j;
0N/A * for (j=0; horizontalPos >= tabStops[j]; j++) {}
0N/A * horizontalPos = tabStops[j];
0N/A * }
0N/A * }
0N/A *
0N/A * verticalPos += maxAscent;
0N/A *
0N/A * Enumeration layoutEnum = layouts.elements();
0N/A * Enumeration positionEnum = penPositions.elements();
0N/A *
0N/A * // now iterate through layouts and draw them
0N/A * while (layoutEnum.hasMoreElements()) {
0N/A * TextLayout nextLayout = (TextLayout) layoutEnum.nextElement();
0N/A * Float nextPosition = (Float) positionEnum.nextElement();
0N/A * nextLayout.draw(graphics, nextPosition.floatValue(), verticalPos);
0N/A * }
0N/A *
0N/A * verticalPos += maxDescent;
0N/A * }
0N/A * }
0N/A * </pre>
0N/A * </blockquote>
0N/A * @see TextLayout
0N/A */
0N/A
0N/Apublic final class LineBreakMeasurer {
0N/A
0N/A private BreakIterator breakIter;
0N/A private int start;
0N/A private int pos;
0N/A private int limit;
0N/A private TextMeasurer measurer;
0N/A private CharArrayIterator charIter;
0N/A
0N/A /**
0N/A * Constructs a <code>LineBreakMeasurer</code> for the specified text.
0N/A *
0N/A * @param text the text for which this <code>LineBreakMeasurer</code>
0N/A * produces <code>TextLayout</code> objects; the text must contain
0N/A * at least one character; if the text available through
0N/A * <code>iter</code> changes, further calls to this
0N/A * <code>LineBreakMeasurer</code> instance are undefined (except,
0N/A * in some cases, when <code>insertChar</code> or
0N/A * <code>deleteChar</code> are invoked afterward - see below)
0N/A * @param frc contains information about a graphics device which is
0N/A * needed to measure the text correctly;
0N/A * text measurements can vary slightly depending on the
0N/A * device resolution, and attributes such as antialiasing; this
0N/A * parameter does not specify a translation between the
0N/A * <code>LineBreakMeasurer</code> and user space
0N/A * @see LineBreakMeasurer#insertChar
0N/A * @see LineBreakMeasurer#deleteChar
0N/A */
0N/A public LineBreakMeasurer(AttributedCharacterIterator text, FontRenderContext frc) {
0N/A this(text, BreakIterator.getLineInstance(), frc);
0N/A }
0N/A
0N/A /**
0N/A * Constructs a <code>LineBreakMeasurer</code> for the specified text.
0N/A *
0N/A * @param text the text for which this <code>LineBreakMeasurer</code>
0N/A * produces <code>TextLayout</code> objects; the text must contain
0N/A * at least one character; if the text available through
0N/A * <code>iter</code> changes, further calls to this
0N/A * <code>LineBreakMeasurer</code> instance are undefined (except,
0N/A * in some cases, when <code>insertChar</code> or
0N/A * <code>deleteChar</code> are invoked afterward - see below)
0N/A * @param breakIter the {@link BreakIterator} which defines line
0N/A * breaks
0N/A * @param frc contains information about a graphics device which is
0N/A * needed to measure the text correctly;
0N/A * text measurements can vary slightly depending on the
0N/A * device resolution, and attributes such as antialiasing; this
0N/A * parameter does not specify a translation between the
0N/A * <code>LineBreakMeasurer</code> and user space
0N/A * @throws IllegalArgumentException if the text has less than one character
0N/A * @see LineBreakMeasurer#insertChar
0N/A * @see LineBreakMeasurer#deleteChar
0N/A */
0N/A public LineBreakMeasurer(AttributedCharacterIterator text,
0N/A BreakIterator breakIter,
0N/A FontRenderContext frc) {
0N/A if (text.getEndIndex() - text.getBeginIndex() < 1) {
0N/A throw new IllegalArgumentException("Text must contain at least one character.");
0N/A }
0N/A
0N/A this.breakIter = breakIter;
0N/A this.measurer = new TextMeasurer(text, frc);
0N/A this.limit = text.getEndIndex();
0N/A this.pos = this.start = text.getBeginIndex();
0N/A
0N/A charIter = new CharArrayIterator(measurer.getChars(), this.start);
0N/A this.breakIter.setText(charIter);
0N/A }
0N/A
0N/A /**
0N/A * Returns the position at the end of the next layout. Does NOT
0N/A * update the current position of this <code>LineBreakMeasurer</code>.
0N/A *
0N/A * @param wrappingWidth the maximum visible advance permitted for
0N/A * the text in the next layout
0N/A * @return an offset in the text representing the limit of the
0N/A * next <code>TextLayout</code>.
0N/A */
0N/A public int nextOffset(float wrappingWidth) {
0N/A return nextOffset(wrappingWidth, limit, false);
0N/A }
0N/A
0N/A /**
0N/A * Returns the position at the end of the next layout. Does NOT
0N/A * update the current position of this <code>LineBreakMeasurer</code>.
0N/A *
0N/A * @param wrappingWidth the maximum visible advance permitted for
0N/A * the text in the next layout
0N/A * @param offsetLimit the first character that can not be included
0N/A * in the next layout, even if the text after the limit would fit
0N/A * within the wrapping width; <code>offsetLimit</code> must be
0N/A * greater than the current position
0N/A * @param requireNextWord if <code>true</code>, the current position
0N/A * that is returned if the entire next word does not fit within
0N/A * <code>wrappingWidth</code>; if <code>false</code>, the offset
0N/A * returned is at least one greater than the current position
0N/A * @return an offset in the text representing the limit of the
0N/A * next <code>TextLayout</code>
0N/A */
0N/A public int nextOffset(float wrappingWidth, int offsetLimit,
0N/A boolean requireNextWord) {
0N/A
0N/A int nextOffset = pos;
0N/A
0N/A if (pos < limit) {
0N/A if (offsetLimit <= pos) {
0N/A throw new IllegalArgumentException("offsetLimit must be after current position");
0N/A }
0N/A
0N/A int charAtMaxAdvance =
0N/A measurer.getLineBreakIndex(pos, wrappingWidth);
0N/A
0N/A if (charAtMaxAdvance == limit) {
0N/A nextOffset = limit;
0N/A }
0N/A else if (Character.isWhitespace(measurer.getChars()[charAtMaxAdvance-start])) {
0N/A nextOffset = breakIter.following(charAtMaxAdvance);
0N/A }
0N/A else {
0N/A // Break is in a word; back up to previous break.
0N/A
0N/A // NOTE: I think that breakIter.preceding(limit) should be
0N/A // equivalent to breakIter.last(), breakIter.previous() but
0N/A // the authors of BreakIterator thought otherwise...
0N/A // If they were equivalent then the first branch would be
0N/A // unnecessary.
0N/A int testPos = charAtMaxAdvance + 1;
0N/A if (testPos == limit) {
0N/A breakIter.last();
0N/A nextOffset = breakIter.previous();
0N/A }
0N/A else {
0N/A nextOffset = breakIter.preceding(testPos);
0N/A }
0N/A
0N/A if (nextOffset <= pos) {
0N/A // first word doesn't fit on line
0N/A if (requireNextWord) {
0N/A nextOffset = pos;
0N/A }
0N/A else {
0N/A nextOffset = Math.max(pos+1, charAtMaxAdvance);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A if (nextOffset > offsetLimit) {
0N/A nextOffset = offsetLimit;
0N/A }
0N/A
0N/A return nextOffset;
0N/A }
0N/A
0N/A /**
0N/A * Returns the next layout, and updates the current position.
0N/A *
0N/A * @param wrappingWidth the maximum visible advance permitted for
0N/A * the text in the next layout
0N/A * @return a <code>TextLayout</code>, beginning at the current
0N/A * position, which represents the next line fitting within
0N/A * <code>wrappingWidth</code>
0N/A */
0N/A public TextLayout nextLayout(float wrappingWidth) {
0N/A return nextLayout(wrappingWidth, limit, false);
0N/A }
0N/A
0N/A /**
0N/A * Returns the next layout, and updates the current position.
0N/A *
0N/A * @param wrappingWidth the maximum visible advance permitted
0N/A * for the text in the next layout
0N/A * @param offsetLimit the first character that can not be
0N/A * included in the next layout, even if the text after the limit
0N/A * would fit within the wrapping width; <code>offsetLimit</code>
0N/A * must be greater than the current position
0N/A * @param requireNextWord if <code>true</code>, and if the entire word
0N/A * at the current position does not fit within the wrapping width,
0N/A * <code>null</code> is returned. If <code>false</code>, a valid
0N/A * layout is returned that includes at least the character at the
0N/A * current position
0N/A * @return a <code>TextLayout</code>, beginning at the current
0N/A * position, that represents the next line fitting within
0N/A * <code>wrappingWidth</code>. If the current position is at the end
0N/A * of the text used by this <code>LineBreakMeasurer</code>,
0N/A * <code>null</code> is returned
0N/A */
0N/A public TextLayout nextLayout(float wrappingWidth, int offsetLimit,
0N/A boolean requireNextWord) {
0N/A
0N/A if (pos < limit) {
0N/A int layoutLimit = nextOffset(wrappingWidth, offsetLimit, requireNextWord);
0N/A if (layoutLimit == pos) {
0N/A return null;
0N/A }
0N/A
0N/A TextLayout result = measurer.getLayout(pos, layoutLimit);
0N/A pos = layoutLimit;
0N/A
0N/A return result;
0N/A } else {
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the current position of this <code>LineBreakMeasurer</code>.
0N/A *
0N/A * @return the current position of this <code>LineBreakMeasurer</code>
0N/A * @see #setPosition
0N/A */
0N/A public int getPosition() {
0N/A return pos;
0N/A }
0N/A
0N/A /**
0N/A * Sets the current position of this <code>LineBreakMeasurer</code>.
0N/A *
0N/A * @param newPosition the current position of this
0N/A * <code>LineBreakMeasurer</code>; the position should be within the
0N/A * text used to construct this <code>LineBreakMeasurer</code> (or in
0N/A * the text most recently passed to <code>insertChar</code>
0N/A * or <code>deleteChar</code>
0N/A * @see #getPosition
0N/A */
0N/A public void setPosition(int newPosition) {
0N/A if (newPosition < start || newPosition > limit) {
0N/A throw new IllegalArgumentException("position is out of range");
0N/A }
0N/A pos = newPosition;
0N/A }
0N/A
0N/A /**
0N/A * Updates this <code>LineBreakMeasurer</code> after a single
0N/A * character is inserted into the text, and sets the current
0N/A * position to the beginning of the paragraph.
0N/A *
0N/A * @param newParagraph the text after the insertion
0N/A * @param insertPos the position in the text at which the character
0N/A * is inserted
0N/A * @throws IndexOutOfBoundsException if <code>insertPos</code> is less
0N/A * than the start of <code>newParagraph</code> or greater than
0N/A * or equal to the end of <code>newParagraph</code>
0N/A * @throws NullPointerException if <code>newParagraph</code> is
0N/A * <code>null</code>
0N/A * @see #deleteChar
0N/A */
0N/A public void insertChar(AttributedCharacterIterator newParagraph,
0N/A int insertPos) {
0N/A
0N/A measurer.insertChar(newParagraph, insertPos);
0N/A
0N/A limit = newParagraph.getEndIndex();
0N/A pos = start = newParagraph.getBeginIndex();
0N/A
0N/A charIter.reset(measurer.getChars(), newParagraph.getBeginIndex());
0N/A breakIter.setText(charIter);
0N/A }
0N/A
0N/A /**
0N/A * Updates this <code>LineBreakMeasurer</code> after a single
0N/A * character is deleted from the text, and sets the current
0N/A * position to the beginning of the paragraph.
0N/A * @param newParagraph the text after the deletion
0N/A * @param deletePos the position in the text at which the character
0N/A * is deleted
0N/A * @throws IndexOutOfBoundsException if <code>deletePos</code> is
0N/A * less than the start of <code>newParagraph</code> or greater
0N/A * than the end of <code>newParagraph</code>
0N/A * @throws NullPointerException if <code>newParagraph</code> is
0N/A * <code>null</code>
0N/A * @see #insertChar
0N/A */
0N/A public void deleteChar(AttributedCharacterIterator newParagraph,
0N/A int deletePos) {
0N/A
0N/A measurer.deleteChar(newParagraph, deletePos);
0N/A
0N/A limit = newParagraph.getEndIndex();
0N/A pos = start = newParagraph.getBeginIndex();
0N/A
0N/A charIter.reset(measurer.getChars(), start);
0N/A breakIter.setText(charIter);
0N/A }
0N/A}