diff --git a/samples/Samples.md b/samples/Samples.md index 930aa28..abc2880 100644 --- a/samples/Samples.md +++ b/samples/Samples.md @@ -188,6 +188,11 @@ Example of using Recording surface, simple copy, then scaled and offset. ![sample record0.png](sample_record0.png "record0 example") ![sample record1.png](sample_record1.png "record1 example") +Example of using Recording surface, find the `ink_extents` of drawing operations, +draw an encompassing rectangle, scale and offset. + +![sample record inkextents.png](sample_record_inkextents.png "record inkextents example") + Example of writing to script, put script text into frame. ![sample script0.png](sample_script0.png "scrip0 example") diff --git a/samples/sample_record_inkextents.jl b/samples/sample_record_inkextents.jl new file mode 100644 index 0000000..b41851f --- /dev/null +++ b/samples/sample_record_inkextents.jl @@ -0,0 +1,53 @@ +## header to provide surface and context +using Cairo +c = CairoARGBSurface(256,256); +cr = CairoContext(c); + +save(cr); +set_source_rgba(cr,0.0,0.0,0.0,0.0); # transparent black +rectangle(cr,0.0,0.0,256.0,256.0); # background +fill(cr); +restore(cr); +save(cr); + +## paint the following to a Recording Surface +s1 = CairoRecordingSurface() +c1 = CairoContext(s1) + +move_to(c1, 128.0, 25.6); +line_to(c1, 230.4, 230.4); +rel_line_to(c1, -102.4, 0.0); +curve_to(c1, 51.2, 230.4, 51.2, 128.0, 128.0, 128.0); +close_path(c1); + +move_to(c1, 64.0, 25.6); +rel_line_to(c1, 51.2, 51.2); +rel_line_to(c1, -51.2, 51.2); +rel_line_to(c1, -51.2, -51.2); +close_path(c1); + +set_line_width(c1, 10.0); +set_source_rgb(c1, 0, 0, 1); +fill_preserve(c1); +set_source_rgb(c1, 0, 0, 0); +stroke(c1); + +## find ink extents and mark with a rectangle +x0, y0, width, height = recording_surface_ink_extents(s1); +rectangle(c1, x0, y0, width, height) +stroke(c1); + +## play back on transform coord +scale(cr,0.5,0.5) +set_source(cr, s1, 128.0, 128.0) + +paint(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_record_inkextents.png"); + diff --git a/samples/sample_record_inkextents.png b/samples/sample_record_inkextents.png new file mode 100644 index 0000000..7e7ff9e Binary files /dev/null and b/samples/sample_record_inkextents.png differ diff --git a/src/Cairo.jl b/src/Cairo.jl index a269c81..9b93150 100644 --- a/src/Cairo.jl +++ b/src/Cairo.jl @@ -72,7 +72,7 @@ export set_source_rgb, set_source_rgba, set_source_surface, set_line_type, set_line_cap, set_line_join, set_operator, get_operator, set_source, - CairoMatrix, + CairoMatrix, recording_surface_ink_extents, # coordinate systems reset_transform, rotate, scale, translate, user_to_device!, @@ -563,6 +563,16 @@ function script_from_recording_surface(s::CairoScript,r::CairoSurface) ccall((:cairo_script_from_recording_surface,libcairo), Int32, (Ptr{Nothing},Ptr{Nothing}),s.ptr, r.ptr) end +function recording_surface_ink_extents(r::CairoSurface) + x0 = Ref{Cdouble}(0) + y0 = Ref{Cdouble}(0) + width = Ref{Cdouble}(0) + height = Ref{Cdouble}(0) + ccall((:cairo_recording_surface_ink_extents,libcairo), Nothing, + (Ptr{Nothing}, Ref{Cdouble}, Ref{Cdouble}, Ref{Cdouble}, Ref{Cdouble}), + r.ptr, x0, y0, width, height) + x0[], y0[], width[], height[] +end # ----------------------------------------------------------------------------- mutable struct CairoContext <: GraphicsContext