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

2017-10-15 Thread Hammer Attila

Hi Eric,

This test code me works correctly too if keeping the line numbers each 
blank lines.
If have a number before a simple \n line, the correct line positioning 
happening when I trying convert back the generated pdf file with 
pdftotext -layout command.
The last line of the first page right positioned the 28TH line (line 
number is real 28 in the txt file), but before the 1 number have only 7 
space characters, not 32 space characters.


If I remove the line numbers before the \n characters, the first page 
last line again not right positioned the txt file when I ran the 
pdftotext -layout pdf file command (real line number now 6, and before 
the 1 number have 9 space characters, not 32).
I tried replacing the blank lines with simple space characters, but this 
is not help of course.


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?

2017-10-15 Thread Eric Cashon via gtk-app-devel-list

 

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()

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

2017-10-15 Thread Hammer Attila

Hi,

I think the last post described exactly what the problem.
I using the pdftotext test only because me not have a Braille printer 
hardware, and any way need verifying the real printed output with equals 
exactly the LiblouisUTDML file2brl command generated formatted text 
braille output file when the printing real happening, character to 
character.


Me have a visual printer, but this verification is not possible, because 
I am a blind person.


With Braille printers insensitive the visual lookup the input text, only 
important when the draw_page function sends the printed output to the 
user selected Braille printer, the already generated text formattings is 
not lost during page drawing (space characters doed indentations, blank 
lines when more blank lines have a page, etc).


In the begin-print signal possible calculating how many Braille pages 
fit the document, simple need divmod the document line numbers with 
lines/page property.


For example, if a document have 29 braille lines, and lines/page 
property is 28 lines, need generating two pages in the begin-print 
signal function the operation.set_n_pages(2) function call.

Me this calculation always estimated right the braille pages count.

Possible in the draving operation need moving the pen the next line if 
have a blank line? If the text more blank lines, possible doing more 
move operations only to move the real paper with the correct position 
when the printing happening a real hardware?


When page draving is happening, Not possible need processing the page 
layout content with layoutLine to layoutLine object, and if the 
layoutLine object text is blank, need moving the pen the next line left 
margin position to resulting a real blank line before the next paragraph?
Left, top, bottom and right margin positions possible querying the 
print_context.get_page_setup() sub functions.


I begin thinking with Pango Layout or CairoContext.show_layout function 
insensitive the \n\n\n\n formatting style text.


When you need drawing a blank line a page, what need you doing the 
draw_page signal?


I not full understanding the line and character positions calculations 
when I need give exactly a line number and a column position coordinates.


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?

2017-10-15 Thread Yuri Khan
On Sun, Oct 15, 2017 at 1:42 AM, Eric Cashon via gtk-app-devel-list
 wrote:

> The first thing that I would try out is to change the font that is being 
> drawn to see if that works.

I am getting an impression that fonts are not going to make any
difference. The OP seems not interested in how the PDF *looks* at all;
rather, what it *means*.

Disclaimer: I am not an expert on the PDF format so the below may not
be completely accurate. Take it as a simplified description.

Disclaimer #2: I have not verified the original claim of what appears on output.


A PDF page is, essentially, a list of instructions of the form “at
position (X, Y), using the font F, render the string S”.

On input, the OP is feeding a text file with lots of blank lines and
spaces used for layout.

On output, the PDF seems to only contain instructions to render the
non-blank lines. pdftotext(1) without the -layout option simply
outputs the text from the instructions present in the PDF.

(pdftotext -layout interprets the instructions and attempts to
reconstruct page layout based on positions.)

For a sighted person, there is no difference in appearance between this PDF:

At 0 inches down, 0 inches across, render "alma"
At 4.667 inches down, 3.1 inches across, render "1"

and this PDF:

At 0 inches down, 0 inches across, render "alma"
At 0.167 inches down, 0 inches across, render ""
At 0.333 inches down, 0 inches across, render ""
…
At 4.5 inches down, 0 inches across render, ""
At 4.667 inches down, 0 inches across render "<31 spaces>1"

For a blind user and/or a third-party tool aimed at blind users, there
seems to be.
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list