fonteditor/main.py
2024-03-03 19:33:19 +01:00

169 lines
4.7 KiB
Python

import os
import unicodedata
from exporter import export
from canvas import *
from project import *
##### CONFIG #####
WINDOW_SIZE = (
(1280,720),
(1920,1019)
)[0]
cross_color = (255, 60, 25)
pixel_color = (255,) * 3
##################
project=Project()
def menu_file_open_project_click():
global project
global canvas_editor
file_path = tkinter.filedialog.askopenfilename(
title="Open font project",
filetypes=[("Font project (json)", "*.fontproj")]
)
if file_path == "" or file_path==():
return
# save last path to cache
with open("last_path_cache.txt", "w") as f:
f.write(file_path)
try:
project.load(file_path)
except AttributeError as e:
tkinter.messagebox.showerror("Opening project",f"Project '{file_path}' is invalid: {e}")
except IOError as e:
tkinter.messagebox.showerror("Opening project",f"Failed to open project '{file_path}': {e}")
finally:
canvas_editor.after_project_load()
canvas_editor.draw()
update_glyph_preview()
def menu_file_save_project_click():
global project
path=None
if project.loaded:
path=project.path
else:
# show file save dialog
path=filedialog.asksaveasfilename(
title="Save font project",
filetypes=[("Font project (json)", "*.fontproj")],
defaultextension=".fontproj"
)
if path=="" or file_path==():
return
try:
project.save(path)
except IOError as e:
tkinter.messagebox.showerror("Saving project",f"Failed to save project '{path}': {e}")
def menu_file_save_project_as_click():
pass
def button_prev_glyph_click():
global canvas_editor
global project
if not project.loaded:
return
canvas_editor.prev_glyph()
update_glyph_preview()
def button_next_glyph_click():
global canvas_editor
global project
if not project.loaded:
return
canvas_editor.next_glyph()
update_glyph_preview()
def button_glyph_search_click():
global canvas_editor
global entry_glyph_id
global project
if not project.loaded:
return
code=entry_glyph_id.get()
canvas_editor.save_char()
try:
canvas_editor.current_char=int(code,base=16)
except ValueError as e:
tkinter.messagebox.showerror("Searching glyph", f"Invalid hex value {code}: {e}")
finally:
canvas_editor.keep_current_char_in_bounds()
canvas_editor.load_char()
update_glyph_preview()
def number_only_validate(val):
for x in val:
if not x.isdigit() and (ord(x)<ord("a") or ord(x)>ord("f")) and (ord(x)<ord("A") or ord(x)>ord("F")):
return False
return True
def update_glyph_preview():
global canvas_editor
global canvas_preview
global label_glyph_name
canvas_preview.delete("all")
canvas_preview.create_text((50,100),text=chr(canvas_editor.current_char),fill="white",font="tkDefaultFont 70")
name=unicodedata.name(chr(canvas_editor.current_char),"unknown")
label_glyph_name.config(text=f"{name} U+{canvas_editor.current_char:04x}")
window=tkinter.Tk()
window.title("fonteditor")
window.geometry(f"{WINDOW_SIZE[0]}x{WINDOW_SIZE[1]}")
menubar=tkinter.Menu(window)
menu_file=tkinter.Menu(menubar,tearoff=False)
menu_file.add_command(label="Open project",command=menu_file_open_project_click)
menu_file.add_command(label="Save project",command=menu_file_save_project_click)
menu_file.add_command(label="Save project as",command=menu_file_save_project_as_click)
menubar.add_cascade(label="File",menu=menu_file)
canvas_editor=EditorCanvas(project,window,bg="black")
canvas_editor.pack(side="left",fill="both",expand=True)
frame_controls=tkinter.Frame(window)
frame_controls.pack(side="right")
canvas_preview=tkinter.Canvas(frame_controls,width=100,height=200,bg="black")
canvas_preview.pack(side="top")
label_glyph_name=tkinter.Label(frame_controls)
label_glyph_name.pack(side="top")
frame_nav=tkinter.Frame(frame_controls)
frame_nav.pack(side="top",pady=10)
button_prev_glyph=tkinter.Button(frame_nav,width=10,text="Previous",command=button_prev_glyph_click)
button_prev_glyph.pack(side="left")
button_next_glyph=tkinter.Button(frame_nav,width=10,text="Next",command=button_next_glyph_click)
button_next_glyph.pack(side="left")
frame_glyph_id=tkinter.Frame(frame_controls)
frame_glyph_id.pack(side="top",pady=10)
entry_glyph_id=tkinter.Entry(frame_glyph_id,validate="all",validatecommand=((window.register(number_only_validate)),"%P"))
entry_glyph_id.pack(side="left")
button_glyph_search=tkinter.Button(frame_glyph_id,width=10,text="Search",command=button_glyph_search_click)
button_glyph_search.pack(side="left")
window.config(menu=menubar)
window.mainloop()