Class MultiLineTextRenderer
- java.lang.Object
-
- gov.nasa.worldwind.render.MultiLineTextRenderer
-
public class MultiLineTextRenderer extends java.lang.Object
Multi line, rectangle bound text renderer with (very) minimal html support.The
MultiLineTextRenderer
(MLTR) handles wrapping, measuring and drawing of multiline text strings using Sun's JOGLTextRenderer
.A multiline text string is a character string containing new line characters in between lines.
MLTR can handle both regular text with new line separators and a very minimal implementation of HTML. Each type of text has its own methods though.
Usage:
Instantiation:
The MLTR needs a Font or a TextRenderer to be instantiated. This will be the font used for text drawing, wrapping and measuring. For HTML methods this font will be considered as the document default font.
Font font = Font.decode("Arial-PLAIN-12"); MultiLineTextRenderer mltr = new MultiLineTextRenderer(font);
orTextRenderer tr = new TextRenderer(Font.decode("Arial-PLAIN-10")); MultiLineTextRenderer mltr = new MultiLineTextRenderer(tr);
Drawing regular text:
String text = "Line one.\nLine two.\nLine three..."; int x = 10; // Upper left corner of text rectangle. int y = 200; // Origin at bottom left of screen. int lineHeight = 14; // Line height in pixels. Color color = Color.RED; mltr.setTextColor(color); mltr.getTextRenderer().begin3DRendering(); mltr.draw(text, x, y, lineHeight); mltr.getTextRenderer().end3DRendering();
Wrapping text to fit inside a width and optionally a height
The MLTR wrap method will insert new line characters inside the text so that it fits a given width in pixels.
If a height dimension above zero is specified too, the text will be truncated if needed, and a continuation string will be appended to the last line. The continuation string can be set with mltr.setContinuationString();
// Fit inside 300 pixels, no height constraint String wrappedText = mltr.wrap(text, new Dimension(300, 0)); // Fit inside 300x400 pixels, text may be truncated String wrappedText = mltr.wrap(text, new Dimension(300, 400));
Measuring text
Rectangle2D textBounds = mltr.getBounds(text);
The textBounds rectangle returned contains the width and height of the text as it would be drawn with the current font.
Note that textBounds.minX is the number of lines found and textBounds.minY is the maximum line height for the font used. This value can be safely used as the lineHeight argument when drawing - or can even be ommited after a getBounds: draw(text, x, y); ...
HTML support
Supported tags are:
- <p></p>, <br> <br />
- <b></b>
- <i></i>
- <a href="..."></a>
- <font color="#ffffff"></font>
See
AbstractAnnotation
.drawAnnotation() for more usage details.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected class
MultiLineTextRenderer.DrawState
static class
MultiLineTextRenderer.WordIteratorHTML
Iterates through words from an HTML text line.
-
Field Summary
Fields Modifier and Type Field Description protected java.awt.Color
backColor
protected java.lang.String
continuationString
protected DrawContext
drawContext
protected boolean
isPicking
protected int
lineHeight
protected int
lineSpacing
protected java.awt.Color
linkColor
protected java.lang.Object
pickObject
protected Position
pickPosition
protected PickSupport
pickSupport
protected static java.util.regex.Pattern
SGMLOrSpacePattern
protected static java.util.regex.Pattern
SGMLPattern
protected java.lang.String
textAlign
protected java.awt.Color
textColor
protected TextRenderer
textRenderer
-
Constructor Summary
Constructors Constructor Description MultiLineTextRenderer(TextRenderer textRenderer)
MultiLineTextRenderer(java.awt.Font font)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static boolean
containsHTML(java.lang.String text)
Return true if the text contains some sgml tags.void
draw(java.lang.String text, int x, int y)
Draw a multi-line text string with bounding rectangle top starting at the y position.void
draw(java.lang.String text, int x, int y, int textLineHeight)
Draw a multi-line text string with bounding rectangle top starting at the y position.void
draw(java.lang.String text, int x, int y, int textLineHeight, java.lang.String effect)
Draw a multi-line text string with bounding rectangle top starting at the y position.void
draw(java.lang.String text, int x, int y, java.lang.String effect)
Draw a multi-line text string with bounding rectangle top starting at the y position.protected void
drawFilledRectangle(DrawContext dc, double x, double y, double width, double height)
void
drawHTML(java.lang.String text, double x, double y, TextRendererCache renderers)
Draw a multi-line html text string with bounding rectangle top starting at the y position.protected void
drawLineHTML(java.lang.String line, double x, double y, MultiLineTextRenderer.DrawState ds)
protected void
drawLineWithUniqueColors(java.lang.String text, int x, int y, DrawContext dc, PickSupport pickSupport, java.lang.Object refObject, Position refPosition)
protected void
drawTextHTML(java.lang.String text, double x, double y, MultiLineTextRenderer.DrawState ds)
protected void
drawWordHTML(java.lang.String word, double x, double y, MultiLineTextRenderer.DrawState ds)
static java.lang.String
getAttributeFromTagHTML(java.lang.String text, java.lang.String attributeName)
Extract an attribute value from a HTML tag string.java.awt.Color
getBackColor()
Get the background color used for EFFECT_SHADOW and EFFECT_OUTLINE.java.awt.Rectangle
getBounds(java.lang.String text)
Returns the bounding rectangle for a multi-line string.java.awt.Rectangle
getBoundsHTML(java.lang.String text, TextRendererCache renderers)
Returns the bounding rectangle for a multi-line html string.protected java.awt.geom.Rectangle2D
getLineBoundsHTML(java.lang.String line, MultiLineTextRenderer.DrawState ds)
int
getLineHeight()
Get the current line height in pixels.int
getLineSpacing()
Get the current line spacing height in pixels.java.awt.Color
getLinkColor()
Get the current link color.double
getMaxLineHeight(TextRenderer tr)
Get the maximum line height for the given text renderer.java.lang.String
getTextAlign()
Get the current text alignment.protected java.awt.geom.Rectangle2D
getTextBoundsHTML(java.lang.String text, MultiLineTextRenderer.DrawState ds)
java.awt.Color
getTextColor()
Get the current text color.TextRenderer
getTextRenderer()
Get the current TextRenderer.protected java.awt.geom.Rectangle2D
getWordBoundsHTML(java.lang.String word, MultiLineTextRenderer.DrawState ds)
void
pick(java.lang.String text, int x, int y, int textLineHeight, DrawContext dc, PickSupport pickSupport, java.lang.Object refObject, Position refPosition)
Draw text with unique colors word bounding rectangles and add each as a pickable object to the provided PickSupport instance.void
pickHTML(java.lang.String text, int x, int y, TextRendererCache renderers, DrawContext dc, PickSupport pickSupport, java.lang.Object refObject, Position refPosition)
Draw text with unique colors word bounding rectangles and add each as a pickable object to the provided PickSupport instance.protected void
pickWord(java.lang.String word, java.lang.String hyperlink, double drawX, double drawY, java.awt.geom.Rectangle2D wordBounds, DrawContext dc, PickSupport pickSupport, java.lang.Object refObject, Position refPosition)
protected void
pickWordHTML(java.lang.String word, double x, double y, MultiLineTextRenderer.DrawState ds)
protected void
pickWordPartHTML(java.lang.String word, double x, double y, java.awt.geom.Rectangle2D partBounds, MultiLineTextRenderer.DrawState ds, boolean expandStart)
static java.lang.String
processLineBreaksHTML(java.lang.String text)
Remove new line characters then replace BR and P tags with appropriate new lines.static java.lang.String
removeTagsHTML(java.lang.String text)
Remove all HTML tags from a text string.void
setBackColor(java.awt.Color color)
Set the background color used for EFFECT_SHADOW and EFFECT_OUTLINE.void
setContinuationString(java.lang.String s)
Set the character string appended at the end of text truncated during a wrap operation when exceeding the given height limit.void
setLineHeight(int height)
Set the current line height in pixels.void
setLineSpacing(int height)
Set the current line spacing height in pixels.void
setLinkColor(java.awt.Color color)
Set the link color.void
setTextAlign(java.lang.String align)
Set the current text alignment.void
setTextColor(java.awt.Color color)
Set the text renderer color.protected java.lang.String
trimTextHTML(java.lang.String text, double height, MultiLineTextRenderer.DrawState ds)
java.lang.String
wrap(java.lang.String text, int width, int height)
Add 'new line' characters inside a string so that it's bounding rectangle tries not to exceed the given dimension width.java.lang.String
wrapHTML(java.lang.String text, double width, double height, TextRendererCache renderers)
Add 'new line' characters inside an html text string so that it's bounding rectangle tries not to exceed the given dimension width.protected java.lang.String
wrapLine(java.lang.String text, int width)
protected java.lang.String
wrapLineHTML(java.lang.String line, double width, MultiLineTextRenderer.DrawState ds)
protected java.lang.String
wrapTextHTML(java.lang.String text, double width, double height, MultiLineTextRenderer.DrawState ds)
-
-
-
Field Detail
-
textRenderer
protected TextRenderer textRenderer
-
lineSpacing
protected int lineSpacing
-
lineHeight
protected int lineHeight
-
textAlign
protected java.lang.String textAlign
-
continuationString
protected java.lang.String continuationString
-
textColor
protected java.awt.Color textColor
-
backColor
protected java.awt.Color backColor
-
linkColor
protected java.awt.Color linkColor
-
isPicking
protected boolean isPicking
-
drawContext
protected DrawContext drawContext
-
pickSupport
protected PickSupport pickSupport
-
pickObject
protected java.lang.Object pickObject
-
pickPosition
protected Position pickPosition
-
SGMLPattern
protected static java.util.regex.Pattern SGMLPattern
-
SGMLOrSpacePattern
protected static java.util.regex.Pattern SGMLOrSpacePattern
-
-
Constructor Detail
-
MultiLineTextRenderer
public MultiLineTextRenderer(TextRenderer textRenderer)
-
MultiLineTextRenderer
public MultiLineTextRenderer(java.awt.Font font)
-
-
Method Detail
-
getTextRenderer
public TextRenderer getTextRenderer()
Get the current TextRenderer.- Returns:
- the current TextRenderer.
-
getLineSpacing
public int getLineSpacing()
Get the current line spacing height in pixels.- Returns:
- the current line spacing height in pixels.
-
setLineSpacing
public void setLineSpacing(int height)
Set the current line spacing height in pixels.- Parameters:
height
- the line spacing height in pixels.
-
getLineHeight
public int getLineHeight()
Get the current line height in pixels.- Returns:
- the current line height in pixels.
-
setLineHeight
public void setLineHeight(int height)
Set the current line height in pixels.- Parameters:
height
- the current line height in pixels.
-
getTextAlign
public java.lang.String getTextAlign()
- Returns:
- the current text alignment.
-
setTextAlign
public void setTextAlign(java.lang.String align)
- Parameters:
align
- the current text alignment.
-
getTextColor
public java.awt.Color getTextColor()
Get the current text color.- Returns:
- the current text color.
-
setTextColor
public void setTextColor(java.awt.Color color)
Set the text renderer color.- Parameters:
color
- the color to use when drawing text.
-
getBackColor
public java.awt.Color getBackColor()
Get the background color used for EFFECT_SHADOW and EFFECT_OUTLINE.- Returns:
- the current background color used when drawing shadow or outline..
-
setBackColor
public void setBackColor(java.awt.Color color)
Set the background color used for EFFECT_SHADOW and EFFECT_OUTLINE.- Parameters:
color
- the color to use when drawing shadow or outline.
-
getLinkColor
public java.awt.Color getLinkColor()
Get the current link color.- Returns:
- the current link color.
-
setLinkColor
public void setLinkColor(java.awt.Color color)
Set the link color.- Parameters:
color
- the color to use when drawing hyperlinks.
-
setContinuationString
public void setContinuationString(java.lang.String s)
Set the character string appended at the end of text truncated during a wrap operation when exceeding the given height limit.- Parameters:
s
- the continuation character string.
-
getMaxLineHeight
public double getMaxLineHeight(TextRenderer tr)
Get the maximum line height for the given text renderer.- Parameters:
tr
- the TextRenderer.- Returns:
- the maximum line height.
-
getBounds
public java.awt.Rectangle getBounds(java.lang.String text)
Returns the bounding rectangle for a multi-line string.Note that the X component of the rectangle is the number of lines found in the text and the Y component of the rectangle is the max line height encountered.
Note too that this method will automatically set the current line height to the max height found.
- Parameters:
text
- the multi-line text to evaluate.- Returns:
- the bounding rectangle for the string.
-
draw
public void draw(java.lang.String text, int x, int y)
Draw a multi-line text string with bounding rectangle top starting at the y position. Depending on the current textAlign, the x position is either the rectangle left side, middle or right side.Uses the current line height.
- Parameters:
text
- the multi-line text to draw.x
- the x position for top left corner of text rectangle.y
- the y position for top left corner of the text rectangle.
-
draw
public void draw(java.lang.String text, int x, int y, java.lang.String effect)
Draw a multi-line text string with bounding rectangle top starting at the y position. Depending on the current textAlign, the x position is either the rectangle left side, middle or right side.Uses the current line height and the given effect.
- Parameters:
text
- the multi-line text to draw.x
- the x position for top left corner of text rectangle.y
- the y position for top left corner of the text rectangle.effect
- the effect to use for the text rendering. Can be one ofEFFECT_NONE
,EFFECT_SHADOW
orEFFECT_OUTLINE
.
-
draw
public void draw(java.lang.String text, int x, int y, int textLineHeight, java.lang.String effect)
Draw a multi-line text string with bounding rectangle top starting at the y position. Depending on the current textAlign, the x position is either the rectangle left side, middle or right side.Uses the given line height and effect.
- Parameters:
text
- the multi-line text to draw.x
- the x position for top left corner of text rectangle.y
- the y position for top left corner of the text rectangle.textLineHeight
- the line height in pixels.effect
- the effect to use for the text rendering. Can be one ofEFFECT_NONE
,EFFECT_SHADOW
orEFFECT_OUTLINE
.
-
draw
public void draw(java.lang.String text, int x, int y, int textLineHeight)
Draw a multi-line text string with bounding rectangle top starting at the y position. Depending on the current textAlign, the x position is either the rectangle left side, middle or right side.Uses the given line height.
- Parameters:
text
- the multi-line text to draw.x
- the x position for top left corner of text rectangle.y
- the y position for top left corner of the text rectangle.textLineHeight
- the line height in pixels.
-
pick
public void pick(java.lang.String text, int x, int y, int textLineHeight, DrawContext dc, PickSupport pickSupport, java.lang.Object refObject, Position refPosition)
Draw text with unique colors word bounding rectangles and add each as a pickable object to the provided PickSupport instance.- Parameters:
text
- the multi-line text to draw.x
- the x position for top left corner of text rectangle.y
- the y position for top left corner of the text rectangle.textLineHeight
- the line height in pixels.dc
- the current DrawContext.pickSupport
- the PickSupport instance to be used.refObject
- the user reference object associated with every picked word.refPosition
- the user reference Position associated with every picked word.
-
drawLineWithUniqueColors
protected void drawLineWithUniqueColors(java.lang.String text, int x, int y, DrawContext dc, PickSupport pickSupport, java.lang.Object refObject, Position refPosition)
-
wrap
public java.lang.String wrap(java.lang.String text, int width, int height)
Add 'new line' characters inside a string so that it's bounding rectangle tries not to exceed the given dimension width.If the dimension height is more than zero, the text will be truncated accordingly and the continuation string will be appended to the last line.
Note that words will not be split and at least one word will be used per line so the longest word defines the final width of the bounding rectangle. Each line is trimmed of leading and trailing spaces.
- Parameters:
text
- the text string to wrapwidth
- the maximum width in pixels the text can occupy.height
- if not zero, the maximum height in pixels the text can occupy.- Returns:
- the wrapped string.
-
wrapLine
protected java.lang.String wrapLine(java.lang.String text, int width)
-
containsHTML
public static boolean containsHTML(java.lang.String text)
Return true if the text contains some sgml tags.- Parameters:
text
- The text string to evaluate.- Returns:
- true if the string contains sgml or html tags
-
processLineBreaksHTML
public static java.lang.String processLineBreaksHTML(java.lang.String text)
Remove new line characters then replace BR and P tags with appropriate new lines.- Parameters:
text
- The html text string to process.- Returns:
- The processed text string.
-
removeTagsHTML
public static java.lang.String removeTagsHTML(java.lang.String text)
Remove all HTML tags from a text string.- Parameters:
text
- the string to filter.- Returns:
- the filtered string.
-
getAttributeFromTagHTML
public static java.lang.String getAttributeFromTagHTML(java.lang.String text, java.lang.String attributeName)
Extract an attribute value from a HTML tag string. The attribute is expected to be formed on the pattern: name="...". Other variants will likely fail.- Parameters:
text
- the HTML tage string.attributeName
- the attribute name.- Returns:
- the attribute value found. Null if empty or not found.
-
getBoundsHTML
public java.awt.Rectangle getBoundsHTML(java.lang.String text, TextRendererCache renderers)
Returns the bounding rectangle for a multi-line html string.- Parameters:
text
- the multi-line html text to evaluate.renderers
- aTextRendererCache
instance.- Returns:
- the bounding rectangle for the rendered text.
-
getTextBoundsHTML
protected java.awt.geom.Rectangle2D getTextBoundsHTML(java.lang.String text, MultiLineTextRenderer.DrawState ds)
-
getLineBoundsHTML
protected java.awt.geom.Rectangle2D getLineBoundsHTML(java.lang.String line, MultiLineTextRenderer.DrawState ds)
-
getWordBoundsHTML
protected java.awt.geom.Rectangle2D getWordBoundsHTML(java.lang.String word, MultiLineTextRenderer.DrawState ds)
-
wrapHTML
public java.lang.String wrapHTML(java.lang.String text, double width, double height, TextRendererCache renderers)
Add 'new line' characters inside an html text string so that it's bounding rectangle tries not to exceed the given dimension width.If the dimension height is more than zero, the text will be truncated accordingly and the continuation string will be appended to the last line.
Note that words will not be split and at least one word will be used per line so the longest word defines the final width of the bounding rectangle. Each line is trimmed of leading and trailing spaces.
- Parameters:
text
- the html text string to wrapwidth
- the maximum width in pixels one text line can occupy.height
- if not zero, the maximum height the text can occupy.renderers
- aTextRendererCache
instance.- Returns:
- the wrapped html string
-
wrapTextHTML
protected java.lang.String wrapTextHTML(java.lang.String text, double width, double height, MultiLineTextRenderer.DrawState ds)
-
trimTextHTML
protected java.lang.String trimTextHTML(java.lang.String text, double height, MultiLineTextRenderer.DrawState ds)
-
wrapLineHTML
protected java.lang.String wrapLineHTML(java.lang.String line, double width, MultiLineTextRenderer.DrawState ds)
-
pickHTML
public void pickHTML(java.lang.String text, int x, int y, TextRendererCache renderers, DrawContext dc, PickSupport pickSupport, java.lang.Object refObject, Position refPosition)
Draw text with unique colors word bounding rectangles and add each as a pickable object to the provided PickSupport instance.- Parameters:
text
- the multi-line text to draw.x
- the x position for top left corner of text rectangle.y
- the y position for top left corner of the text rectangle.renderers
- aTextRendererCache
instance.dc
- the current DrawContext.pickSupport
- the PickSupport instance to be used.refObject
- the user reference object associated with every picked word.refPosition
- the user reference Position associated with every picked word.
-
drawHTML
public void drawHTML(java.lang.String text, double x, double y, TextRendererCache renderers)
Draw a multi-line html text string with bounding rectangle top starting at the y position. The x position is eiher the rectangle left side, middle or right side depending on the current text alignement.- Parameters:
text
- the multi-line text to drawx
- the x position for top left corner of text rectangley
- the y position for top left corner of the text rectanglerenderers
- aTextRendererCache
instance.
-
drawTextHTML
protected void drawTextHTML(java.lang.String text, double x, double y, MultiLineTextRenderer.DrawState ds)
-
drawLineHTML
protected void drawLineHTML(java.lang.String line, double x, double y, MultiLineTextRenderer.DrawState ds)
-
drawWordHTML
protected void drawWordHTML(java.lang.String word, double x, double y, MultiLineTextRenderer.DrawState ds)
-
pickWordHTML
protected void pickWordHTML(java.lang.String word, double x, double y, MultiLineTextRenderer.DrawState ds)
-
pickWordPartHTML
protected void pickWordPartHTML(java.lang.String word, double x, double y, java.awt.geom.Rectangle2D partBounds, MultiLineTextRenderer.DrawState ds, boolean expandStart)
-
pickWord
protected void pickWord(java.lang.String word, java.lang.String hyperlink, double drawX, double drawY, java.awt.geom.Rectangle2D wordBounds, DrawContext dc, PickSupport pickSupport, java.lang.Object refObject, Position refPosition)
-
drawFilledRectangle
protected void drawFilledRectangle(DrawContext dc, double x, double y, double width, double height)
-
-