Class TextUtils


  • public class TextUtils
    extends java.lang.Object
    Some utility methods for working with text in Java2D.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static boolean drawStringsWithFontAttributes
      When this flag is set to true, strings will be drawn as attributed strings with the attributes taken from the current font.
      private static boolean useDrawRotatedStringWorkaround
      A flag that controls whether or not the rotated string workaround is used.
      private static boolean useFontMetricsGetStringBounds
      A flag that controls whether the FontMetrics.getStringBounds() method is used or a workaround is applied.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private TextUtils()
      Private constructor prevents object creation.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.awt.geom.Rectangle2D calcAlignedStringBounds​(java.lang.String text, java.awt.Graphics2D g2, float x, float y, TextAnchor anchor)
      Returns the bounds of an aligned string.
      static java.awt.Shape calculateRotatedStringBounds​(java.lang.String text, java.awt.Graphics2D g2, float textX, float textY, double angle, float rotateX, float rotateY)
      Returns a shape that represents the bounds of the string after the specified rotation has been applied.
      static java.awt.Shape calculateRotatedStringBounds​(java.lang.String text, java.awt.Graphics2D g2, float x, float y, TextAnchor textAnchor, double angle, TextAnchor rotationAnchor)
      Returns a shape that represents the bounds of the string after the specified rotation has been applied.
      static TextBlock createTextBlock​(java.lang.String text, java.awt.Font font, java.awt.Paint paint)
      Creates a TextBlock from a String.
      static TextBlock createTextBlock​(java.lang.String text, java.awt.Font font, java.awt.Paint paint, float maxWidth, int maxLines, TextMeasurer measurer)
      Creates a new text block from the given string, breaking the text into lines so that the maxWidth value is respected.
      static TextBlock createTextBlock​(java.lang.String text, java.awt.Font font, java.awt.Paint paint, float maxWidth, TextMeasurer measurer)
      Creates a new text block from the given string, breaking the text into lines so that the maxWidth value is respected.
      private static float[] deriveRotationAnchorOffsets​(java.awt.Graphics2D g2, java.lang.String text, TextAnchor anchor)
      A utility method that calculates the rotation anchor offsets for a string.
      private static float[] deriveTextBoundsAnchorOffsets​(java.awt.Graphics2D g2, java.lang.String text, TextAnchor anchor)
      A utility method that calculates the anchor offsets for a string.
      private static float[] deriveTextBoundsAnchorOffsets​(java.awt.Graphics2D g2, java.lang.String text, TextAnchor anchor, java.awt.geom.Rectangle2D textBounds)
      A utility method that calculates the anchor offsets for a string.
      static java.awt.geom.Rectangle2D drawAlignedString​(java.lang.String text, java.awt.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​(java.lang.String text, java.awt.Graphics2D g2, double angle, float x, float y)
      A utility method for drawing rotated text.
      static void drawRotatedString​(java.lang.String text, java.awt.Graphics2D g2, float textX, float textY, double angle, float rotateX, float rotateY)
      A utility method for drawing rotated text.
      static void drawRotatedString​(java.lang.String text, java.awt.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​(java.lang.String text, java.awt.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 boolean getDrawStringsWithFontAttributes()
      Returns the flag that controls whether or not strings are drawn using the current font attributes (such as underlining, strikethrough etc).
      static java.awt.geom.Rectangle2D getTextBounds​(java.lang.String text, java.awt.Graphics2D g2, java.awt.FontMetrics fm)
      Returns the bounds for the specified text.
      static boolean getUseFontMetricsGetStringBounds()
      Returns the flag that controls whether the FontMetrics.getStringBounds() method is used or not.
      static boolean isUseDrawRotatedStringWorkaround()
      Returns the flag that controls whether or not a workaround is used for drawing rotated strings.
      private static int nextLineBreak​(java.lang.String text, int start, float width, java.text.BreakIterator iterator, TextMeasurer measurer)
      Returns the character index of the next line break.
      static void setDrawStringsWithFontAttributes​(boolean b)
      Sets the flag that controls whether or not strings are drawn using the current font attributes.
      static void setUseDrawRotatedStringWorkaround​(boolean use)
      Sets the flag that controls whether or not a workaround is used for drawing rotated strings.
      static void setUseFontMetricsGetStringBounds​(boolean use)
      Sets the flag that controls whether the FontMetrics.getStringBounds() method is used or not.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • drawStringsWithFontAttributes

        private static boolean drawStringsWithFontAttributes
        When this flag is set to true, strings will be drawn as attributed strings with the attributes taken from the current font. This allows for underlining, strike-out etc, but it means that TextLayout will be used to render the text: http://www.jfree.org/phpBB2/viewtopic.php?p=45459&highlight=#45459
      • useDrawRotatedStringWorkaround

        private static boolean useDrawRotatedStringWorkaround
        A flag that controls whether or not the rotated string workaround is used.
      • useFontMetricsGetStringBounds

        private static boolean useFontMetricsGetStringBounds
        A flag that controls whether the FontMetrics.getStringBounds() method is used or a workaround is applied.
    • Constructor Detail

      • TextUtils

        private TextUtils()
        Private constructor prevents object creation.
    • Method Detail

      • createTextBlock

        public static TextBlock createTextBlock​(java.lang.String text,
                                                java.awt.Font font,
                                                java.awt.Paint paint)
        Creates a TextBlock from a String. Line breaks are added where the String contains '\n' characters.
        Parameters:
        text - the text.
        font - the font.
        paint - the paint.
        Returns:
        A text block.
      • createTextBlock

        public static TextBlock createTextBlock​(java.lang.String text,
                                                java.awt.Font font,
                                                java.awt.Paint paint,
                                                float maxWidth,
                                                TextMeasurer measurer)
        Creates a new text block from the given string, breaking the text into lines so that the maxWidth value is respected.
        Parameters:
        text - the text.
        font - the font.
        paint - the paint.
        maxWidth - the maximum width for each line.
        measurer - the text measurer.
        Returns:
        A text block.
      • createTextBlock

        public static TextBlock createTextBlock​(java.lang.String text,
                                                java.awt.Font font,
                                                java.awt.Paint paint,
                                                float maxWidth,
                                                int maxLines,
                                                TextMeasurer measurer)
        Creates a new text block from the given string, breaking the text into lines so that the maxWidth value is respected.
        Parameters:
        text - the text.
        font - the font.
        paint - the paint.
        maxWidth - the maximum width for each line.
        maxLines - the maximum number of lines.
        measurer - the text measurer.
        Returns:
        A text block.
      • nextLineBreak

        private static int nextLineBreak​(java.lang.String text,
                                         int start,
                                         float width,
                                         java.text.BreakIterator iterator,
                                         TextMeasurer measurer)
        Returns the character index of the next line break. If the next character is wider than width] this method will return start - the caller should check for this case.
        Parameters:
        text - the text (null not permitted).
        start - the start index.
        width - the target display width.
        iterator - the word break iterator.
        measurer - the text measurer.
        Returns:
        The index of the next line break.
      • getTextBounds

        public static java.awt.geom.Rectangle2D getTextBounds​(java.lang.String text,
                                                              java.awt.Graphics2D g2,
                                                              java.awt.FontMetrics fm)
        Returns the bounds for the specified text.
        Parameters:
        text - the text (null permitted).
        g2 - the graphics context (not null).
        fm - the font metrics (not null).
        Returns:
        The text bounds (null if the text argument is null).
      • calcAlignedStringBounds

        public static java.awt.geom.Rectangle2D calcAlignedStringBounds​(java.lang.String text,
                                                                        java.awt.Graphics2D g2,
                                                                        float x,
                                                                        float y,
                                                                        TextAnchor anchor)
        Returns the bounds of an aligned string.
        Parameters:
        text - the string (null not permitted).
        g2 - the graphics target (null not permitted).
        x - the x-coordinate.
        y - the y-coordinate.
        anchor - the anchor point that will be aligned to (x, y) (null not permitted).
        Returns:
        The text bounds (never null).
      • drawAlignedString

        public static java.awt.geom.Rectangle2D drawAlignedString​(java.lang.String text,
                                                                  java.awt.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

        private static float[] deriveTextBoundsAnchorOffsets​(java.awt.Graphics2D g2,
                                                             java.lang.String text,
                                                             TextAnchor anchor,
                                                             java.awt.geom.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 (not null).
        text - the text.
        anchor - the anchor point.
        textBounds - the text bounds (if not null, this object will be updated by this method to match the string bounds).
        Returns:
        The offsets.
      • drawRotatedString

        public static void drawRotatedString​(java.lang.String text,
                                             java.awt.Graphics2D g2,
                                             double angle,
                                             float x,
                                             float y)
        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​(java.lang.String text,
                                             java.awt.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.
      • drawRotatedString

        public static void drawRotatedString​(java.lang.String text,
                                             java.awt.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​(java.lang.String text,
                                             java.awt.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.
      • calculateRotatedStringBounds

        public static java.awt.Shape calculateRotatedStringBounds​(java.lang.String text,
                                                                  java.awt.Graphics2D g2,
                                                                  float x,
                                                                  float y,
                                                                  TextAnchor textAnchor,
                                                                  double angle,
                                                                  TextAnchor rotationAnchor)
        Returns a shape that represents the bounds of the string after the specified rotation has been applied.
        Parameters:
        text - the text (null permitted).
        g2 - the graphics device.
        x - the x coordinate for the anchor point.
        y - the y coordinate for the anchor point.
        textAnchor - the text anchor.
        angle - the angle.
        rotationAnchor - the rotation anchor.
        Returns:
        The bounds (possibly null).
      • deriveTextBoundsAnchorOffsets

        private static float[] deriveTextBoundsAnchorOffsets​(java.awt.Graphics2D g2,
                                                             java.lang.String text,
                                                             TextAnchor anchor)
        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 (not null).
        text - the text.
        anchor - the anchor point.
        Returns:
        The offsets.
      • deriveRotationAnchorOffsets

        private static float[] deriveRotationAnchorOffsets​(java.awt.Graphics2D g2,
                                                           java.lang.String text,
                                                           TextAnchor anchor)
        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.
      • calculateRotatedStringBounds

        public static java.awt.Shape calculateRotatedStringBounds​(java.lang.String text,
                                                                  java.awt.Graphics2D g2,
                                                                  float textX,
                                                                  float textY,
                                                                  double angle,
                                                                  float rotateX,
                                                                  float rotateY)
        Returns a shape that represents the bounds of the string after the specified rotation has been applied.
        Parameters:
        text - the text (null permitted).
        g2 - the graphics device.
        textX - the x coordinate for the text.
        textY - the y coordinate for the text.
        angle - the angle.
        rotateX - the x coordinate for the rotation point.
        rotateY - the y coordinate for the rotation point.
        Returns:
        The bounds (null if text is null or has zero length).
      • getUseFontMetricsGetStringBounds

        public static boolean getUseFontMetricsGetStringBounds()
        Returns the flag that controls whether the FontMetrics.getStringBounds() method is used or not. If you are having trouble with label alignment or positioning, try changing the value of this flag.
        Returns:
        A boolean.
      • setUseFontMetricsGetStringBounds

        public static void setUseFontMetricsGetStringBounds​(boolean use)
        Sets the flag that controls whether the FontMetrics.getStringBounds() method is used or not. If you are having trouble with label alignment or positioning, try changing the value of this flag.
        Parameters:
        use - the flag.
      • isUseDrawRotatedStringWorkaround

        public static boolean isUseDrawRotatedStringWorkaround()
        Returns the flag that controls whether or not a workaround is used for drawing rotated strings.
        Returns:
        A boolean.
      • setUseDrawRotatedStringWorkaround

        public static void setUseDrawRotatedStringWorkaround​(boolean use)
        Sets the flag that controls whether or not a workaround is used for drawing rotated strings. The related bug is on Sun's bug parade (id 4312117) and the workaround involves using a TextLayout instance to draw the text instead of calling the drawString() method in the Graphics2D class.
        Parameters:
        use - the new flag value.
      • getDrawStringsWithFontAttributes

        public static boolean getDrawStringsWithFontAttributes()
        Returns the flag that controls whether or not strings are drawn using the current font attributes (such as underlining, strikethrough etc). The default value is false.
        Returns:
        A boolean.
      • setDrawStringsWithFontAttributes

        public static void setDrawStringsWithFontAttributes​(boolean b)
        Sets the flag that controls whether or not strings are drawn using the current font attributes. This is a hack to allow underlining of titles without big changes to the API. See: http://www.jfree.org/phpBB2/viewtopic.php?p=45459&highlight=#45459
        Parameters:
        b - the new flag value.