Next: , Previous: , Up: Top   [Index]


15 cairo_surface_t

Base class for surfaces

15.1 Overview

<cairo-surface> is the abstract type representing all different drawing targets that cairo can render to. The actual drawings are performed using a cairo context.

A cairo surface is created by using backend-specific constructors, typically of the form cairo_backend-surface-create.

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. Note that for other surface types it might be necessary to acquire the surface’s device first. See cairo-device-acquire for a discussion of devices.


void
modify_image_surface (cairo_surface_t *surface)
{
  unsigned char *data;
  int width, height, stride;

  // flush to ensure all writing to the image was done
  cairo_surface_flush (surface);

  // modify the image
  data = cairo_image_surface_get_data (surface);
  width = cairo_image_surface_get_width (surface);
  height = cairo_image_surface_get_height (surface);
  stride = cairo_image_surface_get_stride (surface);
  modify_image_data (data, width, height, stride);

  // mark the image dirty so Cairo clears its caches.
  cairo_surface_mark_dirty (surface);
}

15.2 Usage

Function: cairo-surface-create-similar (other <cairo-surface-t>) (content <cairo-content-t>) (width <int>) (height <int>) ⇒  (ret <cairo-surface-t >)

Create a new surface that is as compatible as possible with an existing surface. For example the new surface will have the same fallback resolution and font options as other. Generally, the new surface will also use the same backend as other, unless that is not possible for some reason. The type of the returned surface may be examined with cairo-surface-get-type.

Initially the surface contents are all 0 (transparent if contents have transparency, black otherwise.)

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)

ret

a pointer to the newly allocated surface. The caller owns the surface and should call cairo-surface-destroy when done with it. This function always returns a valid pointer, but it will return a pointer to a "nil" surface if other is already in an error state or any other error occurs.

Function: cairo-surface-create-for-rectangle (target <cairo-surface-t>) (x <double>) (y <double>) (width <double>) (height <double>) ⇒  (ret <cairo-surface-t >)

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.

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)

ret

a pointer to the newly allocated surface. The caller owns the surface and should call cairo-surface-destroy when done with it. This function always returns a valid pointer, but it will return a pointer to a "nil" surface if other is already in an error state or any other error occurs.

Since 1.10

Function: cairo-surface-finish (surface <cairo-surface-t>)

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 cairo-surface-finish the only valid operations on a surface are getting and setting user, referencing and destroying, and flushing and finishing it. Further drawing to the surface will not affect the surface but will instead trigger a ‘CAIRO_STATUS_SURFACE_FINISHED’ error.

When the last call to cairo-surface-destroy decreases the reference count to zero, cairo will call cairo-surface-finish if it hasn’t been called already, before freeing the resources associated with the surface.

surface

the <cairo-surface-t> to finish

Function: cairo-surface-flush (surface <cairo-surface-t>)

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.

surface

a <cairo-surface-t>

Function: cairo-surface-get-device (surface <cairo-surface-t>) ⇒  (ret <cairo-device-t >)

This function returns the device for a surface. See <cairo-device-t>.

surface

a <cairo-surface-t>

ret

The device for surface or ‘#f’ if the surface does not have an associated device.

Since 1.10

Function: cairo-surface-get-font-options (surface <cairo-surface-t>) (options <cairo-font-options-t>)

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.

surface

a <cairo-surface-t>

options

a <cairo-font-options-t> object into which to store the retrieved options. All existing values are overwritten

Function: cairo-surface-get-content (surface <cairo-surface-t>) ⇒  (ret <cairo-content-t>)

This function returns the content type of surface which indicates whether the surface contains color and/or alpha information. See <cairo-content-t>.

surface

a <cairo-surface-t>

ret

The content type of surface.

Since 1.2

Function: cairo-surface-mark-dirty (surface <cairo-surface-t>)

Tells cairo that drawing has been done to surface using means other than cairo, and that cairo should reread any cached areas. Note that you must call cairo-surface-flush before doing such drawing.

surface

a <cairo-surface-t>

Function: cairo-surface-mark-dirty-rectangle (surface <cairo-surface-t>) (x <int>) (y <int>) (width <int>) (height <int>)

Like cairo-surface-mark-dirty, but drawing has been done only to the specified rectangle, so that cairo can retain cached contents for other parts of the surface.

Any cached clip set on the surface will be reset by this function, to make sure that future cairo calls have the clip set that they expect.

surface

a <cairo-surface-t>

x

X coordinate of dirty rectangle

y

Y coordinate of dirty rectangle

width

width of dirty rectangle

height

height of dirty rectangle

Function: cairo-surface-set-device-offset (surface <cairo-surface-t>) (x-offset <double>) (y-offset <double>)

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 <cairo-surface-t> 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-device-to-user will expose the hidden offset.

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

surface

a <cairo-surface-t>

x-offset

the offset in the X direction, in device units

y-offset

the offset in the Y direction, in device units

Function: cairo-surface-get-device-offset (surface <cairo-surface-t>) ⇒  (x-offset <double>) (y-offset <double>)

This function returns the previous device offset set by cairo-surface-set-device-offset.

surface

a <cairo-surface-t>

x-offset

the offset in the X direction, in device units

y-offset

the offset in the Y direction, in device units

Since 1.2

Function: cairo-surface-get-type (surface <cairo-surface-t>) ⇒  (ret <cairo-surface-type-t>)

This function returns the type of the backend used to create a surface. See <cairo-surface-type-t> for available types.

surface

a <cairo-surface-t>

ret

The type of surface.

Since 1.2

Function: cairo-surface-copy-page (surface <cairo-surface-t>)

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 cairo-surface-show-page if you want to get an empty page after the emission.

There is a convenience function for this that takes a <cairo-t>, namely cairo-copy-page.

surface

a <cairo-surface-t>

Since 1.6

Function: cairo-surface-show-page (surface <cairo-surface-t>)

Emits and clears the current page for backends that support multiple pages. Use cairo-surface-copy-page if you don’t want to clear the page.

There is a convenience function for this that takes a <cairo-t>, namely cairo-show-page.

surface

a <cairo--surface-t>

Since 1.6

Function: cairo-surface-has-show-text-glyphs (surface <cairo-surface-t>) ⇒  (ret <cairo-bool-t>)

Returns whether the surface supports sophisticated cairo-show-text-glyphs operations. That is, whether it actually uses the provided text and cluster data to a cairo-show-text-glyphs call.

Note: Even if this function returns ‘#f’, a cairo-show-text-glyphs operation targeted at surface will still succeed. It just will act like a cairo-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.

surface

a <cairo-surface-t>

ret

#t’ if surface supports cairo-show-text-glyphs, ‘#f’ otherwise

Since 1.8

Function: cairo-surface-get-mime-data (surface <cairo-surface-t>) (mime-type <string>) ⇒  (ret <bytevector>)

Return data previously associated with the surface with the given mime-type.

surface

a <cairo-surface-t>

mime-type

a string denoting the MIME type, for example "image/jpeg", "image/png", "image/jp2", or "text/x-uri"

ret

#f if no data was associated with the surface, otherwise a fresh bytevector holding a copy of the data

Since 1.11

Function: cairo-surface-set-mime-data (surface <cairo-surface-t>) (mime-type <string>) (data <bytevector>)

Associate image data with a surface with the given mime-type.

Attach an image in the format mime_type to surface. To remove the data from a surface, call this function with same mime type and #f for 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 surface. This approach tends to be faster and requires less memory and disk space.

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.

surface

a <cairo-surface-t>

mime-type

a string denoting the MIME type, for example "image/jpeg", "image/png", "image/jp2", or "text/x-uri"

data

a bytevector of data to associate with the surface, or #f to remove the association

Since 1.11


Next: , Previous: , Up: Top   [Index]