Haskell, GTK4 and printing

Another part of my experiments with GTK4 and Haskell was to investigate printing. That brought in the Cairo, Pango and PangoCairo C libraries, and the gi-cairo, gi-cairo-render, gi-cairo-connector, gi-pango, and gi-pangocairo Haskell packages.

PrintOperation

At the heart was to create a value of type PrintOperation, specifying its nPages and unit attributes and its handler of the drawPage signal, and use its run method with PrintOperationActionPrintDialog:

renderWithContext

PrintOperationDrawPageCallback is a type synonym for PrintContext -> Int32 -> IO (), and overloaded method getCairoContext yields a value of type GI.Cairo.Structs.Context.Context (package gi-cairo). However, package gi-cairo-render provides no function to render to a Context. The solution is package gi-cairo-connector, which provides renderWithContext to bridge the gap.

exampleRender

Cairo has basic text functionality and Pango provides complex text functionality. My exampleRender used both:

The Cario origin is the top left corner of the printable area. The printable area excludes the margins specified by the PageSetup.

Cairo’s showText renders text so that, by default, it is left-aligned and baseline-aligned.

Package gi-cairo-connector also provides toRender with type (GI.Cairo.Context -> IO ()) -> Render ().

Pango’s showLayout renders text so that the top-left corner of the Layout corresponds to the current point.