Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions samples/sample_font_extents.jl
Original file line number Diff line number Diff line change
@@ -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");
11 changes: 11 additions & 0 deletions src/Cairo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down Expand Up @@ -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))

Expand Down
Loading