|
1 |
| -use HTML::Canvas::Graphic; |
| 1 | +unit class HTML::Canvas::Image; |
2 | 2 |
|
3 |
| -class HTML::Canvas::Image |
4 |
| - does HTML::Canvas::Graphic { |
| 3 | +use HTML::Canvas::Graphic; |
| 4 | +also does HTML::Canvas::Graphic; |
5 | 5 |
|
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; |
12 | 12 |
|
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"; |
22 | 21 | }
|
| 22 | +} |
23 | 23 |
|
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; |
30 | 30 |
|
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 | +} |
36 | 36 |
|
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 | +} |
40 | 40 |
|
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 | +} |
46 | 46 |
|
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>); } |
53 | 52 | }
|
| 53 | +} |
54 | 54 |
|
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); } |
61 | 60 | }
|
| 61 | +} |
62 | 62 |
|
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 | + ) |
77 | 76 | }
|
0 commit comments