org.eclipse.swt.custom
Class StyledTextRenderer

java.lang.Object
  |
  +--org.eclipse.swt.custom.StyledTextRenderer
Direct Known Subclasses:
DisplayRenderer, PrintRenderer

abstract class StyledTextRenderer
extends Object


Field Summary
protected  Font boldFont
           
private  int currentFontStyle
           
private  Device device
           
private  boolean isBidi
           
private  int leftMargin
           
private  int lineEndSpaceWidth
           
private  int lineHeight
           
protected  Font regularFont
           
private  int tabLength
           
private  int tabWidth
           
 
Constructor Summary
(package private) StyledTextRenderer(Device device, Font regularFont, boolean isBidi, int leftMargin)
          Creates an instance of StyledTextRenderer.
 
Method Summary
(package private)  void calculateLineHeight()
          Calculates the line height and space width.
(package private)  void dispose()
          Disposes the resource created by the receiver.
protected abstract  void disposeGC(GC gc)
          Dispose the specified GC.
(package private)  void drawLine(String line, int lineIndex, int paintY, GC gc, Color widgetBackground, Color widgetForeground, boolean clearBackground)
          Draws a line of text at the specified location.
protected abstract  void drawLineSelectionBackground(String line, int lineOffset, StyleRange[] styles, int paintY, GC gc, StyledTextBidi bidi)
          Draws the background of the line selection.
private  void drawStyledLine(String line, int lineOffset, int renderOffset, StyleRange[] styles, int paintX, int paintY, GC gc, Color lineBackground, Color lineForeground, StyledTextBidi bidi)
          Draws the line at the specified location.
private  int drawText(String text, int startOffset, int length, int paintX, int paintY, GC gc, StyledTextBidi bidi)
          Draws the text at the specified location.
(package private)  StyleRange[] filterLineStyles(StyleRange[] styles)
          Filter the given style ranges based on the font style and return the unchanged styles only if there is at least one non-regular (e.g., bold) font.
protected abstract  int[] getBidiSegments(int lineOffset, String lineText)
          Returns the text segments that should be treated as if they had a different direction than the surrounding text.
protected abstract  Rectangle getClientArea()
          Returns the visible client area that can be used for rendering.
protected abstract  StyledTextContent getContent()
          Returns the StyledTextContent to use for line offset calculations.
protected  int getCurrentFontStyle()
          Return the font style that is currently set on the active gc.
(package private)  Device getDevice()
          Returns the Device that is being rendered on.
private  StyleRange[] getFontStyleRanges(StyleRange[] styles, int lineOffset, int lineLength)
          Returns an array of text ranges that have a font style specified (e.g., SWT.BOLD).
protected abstract  GC getGC()
          Returns the GC to use for rendering and measuring.
protected abstract  int getHorizontalPixel()
          Returns the horizontal scroll position.
(package private)  int getLeftMargin()
          Method getLeftMargin.
protected abstract  StyledTextEvent getLineBackgroundData(int lineOffset, String line)
          Returns the line background data for the given line or null if there is none.
(package private)  int getLineEndSpaceWidth()
          Returns the width in pixels of the space used to represent line delimiters.
(package private)  int getLineHeight()
          Returns the height in pixels of a line.
protected abstract  StyledTextEvent getLineStyleData(int lineOffset, String line)
          Returns the line style data for the given line or null if there is none.
(package private)  StyledTextEvent getLineStyleData(StyledTextEvent event, int lineOffset, String line)
          Returns the line style data for the specified line.
protected abstract  Point getSelection()
          Returns the widget selection.
(package private)  StyledTextBidi getStyledTextBidi(String lineText, int lineOffset, GC gc, StyleRange[] styles)
          Returns a StyledTextBidi object for the specified line.
protected abstract  int getStyledTextWidth(String text, int textStartOffset, StyleRange[] lineStyles, int paintX, GC gc)
          Returns the width of the specified text segment.
private  int getTabStop(int x)
          Returns the next tab stop for the specified x location.
(package private)  int getTextPosition(String text, int lineOffset, int length, StyleRange[] lineStyles, GC gc)
          Returns the x position of the character at the specified offset relative to the first character in the line.
(package private)  int getTextWidth(String text, int lineOffset, int startOffset, int length, StyleRange[] lineStyles, int startXOffset, GC gc)
          Returns the width of the specified text range.
(package private)  StyleRange[] getVisualLineStyleData(StyleRange[] logicalStyles, int lineOffset, int lineLength)
          Returns styles for the specified visual (wrapped) line.
protected abstract  boolean getWordWrap()
          Returns the word wrap state.
(package private)  boolean isBidi()
          Returns whether the widget is running on a bidi platform.
protected abstract  boolean isFullLineSelection()
          Returns whether the widget was created with the SWT.FULL_SELECTION style.
protected abstract  StyleRange[] mergeSelectionLineStyles(StyleRange[] styles)
          Merges the selection into the styles that are passed in.
protected  void setCurrentFontStyle(int style)
          Set the font style that is set for the active gc.
private  Color setLineBackground(GC gc, Color currentBackground, Color newBackground)
          Sets the background of the specified GC for a line rendering operation, if it is not already set.
(package private)  void setLineFont(GC gc, int style)
          Sets the font of the specified GC if it is not already set.
private  Color setLineForeground(GC gc, Color currentForeground, Color newForeground)
          Sets the foreground of the specified GC for a line rendering operation, if it is not already set.
(package private)  void setTabLength(int tabLength)
          Calculates the width in pixel of a tab character
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

device

private Device device

regularFont

protected Font regularFont

boldFont

protected Font boldFont

isBidi

private boolean isBidi

leftMargin

private int leftMargin

tabLength

private int tabLength

tabWidth

private int tabWidth

lineHeight

private int lineHeight

lineEndSpaceWidth

private int lineEndSpaceWidth

currentFontStyle

private int currentFontStyle
Constructor Detail

StyledTextRenderer

StyledTextRenderer(Device device,
                   Font regularFont,
                   boolean isBidi,
                   int leftMargin)
Creates an instance of StyledTextRenderer.

Parameters:
device - Device to render on
regularFont - Font to use for regular (non-bold) text
isBidi - true=bidi platform, false=no bidi platform
leftMargin - margin to the left of the text
Method Detail

calculateLineHeight

void calculateLineHeight()
Calculates the line height and space width.


dispose

void dispose()
Disposes the resource created by the receiver.


disposeGC

protected abstract void disposeGC(GC gc)
Dispose the specified GC. Allows subclasses to reuse GCs.

Parameters:
gc - GC to dispose.

drawLine

void drawLine(String line,
              int lineIndex,
              int paintY,
              GC gc,
              Color widgetBackground,
              Color widgetForeground,
              boolean clearBackground)
Draws a line of text at the specified location.

Parameters:
line - the line to draw
lineIndex - index of the line to draw
paintY - y location to draw at
gc - GC to draw on
widgetBackground - the widget background color. Used as the default rendering color.
widgetForeground - the widget foreground color. Used as the default rendering color.
clearBackground - true if the line background should be drawn explicitly.

drawLineSelectionBackground

protected abstract void drawLineSelectionBackground(String line,
                                                    int lineOffset,
                                                    StyleRange[] styles,
                                                    int paintY,
                                                    GC gc,
                                                    StyledTextBidi bidi)
Draws the background of the line selection. Implemented by subclasses for optional selection rendering.

Parameters:
line - the line to draw
lineOffset - offset of the first character in the line. Relative to the start of the document.
styles - line styles
paintY - y location to draw at
gc - GC to draw on
bidi - the bidi object to use for measuring and rendering text in bidi locales. null when not in bidi mode.

drawStyledLine

private void drawStyledLine(String line,
                            int lineOffset,
                            int renderOffset,
                            StyleRange[] styles,
                            int paintX,
                            int paintY,
                            GC gc,
                            Color lineBackground,
                            Color lineForeground,
                            StyledTextBidi bidi)
Draws the line at the specified location.

Parameters:
line - the line to draw
lineOffset - offset of the first character in the line. Relative to the start of the document.
renderOffset - offset of the first character that should be rendered. Relative to the start of the line.
styles - the styles to use for rendering line segments. May be empty but not null.
paintX - x location to draw at, not used in bidi mode
paintY - y location to draw at
gc - GC to draw on
lineBackground - line background color, used when no style is specified for a line segment.
lineForeground - line foreground color, used when no style is specified for a line segment.
bidi - the bidi object to use for measuring and rendering text in bidi locales. null when not in bidi mode.

drawText

private int drawText(String text,
                     int startOffset,
                     int length,
                     int paintX,
                     int paintY,
                     GC gc,
                     StyledTextBidi bidi)
Draws the text at the specified location. Expands tabs to tab stops using the widget tab width.

Parameters:
text - text to draw
startOffset - offset of the first character in text to draw
length - number of characters to draw
paintX - x location to start drawing at, not used in bidi mode
paintY - y location to draw at. Unused when draw is false
gc - GC to draw on location where drawing would stop
bidi - the bidi object to use for measuring and rendering text in bidi locales. null when not in bidi mode.
Returns:
x location where drawing stopped or 0 if the startOffset or length is outside the specified text. In bidi mode this value is the same as the paintX input parameter.

filterLineStyles

StyleRange[] filterLineStyles(StyleRange[] styles)
Filter the given style ranges based on the font style and return the unchanged styles only if there is at least one non-regular (e.g., bold) font.

Parameters:
styles - styles that may contain font styles.
Returns:
null if the styles contain only regular font styles, the unchanged styles otherwise.

getClientArea

protected abstract Rectangle getClientArea()
Returns the visible client area that can be used for rendering.

Returns:
the visible client area that can be used for rendering.

getCurrentFontStyle

protected int getCurrentFontStyle()
Return the font style that is currently set on the active gc. Font style cached for performance reasons.

Returns:
int SWT.BOLD, SWT.NORMAL

getContent

protected abstract StyledTextContent getContent()
Returns the StyledTextContent to use for line offset calculations.

Returns:
the StyledTextContent to use for line offset calculations.

getDevice

Device getDevice()
Returns the Device that is being rendered on.

Returns:
the Device that is being rendered on.

getFontStyleRanges

private StyleRange[] getFontStyleRanges(StyleRange[] styles,
                                        int lineOffset,
                                        int lineLength)
Returns an array of text ranges that have a font style specified (e.g., SWT.BOLD).

Parameters:
styles - style ranges in the line
lineOffset - start index of the line, relative to the start of the document
Returns:
StyleRange[], array of ranges with a font style specified, null if styles parameter is null

getBidiSegments

protected abstract int[] getBidiSegments(int lineOffset,
                                         String lineText)
Returns the text segments that should be treated as if they had a different direction than the surrounding text.

Parameters:
lineOffset - offset of the first character in the line. 0 based from the beginning of the document.
Returns:
text segments that should be treated as if they had a different direction than the surrounding text. Only the start index of a segment is specified, relative to the start of the line. Always starts with 0 and ends with the line length.

getGC

protected abstract GC getGC()
Returns the GC to use for rendering and measuring. Allows subclasses to reuse GCs.

Returns:
the GC to use for rendering and measuring.

getHorizontalPixel

protected abstract int getHorizontalPixel()
Returns the horizontal scroll position.

Returns:
the horizontal scroll position.

getLeftMargin

int getLeftMargin()
Method getLeftMargin.

Returns:
int

getLineEndSpaceWidth

int getLineEndSpaceWidth()
Returns the width in pixels of the space used to represent line delimiters.

Returns:
the width in pixels of the space used to represent line delimiters.

getLineBackgroundData

protected abstract StyledTextEvent getLineBackgroundData(int lineOffset,
                                                         String line)
Returns the line background data for the given line or null if there is none.

Parameters:
lineOffset - offset of the line start relative to the start of the content.
line - line to get line background data for
Returns:
line background data for the given line. may return null

getLineHeight

int getLineHeight()
Returns the height in pixels of a line.

Returns:
the height in pixels of a line.

getLineStyleData

StyledTextEvent getLineStyleData(StyledTextEvent event,
                                 int lineOffset,
                                 String line)
Returns the line style data for the specified line. The lineOffset and line may specify a segment of a logical line stored in the StyledTextContent of the widget. The returned styles are guaranteed to be at least partially on the segment.

Parameters:
event - the styles for the logical line
lineOffset - offset of the line start relative to the start of the content.
line - line to get line styles for
Returns:
line style data for the given line segment. Styles may start before line start and end after line end but are guaranteed to be at least partially on the line.

getLineStyleData

protected abstract StyledTextEvent getLineStyleData(int lineOffset,
                                                    String line)
Returns the line style data for the given line or null if there is none. If there is a LineStyleListener but it does not set any styles, the StyledTextEvent.styles field will be initialized to an empty array.

Parameters:
lineOffset - offset of the line start relative to the start of the content.
line - line to get line styles for
Returns:
line style data for the given line. Styles may start before line start and end after line end

getSelection

protected abstract Point getSelection()
Returns the widget selection. Implemented by subclasses for optional selection rendering.

Returns:
the widget selection.

mergeSelectionLineStyles

protected abstract StyleRange[] mergeSelectionLineStyles(StyleRange[] styles)
Merges the selection into the styles that are passed in. The font style of existing style ranges is preserved in the selection. Implemented by subclasses for optional selection rendering.

Parameters:
styles - the existing styles that the selection should be applied to.
Returns:
the selection style range merged with the existing styles

getStyledTextBidi

StyledTextBidi getStyledTextBidi(String lineText,
                                 int lineOffset,
                                 GC gc,
                                 StyleRange[] styles)
Returns a StyledTextBidi object for the specified line.

Parameters:
lineText - the line that the StyledTextBidi object should work on.
lineOffset - offset of the beginning of the line, relative to the beginning of the document
gc - GC to use when creating a new StyledTextBidi object.
styles - StyleRanges to use when creating a new StyledTextBidi object.
Returns:
a StyledTextBidi object for the specified line.

getStyledTextWidth

protected abstract int getStyledTextWidth(String text,
                                          int textStartOffset,
                                          StyleRange[] lineStyles,
                                          int paintX,
                                          GC gc)
Returns the width of the specified text segment. Expands tabs to tab stops using the widget tab width.

Parameters:
text - text to measure
textStartOffset - offset of the first character in text relative to the first character in the document
lineStyles - styles of the line
paintX - x location to start drawing at
gc - GC to measure with
Returns:
the width of the specified text segment.

getTabStop

private int getTabStop(int x)
Returns the next tab stop for the specified x location.

Parameters:
x - the x location in front of a tab
Returns:
the next tab stop for the specified x location.

getTextPosition

int getTextPosition(String text,
                    int lineOffset,
                    int length,
                    StyleRange[] lineStyles,
                    GC gc)
Returns the x position of the character at the specified offset relative to the first character in the line. Expands tabs to tab stops using the widget tab width.

Parameters:
text - text to be measured.
lineOffset - offset of the first character in the line.
length - number of characters to measure. Tabs are counted as one character in this parameter.
gc - GC to use for measuring text
Returns:
x position of the character at the specified offset with tabs expanded to tab stops. 0 if the length is outside the specified text.

getTextWidth

int getTextWidth(String text,
                 int lineOffset,
                 int startOffset,
                 int length,
                 StyleRange[] lineStyles,
                 int startXOffset,
                 GC gc)
Returns the width of the specified text range. Expand tabs to tab stops using the widget tab width.

Parameters:
text - text to be measured.
lineOffset - offset of the first character in the line.
startOffset - offset of the character to start measuring and expand tabs.
length - number of characters to measure. Tabs are counted as one character in this parameter.
startXOffset - x position of "startOffset" in "text". Used for calculating tab stops
gc - GC to use for measuring text
Returns:
width of the text range with tabs expanded to tab stops or 0 if the startOffset or length is outside the specified text.

getVisualLineStyleData

StyleRange[] getVisualLineStyleData(StyleRange[] logicalStyles,
                                    int lineOffset,
                                    int lineLength)
Returns styles for the specified visual (wrapped) line.

Parameters:
logicalStyles - the styles for a logical (unwrapped) line
lineOffset - offset of the visual line
lineLength - length of the visual line
Returns:
styles in the logicalStyles array that are at least partially on the specified visual line.

getWordWrap

protected abstract boolean getWordWrap()
Returns the word wrap state.

Returns:
true=word wrap is on. false=no word wrap, lines may extend beyond the right side of the client area.

isBidi

boolean isBidi()
Returns whether the widget is running on a bidi platform.

Returns:
true=the widget is running on a bidi platform, false=otherwise.

isFullLineSelection

protected abstract boolean isFullLineSelection()
Returns whether the widget was created with the SWT.FULL_SELECTION style. Implemented by subclasses for optional selection rendering.

Returns:
true=the widget is running in full line selection mode, false=otherwise

setCurrentFontStyle

protected void setCurrentFontStyle(int style)
Set the font style that is set for the active gc. Font style cached for performance reasons.

Parameters:
style - SWT.BOLD, SWT.NORMAL

setLineBackground

private Color setLineBackground(GC gc,
                                Color currentBackground,
                                Color newBackground)
Sets the background of the specified GC for a line rendering operation, if it is not already set.

Parameters:
gc - GC to set the background color in
currentBackground - background color currently set in gc
newBackground - new background color of gc

setLineFont

void setLineFont(GC gc,
                 int style)
Sets the font of the specified GC if it is not already set.

Parameters:
gc - GC to set the font in
style - desired style of the font in gc. Can be one of SWT.NORMAL, SWT. BOLD

setLineForeground

private Color setLineForeground(GC gc,
                                Color currentForeground,
                                Color newForeground)
Sets the foreground of the specified GC for a line rendering operation, if it is not already set.

Parameters:
gc - GC to set the foreground color in
currentForeground - foreground color currently set in gc
newForeground - new foreground color of gc

setTabLength

void setTabLength(int tabLength)
Calculates the width in pixel of a tab character

Parameters:
tabLength - number of space characters represented by a tab character.


comments?