cairomm  1.16.2
Public Types | Public Member Functions | Static Public Member Functions | Protected Attributes | List of all members
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...

#include <cairomm/surface.h>

Inheritance diagram for Cairo::Surface:
Inheritance graph
[legend]

Public Types

enum class  Type {
  IMAGE = CAIRO_SURFACE_TYPE_IMAGE ,
  PDF = CAIRO_SURFACE_TYPE_PDF ,
  PS = CAIRO_SURFACE_TYPE_PS ,
  XLIB = CAIRO_SURFACE_TYPE_XLIB ,
  XCB = CAIRO_SURFACE_TYPE_XCB ,
  GLITZ = CAIRO_SURFACE_TYPE_GLITZ ,
  QUARTZ = CAIRO_SURFACE_TYPE_QUARTZ ,
  WIN32 = CAIRO_SURFACE_TYPE_WIN32 ,
  WIN32_SURFACE = CAIRO_SURFACE_TYPE_WIN32 ,
  BEOS = CAIRO_SURFACE_TYPE_BEOS ,
  DIRECTFB = CAIRO_SURFACE_TYPE_DIRECTFB ,
  SVG = CAIRO_SURFACE_TYPE_SVG ,
  OS2 = CAIRO_SURFACE_TYPE_OS2 ,
  WIN32_PRINTING = CAIRO_SURFACE_TYPE_WIN32_PRINTING ,
  QUARTZ_IMAGE = CAIRO_SURFACE_TYPE_QUARTZ_IMAGE ,
  SCRIPT = CAIRO_SURFACE_TYPE_SCRIPT ,
  QT = CAIRO_SURFACE_TYPE_QT ,
  RECORDING = CAIRO_SURFACE_TYPE_RECORDING ,
  VG = CAIRO_SURFACE_TYPE_VG ,
  GL = CAIRO_SURFACE_TYPE_GL ,
  DRM = CAIRO_SURFACE_TYPE_DRM ,
  TEE = CAIRO_SURFACE_TYPE_TEE ,
  XML = CAIRO_SURFACE_TYPE_XML ,
  SKIA = CAIRO_SURFACE_TYPE_SKIA ,
  SUBSURFACE = CAIRO_SURFACE_TYPE_SUBSURFACE
}
 Cairo::Surface::Type is used to describe the type of a given surface. More...
 
enum class  Format {
  ARGB32 = CAIRO_FORMAT_ARGB32 ,
  RGB24 = CAIRO_FORMAT_RGB24 ,
  A8 = CAIRO_FORMAT_A8 ,
  A1 = CAIRO_FORMAT_A1 ,
  RGB16_565 = CAIRO_FORMAT_RGB16_565
}
 Format is used to identify the memory format of image data. More...
 
typedef sigc::slot< ErrorStatus(const unsigned char *, unsigned int)> SlotWriteFunc
 For example: ErrorStatus my_write_func(unsigned char* data, unsigned int length); More...
 
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. More...
 
typedef sigc::slot< void()> SlotDestroy
 For instance, void on_destroy();. More...
 
typedef cairo_surface_t cobject
 The underlying C cairo surface type. More...
 

Public Member Functions

 Surface (cairo_surface_t *cobject, bool has_reference=false)
 Create a C++ wrapper for the C instance. More...
 
 Surface (const Surface &)=delete
 
Surfaceoperator= (const Surface &)=delete
 
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. More...
 
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. More...
 
void unset_mime_data (const std::string & mime_type)
 Remove the data from a surface. More...
 
void get_font_options (FontOptions & options) const
 Retrieves the default font rendering options for the surface. More...
 
void finish ()
 This function finishes the surface and drops all references to external resources. More...
 
void flush ()
 Do any pending drawing for the surface and also restore any temporary modifications cairo has made to the surface's state. More...
 
void mark_dirty ()
 Tells cairo to consider the data buffer dirty. More...
 
void mark_dirty (int x, int y, int width, int height)
 Marks a rectangular area of the given surface dirty. More...
 
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. More...
 
void get_device_offset (double & x_offset, double & y_offset) const
 Returns a previous device offset set by set_device_offset(). More...
 
void set_device_scale (double x_scale, double y_scale)
 Sets a scale that is multiplied to the device coordinates determined by the CTM when drawing to surface. More...
 
void set_device_scale (double scale)
 Sets x and y scale to the same value. More...
 
void get_device_scale (double & x_scale, double & y_scale) const
 Returns a previous device scale set by set_device_scale(). More...
 
double get_device_scale () const
 Returns the x and y average of a previous device scale set by set_device_scale(). More...
 
void set_fallback_resolution (double x_pixels_per_inch, double y_pixels_per_inch)
 Set the horizontal and vertical resolution for image fallbacks. More...
 
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. More...
 
Type 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. More...
 
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. More...
 
void show_page ()
 Emits and clears the current page for backends that support multiple pages. More...
 
bool has_show_text_glyphs () const
 Returns whether the surface supports sophisticated Context::show_text_glyphs() operations. More...
 
void write_to_png (const std::string & filename)
 Writes the contents of surface to a new file filename as a PNG image. More...
 
void write_to_png_stream (const SlotWriteFunc & write_func)
 Writes the Surface to the write function. More...
 
RefPtr< Deviceget_device ()
 This function returns the device for a surface. More...
 
cobjectcobj ()
 Provides acces to the underlying C cairo surface. More...
 
const cobjectcobj () const
 Provides acces to the underlying C cairo surface. More...
 

Static Public Member Functions

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

Protected Attributes

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

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

Most surface types allow accessing the surface without using Cairo functions. If you do this, keep in mind that it is mandatory that you call Cairo::Surface::flush() before reading from or writing to the surface and that you must use Cairo::Surface::mark_dirty() after modifying it.

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

Member Typedef Documentation

◆ cobject

typedef cairo_surface_t Cairo::Surface::cobject

The underlying C cairo surface type.

◆ SlotDestroy

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

For instance, void on_destroy();.

◆ SlotReadFunc

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
datathe buffer into which to read the data
lengththe amount of data to read
Returns
the status code of the read operation

◆ SlotWriteFunc

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
datathe buffer containing the data to write
lengththe amount of data to write
Returns
the status code of the write operation

Member Enumeration Documentation

◆ Format

Format is used to identify the memory format of image data.

New entries may be added in future versions.

Enumerator
ARGB32 

Each pixel is a 32-bit quantity, with alpha in the upper 8 bits, then red, then green, then blue.

The 32-bit quantities are stored native-endian. Pre-multiplied alpha is used. (That is, 50% transparent red is 0x80800000,

RGB24 

Each pixel is a 32-bit quantity, with the upper 8 bits unused.

Red, Green, and Blue are stored in the remaining 24 bits in that order.

A8 

Each pixel is a 8-bit quantity holding an alpha value.

A1 

Each pikel is a 1-bit quentity holding an alpha value.

Pixels are packed together into 32-bit quantities. The ordering of the bits matches the endianess of the platform. On a big-endian machine, the first pixel is in the uppermost bit, on a little endian machine the first pixel is in the least-significant bit.

RGB16_565 

Each fixel is a 16-bit quantity with red in the upper 5 bits, then green in the middle 6 bits, and blue in the lower 5 bits.

◆ Type

enum Cairo::Surface::Type
strong

Cairo::Surface::Type is used to describe the type of a given surface.

The surface types are also known as "backends" or "surface backends" within cairo.

The surface type can be queried with Surface::get_type()

The various Cairo::Surface functions can be used with surfaces of any type, but some backends also provide type-specific functions that must only be called with a surface of the appropriate type.

New entries may be added in future versions.

Since
1.2
Enumerator
IMAGE 

The surface is of type image.

PDF 

The surface is of type pdf.

PS 

The surface is of type ps.

XLIB 

The surface is of type xlim.

XCB 

The surface is of type xcb.

GLITZ 

The surface is of type glitz.

QUARTZ 

The surface is of type quartz.

WIN32 

The surface is of type win32.

Deprecated:
Use WIN32_SURFACE instead.
WIN32_SURFACE 

The surface is of type win32.

Since
1.16.1
BEOS 

The surface is of type beos.

DIRECTFB 

The surface is of type directfb.

SVG 

The surface is of type svg.

OS2 

The surface is of type os2.

WIN32_PRINTING 

The surface is a win32 printing surface.

QUARTZ_IMAGE 

The surface is of type quartz_image.

SCRIPT 

The surface is of type script.

Since
1.10
QT 

The surface is of type Qt.

Since
1.10
RECORDING 

The surface is of type recording.

Since
1.10
VG 

The surface is a OpenVg surface.

Since
1.10
GL 

The surface is of type OpenGl.

Since
1.10
DRM 

The surface is of type Direct Render Manager.

Since
1.10
TEE 

The surface is of type script 'tee' (a multiplexing surface)

Since
1.10
XML 

The surface is of type XML (for debugging)

Since
1.10
SKIA 

The surface is of type Skia.

Since
1.10
SUBSURFACE 

The surface is of type The surface is a subsurface created with Surface::create()

Since
1.10

Constructor & Destructor Documentation

◆ Surface() [1/2]

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
cobjectThe C instance.
has_referenceWhether we already have a reference. Otherwise, the constructor will take an extra reference.

◆ Surface() [2/2]

Cairo::Surface::Surface ( const Surface )
delete

◆ ~Surface()

virtual Cairo::Surface::~Surface ( )
virtual

Member Function Documentation

◆ cobj() [1/2]

cobject* Cairo::Surface::cobj ( )
inline

Provides acces to the underlying C cairo surface.

◆ cobj() [2/2]

const cobject* Cairo::Surface::cobj ( ) const
inline

Provides acces to the underlying C cairo surface.

◆ copy_page()

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

◆ create() [1/2]

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.

@Note 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
targetan existing surface for which the sub-surface will point to
xthe x-origin of the sub-surface from the top-left of the target surface (in device-space units)
ythe y-origin of the sub-surface from the top-left of the target surface (in device-space units)
widthwidth of the sub-surface (in device-space units)
heightheight of the sub-surface (in device-space units)
Since
1.10

◆ create() [2/2]

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
otheran existing surface used to select the backend of the new surface
contentthe content for the new surface
widthwidth of the new surface, (in device-space units)
heightheight of the new surface (in device-space units)
Returns
a RefPtr to the newly allocated surface.

◆ finish()

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.

◆ flush()

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.

◆ get_content()

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

◆ get_device()

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

◆ get_device_offset()

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

Returns a previous device offset set by set_device_offset().

◆ get_device_scale() [1/2]

double Cairo::Surface::get_device_scale ( ) const

Returns the x and y average of a previous device scale set by set_device_scale().

Since cairomm 1.18:

◆ get_device_scale() [2/2]

void Cairo::Surface::get_device_scale ( double &  x_scale,
double &  y_scale 
) const

Returns a previous device scale set by set_device_scale().

Since cairomm 1.18:

◆ get_fallback_resolution()

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_inchhorizontal pixels per inch
y_pixels_per_inchvertical pixels per inch
Since
1.8

◆ get_font_options()

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::ScaledFont::create().

Parameters
optionsa FontOptions object into which to store the retrieved options. All existing values are overwritten

◆ get_mime_data()

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_typeThe MIME type of the image data.
lengthThis will be set to the length of the image data.
Returns
The image data attached to the surface.
Since
1.10

◆ get_type()

Type Cairo::Surface::get_type ( ) const

◆ has_show_text_glyphs()

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

◆ mark_dirty() [1/2]

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.

◆ mark_dirty() [2/2]

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

Marks a rectangular area of the given surface dirty.

Parameters
xX coordinate of dirty rectangle
yY coordinate of dirty rectangle
widthwidth of dirty rectangle
heightheight of dirty rectangle

◆ operator=()

Surface& Cairo::Surface::operator= ( const Surface )
delete

◆ set_device_offset()

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::Context::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_offsetthe offset in the X direction, in device units
y_offsetthe offset in the Y direction, in device units

◆ set_device_scale() [1/2]

void Cairo::Surface::set_device_scale ( double  scale)
inline

Sets x and y scale to the same value.

See set_device_scale(double, double) for details.

Parameters
scalea scale factor in the X and Y direction
Since cairomm 1.18:

◆ set_device_scale() [2/2]

void Cairo::Surface::set_device_scale ( double  x_scale,
double  y_scale 
)

Sets a scale that is multiplied to the device coordinates determined by the CTM when drawing to surface.

One common use for this is to render to very high resolution display devices at a scale factor, so that code that assumes 1 pixel will be a certain size will still work. Setting a transformation via Cairo::Context::scale() isn't sufficient to do this, since functions like Cairo::Context::device_to_user() will expose the hidden scale.

Note that the scale affects drawing to the surface as well as using the surface in a source pattern.

Parameters
x_scalea scale factor in the X direction
y_scalea scale factor in the Y direction
Since cairomm 1.18:

◆ set_fallback_resolution()

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

Set the horizontal and vertical resolution for image fallbacks.

When certain operations aren't supported natively by a backend, cairo will fallback by rendering operations to an image and then overlaying that image onto the output. For backends that are natively vector-oriented, this function can be used to set the resolution used for these image fallbacks, (larger values will result in more detailed images, but also larger file sizes).

Some examples of natively vector-oriented backends are the ps, pdf, and svg backends.

For backends that are natively raster-oriented, image fallbacks are still possible, but they are always performed at the native device resolution. So this function has no effect on those backends.

Note: The fallback resolution only takes effect at the time of completing a page (with Context::show_page() or Context::copy_page()) so there is currently no way to have more than one fallback resolution in effect on a single page.

The default fallback resoultion is 300 pixels per inch in both dimensions.

Parameters
x_pixels_per_inchPixels per inch in the x direction
y_pixels_per_inchPixels per inch in the y direction
Since
1.2

◆ set_mime_data()

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_typeThe MIME type of the image data. param data The image @data to attach to the surface. param length The length of the image data.
slot_destroyA 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.
Since
1.10

◆ show_page()

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

◆ unset_mime_data()

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

Remove the data from a surface.

See set_mime_data().

◆ write_to_png()

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
filenamethe name of a file to write to

◆ write_to_png_stream()

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_funcThe function to be called when the backend needs to write data to an output stream
Since
1.8

Member Data Documentation

◆ m_cobject

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: