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

'font' CSS shorthand property ignored in SVG 'text' elements #2255

Open
febdoctor opened this issue Sep 19, 2024 · 2 comments
Open

'font' CSS shorthand property ignored in SVG 'text' elements #2255

febdoctor opened this issue Sep 19, 2024 · 2 comments
Labels
bug Existing features not working as expected good first issue Issues that can be quite easily solved by Python developers with a good CSS background

Comments

@febdoctor
Copy link

febdoctor commented Sep 19, 2024

The font properties set via a 'font' CSS shorthand property seem to be totally ignored when rendering text elements inside SVG images.

Here is a minimal SVG to reproduce the issue:

<svg xmlns="http://www.w3.org/2000/svg"  viewBox="0 0 400 400">

<style>
   .octl text {
         font: 7mm cursive;
          fill: purple;
          stroke: white;
          stroke-width: 0.1mm;
          stroke-linecap: butt;
          stroke-linejoin: miter;
   }

   	line {
	    stroke: black;
	}
</style>
<g class="octl">
<text class="octl" x="7mm" y="7mm">Text 14mm in cursive.</text>
<line x1="7mm" y1="7mm" x2="7mm" y2="0mm" />
<line x1="7mm" y1="10mm" x2="14mm" y2="10mm" />
</g>
</svg>

It renders well in the PDF from Firefox:

screenshot_firefox

But Weasyprint uses default font family and size:

screenshot_weasyprint

font1-firefox.svg.pdf
font1-weasyprint.svg.pdf

@febdoctor
Copy link
Author

Replacing the "font" CSS property by specific properties like "font-size" and "font-family" works. This can be used as a workaround.

@liZe liZe added the bug Existing features not working as expected label Sep 19, 2024
@liZe
Copy link
Member

liZe commented Sep 24, 2024

Hi, and thanks for the report!

This missing feature is caused by the dirty code used for SVG attributes management. We should use the real validated and computed values of SVG attributes and take care of font here:

# TODO: use real computed values
style = INITIAL_VALUES.copy()
style['font_family'] = [
font.strip('"\'') for font in
node.get('font-family', 'sans-serif').split(',')]
style['font_style'] = node.get('font-style', 'normal')
style['font_weight'] = node.get('font-weight', 400)
style['font_size'] = font_size
if style['font_weight'] == 'normal':
style['font_weight'] = 400
elif style['font_weight'] == 'bold':
style['font_weight'] = 700
else:
try:
style['font_weight'] = int(style['font_weight'])
except ValueError:
style['font_weight'] = 400

@liZe liZe added the good first issue Issues that can be quite easily solved by Python developers with a good CSS background label Sep 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Existing features not working as expected good first issue Issues that can be quite easily solved by Python developers with a good CSS background
Projects
None yet
Development

No branches or pull requests

2 participants