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:
parent
5755801e6f
commit
3698c72109
|
@ -84,11 +84,26 @@ class EmojiSpan(val viewWeakReference: WeakReference<View>) : ReplacementSpan()
|
||||||
imageDrawable?.let { drawable ->
|
imageDrawable?.let { drawable ->
|
||||||
canvas.save()
|
canvas.save()
|
||||||
|
|
||||||
val emojiSize = (paint.textSize * 1.1).toInt()
|
// start with a width relative to the text size
|
||||||
drawable.setBounds(0, 0, emojiSize, emojiSize)
|
var emojiWidth = paint.textSize * 1.1
|
||||||
|
|
||||||
var transY = bottom - drawable.bounds.bottom
|
// calculate the height, keeping the aspect ratio correct
|
||||||
transY -= paint.fontMetricsInt.descent / 2
|
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())
|
canvas.translate(x, transY.toFloat())
|
||||||
drawable.draw(canvas)
|
drawable.draw(canvas)
|
||||||
|
|
Loading…
Reference in New Issue