Skip to content

Commit

Permalink
refactor recently added matrix classes
Browse files Browse the repository at this point in the history
  • Loading branch information
dwarring committed Jun 15, 2017
1 parent f8510a0 commit 5179700
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 53 deletions.
2 changes: 1 addition & 1 deletion META6.json
Expand Up @@ -2,7 +2,7 @@
"perl" : "v6",
"name" : "Cairo",
"description" : "Cairo 2D graphics library",
"version" : "v0.2",
"version" : "0.2.1",
"depends" : [ ],
"source-url" : "git://github.com/timo/cairo-p6.git",
"tags" : [ "png", "pdf", "svg" ],
Expand Down
2 changes: 1 addition & 1 deletion examples/image-pattern.pl
Expand Up @@ -16,7 +16,7 @@
.scale(1 / sqrt(2), 1 / sqrt(2));
.translate(-128.0, -128.0);

my Cairo::cairo_matrix_t $matrix .= new.init(:scale, w/256.0 * 5.0, h/256.0 * 5.0);
my Cairo::Matrix $matrix .= new.init_scale(w/256.0 * 5.0, h/256.0 * 5.0);
$pattern.matrix = $matrix;
.pattern($pattern);

Expand Down
91 changes: 50 additions & 41 deletions lib/Cairo.pm6
Expand Up @@ -72,54 +72,29 @@ our class cairo_matrix_t is repr('CStruct') {
has num64 $.xy; has num64 $.yy;
has num64 $.x0; has num64 $.y0;

multi method new(Num(Cool) :$xx = 1e0, Num(Cool) :$yx = 0e0, Num(Cool) :$xy = 0e0, Num(Cool) :$yy = 1e0, Num(Cool) :$x0 = 0e0, Num(Cool) :$y0 = 0e0) {
self.bless( :$xx, :$yx, :$xy, :$yy, :$x0, :$y0 );
}

multi method new(Num(Cool) $xx = 1e0, Num(Cool) $yx = 0e0,
Num(Cool) $xy = 0e0, Num(Cool) $yy = 1e0,
Num(Cool) $x0 = 0e0, Num(Cool) $y0 = 0e0) {
self.bless( :$xx, :$yx, :$xy, :$yy, :$x0, :$y0 );
}

sub cairo_matrix_init_identity(cairo_matrix_t $matrix)
method init_identity
is native($cairolib)
is symbol('cairo_matrix_init_identity')
{*}

sub cairo_matrix_init_scale(cairo_matrix_t $matrix, num64 $sx, num64 $sy)
method init_scale(num64 $sx, num64 $sy)
is native($cairolib)
is symbol('cairo_matrix_init_scale')
{*}

sub cairo_matrix_init_translate(cairo_matrix_t $matrix, num64 $tx, num64 $ty)
method init_translate(num64 $tx, num64 $ty)
is native($cairolib)
is symbol('cairo_matrix_init_translate')
{*}

sub cairo_matrix_init(cairo_matrix_t $matrix, num64 $xx, num64 $yx, num64 $xy, num64 $yy, num64 $x0, num64 $y0)
method init(num64 $xx, num64 $yx, num64 $xy, num64 $yy, num64 $x0, num64 $y0)
is native($cairolib)
is symbol('cairo_matrix_init')
{*}

multi method init(:$identity! where .so) {
cairo_matrix_init_identity(self);
return self;
}

multi method init(Num(Cool) $sx, Num(Cool) $sy, :$scale! where .so) {
cairo_matrix_init_scale(self, $sx, $sy);
return self;
}

multi method init(Num(Cool) $sx, Num(Cool) $sy, :$translate! where .so) {
cairo_matrix_init_translate(self, $sx, $sy);
return self;
}

multi method init(Num(Cool) $xx, Num(Cool) $yx,
Num(Cool) $xy, Num(Cool) $yy,
Num(Cool) $x0, Num(Cool) $y0) is default {
cairo_matrix_init( self, $xx, $yx, $xy, $yy, $x0, $y0 );
}
}

our class Matrix { ... }
our class Surface { ... }
our class Image { ... }
our class Pattern { ... }
Expand Down Expand Up @@ -271,8 +246,42 @@ sub cairo_format_stride_for_width(int32 $format, int32 $width)
is native($cairolib)
{*}

class Matrix {
has cairo_matrix_t $.matrix handles <
xx yx xy yy x0 y0
> .= new: :xx(1e0), :yy(1e0);

method init_identity {
$!matrix.init_identity;
self;
}

method init_scale(Num(Cool) $sx, Num(Cool) $sy) {
$!matrix.init_scale($sx, $sy);
self;
}

method init_translate(Num(Cool) $sx, Num(Cool) $sy, :$translate! where .so) {
$!matrix.init_translate($sx, $sy);
self;
}

multi method init(Num(Cool) :$xx = 1e0, Num(Cool) :$yx = 0e0, Num(Cool) :$xy = 0e0, Num(Cool) :$yy = 1e0, Num(Cool) :$x0 = 0e0, Num(Cool) :$y0 = 0e0) {
$!matrix.init( $xx, $yx, $xy, $yy, $x0, $y0 );
self;
}

multi method init(Num(Cool) $xx, Num(Cool) $yx = 0e0,
Num(Cool) $xy = 0e0, Num(Cool) $yy = 1e0,
Num(Cool) $x0 = 0e0, Num(Cool) $y0 = 0e0) {
$!matrix.init( $xx, $yx, $xy, $yy, $x0, $y0 );
self;
}

}

class Surface {
has $.surface handles <reference destroy finish show_page>;
has cairo_surface_t $.surface handles <reference destroy finish show_page>;

method write_png(Str $filename) {
my $result = $!surface.write_to_png($filename);
Expand Down Expand Up @@ -509,9 +518,9 @@ class Pattern {
FETCH => {
my cairo_matrix_t $matrix .= new;
cairo_pattern_get_matrix($!pattern, $matrix);
$matrix;
Cairo::Matrix.new: :$matrix;
},
STORE => -> \c, cairo_matrix_t \matrix { cairo_pattern_set_matrix($!pattern, matrix) }
STORE => -> \c, Cairo::Matrix \matrix { cairo_pattern_set_matrix($!pattern, matrix.matrix) }
}

method destroy() {
Expand Down Expand Up @@ -1051,8 +1060,8 @@ class Context {
cairo_rotate($!context, $angle.Num)
}

method transform(cairo_matrix_t $matrix) {
cairo_transform($!context, $matrix)
method transform(Matrix $matrix) {
cairo_transform($!context, $matrix.matrix)
}

multi method select_font_face(str $family, int32 $slant, int32 $weight) {
Expand Down Expand Up @@ -1151,9 +1160,9 @@ class Context {
FETCH => {
my cairo_matrix_t $matrix .= new;
cairo_get_matrix($!context, $matrix);
$matrix;
Matrix.new: :$matrix;
},
STORE => -> \c, cairo_matrix_t \matrix { cairo_set_matrix($!context, matrix) }
STORE => -> \c, Matrix \matrix { cairo_set_matrix($!context, matrix.matrix) }
}
}

Expand Down
20 changes: 10 additions & 10 deletions t/matrix.t
Expand Up @@ -4,31 +4,31 @@ use Test;

plan 9;

constant Matrix = Cairo::Matrix;
constant matrix_t = Cairo::cairo_matrix_t;

given Cairo::Image.create(Cairo::FORMAT_ARGB32, 128, 128) {
given Cairo::Context.new($_) {

my matrix_t $identity-matrix .= new( :xx(1), :yy(1), );
is-deeply .matrix, $identity-matrix, 'initial';
my Matrix $identity-matrix .= new.init_identity;
is-deeply .matrix, Matrix.new.init(:xx(1e0), :yx(0e0), :xy(0e0), :yy(1e0), :x0(0e0), :y0(0e0)), 'identity';

.translate(10,20);
is-deeply .matrix, matrix_t.new( :xx(1), :yy(1), :x0(10), :y0(20) ), 'translate';

is-deeply .matrix, Matrix.new.init( :xx(1e0), :yy(1e0), :x0(10e0), :y0(20e0) ), 'translate';
.save; {
.scale(2, 3);
is-deeply .matrix, matrix_t.new( :xx(2), :yy(3), :x0(10), :y0(20) ), 'translate + scale';
is-deeply .matrix, Matrix.new.init( :xx(2e0), :yy(3e0), :x0(10e0), :y0(20e0) ), 'translate + scale';

# http://zetcode.com/gfx/cairo/transformations/
my matrix_t $transform-matrix .= new( :xx(1), :yx(0.5),
:xy(0), :yy(1),
:x0(0), :y0(0) );
my Matrix $transform-matrix .= new.init( :xx(1), :yx(0.5),
:xy(0), :yy(1),
:x0(0), :y0(0) );
.transform( $transform-matrix);
is-deeply .matrix, matrix_t.new( :xx(2), :yx(1.5), :yy(3), :x0(10), :y0(20) ), 'transform';
is-deeply .matrix, Matrix.new.init( :xx(2e0), :yx(1.5e0), :yy(3e0), :x0(10e0), :y0(20e0) ), 'transform';
};
.restore;

is-deeply .matrix, matrix_t.new( :xx(1), :yy(1), :x0(10), :y0(20) ), 'save/restore';
is-deeply .matrix, Matrix.new.init( :xx(1e0), :yy(1e0), :x0(10e0), :y0(20e0) ), 'save/restore';

.identity_matrix;
is-deeply .matrix, $identity-matrix, 'identity';
Expand Down

0 comments on commit 5179700

Please sign in to comment.