Java "MONOSPACED" font width - java

I'm creating a game and I'd like to use Java's Font.MONOSPACED font, because it is easy to use (even though it looks disgusting). The problem is that I have no idea about the fonts width-height-ratio, which is very important. I couldn't find a answer online after searching for a while.
If someone knows it or how to find it out, I'd be very grateful.

java.awt.FontMetrics is going to be your friend here.
You get access to it through a Graphics2d object:
Graphics2D gfx = ...;
gfx.setFont(...); // select your preferred font, monospaced or otherwise
FontMetrics metrics = gfx.getFontMetrics();
having done so you can get: (all from the above link to docs)
The width of any single char:
charWidth(char ch)
Returns the advance width of the specified character in this Font.
The width of a string:
stringWidth(String str)
Returns the total advance width for showing the specified String in this Font.
The height of a line
getHeight()
Gets the standard height of a line of text in this font.
I assume you can figure out how to get a width-height ratio from the above, and you can get lots of other information from different FontMetrics methods - check it out.

Related

how to get the size of String when it's rendered as HTML?

Currently i'm developing a new Look and Feel and i had to start from scratch for many reasons , my biggest problem now is getting the width and height of the String in components that have getText() to determine where it should be painted , and i know how to get it for normal text but how to do it if it's rendered as html text ??.
using the FontMetrics really provided every thing needed for plain text but using it for html gives as if it's plain , so i'v tried View.getGraphics() and do the same thing on it with FontMetrics of the view object graphics but still the same while what i want is the Dimension of the text that is painted (the differences that matters like if it contains <br /> the height will not be accurate ! or if the font is changed in html tags ,etc...) .
public Dimension getTextSize(JComponent c, Graphics g, String text) {
//this works if you want to get the size as if it's plain text
//and later in the drawing calculate the descent line distance to draw the text
g.setFont(c.getFont());
FontMetrics fontMetrics = g.getFontMetrics();
Dimension plaintTextSize = new Dimension(fontMetrics.stringWidth(text),fontMetrics.getHeight());
return plaintTextSize;
}
i have tried the above with
View view = (View) JComponent.getClientProperty(BasicHTML.propertyKey);
and pass on the view.getGraphics() to the getTextSize method but still the same .I'v searched a lot about it but i couldn't found any relative results and i got nothing about what class to use and i can't use TextLayout since some times the JComponent might not be a JTextComponent .
please help and thank you.
Edit
it seems like it's a real semi-impossible thing to do so as another solution am gonna settle with View.getPreferredSpan(int axsis) to get the size but some time's it's not reliable to scale the size , i mean like in component's we can predict this conflict but how to predict this in html string painted BasicHTML view to at least handle the size manually .
I haven't tried to do this for over 20 years, but since a JLabel will accept html, try creating a JLabel using your html text and then get the label's preferred size.
sadly searching the whole web with no any satisfying answer for the real question
how to get the size of String when it's rendered as HTML?
but the other question after the edit of how to get differences between the real size and the preferred size of the view using
View.getPreferredSpan(int axsis);
that the return size will give the size of the typed content not the drawn one's
like
if the text was
<html>
<body>
first line<br />
second line <hr />
third line
</body>
</html>
it will use the size of the
View.getPreferredSpan(int axsis);
to get the text size of the first ,second and third line sentences but it will use the Rectangle parameter to determine the end of that <hr /> line .

When i change the width the height changes in itext

When i change the width the height changes in canvas.
ImageData data = ImageDataFactory.create("C:\\800px-Cubicpoly.png");
when i specify:
data.setWidth(100);
data.setHeight(180*2.834646f);
It turns out:
when i specify:
data.setWidth(600);
data.setHeight(180*2.834646f);
It turns out:
I don’t touch the height, why does it change?
doc.add(new Image(data).setPadding(0).setMargins(0,0,0,0));
original img(800x800):
You haven't attached the whole code, but I suppose that you use a default page size (A4). Taking into account that the width of the A4 page is 595 pt (considering margins, it would be even less) and you set the image width as 600pt, I presume that you get the following log message while processing your code: com.itextpdf.layout.renderer.RootRenderer WARN Element does not fit current area.
This lack of area is the reason why you get your image processed in a unexpected way. Just to make you sure, please change the size of the document to a bigger one (for instance, A3) and see that everything is processed as expected.
So what does iText normally do when it's not possible to process an image on a set area? iText autoscales it, trying to take as much place as possible, but to retain the image's proportions. You said that the image's height is changing: but that's the same for the width as well.

Android Text with Spannable image on top/bottom of text

I am developing a simple devotional app, which has a Kannada (a language in India) sentence to be displayed. I am successful in using typeface and displaying the content.
In few places I have word which has a line on top/bottom of the word as shown below. I tried with a spannable image but I am still not able to achieve it properly.
This is a sample of the code which I am referring to. Here I am using a small icon to display it in between the string.
Spannable span1 = new SpannableString("The imageplace");
Drawable android = TestImageActivity.this.getResources().getDrawable(R.drawable.end);
android.setBounds(5, 0, 20, 5);
ImageSpan image = new ImageSpan(android, ImageSpan.ALIGN_BASELINE);
span1.setSpan(image, 3, 4, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
tvTextImage3.setText(span1);
ImageSpan extends ReplacementSpan so any characters you are spanning won't get rendered, as the TextLayout is expecting that the span itself will be doing all the rendering.
What I would recommend is implementing your own ReplacementSpan subclass. Since it looks like your graphics are associated with one character, you would wrap the single character.
In the getSize override, you would use start and end to index into text and get the character(s) you are spanning, then use paint.getTextBounds() to measure the width of the text and return that value. You want the width calculation to work in a way that the width of the span doesn't affect the default spacing of the text.
Another thing this method might need to do is change the FontMetrics by increasing the ascent and descent in order to give you some space to draw the lines.
In the draw override, you use the paint to render the text that isn't being rendered within the span. The paint and font metrics should already have the proper values so that your text render looks like the surrounding text. Of course, you'll also render the line graphics you want.
For some sample code, take a look at my answer to a similar question. This has all the pieces I just discussed.
If you want me to write some code for this, you'll need to provide some code that gives me a starting point with some actual Kannada text along with what the lines are and where they go. I don't even know if Kannada text is LTR or RTL; that might affect how the span subclass is coded. Preferably the text would correspond to the image you posted so I can see how it should look when it's working.

Java- textwrap with drawString()

The normal syntax for java.awt.Graphics.drawString() includes defining the X and Y coordinates for the string. I want to draw a string that has a width and height attribute for text wrapping. Is there a way to specify a java.awt.Rectangle for the string you are trying to draw? Would I have to write something to decide where the word wrap should cut it off or is their an easier way?
Thanks!
This is actually a little bit complicated and somewhat convoluated, but essentially, you need to make use of the LineBreakMeasurer class to help you break the text down into manageable chunks based on the available width
See Drawing Multiple Lines of Text for more details and an example
I don't think there's a built in way to do that, but you can implement your own wrapping if you get the width of the string with something like below:
FontMetrics fontMetrics = new FontMetrics(Graphics.getFont());
int width = fontMetrics.stringWidth("Potentially needs wrapping");
Then split the message into multiple strings as necessary.

Generating FontMetrics from java

am working on generating font metrics on server-side by giving ttf file as input.
Iam trying to Font , FontRenderContext , GlyphVector and GlyphMetrics classes.I have also tried Apache FOP but the metrics generated are not help me.
Would like to know if the following operations are possible?
given unicode , how do I know if this has an independent glyph or it has to be compounded with another glyph etc.
Is it possible to generate font metrics with all these details and use it for calculating my line width
How do I know if a glyph is a ligature and how can I get the glyph width post rendering?
Can I assume that a compounded glyph will always appear to the right?
any pointers to these will help?

Categories