edvin / tornadofx

Lightweight JavaFX Framework for Kotlin
Apache License 2.0
3.68k stars 271 forks source link

Custom font #1313

Closed Blazingsteal closed 3 years ago

Blazingsteal commented 3 years ago

Hi

I've been trying for a while to load a custom font in my tornadofx project but cannot seem to get it to work. Finding resources about how this works is very limited as well. This is the best example I've found but that doesn't seem to work.

This is what I've tried so far:

I've created a folder called "resources" in which I've placed my fonts. /src/main/resources/

val sourceSansPro = loadFont("/resources/fonts/Source_Sans_Pro/SourceSansPro-SemiBold.ttf", 20.0)!!

and setting the font with either:

font = sourceSansPro

or

fontFamily = sourceSansPro.family

but I cannot compile this.

Error: https://pastebin.pl/view/ae45f620

SchweinchenFuntik commented 3 years ago

version tornadofx ? Java 9+ using tornadofx 2.0-SNAPSHOT

Blazingsteal commented 3 years ago

Not using Tornadofx 2 Using Tornadofx 1.7.20 with java 8

SchweinchenFuntik commented 3 years ago

with java 8

at java.base/jdk.internal.reflect.

I see 9+ in the logs. Make sure to compile and run on the same jdk version

Blazingsteal commented 3 years ago

I checked and saw that I was most likely using openJDK 11 to compile. I've made sure I only have java 8 now but I'm still getting an error.

SchweinchenFuntik commented 3 years ago

show a minimal code example where the problem is reproduced

CsonkaDavid commented 3 years ago

Same problem here. Using openjdk 11 and this thing never compiles, not even with paths provided by the IDEA:

val buttonFont = loadFont("misc/blackops.ttf",18)

It's just the Black Ops One Regular font style's ttf file renamed and put into the misc folder of my project and yet loadFont provided by tornadofx always gives the NullPointerExceptionError.

CsonkaDavid commented 3 years ago

Same problem here. Using openjdk 11 and this thing never compiles, not even with paths provided by the IDEA:

val buttonFont = loadFont("misc/blackops.ttf",18)

It's just the Black Ops One Regular font style's ttf file renamed and put into the misc folder of my project and yet loadFont provided by tornadofx always gives the NullPointerExceptionError.

Well as it seems I was trying to change the style of a button but I have to change the font type of a text in a label to do what I wanted, that way I don't get an error, but the label of the button dissapears.

Blazingsteal commented 3 years ago

@SchweinchenFuntik Here's a minimal code example producing the error.

MainView.kt

package com.example.view

import com.example.Styles
import tornadofx.*

class MainView : View("Hello TornadoFX") {
    override val root = hbox {
        label(title) {
            addClass(Styles.heading)
        }
    }
}

Styles.kt

package com.example

import javafx.scene.text.FontWeight
import tornadofx.Stylesheet
import tornadofx.box
import tornadofx.cssclass
import tornadofx.px
import tornadofx.loadFont

class Styles : Stylesheet() {
    companion object {
        val heading by cssclass()
        val testFont1 = loadFont("SourceSansPro-SemiBold.ttf", 20.0)!!
        val testFont2 = loadFont("resources/SourceSansPro-SemiBold.ttf", 20.0)!!

    }

    init {
        heading {
            padding = box(10.px)
            fontSize = 20.px
            fontWeight = FontWeight.BOLD
        }
        label {
            padding = box(10.px)
            fontSize = 20.px
            fontWeight = FontWeight.BOLD
            fontFamily = testFont1.family
        }
    }
}

As @ImSamhel also experienced this results in a NPE when trying to load the font

CsonkaDavid commented 3 years ago

Ye well, whatever I tried to do, It won't load fonts from ttf or otf files :/

SchweinchenFuntik commented 3 years ago

Ye well, whatever I tried to do, It won't load fonts from ttf or otf files :/

what is the path of your files?

PS: val testFont1 = loadFont("/SourceSansPro-SemiBold.ttf", 20.0)!!

CsonkaDavid commented 3 years ago

Ye well, whatever I tried to do, It won't load fonts from ttf or otf files :/

what is the path of your files?

PS: val testFont1 = loadFont("/SourceSansPro-SemiBold.ttf", 20.0)!!

val font = loadFont("/misc/Fontname.ttf", 18.0)!! is what I'm trying to do, but once I try to apply this on a label or text or anything it fails with nullpointerexception

SchweinchenFuntik commented 3 years ago

show the stacktrace

CsonkaDavid commented 3 years ago

I'm gonna just copy it here instead (wouldn't trust external txt either):

Exception in Application start method Exception in Application stop method márc. 08, 2021 8:31:52 DU. tornadofx.DefaultErrorHandler uncaughtException SEVERE: Uncaught error java.lang.RuntimeException: Exception in Application start method at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900) at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195) at java.base/java.lang.Thread.run(Thread.java:834) Caused by: java.lang.ExceptionInInitializerError at app.MainScreen$root$1$1.invoke(MainScreen.kt:21) at app.MainScreen$root$1$1.invoke(MainScreen.kt:17) at tornadofx.CSSKt.style(CSS.kt:1120) at tornadofx.CSSKt.style$default(CSS.kt:1112) at app.MainScreen$root$1.invoke(MainScreen.kt:20) at app.MainScreen$root$1.invoke(MainScreen.kt:17) at tornadofx.LayoutsKt.borderpane(Layouts.kt:465) at app.MainScreen.(MainScreen.kt:19) at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490) at java.base/java.lang.Class.newInstance(Class.java:584) at tornadofx.FXKt.find(FX.kt:434) at tornadofx.FXKt.find$default(FX.kt:423) at tornadofx.App.start(App.kt:83) at app.Application.start(Application.kt:14) at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846) at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455) at com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428) at java.base/java.security.AccessController.doPrivileged(Native Method) at com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427) at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96) at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) at com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174) ... 1 more Caused by: java.lang.NullPointerException at misc.Config.(Config.kt:37) ... 26 more

SchweinchenFuntik commented 3 years ago

@ImSamhel @Blazingsteal download was successful. tornadofx 2.0, Java 15

import javafx.scene.text.FontWeight
import tornadofx.*

class Styles : Stylesheet() {
    companion object {
        val heading by cssclass()
        val fontJBMono = loadFont("/fonts/ttf/JetBrainsMono-Light.ttf", 20)!!
    }

    init {
        heading {
            padding = box(10.px)
            fontSize = 40.px
            fontWeight = FontWeight.BOLD
            fontFamily = fontJBMono.family
        }
        label {
            fontWeight = FontWeight.BOLD
            fontFamily = fontJBMono.family
        }
    }
}

class TestView() : View() {
    override val root = vbox(5) {
        label("Head") { addClass(Styles.heading) }
        label("Label")
    }
}

class TestApp : App(TestView::class, Styles::class)

fun main() {
    launch<TestApp>()
}

зображення

зображення

CsonkaDavid commented 3 years ago

@SchweinchenFuntik

Putting fonts into the resource folder really worked. I don't even know why I didn't think about it. It works for me with Java 11 and TornadoFX 1.7.2 . Thank you.

Blazingsteal commented 3 years ago

@ImSamhel but what about java 8 ? Really dont want to swap to java 9+ just to get this working...

Blazingsteal commented 3 years ago

Screenshot from 2021-03-10 10-10-30 Screenshot from 2021-03-10 10-16-33

Seems as the path to my ttf file wasn't correct and now works as it should as seen in the pictures above.

Embarrassing mistake but happy to not have that issue anymore :)