Skip to content

Commit 2f4acd1

Browse files
committed
Standardize on unit classes
1 parent 16e9992 commit 2f4acd1

File tree

6 files changed

+553
-563
lines changed

6 files changed

+553
-563
lines changed

lib/HTML/Canvas/Gradient.rakumod

Lines changed: 47 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,56 @@
1-
use v6;
1+
unit class HTML::Canvas::Gradient;
22

3-
class HTML::Canvas::Gradient {
3+
use JSON::Fast;
44

5-
use JSON::Fast;
5+
has Numeric $.x0;
6+
has Numeric $.y0;
7+
has Numeric $.x1;
8+
has Numeric $.y1;
9+
has Numeric $.r0;
10+
has Numeric $.r1;
11+
method type { with $!r0 // $!r1 { 'Radial' } else { 'Linear' } }
612

7-
has Numeric $.x0;
8-
has Numeric $.y0;
9-
has Numeric $.x1;
10-
has Numeric $.y1;
11-
has Numeric $.r0;
12-
has Numeric $.r1;
13-
method type { with $!r0 // $!r1 { 'Radial' } else { 'Linear' } }
14-
15-
my class ColorStop {
16-
use CSS::Properties :&to-ast;
17-
use Color;
18-
has Numeric $.offset;
19-
has Color $!color;
20-
method color is rw { $!color }
21-
has CSS::Properties $!css;
22-
has $!css-writer;
23-
subset ColorOrStr where Color|Str;
24-
submethod TWEAK(ColorOrStr:D :$color!) {
25-
$!color = do given $color {
26-
when Str { self!css.color = $_; }
27-
default { $_ }
28-
}
29-
}
30-
method !css { $!css //= CSS::Properties.new; }
31-
method !css-writer { $!css-writer //= (require CSS::Writer).new: :color-names }
32-
method !css-color-str(Color $_) {
33-
self!css-writer.write: |to-ast($_);
13+
my class ColorStop {
14+
use CSS::Properties :&to-ast;
15+
use Color;
16+
has Numeric $.offset;
17+
has Color $!color;
18+
method color is rw { $!color }
19+
has CSS::Properties $!css;
20+
has $!css-writer;
21+
subset ColorOrStr where Color|Str;
22+
submethod TWEAK(ColorOrStr:D :$color!) {
23+
$!color = do given $color {
24+
when Str { self!css.color = $_; }
25+
default { $_ }
3426
}
35-
36-
method to-js(Str $var) {
37-
'%s.addColorStop(%s, %s);'.sprintf($var, to-json($.offset), to-json( self!css-color-str($.color))) }
3827
}
39-
has ColorStop @.colorStops;
40-
41-
method addColorStop(Numeric $offset, Str $color) {
42-
@!colorStops.push: ColorStop.new( :$offset, :$color );
28+
method !css { $!css //= CSS::Properties.new; }
29+
method !css-writer { $!css-writer //= (require CSS::Writer).new: :color-names }
30+
method !css-color-str(Color $_) {
31+
self!css-writer.write: |to-ast($_);
4332
}
4433

45-
method to-js(Str $ctx, Str $var = 'grad' --> Array) {
46-
my @args = do with $!r0 // $!r1 {
47-
$!x0, $!y0, ($!r0 // 0), $!x1, $!y1, ($!r1 // 0);
48-
}
49-
else {
50-
$!x0, $!y0, $!x1, $!y1;
51-
}
52-
my $args-js = @args.map({ to-json($_) }).join: ", ";
53-
my @js = 'var %s = %s.create%sGradient(%s);'.sprintf($var, $ctx, self.type, $args-js);
54-
@js.push: .to-js($var)
55-
for @!colorStops;
56-
@js;
34+
method to-js(Str $var) {
35+
'%s.addColorStop(%s, %s);'.sprintf($var, to-json($.offset), to-json( self!css-color-str($.color))) }
36+
}
37+
has ColorStop @.colorStops;
38+
39+
method addColorStop(Numeric $offset, Str $color) {
40+
@!colorStops.push: ColorStop.new( :$offset, :$color );
41+
}
42+
43+
method to-js(Str $ctx, Str $var = 'grad' --> Array) {
44+
my @args = do with $!r0 // $!r1 {
45+
$!x0, $!y0, ($!r0 // 0), $!x1, $!y1, ($!r1 // 0);
46+
}
47+
else {
48+
$!x0, $!y0, $!x1, $!y1;
5749
}
50+
my $args-js = @args.map({ to-json($_) }).join: ", ";
51+
my @js = 'var %s = %s.create%sGradient(%s);'.sprintf($var, $ctx, self.type, $args-js);
52+
@js.push: .to-js($var)
53+
for @!colorStops;
54+
@js;
5855
}
56+

lib/HTML/Canvas/Image.rakumod

Lines changed: 62 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,76 @@
1-
use HTML::Canvas::Graphic;
1+
unit class HTML::Canvas::Image;
22

3-
class HTML::Canvas::Image
4-
does HTML::Canvas::Graphic {
3+
use HTML::Canvas::Graphic;
4+
also does HTML::Canvas::Graphic;
55

6-
use Base64::Native;
7-
my subset DataURI of Str where /^('data:' [<t=.ident> '/' <s=.ident>]? $<b64>=";base64"? $<start>=",") /;
8-
has DataURI $.data-uri;
9-
my subset Source where Blob|Str|IO::Handle;
10-
has Source:D $.source is required;
11-
has Str $.image-type is required;
6+
use Base64::Native;
7+
my subset DataURI of Str where /^('data:' [<t=.ident> '/' <s=.ident>]? $<b64>=";base64"? $<start>=",") /;
8+
has DataURI $.data-uri;
9+
my subset Source where Blob|Str|IO::Handle;
10+
has Source:D $.source is required;
11+
has Str $.image-type is required;
1212

13-
method !image-type($_, :$path!) {
14-
when m:i/^ jpe?g $/ { 'JPEG' }
15-
when m:i/^ gif $/ { 'GIF' }
16-
when m:i/^ png $/ { 'PNG' }
17-
when m:i/^ svg $/ { 'SVG' }
18-
when m:i/^ bmp $/ { 'BMP' }
19-
default {
20-
fail "unknown image type: $path";
21-
}
13+
method !image-type($_, :$path!) {
14+
when m:i/^ jpe?g $/ { 'JPEG' }
15+
when m:i/^ gif $/ { 'GIF' }
16+
when m:i/^ png $/ { 'PNG' }
17+
when m:i/^ svg $/ { 'SVG' }
18+
when m:i/^ bmp $/ { 'BMP' }
19+
default {
20+
fail "unknown image type: $path";
2221
}
22+
}
2323

24-
multi method open(DataURI $data-uri) {
25-
my $path = ~ $0;
26-
my Str \mime-type = ( $0<t> // '(missing)').lc;
27-
my Str \mime-subtype = ( $0<s> // '').lc;
28-
my Bool \base64 = ? $0<b64>;
29-
my Numeric \start = $0<start>.to;
24+
multi method open(DataURI $data-uri) {
25+
my $path = ~ $0;
26+
my Str \mime-type = ( $0<t> // '(missing)').lc;
27+
my Str \mime-subtype = ( $0<s> // '').lc;
28+
my Bool \base64 = ? $0<b64>;
29+
my Numeric \start = $0<start>.to;
3030

31-
die "expected mime-type 'image/*', got '{mime-type}': $path"
32-
unless mime-type eq 'image';
33-
my $image-type = self!image-type(mime-subtype, :$path);
34-
self.new(:$image-type, :$data-uri);
35-
}
31+
die "expected mime-type 'image/*', got '{mime-type}': $path"
32+
unless mime-type eq 'image';
33+
my $image-type = self!image-type(mime-subtype, :$path);
34+
self.new(:$image-type, :$data-uri);
35+
}
3636

37-
multi method open(IO() $io-path) {
38-
self.open( $io-path.open( :r, :bin) );
39-
}
37+
multi method open(IO() $io-path) {
38+
self.open( $io-path.open( :r, :bin) );
39+
}
4040

41-
multi method open(IO::Handle $source!) {
42-
my $path = $source.path;
43-
my Str $image-type = self!image-type($path.extension, :$path);
44-
self.new( :$source, :$image-type,);
45-
}
41+
multi method open(IO::Handle $source!) {
42+
my $path = $source.path;
43+
my Str $image-type = self!image-type($path.extension, :$path);
44+
self.new( :$source, :$image-type,);
45+
}
4646

47-
method Str returns Str {
48-
given $!source {
49-
when Str { .substr(0); }
50-
when Blob { .decode("latin-1"); }
51-
default { .path.IO.slurp(:enc<latin-1>); }
52-
}
47+
method Str returns Str {
48+
given $!source {
49+
when Str { .substr(0); }
50+
when Blob { .decode("latin-1"); }
51+
default { .path.IO.slurp(:enc<latin-1>); }
5352
}
53+
}
5454

55-
method Blob returns Blob {
56-
given $!source {
57-
when Blob { $_; }
58-
when Str { .encode("latin-1"); }
59-
default { .path.IO.slurp(:bin); }
60-
}
55+
method Blob returns Blob {
56+
given $!source {
57+
when Blob { $_; }
58+
when Str { .encode("latin-1"); }
59+
default { .path.IO.slurp(:bin); }
6160
}
61+
}
6262

63-
method data-uri is rw {
64-
Proxy.new(
65-
FETCH => sub ($) {
66-
$!data-uri //= do with $.Blob {
67-
my Str $enc = base64-encode($_, :str);
68-
'data:image/%s;base64,%s'.sprintf($.image-type.lc, $enc);
69-
}
70-
else {
71-
fail 'image is not associated with a source';
72-
}
73-
},
74-
STORE => sub ($, $!data-uri) {},
75-
)
76-
}
63+
method data-uri is rw {
64+
Proxy.new(
65+
FETCH => sub ($) {
66+
$!data-uri //= do with $.Blob {
67+
my Str $enc = base64-encode($_, :str);
68+
'data:image/%s;base64,%s'.sprintf($.image-type.lc, $enc);
69+
}
70+
else {
71+
fail 'image is not associated with a source';
72+
}
73+
},
74+
STORE => sub ($, $!data-uri) {},
75+
)
7776
}

lib/HTML/Canvas/ImageData.rakumod

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
class HTML::Canvas::ImageData {
1+
unit class HTML::Canvas::ImageData;
22

3-
use HTML::Canvas::Graphic;
4-
also does HTML::Canvas::Graphic;
3+
use HTML::Canvas::Graphic;
4+
also does HTML::Canvas::Graphic;
55

6-
use Cairo;
7-
use JSON::Fast;
6+
use Cairo;
7+
use JSON::Fast;
88

9-
has Cairo::Image $.image;
10-
has Numeric ($.sx, $.sy, $.sw, $.sh);
9+
has Cairo::Image $.image;
10+
has Numeric ($.sx, $.sy, $.sw, $.sh);
1111

12-
method to-js(Str $ctx, --> Array) {
13-
my @js = '%s.getImageData(%s, %s, %s, %s)'.sprintf($ctx, |($!sx, $!sy, $!sw, $!sh).map: { to-json($_) });
14-
@js;
15-
}
16-
}
12+
method to-js(Str $ctx, --> Array) {
13+
my @js = '%s.getImageData(%s, %s, %s, %s)'.sprintf($ctx, |($!sx, $!sy, $!sw, $!sh).map: { to-json($_) });
14+
@js;
15+
}

lib/HTML/Canvas/Pattern.rakumod

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
use v6;
2-
class HTML::Canvas::Pattern {
3-
use HTML::Canvas::Image;
4-
use JSON::Fast;
5-
subset Repetition of Str where 'repeat'|'repeat-x'|'repeat-y'|'no-repeat';
6-
has Repetition $.repetition = 'repeat';
7-
has HTML::Canvas::Image $.image;
1+
unit class HTML::Canvas::Pattern;
82

9-
method to-js(Str $ctx, :$sym = my %{Any} --> Array) {
10-
my $image-js = $sym{$!image} // $!image.js-ref;
11-
my @js = '%s.createPattern(%s, %s)'.sprintf($ctx, $image-js, to-json($!repetition));
12-
@js;
13-
}
14-
}
3+
use HTML::Canvas::Image;
4+
use JSON::Fast;
5+
subset Repetition of Str where 'repeat'|'repeat-x'|'repeat-y'|'no-repeat';
6+
has Repetition $.repetition = 'repeat';
7+
has HTML::Canvas::Image $.image;
8+
9+
method to-js(Str $ctx, :$sym = my %{Any} --> Array) {
10+
my $image-js = $sym{$!image} // $!image.js-ref;
11+
my @js = '%s.createPattern(%s, %s)'.sprintf($ctx, $image-js, to-json($!repetition));
12+
@js;
13+
}

0 commit comments

Comments
 (0)