[reportlab-users] italic fonts

Robin Becker robin at reportlab.com
Tue Nov 22 11:29:07 EST 2005


Alex Greif wrote:
> It is only small strings that I want to convert.
> 
> strange is the fact that the wxPython Demo is able to display comic
> italic. I thought that if it is possible in  the application, then
> there would be an easy way to do this in the PDF too. may be wxPython
> only skews the text matrix.
> 
> Is there a possibility to generate an italic ttf of, which dont
> support it by default, or is it a question of license.
> 
> thanks,
> ALex.
> 
.
>>
...... I think the problem as stated is unsolvable. Skewing the text rendering 
coordinates is possible, but is currently not done in our Textobject class. Weou 
would have to track the text and text line matrices to allow users to mess with 
them in a reasonable way and that overhead that wouldn't be required by 99.99% 
of users.

I suspect that even if we could get a skew matrix to work it wouldn't be doing 
what font experts call italicising, but I'm not an expert.

On the other hand for small amounts of text you can use a simple draw one 
character at a time technique with a global skew matrix set up. You'll have to 
handle the string to canvas stuff (including working out the coordinates) your self.

Try this code and see if it does what you want


###########################################
from reportlab.pdfgen.canvas import Canvas

c = Canvas('/tmp/skewed.pdf')

def skewText(c, x,y,text,fontName,fontSize,fillColor,skewAngle):
	from reportlab.graphics.shapes import skewX
	skewMatrix = skewX(skewAngle)
	c.saveState()
	c.setFillColor(fillColor)
	c.setFont(fontName,fontSize)
	for t in text:
		c.saveState()
		c.translate(x,y)
		c.transform(*skewMatrix)
		c.drawString(0,0,t)
		c.restoreState()
		x += c.stringWidth(t,fontName,fontSize)
	c.restoreState()

#first draw some normal text
fontName='Courier'
fontSize=14
leading=14*1.2
fillColor=(0,0,0)
x=72
y=3*72
c.setFillColor(fillColor)
c.setFont(fontName,fontSize)

c.drawString(x,y,'I should not be skewed')
skewText(c,x,y-leading,'I should be skewed 10',fontName,fontSize,fillColor,10)
skewText(c,x,y-2*leading,'I should be skewed 20',fontName,fontSize,fillColor,20)
skewText(c,x,y-3*leading,'I should be skewed 30',fontName,fontSize,fillColor,30)

c.showPage()
c.save()
###########################################
-- 
Robin Becker


More information about the reportlab-users mailing list