tkinter canvas postscript fontmap

M

Marcel Achim

Hi,

I want to set the fontmap to be used when saving the file. I write to
the canvas using Arial and would like to get either Arial or Courier in
the resulting .ps instead of MsSansSerif... I haven't found any sample
on the net. Has anyone done this before with success ? What is the
format of the "fontmap" option ?

thanks
Marcel
 
R

Richard Townsend

I want to set the fontmap to be used when saving the file. I write to
the canvas using Arial and would like to get either Arial or Courier in
the resulting .ps instead of MsSansSerif... I haven't found any sample
on the net. Has anyone done this before with success ? What is the
format of the "fontmap" option ?

I needed to do something similar some time ago and also found no examples.

I'm not sure if it is the correct way to do it, but the code below appears
to work.

When you press the 'Print' button it creates a fontmap array which
substitutes 'Arial 12' for all occurences of 'Courier 12' and 'Arial-Bold
14' for 'Courier 14'. It then dumps the postscript to a file.

Hope that helps,
Richard Townsend

----------------------------------------------------------------------------
------------------------------------
from Tkinter import *

font1 = "Courier 12"
font2 = "Courier 14"

class Demo:

def __init__(self):
self.root = Tk()

self.canvas = Canvas(self.root, height=300, width=300, bg="white")
self.canvas.pack()

frame = Frame(self.root)
frame.pack()

print_pb = Button(frame, text="Print", command=self.genPostscript)
print_pb.pack(side=LEFT)

quit_pb = Button(frame, text="Quit", command=self.quit)
quit_pb.pack(side=LEFT)

text = 'This is in %s' % font1
self.canvas.create_text(10, 10, text=text, anchor=NW, font=font1)

text = 'This is in %s' % font2
self.canvas.create_text(10, 40, text=text, anchor=NW, font=font2)

self.canvas.update_idletasks()
self.root.mainloop()


def genPostscript(self):
# Create fontmap array
self.root.tk.call('set', 'fontmap(%s)' % font1, 'Arial 12')
self.root.tk.call('set', 'fontmap(%s)' % font2, 'Arial-Bold 14')

#res = self.root.tk.call('array', 'get', 'fontmap')
#print res

# Generate the postscript data using the fontmap
postscript = self.canvas.postscript(fontmap='fontmap')

filename = 'dump.ps'
fd = open(filename, 'w')
fd.write(postscript)
fd.close()


def quit(self):
self.root.quit()


if __name__ == "__main__":

Demo()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top