range and wx co - ords

Bengt Richter bokr at oz.net
Wed Nov 10 11:17:27 CET 2004


On 9 Nov 2004 17:07:12 -0800, mj.clift at virgin.net (Malcolm Clift) wrote:

>Hi All,
>
>I have now got something approaching what I need. Could someone please
>show me how to define the 'line', so rather than just the first 50
>items being printed over and over it goes through the text
>incrementally?
>
>
>        line = text1[:50]
>        for i in range(1000):
>            dc.DrawText(line, 100, 100 + (i//2)*100) 
>
I don't think you want to do 1000 DrawText calls to display 50 characters ;-)

I can't tell what you really want to do, but if you just want to spread characters
evenly to make an array of characters with 50 per row of some width and a box of
some height, I would just walk through the characters and bump coordinates as I went.

If you are tyring to spread _words_ into the space, you might want to keep the characters
in words more together than what overall average spacing will give. I.e., insert extra
space between words, but none or some mild effect between characters. This also depends
on what font you are using, which gets into font metrics. Also how persnickety you are
about precise inclusion within the box area of characters on the right. An '!' is going
to be narrower than an 'M'. Also, do you want to ignore word boundaries and likely break
words between lines?

If you just want to walk through 20 lines of 50 characters taken successively from text1,
you can write something like (untested!!)

    cpl = 50 # chars per line
    totlines = 20
    top = 100; left = 100 # top left pixel position
    width = 500; height = 400 # box pixel(?) dimensions
    cleantext = ' '.join(text1.split()) # change all whitespace to single spaces??
    for linestart in xrange(0, cpl*totlines, cpl):
        line = cleantext[linestart:linestart+cpl]
        for ix, c in enumerate(line):
            # compute position
            iy = linestart//cpl # might want to add 1 if top is not baseline pos
            x = int(left + width*ix/float(cpl))
            y = int(top + height*iy/float(totlines))
            dc.DrawText(c, x, y)

But this is not normal word flowing to fit _words_ in a box justified to both sides,
if that's what you really want. If you want that, you should probably output words
whose characters are not separated as much, and put extra space between words, and
not break words at line ends. Also, if your text has real newlines ('\n') or tabs
etc. in it, you may want to change them to spaces (but not necessarily a single space
for each separation as I did in cleantext above).

HTH and that the bug is not too bad (untested, there's bound to be one ;-)

Regards,
Bengt Richter



More information about the Python-list mailing list