From an xml file, I'm given a width, height and id. All of them can and do vary very quickly. Now, I'm asked to draw a rectangle using the width and height (an easy task), and place the id at its center. The id must not overflow out of the rectangle it's contained it.
For single-character strings, this is also easy - set the font size to the height, play a bit with the x position maybe, and it's centered. The problem is when it's multi-character strings.
So given a width and height and a string, how can you determine what font-size the string should appear in? Assume you have every bit of information you need on the rectangle you're drawing the string in.
[Edit]: I'm using the Graphics 2D class to draw everything.
Start with selecting a Font at your preferred (i.e. maximum) size.
Grab the FontRenderContext from your Graphics2D object using getFontRenderContext.
Use getStringBounds() on the Font to be rendered to get a Rectangle2D object for the specific String to be rendered. That object describes the final size of the String using that Font
Check if the size specified by that Rectangle2D is small enough.
4a. If it is small enough, you're done. Use the last Font you've checked.
4b. If it is too big, use Font.derive() to produce a smaller version of the Font and continue to use that and loop back to 3.
Don't quite have the time to give you a full working example, but here are a couple pointers that should get you going in the right direction. The graphics object you are using to draw with has a getFontMetrics() method, one of the methods on FontMetrics is stringWidth(String str) which gives you the width of a string in the current Font.
If the width is too big for your rectangle set the Font on the Graphics object to the same font just with a smaller size until it fits.
To horizontally center a string in a container (learned long ago in typing class in high school):
(rectangleWidth / 2) - (stringWidth / 2)
http://download.oracle.com/javase/1.5.0/docs/api/java/awt/FontMetrics.html
To create a Font with a smaller size, something like:
Font font = graphics.getFont();
Font smallerFont = font.derive(font.getSize() - 1);
graphics.setFont(smallerFont);
Hope this gets you going in the right direction.
I would recommend for this problem to remove as many unknowns as possible. In this case, the problem chiefly is that font characters can vary in width... well most. That's why I would use a good monospace font like courier new for the ID, that way you know what the width of each character is, you know the width of your rectangle and you know the number of characters in your string. You can simply reduce the pixel size of each character will till your string fits the available width.
Example, if the width of each character is 12px and you have 10 characters in your ID, then you need 120px to fit everything in. If you only have 80px available, it's simple math 80/10 = 8px font-size (reduce half a pixel for padding if you want.
Just my suggestion.
Related
I'm trying to display text inside a rectangle which I have the dimensions of (x, y, width, and height). However, if the text's length is long enough to the point where the text would leak outside the rectangle, I want to instead display as many characters as possible in a single line, followed by ellipses (...). I am aware that I can use the Graphics.getFontMetrics().stringWidth(text) to get the width the text would occupy in pixels. I could use this method by looping through, adding a character at a time to the string that would be displayed and checking if its width exceeds the rectangle's width. However, this feels inefficient.
As such, I want to know if there is a better/efficient way of doing this.
I'm currently drawing a string to a canvas with a specified font. I would, however, like to scale this font based on the window size.
Given a target string, how do I find the point size of a particular font face so that printing the target string will be either h units tall, or w units wide? Is there a linear relationship between point size and font dimensions?
I can think of very smelly ways to determine a relative point size (pick an arbitrary size and shrink / grow until the dimensions are within some epsilon of the target), but would rather do it more cleanly.
I want to do this with fonts-only, if possible, and not resort to affine transformations.
For the best metrics, I prefer TextLayout, illustrated here, but deriveFont(), suggested by #StanislavL among the answers here, is surprisingly agile and not at all malodorous.
In Java when the text of the JLabel could not be displayed due to lack of space the text is truncated and "..." is added in the end.
How can I easily find out if currently JLabel displays full text or the truncated?
EDIT:
I see that there is a way to find out the size of the text by using FontMetrics. However this solution doesn't fully answers the question. In the case the text of JLabel contains HTML decorations the metrics.stringWidth() would also calculate width of HTML tags. So it could happen that result of metrics.stringWidth() would be grater than JLabel's width but still the text would be displayed correctly.
Is there a way know what decision took the JLabel itself while displaying the text. Has it decided to truncate the text or not.
The ellipsis is added by the label's UI delegate, typically a subclass of BasicLabelUI, as part of it's layout and preferred size calculation. The method layoutCL() may be overridden to examine the geometry, as shown on this example.
As a practical matter, I'd ignore the elision and show the full text in a tool tip.
From Oracle - Measuring Text:
// get metrics from the graphics
FontMetrics metrics = graphics.getFontMetrics(font);
// get the height of a line of text in this font and render context
int hgt = metrics.getHeight();
// get the advance of my text in this font and render context
int adv = metrics.stringWidth(text);
// calculate the size of a box to hold the text with some padding.
Dimension size = new Dimension(adv+2, hgt+2);
Compare size to the size of the JLabel.getSize();
I suppose if the component's preferred size is greater than it's actual size, then you can expect truncation. In order for this to work, of course, the component must already be realized.
Check this and see the layoutCompoundLabel() method. It returns a String representing the text of the label. You can compare it to the original to determine if it will be clipped.
Jim S.
The usual way to do this is to use a method that calculates the expected size of the text as it will be displayed in the label. If you're using a monospaced font, this is easy:
lengthOfChar * numChars
If you're not using a monospaced font, it's obviously much harder. I think there are some utilities around that will attempted to calculate this.
Once you have the size of the displayed string, you can compare to the length of the JLabel and see if the label is too small.
To get sizes of html text check this https://www.java.net/node/665691.
View view = (View) javax.swing.plaf.basic.BasicHTML.createHTMLView(label, value.toString());
int width = (int) view.getPreferredSpan(View.X_AXIS);
int height = (int) view.getPreferredSpan(View.Y_AXIS);
The only small problem is that it might have issues with non-html text. So, just use font metrics for non-html strings. The following worked perfectly for me:
if (value.toString().startsWith("<html>")) {
View view = (View) javax.swing.plaf.basic.BasicHTML.createHTMLView(label, value.toString());
width = (int) view.getPreferredSpan(View.X_AXIS);
}
else {
width = (int) label.getFontMetrics(label.getFont()).stringWidth(value.toString());
}
Let's say a string rendered with a 10-point font is 200 pixels wide. If I draw the same string with a 15-point font (a 50% increase), will its rendered width also increase by 50% (to 300 pixels)?
(Of course, this assumes no other changes in the font and the Graphics2D where the text is rendered.)
In other words, is there a direct linear relationship between a font's point size and the size text is rendered? Or are there other variables that prevent me from making any such assumptions?
You're close with your assumption.
However, the spacing between letters doesn't always scale exactly. I believe it has to do with integer division sometimes having a remainder, but I haven't looked at the Java FontMetrics class code in a while.
You're safest always creating a new Font with the appropriate font size, and calling the getStringBounds method on the FontMetrics class.
First problem: You have 400 pixels width to go on, and need to fit some text within that constraint as large as possible (thus, the text shall use that amount of space).
Throw in a new constraint: If the text is just "A", then it shall not zoom this above 100 pixels height (or some specific font size).
Then, a final situation: Linebreaks. Fit some text in the largest possible way within e.g. 400 x 150 pixels.
An obvious way is to simply start with point 1, and then increase until you can't fit it anymore. This would work for all three problems, but would be very crude. The fitting of a single line within bounds could be done by writing it with some fixed point size, check the resulting pixel bounds of the text, and then simply scale it with a transform (the text scales properly too then, check out TransformUI).
Any ideas of other ways to attack this would be greatly appreciated!
As what you are modelling is complex, especially with line breaks, then your initial proposal of trying all sizes is along the right lines, especially if it needs to be accurate.
However, rather than testing each value, you can use a binary search to find the appropriate font size. You know the size is somewhere between 1 and 100 (your upper range). using a binary search, each test sets the font size and checks the resulting layout. If the text is too large, then we search the lower half of the current range of possible values. If the font size fits, then we search the upper half. Your search will use at most 7 attempts (100 log base 2 rounded up), it will be exact, finding the largest size without going over, and it will be flexible if you need to add more requirements later, such as a mix of fonts or more stringent constraints on the layout.
I'm assuming you are using a text component that does line wrapping, and that you can set the maximum width to 400. So, you set the font size and it does the layout giving you back the required height, laying out text within the given width.
You can use hints to try to guide the algorithm to the result quicker, such as making your first guess close to the expected size, but text rendering is fast, that the performance increase may not be worth the implementation effort.
See Wikipedia - Binary Search Algorithm
I would do the following:
Assume you want W pixels wide text.
Pick an arbitrary size, say 10pt, and see what bounding box the text-string gets for that size. Lets say it gets N pixels wide.
Set the new size to 10pt * W/N, and repeat from step one, until you get within a reasonable threshold. (Hopefully it would work within one iteration.)
This relies on the fact that the width of the string, is roughly proportional to the size of the font.
I'd instantiate the Font at the largest desired size: say 72 for one inch glyphs at 72 dpi. Use TextLayout to get the bounds and scale using AffineTransform (direct) or AffineTransformOp (offscreen), while preserving the aspect ratio. Suitable RenderingHints help, too.