Skip to content

Gtk.PrintContext

class — extends GObject.Object

Encapsulates context information that is required when drawing pages for printing.

This includes the cairo context and important parameters like page size and resolution. It also lets you easily create Pango.Layout and Pango.Context objects that match the font metrics of the cairo surface.

GtkPrintContext objects get passed to the PrintOperation.begin-print, PrintOperation.end-print, PrintOperation.request-page-setup and PrintOperation.draw-page signals on the PrintOperation object.

Using GtkPrintContext in a ::draw-page callback

static void
draw_page (GtkPrintOperation *operation,
           GtkPrintContext   *context,
           int                page_nr)
{
  cairo_t *cr;
  PangoLayout *layout;
  PangoFontDescription *desc;

  cr = gtk_print_context_get_cairo_context (context);

  // Draw a red rectangle, as wide as the paper (inside the margins)
  cairo_set_source_rgb (cr, 1.0, 0, 0);
  cairo_rectangle (cr, 0, 0, gtk_print_context_get_width (context), 50);

  cairo_fill (cr);

  // Draw some lines
  cairo_move_to (cr, 20, 10);
  cairo_line_to (cr, 40, 20);
  cairo_arc (cr, 60, 60, 20, 0, M_PI);
  cairo_line_to (cr, 80, 20);

  cairo_set_source_rgb (cr, 0, 0, 0);
  cairo_set_line_width (cr, 5);
  cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
  cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);

  cairo_stroke (cr);

  // Draw some text
  layout = gtk_print_context_create_pango_layout (context);
  pango_layout_set_text (layout, "Hello World! Printing is easy", -1);
  desc = pango_font_description_from_string ("sans 28");
  pango_layout_set_font_description (layout, desc);
  pango_font_description_free (desc);

  cairo_move_to (cr, 30, 20);
  pango_cairo_layout_path (cr, layout);

  // Font Outline
  cairo_set_source_rgb (cr, 0.93, 1.0, 0.47);
  cairo_set_line_width (cr, 0.5);
  cairo_stroke_preserve (cr);

  // Font Fill
  cairo_set_source_rgb (cr, 0, 0.0, 1.0);
  cairo_fill (cr);

  g_object_unref (layout);
}

Methods

create_pango_context

def create_pango_context(self) -> Pango.Context

Creates a new PangoContext that can be used with the GtkPrintContext.

create_pango_layout

def create_pango_layout(self) -> Pango.Layout

Creates a new PangoLayout that is suitable for use with the GtkPrintContext.

get_cairo_context

def get_cairo_context(self) -> cairo.Context[cairo.Surface]

Obtains the cairo context that is associated with the GtkPrintContext.

get_dpi_x

def get_dpi_x(self) -> float

Obtains the horizontal resolution of the GtkPrintContext, in dots per inch.

get_dpi_y

def get_dpi_y(self) -> float

Obtains the vertical resolution of the GtkPrintContext, in dots per inch.

get_hard_margins

def get_hard_margins(self) -> tuple[bool, float, float, float, float]

Obtains the hardware printer margins of the GtkPrintContext, in units.

get_height

def get_height(self) -> float

Obtains the height of the GtkPrintContext, in pixels.

get_page_setup

def get_page_setup(self) -> PageSetup

Obtains the GtkPageSetup that determines the page dimensions of the GtkPrintContext.

get_pango_fontmap

def get_pango_fontmap(self) -> Pango.FontMap

Returns a PangoFontMap that is suitable for use with the GtkPrintContext.

get_width

def get_width(self) -> float

Obtains the width of the GtkPrintContext, in pixels.

set_cairo_context

def set_cairo_context(self, cr: cairo.Context[cairo.Surface], dpi_x: float, dpi_y: float) -> None

Sets a new cairo context on a print context.

This function is intended to be used when implementing an internal print preview, it is not needed for printing, since GTK itself creates a suitable cairo context in that case.

Parameters:

  • cr — the cairo context
  • dpi_x — the horizontal resolution to use with cr
  • dpi_y — the vertical resolution to use with cr