[reportlab-users] Underline String in Graphics

Nathalie Steinmetz nathalie at joltlabs.com
Thu Mar 26 17:17:09 EDT 2015


I ended up implementing it with adding a Line underneath the String:

#####################################
from reportlab.graphics.shapes import Drawing, Line, String
    drawing = Drawing()
    string = String(x, y, text,
                           textAnchor='start',
                           fontName=‘Helvetica',
                           fontSize=9))
    drawing.add(string)
    x_start = string.getEast()
    drawing.add(Line(x_start, y - 2, x, y - 2))
#####################################

cheers,
Nathalie


> On Mar 26, 2015, at 11:01 AM, Nathalie Steinmetz <nathalie at joltlabs.com> wrote:
> 
> Hi Robin,
> 
> thanks for your response! And for the example, I wasn’t aware about XPreformatted… 
> 
> Unfortunately I am not able to use the XPreformatted object - I am not drawing directly on the canvas, but am creating a Drawing object to which I add shapes (Rect, Path, String) from the graphics module (from reportlab.graphics.shapes import Drawing, Rect, Path, String). Those String shape objects don’t seem to allow for much formatting, and all I can add as text is plain text. I can do bold and italic by using a bold or italic font, but that approach doesn’t work for underline.
> 
> Sorry for not having been clear enough about this before! Do you know of any better way to underline such strings, instead of drawing a line underneath?
> 
> Thanks,
> Nathalie
> 
> 
> 
>> On Mar 26, 2015, at 4:04 AM, Robin Becker <robin at reportlab.com> wrote:
>> 
>> 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
>> _______________________________________________
>> reportlab-users mailing list
>> reportlab-users at lists2.reportlab.com
>> https://pairlist2.pair.net/mailman/listinfo/reportlab-users
> 



More information about the reportlab-users mailing list