Skip to content

Commit

Permalink
draw polyline
Browse files Browse the repository at this point in the history
  • Loading branch information
AnonMiraj committed Jul 10, 2024
1 parent 1467775 commit 728eebe
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/backends/generic_cairo/cairo_backend.f90
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module fig_cairo
use fig_cairo_line
use fig_path
use fig_cairo_path
use fig_cairo_poly
use fig_cairo_rect
use fig_cairo_text
use fig_cairo_triangle
Expand Down Expand Up @@ -67,6 +68,8 @@ subroutine cairo_write_shape(canva,sh)
call write_text(canva, canva%cairo, sh)
type is (path)
call write_path(canva, canva%cairo, sh)
type is (polyline)
call write_polyline(canva, canva%cairo, sh)
end select

end subroutine cairo_write_shape
Expand Down
109 changes: 109 additions & 0 deletions src/backends/generic_cairo/shapes/poly.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
module fig_cairo_poly
use cairo
use fig_shapes
use fig_canvas
use fig_cairo_utils
use fig_poly

contains

subroutine write_polyline(canva, cr, pl)
class(base_canvas), intent(inout) :: canva
type(c_ptr), intent(inout):: cr
type(polyline), 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

call fill(cr,pl)
call stroke(cr,pl)
end subroutine write_polyline

subroutine parse_string(input_str, points, p_count)
implicit none
character(len=*), intent(in) :: input_str
type(point), allocatable, intent(out) :: points(:)
integer, intent(out) :: p_count
integer :: i, length
character(len=1) :: current_char
character(len=20) :: temp_string
logical :: is_num

p_count = -1
length = len_trim(input_str)

allocate(points(length))

i = 1
do while (i <= length)
current_char = input_str(i:i)

if (current_char == '-') then
temp_string = '-'
i = i + 1
if (i <= length) current_char = input_str(i:i)
is_num = .true.
do while (is_num .and. i <= length)
if ((current_char >= '0' .and. current_char <= '9') .or. current_char == '.') then
temp_string = trim(temp_string) // current_char
i = i + 1
if (i <= length) then
current_char = input_str(i:i)
else
is_num = .false.
end if
else
is_num = .false.
end if
end do
if (len_trim(temp_string) > 0 .and. temp_string /= '-') then
p_count = p_count + 1

if (iand(p_count,1)/=1) then
read(temp_string, *) points(p_count/2+1)%x
else
read(temp_string, *) points(p_count/2+1)%y
end if
end if
else if ((current_char >= '0' .and. current_char <= '9') .or. current_char == '.') then
temp_string = ''
is_num = .true.
do while (is_num .and. i <= length)
if ((current_char >= '0' .and. current_char <= '9') .or. current_char == '.') then
temp_string = trim(temp_string) // current_char
i = i + 1
if (i <= length) then
current_char = input_str(i:i)
else
is_num = .false.
end if
else
is_num = .false.
end if
end do
if (len_trim(temp_string) > 0) then
p_count = p_count + 1

if (iand(p_count,1)/=1) then
read(temp_string, *) points(p_count/2+1)%x
else
read(temp_string, *) points(p_count/2+1)%y
end if
end if
else
i = i + 1
end if
end do
p_count = (p_count+1) /2

end subroutine parse_string
end module fig_cairo_poly
1 change: 0 additions & 1 deletion test/path_test.f90
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ program path_test
my_path%fill_color = FIG_COLOR_LIGHTGOLDENRODYELLOW
my_path%stroke_color = FIG_COLOR_AQUA
call canva%add_shape(my_path)
print *,my_path%path_string

! Save to bitmap and SVG
call bitmap_canva%init(CANVAS_WIDTH,CANVAS_HEIGHT,file_name)
Expand Down
48 changes: 48 additions & 0 deletions test/poly_test.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
program path_test
use fig_poly
use fig_drawing
use fig_svg
use fig_types
use fig_bitmap
use fig_test
use fig_rgb_color_constants
use fig_cairo_poly

implicit none
integer, parameter :: CANVAS_WIDTH = 500
integer, parameter :: CANVAS_HEIGHT = 180
character(len=:), allocatable :: file_name
type(drawing) :: canva
type(svg_canvas) :: svg_canva
type(bitmap_canvas) :: bitmap_canva
type(polyline) :: pl
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
type(point), allocatable:: points(:)
integer :: p_count, real_ind

x_points=x_points/CANVAS_WIDTH
y_points=y_points/CANVAS_HEIGHT
call canva%init()
file_name= "poly_example"
call pl%add_points(x_points, y_points, n)
pl%stroke_color=FIG_COLOR_RED
pl%fill_color=FIG_COLOR_YELLOW
pl%stroke_width=4
call canva%add_shape(pl)

call bitmap_canva%init(CANVAS_WIDTH,CANVAS_HEIGHT,file_name)
call bitmap_canva%apply_shapes(canva)
call bitmap_canva%save_to_png()
call bitmap_canva%save_to_ppm()
call bitmap_canva%destroy()

call svg_canva%init(CANVAS_WIDTH,CANVAS_HEIGHT,file_name)
call svg_canva%apply_shapes(canva)
call svg_canva%save_to_svg()
call svg_canva%destroy()
call test_both(file_name,bitmap_canva)

end program path_test

0 comments on commit 728eebe

Please sign in to comment.