tkinter canvas postscript fontmap

Discussion in 'Python' started by Marcel Achim, Jan 13, 2004.

  1. Marcel Achim

    Marcel Achim Guest

    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
    Marcel Achim, Jan 13, 2004
    #1
    1. Advertising

  2. > 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()
    Richard Townsend, Jan 13, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Askari
    Replies:
    2
    Views:
    707
    Askari
    Aug 30, 2004
  2. PhilC
    Replies:
    2
    Views:
    882
    PhilC
    Oct 25, 2004
  3. Len Lawrence
    Replies:
    3
    Views:
    110
    Joel VanderWerf
    Apr 15, 2008
  4. Len Lawrence
    Replies:
    0
    Views:
    130
    Len Lawrence
    Jun 14, 2008
  5. Replies:
    10
    Views:
    259
    Rick Johnson
    Mar 15, 2013
Loading...

Share This Page