Skip to content

Commit

Permalink
Add more small image support
Browse files Browse the repository at this point in the history
  • Loading branch information
cooltey committed Nov 8, 2023
1 parent fcd92d1 commit f3da1bc
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
20 changes: 12 additions & 8 deletions app/src/main/java/org/wikipedia/richtext/CustomHtmlParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ import androidx.core.text.toSpanned
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.transition.Transition
import org.wikipedia.WikipediaApp
import org.wikipedia.dataclient.Service
import org.wikipedia.dataclient.WikiSite
import org.wikipedia.util.DimenUtil
import org.wikipedia.util.ResourceUtil
import org.wikipedia.util.UriUtil
import org.wikipedia.util.WhiteBackgroundTransformation
import org.wikipedia.util.log.L
import org.xml.sax.Attributes
Expand Down Expand Up @@ -106,7 +108,8 @@ class CustomHtmlParser constructor(private val handler: TagHandler) : TagHandler
wrapped?.skippedEntity(name)
}

class CustomTagHandler(private val view: TextView?, private val noSmallSizeImage: Boolean = true) : TagHandler {
class CustomTagHandler(private val view: TextView?,
private val noSmallSizeImage: Boolean = true) : TagHandler {
private var lastAClass = ""

override fun handleTag(opening: Boolean, tag: String?, output: Editable?, attributes: Attributes?): Boolean {
Expand All @@ -124,8 +127,8 @@ class CustomHtmlParser constructor(private val handler: TagHandler) : TagHandler
if (imgHeightStr.isEmpty()) {
imgHeightStr = heightRegex.find(styleStr)?.value.orEmpty().replace("height:", "")
}
var imgWidth = DimenUtil.htmlUnitToPxInt(imgWidthStr)
var imgHeight = DimenUtil.htmlUnitToPxInt(imgHeightStr)
var imgWidth = DimenUtil.htmlUnitToPxInt(imgWidthStr) ?: MIN_IMAGE_SIZE
var imgHeight = DimenUtil.htmlUnitToPxInt(imgHeightStr) ?: MIN_IMAGE_SIZE
val imgSrc = getValue(attributes, "src").orEmpty()

if (noSmallSizeImage && (imgWidth < MIN_IMAGE_SIZE || imgHeight < MIN_IMAGE_SIZE)) {
Expand All @@ -147,11 +150,12 @@ class CustomHtmlParser constructor(private val handler: TagHandler) : TagHandler

drawable.setBounds(0, 0, imgWidth, imgHeight)

var uri = imgSrc
if (uri.startsWith("//")) {
uri = WikiSite.DEFAULT_SCHEME + ":" + uri
} else if (uri.startsWith("./")) {
uri = Service.COMMONS_URL + uri.replace("./", "")
val uri = if (imgSrc.startsWith("//")) {
WikiSite.DEFAULT_SCHEME + ":" + imgSrc
} else if (imgSrc.startsWith("./")) {
Service.COMMONS_URL + imgSrc.replace("./", "")
} else {
UriUtil.resolveProtocolRelativeUrl(WikiSite.forLanguageCode(WikipediaApp.instance.appOrSystemLanguageCode), imgSrc)
}

Glide.with(view)
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/org/wikipedia/talk/TalkThreadItemView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class TalkThreadItemView constructor(context: Context, attrs: AttributeSet? = nu
val timestamp = DateUtil.getTimeAndDateString(context, it)
StringUtil.setHighlightedAndBoldenedText(binding.timeStampText, timestamp, searchQuery)
}
val body = CustomHtmlParser.fromHtml(StringUtil.removeStyleTags(item.html), binding.bodyText, false).trim()
val body = CustomHtmlParser.fromHtml(StringUtil.removeStyleTags(item.html), binding.bodyText).trim()
StringUtil.setHighlightedAndBoldenedText(binding.bodyText, body, searchQuery)
binding.bodyText.movementMethod = movementMethod

Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/org/wikipedia/util/DimenUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ object DimenUtil {
private val resources: Resources
get() = WikipediaApp.instance.resources

fun htmlUnitToPxInt(str: String): Int {
fun htmlUnitToPxInt(str: String): Int? {
try {
val unitRegex = "[A-Za-z]{2}".toRegex()
val unit = unitRegex.find(str)?.value.orEmpty()
Expand All @@ -92,7 +92,7 @@ object DimenUtil {
} catch (e: Exception) {
L.e(e)
}
return 0
return null
}

fun getNavigationBarHeight(context: Context): Float {
Expand Down

0 comments on commit f3da1bc

Please sign in to comment.