| Method from org.jfree.text.TextUtilities Detail: |
public static Shape calculateRotatedStringBounds(String text,
Graphics2D g2,
float x,
float y,
TextAnchor textAnchor,
double angle,
TextAnchor rotationAnchor) {
if (text == null || text.equals("")) {
return null;
}
final float[] textAdj = deriveTextBoundsAnchorOffsets(g2, text,
textAnchor);
if (logger.isDebugEnabled()) {
logger.debug("TextBoundsAnchorOffsets = " + textAdj[0] + ", "
+ textAdj[1]);
}
final float[] rotateAdj = deriveRotationAnchorOffsets(g2, text,
rotationAnchor);
if (logger.isDebugEnabled()) {
logger.debug("RotationAnchorOffsets = " + rotateAdj[0] + ", "
+ rotateAdj[1]);
}
final Shape result = calculateRotatedStringBounds(text, g2,
x + textAdj[0], y + textAdj[1], angle,
x + textAdj[0] + rotateAdj[0], y + textAdj[1] + rotateAdj[1]);
return result;
}
Returns a shape that represents the bounds of the string after the
specified rotation has been applied. |
public static Shape calculateRotatedStringBounds(String text,
Graphics2D g2,
float textX,
float textY,
double angle,
float rotateX,
float rotateY) {
if ((text == null) || (text.equals(""))) {
return null;
}
final FontMetrics fm = g2.getFontMetrics();
final Rectangle2D bounds = TextUtilities.getTextBounds(text, g2, fm);
final AffineTransform translate = AffineTransform.getTranslateInstance(
textX, textY);
final Shape translatedBounds = translate.createTransformedShape(bounds);
final AffineTransform rotate = AffineTransform.getRotateInstance(
angle, rotateX, rotateY);
final Shape result = rotate.createTransformedShape(translatedBounds);
return result;
}
Returns a shape that represents the bounds of the string after the
specified rotation has been applied. |
public static TextBlock createTextBlock(String text,
Font font,
Paint paint) {
if (text == null) {
throw new IllegalArgumentException("Null 'text' argument.");
}
final TextBlock result = new TextBlock();
String input = text;
boolean moreInputToProcess = (text.length() > 0);
final int start = 0;
while (moreInputToProcess) {
final int index = input.indexOf("\n");
if (index > start) {
final String line = input.substring(start, index);
if (index < input.length() - 1) {
result.addLine(line, font, paint);
input = input.substring(index + 1);
}
else {
moreInputToProcess = false;
}
}
else if (index == start) {
if (index < input.length() - 1) {
input = input.substring(index + 1);
}
else {
moreInputToProcess = false;
}
}
else {
result.addLine(input, font, paint);
moreInputToProcess = false;
}
}
return result;
}
Creates a TextBlock from a String. Line breaks
are added where the String contains '\n' characters. |
public static TextBlock createTextBlock(String text,
Font font,
Paint paint,
float maxWidth,
TextMeasurer measurer) {
return createTextBlock(text, font, paint, maxWidth, Integer.MAX_VALUE,
measurer);
}
Creates a new text block from the given string, breaking the
text into lines so that the maxWidth value is
respected. |
public static TextBlock createTextBlock(String text,
Font font,
Paint paint,
float maxWidth,
int maxLines,
TextMeasurer measurer) {
final TextBlock result = new TextBlock();
final BreakIterator iterator = BreakIterator.getLineInstance();
iterator.setText(text);
int current = 0;
int lines = 0;
final int length = text.length();
while (current < length && lines < maxLines) {
final int next = nextLineBreak(text, current, maxWidth, iterator,
measurer);
if (next == BreakIterator.DONE) {
result.addLine(text.substring(current), font, paint);
return result;
}
result.addLine(text.substring(current, next), font, paint);
lines++;
current = next;
while (current < text.length()&& text.charAt(current) == '\n") {
current++;
}
}
if (current < length) {
final TextLine lastLine = result.getLastLine();
final TextFragment lastFragment = lastLine.getLastTextFragment();
final String oldStr = lastFragment.getText();
String newStr = "...";
if (oldStr.length() > 3) {
newStr = oldStr.substring(0, oldStr.length() - 3) + "...";
}
lastLine.removeFragment(lastFragment);
final TextFragment newFragment = new TextFragment(newStr,
lastFragment.getFont(), lastFragment.getPaint());
lastLine.addFragment(newFragment);
}
return result;
}
Creates a new text block from the given string, breaking the
text into lines so that the maxWidth value is
respected. |
public static Rectangle2D drawAlignedString(String text,
Graphics2D g2,
float x,
float y,
TextAnchor anchor) {
final Rectangle2D textBounds = new Rectangle2D.Double();
final float[] adjust = deriveTextBoundsAnchorOffsets(g2, text, anchor,
textBounds);
// adjust text bounds to match string position
textBounds.setRect(x + adjust[0], y + adjust[1] + adjust[2],
textBounds.getWidth(), textBounds.getHeight());
g2.drawString(text, x + adjust[0], y + adjust[1]);
return textBounds;
}
Draws a string such that the specified anchor point is aligned to the
given (x, y) location. |
public static void drawRotatedString(String text,
Graphics2D g2,
double angle,
float x,
float y) {
drawRotatedString(text, g2, x, y, angle, x, 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). |
public static void drawRotatedString(String text,
Graphics2D g2,
float textX,
float textY,
double angle,
float rotateX,
float rotateY) {
if ((text == null) || (text.equals(""))) {
return;
}
final AffineTransform saved = g2.getTransform();
// apply the rotation...
final AffineTransform rotate = AffineTransform.getRotateInstance(
angle, rotateX, rotateY);
g2.transform(rotate);
if (useDrawRotatedStringWorkaround) {
// workaround for JDC bug ID 4312117 and others...
final TextLayout tl = new TextLayout(text, g2.getFont(),
g2.getFontRenderContext());
tl.draw(g2, textX, textY);
}
else {
// replaces this code...
g2.drawString(text, textX, textY);
}
g2.setTransform(saved);
}
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). |
public static void drawRotatedString(String text,
Graphics2D g2,
float x,
float y,
TextAnchor textAnchor,
double angle,
TextAnchor rotationAnchor) {
if (text == null || text.equals("")) {
return;
}
final float[] textAdj = deriveTextBoundsAnchorOffsets(g2, text,
textAnchor);
final float[] rotateAdj = deriveRotationAnchorOffsets(g2, text,
rotationAnchor);
drawRotatedString(text, g2, x + textAdj[0], y + textAdj[1],
angle, x + textAdj[0] + rotateAdj[0],
y + textAdj[1] + rotateAdj[1]);
}
Draws a string that is aligned by one anchor point and rotated about
another anchor point. |
public static void drawRotatedString(String text,
Graphics2D g2,
float x,
float y,
TextAnchor textAnchor,
double angle,
float rotationX,
float rotationY) {
if (text == null || text.equals("")) {
return;
}
final float[] textAdj = deriveTextBoundsAnchorOffsets(g2, text,
textAnchor);
drawRotatedString(text, g2, x + textAdj[0], y + textAdj[1], angle,
rotationX, rotationY);
}
Draws a string that is aligned by one anchor point and rotated about
another anchor point. |
public static Rectangle2D getTextBounds(String text,
Graphics2D g2,
FontMetrics fm) {
final Rectangle2D bounds;
if (TextUtilities.useFontMetricsGetStringBounds) {
bounds = fm.getStringBounds(text, g2);
// getStringBounds() can return incorrect height for some Unicode
// characters...see bug parade 6183356, let's replace it with
// something correct
LineMetrics lm = fm.getFont().getLineMetrics(text,
g2.getFontRenderContext());
bounds.setRect(bounds.getX(), bounds.getY(), bounds.getWidth(),
lm.getHeight());
}
else {
final double width = fm.stringWidth(text);
final double height = fm.getHeight();
if (logger.isDebugEnabled()) {
logger.debug("Height = " + height);
}
bounds = new Rectangle2D.Double(0.0, -fm.getAscent(), width,
height);
}
return bounds;
}
Returns the bounds for the specified text. |
public static boolean getUseFontMetricsGetStringBounds() {
return useFontMetricsGetStringBounds;
}
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. |
public static boolean isUseDrawRotatedStringWorkaround() {
return useDrawRotatedStringWorkaround;
}
|
public static void setUseDrawRotatedStringWorkaround(boolean use) {
useDrawRotatedStringWorkaround = 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. |
public static void setUseFontMetricsGetStringBounds(boolean use) {
useFontMetricsGetStringBounds = 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. |