@@ -149,8 +149,7 @@ class MathTagHandler(
149149}
150150
151151/* * An [ImageSpan] that vertically centers a LaTeX drawable within the surrounding text. */
152- private class LatexImageSpan (drawable : Drawable ? ) :
153- ImageSpan (drawable ? : createEmptyDrawable()) {
152+ private class LatexImageSpan (drawable : Drawable ) : ImageSpan(drawable) {
154153
155154 override fun getSize (
156155 paint : Paint ,
@@ -159,23 +158,23 @@ private class LatexImageSpan(drawable: Drawable?) :
159158 end : Int ,
160159 fontMetricsInt : Paint .FontMetricsInt ?
161160 ): Int {
162- val latexDrawable = drawable
163- val drawableBounds = latexDrawable .bounds
164-
165- fontMetricsInt?.let { metrics ->
166- val paintFontMetrics = paint.fontMetricsInt
167- val textHeight = paintFontMetrics .descent - paintFontMetrics .ascent
168- val latexHeight = drawableBounds.height()
169-
170- val centeringOffset = (textHeight - latexHeight) / 2
171-
172- metrics .ascent = paintFontMetrics.ascent + centeringOffset
173- metrics .top = paintFontMetrics.top + centeringOffset
174- metrics.descent = metrics.ascent + latexHeight
175- metrics.bottom = metrics.top + latexHeight
161+ val drawable = drawable
162+ val rect = drawable .bounds
163+
164+ fontMetricsInt?.let { fm ->
165+ val paintMetrics = paint.fontMetricsInt
166+ val fontHeight = paintMetrics .descent - paintMetrics .ascent
167+ val drawableHeight = rect.bottom - rect.top
168+ val centerY = paintMetrics.ascent + fontHeight / 2
169+
170+ // Adjust font metrics to center the drawable vertically
171+ fm .ascent = centerY - drawableHeight / 2
172+ fm .top = fm.ascent
173+ fm.bottom = centerY + drawableHeight / 2
174+ fm.descent = fm.bottom
176175 }
177176
178- return drawableBounds .right
177+ return rect .right
179178 }
180179
181180 override fun draw (
@@ -189,32 +188,17 @@ private class LatexImageSpan(drawable: Drawable?) :
189188 bottom : Int ,
190189 paint : Paint
191190 ) {
192- val latexDrawable = drawable
191+ val drawable = drawable
193192 canvas.save()
194193
195- val fontMetrics = paint.fontMetricsInt
196- val latexHeight = latexDrawable.bounds.height()
194+ // Calculate vertical centering
195+ val paintMetrics = paint.fontMetricsInt
196+ val fontHeight = paintMetrics.descent - paintMetrics.ascent
197+ val centerY = y + paintMetrics.descent - fontHeight / 2
198+ val transY = centerY - (drawable.bounds.bottom - drawable.bounds.top) / 2
197199
198- val centerY = y + (fontMetrics.descent + fontMetrics.ascent) / 2
199- val drawableY = centerY - (latexHeight / 2 )
200-
201- canvas.translate(x, drawableY.toFloat())
202- latexDrawable.draw(canvas)
200+ canvas.translate(x, transY.toFloat())
201+ drawable.draw(canvas)
203202 canvas.restore()
204203 }
205-
206- companion object {
207- private fun createEmptyDrawable (): Drawable {
208- return object : Drawable () {
209- override fun draw (canvas : Canvas ) {}
210- override fun setAlpha (alpha : Int ) {}
211- override fun setColorFilter (colorFilter : android.graphics.ColorFilter ? ) {}
212- override fun getOpacity (): Int = android.graphics.PixelFormat .TRANSPARENT
213-
214- init {
215- setBounds(0 , 0 , 1 , 1 )
216- }
217- }
218- }
219- }
220204}
0 commit comments