[cairo-commit] pycairo/test Makefile.am, 1.5, 1.6 isurface_create_from_png.py, NONE, 1.1 surface_create_for_stream.py, NONE, 1.1 surface_write_to_png.py, NONE, 1.1

Steve Chaplin commit at pdx.freedesktop.org
Sun Jul 2 20:30:09 PDT 2006


Committed by: stevech1097

Update of /cvs/cairo/pycairo/test
In directory kemper:/tmp/cvs-serv4471/test

Modified Files:
	Makefile.am 
Added Files:
	isurface_create_from_png.py surface_create_for_stream.py 
	surface_write_to_png.py 
Log Message:
'SC'

Index: Makefile.am
===================================================================
RCS file: /cvs/cairo/pycairo/test/Makefile.am,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- Makefile.am	3 Jul 2006 03:22:05 -0000	1.5
+++ Makefile.am	3 Jul 2006 03:30:07 -0000	1.6
@@ -3,6 +3,9 @@
 	isurface_create_for_data1.py \
 	isurface_create_for_data2.py \
 	isurface_create_for_data3.py \
+	isurface_create_from_png.py \
 	isurface_get_data_as_rgba.py \
+	surface_create_for_stream.py \
+	surface_write_to_png.py \
 	test.py \
 	test.test

--- NEW FILE: isurface_create_from_png.py ---
#!/usr/bin/env python
# test cairo.ImageSurface.create_from_png()

import cairo

surface = cairo.ImageSurface.create_from_png("/tmp/warpedtext.png")

# write to filename
surface.write_to_png("/tmp/t1.png")

# write to file object
f2=file("/tmp/t2.png", "w")
surface.write_to_png(f2)
f2.close()

# write to object that has a "write" method
import StringIO
buffer = StringIO.StringIO()
surface.write_to_png(buffer)
png_string = buffer.getvalue()
buffer.close()
f3=file("/tmp/t3.png", "w")
f3.write(png_string)
f3.close()

# write to object that has a "write" method
import cStringIO
buffer = cStringIO.StringIO()
surface.write_to_png(buffer)
png_string = buffer.getvalue()
buffer.close()
f4=file("/tmp/t4.png", "w")
f4.write(png_string)
f4.close()

# error test - to check the error message, should raise TypeError
#surface.write_to_png(101)

--- NEW FILE: surface_create_for_stream.py ---
#!/usr/bin/env python
"""
Test PDF/PS/SVG constructors (using streams)
"""

import cStringIO
import gc
import math
import sys
import StringIO

import cairo


class C(object):
    """a file-like object (for testing), it simulates sys.stdout
    """
    def __init__ (self):
        self.closed = False

    def write(self, s):
        """just echo to stdout, without newlines"""
        if self.closed:
            raise ValueError ("I/O operation on closed file")
        sys.stdout.write (s)

    def close(self):
        self.closed = True


# a selection of possible args to surface.write_to_png()
#fo = '/tmp/f.ps'
fo = file('/tmp/f.svg', 'w')
#fo = StringIO.StringIO()
#fo = cStringIO.StringIO()
#fo = sys.stdout
#fo = C()

#fo.close()  # this should cause: ValueError: I/O operation on closed file

WIDTH, HEIGHT  = 256, 256

#surface = cairo.PDFSurface (fo, WIDTH, HEIGHT)
#surface = cairo.PSSurface (fo, WIDTH, HEIGHT)
surface = cairo.SVGSurface (fo, WIDTH, HEIGHT)

#sys.stdout.write ('1\n'); sys.stdout.flush()
ctx = cairo.Context (surface)

#del fo  # test that 'fo' is referenced to keep it alive
#gc.collect()

#fo.close()  # this should cause: ValueError: I/O operation on closed file

ctx.scale (WIDTH/1.0, HEIGHT/1.0)

pat = cairo.LinearGradient (0.0, 0.0, 0.0, 1.0)
pat.add_color_stop_rgba (1, 0, 0, 0, 1)
pat.add_color_stop_rgba (0, 1, 1, 1, 1)

ctx.rectangle (0,0,1,1)
ctx.set_source (pat)
ctx.fill ()

pat = cairo.RadialGradient (0.45, 0.4, 0.1,
                            0.4,  0.4, 0.5)
pat.add_color_stop_rgba (0, 1, 1, 1, 1)
pat.add_color_stop_rgba (1, 0, 0, 0, 1)

ctx.set_source (pat)
ctx.arc (0.5, 0.5, 0.3, 0, 2 * math.pi)
ctx.fill ()

ctx.show_page()
surface.finish()

# for testing StringIO: get data and write to file
#string = fo.getvalue()
#f2 = file('/tmp/f.ps', 'w')
#f2.write(string)
#f2.close()

--- NEW FILE: surface_write_to_png.py ---
#!/usr/bin/env python
"""
Test Surface.write_to_png()
"""

import cStringIO
import math
import sys
import StringIO

import cairo


class C(object):
    """a file-like object (for testing), it simulates sys.stdout
    """
    def __init__ (self):
        self.closed = False

    def write(self, s):
        """just echo to stdout, without newlines"""
        if self.closed:
            raise ValueError ("I/O operation on closed file")
        sys.stdout.write (s)

    def close(self):
        self.closed = True


WIDTH, HEIGHT  = 256, 256

surface = cairo.ImageSurface (cairo.FORMAT_ARGB32, WIDTH, HEIGHT)
ctx = cairo.Context (surface)

ctx.scale (WIDTH/1.0, HEIGHT/1.0)

pat = cairo.LinearGradient (0.0, 0.0, 0.0, 1.0)
pat.add_color_stop_rgba (1, 0, 0, 0, 1)
pat.add_color_stop_rgba (0, 1, 1, 1, 1)

ctx.rectangle (0,0,1,1)
ctx.set_source (pat)
ctx.fill ()

pat = cairo.RadialGradient (0.45, 0.4, 0.1,
                            0.4,  0.4, 0.5)
pat.add_color_stop_rgba (0, 1, 1, 1, 1)
pat.add_color_stop_rgba (1, 0, 0, 0, 1)

ctx.set_source (pat)
ctx.arc (0.5, 0.5, 0.3, 0, 2 * math.pi)
ctx.fill ()

# a selection of possible args to surface.write_to_png()
#fo = '/tmp/f.png'
fo = file('/tmp/f.png', 'w')
#fo = StringIO.StringIO()
#fo = cStringIO.StringIO()
#fo = sys.stdout
#fo = C()

fo.close()  # this should cause: ValueError: I/O operation on closed file
surface.write_to_png (fo)

# for testing StringIO: get data and write to file
#string = fo.getvalue()
#f2 = file('/tmp/f.png', 'w')
#f2.write(string)
#f2.close()



More information about the cairo-commit mailing list