WeasyPrint icon indicating copy to clipboard operation
WeasyPrint copied to clipboard

Apply style defined in HTML on inline SVG images

Open kesara opened this issue 3 years ago • 2 comments

Example:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Attribute Selector Test</title>
    <style>
      svg [font-family~="serif"] {
        font-family: monospace;
        text-transform: uppercase;
      }
    </style>
  </head>
  <body>
    <svg width="300" height="200" xmlns="http://www.w3.org/2000/svg">
      <rect width="100%" height="100%" fill="green" />
      <circle cx="150" cy="100" r="80" fill="black" />
      <text x="150" y="125" font-family="serif" font-size="60" text-anchor="middle" fill="white">main</text>
    </svg>
  </body>
</html>

Firefox screenshot: Screenshot 2022-11-02 at 11 28 13 AM

Weasyprint 57.0 screenshot: Screenshot 2022-11-02 at 11 29 06 AM

kesara avatar Nov 01 '22 22:11 kesara

Hi!

Thanks for this bug report.

The problem is not the attribute selector: you can replace [font-family~="serif"] by text, it won’t work.

Here’s what’s wrong: style defined in the HTML file is not applied to the inline SVG file. That’s the main problem, and this limitation is really hard to fix because everything in WeasyPrint’s CSS validation is based on CSS for HTML, not CSS for SVG. Putting the style tag into the svg tag is the easier workaround for now, and the attribute selector works well here too 😁️.

I’ll change the title to keep track of this problem.

Moreover, many features are not correctly handled in SVG yet. That’s for example the case of text-transform.

liZe avatar Nov 01 '22 23:11 liZe

Thanks for the explanation @liZe.

kesara avatar Nov 01 '22 23:11 kesara