org.writersforge.catalan.layout
Class StyledText

java.lang.Object
  extended byorg.writersforge.catalan.layout.StyledText

public class StyledText
extends java.lang.Object

A lightweight container for styled text. Used by the renderer and managed by LayoutArea objects.

Author:
jsheets

Nested Class Summary
static class StyledText.Span
          A single homogeneous span of markup style.
 
Constructor Summary
StyledText()
          Creates a new default instance of StyledText.
StyledText(java.lang.String text)
          Creates a new instance of StyledText with the given unstyled text.
StyledText(java.lang.String text, TextStyle textStyle)
          Creates a new instance of StyledText with the given text and style.
StyledText(TextStyle textStyle)
          Creates a new instance of StyledText with the given style.
 
Method Summary
 void addMarkup(FontStyle style, int start, int end)
          Adds Style markup across a given range.
 void addText(java.lang.String text)
          Appends the given text to the current text using the default style.
 void addText(java.lang.String text, FontStyle style)
          Appends the given text to the current text using the given Style.
 void clear()
          Removes all text and markup.
 FontStyle getFontStyle()
          Returns the font style of this object
 Markup[] getMarkup()
          Getter for property markup.
 StyledText.Span[] getSpans()
          Returns an array of text spans that delimit all changes in text style, i.e., a list of discrete markup regions.
 java.lang.String getText()
          Returns a plain text version of the text contents.
 int getTextHeight(int width)
          Calculates the vertical extents of the entire run of text, if the text were wrapped to the given width.
 int getTextLength()
          Returns the total count of text characters.
 TextStyle getTextStyle()
          Returns the text style of this object
 java.lang.String[] wrapText(int width)
          Performs line-wrapping on the text contents.
 java.lang.String[] wrapText(int width, int start, int end)
          Performs line-wrapping on the text contents.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StyledText

public StyledText()
Creates a new default instance of StyledText.


StyledText

public StyledText(java.lang.String text)
Creates a new instance of StyledText with the given unstyled text.

Parameters:
text - the text content

StyledText

public StyledText(TextStyle textStyle)
Creates a new instance of StyledText with the given style.

Parameters:
textStyle - the style of text in this object

StyledText

public StyledText(java.lang.String text,
                  TextStyle textStyle)
Creates a new instance of StyledText with the given text and style.

Parameters:
text - the text content
textStyle - the style of text in this object
Method Detail

getTextStyle

public TextStyle getTextStyle()
Returns the text style of this object

Returns:
the text style

getFontStyle

public FontStyle getFontStyle()
Returns the font style of this object

Returns:
the font style

clear

public void clear()
Removes all text and markup.


getText

public java.lang.String getText()
Returns a plain text version of the text contents.

Returns:
plain text contents

getTextLength

public int getTextLength()
Returns the total count of text characters.

Returns:
text length

addText

public void addText(java.lang.String text)
Appends the given text to the current text using the default style. Can use the addMarkup() method later to add styles to existing text.

Parameters:
text - text to append

addText

public void addText(java.lang.String text,
                    FontStyle style)
Appends the given text to the current text using the given Style. Implicitly calls addMarkup() with the given Style for the added range of text.

Parameters:
text - text to append
style - style for the appended text

getTextHeight

public int getTextHeight(int width)
Calculates the vertical extents of the entire run of text, if the text were wrapped to the given width.

Parameters:
width - how wide to wrap the text, in font points
Returns:
the height of the wrapped text

getMarkup

public Markup[] getMarkup()
Getter for property markup. Allows access to the markup array as a whole.

Returns:
the raw array of Markup objects for this span of text

addMarkup

public void addMarkup(FontStyle style,
                      int start,
                      int end)
Adds Style markup across a given range. If the range overflows the current text, the ranges will not be truncated to match it.

Parameters:
style - markup style
start - starting index of markup along the text
end - ending index of markup along the text

getSpans

public StyledText.Span[] getSpans()
Returns an array of text spans that delimit all changes in text style, i.e., a list of discrete markup regions.

Returns:
an array of markup spans

wrapText

public java.lang.String[] wrapText(int width)
Performs line-wrapping on the text contents. Returns the entire text contents broken up into an array of String objects. Leaves the Markup untouched, and does not remove any whitespace, so the span info can still be applied to the wrapped lines as long as index offsets are factored in. Use the wordWrap property to control whether the wrapping breaks apart words or not.

Parameters:
width - the maximum width of the wrapped text, in font point units
Returns:
an array of String objects, one per line of wrapped text

wrapText

public java.lang.String[] wrapText(int width,
                                   int start,
                                   int end)
Performs line-wrapping on the text contents. Returns a fragment of the text contents broken up into an array of String objects. Leaves the Markup untouched, and does not remove any whitespace, so the span info can still be applied to the wrapped lines as long as index offsets are factored in. The Markup spans of course still point to the full text content, so some extra diligence may be needed to keep track of everything. Use the wordWrap property to control whether the wrapping breaks apart words or not.

Parameters:
width - the maximum width of the wrapped text, in font point units
start - the starting offset into the StyledText content
end - the end offset into the StyledText content
Returns:
an array of String objects, one per line of wrapped text