| 1 | use Renard::Incunabula::Common::Setup; | ||
| 2 | package Intertangle::Taffeta::Graphics::Image::PNG; | ||
| 3 | # ABSTRACT: Raster data stored in PNG format | ||
| 4 | $Intertangle::Taffeta::Graphics::Image::PNG::VERSION = '0.001'; | ||
| 5 | use Moo; | ||
| 6 | use Renard::Incunabula::Common::Types qw(Str InstanceOf Int); | ||
| 7 | use Intertangle::Taffeta::Types qw(SVG); | ||
| 8 | use MIME::Base64; | ||
| 9 | use Image::Size; | ||
| 10 | |||
| 11 | extends qw(Intertangle::Taffeta::Graphics::Image); | ||
| 12 | |||
| 13 | has data => ( | ||
| 14 | is => 'ro', | ||
| 15 | isa => Str, | ||
| 16 | required => 1, | ||
| 17 | ); | ||
| 18 | |||
| 19 | method _build_cairo_image_surface() :ReturnType(InstanceOf['Cairo::ImageSurface']) { | ||
| 20 | # read the PNG data in-memory | ||
| 21 | my $img = Cairo::ImageSurface->create_from_png_stream( | ||
| 22 | my $cb = fun ( (Str) $callback_data, (Int) $length ) { | ||
| 23 | state $offset = 0; | ||
| 24 | my $data = substr $callback_data, $offset, $length; | ||
| 25 | $offset += $length; | ||
| 26 | $data; | ||
| 27 | }, $self->data ); | ||
| 28 | |||
| 29 | return $img; | ||
| 30 | } | ||
| 31 | |||
| 32 | method _build_size() :ReturnType(InstanceOf['Intertangle::Yarn::Graphene::Size']) { | ||
| 33 | my ($width, $height, $id_or_error) = Image::Size::imgsize( \($self->data) ); | ||
| 34 | die "Could not compute bounds: $id_or_error" unless $id_or_error eq 'PNG'; | ||
| 35 | Intertangle::Yarn::Graphene::Size->new( | ||
| 36 | width => $width, | ||
| 37 | height => $height, | ||
| 38 | ); | ||
| 39 | } | ||
| 40 | |||
| 41 | method render_svg( (SVG) $svg ) { | ||
| 42 | $svg->image( | ||
| 43 | x => $self->origin->x, | ||
| 44 | y => $self->origin->y, | ||
| 45 | '-href' => "data:image/png;base64,@{[ encode_base64( $self->data ) ]}", | ||
| 46 | $self->svg_transform_parameter, | ||
| 47 | ); | ||
| 48 | } | ||
| 49 | |||
| 50 | with qw( | ||
| 51 | Intertangle::Taffeta::Graphics::Image::Role::FromCairoImageSurface | ||
| 52 | Intertangle::Taffeta::Graphics::Role::SVGRenderable | ||
| 53 | Intertangle::Taffeta::Graphics::Role::WithBounds | ||
| 54 | Intertangle::Taffeta::Graphics::Role::WithTransform | ||
| 55 | Intertangle::Taffeta::Graphics::Role::SVGRenderable::Transform | ||
| 56 | ); | ||
| 57 | |||
| 58 | 1; | ||
| 59 | |||
| 60 | __END__ |