Re: GtkSource.PrintCompositor: How to possible printing plain text documents without lost blank lines and indentations?




 

OK, you got me on the PDF. I don't know about that. I think a monospace font will help though because they 
are easier to keep track of rows and columns since all the characters are in the same sized rectangle. The 
Marburg font is also in a constant sized rectangle. I don't know how to put together a layout with the two  
different fonts sized correctly but I am sure it can be done to fit on a A4 page and have everything work 
correctly. 

This is my latest try at it. It has the A4 page size with a monospace font that will fit at 28 rows per page. 
Maybe it is a step in the right direction.

Eric

#!/usr/bin/env python3

#Needed for cairo context: sudo apt-get install python-gi-cairo
#Tested on Ubuntu16.04 with GTK3.18 and Python3.

import gi
gi.require_version('Gtk', '3.0')
gi.require_version('GtkSource', '3.0')
gi.require_version('PangoCairo', '1.0')
from gi.repository import Gtk, GtkSource, PangoCairo, Pango
import math

class TextBox(GtkSource.View):
    def __init__(self, win):
        GtkSource.View.__init__(self)
        self.parent_win = win
        self.page_width = 0
        self.page_height = 0
        self.lines = 0
        self.font_width = 0
        self.font_height = 0
        self.lines_per_page = 0
        self.set_wrap_mode(1)
        self.set_cursor_visible(True)
        self.set_vexpand(True);
        self.set_hexpand(True);
        self.textbuffer = self.get_buffer() 
        self.textbuffer.set_text("1 
alma\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n25\n26\n27\n28      
                       1\n1 
alma2\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n25\n26\n27\n28     
                        1")

    def print_dialog(self):
        operation = Gtk.PrintOperation()
        page_setup = Gtk.PageSetup()
        #Try the A4 paper size. 
        paper_size = Gtk.PaperSize("iso_a4")
        page_setup.set_paper_size(paper_size)
        operation.set_default_page_setup(page_setup)
        operation.connect("begin_print", self.begin_print)
        operation.connect("draw_page", self.draw_page)
        result = operation.run(Gtk.PrintOperationAction.PRINT_DIALOG, self.parent_win)

    def begin_print(self, operation, gtk_context):
        self.page_width = gtk_context.get_width()
        self.page_height = gtk_context.get_height()
        pango_context = self.get_pango_context()
        description = pango_context.get_font_description()
        #Set a monospace font. Easier for figuring out layouts.
        new_font = Pango.FontDescription("Monospace 24")
        self.pango_layout = gtk_context.create_pango_layout()
        self.pango_layout.set_font_description(new_font)
        self.pango_layout.set_width(int(self.page_width*Pango.SCALE));
        self.pango_layout.set_wrap(Pango.WrapMode.CHAR)

        #Get font width and height for a monospace font.
        self.pango_layout.set_markup("5")
        rectangle_ink, rectangle_log = self.pango_layout.get_extents()
        self.font_width = rectangle_log.width/Pango.SCALE
        self.font_height = rectangle_log.height/Pango.SCALE

        #Calculate lines per page. 28 lines of monspace 24 font fit on a A4 one page. 
        self.lines = self.textbuffer.get_line_count() 
        self.lines_per_page = int(self.page_height / self.font_height)
        operation.set_n_pages(math.ceil(self.lines / self.lines_per_page))

    def draw_page(self, operation, gtk_context, page_number):
        cr = gtk_context.get_cairo_context()
       
        #Draw rectangles around monospaced text.
        cr.set_source_rgb(1.0, 0.0, 1.0)
        cr.set_line_width(1)
        for x in range(28): 
            for y in range(32):
                cr.rectangle(y * self.font_width, x * self.font_height, self.font_width, self.font_height)
                cr.stroke()

        #Page border rectangle.
        cr.set_source_rgb(0.0, 0.0, 1.0)
        cr.set_line_width(2)
        cr.rectangle(0, 0, self.page_width, self.page_height)
        cr.stroke()
          
        #Get the lines of text to put on the page.
        cr.set_source_rgb(0.0, 0.0, 0.0)
        line_offset = page_number * self.lines_per_page
        start = self.textbuffer.get_iter_at_line(line_offset)
        end = self.textbuffer.get_iter_at_line(line_offset + self.lines_per_page - 1)
        end.forward_to_line_end()
        string = self.textbuffer.get_text(start, end, False)
        self.pango_layout.set_markup(string)
        PangoCairo.show_layout(cr, self.pango_layout)

class MainWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self)
        self.set_title("Print")
        self.set_default_size(300,700)
        self.TextBox1 = TextBox(self)
        self.scrolledwindow = Gtk.ScrolledWindow()
        self.scrolledwindow.add(self.TextBox1)
        self.button1 = Gtk.Button("Print Dialog")
        self.button1.connect("clicked", self.print_dialog)
        self.grid = Gtk.Grid()
        self.grid.attach(self.scrolledwindow, 0, 0, 4, 4)
        self.grid.attach(self.button1, 0, 5, 4, 1)
        self.add(self.grid)

    def print_dialog(self, button1):
        self.TextBox1.print_dialog()

win = MainWindow()
win.connect("delete-event", Gtk.main_quit) 
win.show_all()
Gtk.main()




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]