Skip to content

Commit

Permalink
support Cairo::Surface::PDF.new(:$filename, :$width, :$height); #37
Browse files Browse the repository at this point in the history
  • Loading branch information
dwarring committed Jun 10, 2024
1 parent 240e75a commit b3241bd
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 40 deletions.
6 changes: 0 additions & 6 deletions doc/examples/clip-image.raku
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
80 changes: 48 additions & 32 deletions lib/Cairo.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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') { }
Expand Down Expand Up @@ -1231,10 +1254,10 @@ class Matrix {
}

class Surface {
has cairo_surface_t $.surface handles <reference destroy flush finish show_page status>;
method raw handles <reference destroy flush finish show_page status> { 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;
}
Expand All @@ -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];
}

Expand All @@ -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) {
Expand All @@ -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);
}
}

Expand All @@ -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)
Expand Down Expand Up @@ -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));
}
Expand Down
32 changes: 32 additions & 0 deletions t/surface-image.t
Original file line number Diff line number Diff line change
@@ -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<doc/examples/camelia.png> );
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;
9 changes: 8 additions & 1 deletion t/surface-pdf.t
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -20,6 +20,13 @@ lives-ok {
}
};

lives-ok {
my Cairo::Surface::PDF $pdf .= new: :filename<foo2.pdf>, :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;
9 changes: 8 additions & 1 deletion t/surface-svg.t
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -19,6 +19,13 @@ lives-ok {
}
};

lives-ok {
my Cairo::Surface::SVG $svg .= new: :filename<foo2.svg>, :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;

0 comments on commit b3241bd

Please sign in to comment.