Set font height as float, instead of integer
Asked Answered
Z

2

9

Is there a way to specify the font size as a float/double instead of integer. I am creating my font as:

Font font = new Font(shell.getDisplay(), "CormorantGaramond",14, SWT.NULL);

where 14 is the font size in points. But the constructor only accept an integer, which I find odd when the font size is specified in points and not pixel. Is there a way to specify font size as a non integer value. (It is a ttf font, if that matter).

Zed answered 3/8, 2017 at 16:56 Comment(1)
I was going to suggest create awt.Font and set the height as a float deriveFont(float size) then convert AWT to SWT. In Eclipse 3.0, APIs have been introduced for integrating AWT and Swing with SWT. but AWT font is restricted to be converted to an int before the conversion. -eclipse.org/articles/…. I am posting this just so you don't go down this dead end.Bark
F
2

It seems that currently (Eclipse Platform Mars 4.5) there is no platform-independent way to set the font height using the value of the float type.

Reference: org.eclipse.swt.graphics.FontData (Eclipse Platform API Specification) - Help - Eclipse Platform.

Fiacre answered 13/8, 2017 at 20:49 Comment(1)
But the odd thing is that the width field itself is a float. (I know, not part of the api, but still).Zed
W
1

There is a way you can set the font size in float. Java provide a built in function "public Font deriveFont(float size)" in ava.awt.Font class. This deriveFont creates a new instance of the Font based on the values you provide, so you will need to maintain a reference to it

And then you have to use the SWTUtils class to convert awt to swt Here it is SWTUtils.java

Download this file and add it to your project. Thats it.

Workmanlike answered 20/8, 2017 at 11:40 Comment(2)
I will look at this tomorrow, but a quick glance at the code shows it cast the font size to an integer and give it to the font object so not sure that it works. But it also says it calculate the size in PIXEL, and then gives this pixel size to setHeight which according to the documentation for setHeight should take the size in points. So maybe it really does calculate the correct pixels size which is an integer.Zed
I tried it myself. i am satisfied with the result. let me know if it works for you.Workmanlike

© 2022 - 2024 — McMap. All rights reserved.