From f5348a96776bf50f43360478cd866e82811f0d7c Mon Sep 17 00:00:00 2001 From: zml Date: Mon, 23 Dec 2024 14:08:57 -0800 Subject: [PATCH] feat: text shadow support and adveture 4.18.0 bump --- gradle/libs.versions.toml | 2 +- src/commonMain/resources/web/css/style.css | 1 + .../webui/jvm/minimessage/MiniMessage.kt | 3 +++ .../minimessage/hook/StandardRenderHooks.kt | 22 ++++++++++++++----- 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 1fd7425..916516e 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,5 +1,5 @@ [versions] -adventure = "4.17.0" +adventure = "4.18.0" kotlin = "2.1.0" ktlint = "1.0.1" ktor = "3.0.1" diff --git a/src/commonMain/resources/web/css/style.css b/src/commonMain/resources/web/css/style.css index 0ea252d..2de1bf3 100644 --- a/src/commonMain/resources/web/css/style.css +++ b/src/commonMain/resources/web/css/style.css @@ -4,6 +4,7 @@ } .mc-font { font-family: "Minecraft", monospace; + text-shadow: 3px 3px hsl(0, 0%, 24%); } .mono-font { font-family: monospace, monospace; diff --git a/src/jvmMain/kotlin/net/kyori/adventure/webui/jvm/minimessage/MiniMessage.kt b/src/jvmMain/kotlin/net/kyori/adventure/webui/jvm/minimessage/MiniMessage.kt index 9a6ea68..dfaf63f 100644 --- a/src/jvmMain/kotlin/net/kyori/adventure/webui/jvm/minimessage/MiniMessage.kt +++ b/src/jvmMain/kotlin/net/kyori/adventure/webui/jvm/minimessage/MiniMessage.kt @@ -6,6 +6,7 @@ import io.ktor.server.http.content.defaultResource import io.ktor.server.http.content.resource import io.ktor.server.http.content.resources import io.ktor.server.http.content.static +import io.ktor.server.http.content.staticResources import io.ktor.server.request.receiveText import io.ktor.server.response.respondText import io.ktor.server.routing.get @@ -53,6 +54,7 @@ import net.kyori.adventure.webui.websocket.ParseResult import net.kyori.adventure.webui.websocket.Placeholders import net.kyori.adventure.webui.websocket.Response import java.time.Instant +import net.kyori.adventure.webui.jvm.minimessage.hook.SHADOW_COLOR_RENDER_HOOK private val startedAt = Instant.now() @@ -87,6 +89,7 @@ public fun Application.miniMessage() { component(INSERTION_RENDER_HOOK) component(COMPONENT_CLASS_RENDER_HOOK) component(TEXT_COLOR_RENDER_HOOK) + component(SHADOW_COLOR_RENDER_HOOK) component(TEXT_DECORATION_RENDER_HOOK) component(FONT_RENDER_HOOK) component(TEXT_RENDER_HOOK, 500) // content needs to be set last diff --git a/src/jvmMain/kotlin/net/kyori/adventure/webui/jvm/minimessage/hook/StandardRenderHooks.kt b/src/jvmMain/kotlin/net/kyori/adventure/webui/jvm/minimessage/hook/StandardRenderHooks.kt index 9f2d160..8db57a8 100644 --- a/src/jvmMain/kotlin/net/kyori/adventure/webui/jvm/minimessage/hook/StandardRenderHooks.kt +++ b/src/jvmMain/kotlin/net/kyori/adventure/webui/jvm/minimessage/hook/StandardRenderHooks.kt @@ -4,6 +4,7 @@ import kotlinx.html.classes import net.kyori.adventure.text.Component import net.kyori.adventure.text.TextComponent import net.kyori.adventure.text.event.HoverEvent +import net.kyori.adventure.text.format.ShadowColor import net.kyori.adventure.text.format.TextDecoration import net.kyori.adventure.webui.COMPONENT_CLASS import net.kyori.adventure.webui.DATA_CLICK_EVENT_ACTION @@ -56,16 +57,27 @@ public val COMPONENT_CLASS_RENDER_HOOK: ComponentRenderHook = { _ -> public val TEXT_COLOR_RENDER_HOOK: ComponentRenderHook = { component -> component.color()?.let { color -> addStyle("color: ${color.asHexString()}") - - val r = color.red() / 4.0 - val g = color.green() / 4.0 - val b = color.blue() / 4.0 - addStyle("text-shadow: 3px 3px rgb($r, $g, $b)") } true } +/** A render hook for text coloring and shadow. */ +public val SHADOW_COLOR_RENDER_HOOK: ComponentRenderHook = { component -> + component.shadowColor()?.let { color -> + if (color == ShadowColor.none()) { + addStyle("text-shadow: initial") + } else { + val r = color.red() + val g = color.green() + val b = color.blue() + val a = color.alpha().toFloat() / (0xff).toFloat() + addStyle("text-shadow: 3px 3px rgb($r $g $b / $a)") + } + } + + true +} /** A render hook for text decoration. */ public val TEXT_DECORATION_RENDER_HOOK: ComponentRenderHook = { component -> TextDecoration.NAMES.values().forEach { decoration ->