Skip to content

Commit 5e99381

Browse files
committed
updating tests
1 parent b9b9fea commit 5e99381

File tree

2 files changed

+48
-39
lines changed

2 files changed

+48
-39
lines changed

utility/src/main/java/org/oppia/android/util/parser/html/MathTagHandler.kt

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,9 @@ import android.graphics.Paint
77
import android.graphics.drawable.Drawable
88
import android.text.Editable
99
import android.text.Spannable
10-
import android.text.style.DynamicDrawableSpan
1110
import android.text.style.ImageSpan
12-
import android.text.style.ReplacementSpan
1311
import androidx.core.content.res.ResourcesCompat
1412
import io.github.karino2.kotlitex.view.MathExpressionSpan
15-
import java.lang.ref.WeakReference
1613
import org.json.JSONObject
1714
import org.oppia.android.util.R
1815
import org.oppia.android.util.logging.ConsoleLogger
@@ -154,44 +151,56 @@ class MathTagHandler(
154151

155152
/** An [ImageSpan] that vertically centers a LaTeX drawable within the surrounding text. */
156153
private class LatexImageSpan(
157-
private val drawable: Drawable,
158-
private val isInline: Boolean
159-
) : ReplacementSpan() {
154+
imageDrawable: Drawable?,
155+
private val isInlineMode: Boolean
156+
) : ImageSpan(imageDrawable ?: createEmptyDrawable()) {
157+
160158
companion object {
161-
private const val INLINE_SHIFT_FACTOR = 0.9f // Adjust this value (0.2-0.4) as needed
159+
private const val INLINE_VERTICAL_SHIFT_RATIO = 0.9f
160+
161+
private fun createEmptyDrawable(): Drawable {
162+
return object : Drawable() {
163+
override fun draw(canvas: Canvas) {}
164+
override fun setAlpha(alpha: Int) {}
165+
override fun setColorFilter(colorFilter: android.graphics.ColorFilter?) {}
166+
override fun getOpacity(): Int = android.graphics.PixelFormat.TRANSPARENT
167+
168+
init {
169+
setBounds(0, 0, 1, 1)
170+
}
171+
}
172+
}
162173
}
163174

164175
override fun getSize(
165176
paint: Paint,
166177
text: CharSequence,
167178
start: Int,
168179
end: Int,
169-
fm: Paint.FontMetricsInt?
180+
fontMetrics: Paint.FontMetricsInt?
170181
): Int {
171-
val bounds = drawable.bounds
172-
val imageHeight = bounds.height()
173-
val paintMetrics = paint.fontMetricsInt
174-
val textHeight = paintMetrics.descent - paintMetrics.ascent
175-
176-
fm?.let { metrics ->
177-
if (isInline) {
178-
// Reserve space for inline shift
182+
val drawableBounds = drawable.bounds
183+
val imageHeight = drawableBounds.height()
184+
val textMetrics = paint.fontMetricsInt
185+
val textHeight = textMetrics.descent - textMetrics.ascent
186+
187+
fontMetrics?.let { metrics ->
188+
if (isInlineMode) {
179189
val verticalShift = (imageHeight - textHeight) / 2 +
180-
(paintMetrics.descent * INLINE_SHIFT_FACTOR).toInt()
181-
metrics.ascent = paintMetrics.ascent - verticalShift
190+
(textMetrics.descent * INLINE_VERTICAL_SHIFT_RATIO).toInt()
191+
metrics.ascent = textMetrics.ascent - verticalShift
182192
metrics.top = metrics.ascent
183-
metrics.descent = paintMetrics.descent + verticalShift
193+
metrics.descent = textMetrics.descent + verticalShift
184194
metrics.bottom = metrics.descent
185195
} else {
186-
// Block mode calculations remain unchanged
187196
val totalHeight = (imageHeight * 1.2).toInt()
188197
metrics.ascent = -totalHeight / 2
189198
metrics.top = metrics.ascent
190199
metrics.descent = totalHeight / 2
191200
metrics.bottom = metrics.descent
192201
}
193202
}
194-
return bounds.right
203+
return drawableBounds.right
195204
}
196205

197206
override fun draw(
@@ -208,20 +217,17 @@ private class LatexImageSpan(
208217
canvas.save()
209218

210219
val imageHeight = drawable.bounds.height()
211-
val yPosition = when {
212-
isInline -> {
213-
// Apply downward shift for inline equations
214-
val textMidline = baseline - (paint.fontMetrics.descent - paint.fontMetrics.ascent) / 2
215-
val shiftOffset = (paint.fontMetricsInt.descent * INLINE_SHIFT_FACTOR).toInt()
216-
textMidline - (imageHeight / 2) + shiftOffset
217-
}
218-
else -> {
219-
// Block mode remains centered
220-
lineTop + (lineBottom - lineTop - imageHeight) / 2
221-
}
220+
val yOffset = if (isInlineMode) {
221+
val metrics = paint.fontMetricsInt
222+
val ascent = metrics.ascent.toFloat()
223+
val descent = metrics.descent.toFloat()
224+
val expectedCenterY = baseline.toFloat() + (ascent + descent) / 2f
225+
expectedCenterY - (imageHeight / 2f)
226+
} else {
227+
lineTop.toFloat() + (lineBottom - lineTop - imageHeight) / 2f
222228
}
223229

224-
canvas.translate(x, yPosition.toFloat())
230+
canvas.translate(x, yOffset)
225231
drawable.draw(canvas)
226232
canvas.restore()
227233
}

utility/src/test/java/org/oppia/android/util/parser/html/MathTagHandlerTest.kt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ class MathTagHandlerTest {
155155

156156
val metrics = paint.fontMetricsInt
157157
val y = 100
158-
val expectedCenterY = y + (metrics.descent + metrics.ascent) / 2f
159158

160159
imageSpans[0].draw(
161160
mockCanvas,
@@ -168,17 +167,20 @@ class MathTagHandlerTest {
168167
200,
169168
paint
170169
)
171-
// The canvas should be translated to center the drawable vertically
170+
171+
val textHeight = (metrics.descent - metrics.ascent).toFloat()
172+
val textMidline = y.toFloat() - (textHeight / 2f)
173+
val verticalShift = metrics.descent * 0.9f
174+
val drawable = imageSpans[0].drawable
175+
val expectedTranslation = textMidline + verticalShift - (drawable.bounds.height() / 2f)
176+
177+
// The translation should position the drawable centered around the text baseline
172178
verify(mockCanvas).save()
173179
verify(mockCanvas).translate(
174180
eq(0f),
175181
capture(floatCaptor)
176182
)
177-
// The translation should position the drawable centered around the text baseline
178-
val drawable = imageSpans[0].drawable
179-
val expectedTranslation = expectedCenterY - (drawable.bounds.height() / 2)
180183
assertThat(floatCaptor.value).isWithin(1f).of(expectedTranslation)
181-
182184
verify(mockCanvas).restore()
183185
}
184186

@@ -233,6 +235,7 @@ class MathTagHandlerTest {
233235
val lineHeight = paint.textSize * 1.2f
234236
assertThat(totalHeight.toFloat()).isLessThan(lineHeight)
235237
}
238+
236239
@Test
237240
fun testParseHtml_emptyString_doesNotIncludeImageSpan() {
238241
val parsedHtml =

0 commit comments

Comments
 (0)