flipdot/fontconverter/fontconverter.py

117 lines
2.8 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
2023-11-25 11:23:15 +00:00
pixelsize=8
2024-01-09 21:15:54 +00:00
#fontname="font_normal"
#font = pygame.font.Font("Minecraftia-Regular.ttf", fontsize*pixelsize);
#fontsize=8
#pixelsW=6
#pixelsH=8
#offset=(0,0)
fontname="font_large"
fontsize=16
pixelsW=9
pixelsH=8*2
offset=(0,8)
font = pygame.font.Font("Sweet16mono.ttf", fontsize*pixelsize); # https://github.com/kmar/Sweet16Font
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
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")
screen.fill((255, 255, 255))
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
screen.fill((255, 255, 255))
for y in range(pixelsH):
for x in range(pixelsW):
color=(250,250,250)
if (x+y)%2==0:
color=(200,200,200)
pygame.draw.rect(screen, color, pygame.Rect(pixelsize*x, pixelsize*y, pixelsize, pixelsize))
2023-11-25 11:23:15 +00:00
text = font.render(chr(ascii), True, (0, 0, 0))
2024-01-09 21:15:54 +00:00
screen.blit(text,(0+offset[0],-pixelsize*2+offset[1]))
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)))
if (readcolor==(0,0,0)):
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")
ascii+=1
if (ascii>asciiend):
done=True
pygame.display.flip()
2024-01-09 21:15:54 +00:00
clock.tick(fps)
2023-11-25 11:23:15 +00:00
file.write("};")
2024-01-09 21:15:54 +00:00
file.close()