Module Cairo.Surface

Abstract representation of all different drawing targets that cairo can render to; the actual drawings are performed using a cairo context.

type t

A Cairo.Surface.t represents an image, either as the destination of a drawing operation or as source when drawing onto another surface. To draw to a Cairo.Surface.t, create a cairo context with the surface as the target, using Cairo.create.

There are different subtypes of Cairo.Surface.t for different drawing backends; for example, Cairo.Image.create creates a bitmap image in memory. The type of a surface can be queried with Cairo.Surface.get_type.

val create_similar : t -> content -> w:int -> h:int -> t

create_similar other content width height create a new surface that is as compatible as possible with the existing surface other. 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.)

val finish : t -> unit

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. After calling Cairo.Surface.finish the only valid operations on a surface are flushing and finishing it. Further drawing to the surface will not affect the surface but will instead raise Error(SURFACE_FINISHED).

val flush : t -> unit

Do any pending drawing for the surface and also restore any temporary modification's 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.

val get_font_options : t -> Font_options.t

get_font_options surface 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.

val get_content : t -> content

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

val mark_dirty : t -> unit

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.

val mark_dirty_rectangle : t -> int -> int -> w:int -> h:int -> unit

mark_dirty_rectangle x y w h 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.

val set_device_offset : t -> float -> float -> unit

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.

  • parameter x

    the offset in the X direction, in device units.

  • parameter y

    the offset in the Y direction, in device units.

val get_device_offset : t -> float * float

This function returns the previous device offset set by Cairo.Surface.set_device_offset.

val set_fallback_resolution : t -> x:float -> y:float -> unit

set_fallback_resolution surface x_pixels_per_inch y_pixels_per_inch sets 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 Cairo.show_page or Cairo.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.

val get_fallback_resolution : t -> float * float

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

type kind = [
| `Image
| `PDF
| `PS
| `XLib
| `XCB
| `GLITZ
| `Quartz
| `Win32
| `BEOS
| `DirectFB
| `SVG
| `OS2
| `Win32_printing
| `Quartz_image
| `Recording
]

This is used to describe the type of a given surface. The surface types are also known as "backends" or "surface backends" within cairo.

The type of a surface is determined by the function used to create it, which will generally be of the form cairo_type_surface_create (though see Cairo.Surface.create_similar as well).

val get_type : t -> kind

This function returns the type of the backend used to create a surface. See Cairo.Surface.kind for available types.

val copy_page : t -> unit

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.context, namely Cairo.copy_page.

val show_page : t -> unit

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.context, namely Cairo.show_page.

val has_show_text_glyphs : t -> bool

Returns whether the surface supports sophisticated Cairo.Glyph.show_text operations. That is, whether it actually uses the provided text and cluster data to a Cairo.Glyph.show_text call.

Note: Even if this function returns false, a Cairo.Glyph.show_text operation targeted at surface will still succeed. It just will act like a Cairo.Glyph.show operation. Users can use this function to avoid computing UTF-8 text and cluster mapping if the target surface does not use it.