I've read all the existing questions I could find and have tried it both ways.
FontMetrics fm = g.getFontMetrics();
FontRenderContext frc = g.getFontRenderContext();
Rectangle2D rect = font.getStringBounds(line,frc);
int width = (int)rect.getWidth();
and also
int width = fm.stringWidth(line);
Neither are giving me the correct number of pixels.
As an example...the word 'ELLIS' in a particular font and size is actually 58 pixels wide.
Both of those methods tell me that it's 42 wide.
Its black font on a white field, so I'm considering re arranging my entire code so I can loop through a line of the BufferedImage and count the distance between the first and last black pixel. This would at least get me a lot closer.
There has to be a simpler way to do this though.
Appreciate any help.
Related
I met a strange requirements:let the non-monospace font right align,is there are some way to do it?how can detect the actual length for the non-monospace font string?
use the class FontMetrics provided by
FontMetrics myFontMetrics = myGraphics.getFontMetrics()
the call
int width = myFontMetrics.stringWidth(myString);
gives you the string width in pixels, which you can subtract from your drawing coordinate.
I want to align a String drawn by a canvas perfectly in the middle. So if I have code like so
Paint p = new Paint();
Canvas c = holder.lockCanvas();
String centered_text = "Hello World";
and then set my Paint style and size
p.setTextSize(35);
p.setStyle(Style.STROKE);
Now to draw my String in the middle I would do
c.drawText(centered_text, c.getWidth()/2, c.getHeight()/2, p);
But this wouldn't actually center my text, it would put the top left corner in the center. To center it I would need to know the Strings litteral width and height in pixels not characters.
int string_width = centered_text.getWidth(); //String.getWidth() is not a real methode
int string_height = centered_text.getHeight(); //String.getHeight() is not a real methode
c.drawText(centered_text, (c.getWidth()/2)-(string_width/2), (c.getHeight()/2)-(string_height/2), p);
This would center the text but String.getWidth() and String.getHeight() are not real methodes. So my idea is to use the size of the text and the size of the String to find the width and the height. So something along te lines of:
int string_width = centered_text.length()*p.getTextSize();
int string_height = p.getTextSize();
But I feel like this is wrong because different chars are different sizes... anyone have any ideas.
Assuming you're in Java Swing, what you want is FontMetrics.
new FontMetrics(canvas.getFont()).getStringBounds(someString, canvas.getGraphics());
will return a Rectangle2D with the size of your string in it.
I am trying to place two words at the center of the image. But the drawString method does not seem to correctly pickup the "x" coodinate. For example I am trying to place the words "setupa" and "asetup" (image width 30). My image width is 106, thus the x-cord value is 38 in both the cases. But in reality asetup is places at 1-2 pixel shift.
It produces just minor difference, but that shows up in my images.
Sample code is follows.
Graphics2D textGraphics;
textGraphics = image.createGraphics();
textGraphics.setColor(fontColor);
textGraphics.setFont(font);
FontRenderContext frc = new FontRenderContext(null, true, true);
TextLayout layout = new TextLayout(label, font, frc);
Rectangle r = layout.getPixelBounds(frc, 0, 0);
textGraphics.drawString(label, ((imageWidth / 2) - (r.width/2)), (imageWidth / 2));
Is they any way around this? or a better way to place the text at the center.
Thanks
Is there any way how to determine the optimal canvas size for text rendering?
The input is a string with newlines, I want to contruct the canvas to fit (no insets) while using both font types - proportional and non-proportional, these types will be never mixed.
Thanks.
From the Java Tutorial Measuring Text
FontMetrics metrics = graphics.getFontMetrics(font);
int hgt = metrics.getHeight();
int adv = metrics.stringWidth(text);
Dimension size = new Dimension(adv+2, hgt+2);
You probably need to do this line by line of your text and detect whether your font changes between lines.
From the following code, we can get the bounding rectangle of our text:
Graphics2D twoD = (Graphics2D) g;
FontRenderContext frc = twoD.getFontRenderContext();
Rectangle2D textBound = myFont.getStringBounds(myText, frc);
but in core java textbook, it says that
the rectangle has its origin at the
baseline of the string, and the top
y-coordinate of the rectangle is
negative.
What is that mean, why the top y-coordinate is negative?
And any more clarification to this concept is highly appreciated?
Thanks
When you measure text, there is no concept of where you are drawing it. So they make the somewhat arbitrary, but i suppose sensible decision that the point (0, 0) is at the baseline of the text, to the left of the first character.
This is good because if you were to say
void drawSomeText(Graphics g, String sample) {
g.drawString(sample, 0, 0);
}
it would fit in the box that measure text told you about.
Given that the ascent of the text goes more and more negative, while the descenders occupy positive y space.