Topic: Cairo calling
Jeff,
Thanks to the Simply Fortran magic, I have been able to call
Cairo graphics library (almost) directly from Fortran. I just created SF project
with two files:
(fortran code )
!!!
program cairo_two
use iso_c_binding
implicit none
!
interface
subroutine spiral_png (a, b) bind (c, name = 'spiral_png')
! plots spiral shape with label text at position a, b
use iso_c_binding
real(c_float), value :: a, b
end subroutine
end interface
!
real a, b
a = 40.0
b = 60.0
print *, "Fortran calling Cairo void function spiral_png"
call spiral_png(a, b)
end program
!!!
(and C-code)
!!!
/* C source to be compiled by SF, no main function */
#include <cairo.h>
#define _USE_MATH_DEFINES
#include <math.h>
/* spiral png*/
void spiral_png(float a, float b) {
cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, 800, 800);
cairo_t *cr = cairo_create(surface);
cairo_set_source_rgb(cr, 1, 1, 1);
cairo_paint(cr);
cairo_set_source_rgb(cr, 0, 0, 0);
for (int i = 0; i <= 10000; i++) {
double x = 400 + cos(2 * M_PI * i / 500) * 140 + cos(2 * M_PI * i / 10000) * 220;
double y = 400 + sin(2 * M_PI * i / 500) * 140 + sin(2 * M_PI * i / 10000) * 220;
if (i == 0)
cairo_move_to(cr, x, y);
else
cairo_line_to(cr, x, y);
}
cairo_close_path(cr);
cairo_stroke(cr);
cairo_select_font_face(cr, "Sans", CAIRO_FONT_SLANT_NORMAL,
CAIRO_FONT_WEIGHT_NORMAL);
cairo_set_font_size(cr, 40.0);
cairo_move_to(cr, a, b);
cairo_show_text(cr, "Spiral");
cairo_surface_write_to_png(surface, "spiral.png");
cairo_destroy(cr);
cairo_surface_destroy(surface);
}
!!!!
Project compiles and PNG file with a nice plot is created.
My question is: How to draw on window created by AppGraphics?
Win32/GDI/C programming is high above me, but I tried blindly:
...
#include <cairo-win32.h>
#include <appgraphics.h>
...
void spiral_gdi(float a, float b) {
int wiha;
wiha = initwindow(800, 800,
"Cairo drawing",
DEFAULT_POSITION, DEFAULT_POSITION,
FALSE,
TRUE);
HDC dc = GetDC(wiha);
cairo_surface_t *surface = cairo_win32_surface_create(dc);
ReleaseDC(wiha, dc);
...
...
I don't know if it has any sense, since I have got compile error
(..undefined reference to `initwindow').
Any help is appreciated,
Carlos,
BTW, Cairo binaries came from https://github.com/preshing/cairo-windows/releases