[reportlab-users] renderSVG

Matthias Bernt bernt at informatik.uni-leipzig.de
Thu Aug 16 12:07:03 EDT 2007


hi,

I'm trying to write a Widget that gives me circular text. My code works
like I would expect it if I use renderPDF or renderPS but renderSVG
gives me totally different results. It seems as the different renderers
handle the transformations in different ways (in my case rotations).

Is there a way to apply transformations in such a way that the
different renderers behave in the same way?

Thanks

Matthias

The code looks like this:

class circular_text(Widget):
""" a class for circular text """
def __init__(self, text, radius, angle, fontname, fontsize, textcolor, textanchor):
""" init
\param radius the radius on which the text should be drawn = the middle of the text
\param angle the angle where the middle of the text should be
\param fontname the font to use
\param fontsize the size of the font
\param textcolor the color of the text as string (e.g. "black")
\param textanchor start/middle/end
"""
self.text = text
self.radius = radius - fontsize/2.0
self.angle = angle
self.fontname = fontname
self.fontsize = fontsize
self.textcolor = textcolor
self.textanchor=textanchor
def draw(self):
"""
draw circular text on a canvas
"""

rat = 360/(2*pi*self.radius)
print "u = %f ratio %f" %(2*self.radius*pi, rat)
g = Group()
for i in range(len(self.text)):
w = rat*stringWidth( self.text[0:i], self.fontname, self.fontsize )
g.add( String(x=0, y=0, text=self.text[i], textAnchor="start",transform=mmult(rotate(-1*w), translate(0, self.radius)), \
fontName=self.fontname, fontSize=self.fontsize, fillColor=self.textcolor ) )

# up to now the text starts at 0deg
if self.textanchor == "start":
g.rotate(-1*self.angle)
elif self.textanchor == "middle":
w = rat*stringWidth( self.text, self.fontname, self.fontsize )/2
g.rotate(-1*(self.angle-w) )
elif self.textanchor == "end":
w = rat*stringWidth( self.text, self.fontname, self.fontsize )
g.rotate(-1*(self.angle-w) )

return g


d = Drawing(400, 400)
d.add( mtviz.circular_text("000", 100, 0, "Helvetica", 10, colors.black, "start") )
d.add( mtviz.circular_text("000", 120, 0, "Helvetica", 10, colors.black, "middle") )
d.add( mtviz.circular_text("012", 140, 0, "Helvetica", 10, colors.black, "end") )

from reportlab.graphics import renderPDF
from reportlab.graphics import renderSVG
renderSVG.drawToFile(d, 'example1.svg')
renderPDF.drawToFile(d, 'example1.pdf', 'circular text')


I have played around with the draw method code with the result
that in the svg output the text is also rotated, but unfortunately in a different way.. :-(
the difference is in the for-loop here i created a group per character and
applied the transformations on each group seperately.

...
g = Group()
for i in range(len(self.text)):
w = rat*stringWidth( self.text[0:i], self.fontname, self.fontsize )
cg = Group()
cg.add( String(x=0, y=0, text=self.text[i], textAnchor="start", \
fontName=self.fontname, fontSize=self.fontsize, fillColor=self.textcolor ) )
cg.rotate(-w)
cg.translate(0, self.radius)
g.add(cg)
...





More information about the reportlab-users mailing list