class Gtk::PrintContext

Overview

A Gtk::PrintContext 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.

Gtk::PrintContext objects get passed to the [signal@Gtk.PrintOperation::begin-print], [signal@Gtk.PrintOperation::end-print], [signal@Gtk.PrintOperation::request-page-setup] and [signal@Gtk.PrintOperation::draw-page] signals on the Gtk::PrintOperation object.

Using Gtk::PrintContext in a ::draw-page callback

WARNING ⚠️ The following code is in c ⚠️

static void
draw_page (Gtk::PrintOperation *operation,
           Gtk::PrintContext   *context,
           int                page_nr)
{
  cairo_::t *cr;
  Pango::Layout *layout;
  Pango::FontDescription *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);
}

Defined in:

lib/gi-crystal/src/auto/gtk-4.0/print_context.cr

Constructors

Class Method Summary

Instance Method Summary

Instance methods inherited from class GObject::Object

==(other : self) ==, bind_property(source_property : String, target : GObject::Object, target_property : String, flags : GObject::BindingFlags) : GObject::Binding bind_property, bind_property_full(source_property : String, target : GObject::Object, target_property : String, flags : GObject::BindingFlags, transform_to : GObject::Closure, transform_from : GObject::Closure) : GObject::Binding bind_property_full, data(key : String) : Pointer(Void) | Nil data, finalize finalize, freeze_notify : Nil freeze_notify, getv(names : Enumerable(String), values : Enumerable(_)) : Nil getv, hash(hasher) hash, notify(property_name : String) : Nil notify, notify_by_pspec(pspec : GObject::ParamSpec) : Nil notify_by_pspec, notify_signal notify_signal, property(property_name : String, value : _) : Nil property, qdata(quark : UInt32) : Pointer(Void) | Nil qdata, ref_count : UInt32 ref_count, run_dispose : Nil run_dispose, set_data(key : String, data : Pointer(Void) | Nil) : Nil set_data, set_property(property_name : String, value : _) : Nil set_property, steal_data(key : String) : Pointer(Void) | Nil steal_data, steal_qdata(quark : UInt32) : Pointer(Void) | Nil steal_qdata, thaw_notify : Nil thaw_notify, to_unsafe : Pointer(Void) to_unsafe, watch_closure(closure : GObject::Closure) : Nil watch_closure

Constructor methods inherited from class GObject::Object

cast(obj : GObject::Object) : self cast, new(pointer : Pointer(Void), transfer : GICrystal::Transfer)
new
new
, newv(object_type : UInt64, parameters : Enumerable(GObject::Parameter)) : self newv

Class methods inherited from class GObject::Object

cast?(obj : GObject::Object) : self | Nil cast?, compat_control(what : UInt64, data : Pointer(Void) | Nil) : UInt64 compat_control, g_type : UInt64 g_type, interface_find_property(g_iface : GObject::TypeInterface, property_name : String) : GObject::ParamSpec interface_find_property, interface_list_properties(g_iface : GObject::TypeInterface) : Enumerable(GObject::ParamSpec) interface_list_properties

Macros inherited from class GObject::Object

previous_vfunc(*args) previous_vfunc, previous_vfunc!(*args) previous_vfunc!, signal(signature) signal

Constructor Detail

def self.new #

Initialize a new PrintContext.


[View source]

Class Method Detail

def self.g_type : UInt64 #

Returns the type id (GType) registered in GLib type system.


[View source]

Instance Method Detail

def ==(other : self) #
Description copied from class Reference

Returns true if this reference is the same as other. Invokes same?.


def cairo_context : Cairo::Context #

Obtains the cairo context that is associated with the Gtk::PrintContext.


[View source]
def create_pango_context : Pango::Context #

Creates a new Pango::Context that can be used with the Gtk::PrintContext.


[View source]
def create_pango_layout : Pango::Layout #

Creates a new Pango::Layout that is suitable for use with the Gtk::PrintContext.


[View source]
def dpi_x : Float64 #

Obtains the horizontal resolution of the Gtk::PrintContext, in dots per inch.


[View source]
def dpi_y : Float64 #

Obtains the vertical resolution of the Gtk::PrintContext, in dots per inch.


[View source]
def hard_margins(top : Float64, bottom : Float64, left : Float64, right : Float64) : Bool #

Obtains the hardware printer margins of the Gtk::PrintContext, in units.


[View source]
def hash(hasher) #
Description copied from class Reference

See Object#hash(hasher)


def height : Float64 #

Obtains the height of the Gtk::PrintContext, in pixels.


[View source]
def page_setup : Gtk::PageSetup #

Obtains the Gtk::PageSetup that determines the page dimensions of the Gtk::PrintContext.


[View source]
def pango_fontmap : Pango::FontMap #

Returns a Pango::FontMap that is suitable for use with the Gtk::PrintContext.


[View source]
def set_cairo_context(cr : Cairo::Context, dpi_x : Float64, dpi_y : Float64) : Nil #

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.


[View source]
def width : Float64 #

Obtains the width of the Gtk::PrintContext, in pixels.


[View source]