# Cairo::Surface Class Reference

A cairo surface represents an image, either as the destination of a drawing operation or as source when drawing onto another surface. More...

Inheritance diagram for Cairo::Surface:
[legend]

List of all members.

## Public Types

typedef sigc::slot < ErrorStatus, const unsigned char*, unsigned int > | SlotWriteFunc
| For example: `ErrorStatus my_write_func(unsigned char* data, unsigned int length);`
typedef sigc::slot < ErrorStatus, unsigned char *, unsigned int > | SlotReadFunc
| This is the type of function which is called when a backend needs to read data from an input stream.
typedef sigc::slot< void > | SlotDestroy
| For instance, void on_destroy();.
typedef cairo_surface_t | cobject
| The underlying C cairo surface type.

## Public Member Functions

| Surface (cairo_surface_t*cobject, bool has_reference=false)
| Create a C++ wrapper for the C instance.
virtual | ~Surface ()
const unsigned char* | get_mime_data (const std::string& mime_type, unsigned long& length)
| Return mime data previously attached to surface using the specified mime type.
void | set_mime_data (const std::string& mime_type, unsigned char* data, unsigned long length, const SlotDestroy& slot_destroy)
| Attach an image in the format mime_type to surface.
void | unset_mime_data (const std::string& mime_type)
| Remove the data from a surface.
void | get_font_options (FontOptions& options) const
| Retrieves the default font rendering options for the surface.
void | finish ()
| This function finishes the surface and drops all references to external resources.
void | flush ()
| Do any pending drawing for the surface and also restore any temporary modifications cairo has made to the surface's state.
void | mark_dirty ()
| Tells cairo to consider the data buffer dirty.
void | mark_dirty (int x, int y, int width, int height)
| Marks a rectangular area of the given surface dirty.
void | set_device_offset (double x_offset, double y_offset)
| Sets an offset that is added to the device coordinates determined by the CTM when drawing to surface.
void | get_device_offset (double& x_offset, double& y_offset) const
| Returns a previous device offset set by set_device_offset().
void | set_fallback_resolution (double x_pixels_per_inch, double y_pixels_per_inch)
| Sets the fallback resolution of the image in dots per inch.
void | get_fallback_resolution (double& x_pixels_per_inch, double& y_pixels_per_inch) const
| This function returns the previous fallback resolution set by set_fallback_resolution(), or default fallback resolution if never set.
SurfaceType | get_type () const
Content | get_content () const
| This function returns the content type of surface which indicates whether the surface contains color and/or alpha information.
void | copy_page ()
| Emits the current page for backends that support multiple pages, but doesn't clear it, so that the contents of the current page will be retained for the next page.
void | show_page ()
| Emits and clears the current page for backends that support multiple pages.
bool | has_show_text_glyphs () const
| Returns whether the surface supports sophisticated Context::show_text_glyphs() operations.
void | write_to_png (const std::string& filename)
| Writes the contents of surface to a new file filename as a PNG image.
void | write_to_png_stream (const SlotWriteFunc& write_func)
| Writes the Surface to the write function.
void | write_to_png (cairo_write_func_t write_func, void* closure)
RefPtr< Device > | get_device ()
| This function returns the device for a surface.
cobject* | cobj ()
| Provides acces to the underlying C cairo surface.
const cobject* | cobj () const
| Provides acces to the underlying C cairo surface.

## Static Public Member Functions

static RefPtr< Surface > | create (const RefPtr< Surface > other, Content content, int width, int height)
| Create a new surface that is as compatible as possible with an existing surface.
static RefPtr< Surface > | create (const RefPtr< Surface >& target, double x, double y, double width, double height)
| Create a new surface that is a rectangle within the target surface.

## Protected Attributes

cobject* | m_cobject
| The underlying C cairo surface type that is wrapped by this Surface.

## Detailed Description

A cairo surface represents an image, either as the destination of a drawing operation or as source when drawing onto another surface.

There are different subtypes of cairo surface for different drawing backends. This class is a base class for all subtypes and should not be used directly

Surfaces are reference-counted objects that should be used via Cairo::RefPtr.

## Member Typedef Documentation

typedef cairo_surface_t Cairo::Surface::cobject

The underlying C cairo surface type.

typedef sigc::slot<void> Cairo::Surface::SlotDestroy

For instance, void on_destroy();.

typedef sigc::slot<ErrorStatus, unsigned char* , unsigned int > Cairo::Surface::SlotReadFunc

This is the type of function which is called when a backend needs to read data from an input stream.

It is passed the buffer to read the data into and the length of the data in bytes. The read function should return CAIRO_STATUS_SUCCESS if all the data was successfully read, CAIRO_STATUS_READ_ERROR otherwise.

Parameters:

  data | the buffer into which to read the data
  length | the amount of data to read

Returns:
  the status code of the read operation

typedef sigc::slot<ErrorStatus, const unsigned char* , unsigned int > Cairo::Surface::SlotWriteFunc

For example: `ErrorStatus my_write_func(unsigned char* data, unsigned int length);`

This is the type of function which is called when a backend needs to write data to an output stream. It is passed the data to write and the length of the data in bytes. The write function should return CAIRO_STATUS_SUCCESS if all the data was successfully written, CAIRO_STATUS_WRITE_ERROR otherwise.

Parameters:

  data | the buffer containing the data to write
  length | the amount of data to write

Returns:
  the status code of the write operation

## Constructor & Destructor Documentation

Cairo::Surface::Surface | ( | cairo_surface_t * | cobject,
|  | bool | has_reference = `false`
| ) |  | `[explicit]`

Create a C++ wrapper for the C instance.

This C++ instance should then be given to a RefPtr.

Parameters:

  cobject | The C instance.
  has_reference | Whether we already have a reference. Otherwise, the constructor will take an extra reference.

virtual Cairo::Surface::~Surface | ( |  | ) | `[virtual]`

## Member Function Documentation

cobject* Cairo::Surface::cobj | ( |  | ) | `[inline]`

Provides acces to the underlying C cairo surface.

const cobject* Cairo::Surface::cobj | ( |  | ) | const`[inline]`

Provides acces to the underlying C cairo surface.

void Cairo::Surface::copy_page | ( |  | ) |

Emits the current page for backends that support multiple pages, but doesn't clear it, so that the contents of the current page will be retained for the next page.

Use show_page() if you want to get an empty page after the emission.

Since:
  1.6

static RefPtr<Surface> Cairo::Surface::create | ( | const RefPtr< Surface > | other,
|  | Content | content,
|  | int | width,
|  | int | height
| ) |  | `[static]`

Create a new surface that is as compatible as possible with an existing surface.

The new surface will use the same backend as other unless that is not possible for some reason.

Parameters:

  other | an existing surface used to select the backend of the new surface
  content | the content for the new surface
  width | width of the new surface, (in device-space units)
  height | height of the new surface (in device-space units)

Returns:
  a RefPtr to the newly allocated surface.

static RefPtr<Surface> Cairo::Surface::create | ( | const RefPtr< Surface >& | target,
|  | double | x,
|  | double | y,
|  | double | width,
|  | double | height
| ) |  | `[static]`

Create a new surface that is a rectangle within the target surface.

All operations drawn to this surface are then clipped and translated onto the target surface. Nothing drawn via this sub-surface outside of its bounds is drawn onto the target surface, making this a useful method for passing constrained child surfaces to library routines that draw directly onto the parent surface, i.e. with no further backend allocations, double buffering or copies.

The semantics of subsurfaces have not been finalized yet unless the rectangle is in full device units, is contained within the extents of the target surface, and the target or subsurface's device transforms are not changed.

Parameters:

  target | an existing surface for which the sub-surface will point to
  x | the x-origin of the sub-surface from the top-left of the target surface (in device-space units)
  y | the y-origin of the sub-surface from the top-left of the target surface (in device-space units)
  width | width of the sub-surface (in device-space units)
  height | height of the sub-surface (in device-space units)

Since:
  1.10

void Cairo::Surface::finish | ( |  | ) |

This function finishes the surface and drops all references to external resources.

For example, for the Xlib backend it means that cairo will no longer access the drawable, which can be freed. After calling finish() the only valid operations on a surface are getting and setting user data and referencing and destroying it. Further drawing to the surface will not affect the surface but will instead trigger a CAIRO_STATUS_SURFACE_FINISHED error.

When the Surface is destroyed, cairo will call finish() if it hasn't been called already, before freeing the resources associated with the Surface.

void Cairo::Surface::flush | ( |  | ) |

Do any pending drawing for the surface and also restore any temporary modifications cairo has made to the surface's state.

This function must be called before switching from drawing on the surface with cairo to drawing on it directly with native APIs. If the surface doesn't support direct access, then this function does nothing.

Content Cairo::Surface::get_content | ( |  | ) | const

This function returns the content type of surface which indicates whether the surface contains color and/or alpha information.

Since:
  1.8

RefPtr<Device> Cairo::Surface::get_device | ( |  | ) |

This function returns the device for a surface.

Returns:
  The device for this surface, or an empty RefPtr if the surface has no associated device

void Cairo::Surface::get_device_offset | ( | double & | x_offset,
|  | double & | y_offset
| ) |  | const

Returns a previous device offset set by set_device_offset().

void Cairo::Surface::get_fallback_resolution | ( | double & | x_pixels_per_inch,
|  | double & | y_pixels_per_inch
| ) |  | const

This function returns the previous fallback resolution set by set_fallback_resolution(), or default fallback resolution if never set.

Parameters:

  x_pixels_per_inch | horizontal pixels per inch
  y_pixels_per_inch | vertical pixels per inch

Since:
  1.8

void Cairo::Surface::get_font_options | ( | FontOptions& | options | ) | const

Retrieves the default font rendering options for the surface.

This allows display surfaces to report the correct subpixel order for rendering on them, print surfaces to disable hinting of metrics and so forth. The result can then be used with cairo_scaled_font_create().

Parameters:

  options | a FontOptions object into which to store the retrieved options. All existing values are overwritten

const unsigned char* Cairo::Surface::get_mime_data | ( | const std::string & | mime_type,
|  | unsigned long & | length
| ) |  |

Return mime data previously attached to surface using the specified mime type.

If no data has been attached with the given mime type then this returns 0.

Parameters:

  mime_type | The MIME type of the image data.
  length | This will be set to the length of the image data.

Returns:
  The image data attached to the surface.

SurfaceType Cairo::Surface::get_type | ( |  | ) | const

bool Cairo::Surface::has_show_text_glyphs | ( |  | ) | const

Returns whether the surface supports sophisticated Context::show_text_glyphs() operations.

That is, whether it actually uses the provided text and cluster data to a Context::show_text_glyphs() call.

Note: Even if this function returns FALSE, a Context::show_text_glyphs() operation targeted at this surface will still succeed. It just will act like a Context::show_glyphs() operation. Users can use this function to avoid computing UTF-8 text and cluster mapping if the target surface does not use it.

Since:
  1.8

void Cairo::Surface::mark_dirty | ( | int | x,
|  | int | y,
|  | int | width,
|  | int | height
| ) |  |

Marks a rectangular area of the given surface dirty.

Parameters:

  x | X coordinate of dirty rectangle
  y | Y coordinate of dirty rectangle
  width | width of dirty rectangle
  height | height of dirty rectangle

void Cairo::Surface::mark_dirty | ( |  | ) |

Tells cairo to consider the data buffer dirty.

In particular, if you've created an ImageSurface with a data buffer that you've allocated yourself and you draw to that data buffer using means other than cairo, you must call mark_dirty() before doing any additional drawing to that surface with cairo.

Note that if you do draw to the Surface outside of cairo, you must call flush() before doing the drawing.

void Cairo::Surface::set_device_offset | ( | double | x_offset,
|  | double | y_offset
| ) |  |

Sets an offset that is added to the device coordinates determined by the CTM when drawing to surface.

One use case for this function is when we want to create a Surface that redirects drawing for a portion of an onscreen surface to an offscreen surface in a way that is completely invisible to the user of the cairo API. Setting a transformation via cairo_translate() isn't sufficient to do this, since functions like Cairo::Context::device_to_user() will expose the hidden offset.

Note that the offset only affects drawing to the surface, not using the surface in a surface pattern.

Parameters:

  x_offset | the offset in the X direction, in device units
  y_offset | the offset in the Y direction, in device units

void Cairo::Surface::set_fallback_resolution | ( | double | x_pixels_per_inch,
|  | double | y_pixels_per_inch
| ) |  |

Sets the fallback resolution of the image in dots per inch.

Parameters:

  x_pixels_per_inch | Pixels per inch in the x direction
  y_pixels_per_inch | Pixels per inch in the y direction

void Cairo::Surface::set_mime_data | ( | const std::string & | mime_type,
|  | unsigned char * | data,
|  | unsigned long | length,
|  | const SlotDestroy & | slot_destroy
| ) |  |

Attach an image in the format mime_type to surface.

To remove the data from a surface, call unset_mime_data() with same mime type.

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 surface. This approach tends to be faster and requires less memory and disk space.

The recognized MIME types are the following: CAIRO_MIME_TYPE_JPEG, CAIRO_MIME_TYPE_PNG, CAIRO_MIME_TYPE_JP2, CAIRO_MIME_TYPE_URI.

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.

Parameters:

  mime_type | The MIME type of the image data.
  data | The image data to attach to the surface.
  length | The length of the image data.
  slot_destroy | A callback slot that will be called when the Surface no longer needs the data. For instance, when the Surface is destroyed or when new image data is attached using the same MIME tpe.

void Cairo::Surface::show_page | ( |  | ) |

Emits and clears the current page for backends that support multiple pages.

Use copy_page() if you don't want to clear the page.

Since:
  1.6

void Cairo::Surface::unset_mime_data | ( | const std::string & | mime_type | ) |

Remove the data from a surface.

See set_mime_data().

void Cairo::Surface::write_to_png | ( | const std::string & | filename | ) |

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

Note:
  For this function to be available, cairo must have been compiled with PNG support

Parameters:

  filename | the name of a file to write to

void Cairo::Surface::write_to_png | ( | cairo_write_func_t | write_func,
|  | void * | closure
| ) |  |

Deprecated:
  Use write_to_png_stream instead

void Cairo::Surface::write_to_png_stream | ( | const SlotWriteFunc & | write_func | ) |

Writes the Surface to the write function.

Note:
  For this function to be available, cairo must have been compiled with PNG support

Parameters:

  write_func | The function to be called when the backend needs to write data to an output stream

Since:
  1.8

## Member Data Documentation

cobject* Cairo::Surface::m_cobject `[protected]`

The underlying C cairo surface type that is wrapped by this Surface.

The documentation for this class was generated from the following file:

- cairomm/surface.h