--- test_libartdevice.orig.py 2006-04-10 16:27:24.000000000 +0300 +++ test_libartdevice.py 2006-04-10 16:26:52.000000000 +0300 @@ -30,7 +30,7 @@ import support support.add_sketch_dir_to_path() -from Sketch import Identity, Trafo, Rect +from Sketch import Identity, Trafo, Translation, Scale, Rotation, Rect from Sketch.Graphics import Document, Rectangle, SolidPattern, StandardColors from Sketch.UI.libartdevice import LibartDevice @@ -158,5 +158,83 @@ self.check_images() +class TestTrafoStack(LibartDeviceTest): + + """Test LibartDevice with PushTrafo/PopTrafo""" + + width = 100 + height = 100 + + def setUp(self): + self.obj = Rectangle(Trafo(30, 0, 0, 30, 10, 10)) + pattern = SolidPattern(StandardColors.red) + self.obj.Properties().SetProperty(fill_pattern = pattern) + LibartDeviceTest.setUp(self) + + def tearDown(self): + self.obj = None + LibartDeviceTest.tearDown(self) + + def check_renderings(self, trafo): + """Compare two renderings of self.obj with trafo. + + The first image is created by rendering self.obj and a transformed + duplicate of self.obj. The second renders self.obj, adds the trafo to + the trafo_stack of the device and renders again self.obj. The + resulting images should be identical. + """ + rect = Rect(0, 0, self.width, self.height) + overlaps = rect.overlaps + + # Draw and export the first image. + # First transform with trafo a duplicate of self.obj + obj2 = self.obj.Duplicate() + obj2.Transform(trafo) + # Draw the two objects and save the image + self.dev.BeginDraw(None) + self.dev.DrawObjects([self.obj, obj2], rect, overlaps) + self.dev.EndDraw(None) + im1_name = self.make_image_filename(suffix = "-with-duplicate") + self.dev.export_image(im1_name) + + # Draw and export the second image. + # Start by drawing self.obj + self.dev.BeginDraw(None) + self.dev.DrawObjects([self.obj], rect, overlaps) + # Push trafo onto the stack, draw self.obj again and save the image. + self.dev.PushTrafo() + self.dev.Concat(trafo) + self.dev.DrawObjects([self.obj], rect, overlaps) + self.dev.PopTrafo() + self.dev.EndDraw(None) + im2_name = self.make_image_filename(suffix = "-with-stack") + self.dev.export_image(im2_name) + + # Compare the images + im1 = PIL.Image.open(im1_name) + im2 = PIL.Image.open(im2_name) + difference = PIL.ImageChops.difference(im1, im2) + difference.save(self.make_image_filename("-difference")) + + self.failUnless(max(map(max, difference.getextrema())) <= 1) + hist = difference.histogram() + num_pixels = difference.size[0] * difference.size[1] + max_ratio = 0.015 + self.failUnless(float(hist[1]) / num_pixels < max_ratio) + self.failUnless(float(hist[256 + 1]) / num_pixels < max_ratio) + self.failUnless(float(hist[2 * 256 + 1]) / num_pixels < max_ratio) + + def test_scale(self): + trafo = Scale(1.2) + self.check_renderings(trafo) + + def test_translation(self): + trafo = Translation(50, 50) + self.check_renderings(trafo) + + def test_rotation(self): + trafo = Rotation(0.2618, self.obj.coord_rect.center()) + self.check_renderings(trafo) + if __name__ == "__main__": unittest.main()