javafx - Unable to load external TTF fonts -
for reason unable load ttf fonts in app. here simple example:
file f = new file("/blah/font.ttf"); system.out.println(f.canread()); system.out.println(f.length()); system.out.println(font.loadfont(new fileinputstream(f), 20)); the output:
true 52168 null the font not loaded. tried different fonts, none of them worked. problem? i'm running arch linux.
$ java -version java version "1.8.0_45" java(tm) se runtime environment (build 1.8.0_45-b14) java hotspot(tm) 64-bit server vm (build 25.45-b02, mixed mode)
assuming have package structure in application this

the following code should work
import java.io.inputstream; import javafx.scene.text.font; public class accesstest { public static void main(string[] args) throws urisyntaxexception, malformedurlexception { inputstream = accesstest.class.getresourceasstream("opensans-regular.ttf"); font font = font.loadfont(is, 12.0); system.out.println("font: " +font); file f = new file(accesstest.class.getresource("opensans-regular.ttf").touri()); // should not used because of f.tourl() deprecated font font1 = font.loadfont(f.tourl().toexternalform(), 12.0); system.out.println("font 1: " + font1); font font2 = font.loadfont(f.touri().tourl().toexternalform(), 12.0); system.out.println("font 2: " + font2); } } this gives me following output:
font: font[name=open sans, family=open sans, style=regular, size=12.0] font 1: font[name=open sans, family=open sans, style=regular, size=12.0] font 2: font[name=open sans, family=open sans, style=regular, size=12.0] if put font file outside of package there access violation. in case font.load() method breaks in construction suitable inputstream. bit tricky suitable url or uri load method uses.
Comments
Post a Comment