On Mon, 2005-02-14 at 21:23 -0500, Thomas Mills Hinkle wrote:

>This is something I haven't been doing until now -- I'll be more than
>happy to give up my lousy layout routine and pass it off to pango.
>However, I'm confused how the pango stuff works. How do you know where
>context.pango_layout(...) leaves me on the page? I'm used to writing
>each line with my own routine and keeping track of e.g. margins,
>wrapping-around pictures, and page-breaks/page numbers/page headers
>myself. Do you understand how all this works with
>context.pango_layout...?

OK, here's a few more details I've been able to uncover this week.

# BEGIN ================
import gnomeprint

config = gnomeprint.config_default()
job = gnomeprint.Job(config)
printContext = job.get_context()

printContext.beginpage("1")
#I think this is in pixels; anyone know for certain?
printContext.moveto(50, 200)

#this line creates a pango.Layout object
#simpler than previous example; best for bog-standard printing
para = printContext.pango_create_layout()

# device units / pango.SCALE = pixels
# pixels * pango.SCALE = device units

para.set_markup("""line 0
line 1 will wrap if comment removed from para.set_width()
line 2
line 3
line 4
line 5
""")

#can constrain the width in device units
#para.set_width(46800)

#see pango.Layout reference for other useful properties,
#including alignment, tab stops, and wrapping mode

#first tuple is ink extents
#second tuple is logical (layout) extents
print "extent: device units", para.get_extents()[1]
print "extent: pixels", para.get_pixel_extents()[1]

#width and height
#for normal text layout, use height to advance cursor
#moveTo(x, y - para.get_pixel_size()[1])
#will allow you to start next position
print "size: device units", para.get_size()
print "size: pixels", para.get_pixel_size()

printContext.pango_layout(para)
printContext.showpage()

job.close()
#gnomeprint.ui.JobPreview(job, "Printing sample").show()
# END ================
In theory, you can get individual lines using pango.Layout.get_lines()
or pango.Layout.get_line(index); however these appear to be missing from
pyGTK along with the corresponding PangoLayoutLine structure.
In the meantime, you can use the following snippet to get the logical
extents and starting character index of each line:

lines = para.get_iter()
for i in range(para.get_line_count()):
        print "line", i, lines.get_line_extents()
        print "markup index", lines.get_index()
        lines.next_line()

It's too late for me to put together a real layout algorithm now, but:
gpc.moveTo(pageLeft, pageTop)
paraTop = pageTop
paragraphs = []
for i in range(30):
        newPara = printContext.pango_create_layout()
        newPara.set_markup("some long pango markup goes here")
for para in paragraphs:
        para.set_width(printableWidth)
        size = para.get_pixel_size()
        if size.y > printableHeight:
                #iterate through lines, those that fit
                #use missing API call printContext.pango_layout_line()
                #create new para with remaining lines
                #print new para at top of new page
        else:
                printContext.pango_layout(para)
                printableHeight -= size.y
                paraTop -= size.y #default y axis has 0 at bottom
                printContext.moveTo(pageLeft, paraTop)

A real layout algorithm would work for bi-directional text (think
Arabic) and work within regions instead of the whole page.
But hopefully this gives you an idea. Whoever maintains the pango
bindings should look at including the PangoLayoutLine stuff since it
makes things easier.
If you're really into advanced layout, you can iterate through text runs
(collections of glyphs with contiguous formatting) instead of lines.
Lines is best though since some languages have very complex rules - Thai
is a good example where only certain characters are allowed to begin or
end a line; Arabic has complicated kerning rules because it is
essentially a cursive script.

Attachment: signature.asc
Description: This is a digitally signed message part

_______________________________________________
pygtk mailing list   [email protected]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/

Reply via email to