## Cairo Interaction Cairo Interaction — Functions to support using cairo |

## Functions

cairo_surface_t * | gdk_window_create_similar_surface ()
cairo_surface_t * | gdk_window_create_similar_image_surface ()
cairo_t * | gdk_cairo_create ()
gboolean | gdk_cairo_get_clip_rectangle ()
GdkDrawingContext * | gdk_cairo_get_drawing_context ()
void | gdk_cairo_set_source_color ()
void | gdk_cairo_set_source_rgba ()
void | gdk_cairo_set_source_pixbuf ()
void | gdk_cairo_set_source_window ()
void | gdk_cairo_rectangle ()
void | gdk_cairo_region ()
cairo_region_t * | gdk_cairo_region_create_from_surface ()
cairo_surface_t * | gdk_cairo_surface_create_from_pixbuf ()
void | gdk_cairo_draw_from_gl ()

## Includes

    #include <gdk/gdk.h>

## Description

Cairo is a graphics library that supports vector graphics and image compositing that can be used with GDK. GTK+ does all of its drawing using cairo.

GDK does not wrap the cairo API, instead it allows to create cairo contexts which can be used to draw on GdkWindows. Additional functions allow use GdkRectangles with cairo and to use GdkColors, GdkRGBAs, GdkPixbufs and GdkWindows as sources for drawing operations.

## Functions

### gdk_window_create_similar_surface ()

    cairo_surface_t *
    gdk_window_create_similar_surface (GdkWindow *window,
                                       cairo_content_t content,
                                       int width,
                                       int height);

Create a new surface that is as compatible as possible with the given `window` . For example the new surface will have the same fallback resolution and font options as `window` . Generally, the new surface will also use the same backend as `window` , 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.)

#### Parameters

window | window to make new surface similar to |
content | the content for the new surface |
width | width of the new surface |
height | height of the new surface |

#### Returns

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: 2.22

### gdk_window_create_similar_image_surface ()

    cairo_surface_t *
    gdk_window_create_similar_image_surface
                                   (GdkWindow *window,
                                    cairo_format_t format,
                                    int width,
                                    int height,
                                    int scale);

Create a new image surface that is efficient to draw on the given `window` .

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

The `width` and `height` of the new surface are not affected by the scaling factor of the `window` , or by the `scale` argument; they are the size of the surface in device pixels. If you wish to create an image surface capable of holding the contents of `window` you can use:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    int scale = gdk_window_get_scale_factor (window);
    int width = gdk_window_get_width (window) * scale;
    int height = gdk_window_get_height (window) * scale;

    // format is set elsewhere
    cairo_surface_t *surface =
      gdk_window_create_similar_image_surface (window,
                                               format,
                                               width, height,
                                               scale);

Note that unlike `cairo_surface_create_similar_image()`, the new surface's device scale is set to `scale` , or to the scale factor of `window` if `scale` is 0.

#### Parameters

window | window to make new surface similar to, or `NULL` if none. | [nullable]
format | the format for the new surface. | [type int]
width | width of the new surface |
height | height of the new surface |
scale | the scale of the new surface, or 0 to use same as `window` |

#### Returns

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: 3.10

### gdk_cairo_create ()

    cairo_t *
    gdk_cairo_create (GdkWindow *window);

`gdk_cairo_create` has been deprecated since version 3.22 and should not be used in newly-written code.

Use `gdk_window_begin_draw_frame()` and `gdk_drawing_context_get_cairo_context()` instead

Creates a Cairo context for drawing to `window` .

Note that calling `cairo_reset_clip()` on the resulting cairo_t will produce undefined results, so avoid it at all costs.

Typically, this function is used to draw on a GdkWindow out of the paint cycle of the toolkit; this should be avoided, as it breaks various assumptions and optimizations.

If you are drawing on a native GdkWindow in response to a `GDK_EXPOSE` event you should use `gdk_window_begin_draw_frame()` and `gdk_drawing_context_get_cairo_context()` instead. GTK will automatically do this for you when drawing a widget.

#### Parameters

window | a GdkWindow |

#### Returns

A newly created Cairo context. Free with `cairo_destroy()` when you are done drawing.

Since: 2.8

### gdk_cairo_get_clip_rectangle ()

    gboolean
    gdk_cairo_get_clip_rectangle (cairo_t *cr,
                                  GdkRectangle *rect);

This is a convenience function around `cairo_clip_extents()`. It rounds the clip extents to integer coordinates and returns a boolean indicating if a clip area exists.

#### Parameters

cr | a cairo context |
rect | return location for the clip, or `NULL`. | [out][allow-none]

#### Returns

`TRUE` if a clip rectangle exists, `FALSE` if all of `cr` is clipped and all drawing can be skipped

### gdk_cairo_get_drawing_context ()

    GdkDrawingContext *
    gdk_cairo_get_drawing_context (cairo_t *cr);

Retrieves the GdkDrawingContext that created the Cairo context `cr` .

#### Parameters

cr | a Cairo context |

#### Returns

a GdkDrawingContext, if any is set.

[transfer none][nullable]

Since: 3.22

### gdk_cairo_set_source_color ()

    void
    gdk_cairo_set_source_color (cairo_t *cr,
                                const GdkColor *color);

`gdk_cairo_set_source_color` has been deprecated since version 3.4 and should not be used in newly-written code.

Use `gdk_cairo_set_source_rgba()` instead

Sets the specified GdkColor as the source color of `cr` .

#### Parameters

cr | a cairo context |
color | a GdkColor |

Since: 2.8

### gdk_cairo_set_source_rgba ()

    void
    gdk_cairo_set_source_rgba (cairo_t *cr,
                               const GdkRGBA *rgba);

Sets the specified GdkRGBA as the source color of `cr` .

#### Parameters

cr | a cairo context |
rgba | a GdkRGBA |

Since: 3.0

### gdk_cairo_set_source_pixbuf ()

    void
    gdk_cairo_set_source_pixbuf (cairo_t *cr,
                                 const GdkPixbuf *pixbuf,
                                 gdouble pixbuf_x,
                                 gdouble pixbuf_y);

Sets the given pixbuf as the source pattern for `cr` .

The pattern has an extend mode of `CAIRO_EXTEND_NONE` and is aligned so that the origin of `pixbuf` is `pixbuf_x` , `pixbuf_y` .

#### Parameters

cr | a cairo context |
pixbuf | a GdkPixbuf |
pixbuf_x | X coordinate of location to place upper left corner of `pixbuf` |
pixbuf_y | Y coordinate of location to place upper left corner of `pixbuf` |

Since: 2.8

### gdk_cairo_set_source_window ()

    void
    gdk_cairo_set_source_window (cairo_t *cr,
                                 GdkWindow *window,
                                 gdouble x,
                                 gdouble y);

Sets the given window as the source pattern for `cr` .

The pattern has an extend mode of `CAIRO_EXTEND_NONE` and is aligned so that the origin of `window` is `x` , `y` . The window contains all its subwindows when rendering.

Note that the contents of `window` are undefined outside of the visible part of `window` , so use this function with care.

#### Parameters

cr | a cairo context |
window | a GdkWindow |
x | X coordinate of location to place upper left corner of `window` |
y | Y coordinate of location to place upper left corner of `window` |

Since: 2.24

### gdk_cairo_rectangle ()

    void
    gdk_cairo_rectangle (cairo_t *cr,
                         const GdkRectangle *rectangle);

Adds the given rectangle to the current path of `cr` .

#### Parameters

cr | a cairo context |
rectangle | a GdkRectangle |

Since: 2.8

### gdk_cairo_region ()

    void
    gdk_cairo_region (cairo_t *cr,
                      const cairo_region_t *region);

Adds the given region to the current path of `cr` .

#### Parameters

cr | a cairo context |
region | a cairo_region_t |

Since: 2.8

### gdk_cairo_region_create_from_surface ()

    cairo_region_t *
    gdk_cairo_region_create_from_surface (cairo_surface_t *surface);

Creates region that describes covers the area where the given `surface` is more than 50% opaque.

This function takes into account device offsets that might be set with `cairo_surface_set_device_offset()`.

#### Parameters

surface | a cairo surface |

#### Returns

A cairo_region_t; must be freed with `cairo_region_destroy()`

### gdk_cairo_surface_create_from_pixbuf ()

    cairo_surface_t *
    gdk_cairo_surface_create_from_pixbuf (const GdkPixbuf *pixbuf,
                                          int scale,
                                          GdkWindow *for_window);

Creates an image surface with the same contents as the pixbuf.

#### Parameters

pixbuf | a GdkPixbuf |
scale | the scale of the new surface, or 0 to use same as `window` |
for_window | The window this will be drawn to, or `NULL`. | [allow-none]

#### Returns

a new cairo surface, must be freed with `cairo_surface_destroy()`

Since: 3.10

### gdk_cairo_draw_from_gl ()

    void
    gdk_cairo_draw_from_gl (cairo_t *cr,
                            GdkWindow *window,
                            int source,
                            int source_type,
                            int buffer_scale,
                            int x,
                            int y,
                            int width,
                            int height);

This is the main way to draw GL content in GTK+. It takes a render buffer ID (`source_type` == GL_RENDERBUFFER) or a texture id (`source_type` == GL_TEXTURE) and draws it onto `cr` with an OVER operation, respecting the current clip. The top left corner of the rectangle specified by `x` , `y` , `width` and `height` will be drawn at the current (0,0) position of the cairo_t.

This will work for *all* cairo_t, as long as `window` is realized, but the fallback implementation that reads back the pixels from the buffer may be used in the general case. In the case of direct drawing to a window with no special effects applied to `cr` it will however use a more efficient approach.

For GL_RENDERBUFFER the code will always fall back to software for buffers with alpha components, so make sure you use GL_TEXTURE if using alpha.

Calling this may change the current GL context.

#### Parameters

cr | a cairo context |
window | The window we're rendering for (not necessarily into) |
source | The GL ID of the source buffer |
source_type | The type of the `source` |
buffer_scale | The scale-factor that the `source` buffer is allocated for |
x | The source x position in `source` to start copying from in GL coordinates |
y | The source y position in `source` to start copying from in GL coordinates |
width | The width of the region to draw |
height | The height of the region to draw |

Since: 3.16