Re: Underline String in Graphics

Robin Becker <[email protected]>
Newsgroups gmane.comp.python.reportlab.user
Message-ID <[email protected]>
On 25/03/2015 20:43, Nathalie Steinmetz wrote:
> Hi,
>
> I realize that I can underline text within Paragraphs using <u></u>. For text within Graphics, in String objects, the recommendation seems to have been to draw a Line underneath the String.
>
> Is this still the way to go, or is there an easier way to have underlined text in graphics?
>

Hi Nathalie,

nothing prevents you from using Paragraph / XPreformatted to do your own drawing 
of complex strings. It's a bit more difficult, but see the end of this program 
for an example

###############################################################################
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfbase.pdfmetrics import registerFont
from reportlab.pdfgen.canvas import Canvas
from reportlab.lib.colors import green, pink, lightblue, red
from reportlab.platypus import XPreformatted
from reportlab.lib.styles import getSampleStyleSheet
registerFont(TTFont("Vera", "Vera.ttf"))
registerFont(TTFont("VeraBI", "VeraBI.ttf"))

c = Canvas('helloworld.pdf', pagesize=(300,500))
c.setFont('Vera', 10)
c.drawString(100, 200, u'Hello World')

c.showPage()
c.setPageSize((400,600))
c.setFont('Vera', 20)
c.setFillColor(green)
c.drawString(100, 300, u'Hello World')

c.showPage()
c.setPageSize((400,600))

sss = getSampleStyleSheet()
normal = sss['Normal']
xpre = XPreformatted('<font color="red">Red</font> <u>underliney</u>',normal)

w,h = xpre.wrap(0,0)

c.saveState()
#draw lines to show what's going on
c.setDash(2,2)
c.setStrokeColor(green)
c.setLineWidth(0.01)
c.line(72,100,72+w,100)
c.setStrokeColor(red)
c.line(72,100+h,72+w,100+h)
c.restoreState()
xpre.drawOn(c,72,100)

c.save()
###############################################################################

the main code is
xpre = XPreformatted('<font color="red">Red</font> <u>underliney</u>',normal)
w,h = xpre.wrap(0x7fffffff,0x7fffffff)
xpre.drawOn(c,72,100)

ie set up the styled text object, wrap and obtain the size. Note that the 
wrap(0,0) trick will likely not work with Paragraphs as they need a width to do 
their actual wrapping. The XPreformatted returns the max of the available width 
and the required width.


> Thanks!
> Nathalie
>
>
> --
> Nathalie Steinmetz
> Senior Software Engineer
>
> Jolt Labs


-- 
Robin Becker
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.