[reportlab-users] Re: A (hopefully small) sample of font/fontsize problem drawing a paragraph directly on a canvas

Robin Becker reportlab-users@reportlab.com
Wed, 26 Jun 2002 11:03:55 +0100


In message <3D18A635.FCB58FDF@ttfn.gsfc.nasa.gov>,
pbear@ttfn.gsfc.nasa.gov writes

Hi Ginny,
I see your error or use of unexplained features :),
The defaults you set up for the style were just defaults.
So instead of
>    properties = PropertySet("foo")
>    style = ParagraphStyle(properties)
>    style.defaults["fontName"] = font
>    style.defaults["fontSize"] = fontsize
>    style.defaults["bulletFontName"] = font
>    style.defaults["bulletFontSize"] = fontsize
>    style.defaults["leading"] = 12
>    style.defaults["spaceBefore"] = 0

I used
        style = ParagraphStyle( name='pbear-bold',
                                fontName=font,
                                fontSize=fontsize,
                                bulletFontName=font,
                                bulletFontSize=fontsize,
                                leading=12,
                                spaceBefore=0,
                                )

You can also use sgml type markup inside the paragraph text to control
appearance. Note that you originally used CourierBold as the font so
it's then impossible to turn bolding on and off using that technique.

Note that you can set almost all of the paragraph style attributes in an
enclosing <para> tag </tag>

eg <para font='times' fontsize=....


The paraparser._paraAttrMap determines the attribute names that are
accepted ie font, face & fontname all map to the official fontName style
setting
 
_paraAttrMap = {'font': ('fontName', None),
                'face': ('fontName', None),
                'fontsize': ('fontSize', _num),
                'size': ('fontSize', _num),
                'leading': ('leading', _num),
                'lindent': ('leftIndent', _num),
                'rindent': ('rightIndent', _num),
                'findent': ('firstLineIndent', _num),
                'align': ('alignment', _align),
                'spaceb': ('spaceBefore', _num),
                'spacea': ('spaceAfter', _num),
                'bfont': ('bulletFontName', None),
                'bfontsize': ('bulletFontSize',_num),
                'bindent': ('bulletIndent',_num),
                'bcolor': ('bulletColor',toColor),
                'color':('textColor',toColor),
                'backcolor':('backColor',toColor),
                'bgcolor':('backColor',toColor),
                'bg':('backColor',toColor),
                'fg': ('textColor',toColor)}

[ Section: 1/1 File: pbear.py UUencoded by: Turnpike Integrated Version 5.01 U ]

begin 644 pbear.py
M9G)O;2!R97!O<G1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2`K/2!H("L@9F]N='-I>F4J,2XU#0H)"0D):'-P86-E("T]("AH
M*V9O;G1S:7IE*C$N-2D-"@D)"65L<V4Z#0H)"0D)8G)E86L[#0H)8V%N=F%S
M+G-A=F4H*0T*#0II9B!?7VYA;65?7ST])U]?;6%I;E]?)SH-"@EF;V\H*0T*
"#0H`
`
end

sum -r/size 30212/2387 section (from "begin" to "end")
sum -r/size 45113/1712 entire input file

>Hello...
>
>
>In response to the reportlab digest, below is a small file that
>generates a pdf with one paragraph.
>
>I am not using a DocTemplate, nor a Story, I am just using Paragraph's
>drawOn method.  In addition, I've turned off bottomup.
>
>Does ReportLab require more than a Canvas in this case?
>
>I did some poking around in Paragraph, ParaFrag, etc., and I have put a
>few prints in the code, which you will see if you run it.  The fonts and
>fontnames in frags and blPara are both the default Times-Roman.  I have
>tried modifying them directly (ick!), but the wrap is still assuming
>Times-Roman, and the paragraph is wider than the allotted width.
>
>
>ttfn
>
>Ginny Keech
>NASA/GSFC
>301.286.1988
>
>
>-------------------------------------------------------------------------
>
>from reportlab.pdfgen.canvas import Canvas
>from reportlab.lib.pagesizes import letter
>from reportlab.platypus import Paragraph
>from reportlab.lib.styles import ParagraphStyle, PropertySet
>from reportlab.rl_config import defaultPageSize
>
>
>CourierBold = "Courier-Bold"
>Courier = "Courier"
>FontSize = 14
>
>def foo():
>
>    # creates foo.pdf in current directory
>
>    x = 20
>    y = 100
>    width = 555
>    height = 128
>    font = CourierBold
>    fontsize = FontSize
>
>    canvas = Canvas("foo.pdf", letter, 0)
>
>    paragraphs = ["After carefully strapping rocket on coyote with No. 5
>rope, rocket ignites, but fuel consumption rate is excessive.  " + 
>                  "Net effect: coyote suffers unacceptable damage from
>unexpected collision with cliff."]
>#    print x, y, width, height, font, fontsize
>    
>    canvas.setFont(font, fontsize)  # just in case this needed to be
>done as well.
>
>    properties = PropertySet("foo")
>    style = ParagraphStyle(properties)
>    style.defaults["fontName"] = font
>    style.defaults["fontSize"] = fontsize
>    style.defaults["bulletFontName"] = font
>    style.defaults["bulletFontSize"] = fontsize
>    style.defaults["leading"] = 12
>    style.defaults["spaceBefore"] = 0
>#    print style.defaults
>
>    wspace = width
>    hspace = height
>    for ndx in range(len(paragraphs)):
>        para = Paragraph(paragraphs[ndx], style)
>        w, h = para.wrap(wspace, hspace)
>#        print ndx
>#        print para
>#        print para.style.defaults
>        print len(para.frags)
>        print para.frags[0].fontName
>        print para.blPara.fontName
>        print para.style.defaults["fontName"]
>        if w <= wspace and h <= hspace:
>            para.drawOn(canvas, x, y-h) # compensate for drawing from
>lower left
>            y += h + fontsize * 1.5
>            hspace -= (h + fontsize * 1.5)
>        else:
>            break;
>
>    canvas.save()
>
>if __name__=='__main__':
>    foo()
>

-- 
Robin Becker