public class MultiLineTextRenderer extends Object
The MultiLineTextRenderer
(MLTR) handles wrapping, measuring and drawing of multiline text strings using Sun's JOGL TextRenderer
.
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);or
TextRenderer 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:
See AbstractAnnotation
.drawAnnotation() for more usage details.
Modifier and Type | Class and Description |
---|---|
protected class |
MultiLineTextRenderer.DrawState |
static class |
MultiLineTextRenderer.WordIteratorHTML
Iterates through words from an HTML text line.
|
Modifier and Type | Field and Description |
---|---|
protected Color |
backColor |
protected String |
continuationString |
protected DrawContext |
drawContext |
protected boolean |
isPicking |
protected int |
lineHeight |
protected int |
lineSpacing |
protected Color |
linkColor |
protected Object |
pickObject |
protected Position |
pickPosition |
protected PickSupport |
pickSupport |
protected static Pattern |
SGMLOrSpacePattern |
protected static Pattern |
SGMLPattern |
protected String |
textAlign |
protected Color |
textColor |
protected TextRenderer |
textRenderer |
Constructor and Description |
---|
MultiLineTextRenderer(Font font) |
MultiLineTextRenderer(TextRenderer textRenderer) |
Modifier and Type | Method and Description |
---|---|
static boolean |
containsHTML(String text)
Return true if the text contains some sgml tags.
|
void |
draw(String text,
int x,
int y)
Draw a multi-line text string with bounding rectangle top starting at the y position.
|
void |
draw(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(String text,
int x,
int y,
int textLineHeight,
String effect)
Draw a multi-line text string with bounding rectangle top starting at the y position.
|
void |
draw(String text,
int x,
int y,
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(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(String line,
double x,
double y,
MultiLineTextRenderer.DrawState ds) |
protected void |
drawLineWithUniqueColors(String text,
int x,
int y,
DrawContext dc,
PickSupport pickSupport,
Object refObject,
Position refPosition) |
protected void |
drawTextHTML(String text,
double x,
double y,
MultiLineTextRenderer.DrawState ds) |
protected void |
drawWordHTML(String word,
double x,
double y,
MultiLineTextRenderer.DrawState ds) |
static String |
getAttributeFromTagHTML(String text,
String attributeName)
Extract an attribute value from a HTML tag string.
|
Color |
getBackColor()
Get the background color used for EFFECT_SHADOW and EFFECT_OUTLINE.
|
Rectangle |
getBounds(String text)
Returns the bounding rectangle for a multi-line string.
|
Rectangle |
getBoundsHTML(String text,
TextRendererCache renderers)
Returns the bounding rectangle for a multi-line html string.
|
protected Rectangle2D |
getLineBoundsHTML(String line,
MultiLineTextRenderer.DrawState ds) |
int |
getLineHeight()
Get the current line height in pixels.
|
int |
getLineSpacing()
Get the current line spacing height in pixels.
|
Color |
getLinkColor()
Get the current link color.
|
double |
getMaxLineHeight(TextRenderer tr)
Get the maximum line height for the given text renderer.
|
String |
getTextAlign()
Get the current text alignment.
|
protected Rectangle2D |
getTextBoundsHTML(String text,
MultiLineTextRenderer.DrawState ds) |
Color |
getTextColor()
Get the current text color.
|
TextRenderer |
getTextRenderer()
Get the current TextRenderer.
|
protected Rectangle2D |
getWordBoundsHTML(String word,
MultiLineTextRenderer.DrawState ds) |
void |
pick(String text,
int x,
int y,
int textLineHeight,
DrawContext dc,
PickSupport pickSupport,
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(String text,
int x,
int y,
TextRendererCache renderers,
DrawContext dc,
PickSupport pickSupport,
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(String word,
String hyperlink,
double drawX,
double drawY,
Rectangle2D wordBounds,
DrawContext dc,
PickSupport pickSupport,
Object refObject,
Position refPosition) |
protected void |
pickWordHTML(String word,
double x,
double y,
MultiLineTextRenderer.DrawState ds) |
protected void |
pickWordPartHTML(String word,
double x,
double y,
Rectangle2D partBounds,
MultiLineTextRenderer.DrawState ds,
boolean expandStart) |
static String |
processLineBreaksHTML(String text)
Remove new line characters then replace BR and P tags with appropriate new lines.
|
static String |
removeTagsHTML(String text)
Remove all HTML tags from a text string.
|
void |
setBackColor(Color color)
Set the background color used for EFFECT_SHADOW and EFFECT_OUTLINE.
|
void |
setContinuationString(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(Color color)
Set the link color.
|
void |
setTextAlign(String align)
Set the current text alignment.
|
void |
setTextColor(Color color)
Set the text renderer color.
|
protected String |
trimTextHTML(String text,
double height,
MultiLineTextRenderer.DrawState ds) |
String |
wrap(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.
|
String |
wrapHTML(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 String |
wrapLine(String text,
int width) |
protected String |
wrapLineHTML(String line,
double width,
MultiLineTextRenderer.DrawState ds) |
protected String |
wrapTextHTML(String text,
double width,
double height,
MultiLineTextRenderer.DrawState ds) |
protected Color backColor
protected String continuationString
protected DrawContext drawContext
protected boolean isPicking
protected int lineHeight
protected int lineSpacing
protected Color linkColor
protected Object pickObject
protected Position pickPosition
protected PickSupport pickSupport
protected static Pattern SGMLOrSpacePattern
protected static Pattern SGMLPattern
protected String textAlign
protected Color textColor
protected TextRenderer textRenderer
public MultiLineTextRenderer(Font font)
public MultiLineTextRenderer(TextRenderer textRenderer)
public static boolean containsHTML(String text)
text
- The text string to evaluate.public void draw(String text, int x, int y)
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.public void draw(String text, int x, int y, int textLineHeight)
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.public void draw(String text, int x, int y, int textLineHeight, String effect)
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 of EFFECT_NONE
,
EFFECT_SHADOW
or EFFECT_OUTLINE
.public void draw(String text, int x, int y, String effect)
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 of EFFECT_NONE
,
EFFECT_SHADOW
or EFFECT_OUTLINE
.protected void drawFilledRectangle(DrawContext dc, double x, double y, double width, double height)
public void drawHTML(String text, double x, double y, TextRendererCache renderers)
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
- a TextRendererCache
instance.protected void drawLineHTML(String line, double x, double y, MultiLineTextRenderer.DrawState ds)
protected void drawLineWithUniqueColors(String text, int x, int y, DrawContext dc, PickSupport pickSupport, Object refObject, Position refPosition)
protected void drawTextHTML(String text, double x, double y, MultiLineTextRenderer.DrawState ds)
protected void drawWordHTML(String word, double x, double y, MultiLineTextRenderer.DrawState ds)
public static String getAttributeFromTagHTML(String text, String attributeName)
text
- the HTML tage string.attributeName
- the attribute name.public Color getBackColor()
public Rectangle getBounds(String text)
text
- the multi-line text to evaluate.public Rectangle getBoundsHTML(String text, TextRendererCache renderers)
text
- the multi-line html text to evaluate.renderers
- a TextRendererCache
instance.protected Rectangle2D getLineBoundsHTML(String line, MultiLineTextRenderer.DrawState ds)
public int getLineHeight()
public int getLineSpacing()
public Color getLinkColor()
public double getMaxLineHeight(TextRenderer tr)
tr
- the TextRenderer.public String getTextAlign()
protected Rectangle2D getTextBoundsHTML(String text, MultiLineTextRenderer.DrawState ds)
public Color getTextColor()
public TextRenderer getTextRenderer()
protected Rectangle2D getWordBoundsHTML(String word, MultiLineTextRenderer.DrawState ds)
public void pick(String text, int x, int y, int textLineHeight, DrawContext dc, PickSupport pickSupport, Object refObject, Position refPosition)
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.public void pickHTML(String text, int x, int y, TextRendererCache renderers, DrawContext dc, PickSupport pickSupport, Object refObject, Position refPosition)
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
- a TextRendererCache
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.protected void pickWord(String word, String hyperlink, double drawX, double drawY, Rectangle2D wordBounds, DrawContext dc, PickSupport pickSupport, Object refObject, Position refPosition)
protected void pickWordHTML(String word, double x, double y, MultiLineTextRenderer.DrawState ds)
protected void pickWordPartHTML(String word, double x, double y, Rectangle2D partBounds, MultiLineTextRenderer.DrawState ds, boolean expandStart)
public static String processLineBreaksHTML(String text)
text
- The html text string to process.public static String removeTagsHTML(String text)
text
- the string to filter.public void setBackColor(Color color)
color
- the color to use when drawing shadow or outline.public void setContinuationString(String s)
s
- the continuation character string.public void setLineHeight(int height)
height
- the current line height in pixels.public void setLineSpacing(int height)
height
- the line spacing height in pixels.public void setLinkColor(Color color)
color
- the color to use when drawing hyperlinks.public void setTextAlign(String align)
align
- the current text alignment.public void setTextColor(Color color)
color
- the color to use when drawing text.protected String trimTextHTML(String text, double height, MultiLineTextRenderer.DrawState ds)
public String wrap(String text, int width, int height)
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.public String wrapHTML(String text, double width, double height, TextRendererCache renderers)
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
- a TextRendererCache
instance.protected String wrapLineHTML(String line, double width, MultiLineTextRenderer.DrawState ds)
protected String wrapTextHTML(String text, double width, double height, MultiLineTextRenderer.DrawState ds)