()
Testing routine for L{TextDrawer}
Testing routine for L{TextDrawer}
Testing routine for L { TextDrawer }
def test():
    """Testing routine for L{TextDrawer}"""
    import math
    from igraph.drawing.utils import find_cairo

    cairo = find_cairo()

    text = "The quick brown fox\njumps over a\nlazy dog"
    width, height = (600, 1000)

    surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
    context = cairo.Context(surface)
    drawer = TextDrawer(context, text)

    context.set_source_rgb(1, 1, 1)
    context.set_font_size(16.0)
    context.rectangle(0, 0, width, height)
    context.fill()

    context.set_source_rgb(0.5, 0.5, 0.5)
    for i in range(200, width, 200):
        context.move_to(i, 0)
        context.line_to(i, height)
        context.stroke()
    for i in range(200, height, 200):
        context.move_to(0, i)
        context.line_to(width, i)
        context.stroke()
    context.set_source_rgb(0.75, 0.75, 0.75)
    context.set_line_width(0.5)
    for i in range(100, width, 200):
        context.move_to(i, 0)
        context.line_to(i, height)
        context.stroke()
    for i in range(100, height, 200):
        context.move_to(0, i)
        context.line_to(width, i)
        context.stroke()

    def mark_point(red, green, blue):
        """Marks the current point on the canvas by the given color"""
        x, y = context.get_current_point()
        context.set_source_rgba(red, green, blue, 0.5)
        context.arc(x, y, 4, 0, 2 * math.pi)
        context.fill()

    # Testing drawer.draw_at()
    for i, halign in enumerate(("left", "center", "right")):
        # Mark the reference points
        context.move_to(i * 200, 40)
        mark_point(0, 0, 1)
        context.move_to(i * 200, 140)
        mark_point(0, 0, 1)

        # Draw the text
        context.set_source_rgb(0, 0, 0)
        drawer.halign = halign
        drawer.draw_at(i * 200, 40)
        drawer.draw_at(i * 200, 140, width=200)

        # Mark the new reference point
        mark_point(1, 0, 0)

    # Testing TextDrawer.draw()
    for i, halign in enumerate(("left", "center", "right")):
        for j, valign in enumerate(("top", "center", "bottom")):
            # Draw the text
            context.set_source_rgb(0, 0, 0)
            drawer.halign = halign
            drawer.valign = valign
            drawer.bbox = (i * 200, j * 200 + 200, i * 200 + 200, j * 200 + 400)
            drawer.draw()
            # Mark the new reference point
            mark_point(1, 0, 0)

    # Testing TextDrawer.wrap()
    drawer.text = (
        "Jackdaws love my big sphinx of quartz. Yay, wrapping! "
        + "Jackdaws love my big sphinx of quartz.\n\n"
        + "Jackdaws  love  my  big  sphinx  of  quartz."
    )
    drawer.valign = TextDrawer.TOP
    for i, halign in enumerate(("left", "center", "right")):
        context.move_to(i * 200, 840)

        # Mark the reference point
        mark_point(0, 0, 1)

        # Draw the text
        context.set_source_rgb(0, 0, 0)
        drawer.halign = halign
        drawer.draw_at(i * 200, 840, width=199, wrap=True)

        # Mark the new reference point
        mark_point(1, 0, 0)

    surface.write_to_png("test.png")
def test ( ) : import math from igraph . drawing . utils import find_cairo cairo = find_cairo ( ) text = "The quick brown fox\njumps over a\nlazy dog" width , height = ( 600 , 1000 ) surface = cairo . ImageSurface ( cairo . FORMAT_ARGB32 , width , height ) context = cairo . Context ( surface ) drawer = TextDrawer ( context , text ) context . set_source_rgb ( 1 , 1 , 1 ) context . set_font_size ( 16.0 ) context . rectangle ( 0 , 0 , width , height ) context . fill ( ) context . set_source_rgb ( 0.5 , 0.5 , 0.5 ) for i in range ( 200 , width , 200 ) : context . move_to ( i , 0 ) context . line_to ( i , height ) context . stroke ( ) for i in range ( 200 , height , 200 ) : context . move_to ( 0 , i ) context . line_to ( width , i ) context . stroke ( ) context . set_source_rgb ( 0.75 , 0.75 , 0.75 ) context . set_line_width ( 0.5 ) for i in range ( 100 , width , 200 ) : context . move_to ( i , 0 ) context . line_to ( i , height ) context . stroke ( ) for i in range ( 100 , height , 200 ) : context . move_to ( 0 , i ) context . line_to ( width , i ) context . stroke ( ) def mark_point ( red , green , blue ) : """Marks the current point on the canvas by the given color""" x , y = context . get_current_point ( ) context . set_source_rgba ( red , green , blue , 0.5 ) context . arc ( x , y , 4 , 0 , 2 * math . pi ) context . fill ( ) # Testing drawer.draw_at() for i , halign in enumerate ( ( "left" , "center" , "right" ) ) : # Mark the reference points context . move_to ( i * 200 , 40 ) mark_point ( 0 , 0 , 1 ) context . move_to ( i * 200 , 140 ) mark_point ( 0 , 0 , 1 ) # Draw the text context . set_source_rgb ( 0 , 0 , 0 ) drawer . halign = halign drawer . draw_at ( i * 200 , 40 ) drawer . draw_at ( i * 200 , 140 , width = 200 ) # Mark the new reference point mark_point ( 1 , 0 , 0 ) # Testing TextDrawer.draw() for i , halign in enumerate ( ( "left" , "center" , "right" ) ) : for j , valign in enumerate ( ( "top" , "center" , "bottom" ) ) : # Draw the text context . set_source_rgb ( 0 , 0 , 0 ) drawer . halign = halign drawer . valign = valign drawer . bbox = ( i * 200 , j * 200 + 200 , i * 200 + 200 , j * 200 + 400 ) drawer . draw ( ) # Mark the new reference point mark_point ( 1 , 0 , 0 ) # Testing TextDrawer.wrap() drawer . text = ( "Jackdaws love my big sphinx of quartz. Yay, wrapping! " + "Jackdaws love my big sphinx of quartz.\n\n" + "Jackdaws  love  my  big  sphinx  of  quartz." ) drawer . valign = TextDrawer . TOP for i , halign in enumerate ( ( "left" , "center" , "right" ) ) : context . move_to ( i * 200 , 840 ) # Mark the reference point mark_point ( 0 , 0 , 1 ) # Draw the text context . set_source_rgb ( 0 , 0 , 0 ) drawer . halign = halign drawer . draw_at ( i * 200 , 840 , width = 199 , wrap = True ) # Mark the new reference point mark_point ( 1 , 0 , 0 ) surface . write_to_png ( "test.png" )