[pygtk] Segfault using pangocairo

John C Barstow jbowtie at amathaine.com
Mon Jul 30 20:06:23 WST 2007


Running Ubunu Gutsy; pycairo 1.4.0, pygtk 2.10.6

I'm using show_layout_line to display paragraphs, and it works just fine
if I do just one paragraph.  However, if I do two paragraphs, I get a
segfault. I've tried different backends and they all show the same
behaviour, so I suspect it's in the bindings somewhere.

The following script demonstrates the bug.

#-----BEGIN pygtk-bug.py-------
#! /usr/bin/env python

import gtk, cairo, pangocairo, pango
import os

# from reportlab
cm = 28.3464566929

class ProblemSheet:
    def __init__(self):
        self.top_margin = 1*cm
        self.left_margin = 1.5*cm
    
    def draw_page(self, operation, context, page_nr):
        c = context.get_cairo_context()
        self.drawDetails(c)
        
    def drawDetails(self, c):
        top = 0
        layout = c.create_layout()
        layout.set_font_description(pango.FontDescription("serif 12"))
        layout.set_text("Headline")
        top += self.renderPara(c, layout, self.left_margin+0.5*cm,
self.top_margin)
        ###
        layout2 = c.create_layout()
        c.move_to(self.left_margin+0.5*cm, self.top_margin+0.5*cm+top)
        layout2.set_font_description(pango.FontDescription("sans 10"))
        layout2.set_text("Line 1\nLine 2")
        self.renderPara(c, layout2, self.left_margin+0.5*cm,
self.top_margin+0.5*cm+top)
            
    def renderPara(self, c, layout, left, top):
        x = left
        y = top
        c.update_layout(layout)
        lines = layout.get_iter()
        prev_baseline = lines.get_baseline()/pango.SCALE
        
        while True:
            line = lines.get_line()
            ink, logical = lines.get_line_extents()
            y_begin, y_end = lines.get_line_yrange()
            baseline = lines.get_baseline()/pango.SCALE
            c.move_to(x + logical[0]/pango.SCALE, y + baseline)
            c.show_layout_line(line)
            prev_baseline = baseline
            if not lines.next_line():
                break
        return prev_baseline

    def PrintPreview(self, filename):
        surface = cairo.PDFSurface(filename, 595.0, 842.0)
        cc = cairo.Context(surface)
        c = pangocairo.CairoContext(cc)
        self.drawDetails(c)
        c.show_page()
        surface.finish()
        os.spawnlp(os.P_WAIT, 'evince', 'evince', filename)

if __name__ == "__main__":
    s = ProblemSheet()
    s.PrintPreview('pygtk-bug.pdf')




More information about the pygtk mailing list