[reportlab-users] SVG font-size values lack units (invalid CSS)

Peter peter at maubp.freeserve.co.uk
Mon Feb 16 11:11:13 EST 2009


I noticed that font sizes were not working consistently across
viewers, and eventually identified the problem. Firefox 3.0 and Opera
9.20 will ignore a font-size in a style attribute if it has no units
(this is considered invalid CSS), showing text in its default size.
On the other hand, Safari 3.2.1, Adobe Illustrator CS3 and Mac OS X
10.5 preview will tolerate the missing units, and show the images as I
expected (arguably buggy behaviour). See
https://bugzilla.mozilla.org/show_bug.cgi?id=311569 for a discussion
of this.

Currently renderSVG outputs this kind of invalid CSS in its SVG files,

<text style="font-size: 10; font-family: Times-Roman;
fill: rgb(0%,39%,0%);" transform="translate(0,56) scale(1,-1)"
x="28.3464566929" y="28.3464566929">
Hello World!
</text>

This should be:

<text style="font-size: 10pt; font-family:
Times-Roman; fill: rgb(0%,39%,0%);" transform="translate(0,56)
scale(1,-1)" x="28.3464566929" y="28.3464566929">
Hello World!
</text>

Or, instead of using the style attribute we could use the font-size
attribute where the units are optional, e.g.

<text font-size="10" style="font-family: Times-Roman;
fill: rgb(0%,39%,0%);" transform="translate(0,56) scale(1,-1)"
x="28.3464566929" y="28.3464566929">
Hello World!
</text>

My suggested fix is to update renderSVG.py to replace this:

def setFont(self, font, fontSize):
if self._font != font or self._fontSize != fontSize:
self._font, self._fontSize = (font, fontSize)
self.style['font-family'] = font
self.style['font-size'] = fontSize

with this:

def setFont(self, font, fontSize):
if self._font != font or self._fontSize != fontSize:
self._font, self._fontSize = (font, fontSize)
self.style['font-family'] = font
#The style attribute contains CSS, so the units are required:
self.style['font-size'] = "%ipt" % fontSize

The images don't look identical to those from the PNG output from
renderPM (the fonts sizes differ a little), but this does look pretty
close and it now works on all the browsers I have tested.

Peter


More information about the reportlab-users mailing list