diff --git a/doc/examples/clip-image.raku b/doc/examples/clip-image.raku index da9373e..e960dcd 100644 --- a/doc/examples/clip-image.raku +++ b/doc/examples/clip-image.raku @@ -1,12 +1,6 @@ use v6; use Cairo; -constant xc = 128.0; -constant yc = 128.0; -constant radius = 100.0; -constant angle1 = 45.0 * (pi/180.0); # angles are specified -constant angle2 = 180.0 * (pi/180.0); # in radians - given Cairo::Image.create(Cairo::FORMAT_ARGB32, 256, 256) { given Cairo::Context.new($_) { .arc(128.0, 128.0, 76.8, 0, 2*pi); diff --git a/lib/Cairo.pm6 b/lib/Cairo.pm6 index 0d2b1b0..898e26f 100644 --- a/lib/Cairo.pm6 +++ b/lib/Cairo.pm6 @@ -282,6 +282,13 @@ our class cairo_surface_t is repr('CPointer') { class cairo_pdf_surface_t is cairo_surface_t is repr('CPointer') { + our sub create(str $filename, num64 $width, num64 $height) + returns cairo_pdf_surface_t + is native($cairolib) + is symbol('cairo_pdf_surface_create') + {*} + + method add_outline(int32 $parent-id, Str $name, Str $link-attrs, int32 $flags --> int32) is native($cairolib) is symbol('cairo_pdf_surface_add_outline') @@ -291,6 +298,22 @@ class cairo_pdf_surface_t is cairo_surface_t is repr('CPointer') { is native($cairolib) is symbol('cairo_pdf_surface_set_metadata') {*} + + method new(Str:D :$filename!, Num:D() :$width!, Num:D() :$height! --> cairo_pdf_surface_t:D) { + create($filename, $width, $height); + } +} + +class cairo_svg_surface_t is cairo_surface_t is repr('CPointer') { + our sub create(str $filename, num64 $width, num64 $height) + returns cairo_svg_surface_t + is native($cairolib) + is symbol('cairo_svg_surface_create') + {*} + + method new(Str:D :$filename!, Num:D() :$width!, Num:D() :$height! --> cairo_svg_surface_t) { + create($filename, $width, $height); + } } our class cairo_rectangle_t is repr('CPointer') { } @@ -1231,10 +1254,10 @@ class Matrix { } class Surface { - has cairo_surface_t $.surface handles ; + method raw handles { self.surface } method write_png(Str $filename) { - my $result = CairoStatus( $!surface.write_to_png($filename) ); + my $result = CairoStatus( $.surface.write_to_png($filename) ); fail $result if $result != STATUS_SUCCESS; $result; } @@ -1243,7 +1266,7 @@ class Surface { my $buf = CArray[uint8].new; $buf[$size] = 0; my $closure = StreamClosure.new: :$buf, :buf-len(0), :n-read(0), :$size; - $!surface.write_to_png_stream(&StreamClosure::write, $closure); + $.surface.write_to_png_stream(&StreamClosure::write, $closure); return Blob[uint8].new: $buf[0 ..^ $closure.buf-len]; } @@ -1257,25 +1280,18 @@ class Surface { } class Surface::PDF is Surface { - sub cairo_pdf_surface_create(str $filename, num64 $width, num64 $height) - returns cairo_pdf_surface_t - is native($cairolib) - {*} - - has Num $.width; - has Num $.height; + has cairo_pdf_surface_t $.surface; + has Num:D() $.width is required; + has Num:D() $.height is required; + submethod TWEAK(Str:D() :$filename!) { + $!surface .= new: :$!width, :$!height, :$filename; + } multi method create(str $filename, num64 $width, num64 $height) { - return self.new( - surface => cairo_pdf_surface_create($filename, $width, $height), - :$width, :$height, - ) + return self.new( :$filename, :$width, :$height ); } multi method create(Str(Cool) $filename, Num(Cool) $width, Num(Cool) $height) { - return self.new( - surface => cairo_pdf_surface_create($filename, $width, $height), - :$width, :$height, - ) + return self.new( :$filename, :$width, :$height ); } method add_outline(Int :$parent-id, Str:D :$name = '', :$flags = 0, *%attrs) { @@ -1286,25 +1302,19 @@ class Surface::PDF is Surface { } class Surface::SVG is Surface { - sub cairo_svg_surface_create(str $filename, num64 $width, num64 $height) - returns cairo_surface_t - is native($cairolib) - {*} + has cairo_svg_surface_t $.surface; + has Num:D() $.width is required; + has Num:D() $.height is required; - has Num $.width; - has Num $.height; + submethod TWEAK(Str:D() :$filename!) { + $!surface .= new: :$!width, :$!height, :$filename; + } multi method create(str $filename, num64 $width, num64 $height) { - return self.new( - surface => cairo_svg_surface_create($filename, $width, $height), - :$width, :$height, - ) + return self.new(:$filename, :$width, :$height); } multi method create(Str(Cool) $filename, Num(Cool) $width, Num(Cool) $height) { - return self.new( - surface => cairo_svg_surface_create($filename, $width, $height), - :$width, :$height, - ) + return self.new(:$filename, :$width, :$height); } } @@ -1330,6 +1340,7 @@ class RecordingSurface { } class Image is Surface { + has cairo_surface_t $.surface; sub cairo_image_surface_create(int32 $format, int32 $width, int32 $height) returns cairo_surface_t is native($cairolib) @@ -1365,6 +1376,11 @@ class Image is Surface { is native($cairolib) {*} + multi submethod TWEAK(cairo_surface_t:D :surface($)!) {} + multi submethod TWEAK(Str:D :$filename!) { + $!surface //= cairo_image_surface_create_from_png($filename) + } + multi method create(Int() $format, Cool $width, Cool $height) { return self.new(surface => cairo_image_surface_create($format.Int, $width.Int, $height.Int)); } diff --git a/t/surface-image.t b/t/surface-image.t new file mode 100644 index 0000000..71536c7 --- /dev/null +++ b/t/surface-image.t @@ -0,0 +1,32 @@ +use v6; +use Cairo; +use Test; +plan 2; + +lives-ok { + given Cairo::Image.create(Cairo::FORMAT_ARGB32, 256, 256) { + given Cairo::Context.new($_) { + .arc(128.0, 128.0, 76.8, 0, 2*pi); + .clip; + .new_path; # path not consumed by .clip + + my \image = Cairo::Image.new(:filename ); + my \w = image.width; + my \h = image.height; + + .scale(256.0/w, 256.0/h); + + .set_source_surface(image, 0, 0); + .paint; + + image.destroy; + }; + .write_png("clip-image.png"); + } +}; + +ok "clip-image.png".IO.e, "png created from new"; + +unlink "clip-image.png"; # don't care if failed + +done-testing; diff --git a/t/surface-pdf.t b/t/surface-pdf.t index c6da9df..44cce85 100644 --- a/t/surface-pdf.t +++ b/t/surface-pdf.t @@ -2,7 +2,7 @@ use v6; use Cairo; use Test; -plan 4; +plan 6; lives-ok { given Cairo::Surface::PDF.create("foobar.pdf", 128, 128) { @@ -20,6 +20,13 @@ lives-ok { } }; +lives-ok { + my Cairo::Surface::PDF $pdf .= new: :filename, :width(128), :height(64); + $pdf.finish; +} +ok "foo2.pdf".IO.e, "pdf created from new"; + unlink "foobar.pdf"; # don't care if failed +unlink "foo2.pdf"; # don't care if failed done-testing; diff --git a/t/surface-svg.t b/t/surface-svg.t index 0abf1a6..cedb40c 100644 --- a/t/surface-svg.t +++ b/t/surface-svg.t @@ -2,7 +2,7 @@ use v6; use Cairo; use Test; -plan 4; +plan 6; lives-ok { given Cairo::Surface::SVG.create("foobar.svg", 128, 128) { @@ -19,6 +19,13 @@ lives-ok { } }; +lives-ok { + my Cairo::Surface::SVG $svg .= new: :filename, :width(128), :height(64); + $svg.finish; +} +ok "foo2.svg".IO.e, "svg created from new"; + unlink "foobar.svg"; # don't care if failed +unlink "foo2.svg"; # don't care if failed done-testing;