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 test_png(self): path = Path(__file__).parent.joinpath('output/canvas_builder.png') path.unlink(missing_ok=True) canvas_builder = CanvasBuilder() canvas_builder.set_path(path) canvas_builder.set_size(Cu.from_px(100), Cu.from_px(100)) canvas_builder.set_pixel_scale_factor(2) canvas = canvas_builder.build() canvas.close() assert path.exists()
def test_validating_pixel_scale_factor(self): path = Path(__file__).parent.joinpath( 'output/canvas_builder_pixel_scale_factor.svg') canvas_builder = CanvasBuilder() canvas_builder.set_path(path) canvas_builder.set_size(Cu.from_px(100), Cu.from_px(100)) canvas_builder.set_pixel_scale_factor(2) with self.assertRaises(RuntimeError): canvas_builder.build() assert not path.exists()