Skip to content

Commit

Permalink
Merge pull request #2 from cbruegg/fix/flag-size
Browse files Browse the repository at this point in the history
Fix: Noto flag emojis exceed bounds
  • Loading branch information
SalomonBrys authored May 7, 2024
2 parents d49d46b + 07a4784 commit e241fdf
Showing 1 changed file with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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)
}
}
}
}
Expand Down

0 comments on commit e241fdf

Please sign in to comment.