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.
Related
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.
I have a Font object and I need both the width and height of the font. I know that the getSize() returns the height of the font as the point-size of a font is generally the height, but I'm at a loss when it comes to determining the width of the font.
Alternatively, being able to determine the width of each specific character supported by the Font would also be an acceptable solution.
My best guess is that the width is specified when using the loadFont method, but the documentation does not specify whether the size parameter represents the width or the height of the font.
All fonts being used are mono-space fonts such as DejaVu Sans Mono, GNU FreeMono, and Lucida Sans Unicode.
I have run into the same problem, and there seems to be no easy way. My solution was to create a Text element with the correct font and check it's size:
double computeTextWidth(Font font, String text, double wrappingWidth) {
Text helper = new Text();
helper.setFont(font);
helper.setText(text);
// Note that the wrapping width needs to be set to zero before
// getting the text's real preferred width.
helper.setWrappingWidth(0);
helper.setLineSpacing(0);
double w = Math.min(helper.prefWidth(-1), wrappingWidth);
helper.setWrappingWidth((int)Math.ceil(w));
double textWidth = Math.ceil(helper.getLayoutBounds().getWidth());
return textWidth;
}
If I remember correctly the trick here is to set prefWidth to -1. See the JavaDoc.
FontMetrics metrics = Toolkit.getToolkit().getFontLoader().getFontMetrics(font);
float charWidth = metrics.computeStringWidth("a");
float charHeight = metrics.getLineHeight();
The text view i am using in the application is supposed to be single line and has a predefined width which may contain text fetched from database ranging from single word to maximum 3 words. I found a link in stack overflow for adjusting the text size according to height of the text view but i am looking for something more particular to width of text view.
This will work, but you might find faster ways posted. The incrementation in the loop does cost some time, so use sparingly in a single layout.
Display display = this.getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x; //Get screen width. There are other ways to do this too.
int tSize = 10;
tv.setTextSize(tSize); //tv is your TextView
while (tv.getPaint().measureText((String) tv.getText())
< width){
tSize++;
tv.setTextSize(tSize); //Increment up till screen width is filled.
}
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've large string, i want to split it. i got screen width and height using below code,
DisplayMetrics metrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
screenHeight = metrics.heightPixels;
screenWidth = metrics.widthPixels;
I want to know how many character to display on screen.
how to calculate ? and split the string.?
On a Swing / AWT Java platform, you could use a FontMetrics object to measure the width of the particular characters you are trying to display.
References:
How to calculate the font's width?
But it would probably be simpler to use something that can take care of the character rendering and wrapping for you.
On the Android platform, the Paint class has a number of methods that will help you do this kind of thing.
i think if you set width property to wrapcontent than string automatically split and display on next line.
use this property
android:layout_width="wrap_content"
android:layout_height="wrap_content"
use the substring method
String substring(int startIndex, int endIndex)
split the strings per line and store them in an array of Strings
String originalString=" ....some long text " ;
String stringsByLine[]= new String(screenHeight);
int i,j=0;
for ( i = 0; j<originalString.length;i++,j++){
stringsByLine[i]=originalString.substring(j,j+screenWidth);
j+=screenWidth;
}
havent tried it myself, but this logic should work. :-)