11package io.github.rosemoe.sora.lsp.editor.text
22
3- import android.annotation.SuppressLint
43import android.graphics.Bitmap
54import android.graphics.BitmapFactory
65import android.graphics.Typeface
76import android.graphics.drawable.BitmapDrawable
87import android.graphics.drawable.Drawable
9- import android.os.Build
108import android.text.SpannableStringBuilder
119import android.text.Spanned
1210import android.text.TextPaint
@@ -18,7 +16,6 @@ import android.text.style.RelativeSizeSpan
1816import android.text.style.StyleSpan
1917import android.text.style.URLSpan
2018import android.util.Base64
21- import androidx.annotation.RequiresApi
2219import java.util.Locale
2320
2421object SimpleMarkdownRenderer {
@@ -410,10 +407,11 @@ object SimpleMarkdownRenderer {
410407 val start = builder.length
411408 val drawable = globalImageProvider.load(inline.url)
412409 if (drawable != null ) {
410+ drawable.setBounds(0 , 0 , drawable.intrinsicWidth, drawable.intrinsicHeight)
413411 builder.append(' \uFFFC ' ) // Object replacement character
414412 val end = builder.length
415413 builder.setSpan(
416- ImageSpan (drawable, ImageSpan .ALIGN_BOTTOM ),
414+ ImageSpan (drawable, ImageSpan .ALIGN_CENTER ),
417415 start,
418416 end,
419417 SPAN_MODE
@@ -882,23 +880,36 @@ object SimpleMarkdownRenderer {
882880 *
883881 * @param maxWidth Maximum width for returned Bitmaps. Images wider than this will be scaled down preserving aspect ratio.
884882 */
885- class DefaultImageProvider (
883+ open class DefaultImageProvider (
886884 private val maxWidth : Int = 800
887885 ): ImageProvider {
888886 override fun load (src : String ): Drawable ? {
889- if (src.startsWith(" data:" )) {
890- val payload = src.substringAfter(" base64," )
891- val imageByteArray = try {
892- Base64 .decode(payload, Base64 .DEFAULT )
893- } catch (_: Exception ) {
894- return null
895- }
896- val bitmap = BitmapFactory .decodeByteArray(imageByteArray, 0 , imageByteArray.size) ? : return null
897- val scaledBitmap = scaleIfNeeded(bitmap, maxWidth)
898- val drawable = BitmapDrawable (scaledBitmap)
899- return drawable
887+ if (! src.startsWith(" data:" )) return null
888+
889+ val payload = src.substringAfter(" base64," , " " )
890+ if (payload.isEmpty()) return null
891+
892+ val imageByteArray = try {
893+ Base64 .decode(payload, Base64 .DEFAULT )
894+ } catch (_: Exception ) {
895+ return null
900896 }
901- return null
897+ val bitmap = BitmapFactory .decodeByteArray(imageByteArray, 0 , imageByteArray.size) ? : return null
898+ val scaledBitmap = scaleIfNeeded(bitmap, maxWidth)
899+ return BitmapDrawable (scaledBitmap)
900+ }
901+
902+ /* *
903+ * Scale down a bitmap to maxWidth preserving aspect ratio. If bitmap width
904+ * is already <= maxWidth, the original bitmap is returned.
905+ */
906+ private fun scaleIfNeeded (bmp : Bitmap , maxWidth : Int ): Bitmap {
907+ val currentWidth = bmp.width
908+ if (currentWidth <= maxWidth) return bmp
909+ val ratio = maxWidth.toFloat() / currentWidth.toFloat()
910+
911+ val newHeight = (bmp.height * ratio).toInt()
912+ return Bitmap .createScaledBitmap(bmp, maxWidth, newHeight, true )
902913 }
903914 }
904915
@@ -911,20 +922,6 @@ object SimpleMarkdownRenderer {
911922 fun load (src : String ): Drawable ?
912923 }
913924
914- /* *
915- * Scale down a bitmap to maxWidth preserving aspect ratio. If bitmap width
916- * is already <= maxWidth, the original bitmap is returned.
917- */
918- @SuppressLint(" UseKtx" )
919- private fun scaleIfNeeded (bmp : Bitmap , maxWidth : Int ): Bitmap {
920- val currentWidth = bmp.width
921- if (currentWidth <= maxWidth) return bmp
922- val ratio = maxWidth.toFloat() / currentWidth.toFloat()
923-
924- val newHeight = (bmp.height * ratio).toInt()
925- return Bitmap .createScaledBitmap(bmp, maxWidth, newHeight, true )
926- }
927-
928925 private sealed interface Block {
929926 class Heading (val level : Int , val inlines : List <Inline >) : Block
930927 class Paragraph (val inlines : List <Inline >) : Block
0 commit comments