Correctly show non-square custom emojis (#3711)

Instead of forcing all emojis to be square, only the width is fixed now and the emoji scaled to fit.

Fixes #635
This commit is contained in:
Konrad Pozniak 2023-06-11 20:06:34 +02:00 committed by GitHub
parent 5755801e6f
commit 3698c72109
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 19 additions and 4 deletions

View File

@ -84,11 +84,26 @@ class EmojiSpan(val viewWeakReference: WeakReference<View>) : ReplacementSpan()
imageDrawable?.let { drawable ->
canvas.save()
val emojiSize = (paint.textSize * 1.1).toInt()
drawable.setBounds(0, 0, emojiSize, emojiSize)
// start with a width relative to the text size
var emojiWidth = paint.textSize * 1.1
var transY = bottom - drawable.bounds.bottom
transY -= paint.fontMetricsInt.descent / 2
// calculate the height, keeping the aspect ratio correct
val drawableWidth = drawable.intrinsicWidth
val drawableHeight = drawable.intrinsicHeight
var emojiHeight = emojiWidth / drawableWidth * drawableHeight
// how much vertical space there is draw the emoji
val drawableSpace = (bottom - top).toDouble()
// in case the calculated height is bigger than the available space, scale the emoji down, preserving aspect ratio
if (emojiHeight > drawableSpace) {
emojiWidth *= drawableSpace / emojiHeight
emojiHeight = drawableSpace
}
drawable.setBounds(0, 0, emojiWidth.toInt(), emojiHeight.toInt())
// vertically center the emoji in the line
val transY = top + (drawableSpace / 2 - emojiHeight / 2)
canvas.translate(x, transY.toFloat())
drawable.draw(canvas)