Class PrintCompositor

All Implemented Interfaces:
Proxy

@Generated("org.javagi.JavaGI") public class PrintCompositor extends GObject

Compose a Buffer for printing.

The GtkSourcePrintCompositor object is used to compose a Buffer for printing. You can set various configuration options to customize the printed output. GtkSourcePrintCompositor is designed to be used with the high-level printing API of gtk+, i.e. PrintOperation.

The margins specified in this object are the layout margins: they define the blank space bordering the printed area of the pages. They must not be confused with the "print margins", i.e. the parts of the page that the printer cannot print on, defined in the PageSetup objects. If the specified layout margins are smaller than the "print margins", the latter ones are used as a fallback by the GtkSourcePrintCompositor object, so that the printed area is not clipped.

  • Constructor Details

    • PrintCompositor

      public PrintCompositor(MemorySegment address)
      Create a PrintCompositor instance for the provided memory address.
      Parameters:
      address - the memory address of the native object
    • PrintCompositor

      public PrintCompositor(Buffer buffer)
      Creates a new print compositor that can be used to print buffer.
      Parameters:
      buffer - the GtkSourceBuffer to print.
    • PrintCompositor

      public PrintCompositor()
      Create a new PrintCompositor.
  • Method Details

    • getType

      public static @Nullable Type getType()
      Get the GType of the PrintCompositor class.
      Returns:
      the GType
    • getMemoryLayout

      public static MemoryLayout getMemoryLayout()
      The memory layout of the native struct.
      Returns:
      the memory layout
    • asParent

      protected PrintCompositor asParent()
      Return this instance as if it were its parent type. Comparable to the Java super keyword, but ensures the parent typeclass is also used in native code.
      Overrides:
      asParent in class GObject
      Returns:
      the instance as if it were its parent type
    • fromView

      public static PrintCompositor fromView(View view)

      Creates a new print compositor that can be used to print the buffer associated with view.

      This constructor sets some configuration properties to make the printed output match view as much as possible. The properties set are PrintCompositor:tab-width, PrintCompositor:highlight-syntax, PrintCompositor:wrap-mode, PrintCompositor:body-font-name and PrintCompositor:print-line-numbers.

      Parameters:
      view - a GtkSourceView to get configuration from.
      Returns:
      a new print compositor object.
    • drawPage

      public void drawPage(PrintContext context, int pageNr)

      Draw page pageNr for printing on the the Cairo context encapsuled in context.

      This method has been designed to be called in the handler of the Gtk.PrintOperation::draw_page signal as shown in the following example:

      // Signal handler for the GtkPrintOperation::draw_page signal
      
      static void
      draw_page (GtkPrintOperation *operation,
                 GtkPrintContext   *context,
                 gint               page_nr,
                 gpointer           user_data)
      {
          GtkSourcePrintCompositor *compositor;
      
          compositor = GTK_SOURCE_PRINT_COMPOSITOR (user_data);
      
          gtk_source_print_compositor_draw_page (compositor,
                                                 context,
                                                 page_nr);
      }
      
      def on_draw_page(
          operation: Gtk.PrintOperation,
          context: Gtk.PrintContext,
          page_nr: int,
          compositor: GtkSource.PrintCompositor,
      ) -> None:
          """Signal handler for draw-page that renders a single page."""
          compositor.draw_page(context=context, page_nr=page_nr)
      
      Parameters:
      context - the GtkPrintContext encapsulating the context information that is required when drawing the page for printing.
      pageNr - the number of the page to print.
    • getBodyFontName

      public String getBodyFontName()

      Returns the name of the font used to print the text body.

      The returned string must be freed with g_free().

      Returns:
      a new string containing the name of the font used to print the text body.
    • getBottomMargin

      public double getBottomMargin(Unit unit)
      Gets the bottom margin in units of unit.
      Parameters:
      unit - the unit for the return value.
      Returns:
      the bottom margin.
    • getBuffer

      public Buffer getBuffer()

      Gets the Buffer associated with the compositor.

      The returned object reference is owned by the compositor object and should not be unreferenced.

      Returns:
      the GtkSourceBuffer associated with the compositor.
    • getFooterFontName

      public String getFooterFontName()

      Returns the name of the font used to print the page footer.

      The returned string must be freed with g_free().

      Returns:
      a new string containing the name of the font used to print the page footer.
    • getHeaderFontName

      public String getHeaderFontName()

      Returns the name of the font used to print the page header.

      The returned string must be freed with g_free().

      Returns:
      a new string containing the name of the font used to print the page header.
    • getHighlightSyntax

      public boolean getHighlightSyntax()

      Determines whether the printed text will be highlighted according to the buffer rules.

      Note that highlighting will happen only if the buffer to print has highlighting activated.

      Returns:
      true if the printed output will be highlighted.
    • getLeftMargin

      public double getLeftMargin(Unit unit)
      Gets the left margin in units of unit.
      Parameters:
      unit - the unit for the return value.
      Returns:
      the left margin
    • getLineNumbersFontName

      public String getLineNumbersFontName()

      Returns the name of the font used to print line numbers on the left margin.

      The returned string must be freed with g_free().

      Returns:
      a new string containing the name of the font used to print line numbers on the left margin.
    • getNPages

      public int getNPages()
      Returns the number of pages in the document or -1 if the document has not been completely paginated.
      Returns:
      the number of pages in the document or -1 if the document has not been completely paginated.
    • getPaginationProgress

      public double getPaginationProgress()
      Returns the current fraction of the document pagination that has been completed.
      Returns:
      a fraction from 0.0 to 1.0 inclusive.
    • getPrintFooter

      public boolean getPrintFooter()

      Determines if a footer is set to be printed for each page.

      A footer will be printed if this function returns true and some format strings have been specified with setFooterFormat(boolean, String, String, String).

      Returns:
      true if the footer is set to be printed.
    • getPrintHeader

      public boolean getPrintHeader()

      Determines if a header is set to be printed for each page.

      A header will be printed if this function returns true and some format strings have been specified with setHeaderFormat(boolean, String, String, String).

      Returns:
      true if the header is set to be printed.
    • getPrintLineNumbers

      public int getPrintLineNumbers()

      Returns the interval used for line number printing.

      If the value is 0, no line numbers will be printed. The default value is 1 (i.e. numbers printed in all lines).

      Returns:
      the interval of printed line numbers.
    • getRightMargin

      public double getRightMargin(Unit unit)
      Gets the right margin in units of unit.
      Parameters:
      unit - the unit for the return value.
      Returns:
      the right margin.
    • getTabWidth

      public int getTabWidth()
      Returns the width of tabulation in characters for printed text.
      Returns:
      width of tab.
    • getTopMargin

      public double getTopMargin(Unit unit)
      Gets the top margin in units of unit.
      Parameters:
      unit - the unit for the return value.
      Returns:
      the top margin.
    • getWrapMode

      public WrapMode getWrapMode()
      Gets the line wrapping mode for the printed text.
      Returns:
      the line wrap mode.
    • ignoreTag

      public void ignoreTag(TextTag tag)
      Specifies a tag whose style should be ignored when compositing the document to the printable page.
      Parameters:
      tag - a GtkTextTag
      Since:
      5.2
    • paginate

      public boolean paginate(PrintContext context)

      Paginate the document associated with the compositor.

      In order to support non-blocking pagination, document is paginated in small chunks. Each time paginate(PrintContext) is invoked, a chunk of the document is paginated. To paginate the entire document, paginate(PrintContext) must be invoked multiple times. It returns true if the document has been completely paginated, otherwise it returns false.

      This method has been designed to be invoked in the handler of the Gtk.PrintOperation::paginate signal, as shown in the following example:

      // Signal handler for the GtkPrintOperation::paginate signal
      
      static gboolean
      paginate (GtkPrintOperation *operation,
                GtkPrintContext   *context,
                gpointer           user_data)
      {
          GtkSourcePrintCompositor *compositor;
      
          compositor = GTK_SOURCE_PRINT_COMPOSITOR (user_data);
      
          if (gtk_source_print_compositor_paginate (compositor, context))
          {
              gint n_pages;
      
              n_pages = gtk_source_print_compositor_get_n_pages (compositor);
              gtk_print_operation_set_n_pages (operation, n_pages);
      
              return TRUE;
          }
      
          return FALSE;
      }
      
      def on_paginate(
          operation: Gtk.PrintOperation,
          context: Gtk.PrintContext,
          compositor: GtkSource.PrintCompositor,
      ) -> bool:
          if compositor.paginate(context=context):
              n_pages = compositor.get_n_pages()
              operation.set_n_pages(n_pages=n_pages)
              return True
          return False
      

      If you don't need to do pagination in chunks, you can simply do it all in the Gtk.PrintOperation::begin-print handler, and set the number of pages from there, like in the following example:

      // Signal handler for the GtkPrintOperation::begin-print signal
      
      static void
      begin_print (GtkPrintOperation *operation,
                   GtkPrintContext   *context,
                   gpointer           user_data)
      {
          GtkSourcePrintCompositor *compositor;
          gint n_pages;
      
          compositor = GTK_SOURCE_PRINT_COMPOSITOR (user_data);
      
          while (!gtk_source_print_compositor_paginate (compositor, context));
      
          n_pages = gtk_source_print_compositor_get_n_pages (compositor);
          gtk_print_operation_set_n_pages (operation, n_pages);
      }
      
      def on_begin_print(
          operation: Gtk.PrintOperation,
          context: Gtk.PrintContext,
          compositor: GtkSource.PrintCompositor,
      ) -> None:
          # Paginate until complete
          while not compositor.paginate(context=context):
              pass
      
          n_pages = compositor.get_n_pages()
          operation.set_n_pages(n_pages=n_pages)
      
      Parameters:
      context - the GtkPrintContext whose parameters (e.g. paper size, print margins, etc.) are used by the the this PrintCompositor to paginate the document.
      Returns:
      true if the document has been completely paginated, false otherwise.
    • setBodyFontName

      public void setBodyFontName(String fontName)

      Sets the default font for the printed text.

      fontName should be a string representation of a font description Pango can understand. (e.g. "Monospace 10"). See FontDescription#fromString for a description of the format of the string representation.

      This function cannot be called anymore after the first call to the paginate(PrintContext) function.

      Parameters:
      fontName - the name of the default font for the body text.
    • setBottomMargin

      public void setBottomMargin(double margin, Unit unit)
      Sets the bottom margin used by compositor.
      Parameters:
      margin - the new bottom margin in units of unit.
      unit - the units for margin.
    • setFooterFontName

      public void setFooterFontName(@Nullable String fontName)

      Sets the font for printing the page footer.

      If null is supplied, the default font (i.e. the one being used for the text) will be used instead.

      fontName should be a string representation of a font description Pango can understand. (e.g. "Monospace 10"). See FontDescription#fromString for a description of the format of the string representation.

      This function cannot be called anymore after the first call to the paginate(PrintContext) function.

      Parameters:
      fontName - the name of the font for the footer text, or null.
    • setFooterFormat

      public void setFooterFormat(boolean separator, @Nullable String left, @Nullable String center, @Nullable String right)
      See setHeaderFormat(boolean, String, String, String) for more information about the parameters.
      Parameters:
      separator - true if you want a separator line to be printed.
      left - a format string to print on the left of the footer.
      center - a format string to print on the center of the footer.
      right - a format string to print on the right of the footer.
    • setHeaderFontName

      public void setHeaderFontName(@Nullable String fontName)

      Sets the font for printing the page header.

      If null is supplied, the default font (i.e. the one being used for the text) will be used instead.

      fontName should be a string representation of a font description Pango can understand. (e.g. "Monospace 10"). See FontDescription#fromString for a description of the format of the string representation.

      This function cannot be called anymore after the first call to the paginate(PrintContext) function.

      Parameters:
      fontName - the name of the font for header text, or null.
    • setHeaderFormat

      public void setHeaderFormat(boolean separator, @Nullable String left, @Nullable String center, @Nullable String right)

      Sets strftime like header format strings, to be printed on the left, center and right of the top of each page.

      The strings may include strftime(3) codes which will be expanded at print time. A subset of strftime() codes are accepted, see DateTime#format for more details on the accepted format specifiers. Additionally the following format specifiers are accepted:

      • N: the page number
      • Q: the page count.

      separator specifies if a solid line should be drawn to separate the header from the document text.

      If null is given for any of the three arguments, that particular string will not be printed.

      For the header to be printed, in addition to specifying format strings, you need to enable header printing with setPrintHeader(boolean).

      This function cannot be called anymore after the first call to the paginate(PrintContext) function.

      Parameters:
      separator - true if you want a separator line to be printed.
      left - a format string to print on the left of the header.
      center - a format string to print on the center of the header.
      right - a format string to print on the right of the header.
    • setHighlightSyntax

      public void setHighlightSyntax(boolean highlight)

      Sets whether the printed text will be highlighted according to the buffer rules. Both color and font style are applied.

      This function cannot be called anymore after the first call to the paginate(PrintContext) function.

      Parameters:
      highlight - whether syntax should be highlighted.
    • setLeftMargin

      public void setLeftMargin(double margin, Unit unit)
      Sets the left margin used by compositor.
      Parameters:
      margin - the new left margin in units of unit.
      unit - the units for margin.
    • setLineNumbersFontName

      public void setLineNumbersFontName(@Nullable String fontName)

      Sets the font for printing line numbers on the left margin.

      If null is supplied, the default font (i.e. the one being used for the text) will be used instead.

      fontName should be a string representation of a font description Pango can understand. (e.g. "Monospace 10"). See FontDescription#fromString for a description of the format of the string representation.

      This function cannot be called anymore after the first call to the paginate(PrintContext) function.

      Parameters:
      fontName - the name of the font for line numbers, or null.
    • setPrintFooter

      public void setPrintFooter(boolean print)

      Sets whether you want to print a footer in each page.

      The footer consists of three pieces of text and an optional line separator, configurable with setFooterFormat(boolean, String, String, String).

      Note that by default the footer format is unspecified, and if it's empty it will not be printed, regardless of this setting.

      This function cannot be called anymore after the first call to the paginate(PrintContext) function.

      Parameters:
      print - true if you want the footer to be printed.
    • setPrintHeader

      public void setPrintHeader(boolean print)

      Sets whether you want to print a header in each page.

      The header consists of three pieces of text and an optional line separator, configurable with setHeaderFormat(boolean, String, String, String).

      Note that by default the header format is unspecified, and if it's empty it will not be printed, regardless of this setting.

      This function cannot be called anymore after the first call to the paginate(PrintContext) function.

      Parameters:
      print - true if you want the header to be printed.
    • setPrintLineNumbers

      public void setPrintLineNumbers(int interval)

      Sets the interval for printed line numbers.

      If interval is 0 no numbers will be printed. If greater than 0, a number will be printed every interval lines (i.e. 1 will print all line numbers).

      Maximum accepted value for interval is 100.

      This function cannot be called anymore after the first call to the paginate(PrintContext) function.

      Parameters:
      interval - interval for printed line numbers.
    • setRightMargin

      public void setRightMargin(double margin, Unit unit)
      Sets the right margin used by compositor.
      Parameters:
      margin - the new right margin in units of unit.
      unit - the units for margin.
    • setTabWidth

      public void setTabWidth(int width)

      Sets the width of tabulation in characters for printed text.

      This function cannot be called anymore after the first call to the paginate(PrintContext) function.

      Parameters:
      width - width of tab in characters.
    • setTopMargin

      public void setTopMargin(double margin, Unit unit)
      Sets the top margin used by compositor.
      Parameters:
      margin - the new top margin in units of unit
      unit - the units for margin
    • setWrapMode

      public void setWrapMode(WrapMode wrapMode)

      Sets the line wrapping mode for the printed text.

      This function cannot be called anymore after the first call to the paginate(PrintContext) function.

      Parameters:
      wrapMode - a GtkWrapMode.
    • builder

      public static PrintCompositor.Builder<? extends PrintCompositor.Builder> builder()
      A PrintCompositor.Builder object constructs a PrintCompositor with the specified properties. Use the various set...() methods to set properties, and finish construction with PrintCompositor.Builder.build().
      Returns:
      the builder object