diff --git a/WORKSPACE b/WORKSPACE index c6c1f09af5f..7b21505c587 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -141,7 +141,7 @@ git_repository( # min target SDK version to be compatible with Oppia. git_repository( name = "kotlitex", - commit = "6b7db8ff9e0f4a70bdaa25f482143e038fd0c301", + commit = "43139c140833c7120f351d63d74b42c253d2b213", remote = "https://github.com/oppia/kotlitex", shallow_since = "1647554845 -0700", ) diff --git a/utility/BUILD.bazel b/utility/BUILD.bazel index 96fcde1f0a1..aff05e104d5 100644 --- a/utility/BUILD.bazel +++ b/utility/BUILD.bazel @@ -33,6 +33,7 @@ android_library( custom_package = "org.oppia.android.util", manifest = "src/main/AndroidManifest.xml", resource_files = glob(["src/main/res/**/*.xml"]), + visibility = ["//visibility:public"], ) # Visibility for migrated utility tests. diff --git a/utility/build.gradle b/utility/build.gradle index 4af0a029e0e..568eac1841b 100644 --- a/utility/build.gradle +++ b/utility/build.gradle @@ -83,7 +83,7 @@ dependencies { 'androidx.lifecycle:lifecycle-livedata-ktx:2.2.0-alpha03', 'androidx.work:work-runtime-ktx:2.4.0', 'com.github.oppia:androidsvg:4bc1d26412f0fb9fd4ef263fa93f6a64f4d4dbcf', - 'com.github.oppia:kotlitex:6b7db8ff9e0f4a70bdaa25f482143e038fd0c301', + 'com.github.oppia:kotlitex:43139c140833c7120f351d63d74b42c253d2b213', 'com.github.bumptech.glide:glide:4.11.0', 'com.google.dagger:dagger:2.24', 'com.google.firebase:firebase-analytics-ktx:17.5.0', diff --git a/utility/src/main/java/org/oppia/android/util/parser/html/HtmlParser.kt b/utility/src/main/java/org/oppia/android/util/parser/html/HtmlParser.kt index 7aef1725c76..ca7b8c0b661 100755 --- a/utility/src/main/java/org/oppia/android/util/parser/html/HtmlParser.kt +++ b/utility/src/main/java/org/oppia/android/util/parser/html/HtmlParser.kt @@ -1,5 +1,6 @@ package org.oppia.android.util.parser.html +import android.app.Application import android.content.Context import android.text.Spannable import android.text.SpannableStringBuilder @@ -134,7 +135,8 @@ class HtmlParser private constructor( consoleLogger, context.assets, htmlContentTextView.lineHeight.toFloat(), - cacheLatexRendering + cacheLatexRendering, + context as? Application ?: context.applicationContext as Application ) if (supportsConceptCards) { handlersMap[CUSTOM_CONCEPT_CARD_TAG] = conceptCardTagHandler diff --git a/utility/src/main/java/org/oppia/android/util/parser/html/MathTagHandler.kt b/utility/src/main/java/org/oppia/android/util/parser/html/MathTagHandler.kt index 9e20b7715ea..42ce1a0676e 100644 --- a/utility/src/main/java/org/oppia/android/util/parser/html/MathTagHandler.kt +++ b/utility/src/main/java/org/oppia/android/util/parser/html/MathTagHandler.kt @@ -1,11 +1,14 @@ package org.oppia.android.util.parser.html +import android.app.Application import android.content.res.AssetManager import android.text.Editable import android.text.Spannable import android.text.style.ImageSpan +import androidx.core.content.res.ResourcesCompat import io.github.karino2.kotlitex.view.MathExpressionSpan import org.json.JSONObject +import org.oppia.android.util.R import org.oppia.android.util.logging.ConsoleLogger import org.oppia.android.util.parser.html.CustomHtmlContentHandler.ImageRetriever.Type.BLOCK_IMAGE import org.oppia.android.util.parser.html.CustomHtmlContentHandler.ImageRetriever.Type.INLINE_TEXT_IMAGE @@ -24,7 +27,8 @@ class MathTagHandler( private val consoleLogger: ConsoleLogger, private val assetManager: AssetManager, private val lineHeight: Float, - private val cacheLatexRendering: Boolean + private val cacheLatexRendering: Boolean, + private val application: Application ) : CustomHtmlContentHandler.CustomTagHandler { override fun handleTag( attributes: Attributes, @@ -65,7 +69,15 @@ class MathTagHandler( ) } else { MathExpressionSpan( - content.rawLatex, lineHeight, assetManager, isMathMode = !useInlineRendering + content.rawLatex, + lineHeight, + assetManager, + isMathMode = !useInlineRendering, + ResourcesCompat.getColor( + application.resources, + R.color.component_color_shared_equation_color, + /* theme = */ null + ) ) } } diff --git a/utility/src/main/java/org/oppia/android/util/parser/math/BUILD.bazel b/utility/src/main/java/org/oppia/android/util/parser/math/BUILD.bazel index 2beb8af1107..14eee4919a5 100644 --- a/utility/src/main/java/org/oppia/android/util/parser/math/BUILD.bazel +++ b/utility/src/main/java/org/oppia/android/util/parser/math/BUILD.bazel @@ -28,6 +28,7 @@ kt_android_library( ":math_latex_model", "//third_party:com_github_bumptech_glide_glide", "//third_party:io_github_karino2_kotlitex", + "//utility:resources", "//utility/src/main/java/org/oppia/android/util/logging:console_logger_injector_provider", "//utility/src/main/java/org/oppia/android/util/threading:dispatcher_injector_provider", ], diff --git a/utility/src/main/java/org/oppia/android/util/parser/math/MathBitmapModelLoader.kt b/utility/src/main/java/org/oppia/android/util/parser/math/MathBitmapModelLoader.kt index 73100116aea..605529a7f4c 100644 --- a/utility/src/main/java/org/oppia/android/util/parser/math/MathBitmapModelLoader.kt +++ b/utility/src/main/java/org/oppia/android/util/parser/math/MathBitmapModelLoader.kt @@ -12,6 +12,7 @@ import android.text.Spannable import android.text.SpannableStringBuilder import android.text.StaticLayout import android.text.TextPaint +import androidx.core.content.res.ResourcesCompat import com.bumptech.glide.Priority import com.bumptech.glide.load.DataSource import com.bumptech.glide.load.Options @@ -26,6 +27,7 @@ import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.launch import kotlinx.coroutines.withContext +import org.oppia.android.util.R import org.oppia.android.util.logging.ConsoleLogger import org.oppia.android.util.logging.ConsoleLoggerInjectorProvider import org.oppia.android.util.threading.DispatcherInjectorProvider @@ -109,7 +111,16 @@ class MathBitmapModelLoader private constructor( // creation can still happen in parallel, and those are the more expensive steps. val span = withContext(CoroutineScope(blockingDispatcher).coroutineContext) { MathExpressionSpan( - model.rawLatex, model.lineHeight, application.assets, !model.useInlineRendering + model.rawLatex, + model.lineHeight, + application.assets, + !model.useInlineRendering, + // TODO(#1523): Test color parameter in MathBitmapModelLoader + ResourcesCompat.getColor( + application.resources, + R.color.component_color_shared_equation_color, + /* theme = */null + ) ).also { it.ensureDrawable() } } val renderableText = SpannableStringBuilder("\uFFFC").apply { @@ -121,6 +132,7 @@ class MathBitmapModelLoader private constructor( // since the width isn't necessarily known ahead of time). // Any TextPaint can be used since the span will use its own. val textPaint = TextPaint() + @Suppress("DEPRECATION") // This call is necessary for the supported min API version. val staticTextLayout = StaticLayout( diff --git a/utility/src/main/res/values-night/color_palette.xml b/utility/src/main/res/values-night/color_palette.xml new file mode 100644 index 00000000000..172ed64d460 --- /dev/null +++ b/utility/src/main/res/values-night/color_palette.xml @@ -0,0 +1,5 @@ + + + + @color/color_def_white + diff --git a/utility/src/main/res/values/color_defs.xml b/utility/src/main/res/values/color_defs.xml new file mode 100644 index 00000000000..ec5a46fc52d --- /dev/null +++ b/utility/src/main/res/values/color_defs.xml @@ -0,0 +1,6 @@ + + + + #000000 + #FFFFFF + diff --git a/utility/src/main/res/values/color_palette.xml b/utility/src/main/res/values/color_palette.xml new file mode 100644 index 00000000000..39430259019 --- /dev/null +++ b/utility/src/main/res/values/color_palette.xml @@ -0,0 +1,5 @@ + + + + @color/color_def_black + diff --git a/utility/src/main/res/values/component_colors.xml b/utility/src/main/res/values/component_colors.xml new file mode 100644 index 00000000000..e3bfeccce95 --- /dev/null +++ b/utility/src/main/res/values/component_colors.xml @@ -0,0 +1,5 @@ + + + + @color/color_palette_shared_equation_color + diff --git a/utility/src/test/java/org/oppia/android/util/parser/html/MathTagHandlerTest.kt b/utility/src/test/java/org/oppia/android/util/parser/html/MathTagHandlerTest.kt index 870ea193d7c..535d5c90e58 100644 --- a/utility/src/test/java/org/oppia/android/util/parser/html/MathTagHandlerTest.kt +++ b/utility/src/test/java/org/oppia/android/util/parser/html/MathTagHandlerTest.kt @@ -2,6 +2,7 @@ package org.oppia.android.util.parser.html import android.app.Application import android.content.Context +import android.graphics.Color import android.text.Html import android.text.Spannable import android.text.style.ImageSpan @@ -34,6 +35,7 @@ import org.oppia.android.util.logging.ConsoleLogger import org.oppia.android.util.logging.LoggerModule import org.oppia.android.util.parser.html.CustomHtmlContentHandler.CustomTagHandler import org.oppia.android.util.parser.html.CustomHtmlContentHandler.ImageRetriever +import org.robolectric.annotation.Config import org.robolectric.annotation.LooperMode import javax.inject.Inject import javax.inject.Singleton @@ -246,6 +248,33 @@ class MathTagHandlerTest { verifyNoMoreInteractions(mockImageRetriever) // No cached image loading. } + @Test + fun testParseHtmlDayColor_withMathMarkup_cachingOff_getEquationDayColor() { + val parsedHtml = + CustomHtmlContentHandler.fromHtml( + html = MATH_WITHOUT_FILENAME_MARKUP, + imageRetriever = mockImageRetriever, + customTagHandlers = tagHandlersWithUncachedMathSupport + ) + + val equationColor = parsedHtml.getSpansFromWholeString(MathExpressionSpan::class) + assertThat(equationColor[0].equationColor).isEqualTo(Color.BLACK) + } + + @Config(qualifiers = "night") + @Test + fun testParseHtmlNightColor_withMathMarkup_cachingOff_getEquationNightColor() { + val parsedHtml = + CustomHtmlContentHandler.fromHtml( + html = MATH_WITHOUT_FILENAME_MARKUP, + imageRetriever = mockImageRetriever, + customTagHandlers = tagHandlersWithUncachedMathSupport + ) + + val equationColor = parsedHtml.getSpansFromWholeString(MathExpressionSpan::class) + assertThat(equationColor[0].equationColor).isEqualTo(Color.WHITE) + } + @Test fun testParseHtml_noTagHandler_withMathMarkup_doesNotIncludeImageSpan() { val parsedHtml = @@ -306,7 +335,13 @@ class MathTagHandlerTest { private fun createMathTagHandler(cacheLatexRendering: Boolean): MathTagHandler { // Pick an arbitrary line height since rendering doesn't actually happen in tests. - return MathTagHandler(consoleLogger, context.assets, lineHeight = 10.0f, cacheLatexRendering) + return MathTagHandler( + consoleLogger, + context.assets, + lineHeight = 10.0f, + cacheLatexRendering, + application = context.applicationContext as Application + ) } private fun Spannable.getSpansFromWholeString(spanClass: KClass): Array =