From 07a47844a9f21be73071ad423ac770421b80efb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Br=C3=BCggemann?= Date: Sun, 21 Apr 2024 15:14:55 +0200 Subject: [PATCH] Fix: Noto flag emojis exceed bounds --- .../kodein/emoji/compose/skiaVectorImages.kt | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/emoji-compose/src/skiaMain/kotlin/org/kodein/emoji/compose/skiaVectorImages.kt b/emoji-compose/src/skiaMain/kotlin/org/kodein/emoji/compose/skiaVectorImages.kt index fe77c9d..8359ad2 100644 --- a/emoji-compose/src/skiaMain/kotlin/org/kodein/emoji/compose/skiaVectorImages.kt +++ b/emoji-compose/src/skiaMain/kotlin/org/kodein/emoji/compose/skiaVectorImages.kt @@ -16,7 +16,8 @@ import org.jetbrains.skia.Rect import org.jetbrains.skia.skottie.Animation import org.jetbrains.skia.sksg.InvalidationController import org.jetbrains.skia.svg.SVGDOM -import org.jetbrains.skia.svg.SVGLengthContext +import org.jetbrains.skia.svg.SVGLengthUnit +import kotlin.math.min import kotlin.math.roundToInt @@ -41,9 +42,24 @@ internal actual fun SVGImage(image: SVGImage, contentDescription: String, modifi this.role = Role.Image } ) { - image.dom.setContainerSize(size.width, size.height) + val svgWidth = image.dom.root?.width + val svgHeight = image.dom.root?.height + // If the SVG has width+height specs instead of viewBox, Skia will not honor the container size + val scaleManually = + svgWidth?.unit == SVGLengthUnit.NUMBER && svgHeight?.unit == SVGLengthUnit.NUMBER + if (!scaleManually) { + image.dom.setContainerSize(size.width, size.height) + } drawIntoCanvas { canvas -> - image.dom.render(canvas.nativeCanvas) + if (svgWidth != null && svgHeight != null && scaleManually) { + val scaleFactor = min(size.width / svgWidth.value, size.height / svgHeight.value) + canvas.save() + canvas.scale(scaleFactor, scaleFactor) + image.dom.render(canvas.nativeCanvas) + canvas.restore() + } else { + image.dom.render(canvas.nativeCanvas) + } } } }