Hey, I've been stuck on this problem for quite a w...
# tornadofx
d
Hey, I've been stuck on this problem for quite a while now... So I'm trying to add my custom fonts from resources, and I have this CSS code for adding them:
Copy code
@font-face {
    -fx-font-family: "Poppins", poppins;
    -fx-font-weight: bold;
    src: url('../font/Poppins-Bold.ttf');
}
@font-face {
    -fx-font-family: "Poppins", poppins;
    -fx-font-weight: medium;
    src: url('../font/Poppins-Medium.ttf');
}
@font-face {
    -fx-font-family: "Poppins", poppins;
    -fx-font-weight: medium;
    -fx-font-style: italic;
    src: url('../font/Poppins-MediumItalic.ttf');
}
But when I tried to apply them it doesn't work. I mostly use medium and when I try to set the font-weight, on a label let's say I get this error:
Copy code
main-view.css: Expected '<font-weight>' while parsing '-fx-font-weight' at [13,21]
I have found some StackOverflow questions, but none of the answers seem to have helped.
v
Does -fx-font-weight have medium property?
d
It does, but I have also tried with 500px, and same issue.
v
Really? What JavaFx version you use?
d
I followed the TornadoFX setup guide and I use jdk 11.
v
<font-weight> The font's weight, using the following syntax: [ normal | bold | bolder | lighter | 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900 ]
d
I see, but as I said, I tried 500px.
v
Did you use 500px string or just 500?
d
I used 500px, because my ide recommended it.
v
I think you should use 500 without px
d
So @Vladimir N. how do I fix this?
v
Can you try to replace
medium
to
500
?
Copy code
@font-face {
    -fx-font-family: "Poppins", poppins;
    -fx-font-weight: 500;
    -fx-font-style: italic;
    src: url('../font/Poppins-MediumItalic.ttf');
}
d
Without px?
Alright I'll try that when I get home.
@Vladimir N. no errors now, that's good! But it seems like I'm just getting the regular version of the font rendered.
Looking at my app in scenic view it says, it's using `System Bold`/`System Regular`as the font. Which is obviously not what I want.
s
@diniamo try this, I created a minimal example for you: https://gist.github.com/SKeeneCode/313bcb6183193877f8a3e484581f74cf
Also I do like that font... might have to use it now in my applications :)
d
@Samkeene I see, thanks! But I wanted to keep all the styling in css.
s
Are you not using tornadofx?
Nevertheless https://edencoding.com/resources/css_properties/fx-font-weight/ should be all you need if you want to remain pure css!
d
I am using tornadofx, yes.
Alright I tried doing that, didn't work, I'll make the repo public and send the link here, one second.
https://github.com/diniamo/sm-pixel-art-creator Here you go. It's still using System fonts.
s
What exactly did you try? Also, consider upgrading to javafx 16 and tornadofx2, as it may be something that was fixed in later versions.
d
Well, you can look at the project for what I tried? And how exactly do I upgrade?
s
Well, you can upgrade javafx easy enough in your build gradle. You clone and build tornadofx2 yourself : https://github.com/edvin/tornadofx2 or get its snapshot release from sonotypes repo. You haven't mentioned any css errors in the console so I'm assuming the fonts are loaded correctly. The other css properties you have in main-view.css such as border color and width are being correctly applied to the appropriate node right?
I cloned and built your project - the fonts will work if you move the
@font-face
declarations into the
main-view.css
- this isn't ideal, maybe its a quirk of javafx css that I am not aware..
d
No errors, and yes the other stuff is getting applied.
Moving it is not what I wanted, and I use the font in another css file.
s
using
loadFont
also works
Oh I finally figured it out
Always the silly things
your
font-face
declarations need to be the first thing in the css file, you have a
root
declaration above them. Move that under and it all works.
d
Sounds stupid, but I'll try that.
It works, thank you so much.