Adjust project exporting

This commit is contained in:
mrkubax10 2024-03-04 21:01:14 +01:00
parent d6558a77dd
commit 5bddf37c05
3 changed files with 101 additions and 12 deletions

View File

@ -90,17 +90,9 @@ class EditorCanvas(tkinter.Canvas):
self.current_char_pixels[x]=0
self.draw()
return
base64_data=self.project.chars[chr(self.current_char)]
pixels=base64.b64decode(base64_data.encode("ascii"))
pixel_index=0
for pixel in pixels:
if pixel_index>=len(self.current_char_pixels):
break
for x in range(8):
if pixel_index>=len(self.current_char_pixels):
break
self.current_char_pixels[pixel_index]=(pixel>>(7-x))&1
pixel_index+=1
pixels=self.project.decode_char(chr(self.current_char))
for x in range(len(pixels)):
self.current_char_pixels[x]=pixels[x]
self.draw()

27
main.py
View File

@ -1,6 +1,6 @@
import os
import tkinter.filedialog
import unicodedata
from exporter import export
from canvas import *
from project import *
@ -116,6 +116,27 @@ def save_project(ask):
tkinter.messagebox.showerror("Saving project",f"Failed to save project '{path}': {e}")
def export_project(ask):
if not project.loaded:
return
path=None
if project.export_path and not ask:
path=project.export_path
else:
path = tkinter.filedialog.askdirectory(
title="Choose folder where exported pages will be saved"
)
if path=="" or path==():
return
canvas_editor.save_char()
try:
project.export(path)
except IOError as e:
tkinter.messagebox.showerror("Exporting project",f"Failed to export project: {e}")
def button_prev_glyph_click():
global canvas_editor
global project
@ -182,7 +203,11 @@ menu_file.add_command(label="New project",command=menu_file_new_project_click)
menu_file.add_command(label="Open project",command=menu_file_open_project_click)
menu_file.add_command(label="Save project",command=lambda: save_project(False))
menu_file.add_command(label="Save project as",command=lambda: save_project(True))
menu_export=tkinter.Menu(menubar,tearoff=False)
menu_export.add_command(label="Export",command=lambda: export_project(False))
menu_export.add_command(label="Export as",command=lambda: export_project(True))
menubar.add_cascade(label="File",menu=menu_file)
menubar.add_cascade(label="Export",menu=menu_export)
canvas_editor=EditorCanvas(project,window,bg="black")
canvas_editor.pack(side="left",fill="both",expand=True)

View File

@ -1,10 +1,19 @@
import base64
import json
import PIL.Image
def create_zeroed_array(length):
output=[]
for _ in range(length):
output.append(0)
return output
class Project:
def __init__(self):
self.chars={}
self.char_res=(0,0)
self.path=None
self.export_path=None
self.loaded=False
self.modified=False
@ -38,5 +47,68 @@ class Project:
self.loaded=True
def export(self,path):
chars = tuple(self.chars.items())
# there will be 256 characters per page
last_char_idx = len(chars) - 1
page_res = (self.char_res[0] * 16,self.char_res[1] * 16)
page = 0
page_char_x = 0
page_char_y = 0
for char_idx, char in enumerate(chars):
if page_char_x == 0 and page_char_y == 0:
page_arr = create_zeroed_array(page_res[0]*page_res[1])
char_map_string = ""
char_bitmap=self.decode_char(char[0])
# put char_bitmap onto page_img at correct position
for i in range(len(char_bitmap)):
x=i//self.char_res[1]+page_char_x*self.char_res[0]
y=i%self.char_res[1]+page_char_y*self.char_res[1]
page_arr[y*page_res[0]+x]=char_bitmap[i]
char_map_string += char[0]
page_char_x += 1
if page_char_x == 16:
page_char_x = 0
page_char_y += 1
if page_char_y == 16 or char_idx == last_char_idx:
## save page
for x in range(len(page_arr)):
page_arr[x]*=255
page_img = PIL.Image.frombytes("L",page_res,bytes(page_arr))
# save page
page_img.save(f"{path}/page_{page}.png")
# save char map
with open(f"{path}/page_{page}.txt", "w", encoding="utf-8") as f:
f.write(char_map_string)
page += 1
page_char_y = 0
self.export_path=path
def decode_char(self,char):
result=[0 for _ in range(self.char_res[0]*self.char_res[1])]
pixels=base64.b64decode(self.chars[char].encode("ascii"))
pixel_index=0
for pixel in pixels:
if pixel_index>=len(result):
break
for x in range(8):
if pixel_index>=len(result):
break
result[pixel_index]=(pixel>>(7-x))&1
pixel_index+=1
return result
def does_char_exist(self,c):
return chr(c) in self.chars