While we are looking for a solution, I tried to write some reusable code to show nicely Cairo version in all programs. The method for drawing the rectangle comes from a C example shipped with Cairo source.
Code: Select all
' cairocolor.bi
type tcairocolor
r as double
g as double
b as double
a as double
declare constructor(as uinteger, as double = 1.0)
declare constructor(as double, as double, as double, as double = 1.0)
end type
constructor tcairocolor(rgb_ as uinteger, a_ as double)
r = (rgb_ and &hFF0000) / &hFF0000
g = (rgb_ and &h00FF00) / &h00FF00
b = (rgb_ and &h0000FF) / &h0000FF
a = a_
end constructor
constructor tcairocolor(r_ as double, g_ as double, b_ as double, a_ as double)
r = r_
g = g_
b = b_
a = a_
end constructor
/'
dim c1 as tcairocolor = tcairocolor(rgb(&HFF, &H00, &HFF))
with c1
print(.r)
print(.g)
print(.b)
print(.a)
end with
dim c2 as tcairocolor = tcairocolor(rgb(&HFF, &H00, &HFF), 0.5)
with c2
print(.r)
print(.g)
print(.b)
print(.a)
end with
dim c3 as tcairocolor = tcairocolor(1.0, 1.0, 1.0, 0.5)
with c3
print(.r)
print(.g)
print(.b)
print(.a)
end with
'/
Code: Select all
' cairoversion.bas
#include once "cairo/cairo.bi"
#include once "cairocolor.bi"
sub DrawMain(cr as cairo_t ptr)
' Repaint surface
cairo_set_source_rgb(cr, 1.0, 0.0, 1.0)
cairo_paint(cr)
' Select font
cairo_select_font_face(cr, "Palatino Linotype", 0, 0)
cairo_set_font_size(cr, 20)
end sub
sub DrawText(cr as cairo_t ptr, text as string, x as double, y as double, rect_color as tcairocolor, text_color as tcairocolor)
dim as cairo_text_extents_t te
cairo_text_extents(cr, text, @te)
dim as double lw = cairo_get_line_width(cr)
y -= te.y_bearing ' So that y be the top of rectangle
cairo_save(cr)
' Draw rectangle
cairo_rectangle(cr, x + te.x_bearing - lw, y + te.y_bearing - lw, te.width + 2 * lw, te.height + 2 * lw)
with rect_color
cairo_set_source_rgba(cr, .r, .g, .b, .a)
end with
cairo_fill(cr)
' Draw text
cairo_move_to(cr, x, y)
with text_color
cairo_set_source_rgb(cr, .r, .g, .b)
end with
cairo_show_text(cr, text)
cairo_restore(cr)
end sub
sub DrawCairoVersion(cr as cairo_t ptr)
dim as string version = "Cairo " & *cairo_version_string()
dim as tcairocolor rect_color = tcairocolor(1.0, 1.0, 1.0, 0.7)
dim as tcairocolor text_color = tcairocolor(0.0, 0.0, 1.0)
DrawText(cr, version, 8, 8, rect_color, text_color)
end sub
const WND_W = 480
const WND_H = 480
screenres WND_W, WND_H, 32
line (0, 0)-(WND_W, WND_H), rgb(192, 192, 192), BF
const IMG_W = 464
const IMG_H = 464
dim as any ptr image = imagecreate(IMG_W, IMG_H)
dim as any ptr pixels
imageinfo(image, IMG_W, IMG_H,,, pixels)
dim as long stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, IMG_W) ' https://www.freebasic.net/forum/viewtopic.php?p=215065#p215065
dim as cairo_surface_t ptr sf = cairo_image_surface_create_for_data(pixels, CAIRO_FORMAT_ARGB32, IMG_W, IMG_H, stride)
dim as cairo_t ptr cr = cairo_create(sf)
const DELAY = 100
'do
DrawMain(cr)
DrawCairoVersion(cr)
screenlock()
put (8, 8), image, PSET
screenunlock()
sleep(DELAY)
'loop until len(inkey)
cairo_destroy(cr)
cairo_surface_destroy(sf)
imagedestroy image
sleep