### ----------------------------------------------------
### -- Gnome::Cairo
### -- Licenses: Artistic-2.0
### -- Authors: Marcel Timmerman
### -- File: lib/Gnome/Cairo/Surface.rakumod
### ----------------------------------------------------
### -- Chunk 2 of 2
 function with same mime type and C<Any> for I<data>.  The attached image (or filename) data can later be used by backends which support it (currently: PDF, PS, SVG and Win32 Printing surfaces) to emit this data instead of making a snapshot of the I<surface>.  This approach tends to be faster and requires less memory and disk space.  The recognized MIME types are the following: C<CAIRO_MIME_TYPE_JPEG>, C<CAIRO_MIME_TYPE_PNG>, C<CAIRO_MIME_TYPE_JP2>, C<CAIRO_MIME_TYPE_URI>, C<CAIRO_MIME_TYPE_UNIQUE_ID>, C<CAIRO_MIME_TYPE_JBIG2>, C<CAIRO_MIME_TYPE_JBIG2_GLOBAL>, C<CAIRO_MIME_TYPE_JBIG2_GLOBAL_ID>, C<CAIRO_MIME_TYPE_CCITT_FAX>, C<CAIRO_MIME_TYPE_CCITT_FAX_PARAMS>.  See corresponding backend surface docs for details about which MIME types it can handle. Caution: the associated MIME data will be discarded if you draw on the surface afterwards. Use this function with care.  Even if a backend supports a MIME type, that does not mean cairo will always be able to use the attached MIME data. For example, if the backend does not natively support the compositing operation used to apply the MIME data to the backend. In that case, the MIME data will be ignored. Therefore, to apply an image in all cases, it is best to create an image surface which contains the decoded image data and then attach the MIME data to that. This ensures the image will always be used while still allowing the MIME data to be used whenever possible.  Return value: C<CAIRO_STATUS_SUCCESS> or C<CAIRO_STATUS_NO_MEMORY> if a slot could not be allocated for the user data.

  method set-mime-data ( unsigned Int-ptr $data, UInt $length, cairo_destroy_func_t $destroy, void-ptr $closure --> Int )

=item Int $mime_type; a B<cairo_surface_t>
=item unsigned Int-ptr $data; the MIME type of the image data
=item UInt $length; the image data to attach to the surface
=item cairo_destroy_func_t $destroy; the length of the image data
=item void-ptr $closure; a B<cairo_destroy_func_t> which will be called when the surface is destroyed or when new image data is attached using the same mime type.
=end pod

method set-mime-data ( unsigned Int-ptr $data, UInt $length, cairo_destroy_func_t $destroy, void-ptr $closure --> Int ) {

  cairo_surface_set_mime_data(
    self._get-native-object-no-reffing, my gint $mime_type, $data, $length, $destroy, $closure
  )
}

sub cairo_surface_set_mime_data (
  cairo_surface_t $surface, gchar-ptr $mime_type, unsigned gchar-ptr $data, int64 $length, cairo_destroy_func_t $destroy, void-ptr $closure --> gint32
) is native(&cairo-lib)
  { * }
}}
#`{{
#-------------------------------------------------------------------------------
#TM:0:set-user-data:
=begin pod
=head2 set-user-data

Attach user data to I<surface>.  To remove user data from a surface, call this function with the key that was used to set it and C<Any> for I<data>.  Return value: C<CAIRO_STATUS_SUCCESS> or C<CAIRO_STATUS_NO_MEMORY> if a slot could not be allocated for the user data.

  method set-user-data ( cairo_user_data_key_t $key, void-ptr $user_data, cairo_destroy_func_t $destroy --> Int )

=item cairo_user_data_key_t $key; a B<cairo_surface_t>
=item void-ptr $user_data; the address of a B<cairo_user_data_key_t> to attach the user data to
=item cairo_destroy_func_t $destroy; the user data to attach to the surface
=end pod

method set-user-data ( cairo_user_data_key_t $key, void-ptr $user_data, cairo_destroy_func_t $destroy --> Int ) {

  cairo_surface_set_user_data(
    self._get-native-object-no-reffing, $key, $user_data, $destroy
  )
}

sub cairo_surface_set_user_data (
  cairo_surface_t $surface, cairo_user_data_key_t $key, void-ptr $user_data, cairo_destroy_func_t $destroy --> gint32
) is native(&cairo-lib)
  { * }
}}

#-------------------------------------------------------------------------------
#TM:0:show-page:
=begin pod
=head2 show-page

Emits and clears the current page for backends that support multiple pages.  Use C<copy-page()> if you don't want to clear the page. There is a convenience function for this that takes a B<cairo_t>, namely C<show-page()>.

  method show-page ( )

=end pod

method show-page ( ) {
  cairo_surface_show_page(self._get-native-object-no-reffing)
}

sub cairo_surface_show_page (
  cairo_surface_t $surface
) is native(&cairo-lib)
  { * }

#-------------------------------------------------------------------------------
#TM:1:status:
=begin pod
=head2 status

Checks whether an error has previously occurred for this surface.
Return value can be one of: C<CAIRO_STATUS_SUCCESS>, C<CAIRO_STATUS_NULL_POINTER>, C<CAIRO_STATUS_NO_MEMORY>, C<CAIRO_STATUS_READ_ERROR>, C<CAIRO_STATUS_INVALID_CONTENT>, C<CAIRO_STATUS_INVALID_FORMAT>, or C<CAIRO_STATUS_INVALID_VISUAL>.

  method status ( --> cairo_status_t )

=end pod

method status ( --> cairo_status_t ) {
  cairo_status_t(cairo_surface_status(self._get-native-object-no-reffing))
}

sub cairo_surface_status (
  cairo_surface_t $surface --> gint32
) is native(&cairo-lib)
  { * }

#`{{
#-------------------------------------------------------------------------------
#TM:0:supports-mime-type:
=begin pod
=head2 supports-mime-type

Return whether I<surface> supports I<mime_type>.  Return value: C<True> if I<surface> supports I<mime_type>, C<False> otherwise

  method supports-mime-type ( --> Bool )

=item Int $mime_type; a B<cairo_surface_t>
=end pod

method supports-mime-type ( --> Int ) {
  cairo_surface_supports_mime_type(
    self._get-native-object-no-reffing, my gint $mime_type
  )
}

sub cairo_surface_supports_mime_type (
  cairo_surface_t $surface, gchar-ptr $mime_type --> gint32
) is native(&cairo-lib)
  { * }
}}

#-------------------------------------------------------------------------------
#TM:1:unmap-image:
=begin pod
=head2 unmap-image

Unmaps the image surface as returned from C<map-to-image>()>. The content of the image will be uploaded to the target surface. Afterwards, the image is destroyed. Using an image surface which wasn't returned by C<map-to-image()> results in undefined behavior.

  method unmap-image ( cairo_surface_t $image )

=item $image; the currently mapped image
=end pod

method unmap-image ( $image is copy ) {
  $image .= _get-native-object-no-reffing unless $image ~~ cairo_surface_t;
  cairo_surface_unmap_image( self._get-native-object-no-reffing, $image)
}

sub cairo_surface_unmap_image (
  cairo_surface_t $surface, cairo_surface_t $image
) is native(&cairo-lib)
  { * }

#-------------------------------------------------------------------------------
#TM:1:write-to-png:
=begin pod
=head2 write-to-png

=comment The PNG functions allow reading PNG images into image surfaces, and writing any surface to a PNG file. It is a toy API. It only offers very simple support for reading and writing PNG files, which is sufficient for testing and demonstration purposes. Applications which need more control over the generated PNG file should access the pixel data directly, using C<image-surface-get-data()> or a backend-specific access function, and process it with another library, e.g. B<Gnome::Gdk3::Pixbuf> or libpng.

=comment CAIRO_HAS_PNG_FUNCTIONS:  Defined if the PNG functions are available. This macro can be used to conditionally compile code using the cairo PNG functions.   stderr and rely on the user to check for errors via the B<cairo_status_t> return. loading the image after a warning. So we also want to return the (incorrect?) surface.  We use our own warning callback to squelch any attempts by libpng to write to stderr as we may not be in control of that output. Otherwise, we will segfault if we are writing to a stream.

Writes the contents of surface to a new file filename as a PNG image.

  method write-to-png ( Str $filename --> cairo_status_t )

=item $filename; PNG Support

=end pod

method write-to-png ( Str $filename --> cairo_status_t ) {
  cairo_status_t(
    cairo_surface_write_to_png( self._get-native-object-no-reffing, $filename)
  )
}

sub cairo_surface_write_to_png (
  cairo_surface_t $surface, Str $filename --> int32
) is native(&cairo-lib)
  { * }


