Package com.orsonpdf.util
Class TextUtils
java.lang.Object
com.orsonpdf.util.TextUtils
Text utility functions.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static float[]
deriveRotationAnchorOffsets
(Graphics2D g2, String text, TextAnchor anchor) A utility method that calculates the rotation anchor offsets for a string.private static float[]
deriveTextBoundsAnchorOffsets
(Graphics2D g2, String text, TextAnchor anchor) A utility method that calculates the anchor offsets for a string.private static float[]
deriveTextBoundsAnchorOffsets
(Graphics2D g2, String text, TextAnchor anchor, Rectangle2D textBounds) A utility method that calculates the anchor offsets for a string.static Rectangle2D
drawAlignedString
(String text, Graphics2D g2, float x, float y, TextAnchor anchor) Draws a string such that the specified anchor point is aligned to the given(x, y)
location.static void
drawRotatedString
(String text, Graphics2D g2, double angle, float x, float y) A utility method for drawing rotated text.static void
drawRotatedString
(String text, Graphics2D g2, float textX, float textY, double angle, float rotateX, float rotateY) A utility method for drawing rotated text.static void
drawRotatedString
(String text, Graphics2D g2, float x, float y, TextAnchor textAnchor, double angle, float rotationX, float rotationY) Draws a string that is aligned by one anchor point and rotated about another anchor point.static void
drawRotatedString
(String text, Graphics2D g2, float x, float y, TextAnchor textAnchor, double angle, TextAnchor rotationAnchor) Draws a string that is aligned by one anchor point and rotated about another anchor point.static Rectangle2D
getTextBounds
(String text, Graphics2D g2, FontMetrics fm) Returns the bounds for the specified text.
-
Constructor Details
-
TextUtils
private TextUtils()
-
-
Method Details
-
drawAlignedString
public static Rectangle2D drawAlignedString(String text, Graphics2D g2, float x, float y, TextAnchor anchor) Draws a string such that the specified anchor point is aligned to the given(x, y)
location.- Parameters:
text
- the text.g2
- the graphics device.x
- the x coordinate (Java 2D).y
- the y coordinate (Java 2D).anchor
- the anchor location.- Returns:
- The text bounds (adjusted for the text position).
-
deriveTextBoundsAnchorOffsets
A utility method that calculates the anchor offsets for a string. Normally, the(x, y)
coordinate for drawing text is a point on the baseline at the left of the text string. If you add these offsets to(x, y)
and draw the string, then the anchor point should coincide with the(x, y)
point.- Parameters:
g2
- the graphics device (notnull
).text
- the text.anchor
- the anchor point.- Returns:
- The offsets.
-
deriveTextBoundsAnchorOffsets
private static float[] deriveTextBoundsAnchorOffsets(Graphics2D g2, String text, TextAnchor anchor, Rectangle2D textBounds) A utility method that calculates the anchor offsets for a string. Normally, the(x, y)
coordinate for drawing text is a point on the baseline at the left of the text string. If you add these offsets to(x, y)
and draw the string, then the anchor point should coincide with the(x, y)
point.- Parameters:
g2
- the graphics device (notnull
).text
- the text.anchor
- the anchor point.textBounds
- the text bounds (if notnull
, this object will be updated by this method to match the string bounds).- Returns:
- The offsets.
-
getTextBounds
Returns the bounds for the specified text.- Parameters:
text
- the text (null
permitted).g2
- the graphics context (notnull
).fm
- the font metrics (notnull
).- Returns:
- The text bounds (
null
if thetext
argument isnull
).
-
drawRotatedString
public static void drawRotatedString(String text, Graphics2D g2, float x, float y, TextAnchor textAnchor, double angle, float rotationX, float rotationY) Draws a string that is aligned by one anchor point and rotated about another anchor point.- Parameters:
text
- the text.g2
- the graphics device.x
- the x-coordinate for positioning the text.y
- the y-coordinate for positioning the text.textAnchor
- the text anchor.angle
- the rotation angle.rotationX
- the x-coordinate for the rotation anchor point.rotationY
- the y-coordinate for the rotation anchor point.
-
drawRotatedString
public static void drawRotatedString(String text, Graphics2D g2, float x, float y, TextAnchor textAnchor, double angle, TextAnchor rotationAnchor) Draws a string that is aligned by one anchor point and rotated about another anchor point.- Parameters:
text
- the text.g2
- the graphics device.x
- the x-coordinate for positioning the text.y
- the y-coordinate for positioning the text.textAnchor
- the text anchor.angle
- the rotation angle (in radians).rotationAnchor
- the rotation anchor.
-
deriveRotationAnchorOffsets
A utility method that calculates the rotation anchor offsets for a string. These offsets are relative to the text starting coordinate (BASELINE_LEFT
).- Parameters:
g2
- the graphics device.text
- the text.anchor
- the anchor point.- Returns:
- The offsets.
-
drawRotatedString
A utility method for drawing rotated text.A common rotation is
-Math.PI/2
which draws text 'vertically' (with the top of the characters on the left).- Parameters:
text
- the text.g2
- the graphics device.angle
- the angle of the (clockwise) rotation (in radians).x
- the x-coordinate.y
- the y-coordinate.
-
drawRotatedString
public static void drawRotatedString(String text, Graphics2D g2, float textX, float textY, double angle, float rotateX, float rotateY) A utility method for drawing rotated text.A common rotation is
-Math.PI/2
which draws text 'vertically' (with the top of the characters on the left).- Parameters:
text
- the text.g2
- the graphics device.textX
- the x-coordinate for the text (before rotation).textY
- the y-coordinate for the text (before rotation).angle
- the angle of the (clockwise) rotation (in radians).rotateX
- the point about which the text is rotated.rotateY
- the point about which the text is rotated.
-