def test_output_translate_rotate_scale(self): path = Path(__file__).parent.joinpath('output/svg_trs.svg') path.unlink(missing_ok=True) canvas_builder = CanvasBuilder() canvas_builder.set_path(path) canvas_builder.set_size( Cu.from_cm(4), Cu.from_cm(4) ) canvas = canvas_builder.build() background = Background() background.color = (0.8, 1, 0.8, 1) background.draw(canvas) svg = Svg(Path(__file__).parent.joinpath('test_svg_grid.svg')) svg_size = svg.read_svg_size() # Should set the origin of the image to the center. svg.svg_origin = Cc(svg_size[0] / 2, svg_size[1] / 2) # Should position the image in the center of the screen. svg.position = Cc.from_cm(2, 2) # Should rotate the image clock-wise. svg.rotation = math.pi / 8 # Resizes the image to almost the size of the canvas, but not exactly. svg.width = Cu.from_cm(3) svg.height = Cu.from_cm(3) svg.draw(canvas) canvas.close() assert path.exists()
def test_identity(self): assert CanvasUnit.from_pt(1).pt == 1 assert CanvasUnit.from_in(1).inches == 1 assert CanvasUnit.from_cm(1).cm == 1 assert CanvasUnit.from_mm(1).mm == 1 assert CanvasUnit.from_px(1).px == 1 assert CanvasUnit.from_pango(1).pango == 1 assert CanvasUnit.from_pt(2).pt == 2 assert CanvasUnit.from_in(2).inches == 2 assert CanvasUnit.from_cm(2).cm == 2 assert CanvasUnit.from_mm(2).mm == 2 assert CanvasUnit.from_px(2).px == 2 assert CanvasUnit.from_pango(2).pango == 2
def test_conversion(self): wgs84_crs = pyproj.CRS.from_epsg(4326) british_crs = pyproj.CRS.from_epsg(27700) coordinate_to_project = GeoCoordinate(55.862777, -4.260919, wgs84_crs) expected_canvas_coordinates = CanvasCoordinate( CanvasUnit.from_cm(1 + 6), CanvasUnit.from_cm(1 + 4)).pt origin_for_geo = GeoCoordinate(258000, 666000, british_crs) origin_for_canvas = CanvasCoordinate(CanvasUnit.from_cm(1), CanvasUnit.from_cm(1)) # 100 meters for every centimeter geo_to_canvas_scale = geo_canvas_ops.GeoCanvasScale( 100, CanvasUnit.from_cm(1)) transformation_func = geo_canvas_ops.build_transformer( crs=british_crs, data_crs=wgs84_crs, scale=geo_to_canvas_scale, origin_for_geo=origin_for_geo, origin_for_canvas=origin_for_canvas) self.assert_coordinates_are_close( transformation_func(*coordinate_to_project.tuple), expected_canvas_coordinates) # Repeat the same test, but this time without data_crs: transformation_func = geo_canvas_ops.build_transformer( crs=british_crs, scale=geo_to_canvas_scale, origin_for_geo=origin_for_geo, origin_for_canvas=origin_for_canvas) coordinate_to_project = GeoCoordinate(258600, 665600, british_crs) self.assert_coordinates_are_close( transformation_func(*coordinate_to_project.tuple), expected_canvas_coordinates)
def test_scale(self): path = Path(__file__).parent.joinpath('output/svg_scale.svg') path.unlink(missing_ok=True) canvas_builder = CanvasBuilder() canvas_builder.set_path(path) canvas_builder.set_size( Cu.from_cm(9), Cu.from_cm(6) ) canvas = canvas_builder.build() background = Background() background.color = (1, 0.8, 0.8, 1) background.draw(canvas) # No scaling svg = Svg(Path(__file__).parent.joinpath('test_svg.svg')) svg.position = Cc.from_cm(1, 1) svg.draw(canvas) # Scale 2x by height svg = Svg(Path(__file__).parent.joinpath('test_svg.svg')) svg.position = Cc.from_cm(5, 1) svg.width = Cu.from_cm(3) svg.draw(canvas) # Scale 2x by height svg = Svg(Path(__file__).parent.joinpath('test_svg.svg')) svg.position = Cc.from_cm(1, 4) svg.height = Cu.from_cm(1) svg.draw(canvas) # Scale without ratio preservation svg = Svg(Path(__file__).parent.joinpath('test_svg.svg')) svg.position = Cc.from_cm(5, 4) svg.width = Cu.from_cm(3) svg.height = Cu.from_cm(1) svg.draw(canvas) canvas.close() assert path.exists()
def test_div(self): assert CanvasUnit.from_cm(5) / 2.5 == CanvasUnit.from_cm(2) assert CanvasUnit.from_cm(5) / 2 == CanvasUnit.from_cm(2.5)
def test_comparisons(self): assert CanvasUnit.from_in(1).pt > CanvasUnit.from_cm(1).pt assert CanvasUnit.from_cm(1).pt > CanvasUnit.from_mm(1).pt assert CanvasUnit.from_mm(1).pt > CanvasUnit.from_pt(1).pt assert CanvasUnit.from_pt(1).pt > CanvasUnit.from_px(1).pt assert CanvasUnit.from_px(1).pt > CanvasUnit.from_pango(1).pt
def test_mul(self): assert CanvasUnit.from_cm(2.5) * 2 == CanvasUnit.from_cm(5) assert CanvasUnit.from_cm(2) * 2.5 == CanvasUnit.from_cm(5)
def test_neg(self): assert -CanvasUnit.from_cm(2) == CanvasUnit.from_cm(-2)
def test_sub(self): assert CanvasUnit.from_cm(2) - CanvasUnit.from_cm(3) == \ CanvasUnit.from_cm(-1) with self.assertRaises(NotImplementedError): CanvasUnit.from_cm(2) - 1
def test_add(self): assert CanvasUnit.from_cm(2) + CanvasUnit.from_cm(3) == \ CanvasUnit.from_cm(5) assert CanvasUnit.from_cm(2).__radd__(CanvasUnit.from_cm(3)) == \ CanvasUnit.from_cm(5) assert 0 + CanvasUnit.from_cm(2) == CanvasUnit.from_cm(2) assert CanvasUnit.from_cm(2) + 0 == CanvasUnit.from_cm(2) assert sum([ CanvasUnit.from_cm(2), CanvasUnit.from_cm(3), CanvasUnit.from_cm(4) ]) == CanvasUnit.from_cm(9) with self.assertRaises(NotImplementedError): CanvasUnit.from_cm(2) + 1 with self.assertRaises(NotImplementedError): 1 + CanvasUnit.from_cm(2)
def test_eq(self): assert CanvasUnit.from_cm(2) == CanvasUnit.from_cm(2) assert CanvasUnit.from_cm(2) != 2
def from_cm(cls, x: float, y: float) -> 'CanvasCoordinate': return CanvasCoordinate(CanvasUnit.from_cm(x), CanvasUnit.from_cm(y))