Package ca.cgjennings.graphics.strokes
Class TextStroke
- java.lang.Object
-
- ca.cgjennings.graphics.strokes.TextStroke
-
- All Implemented Interfaces:
java.awt.Stroke
public class TextStroke extends java.lang.Object implements java.awt.Stroke
A stroke that draws text along the stroked path, rotating the individual glyphs to follow the shape of the curve.- Author:
- Chris Jennings
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
TextStroke.Style
TheStyle
determines how the text is placed fitted to the path when the length of the text and the length of a path are not the same.
-
Constructor Summary
Constructors Constructor Description TextStroke(java.lang.String text, java.awt.Font font)
Creates a text stroke for the given text and font that will fit the text to fill the entire path and use defaultflatness
andlimit
parameters.TextStroke(java.lang.String text, java.awt.Font font, TextStroke.Style style)
Creates a text stroke for the given text, font, and fitting style and use defaultflatness
andlimit
parameters.TextStroke(java.lang.String text, java.awt.Font font, TextStroke.Style style, double flatness, int limit)
Creates a text stroke for the given text and font that will fit the text to fill the entire path and use defaultflatness
andlimit
parameters.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.awt.Shape
createStrokedShape(java.awt.Shape shape)
java.awt.geom.Point2D.Double
getEndPoint()
Returns the point at which the last call tocreateStrokedShape(java.awt.Shape)
last drew a glyph.java.awt.Font
getFont()
Returns the font used to draw text.java.lang.String
getText()
Returns the text that will be drawn along stroked paths.boolean
isInsideOut()
Returnstrue
if the text is drawn inside-out, that is, if the direction that the glyphs protrude from the path is flipped.boolean
isLeftToRight()
Returnstrue
if the text is treated as a left-to-right language.void
setFont(java.awt.Font font)
Sets the font used that the stroke draws text with.void
setInsideOut(boolean flip)
Sets whether the text will protrude out of the shape (the default) or into it.void
setLeftToRight(boolean leftToRight)
Sets the bidi orientation of the text.void
setText(java.lang.String text)
Sets the text that will be drawn along stroked paths.double
textWidth()
Returns the width of the current text, if it was rendered in a straight line.
-
-
-
Constructor Detail
-
TextStroke
public TextStroke(java.lang.String text, java.awt.Font font)
Creates a text stroke for the given text and font that will fit the text to fill the entire path and use defaultflatness
andlimit
parameters.- Parameters:
text
- the text to be drawn along stroked pathsfont
- the font to drawn the text in
-
TextStroke
public TextStroke(java.lang.String text, java.awt.Font font, TextStroke.Style style)
Creates a text stroke for the given text, font, and fitting style and use defaultflatness
andlimit
parameters.- Parameters:
text
- the text to be drawn along stroked pathsfont
- the font to drawn the text instyle
- the style used to fit text to the path
-
TextStroke
public TextStroke(java.lang.String text, java.awt.Font font, TextStroke.Style style, double flatness, int limit)
Creates a text stroke for the given text and font that will fit the text to fill the entire path and use defaultflatness
andlimit
parameters.- Parameters:
text
- the text to be drawn along stroked pathsfont
- the font to drawn the text instyle
- the style used to fit text to the pathflatness
- the maximum allowable distance between the control points and the flattened curvelimit
- log2 of the maximum number of line segments that will be generated for any curved segment of the path- Throws:
java.lang.IllegalArgumentException
- ifflatness
orlimit
is less than 0
-
-
Method Detail
-
setInsideOut
public final void setInsideOut(boolean flip)
Sets whether the text will protrude out of the shape (the default) or into it. When drawing text along the path, the rotation of each glyph is determined by finding the right angle vector to a tangent to the curve along the portion of the curve covered by the glyph. There are two such right vectors: one points from the curve towards the outside of the shape, and one points from the curve towards the inside of the shape. When this is set totrue
, the inside vector will be selected and the order in which the glyphs are drawn will be reversed.- Parameters:
flip
- iftrue
, flips the text inside-out
-
isInsideOut
public final boolean isInsideOut()
Returnstrue
if the text is drawn inside-out, that is, if the direction that the glyphs protrude from the path is flipped.- Returns:
true
if the glyph direction should be flipped
-
setText
public final void setText(java.lang.String text)
Sets the text that will be drawn along stroked paths.- Parameters:
text
- the text drawn by this stroke
-
getText
public final java.lang.String getText()
Returns the text that will be drawn along stroked paths.- Returns:
- the text drawn by this stroke
-
setLeftToRight
public final void setLeftToRight(boolean leftToRight)
Sets the bidi orientation of the text. Set totrue
for left-to-right languages (e.g., English) andfalse
for right-to-left languages (e.g., Arabic). The default istrue
.- Parameters:
leftToRight
-true
if the text should be treated as left-to-right
-
isLeftToRight
public final boolean isLeftToRight()
Returnstrue
if the text is treated as a left-to-right language.- Returns:
true
if the bidi order is treated as left-to-right
-
setFont
public final void setFont(java.awt.Font font)
Sets the font used that the stroke draws text with.- Parameters:
font
- the font used by letters in this stroke
-
getFont
public final java.awt.Font getFont()
Returns the font used to draw text.- Returns:
- the font used to draw letters along stroked paths
-
createStrokedShape
public java.awt.Shape createStrokedShape(java.awt.Shape shape)
- Specified by:
createStrokedShape
in interfacejava.awt.Stroke
-
getEndPoint
public java.awt.geom.Point2D.Double getEndPoint()
Returns the point at which the last call tocreateStrokedShape(java.awt.Shape)
last drew a glyph.- Returns:
- the end point of the last glyph
-
textWidth
public double textWidth()
Returns the width of the current text, if it was rendered in a straight line.- Returns:
- the length of path that would naturally fit the current text
-
-