Skip to content

Commit

Permalink
Gallery: Fix svg sub-sampling in Media View
Browse files Browse the repository at this point in the history
Signed-off-by: IacobIonut01 <[email protected]>
  • Loading branch information
IacobIonut01 committed Mar 30, 2024
1 parent 4a9eaed commit 668d725
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ package me.saket.telephoto.zoomable.coil3

import android.annotation.SuppressLint
import android.util.TypedValue
import coil.decode.DecodeUtils
import coil3.decode.DecodeUtils
import coil3.svg.isSvg
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import me.saket.telephoto.subsamplingimage.AssetImageSource
Expand All @@ -13,51 +14,47 @@ import me.saket.telephoto.subsamplingimage.RawImageSource
import me.saket.telephoto.subsamplingimage.ResourceImageSource
import me.saket.telephoto.subsamplingimage.SubSamplingImageSource
import me.saket.telephoto.subsamplingimage.UriImageSource
import me.saket.telephoto.zoomable.coil.isSvg
import okio.FileSystem
import okio.Source
import okio.buffer
import okio.source
import okio.use

context(Resolver)
internal suspend fun SubSamplingImageSource.canBeSubSampled(): Boolean {
val preventSubSampling = when (this) {
is ResourceImageSource -> isVectorDrawable()
is AssetImageSource -> isSvgDecoderPresent() && isSvg()
is UriImageSource -> isSvgDecoderPresent() && isSvg()
is FileImageSource -> isSvgDecoderPresent() && isSvg(FileSystem.SYSTEM.source(path))
is RawImageSource -> isSvgDecoderPresent() && isSvg(source.invoke())
}
return !preventSubSampling
val preventSubSampling = when (this) {
is ResourceImageSource -> isVectorDrawable()
is AssetImageSource -> isSvgDecoderPresent() && isSvg()
is UriImageSource -> isSvgDecoderPresent() && isSvg()
is FileImageSource -> isSvgDecoderPresent() && isSvg(FileSystem.SYSTEM.source(path))
is RawImageSource -> isSvgDecoderPresent() && isSvg(source.invoke())
}
return !preventSubSampling
}

context(Resolver)
private fun isSvgDecoderPresent(): Boolean {
// Searching for coil's SvgDecoder by name isn't the best idea,
// but it'll prevent opening of bitmap sources and inspecting
// them for SVGs for projects that don't need SVGs.
return imageLoader.components.decoderFactories.any {
it::class.qualifiedName?.contains("svg", ignoreCase = true) == true
}
// Only available in this app
return true
}

context(Resolver)
private fun ResourceImageSource.isVectorDrawable(): Boolean =
TypedValue().apply {
request.context.resources.getValue(id, this, /* resolveRefs = */ true)
}.string.endsWith(".xml")
TypedValue().apply {
request.context.resources.getValue(id, this, /* resolveRefs = */ true)
}.string.endsWith(".xml")

context(Resolver)
private suspend fun AssetImageSource.isSvg(): Boolean =
isSvg(peek(request.context).source())
isSvg(peek(request.context).source())

context(Resolver)
@SuppressLint("Recycle")
private suspend fun UriImageSource.isSvg(): Boolean =
isSvg(peek(request.context).source())
isSvg(peek(request.context).source())

private suspend fun isSvg(source: Source?): Boolean {
return withContext(Dispatchers.IO) {
source?.buffer()?.use(DecodeUtils::isSvg) == true
}
return withContext(Dispatchers.IO) {
source?.buffer()?.use { DecodeUtils.isSvg(source.buffer()) } == true
}
}
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ accompanist = "0.34.0"
datastore = "1.1.0-beta02"
uiautomator = "2.3.0"
zoomable = "1.6.1"
zoomableImageCoil = "0.8.0"
zoomableImageCoil = "0.9.0"

[libraries]
# AndroidX
Expand Down

0 comments on commit 668d725

Please sign in to comment.