diff --git a/src/backends/fig_poly_shape.f90 b/src/backends/fig_poly_shape.f90 index caf0b99..0434227 100644 --- a/src/backends/fig_poly_shape.f90 +++ b/src/backends/fig_poly_shape.f90 @@ -6,6 +6,9 @@ module fig_poly procedure :: add_points end type polyline + type, extends(polyline) :: polygon + end type polygon + contains subroutine add_points(this, x, y, n) diff --git a/src/backends/generic_cairo/cairo_backend.f90 b/src/backends/generic_cairo/cairo_backend.f90 index 02a8293..25f0be9 100644 --- a/src/backends/generic_cairo/cairo_backend.f90 +++ b/src/backends/generic_cairo/cairo_backend.f90 @@ -70,6 +70,8 @@ subroutine cairo_write_shape(canva,sh) call write_path(canva, canva%cairo, sh) type is (polyline) call write_polyline(canva, canva%cairo, sh) + type is (polygon) + call write_polygon(canva, canva%cairo, sh) end select end subroutine cairo_write_shape diff --git a/src/backends/generic_cairo/shapes/poly.f90 b/src/backends/generic_cairo/shapes/poly.f90 index cf9481d..cd1e5fc 100644 --- a/src/backends/generic_cairo/shapes/poly.f90 +++ b/src/backends/generic_cairo/shapes/poly.f90 @@ -27,6 +27,29 @@ subroutine write_polyline(canva, cr, pl) call fill(cr,pl) call stroke(cr,pl) end subroutine write_polyline + + subroutine write_polygon(canva, cr, pl) + class(base_canvas), intent(inout) :: canva + type(c_ptr), intent(inout):: cr + type(polygon), intent(in) :: pl + type(point), allocatable:: points(:) + integer :: p_count,i + type(canvas_point) :: p + type(canvas_size):: s + call parse_string(pl%poly_string, points, p_count) + + p = to_canvas(points(1), canva%size) + call cairo_move_to(cr, p%x, p%y) + do i = 2, p_count, 1 + p = to_canvas(points(i), canva%size) + call cairo_line_to(cr, p%x, p%y) + end do + p = to_canvas(points(1), canva%size) + call cairo_line_to(cr, p%x, p%y) + + call fill(cr,pl) + call stroke(cr,pl) + end subroutine write_polygon subroutine parse_string(input_str, points, p_count) implicit none diff --git a/test/poly_test.f90 b/test/poly_test.f90 index c2bed7e..6d49d18 100644 --- a/test/poly_test.f90 +++ b/test/poly_test.f90 @@ -10,20 +10,27 @@ program path_test implicit none integer, parameter :: CANVAS_WIDTH = 500 - integer, parameter :: CANVAS_HEIGHT = 180 + integer, parameter :: CANVAS_HEIGHT = 210 character(len=:), allocatable :: file_name type(drawing) :: canva type(svg_canvas) :: svg_canva type(bitmap_canvas) :: bitmap_canva type(polyline) :: pl + type(polygon) :: pg real :: x_points(7) = [0.0, 40.0, 40.0, 80.0, 80.0, 120.0, 120.0] real :: y_points(7) = [40.0, 40.0, 80.0, 80.0, 120.0, 120.0, 160.0] - integer :: n=7,i + + real :: polygon_x_points(5) = [200.0, 140.0, 290.0, 110.0, 260.0] + real :: polygon_y_points(5) = [10.0, 198.0, 78.0, 78.0, 198.0] + integer :: n=7,n2=5,i type(point), allocatable:: points(:) integer :: p_count, real_ind x_points=x_points/CANVAS_WIDTH y_points=y_points/CANVAS_HEIGHT + + polygon_x_points=polygon_x_points/CANVAS_WIDTH + polygon_y_points=polygon_y_points/CANVAS_HEIGHT call canva%init() file_name= "poly_example" call pl%add_points(x_points, y_points, n) @@ -32,6 +39,13 @@ program path_test pl%stroke_width=4 call canva%add_shape(pl) + call pg%add_points(polygon_x_points, polygon_y_points, n2) + pg%stroke_color=FIG_COLOR_BLACK + pg%fill_color=FIG_COLOR_YELLOW + pg%stroke_width=4 + call canva%add_shape(pg) + + call bitmap_canva%init(CANVAS_WIDTH,CANVAS_HEIGHT,file_name) call bitmap_canva%apply_shapes(canva) call bitmap_canva%save_to_png()