-
Notifications
You must be signed in to change notification settings - Fork 43
/
cairo-basics.f90
179 lines (158 loc) · 7.22 KB
/
cairo-basics.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
! This file is part of gtk-fortran, a GTK / Fortran interface library.
! Copyright (C) 2011 The gtk-fortran team
!
! This is free software; you can redistribute it and/or modify
! it under the terms of the GNU General Public License as published by
! the Free Software Foundation; either version 3, or (at your option)
! any later version.
!
! This software is distributed in the hope that it will be useful,
! but WITHOUT ANY WARRANTY; without even the implied warranty of
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
! GNU General Public License for more details.
!
! Under Section 7 of GPL version 3, you are granted additional
! permissions described in the GCC Runtime Library Exception, version
! 3.1, as published by the Free Software Foundation.
!
! You should have received a copy of the GNU General Public License along with
! this program; see the files LICENSE and LICENSE_EXCEPTION respectively.
! If not, see <https://www.gnu.org/licenses/>.
!
! Contributed by Jerry DeLisle and Vincent Magnin
! Last modification: vmagnin 2023-04-23
module handlers
use, intrinsic :: iso_c_binding
use gtk, only: gtk_application_window_new, gtk_drawing_area_new, &
& gtk_drawing_area_set_content_width, gtk_drawing_area_set_content_height, &
& gtk_drawing_area_set_draw_func, gtk_window_set_child, gtk_widget_show, &
& gtk_window_set_default_size, gtk_window_set_title, CAIRO_SVG_VERSION_1_2, &
& FALSE, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL
use cairo, only: cairo_arc, cairo_create, cairo_curve_to, cairo_destroy, &
& cairo_get_target, cairo_line_to, cairo_move_to, cairo_new_sub_path, &
& cairo_select_font_face, cairo_set_font_size, cairo_set_line_width, &
& cairo_set_source, cairo_set_source_rgb, cairo_show_text, cairo_stroke, &
& cairo_surface_write_to_png, cairo_svg_surface_create, &
& cairo_svg_surface_restrict_to_version, cairo_surface_destroy, &
& cairo_pdf_surface_create
implicit none
contains
! The GUI is defined here:
subroutine activate(app, gdata) bind(c)
type(c_ptr), value, intent(in) :: app, gdata
type(c_ptr) :: window
type(c_ptr) :: my_drawing_area
integer(c_int) :: width, height
window = gtk_application_window_new(app)
width = 700
height = 700
call gtk_window_set_default_size(window, width, height)
call gtk_window_set_title(window, "Cairo basics demo"//c_null_char)
my_drawing_area = gtk_drawing_area_new()
call gtk_drawing_area_set_content_width(my_drawing_area, width)
call gtk_drawing_area_set_content_height(my_drawing_area, height)
call gtk_drawing_area_set_draw_func(my_drawing_area, &
& c_funloc(my_draw_function), c_null_ptr, c_null_funptr)
call gtk_window_set_child(window, my_drawing_area)
call gtk_widget_show(window)
end subroutine activate
! "It is called whenever GTK needs to draw the contents of the drawing area
! to the screen."
! https://docs.gtk.org/gtk4/class.DrawingArea.html
subroutine my_draw_function(widget, my_cairo_context, width, height, gdata) bind(c)
use, intrinsic :: iso_c_binding, only: dp=>c_double
type(c_ptr), value, intent(in) :: widget, my_cairo_context, gdata
integer(c_int), value, intent(in) :: width, height
integer :: cstatus
integer :: rendering
type(c_ptr) :: surface_svg, surface_pdf, cr_svg, cr_pdf
! We will draw three times, once for screen, once in a SVG file, once in a PDF:
do rendering = 1, 3
if (rendering == 1) then
! Rendering on screen:
call draw(my_cairo_context, width, height)
! Save the image as a PNG:
cstatus = cairo_surface_write_to_png(cairo_get_target(my_cairo_context), &
& "cairo-basics.png"//c_null_char)
call cairo_destroy(my_cairo_context)
print *, "Saved in cairo-basics.png"
else if (rendering == 2) then
! Rendering in a SVG file:
surface_svg = cairo_svg_surface_create("cairo-basics.svg"//c_null_char, &
& real(width, KIND=dp), real(height, KIND=dp))
cr_svg = cairo_create(surface_svg)
call cairo_svg_surface_restrict_to_version(surface_svg, CAIRO_SVG_VERSION_1_2)
call draw(cr_svg, width, height)
call cairo_destroy(cr_svg)
call cairo_surface_destroy(surface_svg)
print *, "Saved in cairo-basics.svg"
else
! Rendering in a PDF file:
surface_pdf = cairo_pdf_surface_create("cairo-basics.pdf"//c_null_char, &
& real(width, KIND=dp), real(height, KIND=dp))
cr_pdf = cairo_create(surface_pdf)
call draw(cr_pdf, width, height)
call cairo_surface_destroy(surface_pdf)
print *, "Saved in cairo-basics.pdf"
end if
end do
end subroutine my_draw_function
! It will be called two time, for screen and SVG file:
subroutine draw(cr, width, height)
use, intrinsic :: iso_c_binding, only: dp=>c_double
type(c_ptr), value, intent(in) :: cr
integer(c_int), value, intent(in) :: width, height
integer :: t
real(dp), parameter :: pi = acos(-1.0_dp)
! Bezier curve:
call cairo_set_source_rgb(cr, 0.9_dp, 0.8_dp, 0.8_dp)
call cairo_set_line_width(cr, 4._dp)
call cairo_move_to(cr, 0._dp, 0._dp)
call cairo_curve_to(cr, 600._dp, 50._dp, 115._dp, 545._dp, &
& width*1._dp, height*1._dp)
call cairo_stroke(cr)
! Lines:
call cairo_set_source_rgb(cr, 0._dp, 0.5_dp, 0.5_dp)
call cairo_set_line_width(cr, 2._dp)
do t = 0, height, +20
call cairo_move_to(cr, 0._dp, t*1._dp)
call cairo_line_to(cr, t*1._dp, height*1._dp)
call cairo_stroke(cr)
end do
! Text:
call cairo_set_source_rgb(cr, 0._dp, 0._dp, 1._dp)
call cairo_select_font_face(cr, "Times"//c_null_char, &
& CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL)
call cairo_set_font_size (cr, 40._dp)
call cairo_move_to(cr, 100._dp, 30._dp)
call cairo_show_text (cr, "gtk-fortran"//c_null_char)
call cairo_move_to(cr, 100._dp, 75._dp)
call cairo_show_text (cr, &
& "Cairo & Fortran are good friends"//c_null_char)
! Circles:
call cairo_new_sub_path(cr)
do t = 1, 50
call cairo_set_source_rgb(cr, t/50._dp, 0._dp, 0._dp)
call cairo_set_line_width(cr, 5._dp*t/50._dp)
call cairo_arc(cr, 353._dp + 200._dp*cos(t*2._dp*pi/50), &
& 350._dp + 200._dp*sin(t*2._dp*pi/50), 50._dp, 0._dp, 2*pi)
call cairo_stroke(cr)
end do
end subroutine draw
end module handlers
! We create a GtkApplication:
program cairo_basics
use, intrinsic :: iso_c_binding
use gtk, only: gtk_application_new, g_signal_connect, G_APPLICATION_FLAGS_NONE
use g, only: g_application_run, g_object_unref
use handlers, only: activate
implicit none
integer(c_int) :: exit_status
type(c_ptr) :: app
app = gtk_application_new("gtk-fortran.examples.cairo-basics"//c_null_char, &
& G_APPLICATION_FLAGS_NONE)
call g_signal_connect(app, "activate"//c_null_char, c_funloc(activate), &
& c_null_ptr)
exit_status = g_application_run(app, 0_c_int, [c_null_ptr])
call g_object_unref(app)
end program cairo_basics