edu.northwestern.at.wordhoard.model.text
Class TextRun

java.lang.Object
  extended by edu.northwestern.at.wordhoard.model.text.TextRun
All Implemented Interfaces:
java.lang.Cloneable

public class TextRun
extends java.lang.Object
implements java.lang.Cloneable

A styled run of text.

A run is a string of text plus character set, font size, and style attributes.

The style attributes are:

Runs can also contain font information, which is set and used in the client to facilitate word wrapping and presentation. See WrappedText and FontManager for more details.

Runs can also contain color information, which is also set and used in the client.


Field Summary
static int BOLD
          Bold style mask.
static int EXTENDED
          Extended style mask.
static int ITALIC
          Italic style mask.
static int MONOSPACED
          Monospaced style mask.
static int OVERLINE
          Overline style mask.
static int SUBSCRIPT
          Subscript style mask.
static int SUPERSCRIPT
          Superscript style mask.
static int UNDERLINE
          Underline style mask.
 
Constructor Summary
TextRun()
          Creates a new empty run.
TextRun(java.lang.String text, byte charset)
          Creates a new run.
TextRun(java.lang.String text, byte charset, byte size)
          Creates a new run.
TextRun(java.lang.String text, byte charset, byte size, boolean bold, boolean italic)
          Creates a new run.
TextRun(java.lang.String text, FontInfo fontInfo)
          Creates a new run with font information.
 
Method Summary
 java.lang.Object clone()
          Clones the run.
(package private)  void draw(DrawingContext context, int x, int y, int selStartOffsetInRun, int selEndOffsetInRun)
          Draws the run.
 byte getCharset()
          Gets the character set.
 java.awt.Color getColor()
          Gets the color of the run.
 FontInfo getFontInfo()
          Gets the font information for the run.
 int getLength()
          Gets the length of the run in characters.
 byte getSize()
          Gets the font size.
 int getStyle()
          Gets the style attributes.
 int getSubstringWidth(int start, int end)
          Gets the width of a substring of the run.
 java.lang.String getText()
          Gets the text of the run.
 int getWidth()
          Gets the width of the the run in pixels.
 boolean isBold()
          Returns true if bold.
 boolean isEmpty()
          Returns true if the run is empty.
 boolean isExtended()
          Returns true if extended.
 boolean isItalic()
          Returns true if italic.
 boolean isMonospaced()
          Returns true if monospaced.
 boolean isOverline()
          Returns true if overline.
 boolean isSubscript()
          Returns true if subscript.
 boolean isSuperscript()
          Returns true if superscript.
 boolean isUnderline()
          Returns true if underline.
 void readExternal(java.io.ObjectInput in)
          Reads the run from an object input stream (deserializes the object).
 void setBold()
          Sets the bold style attribute.
 void setCharset(byte charset)
          Sets the character set.
 void setColor(java.awt.Color color)
          Sets the color of the run.
 void setExtended()
          Sets the extended style attribute.
 void setFontInfo(FontInfo fontInfo)
          Sets the font information for the run.
 void setItalic()
          Sets the italic style attribute.
 void setMonospaced()
          Sets the monospaced style attribute.
 void setOverline()
          Sets the overline style attribute.
 void setSize(byte size)
          Sets the font size.
 void setStyle(int style)
          Sets the style attributes.
 void setSubscript()
          Sets the subscript style attribute.
 void setSuperscript()
          Sets the superscript style attribute.
 void setText(java.lang.String text)
          Sets the text of the run.
 void setUnderline()
          Sets the underline style attribute.
 void setXOffset(int xOffset)
          Sets the X offset of the run.
 void setYOffset(int yOffset)
          Sets the Y offset of the run.
(package private)  int viewToModel(DrawingContext context, int x)
          Converts a point to a character offset.
 void writeExternal(java.io.ObjectOutput out)
          Writes the run to an object output stream (serializes the object).
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BOLD

public static final int BOLD
Bold style mask.

See Also:
Constant Field Values

ITALIC

public static final int ITALIC
Italic style mask.

See Also:
Constant Field Values

EXTENDED

public static final int EXTENDED
Extended style mask.

See Also:
Constant Field Values

UNDERLINE

public static final int UNDERLINE
Underline style mask.

See Also:
Constant Field Values

OVERLINE

public static final int OVERLINE
Overline style mask.

See Also:
Constant Field Values

SUPERSCRIPT

public static final int SUPERSCRIPT
Superscript style mask.

See Also:
Constant Field Values

SUBSCRIPT

public static final int SUBSCRIPT
Subscript style mask.

See Also:
Constant Field Values

MONOSPACED

public static final int MONOSPACED
Monospaced style mask.

See Also:
Constant Field Values
Constructor Detail

TextRun

public TextRun(java.lang.String text,
               byte charset,
               byte size)
Creates a new run.

The new run is plain (no style attributes).

Parameters:
text - Text.
charset - Character set.
size - Font size.

TextRun

public TextRun(java.lang.String text,
               byte charset,
               byte size,
               boolean bold,
               boolean italic)
Creates a new run.

Parameters:
text - Text.
charset - Character set.
size - Font size.
bold - True if bold.
italic - True if italic.

TextRun

public TextRun(java.lang.String text,
               byte charset)
Creates a new run.

The new run has size 0 and is plain.

Parameters:
text - Text.
charset - Character set.

TextRun

public TextRun(java.lang.String text,
               FontInfo fontInfo)
Creates a new run with font information.

The new run has size 0, is plain, and uses the Roman character set.

Parameters:
text - Text.
fontInfo - Font information.

TextRun

public TextRun()
Creates a new empty run.

Method Detail

getText

public java.lang.String getText()
Gets the text of the run.

Returns:
The text of the run.

setText

public void setText(java.lang.String text)
Sets the text of the run.

Parameters:
text - The text of the run.

getCharset

public byte getCharset()
Gets the character set.

Returns:
Character set.

setCharset

public void setCharset(byte charset)
Sets the character set.

Parameters:
charset - The character set.

getSize

public byte getSize()
Gets the font size.

Returns:
The font size.

setSize

public void setSize(byte size)
Sets the font size.

Parameters:
size - The font size.

isBold

public boolean isBold()
Returns true if bold.

Returns:
True if bold.

setBold

public void setBold()
Sets the bold style attribute.


isItalic

public boolean isItalic()
Returns true if italic.

Returns:
True if italic.

setItalic

public void setItalic()
Sets the italic style attribute.


isExtended

public boolean isExtended()
Returns true if extended.

Returns:
True if extended.

setExtended

public void setExtended()
Sets the extended style attribute.


isUnderline

public boolean isUnderline()
Returns true if underline.

Returns:
True if underline.

setUnderline

public void setUnderline()
Sets the underline style attribute.


isOverline

public boolean isOverline()
Returns true if overline.

Returns:
True if overline.

setOverline

public void setOverline()
Sets the overline style attribute.


isSuperscript

public boolean isSuperscript()
Returns true if superscript.

Returns:
True if superscript.

setSuperscript

public void setSuperscript()
Sets the superscript style attribute.


isSubscript

public boolean isSubscript()
Returns true if subscript.

Returns:
True if subscript.

setSubscript

public void setSubscript()
Sets the subscript style attribute.


isMonospaced

public boolean isMonospaced()
Returns true if monospaced.

Returns:
True if monospaced.

setMonospaced

public void setMonospaced()
Sets the monospaced style attribute.


isEmpty

public boolean isEmpty()
Returns true if the run is empty.

Returns:
True if run is empty (all whitespace).

getStyle

public int getStyle()
Gets the style attributes.

Returns:
Style attributes.

setStyle

public void setStyle(int style)
Sets the style attributes.

Parameters:
style - Style attributes.

getLength

public int getLength()
Gets the length of the run in characters.

Returns:
Length of the run in characters.

getFontInfo

public FontInfo getFontInfo()
Gets the font information for the run.

Returns:
The font information for the run, or null if not yet set.

setFontInfo

public void setFontInfo(FontInfo fontInfo)
Sets the font information for the run.

This method also computes and sets the width of the run.

Parameters:
fontInfo - The font information.

getSubstringWidth

public int getSubstringWidth(int start,
                             int end)
Gets the width of a substring of the run.

Parameters:
start - Start index of substring.
end - End index of substring.

getColor

public java.awt.Color getColor()
Gets the color of the run.

Returns:
The color of the run (default black).

setColor

public void setColor(java.awt.Color color)
Sets the color of the run.

Parameters:
color - The color of the run.

getWidth

public int getWidth()
Gets the width of the the run in pixels.

Returns:
The width of the run in pixels, or -1 if not yet set.

setXOffset

public void setXOffset(int xOffset)
Sets the X offset of the run.

Parameters:
xOffset - Offset from left edge in pixels.

setYOffset

public void setYOffset(int yOffset)
Sets the Y offset of the run.

Parameters:
yOffset - Offset from baseline in pixels.

clone

public java.lang.Object clone()
Clones the run.

The clone is shallow - the font information is not cloned.

Overrides:
clone in class java.lang.Object
Returns:
A clone of the run.

writeExternal

public void writeExternal(java.io.ObjectOutput out)
                   throws java.io.IOException
Writes the run to an object output stream (serializes the object).

Parameters:
out - Object output stream.
Throws:
java.io.IOException

readExternal

public void readExternal(java.io.ObjectInput in)
                  throws java.io.IOException,
                         java.lang.ClassNotFoundException
Reads the run from an object input stream (deserializes the object).

Parameters:
in - Object input stream.
Throws:
java.io.IOException
java.lang.ClassNotFoundException

draw

void draw(DrawingContext context,
          int x,
          int y,
          int selStartOffsetInRun,
          int selEndOffsetInRun)
Draws the run.

The font information must be set.

Parameters:
context - Drawing context.
x - X coordinate of left edge of run.
y - Y coordinate of baseline of run.
selStartOffsetInRun - Selection start offset in run.
selEndOffsetInRun - Selection end offset in run.

viewToModel

int viewToModel(DrawingContext context,
                int x)
Converts a point to a character offset.

Parameters:
context - Drawing context.
x - X coordinate of point in run.
Returns:
Character offset in run.