def test_scaling_outputs(self): surface_types = [('pdf', 1), ('svg', 1), ('png', 0.5), ('png', 1), ('png', 2)] units = ['pt', 'in', 'cm', 'mm', 'px'] for (surface_type, pixel_scale_factor) in surface_types: for unit in units: canvas_builder = CanvasBuilder() # We want to test all units, but some sizes are just too big # and will take up lots of disk space. So we make an exception # for inches and centimeters. expected_size_in_unit: int expected_size: Optional[CanvasUnit] if unit in ['in', 'cm']: expected_size_in_unit = 4 expected_size = Cu.from_unit(4, unit) # Setting to a smaller size to avoid disk space issues canvas_builder.set_size(expected_size, expected_size) else: expected_size_in_unit = 100 expected_size = CanvasUnit.from_unit(100, unit) canvas_builder.set_size(expected_size, expected_size) path = Path(__file__).parent \ .joinpath( 'output/canvas_builder_%s_%d%s_x%s.%s' % ( surface_type, expected_size_in_unit, unit, pixel_scale_factor, surface_type ) ) path.unlink(missing_ok=True) canvas_builder.set_path(path) canvas_builder.set_pixel_scale_factor(pixel_scale_factor) canvas = canvas_builder.build() self.draw_rectangle_top_left(canvas, unit) canvas.close() assert path.exists() actual = self.get_file_dimensions_and_scale(path) if surface_type == 'pdf': self.assert_match(expected_size_in_unit, unit, surface_type, 1, actual, expected_size.pt) elif surface_type == 'svg': self.assert_match(expected_size_in_unit, unit, surface_type, 1, actual, expected_size.pt) elif surface_type == 'png': self.assert_match(expected_size_in_unit, unit, surface_type, pixel_scale_factor, actual, expected_size.px)
def draw_rectangle_top_left(canvas: Canvas, unit: str): if unit in ['in', 'cm']: # Setting to a smaller size to avoid disk space issues midpoint = CanvasUnit.from_unit(2, unit).pt else: midpoint = CanvasUnit.from_unit(50, unit).pt top_left = (0, 0) top_right = (midpoint, 0) bottom_left = (0, midpoint) bottom_right = (midpoint, midpoint) shape = Polygon( [top_left, top_right, bottom_right, bottom_left, top_left]) canvas.context.set_source_rgb(0, 0, 0) CairoHelper.draw_polygon(canvas.context, shape) canvas.context.fill() point_unit_shape = Polygon([(0, 0), (0, 1), (1, 1), (1, 0), (0, 0)]) canvas.context.set_source_rgb(1, 0, 0) CairoHelper.draw_polygon(canvas.context, point_unit_shape) canvas.context.fill()
def draw_rectangle(self, canvas: Canvas, offset: int, unit: str): height = CanvasUnit.from_pt(10).pt width = CanvasUnit.from_unit(1, unit).pt offset_height = height * offset top_left = (0, offset_height) top_right = (width, offset_height) bottom_left = (0, offset_height + height) bottom_right = (width, offset_height + height) shape = Polygon([ top_left, top_right, bottom_right, bottom_left, top_left ]) canvas.context.set_source_rgba(*self.color) CairoHelper.draw_polygon(canvas.context, shape) canvas.context.set_source_rgba(0.5, 0.5, 0.5, 1) canvas.context.fill()
def test_unknown_unit(self): with self.assertRaises(Exception): CanvasUnit.from_unit(1, 'unknown')