From a84dbdd5787d66af5e034f8c013577a0f45d39bd Mon Sep 17 00:00:00 2001 From: Paul Melis Date: Sat, 28 Sep 2024 15:42:32 +0200 Subject: [PATCH] Add font_extents() --- samples/sample_font_extents.jl | 52 ++++++++++++++++++++++++++++++++++ src/Cairo.jl | 11 +++++++ 2 files changed, 63 insertions(+) create mode 100644 samples/sample_font_extents.jl diff --git a/samples/sample_font_extents.jl b/samples/sample_font_extents.jl new file mode 100644 index 0000000..21c97ab --- /dev/null +++ b/samples/sample_font_extents.jl @@ -0,0 +1,52 @@ +## header to provide surface and context +using Cairo +c = CairoRGBSurface(1024,256); +cr = CairoContext(c); + +save(cr); +set_source_rgb(cr,0.8,0.8,0.8); # light gray +rectangle(cr,0.0,0.0,1024.0,256.0); # background +fill(cr); +restore(cr); + +save(cr); +## original example, following here +select_font_face(cr, "Sans", Cairo.FONT_SLANT_NORMAL, + Cairo.FONT_WEIGHT_NORMAL); +set_font_size(cr, 100.0); +extents = font_extents(cr); + +#typedef struct { +# double ascent; +# double descent; +# double height; +# double max_x_advance; +# double max_y_advance; +#} cairo_font_extents_t; + +x = 25.0; +y = 150.0; + +move_to(cr, x, y); +show_text(cr, "Cairo! abcdefghijklmnopqrstuvwxyz"); + +# draw helping lines +set_source_rgba(cr, 1, 0.2, 0.2, 0.6); +set_line_width(cr, 6.0); +arc(cr, x, y, 10.0, 0, 2*pi); +fill(cr); +move_to(cr, x,y); +rel_line_to(cr, 0, -extents[1]); +rel_line_to(cr, 1024, 0); +#rel_line_to(cr, extents[1], -extents[2]); +stroke(cr); +move_to(cr, x,y); +rel_line_to(cr, 0, extents[2]); +rel_line_to(cr, 1024, 0); +stroke(cr); +## mark picture with current date +restore(cr); +move_to(cr,0.0,12.0); +set_source_rgb(cr, 0,0,0); +show_text(cr,Libc.strftime(time())); +write_to_png(c,"sample_font_extents.png"); diff --git a/src/Cairo.jl b/src/Cairo.jl index 541c14b..eb0925e 100644 --- a/src/Cairo.jl +++ b/src/Cairo.jl @@ -102,6 +102,7 @@ export set_text, set_latex, set_font_face, set_font_size, select_font_face, textwidth, textheight, text_extents, + font_extents, TeXLexer, tex2pango, show_text, text_path, set_font_matrix, get_font_matrix, @@ -1215,6 +1216,16 @@ function show_layout(ctx::CairoContext) (Ptr{Nothing},Ptr{Nothing}), ctx.ptr, ctx.layout) end +font_extents(ctx::CairoContext) = + font_extents!(ctx, Matrix{Float64}(undef, 5, 1)) + +function font_extents!(ctx::CairoContext,extents) + ccall((:cairo_font_extents, libcairo), + Nothing, (Ptr{Nothing}, Ptr{Float64}), + ctx.ptr, extents) + extents +end + text_extents(ctx::CairoContext,value::AbstractString) = text_extents!(ctx,value, Matrix{Float64}(undef, 6, 1))