lovell / lovell/sharp

SVG conversion to PNG ignores <style> variables

Open
#3,502 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

blocked-upstream-dependency enhancement
Dominant language
JavaScript
Stars
32.7k
Forks
1.4k
Avg merge
1d 14h
Merged PRs (30d)
5

Description

## Possible bug

SVG conversion to PNG ignores variables.

This issue limits compatibility with several of the assets I am using, and with 100% that I generate.

Maybe... this could be because of libvips, which uses librsvg. Acccording to this post, they still do not support style variables:
https://gitlab.gnome.org/GNOME/librsvg/-/issues/459

Even if the issue is because of a dependent package, it is important to track it until it is supported and be transparent to developers that use style variables.

### Are you using the latest version of sharp?

- [x] I am using the latest version of `sharp` as reported by `npm view sharp dist-tags.latest`.

### What is the output of running `npx envinfo --binaries --system --npmPackages=sharp --npmGlobalPackages=sharp`?

```
System:
OS: macOS 11.7
CPU: (4) x64 Intel(R) Core(TM) i5-7500 CPU @ 3.40GHz
Memory: 6.26 GB / 32.00 GB
Shell: 5.8 - /bin/zsh
Binaries:
Node: 16.14.0 - /usr/local/bin/node
npm: 8.15.1 - /usr/local/bin/npm
npmPackages:
sharp: ^0.31.2 => 0.31.2

```
### What are the steps to reproduce?

- Convert the attached sample SVG to PNG

### What is the expected behaviour?

- Output should have a blue background, as defined in the --Bg variable, but the background is black.

### Please provide a minimal, standalone code sample, without other dependencies, that demonstrates this problem

This is a nextjs api endpoint:

```
const sharp = require('sharp');
const sharpOptions = {
compressionLevel: 0,
quality: 100,
}

export default async function handler(request, response) {
const svg = '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="600" height="600" viewBox="0 0 20 20"><defs><style>:root{--Bg:#0000FF;}.Bg{fill:var(--Bg);}.Rect{fill:#00ff00;}';

const svgBuffer = Buffer.from(svg);
const png = sharp(svgBuffer).png(sharpOptions);
const pngData = await png.toBuffer();

response.statusCode = 200;
response.setHeader('Content-Type', 'image/png');
response.setHeader('Cache-Control', 'public, max-age=0, must-revalidate');
response.end(pngData);
}
```

### Please provide sample image(s) that help explain this problem

![sample](https://user-images.githubusercontent.com/229161/208944942-33bde27c-9b63-4c48-8e28-53a0fc0ca1a6.svg)
![output_BAD](https://user-images.githubusercontent.com/229161/208944995-024fcc1c-2e19-40da-81e9-ac960ea05b8d.png)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the conversion with the standalone sharp example and attached SVG, then check the referenced libvips/librsvg limitation at GNOME issue 459. Done means establishing whether the dependency now supports CSS style variables and documenting the resulting sharp behavior or limitation.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, nodejs
Domain
backend, computer-graphics
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.