Hi to all,
I'm replying to myself after a night spent on gnomeprint I've found some
answers:
-the basic method for print string with gnomeprint is to use the show()
method of the GnomePrintContext object. First you have to set the cursor
position using the move() method (the origin is on the bottom-left
corner) and than you can show your string. if you want to know width and
height of the string you can use the method get_width_utf8("the string")
of object GnomePrintFont. For height info is more obscure; I'm using
this but I don't know if there is a better solution:
font_height = font.get_ascender() + font.get_descender()
where font is a GnomePrintFont.
-there is no easy way for print the contens of a TextBuffer according
with tags. I've looked on some C sources of gtkSourceView project
assuming that they know very well gnomeprint internals, (at least better
than me :-) and the procedure they use is the following:
basically for every chars in a line, you must look at its tags, take the
one with highest priority and set the font according with the tag (i.e.
weight foreground color, ). As said before you have to do this for every
char.
Once again I'm not sure this is the only solution (or the best solution)
because I'm not an expert of gnomeprint. So if someone has more infos,
please share with us! :-)
I've attached an example and I hope that it can be useful to someone
else; just a few words about it. It is inspired by the examples of
Gustavo Carneiro (great work Gustavo!), you can grab it on gnome CVS.
You start the example passing a file to print. Please note that the
program save your print configuration on a file "conf" so you can have
persistence of your settings over executions. If you make a selection
into the TextView the gnomeprint Dialog will add the "print selection"
option. You can enable/disable the line_number on output and you can
change its step (change the self.print_line_number and
self.line_number_step on __init__ method). If the line is too long, the
program simply truncate it (and put a small arrow to indicate it). This
is ok for my needs but probably not for your, so change it...
I hope this can help!
Regards
Pier
Il mer, 2003-11-26 alle 17:18, Fernando San MartÃn W. ha scritto:
> El miÃ, 26-11-2003 a las 12:51, Pier Carteri escribiÃ:
> > Hello!
> > I'm trying to print the contens of a gtkTextBuffer using gnomeprint, and
> > I've some questions:
> > -I'm using the show() function to print the line and I would like to
> > know how to wrap the line if it's too long; more precisely, how can I
> > have infos about the width and height of the string I'm showing?
> >
> > -Are there any easy way to print text according with the tags into the
> > textBuffer (at least for foreground-color, weight and style properties)?
> > Thanks to all!
> >
>
> have you any code to show, i'm trying to do the same!
>
> regards
--
Pier Carteri <[EMAIL PROTECTED]>
import pygtk; pygtk.require("2.0")
import gtk, gnome, gnomeprint, gnomeprint.ui
import time,os
CONFIG_FILE_NAME="conf"
class PrintAssistant:
def __init__(self, source, file_name):
self.source = source
self.file_name = file_name
self.config = self.load_config()
self.job = gnomeprint.Job(self.config)
self.print_line_number = gtk.TRUE
self.line_number_step = 5
self.print_range = "all"
def show_print_dialog(self):
dlg = gnomeprint.ui.Dialog(self.job, "Print dialog",
gnomeprint.ui.DIALOG_RANGE|
gnomeprint.ui.DIALOG_COPIES)
#check if there is a selection into the current buffer
if len(self.source.get_selection_bounds())>0:
dlg.construct_range_page(gnomeprint.ui.RANGE_ALL|
gnomeprint.ui.RANGE_SELECTION,
1, 1, "A", "Pages")
else:
dlg.construct_range_page(gnomeprint.ui.RANGE_ALL,
1, 1, "A", "Pages")
dlg.connect("response", self.actions)
dlg.show()
def actions(self, dialog, ans):
if ans == gnomeprint.ui.DIALOG_RESPONSE_CANCEL:
self.quit(dialog)
if dialog.get_range() == gnomeprint.ui.RANGE_SELECTION:
self.print_range = "selection"
if ans == gnomeprint.ui.DIALOG_RESPONSE_PRINT:
self.start_print()
if ans == gnomeprint.ui.DIALOG_RESPONSE_PREVIEW:
self._show_preview(dialog)
def render_text_buffer(self):
"""Read information from a TextBuffer and create a PrintJob
"""
width, height = gnomeprint.job_get_page_size_from_config(self.config)
# obtain font infos
font = gnomeprint.font_find_closest("Bitstream Charter Regular", 12)
font_height = font.get_ascender() + font.get_descender()
line_spacing = font.get_size() * 1.2
if self.print_line_number:
nbr_font=gnomeprint.font_find_closest("Monospace", 8)
if self.print_range == "selection":
s,e =self.source.get_selection_bounds()
else:
s,e =self.source.get_bounds()
text=self.source.get_text(s,e, gtk.FALSE)
start_line_number=s.get_line()
text_line = 0
lines= text.split("\n")
pc = self.job.get_context()
cur_page=1
go_on = gtk.TRUE
while go_on:
pc.beginpage(str(cur_page))
#draw header footer
self.draw_header(pc, self.file_name,
cur_page, width/10, height *9/10,
width * 8/10, 50)
self.draw_footer(pc, self.file_name,
cur_page, width/10, height/10,
width * 8/10, 50)
#reset the font
pc.setfont(font)
cur_y_pos= (height * 9/10) -20
if self.print_line_number:
cur_x_pos= (width/10) + 30 #XXX fix it
else:
cur_x_pos= (width/10)
#start draw text into page
while cur_y_pos > (height/10 +10):
if (self.print_line_number and
text_line % self.line_number_step == 0): # draw line number
nbr="%d"%(start_line_number + text_line +1)
nbr_width = font.get_width_utf8(nbr)
offset = nbr_width + 5
pc.moveto(cur_x_pos - offset, cur_y_pos)
pc.setfont(nbr_font)
pc.show(nbr)
pc.setfont(font)
pc.moveto(cur_x_pos, cur_y_pos)
line = lines[text_line]
# check line length
line_width = font.get_width_utf8(line)
printable_line_length =(width * 9/10) - cur_x_pos
truncated = gtk.FALSE
while line_width > printable_line_length:
#truncate line
truncated = gtk.TRUE
line = line[:-1]
line_width = font.get_width_utf8(line)
pc.show(line)
if truncated:
#put somethings to indicate that the line was truncated
#draw an arrow...
pc.line_stroked(cur_x_pos+printable_line_length,
cur_y_pos+3,
cur_x_pos+printable_line_length + 15,
cur_y_pos+3)
pc.line_stroked(cur_x_pos+printable_line_length + 9,
cur_y_pos+8,
cur_x_pos+printable_line_length + 15,
cur_y_pos+3)
pc.line_stroked(cur_x_pos+printable_line_length + 9,
cur_y_pos-2,
cur_x_pos+printable_line_length + 15,
cur_y_pos+3)
cur_y_pos -= (font_height + line_spacing)
text_line += 1
if text_line == len(lines):
go_on = gtk.FALSE
break
pc.showpage()
cur_page += 1
#end drawing
self.job.close()
def start_print(self):
self.render_text_buffer()
#print it
self.job.print_()
def _show_preview(self, dlg=None):
"""Show a preview of the current Job.
Internal use only. Use it with the GnomePrintDialog
"""
self.render_text_buffer()
win = gnomeprint.ui.JobPreview(self.job, "Print preview...")
win.set_property("allow-grow", 1)
win.set_property("allow-shrink", 1)
if dlg:
dlg.hide()
win.show_all()
def draw_header(self, pc, file_name, page_num, x, y, w, h):
"""Draw an header for the current page.
The header will be within a rectangle (x, y, w, h)
pc = the gnome print context
file_name = a string indicating the name of the file
page_num = the number to print
"""
font= gnomeprint.font_find_closest("Sans Regular", 14)
pc.setfont(font)
pc.setlinewidth(1.0)
pc.rect_stroked(x, y, w, h)
pc.moveto(x+(w/12), y+(h/2) )
file_name = os.path.basename(file_name)
pc.show("File:"+file_name)
pc.moveto(x+(w*9/12), y+(h/2) )
pc.show("Page:%d"%(page_num))
def draw_footer(self, pc, file_name, page, x, y, w, h):
"""Draw an footer for the current page.
The footer will be within a rectangle (x, y, w, h)
pc = the gnome print context
file_name = a string indicating the name of the file
page_num = the number to print
"""
font= gnomeprint.font_find_closest("Sans Regular", 12)
pc.setfont(font)
pc.setlinewidth(1.0)
pc.line_stroked(x, y, x+w, y)
pc.moveto(x, y-15)
date = time.strftime("%a, %d %b %Y", time.gmtime())
pc.show("Date:"+date)
pc.moveto(x+(w/2), y-15)
Time = time.strftime("%H:%M:%S", time.gmtime())
pc.show("Time "+Time)
def load_config(self):
"""Load the print configuration from file"""
try:
file_ = file(CONFIG_FILE_NAME)
except IOError:
return gnomeprint.config_default()
return gnomeprint.config_from_string(file_.read(), 0)
def save_config(self, cf):
"""Save the print configuration to file"""
try:
file(CONFIG_FILE_NAME, "w").write(cf.to_string(0))
except IOError:
return
def quit(self, dlg):
#save config
self.save_config(self.config)
dlg.hide()
if __name__ == "__main__":
import sys
if len(sys.argv)<2:
print "use gp_example.py <file_to_print>"
sys.exit()
text= open(sys.argv[1], 'r').read()
win=gtk.Dialog("GnomePrint Example")
win.connect("destroy", lambda win:gtk.mainquit())
win.set_default_size(500,400)
view=gtk.TextView()
buffer=view.get_buffer()
buffer.set_text(text)
sw=gtk.ScrolledWindow()
sw.add(view)
view.show()
win.vbox.add(sw)
sw.show()
win.add_button(gtk.STOCK_PRINT, 4)
ans = win.run()
if ans==4:
pa = PrintAssistant(buffer, sys.argv[1])
pa.show_print_dialog()
gtk.mainloop()
_______________________________________________
pygtk mailing list [EMAIL PROTECTED]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/