Advertisement
Guest User

VectorFont - Tkinter

a guest
Sep 24th, 2014
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.70 KB | None | 0 0
  1. # Adapted by Willie Lawrence from http://wiki.tcl.tk/27638
  2. _DEFAULT_FONT = {
  3.     "a": [[4, 0, 1, 0, 0, 1, 0, 3, 1, 4, 4, 4, 4, 0]],
  4.     "b" : [[0, 6, 0, 0, 3, 0, 4, 1, 4, 3, 3, 4, 0, 4]],
  5.     "c" : [[4, 0, 1, 0, 0, 1, 0, 3, 1, 4, 4, 4]],
  6.     "d" : [[4, 6, 4, 0, 1, 0, 0, 1, 0, 3, 1, 4, 4, 4]],
  7.     "e" : [[4, 0, 1, 0, 0, 1, 0, 3, 1, 4, 3, 4, 4, 3, 4, 2, 0, 2]],
  8.     "f" : [[2, 0, 2, 5, 3, 6, 4, 6], [0, 3, 4, 3]],
  9.     "g" : [[0, 0, 3, 0, 4, 1, 4, 4, 3, 5, 1, 5, 0, 4, 0, 3, 1, 2, 4, 2]],
  10.     "h" : [[0, 6, 0, 0], [0, 4, 3, 4, 4, 3, 4, 0]],
  11.     "i" :  [[2, 0, 2, 4], [2, 5, 2, 6]],
  12.     "j" : [[0, 0, 1, 0, 2, 1, 2, 4], [2, 5, 2, 6]],
  13.     "k" : [[0, 0, 0, 6], [4, 0, 0, 2, 3, 5]],
  14.     "l" : [[1, 6, 2, 6, 2, 0], [1, 0, 3, 0]],
  15.     "m" : [[0, 0, 0, 4, 1, 4, 2, 3, 3, 4, 4, 4, 4, 0], [2, 0, 2, 3]],
  16.     "n" : [[0, 4, 0, 0], [0, 3, 1, 4, 3, 4, 4, 3, 4, 0]],
  17.     "o" : [[0, 1, 0, 3, 1, 4, 3, 4, 4, 3, 4, 1, 3, 0, 1, 0, 0, 1]],
  18.     "p" : [[0, 0, 0, 5, 3, 5, 4, 4, 4, 3, 3, 2, 0, 2]],
  19.     "q" : [[4, 0, 4, 5, 1, 5, 0, 4, 0, 3, 1, 2, 4, 2]],
  20.     "r" : [[0, 0, 0, 4, 3, 4, 4, 3]],
  21.     "s" : [[0, 0, 4, 0, 4, 2, 0, 2, 0, 4, 4, 4]],
  22.     "t" : [[1, 6, 1, 1, 2, 0, 3, 0, 4, 1], [0, 5, 3, 5]],
  23.     "u" : [[4, 4, 4, 0], [4, 1, 3, 0, 1, 0, 0, 1, 0, 4]],
  24.     "v" : [[0, 4, 2, 0, 4, 4]],
  25.     "w" : [[0, 4, 0, 0, 2, 2, 4, 0, 4, 4]],
  26.     "x" : [[0, 0, 4, 4], [0, 4, 4, 0]],
  27.     "y" : [[0, 5, 0, 3, 1, 2, 3, 2, 4, 3], [4, 5, 4, 1, 3, 0, 0, 0]],
  28.     "z" : [[0, 4, 4, 4, 0, 0, 4, 0]],
  29.     "A" : [[0, 0, 0, 4, 2, 6, 4, 4, 4, 0], [0, 2, 4, 2]],
  30.     "B" : [[0, 0, 0, 6, 3, 6, 4, 5, 4, 4, 3, 3, 4, 2, 4, 1, 3, 0, 0, 0], [0, 3, 3, 3]],
  31.     "C" : [[4, 0, 0, 0, 0, 6, 4, 6]],
  32.     "D" : [[0, 0, 0, 6, 2, 6, 4, 4, 4, 2, 2, 0, 0, 0]],
  33.     "E" : [[4, 0, 0, 0, 0, 6, 4, 6], [0, 3, 4, 3]],
  34.     "F" : [[0, 0, 0, 6, 4, 6], [0, 3, 3, 3]],
  35.     "G" : [[2, 2, 4, 2, 4, 0, 0, 0, 0, 6, 4, 6, 4, 4]],
  36.     "H" : [[0, 0, 0, 6], [4, 0, 4, 6], [0, 3, 4, 3]],
  37.     "I" : [[0, 0, 4, 0], [2, 0, 2, 6], [0, 6, 4, 6]],
  38.     "J" : [[0, 2, 2, 0, 4, 0, 4, 6]],
  39.     "K" : [[0, 0, 0, 6], [4, 6, 0, 3, 4, 0]],
  40.     "L" : [[4, 0, 0, 0, 0, 6]],
  41.     "M" : [[0, 0, 0, 6, 2, 4, 4, 6, 4, 0]],
  42.     "N" : [[0, 0, 0, 6, 4, 0, 4, 6]],
  43.     "O" : [[0, 0, 0, 6, 4, 6, 4, 0, 0, 0]],
  44.     "P" : [[0, 0, 0, 6, 4, 6, 4, 3, 0, 3]],
  45.     "Q" : [[0, 0, 0, 6, 4, 6, 4, 2, 2, 0, 0, 0], [2, 2, 4, 0]],
  46.     "R" : [[0, 0, 0, 6, 4, 6, 4, 3, 0, 3], [1, 3, 4, 0]],
  47.     "S" : [[0, 0, 3, 0, 4, 1, 4, 2, 3, 3, 1, 3, 0, 4, 0, 5, 1, 6, 4, 6]],
  48.     "T" : [[2, 0, 2, 6], [0, 6, 4, 6]],
  49.     "U" : [[0, 6, 0, 0, 4, 0, 4, 6]],
  50.     "V" : [[0, 6, 2, 0, 4, 6]],
  51.     "W" : [[0, 6, 0, 0, 2, 2, 4, 0, 4, 6]],
  52.     "X" : [[0, 0, 4, 6], [0, 6, 4, 0]],
  53.     "Y" : [[0, 6, 2, 4, 4, 6], [2, 0, 2, 4]],
  54.     "Z" : [[0, 6, 4, 6, 0, 0, 4, 0], [1, 3, 3, 3]],
  55.     "0" : [[0, 0, 0, 6, 4, 6, 4, 0, 0, 0], [0, 0, 4, 6]],
  56.     "1" : [[2, 0, 2, 6, 0, 4], [0, 0, 4, 0]],
  57.     "2" : [[0, 6, 4, 6, 4, 3, 0, 3, 0, 0, 4, 0]],
  58.     "3" : [[0, 6, 4, 6, 4, 0, 0, 0], [0, 3, 4, 3]],
  59.     "4" : [[0, 6, 0, 3, 4, 3], [4, 6, 4, 0]],
  60.     "5" : [[0, 0, 4, 0, 4, 3, 0, 3, 0, 6, 4, 6]],
  61.     "6" : [[4, 6, 0, 6, 0, 0, 4, 0, 4, 3, 0, 3]],
  62.     "7" : [[0, 6, 4, 6, 4, 0]],
  63.     "8" : [[0, 0, 0, 6, 4, 6, 4, 0, 0, 0], [0, 3, 4, 3]],
  64.     "9" : [[4, 0, 4, 6, 0, 6, 0, 3, 4, 3]],
  65.     "~" : [[0, 4, 0, 5, 2, 5, 2, 4, 4, 4, 4, 5]],
  66.     "`" : [[1, 6, 3, 4]],
  67.     "!" : [[2, 0, 2, 1], [2, 2, 2, 6]],
  68.     "@" : [[3, 2, 3, 4, 1, 4, 1, 2, 3, 2, 4, 1, 4, 6, 0, 6, 0, 0, 3, 0]],
  69.     "#" : [[1, 0, 1, 6], [3, 0, 3, 6], [0, 2, 4, 2], [0, 4, 4, 4]],
  70.     "$" : [[0, 2, 0, 1, 4, 1, 4, 3, 0, 3, 0, 5, 4, 5, 4, 4], [2, 0, 2, 6]],
  71.     "%" : [[0, 6, 0, 4, 2, 4, 2, 6, 0, 6], [2, 0, 4, 0, 4, 2, 2, 2, 2, 0], [0, 0, 4, 6]],
  72.     "^" : [[0, 4, 2, 6, 4, 4]],
  73.     "&" : [[4, 0, 1, 0, 0, 1, 0, 2, 3, 5, 2, 6, 1, 6, 0, 5, 4, 0]],
  74.     "*" : [[2, 0, 2, 6], [0, 3, 4, 3], [0, 1, 4, 5], [0, 5, 4, 1]],
  75.     "(" : [[4, 0, 3, 0, 1, 2, 1, 4, 3, 6, 4, 6]],
  76.     ")" : [[0, 0, 1, 0, 3, 2, 3, 4, 1, 6, 0, 6]],
  77.     "_" : [[0, 0, 4, 0]],
  78.     "-" : [[0, 3, 4, 3]],
  79.     "+" : [[0, 3, 4, 3], [2, 1, 2, 5]],
  80.     "=" : [[0, 2, 4, 2], [0, 4, 4, 4]],
  81.     "[" : [[4, 0, 2, 0, 2, 6, 4, 6]],
  82.     "]" : [[0, 0, 2, 0, 2, 6, 0, 6]],
  83.     "{" : [[4, 0, 2, 0, 2, 2, 1, 3, 2, 4, 2, 6, 4, 6]],
  84.     "}" : [[0, 0, 2, 0, 2, 2, 3, 3, 2, 4, 2, 6, 0, 6]],
  85.     "|" : [[2, 0, 2, 2], [2, 4, 2, 6]],
  86.     "\\" : [[0, 6, 4, 0]],
  87.     ":" : [[2, 1, 2, 2], [2, 4, 2, 5]],
  88.     ";" : [[1, 0, 2, 1, 2, 2], [2, 4, 2, 5]],
  89.     '"' : [[1, 6, 1, 4], [3, 6, 3, 4]],
  90.     "'" : [[2, 6, 2, 4]],
  91.     "," : [[1, 0, 2, 1, 2, 2]],
  92.     "." : [[2, 0, 2, 1]],
  93.     "/" : [[0, 0, 4, 6]],
  94.     "?" : [[2, 0, 2, 1], [2, 2, 4, 4, 4, 6, 0, 6, 0, 4]],
  95.     "<" : [[4, 6, 0, 3, 4, 0]],
  96.     ">" : [[0, 0, 4, 3, 0, 6]],
  97. }
  98. def draw_letter(c, basecoords, letter, scale, **args):
  99.     """
  100.     Draws a given letter on canvas c, scaling the size of
  101.     the letter according to scale.  Returns a list of
  102.     handles of canvas objects (lines) that form the new
  103.     object.
  104.     """
  105.     fontarray = args.pop("font", _DEFAULT_FONT)
  106.     xbase, ybase = basecoords
  107.     retlist = []
  108.     for coordset in fontarray[letter]:
  109.         coords = []
  110.         for coord in coordset:
  111.             coords.append(scale * coord)
  112.         newcoords = []
  113.         for i in range(len(coords)):
  114.             # set cvalue [lindex $coords $i]
  115.             cvalue = coords[i]
  116.             if i % 2:
  117.                 newcoords.append(ybase - cvalue)
  118.             else:
  119.                 newcoords.append(cvalue + xbase)
  120.         retlist.append(c.create_line(*newcoords, **args))
  121.     return retlist
  122.  
  123. def draw_string(c, basecoords, string, scale, **args):
  124.     """
  125.     Draws a string at the given basecoords on canvas c
  126.     and at the given scale.  Args are passed to the canvas
  127.     line object creation command.  Returns a list of all
  128.     canvas object IDs corresponding to the vectors in
  129.     the letters of the string.
  130.     """
  131.     xbase, ybase = basecoords
  132.     retlist = []
  133.     xcoord, ycoord = xbase, ybase
  134.     for i in range(len(string)):
  135.         char = string[i]
  136.         if char == " ":
  137.             xcoord += 4*scale
  138.         elif char == "\n" or char == "\r":
  139.             xcoord = xbase
  140.             ycoord += 8 * scale
  141.         else:
  142.             r = draw_letter(c, [xcoord, ycoord], char, scale, **args)
  143.             retlist.extend(r)
  144.             xcoord += 5.5 * scale
  145.     return retlist
  146.  
  147. if __name__ == "__main__":
  148.     # example
  149.     from Tkinter import *
  150.     top = Tk()
  151.     top.title("VectorFont")
  152.     top.state("zoom")
  153.     ca = Canvas(top)
  154.     top.bind("<Escape>", lambda e: top.destroy(), "+")
  155.     ca.pack(expand=YES,fill=BOTH)
  156.     # draw_letter(ca, [100,100], 'A', 5.0,width=5, tag="A")
  157.     TEXT = "Vector Font a"
  158.     draw_string(ca, [100,100], TEXT, 5, tag="my_text")
  159.     G_SCALE = 5
  160.     BASE_COORD = [100,100]
  161.     def _g(*args):
  162.         global G_SCALE
  163.         evt = args[0]
  164.         ca.delete("my_text")
  165.         if evt.delta > 0:
  166.             G_SCALE += 1.0
  167.         if evt.delta < 0:
  168.             G_SCALE -= 1.0
  169.         draw_string(ca, BASE_COORD, TEXT, G_SCALE, tag="my_text")
  170.     def reset_base(evt):
  171.         global BASE_COORD
  172.         BASE_COORD = [top.winfo_pointerx()-top.winfo_rootx(),
  173.                 top.winfo_pointery()-top.winfo_rooty()]
  174.         ca.delete("my_text")
  175.         draw_string(ca, BASE_COORD, TEXT, G_SCALE, tag="my_text")
  176.     ca.bind("<Motion>", reset_base, "+")
  177.     ca.focus_force()
  178.  
  179.     top.bind("<MouseWheel>", _g, "+")
  180.     top.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement