flipdot/fontconverter/fontconverter.py

150 lines
3.5 KiB
Python
Raw Normal View History

2023-11-25 11:23:15 +00:00
import pygame
pygame.init()
screen = pygame.display.set_mode((640, 480))
clock = pygame.time.Clock()
done = False
2024-01-09 21:15:54 +00:00
persistance=False
fps=10
2024-02-25 13:01:53 +00:00
color_checkerboardA=(250,250,250)
color_checkerboardB=(200,200,200)
color_bg=(255,255,255)
color_text=(0,0,0)
2024-01-09 21:15:54 +00:00
2024-02-25 13:01:53 +00:00
exportImages=True
exportImageFolder="imageexport/"
pixelsize=32
if exportImages:
pixelsize=1
color_checkerboardA=(0,0,0)
color_checkerboardB=(0,0,0)
color_bg=(0,0,0)
color_text=(255,255,255)
2023-11-25 11:23:15 +00:00
2024-01-09 21:15:54 +00:00
#fontname="font_normal"
#font = pygame.font.Font("Minecraftia-Regular.ttf", fontsize*pixelsize);
#fontsize=8
#pixelsW=6
2024-02-25 13:01:53 +00:00
#pixelsH=8KeyboardInterrupt
2024-01-09 21:15:54 +00:00
#offset=(0,0)
2024-02-25 13:01:53 +00:00
#fontname="font_large"
#fontsize=16
#pixelsW=9
#pixelsH=8*2
#offset=(0,1)
#font = pygame.font.Font("Sweet16mono.ttf", fontsize*pixelsize); # https://github.com/kmar/Sweet16Font
fontname="font_small"
fontsize=10
pixelsW=3
pixelsH=8
offset=(0,2)
font = pygame.font.Font("pzim3x5.ttf", fontsize*pixelsize); # https://www.fonts4free.net/pixelzim-3x5-font.html
2024-01-09 21:15:54 +00:00
2023-11-25 11:23:15 +00:00
asciioffset=33
asciiend=126
ascii = asciioffset
2024-01-09 21:15:54 +00:00
2023-11-25 11:23:15 +00:00
2024-02-25 13:01:53 +00:00
2023-11-25 11:23:15 +00:00
file = open("output.txt", "w") # write mode
2023-11-25 13:31:07 +00:00
2024-01-09 21:15:54 +00:00
file.write("const uint8_t "+fontname+"_width="+str(pixelsW)+";\n");
file.write("const uint8_t "+fontname+"_offset="+str(asciioffset)+";\n");
2023-11-25 11:23:15 +00:00
#file.write("uint8_t "+fontname+"["+str(asciiend-asciioffset+1)+"]["+str(pixelsW)+"];\n")
2024-01-09 21:15:54 +00:00
file.write("const uint"+str(pixelsH)+"_t "+fontname+"["+str(asciiend-asciioffset+1)+"]["+str(pixelsW)+"]= {\n")
2024-02-25 13:01:53 +00:00
screen.fill(color_bg)
2024-01-09 21:15:54 +00:00
screencleared=False
2023-11-25 11:23:15 +00:00
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
done = True
2024-01-09 21:15:54 +00:00
if not screencleared or not persistance:
screencleared=True
2024-02-25 13:01:53 +00:00
screen.fill(color_bg)
2024-01-09 21:15:54 +00:00
for y in range(pixelsH):
for x in range(pixelsW):
2024-02-25 13:01:53 +00:00
color=color_checkerboardA
2024-01-09 21:15:54 +00:00
if (x+y)%2==0:
2024-02-25 13:01:53 +00:00
color=color_checkerboardA
2024-01-09 21:15:54 +00:00
pygame.draw.rect(screen, color, pygame.Rect(pixelsize*x, pixelsize*y, pixelsize, pixelsize))
2023-11-25 11:23:15 +00:00
2024-02-25 13:01:53 +00:00
text = font.render(chr(ascii), True, color_text)
screen.blit(text,(0+offset[0]*pixelsize,-pixelsize*2+offset[1]*pixelsize))
2023-11-25 11:23:15 +00:00
print("ascii="+str(ascii)+" char="+chr(ascii))
'''
uint16_t font[COLUMNS];
font[ascii][column]=0b00000000;
int a[2][3] = {
{0, 2, 1} , /* row at index 0 */
{4, 3, 7} , /* row at index 1 */
};
'''
file.write("{")
for x in range(pixelsW):
int8number=0
string8=""
for y in range(pixelsH):
readcolor = screen.get_at(((int)(pixelsize*x+pixelsize/2),(int)(pixelsize*y+pixelsize/2)))
2024-02-25 13:01:53 +00:00
if (readcolor==color_text):
2023-11-25 11:23:15 +00:00
int8number+=pow(2,y)
string8+='1'
else:
string8+='0'
#file.write(fontname+"["+str(ascii-asciioffset)+"]["+str(x)+"]=0b"+string8+";\n")
file.write(str(int8number))
if (x<pixelsW-1):
file.write(",")
comment_character=chr(ascii)
if comment_character=='\\':
comment_character="Backslash"
file.write("}, // "+str(ascii)+"="+comment_character+"\n")
2024-02-25 13:01:53 +00:00
pygame.display.flip()
if exportImages:
exportrect=pygame.Rect(0,0,pixelsW,pixelsH)
sub = screen.subsurface(exportrect)
pygame.image.save(sub, exportImageFolder+str(ascii)+".png")
clock.tick(fps)
2023-11-25 11:23:15 +00:00
ascii+=1
if (ascii>asciiend):
done=True
file.write("};")
2024-01-09 21:15:54 +00:00
file.close()