Guest User

FontEditor.py

a guest
Nov 11th, 2014
2,905
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # -*- coding: utf-8 -*-
  2. from Tkinter import *
  3. import tkFileDialog
  4.  
  5.  
  6. START = """#ifndef FONT5X7_H
  7. #define FONT5X7_H
  8.  
  9. #ifdef __AVR__
  10. #include <avr/io.h>
  11. #include <avr/pgmspace.h>
  12. #else
  13. #define PROGMEM
  14. #endif
  15.  
  16. // Standard ASCII 5x7 font
  17.  
  18. static const unsigned char font[] PROGMEM = {
  19. """
  20. END = """};
  21. #endif // FONT5X7_H"""
  22.  
  23.  
  24. def hexf(x):
  25.   return hex(x)[2:].upper().rjust(2, "0")
  26.  
  27.  
  28. class Application(Frame):
  29.   def __init__(self, master = None):
  30.     Frame.__init__(self, master)
  31.     self.pack()
  32.     m = Menu(self)
  33.     m.add_command(label = "Open", command = self.open_file)
  34.     m.add_command(label = "Save", command = self.save_file)
  35.     master.config(menu = m)
  36.     self.canvas = Canvas(self, width = 464, height = 408, highlightthickness = 0)
  37.     self.canvas.bind("<Button-1>", self.click)
  38.     self.canvas.pack(padx = 5, pady = 5)
  39.     self.area = []
  40.     for x in xrange(5):
  41.       for y in xrange(8):
  42.         self.area.append(self.canvas.create_rectangle(x*51, y*51, x*51+50, y*51+50, fill = "white", width = 0))
  43.     self.indexs, self.images, self.rectangles = [], [], []
  44.     image = PhotoImage(width = 10, height = 16)
  45.     for y in xrange(16):
  46.       for x in xrange(10):
  47.         image.put("#fff", (x, y))
  48.     for y in xrange(1, 17, 1):
  49.       for x in xrange(1, 17, 1):
  50.         i = 0 if y < 9 else 1
  51.         self.rectangles.append(self.canvas.create_rectangle(x*12+260, y*18+i, x*12+271, y*18+i+17, fill = "white", outline = "grey"))
  52.         img = image.copy()
  53.         self.indexs.append(self.canvas.create_image(x*12+261, y*18+i+1, image = img, anchor = NW))
  54.         self.images.append(img)
  55.     for xy in xrange(1, 17, 1):
  56.       i = 0 if xy < 9 else 1
  57.       self.canvas.create_text(xy*12+266, 10, text = hex(xy-1)[2:].upper())
  58.       self.canvas.create_text(266, xy*18+i+8, text = hex(xy-1)[2:].upper())
  59.     self.canvas.create_line(260, 162, 464, 162, fill = "red")
  60.     self.canvas.create_text(270, 330, text = "Selected int:", anchor = SW)
  61.     self.canvas.create_text(345, 330, text = " ", anchor = SW, tags = "INT")
  62.     self.canvas.create_text(270, 350, text = "Selected hex:", anchor = SW)
  63.     self.canvas.create_text(345, 350, text = " ", anchor = SW, tags = "HEX")
  64.     self.index = 0
  65.     self.load(self.images[self.index])
  66.     self.canvas.itemconfig(self.rectangles[self.index], outline = "blue")
  67.  
  68.   def open_file(self):
  69.     filename = tkFileDialog.askopenfilename(defaultextension = '.c', filetypes = [('all files', '.*'), ('c code', '.c')])
  70.     if not filename:return
  71.     try:
  72.       txt = open(filename, "rb").read()
  73.     except:
  74.       print "cannot open file"
  75.       return
  76.     start = txt.find("{")
  77.     if start == -1:
  78.       print "cannot find array start"
  79.       return
  80.     end = txt.find("}", start)
  81.     if end == -1:
  82.       print "cannot find array end"
  83.       return
  84.     txt = txt[start:end+1]
  85.     txt = txt.replace("{", "[")
  86.     txt = txt.replace("}", "]")
  87.     txt = txt.replace("//", "#")
  88.     try:sp = eval(txt)
  89.     except:
  90.       print "wrong array format"
  91.       return
  92.     if len(sp) != 1280:
  93.       print "wrong array length, try: 256 rows by 5 columns, total 1280 elements"
  94.       return
  95.     for i in xrange(256):
  96.       txt = []
  97.       for b in sp[i*5:i*5+5]:
  98.         txt += list(bin(b)[2:].rjust(8, "0")[::-1])
  99.       image = self.images[i]
  100.       for x in xrange(5):
  101.         for y in xrange(8):
  102.           color = "#fff" if txt.pop(0) == "0" else "#000"
  103.           self.putpixel(image, x, y, color)
  104.     self.canvas.itemconfig(self.rectangles[self.index], outline = "grey")
  105.     self.index = 0
  106.     self.load(self.images[self.index])
  107.     self.canvas.itemconfig(self.rectangles[self.index], outline = "blue")
  108.  
  109.   def save_file(self):
  110.     filename = tkFileDialog.asksaveasfilename(defaultextension = '.c', filetypes = [('all files', '.*'), ('c code', '.c')])
  111.     if not filename:return
  112.     try:
  113.       f = open(filename, "wb")
  114.     except:
  115.       print "cannot open file"
  116.       return
  117.     sp = []
  118.     for i in xrange(256):
  119.       image = self.images[i]
  120.       for x in xrange(5):
  121.         txt = ""
  122.         for y in xrange(8):
  123.           color = "0" if image.get(x*2, y*2) == "255 255 255" else "1"
  124.           txt += color
  125.         txt = txt[::-1]
  126.         sp.append("0x%s"%hexf(int(txt, 2)))
  127.     f.write(START)
  128.     txt = ""
  129.     for i in xrange(256):
  130.       b = sp[i*5:i*5+5]
  131.       b = "\t" + ", ".join(b)
  132.       if i != 255:b += ","
  133.       b += "\t//%s\t%s\r\n"%(hexf(i), i)
  134.       txt += b
  135.     f.write(txt)
  136.     f.write(END)
  137.     f.close()
  138.     print "save ok"
  139.  
  140.   def click(self, event):
  141.     tag = self.canvas.find_withtag(CURRENT)
  142.     if not tag:return
  143.     tag = tag[0]
  144.     if self.canvas.type(tag) == "rectangle" and tag in self.area:
  145.       index = self.area.index(tag)
  146.       image = self.images[self.index]
  147.       x = index / 8
  148.       y = index % 8
  149.       if image.get(x*2, y*2) == "255 255 255":
  150.         self.putpixel(image, x, y, "#000")
  151.         self.canvas.itemconfig(tag, fill = "black")
  152.       else:
  153.         self.putpixel(image, x, y, "#fff")
  154.         self.canvas.itemconfig(tag, fill = "white")
  155.     elif self.canvas.type(tag) == "image" and tag in self.indexs:
  156.       index = self.indexs.index(tag)
  157.       image = self.images[index]
  158.       rectangle = self.rectangles[index]
  159.       self.canvas.itemconfig(self.rectangles[self.index], outline = "grey")
  160.       self.index = index
  161.       self.canvas.itemconfig(rectangle, outline = "blue")
  162.       self.canvas.itemconfig("INT", text = str(index))
  163.       self.canvas.itemconfig("HEX", text = hexf(index))
  164.       self.load(image)
  165.  
  166.   def load(self, image):
  167.     i = 0
  168.     for x in xrange(5):
  169.       for y in xrange(8):
  170.         color = "white" if image.get(x*2, y*2) == "255 255 255" else "black"
  171.         self.canvas.itemconfig(self.area[i], fill = color)
  172.         i += 1
  173.  
  174.   def putpixel(self, image, x, y, color):
  175.     image.put(color, (x*2, y*2))
  176.     image.put(color, (x*2, y*2+1))
  177.     image.put(color, (x*2+1, y*2))
  178.     image.put(color, (x*2+1, y*2+1))
  179.  
  180.  
  181. root = Tk()
  182. app = Application(master = root)
  183. app.master.title("Font Editor")
  184. app.master.resizable(width = FALSE, height = FALSE)
  185. app.mainloop()
RAW Paste Data