Java gets font size based on height

Is there a (neat) way to get the font size of a certain font (in this case SansSerif) that creates a given height instead of the height of a certain font size?

I could, of course, scroll through the font size or use some form of binary chop, but if possible, I would like to use something a little cleaner and less resource intensive. The best way I've found is to use this .

+4
source share
3 answers

When searching for TextLayout , shown here and here , it will provide the most stringent ratings for a given text.

+2
source

There is no easy way.

At first you know that java provides only five logical font families. All other fonts are not guaranteed to be present on the system that you run the program.

Then no, there is no automatic mapping of font properties on your system. You will need to download them all and program the search for the desired measure.

with this code you can encode available fonts:

 import java.awt.Font; import java.awt.GraphicsEnvironment; public class MainClass { public static void main(String[] a) { GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment(); Font[] fonts = e.getAllFonts(); // Get the fonts for (Font f : fonts) { System.out.println(f.getFontName()); } } } 

and then choose the one that suits your needs. Change the code to display the information you want: weight, for example.

Edit: Pay attention to They are merely font-type names recognized by the Java runtime which must be mapped to some physical font that is installed on your system observation at http://download.oracle.com/javase/1.3/docs/guide /intl/addingfonts.html .

Even logical fonts are not guaranteed to be always the same. What you can do is get the size 10pt and calculate the font size. how

font_size_in_points = ((10 * desidered_measure) / analog_measure_of_the_10pt)

+3
source
 FontMetrics metric = new FontMetrics(new Font("font"), Font.BOLD, 12); metrics.getHeight() 

Perhaps you are looking.

Additional information: http://download.oracle.com/javase/6/docs/api/java/awt/FontMetrics.html

+1
source

Source: https://habr.com/ru/post/1368972/


All Articles