package com.kaixed.kchat.utils

import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.drawable.Drawable
import android.text.style.ImageSpan
import android.util.Log


/**
 * @Author: kaixed
 * @Date: 2024/10/28 15:57
 */
class VerticalAlignImageSpan : ImageSpan {
    constructor(d: Drawable?) : super(d!!)

    constructor(d: Drawable?, verticalAlignment: Int) : super(d!!, verticalAlignment)

    override fun draw(
        canvas: Canvas,
        text: CharSequence?,
        start: Int,
        end: Int,
        x: Float,
        top: Int,
        y: Int,
        bottom: Int,
        paint: Paint
    ) {
        val drawable = drawable
        val fontMetrics = paint.fontMetrics
        val transY =
            ((y + fontMetrics.ascent + y + fontMetrics.descent) / 2 - (drawable.bounds.bottom + drawable
                .bounds.top) / 2).toInt()
        Log.d("VerticalAlignImageSpan", "transY-> $transY")
        canvas.save()
        canvas.translate(x, transY.toFloat())
        drawable.draw(canvas)
        canvas.restore()
    }
}
