Java Doc for LineBreakMeasurer.java in  » 6.0-JDK-Core » AWT » java » awt » font » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Home
Java Source Code / Java Documentation
1.6.0 JDK Core
2.6.0 JDK Modules
3.6.0 JDK Modules com.sun
4.6.0 JDK Modules com.sun.java
5.6.0 JDK Modules sun
6.6.0 JDK Platform
7.Ajax
8.Apache Harmony Java SE
9.Aspect oriented
10.Authentication Authorization
11.Blogger System
12.Build
13.Byte Code
14.Cache
15.Chart
16.Chat
17.Code Analyzer
18.Collaboration
19.Content Management System
20.Database Client
21.Database DBMS
22.Database JDBC Connection Pool
23.Database ORM
24.Development
25.EJB Server
26.ERP CRM Financial
27.ESB
28.Forum
29.Game
30.GIS
31.Graphic 3D
32.Graphic Library
33.Groupware
34.HTML Parser
35.IDE
36.IDE Eclipse
37.IDE Netbeans
38.Installer
39.Internationalization Localization
40.Inversion of Control
41.Issue Tracking
42.J2EE
43.J2ME
44.JBoss
45.JMS
46.JMX
47.Library
48.Mail Clients
49.Music
50.Net
51.Parser
52.PDF
53.Portal
54.Profiler
55.Project Management
56.Report
57.RSS RDF
58.Rule Engine
59.Science
60.Scripting
61.Search Engine
62.Security
63.Sevlet Container
64.Source Control
65.Swing Library
66.Template Engine
67.Test Coverage
68.Testing
69.UML
70.Web Crawler
71.Web Framework
72.Web Mail
73.Web Server
74.Web Services
75.Web Services apache cxf 2.2.6
76.Web Services AXIS2
77.Wiki Engine
78.Workflow Engines
79.XML
80.XML UI
Java Source Code / Java Documentation » 6.0 JDK Core » AWT » java.awt.font 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


java.lang.Object
   java.awt.font.LineBreakMeasurer

LineBreakMeasurer
final public class LineBreakMeasurer (Code)
The LineBreakMeasurer class allows styled text to be broken into lines (or segments) that fit within a particular visual advance. This is useful for clients who wish to display a paragraph of text that fits within a specific width, called the wrapping width.

LineBreakMeasurer is constructed with an iterator over styled text. The iterator's range should be a single paragraph in the text. LineBreakMeasurer maintains a position in the text for the start of the next text segment. Initially, this position is the start of text. Paragraphs are assigned an overall direction (either left-to-right or right-to-left) according to the bidirectional formatting rules. All segments obtained from a paragraph have the same direction as the paragraph.

Segments of text are obtained by calling the method nextLayout, which returns a TextLayout representing the text that fits within the wrapping width. The nextLayout method moves the current position to the end of the layout returned from nextLayout.

LineBreakMeasurer implements the most commonly used line-breaking policy: Every word that fits within the wrapping width is placed on the line. If the first word does not fit, then all of the characters that fit within the wrapping width are placed on the line. At least one character is placed on each line.

The TextLayout instances returned by LineBreakMeasurer treat tabs like 0-width spaces. Clients who wish to obtain tab-delimited segments for positioning should use the overload of nextLayout which takes a limiting offset in the text. The limiting offset should be the first character after the tab. The TextLayout objects returned from this method end at the limit provided (or before, if the text between the current position and the limit won't fit entirely within the wrapping width).

Clients who are laying out tab-delimited text need a slightly different line-breaking policy after the first segment has been placed on a line. Instead of fitting partial words in the remaining space, they should place words which don't fit in the remaining space entirely on the next line. This change of policy can be requested in the overload of nextLayout which takes a boolean parameter. If this parameter is true, nextLayout returns null if the first word won't fit in the given space. See the tab sample below.

In general, if the text used to construct the LineBreakMeasurer changes, a new LineBreakMeasurer must be constructed to reflect the change. (The old LineBreakMeasurer continues to function properly, but it won't be aware of the text change.) Nevertheless, if the text change is the insertion or deletion of a single character, an existing LineBreakMeasurer can be 'updated' by calling insertChar or deleteChar. Updating an existing LineBreakMeasurer is much faster than creating a new one. Clients who modify text based on user typing should take advantage of these methods.

Examples:

Rendering a paragraph in a component

 public void paint(Graphics graphics) {
 Point2D pen = new Point2D(10, 20);
 Graphics2D g2d = (Graphics2D)graphics;
 FontRenderContext frc = g2d.getFontRenderContext();
 // let styledText be an AttributedCharacterIterator containing at least
 // one character
 LineBreakMeasurer measurer = new LineBreakMeasurer(styledText, frc);
 float wrappingWidth = getSize().width - 15;
 while (measurer.getPosition() < fStyledText.length()) {
 TextLayout layout = measurer.nextLayout(wrappingWidth);
 pen.y += (layout.getAscent());
 float dx = layout.isLeftToRight() ?
 0 : (wrappingWidth - layout.getAdvance());
 layout.draw(graphics, pen.x + dx, pen.y);
 pen.y += layout.getDescent() + layout.getLeading();
 }
 }
 

Rendering text with tabs. For simplicity, the overall text direction is assumed to be left-to-right

 public void paint(Graphics graphics) {
 float leftMargin = 10, rightMargin = 310;
 float[] tabStops = { 100, 250 };
 // assume styledText is an AttributedCharacterIterator, and the number
 // of tabs in styledText is tabCount
 int[] tabLocations = new int[tabCount+1];
 int i = 0;
 for (char c = styledText.first(); c != styledText.DONE; c = styledText.next()) {
 if (c == '\t') {
 tabLocations[i++] = styledText.getIndex();
 }
 }
 tabLocations[tabCount] = styledText.getEndIndex() - 1;
 // Now tabLocations has an entry for every tab's offset in
 // the text.  For convenience, the last entry is tabLocations
 // is the offset of the last character in the text.
 LineBreakMeasurer measurer = new LineBreakMeasurer(styledText);
 int currentTab = 0;
 float verticalPos = 20;
 while (measurer.getPosition() < styledText.getEndIndex()) {
 // Lay out and draw each line.  All segments on a line
 // must be computed before any drawing can occur, since
 // we must know the largest ascent on the line.
 // TextLayouts are computed and stored in a Vector;
 // their horizontal positions are stored in a parallel
 // Vector.
 // lineContainsText is true after first segment is drawn
 boolean lineContainsText = false;
 boolean lineComplete = false;
 float maxAscent = 0, maxDescent = 0;
 float horizontalPos = leftMargin;
 Vector layouts = new Vector(1);
 Vector penPositions = new Vector(1);
 while (!lineComplete) {
 float wrappingWidth = rightMargin - horizontalPos;
 TextLayout layout =
 measurer.nextLayout(wrappingWidth,
 tabLocations[currentTab]+1,
 lineContainsText);
 // layout can be null if lineContainsText is true
 if (layout != null) {
 layouts.addElement(layout);
 penPositions.addElement(new Float(horizontalPos));
 horizontalPos += layout.getAdvance();
 maxAscent = Math.max(maxAscent, layout.getAscent());
 maxDescent = Math.max(maxDescent,
 layout.getDescent() + layout.getLeading());
 } else {
 lineComplete = true;
 }
 lineContainsText = true;
 if (measurer.getPosition() == tabLocations[currentTab]+1) {
 currentTab++;
 }
 if (measurer.getPosition() == styledText.getEndIndex())
 lineComplete = true;
 else if (horizontalPos >= tabStops[tabStops.length-1])
 lineComplete = true;
 if (!lineComplete) {
 // move to next tab stop
 int j;
 for (j=0; horizontalPos >= tabStops[j]; j++) {}
 horizontalPos = tabStops[j];
 }
 }
 verticalPos += maxAscent;
 Enumeration layoutEnum = layouts.elements();
 Enumeration positionEnum = penPositions.elements();
 // now iterate through layouts and draw them
 while (layoutEnum.hasMoreElements()) {
 TextLayout nextLayout = (TextLayout) layoutEnum.nextElement();
 Float nextPosition = (Float) positionEnum.nextElement();
 nextLayout.draw(graphics, nextPosition.floatValue(), verticalPos);
 }
 verticalPos += maxDescent;
 }
 }
 

See Also:   TextLayout



Constructor Summary
public  LineBreakMeasurer(AttributedCharacterIterator text, FontRenderContext frc)
     Constructs a LineBreakMeasurer for the specified text.
public  LineBreakMeasurer(AttributedCharacterIterator text, BreakIterator breakIter, FontRenderContext frc)
     Constructs a LineBreakMeasurer for the specified text.

Method Summary
public  voiddeleteChar(AttributedCharacterIterator newParagraph, int deletePos)
     Updates this LineBreakMeasurer after a single character is deleted from the text, and sets the current position to the beginning of the paragraph.
public  intgetPosition()
     Returns the current position of this LineBreakMeasurer.
public  voidinsertChar(AttributedCharacterIterator newParagraph, int insertPos)
     Updates this LineBreakMeasurer after a single character is inserted into the text, and sets the current position to the beginning of the paragraph.
public  TextLayoutnextLayout(float wrappingWidth)
     Returns the next layout, and updates the current position.
public  TextLayoutnextLayout(float wrappingWidth, int offsetLimit, boolean requireNextWord)
     Returns the next layout, and updates the current position.
Parameters:
  wrappingWidth - the maximum visible advance permittedfor the text in the next layout
Parameters:
  offsetLimit - the first character that can not beincluded in the next layout, even if the text after the limitwould fit within the wrapping width; offsetLimit must be greater than the current position
Parameters:
  requireNextWord - if true, and if the entire wordat the current position does not fit within the wrapping width,null is returned.
public  intnextOffset(float wrappingWidth)
     Returns the position at the end of the next layout.
public  intnextOffset(float wrappingWidth, int offsetLimit, boolean requireNextWord)
     Returns the position at the end of the next layout.
public  voidsetPosition(int newPosition)
     Sets the current position of this LineBreakMeasurer.


Constructor Detail
LineBreakMeasurer
public LineBreakMeasurer(AttributedCharacterIterator text, FontRenderContext frc)(Code)
Constructs a LineBreakMeasurer for the specified text.
Parameters:
  text - the text for which this LineBreakMeasurerproduces TextLayout objects; the text must contain at least one character; if the text available through iter changes, further calls to this LineBreakMeasurer instance are undefined (except,in some cases, when insertChar or deleteChar are invoked afterward - see below)
Parameters:
  frc - contains information about a graphics device which is needed to measure the text correctly;text measurements can vary slightly depending on thedevice resolution, and attributes such as antialiasing; thisparameter does not specify a translation between theLineBreakMeasurer and user space
See Also:   LineBreakMeasurer.insertChar
See Also:   LineBreakMeasurer.deleteChar



LineBreakMeasurer
public LineBreakMeasurer(AttributedCharacterIterator text, BreakIterator breakIter, FontRenderContext frc)(Code)
Constructs a LineBreakMeasurer for the specified text.
Parameters:
  text - the text for which this LineBreakMeasurerproduces TextLayout objects; the text must contain at least one character; if the text available through iter changes, further calls to this LineBreakMeasurer instance are undefined (except,in some cases, when insertChar or deleteChar are invoked afterward - see below)
Parameters:
  breakIter - the BreakIterator which defines linebreaks
Parameters:
  frc - contains information about a graphics device which isneeded to measure the text correctly;text measurements can vary slightly depending on thedevice resolution, and attributes such as antialiasing; thisparameter does not specify a translation between theLineBreakMeasurer and user space
throws:
  IllegalArgumentException - if the text has less than one character
See Also:   LineBreakMeasurer.insertChar
See Also:   LineBreakMeasurer.deleteChar




Method Detail
deleteChar
public void deleteChar(AttributedCharacterIterator newParagraph, int deletePos)(Code)
Updates this LineBreakMeasurer after a single character is deleted from the text, and sets the current position to the beginning of the paragraph.
Parameters:
  newParagraph - the text after the deletion
Parameters:
  deletePos - the position in the text at which the characteris deleted
throws:
  IndexOutOfBoundsException - if deletePos isless than the start of newParagraph or greaterthan the end of newParagraph
throws:
  NullPointerException - if newParagraph isnull
See Also:   LineBreakMeasurer.insertChar



getPosition
public int getPosition()(Code)
Returns the current position of this LineBreakMeasurer. the current position of this LineBreakMeasurer
See Also:   LineBreakMeasurer.setPosition



insertChar
public void insertChar(AttributedCharacterIterator newParagraph, int insertPos)(Code)
Updates this LineBreakMeasurer after a single character is inserted into the text, and sets the current position to the beginning of the paragraph.
Parameters:
  newParagraph - the text after the insertion
Parameters:
  insertPos - the position in the text at which the characteris inserted
throws:
  IndexOutOfBoundsException - if insertPos is lessthan the start of newParagraph or greater thanor equal to the end of newParagraph
throws:
  NullPointerException - if newParagraph is null
See Also:   LineBreakMeasurer.deleteChar



nextLayout
public TextLayout nextLayout(float wrappingWidth)(Code)
Returns the next layout, and updates the current position.
Parameters:
  wrappingWidth - the maximum visible advance permitted forthe text in the next layout a TextLayout, beginning at the currentposition, which represents the next line fitting within wrappingWidth



nextLayout
public TextLayout nextLayout(float wrappingWidth, int offsetLimit, boolean requireNextWord)(Code)
Returns the next layout, and updates the current position.
Parameters:
  wrappingWidth - the maximum visible advance permittedfor the text in the next layout
Parameters:
  offsetLimit - the first character that can not beincluded in the next layout, even if the text after the limitwould fit within the wrapping width; offsetLimit must be greater than the current position
Parameters:
  requireNextWord - if true, and if the entire wordat the current position does not fit within the wrapping width,null is returned. If false, a validlayout is returned that includes at least the character at thecurrent position a TextLayout, beginning at the currentposition, that represents the next line fitting within wrappingWidth. If the current position is at the end of the text used by this LineBreakMeasurer,null is returned



nextOffset
public int nextOffset(float wrappingWidth)(Code)
Returns the position at the end of the next layout. Does NOT update the current position of this LineBreakMeasurer.
Parameters:
  wrappingWidth - the maximum visible advance permitted forthe text in the next layout an offset in the text representing the limit of thenext TextLayout.



nextOffset
public int nextOffset(float wrappingWidth, int offsetLimit, boolean requireNextWord)(Code)
Returns the position at the end of the next layout. Does NOT update the current position of this LineBreakMeasurer.
Parameters:
  wrappingWidth - the maximum visible advance permitted forthe text in the next layout
Parameters:
  offsetLimit - the first character that can not be includedin the next layout, even if the text after the limit would fitwithin the wrapping width; offsetLimit must begreater than the current position
Parameters:
  requireNextWord - if true, the current positionthat is returned if the entire next word does not fit withinwrappingWidth; if false, the offsetreturned is at least one greater than the current position an offset in the text representing the limit of thenext TextLayout



setPosition
public void setPosition(int newPosition)(Code)
Sets the current position of this LineBreakMeasurer.
Parameters:
  newPosition - the current position of thisLineBreakMeasurer; the position should be within thetext used to construct this LineBreakMeasurer (or inthe text most recently passed to insertCharor deleteChar
See Also:   LineBreakMeasurer.getPosition



Methods inherited from java.lang.Object
native protected Object clone() throws CloneNotSupportedException(Code)(Java Doc)
public boolean equals(Object obj)(Code)(Java Doc)
protected void finalize() throws Throwable(Code)(Java Doc)
final native public Class getClass()(Code)(Java Doc)
native public int hashCode()(Code)(Java Doc)
final native public void notify()(Code)(Java Doc)
final native public void notifyAll()(Code)(Java Doc)
public String toString()(Code)(Java Doc)
final native public void wait(long timeout) throws InterruptedException(Code)(Java Doc)
final public void wait(long timeout, int nanos) throws InterruptedException(Code)(Java Doc)
final public void wait() throws InterruptedException(Code)(Java Doc)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.