Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistencies in SVG Image Sizing and Blurriness When Loaded from URLs Using Coil in Jetpack Compose #2048

Open
NatashaIJ opened this issue Jan 16, 2024 · 4 comments
Labels
help wanted Issues that are up for grabs + are good candidates for community PRs

Comments

@NatashaIJ
Copy link

Description:
In a Jetpack Compose project utilizing Coil (version 2.4.0), SVG images loaded from URLs fail to retain their intrinsic dimensions, scaling up to the maximum size set with .sizeIn(maxWidth = 16.dp, maxHeight = 16.dp). This issue does not occur with SVGs loaded from drawable resources, which correctly maintain their intrinsic sizes. To ensure accurate sizing, Size.ORIGINAL is utilized in the image request, which leads to another problem: the images become blurry due to scaling from pixel-based sizes to dp in the UI.

Steps to Reproduce:

  1. Initiate a Jetpack Compose project with Coil for image loading.
  2. Load an SVG from a URL, which has smaller intrinsic dimensions than the maximum size, using .sizeIn(maxWidth = 16.dp, maxHeight = 16.dp) in the Image modifier
  3. Observe the SVG incorrectly scaling to the maximum set size.
  4. Apply Size.ORIGINAL to retrieve correct intrinsic dimensions for sizing in the Image modifier.
  5. Notice the correct sizing but with resultant blurriness.

Expected Behavior:

  1. SVG images from URLs should reflect their intrinsic dimensions without scaling to a set maximum, similar to drawable resource behavior.
  2. When employing Size.ORIGINAL, the images should remain clear, without the blurriness introduced by upscaling for the UI's dp dimensions.

Additional Context:
A custom SvgDecoder was introduced to tackle the blurriness, altering the getDstSize function to translate SVG dimensions into dp for UI consistency. While this resolved the blurriness, modifying library components is risky, potentially leading to future compatibility issues. Ideally, default support for this scenario in the SVGDecoder would avoid the need for custom implementations.

Version:
Coil version: 2.4.0
The problem has been noted on various API levels and Android devices.

Related Bug and Discussions:

  1. Issue #566 with a similar problem and discussion
  2. A related discussion on handling SVGs with Coil

Note:
In my view, such intrinsic size handling should be natively supported by Coil's SVGDecoder to avoid the need for custom implementations that may introduce future maintenance challenges.

@colinrtwhite
Copy link
Member

colinrtwhite commented Jan 18, 2024

The svg decoder should already take into account the intrinsic size. Can you try setting useViewBoundsAsIntrinsicSize = false and see if that fixes your issue? Does it work properly if you use AsyncImage instead of rememberAsyncImagePainter?

Can you include sample code that reproduces the issue? It sounds like you might be using a specific combination of size modifiers.

@colinrtwhite
Copy link
Member

If the ask is to multiply an SVG image's intrinsic width/height px to match its density similar to #2048 then we could potentially add a new constructor param. Though, my understanding of the SVG specification was that values represented in px units shouldn't be scaled to a device's density. This change would add different behaviour from the specification.

@NatashaIJ
Copy link
Author

Sample code:
CoilImages.zip

  1. It works same with rememberAsyncImagePainter and with AsyncImage
  2. useViewBoundsAsIntrinsicSize = false - doesn't fix the issue

@colinrtwhite colinrtwhite added the help wanted Issues that are up for grabs + are good candidates for community PRs label Jan 20, 2024
seanpont added a commit to seanpont/coil that referenced this issue Jan 30, 2024
Scales SVGs correctly

Issue: coil-kt#2048
@seanpont
Copy link

#2090

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Issues that are up for grabs + are good candidates for community PRs
Projects
None yet
Development

No branches or pull requests

3 participants