tkinter canvas postscript fontmap

Richard Townsend nospam at here.com
Tue Jan 13 15:13:04 EST 2004


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





More information about the Python-list mailing list