[reportlab-users] Achieving uniform spacing in a text object
Jeff Kowalczyk
reportlab-users@reportlab.com
Fri, 23 Aug 2002 12:20:09 -0400
Is there a way to achieve uniform spacing of characters, without relying on
the monospaced-ness of fonts like courier.
The attached example is helvetica (proportional), but if you change it to
courier, it is still very sensitive to spacing drift. I was fiddling with
7.78 and 7.79 in courier, and still not getting the later chars to align.
I guess what I'm looking for is a sane way to grid the characters out,
regardless of the font. It would be a nice API addition to textobject, if
possible: something like setCharSpaceGrid().
# CharSpacingTest.py - Test monospacing chars with proportional fonts
from reportlab.pdfgen.canvas import Canvas
Courier = 'Courier'
Helvetica = 'Helvetica'
class Form:
def __init__(self, **kw):
self.pdfpath = kw.get('pdfpath', 'CharSpacingTest.pdf')
self.verbose = kw.get('verbose', 0)
self.text_font = kw.get('typeface', Courier)
self.text_size = kw.get('fontsize', 10)
self.canvas = Canvas(self.pdfpath)
self.__draw_grid()
self.__draw_filled_boxes()
FilledBoxes = [ \
( 100, 700, 20, '01234567890!,ABCDEFG', ),
]
def __draw_filled_boxes(self):
c = self.canvas
boxwidth = 15
boxheight = 15
for x1, y1, chars, filler in self.FilledBoxes:
xlist = range(x1, x1+(chars*boxwidth)+1, boxwidth)
ylist = [y1, y1+boxheight]
c.setLineWidth(0.25)
c.setStrokeGray(0.25)
c.grid(xlist,ylist)
c.setStrokeGray(0)
t = c.beginText(x1,y1+3)
t.setFont(Helvetica,12)
t.setCharSpace(7.5)
t.textOut(filler)
c.drawText(t)
t = None
def __draw_grid(self):
c = self.canvas
xlist = range(0, 620, 20)
ylist = range(0, 900, 20)
c.setLineWidth(0.05)
c.setStrokeGray(0.25)
c.grid(xlist,ylist)
for x in xlist:
t = c.beginText(x,ylist[36])
t.setFont(Helvetica,6)
t.textOut(str(x))
c.drawText(t)
for y in ylist:
t = c.beginText(xlist[10],y)
t.setFont(Helvetica,6)
t.textOut(str(y))
c.drawText(t)
c.setStrokeGray(1.0)
def save(self):
self.canvas.save()
if __name__ == '__main__':
from Fields import test_pattern
form = Form()
#form.populate(test_pattern())
form.save()
begin 666 CharSpacingTest.pdf
M)5!$1BTQ+C,-"B63C(N>(%)E<&]R=$QA8B!'96YE<F%T960@4$1&(&1O8W5M
M96YT(&AT=' Z+R]W=W<N<F5P;W)T;&%B+F-O;0T*)2 G0F%S:6-&;VYT<R<@
M)SQR97!O<G1L86(N<&1F8F%S92YP9&9D;V,N4$1&1&EC=&EO;F%R>2!I;G-T
M86YC92!A)R -"C$@,"!O8FH-"B4@5&AE('-T86YD87)D(&9O;G1S(&1I8W1I
M;VYA<GD-"CP\("]&,2 R(# @4B ^/@T*96YD;V)J#0HE("=&,2<@)SQR97!O
M<G1L86(N<&1F8F%S92YP9&9D;V,N4$1&5'EP93%&;VYT(&EN<W1A;F-E(&%T
M)R -"C(@,"!O8FH-"B4@1F]N="!(96QV971I8V$-"CP\("]"87-E1F]N=" O
M2&5L=F5T:6-A#0H@+T5N8V]D:6YG("]7:6Y!;G-I16YC;V1I;F<-"B O3F%M
M92 O1C$-"B O4W5B='EP92 O5'EP93$-"B O5'EP92 O1F]N=" ^/@T*96YD
M;V)J#0HE("=086=E,2<@)SQR97!O<G1L86(N<&1F8F%S92YP9&9D;V,N4$1&
M4&%G92!I;G-T86YC92!A=" P># P)R -"C,@,"!O8FH-"B4@4&%G92!D:6-T
M:6]N87)Y#0H\/" O0V]N=&5N=',@-R P(%(-"B O365D:6%";W@@6R P#0H@
M(# -"B @-3DU+C(W#0H@(#@T,2XX.2!=#0H@+U!A<F5N=" V(# @4@T*("]2
M97-O=7)C97,@/#P@+T9O;G0@,2 P(%(-"B @+U!R;V-3970@6R O4$1�H@
M(" O5&5X= T*(" @+TEM86=E0@T*(" @+TEM86=E0PT*(" @+TEM86=E22!=
M(#X^#0H@+U1R86YS(#P\(" ^/@T*("]4>7!E("]086=E(#X^#0IE;F1O8FH-
M"B4@)U(T)R G/')E<&]R=&QA8BYP9&9B87-E+G!D9F1O8RY01$9#871A;&]G
M(&EN<W1A;F-E(&%T(#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