Last active
February 15, 2022 12:11
-
-
Save SKeeneCode/313bcb6183193877f8a3e484581f74cf to your computer and use it in GitHub Desktop.
Using fonts in tornadofx using type-safe css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.geometry.Pos | |
import javafx.scene.text.FontPosture | |
import javafx.scene.text.FontWeight | |
import javafx.scene.text.TextAlignment | |
fun main() { | |
launch<FontApp>() | |
} | |
class FontApp : App(FontView::class, Styles::class) | |
class FontView : View() { | |
override val root = vbox { | |
stackpane { | |
addClass(Styles.headerText) | |
label("I am some header font") | |
} | |
text { | |
addClass(Styles.quoteText) | |
wrappingWidth = 400.0 | |
text = "I am some important quote " + | |
"I am some important quote " + | |
"I am some important quote " + | |
"I am some important quote " + | |
"I am some important...." | |
} | |
text("I am some normal font") | |
} | |
} | |
class Styles : Stylesheet() { | |
private val poppinsBlack = loadFont("/font/Poppins-Black.ttf", 12)!! | |
private val poppinsBlackItalic = loadFont("/font/Poppins-BlackItalic.ttf", 12)!! | |
private val poppinsBold = loadFont("/font/Poppins-Bold.ttf", 12)!! | |
private val poppinsBoldItalic = loadFont("/font/Poppins-BoldItalic.ttf", 12)!! | |
private val poppinsExtraBold = loadFont("/font/Poppins-ExtraBold.ttf", 12)!! | |
private val poppinsExtraBoldItalic = loadFont("/font/Poppins-ExtraBoldItalic.ttf", 12)!! | |
private val poppinsExtraLight = loadFont("/font/Poppins-ExtraLight.ttf", 12)!! | |
private val poppinsExtraLightItalic = loadFont("/font/Poppins-ExtraLightItalic.ttf", 12)!! | |
private val poppinsItalic = loadFont("/font/Poppins-Italic.ttf", 12)!! | |
private val poppinsLight = loadFont("/font/Poppins-Light.ttf", 12)!! | |
private val poppinsLightItalic = loadFont("/font/Poppins-LightItalic.ttf", 12)!! | |
private val poppinsMedium = loadFont("/font/Poppins-Medium.ttf", 12)!! | |
private val poppinsMediumItalic = loadFont("/font/Poppins-MediumItalic.ttf", 12)!! | |
private val poppinsReg = loadFont("/font/Poppins-Regular.ttf", 12)!! | |
private val poppinsSemiBold = loadFont("/font/Poppins-SemiBold.ttf", 12)!! | |
private val poppinsSemiBoldItalic = loadFont("/font/Poppins-SemiBoldItalic.ttf", 12)!! | |
private val poppinsThin = loadFont("/font/Poppins-Thin.ttf", 12)!! | |
private val poppinsThinItalic = loadFont("/font/Poppins-ThinItalic.ttf", 12)!! | |
companion object { | |
val normalText by cssclass() | |
val quoteText by cssclass() | |
val headerText by cssclass() | |
} | |
init { | |
//Families | |
val poppins = mixin { fontFamily = "Poppins" } | |
// Font Weights | |
// We use fontFamily here instead of FontWeight to get around this bug: | |
// https://edencoding.com/resources/css_properties/fx-font-weight/ | |
val black = mixin { fontFamily = "Poppins Black" } | |
val extraBold = mixin { fontFamily = "Poppins ExtraBold" } | |
val bold = mixin { fontFamily = "Poppins Bold" } | |
val semiBold = mixin { fontFamily = "Poppins SemiBold" } | |
val medium = mixin { fontFamily = "Poppins Medium" } | |
val regular = mixin { fontFamily = "Poppins Regular" } | |
val light = mixin { fontFamily = "Poppins Light" } | |
val extraLight = mixin { fontFamily = "Poppins ExtraLight" } | |
val thin = mixin { fontFamily = "Poppins Thin" } | |
// Font Postures | |
val normalPosture = mixin { fontStyle = FontPosture.REGULAR } | |
val italic = mixin { fontStyle = FontPosture.ITALIC } | |
// Font Sizes | |
val small = mixin { fontSize = 12.px } | |
val normalSize = mixin { fontSize = 16.px } | |
val large = mixin { fontSize = 28.px } | |
// Alignments | |
val rightAlign = mixin { textAlignment = TextAlignment.RIGHT } | |
root { +poppins; +normalPosture; +normalSize } | |
quoteText { +light; +italic; +small; +rightAlign } | |
headerText { +bold; +large; alignment = Pos.CENTER } | |
} | |
} |
Author
SKeeneCode
commented
Apr 9, 2021
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment