GtkSource.PrintCompositor: How to possible printing plain text documents without lost blank lines and indentations?
Dear list, Possible you have an ydea with following problem: I have a little Python3 code part with uses GtkSource.View and GtkSource.PrintCompositor to print native text file content with Gtk.Printoperation class. The example code is following: #!/usr/bin/env python3 import gi gi.require_version('Gtk', '3.0') gi.require_version('GtkSource', '3.0') from gi.repository import Gtk, GtkSource, Gio, GLib def begin_printing(operation, context, compositor): while not compositor.paginate(context): pass n_pages = compositor.get_n_pages() operation.set_n_pages(n_pages) def draw_page(operation, context, page_nr, compositor): compositor.draw_page(context, page_nr) view=GtkSource.View() filename = GtkSource.File() filename.set_location(Gio.File.new_for_path('test.txt')) fileloader= GtkSource.FileLoader.new(view.get_buffer(), filename) fileloader.load_async(GLib.PRIORITY_LOW, None, None, None, None, filename) printop=Gtk.PrintOperation() printcompositor=GtkSource.PrintCompositor.new_from_view(view) printop.connect('begin-print', begin_printing, printcompositor) printop.connect('draw-page', draw_page, printcompositor) printop.run(Gtk.PrintOperationAction.PRINT_DIALOG, None) The test.txt file is very simple, but important to keep entire content when the printing is happening the begin_printing and draw_page code parts: The first line have two spaces and the hungarian alma word. After this line have 26 blank lines (this is need to keeping future). The 28 TH line have 31 space characters, and the 32. column have an 1 number. This is a typical braille document similar content. What happening when I running this example code and generating a .pdf file document? The logic answer is the printed file content is not changed in the PDF file (I think this previous). But, not this is the case. If I my Ubuntu system running for example with pdftotext -layout test.pdf command, the converted text file result is following: "alma 1" The -layout switch tells pdftotext converter to keep original layout. If I running pdftotext test.pdf command, the converted text file containing following text: "alma 1" Unfortunatelly I only this way possible testing printed output, because I not see entire the screen, I using Ubuntu system with the Orca Screen Reader. The interesting bonus thing: If I turning on the line number printing related property before paginate and set line numbering related property with 1, everi blank line inserted with the printed output. I experiencing similar working method with Gedit text editor with this example text. I tried debugging the begin_printing code part with the printcompositor buffer inserted text. The text is equals my original text file content. I haven't got another idea to how can possible keeping the original content when GtkSource Print compositor drawing the printed pages. I tryed replacing GtkSource.PrintCompositor related code part with general Cairo and Pangolayout related codes, without success. In the Pangolayout.get_text() code returns the original content, but the printed .pdf file dropped lot of blank lines. Why important this? I working a hungarian language specific application with converts from normal documents to braille documents using with Liblouis UTDML tool. Everithing working right, except the printing. So, when the printing is happening, need sending the printer the generated raw braille content without format loss from the GTKSource.PrintCompositor buffer. Have chance to I forgot a very simple think. I already readed lot of GTK documentation with GtkPrintOperation printing related, but not yet founding a solution this issue related. I attaching the example code, the test.txt file and a generated .pdf document with I created from the GtkPrintoperation print dialog. Kind regards, Attila Hammer alma 1 ___ gtk-app-devel-list mailing list gtk-app-devel-list@gnome.org https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
GtkSource.PrintCompositor: How to possible printing plain text documents without lost blank lines and indentations?
Dear list, Possible you have an ydea with following problem: I have a little Python3 code part with uses GtkSource.View and GtkSource.PrintCompositor to print native text file content with Gtk.Printoperation class. The example code is following: #!/usr/bin/env python3 import gi gi.require_version('Gtk', '3.0') gi.require_version('GtkSource', '3.0') from gi.repository import Gtk, GtkSource, Gio, GLib def begin_printing(operation, context, compositor): while not compositor.paginate(context): pass n_pages = compositor.get_n_pages() operation.set_n_pages(n_pages) def draw_page(operation, context, page_nr, compositor): compositor.draw_page(context, page_nr) view=GtkSource.View() filename = GtkSource.File() filename.set_location(Gio.File.new_for_path('test.txt')) fileloader= GtkSource.FileLoader.new(view.get_buffer(), filename) fileloader.load_async(GLib.PRIORITY_LOW, None, None, None, None, filename) printop=Gtk.PrintOperation() printcompositor=GtkSource.PrintCompositor.new_from_view(view) printop.connect('begin-print', begin_printing, printcompositor) printop.connect('draw-page', draw_page, printcompositor) printop.run(Gtk.PrintOperationAction.PRINT_DIALOG, None) The test.txt file is very simple, but important to keep entire content when the printing is happening the begin_printing and draw_page code parts: The first line have two spaces and the hungarian alma word. After this line have 26 blank lines (this is need to keeping future). The 28 TH line have 31 space characters, and the 32. column have an 1 number. This is a typical braille document similar content. What happening when I running this example code and generating a .pdf file document? The logic answer is the printed file content is not changed in the PDF file (I think this previous). But, not this is the case. If I my Ubuntu system running for example with pdftotext -layout test.pdf command, the converted text file result is following: "alma 1" The -layout switch tells pdftotext converter to keep original layout. If I running pdftotext test.pdf command, the converted text file containing following text: "alma 1" Unfortunatelly I only this way possible testing printed output, because I not see entire the screen, I using Ubuntu system with the Orca Screen Reader. The interesting bonus thing: If I turning on the line number printing related property before paginate and set line numbering related property with 1, everi blank line inserted with the printed output. I experiencing similar working method with Gedit text editor with this example text. I tried debugging the begin_printing code part with the printcompositor buffer inserted text. The text is equals my original text file content. I haven't got another idea to how can possible keeping the original content when GtkSource Print compositor drawing the printed pages. I tryed replacing GtkSource.PrintCompositor related code part with general Cairo and Pangolayout related codes, without success. In the Pangolayout.get_text() code returns the original content, but the printed .pdf file dropped lot of blank lines. Why important this? I working a hungarian language specific application with converts from normal documents to braille documents using with Liblouis UTDML tool. Everithing working right, except the printing. So, when the printing is happening, need sending the printer the generated raw braille content without format loss from the GTKSource.PrintCompositor buffer. Have chance to I forgot a very simple think. I already readed lot of GTK documentation with GtkPrintOperation printing related, but not yet founding a solution this issue related. I attaching the example code, the test.txt file and a generated .pdf document with I created from the GtkPrintoperation print dialog. Kind regards, Attila Hammer alma 1 ___ gtk-app-devel-list mailing list gtk-app-devel-list@gnome.org https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Re: GtkSource.PrintCompositor: How to possible printing plain text documents without lost blank lines and indentations?
Hi List, Sorry, but unfortunatelly duplicated sent my letter. When first time sent my letter, longer time later I not seeing my post this list. Attila ___ gtk-app-devel-list mailing list gtk-app-devel-list@gnome.org https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Re: GtkSource.PrintCompositor: How to possible printing plain text documents without lost blank lines and indentations?
Hi Attila, You have a print dialog and click on "Print to File" to save the contents to a PDF file and that file is not being formatted correctly and isn't the same as what is in the print preview? I tried a small test program. It prints well to PDF with the extra lines. Does this program work OK or does it cause the same problem. 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.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(" alma\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n 1") def print_dialog(self): operation = Gtk.PrintOperation() #Just print 1 page. operation.set_n_pages(1) 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() self.pango_layout = gtk_context.create_pango_layout() self.pango_layout.set_font_description(description) self.pango_layout.set_width(int(self.page_width*Pango.SCALE)); self.pango_layout.set_wrap(Pango.WrapMode.CHAR) def draw_page(self, operation, gtk_context, page_number): cr = gtk_context.get_cairo_context() #Draw a rectangle. cr.set_source_rgb(1.0, 0.0, 1.0) cr.set_line_width(5) cr.rectangle(40, 40, self.page_width-80, self.page_height-500) cr.stroke() #Show Text. cr.set_source_rgb(0.0, 0.0, 0.0) start = self.textbuffer.get_start_iter() end = self.textbuffer.get_end_iter() 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() ___ gtk-app-devel-list mailing list gtk-app-devel-list@gnome.org https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list