Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Howto embed font variants in JavaFX

I need to embed local .ttf files on a JavaFX project with some variants (bold, italic and so on) but the css parser seems to not support font properties in @font-face declaration:

@font-face {
    font-family: 'Open Sans', sans-serif;
    src: url('fonts/OpenSans-Regular.ttf');
}
@font-face {
    font-family: 'Open Sans', sans-serif;
    src: url('fonts/OpenSans-Bold.ttf');
    font-weight: bold;
}
@font-face {
    font-family: 'Open Sans', sans-serif;
    src: url('fonts/OpenSans-ExtraBoldItalic.ttf');
    font-style: italic;
}

WARNING: CSS Error parsing jar:file:/home/test/dist/run736633436/test.jar!/it/test/vending/css/application.css: Unexpected TOKEN [:] at [8,15] mag 06, 2015 3:40:15 PM com.sun.javafx.css.parser.CSSParser fontFace WARNING: CSS Error parsing jar:file:/home/test/dist/run736633436/test.jar!/it/test/vending/css/application.css: Unexpected TOKEN [:] at [13,14]

How could I achieve this?

==EDIT==


Notice that the parser complains on font-weight: bold; and font-style: italic; rules.

like image 557
Azathoth Avatar asked May 06 '15 14:05

Azathoth


People also ask

How do I add text to javafx?

Adding Text. To add a text object to your application, use any of the constructors shown in Example 1 through Example 3. Text t = new Text (10, 20, "This is a text sample"); You can also create text objects by using the javafx.

How do you change the color of text in javafx?

If you are designing your Javafx application using SceneBuilder then use -fx-text-fill (if not available as option then write it in style input box) as style and give the color you want,it will change the text color of your Textfield .

How do you change the size of text in javafx?

You can change the font size and color of the text using the setFont() method. This method accepts an object of the Font class. The class named Font of the package javafx.


1 Answers

I think I found a solution. Try to swap the font-weight and font-style attributes with the src attribute. In your case your css would look like:

@font-face {
    font-family: 'Open Sans', sans-serif;
    src: url('fonts/OpenSans-Regular.ttf');
}
@font-face {
    font-family: 'Open Sans', sans-serif;
    font-weight: bold;
    src: url('fonts/OpenSans-Bold.ttf');
}
@font-face {
    font-family: 'Open Sans', sans-serif;
    font-style: italic;
    src: url('fonts/OpenSans-ExtraBoldItalic.ttf');
}

I had the same issue and swapping the attributes did the job. I have no idea why that is but it seems to resolve the issue.

like image 195
Franz Bender Avatar answered Oct 21 '22 05:10

Franz Bender