font.py

Created by julien-bernon

Created on July 16, 2021

5.22 KB

A sizeable font module (needed for some of my games, like tetris) Each character is drawn on a 5x8 matrix, easy to modify and easy to add needed characters. 2 fonctions availables : texte and texte_bg They should manage multiple line (but only works in fact with size =1) ================================================================ Une police de taille réglable. Chaque lettre est écrite dans une matrice de 5x8, elle est donc facile à modifier et il est facile d’ajouter des caractères. Un caractère non codé sera remplacé par une espace.


from kandinsky import *

lettres={
            "A":"0111010001100011000111111100011000100000",
            "B":"1111010001100011111010001100011111000000",
            "C":"0111010001100001000010000100010111000000",
            "D":"1111010010100011000110001100101111000000",
            "E":"1111110000100001110010000100001111100000",
            "F":"1111110000100001110010000100001000000000",
            "G":"0111110000100001001110001100010111100000",
            "H":"1000110001100011111110001100011000100000",
            "I":"0111000100001000010000100001000111000000",
            "J":"0111100010000100001000010100100110000000",
            "K":"1000110010101001110010010100101000100000",
            "L":"0100001000010000100001000010000111100000",
            "M":"1000111011101011000110001100011000100000",
            "N":"1000111001101011001110001100011000100000",
            "O":"0111010001100011000110001100010111000000",
            "P":"1111010001100011111010000100001000000000",
            "Q":"0111010001100011000110101100100110100000",
            "R":"1111010001100011111010010100011000100000",
            "S":"0111010001100000111000001100010111000000",
            "T":"1111100100001000010000100001000010000000",
            "U":"1000110001100011000110001100010111000000",
            "V":"1000110001100011000101010010100010000000",
            "W":"1000110001100011000110101110111000100000",
            "X":"1000110001010100010001010100011000100000",
            "Y":"1000110001100010101000100001000010000000",
            "Z":"1111100001000100010001000100001111100000",
            
            "a":"0000001110000010111110001100010111100000",
            "b":"1000010000111101000110001100011111000000",
            "c":"0000000000011101000110000100000111100000",
            "d":"0000100001011111000110001100010111100000",
            "e":"0000001110100011111110000100000111100000",
            "f":"0000000111010000100011110010000100000000",
            "g":"0000001111100011000110001011110000101110",
            "h":"1000010000100001011011001100011000100000",
            "i":"0000000100000000110000100001000111000000",
            "j":"0000000100000000010000100001000010011000",
            "k":"0000001000010000100101010011010100100000",
            "l":"0100001000010000100001000001000001100000",
            "m":"0000000000010101010110101101011010100000",
            "n":"0000000000101101100110001100011000100000",
            "o":"0000000000011101000110001100010111000000",
            "p":"0000000110010010100101110010000100001000",
            "q":"0000000000001100100101001001110000100001",
            "r":"0000000000001110110001000010000100000000",
            "s":"0000000000001110100000111000010111000000",
            "t":"0000001000011100100001000010000011000000",
            "u":"0000000000000001000110001100110110100000",
            "v":"0000000000000001000110001010100010000000",
            "w":"0000000000000001000110101101010101000000",
            "x":"0000010001010100010000100010101000100000",
            "y":"0000001001010010100100110000100110000010",
            "z":"0000000000011110000100110010000111100000",
            
            "0":"0111010001101011010110101100010111000000",
            "1":"0010011100001000010000100001001111100000",
            "2":"0111010001000010001000100010011111100000",
            "3":"0111010001000010011000001100010111000000",
            "4":"0010001010100101001011111000100001000000",
            "5":"1111110000111100000100001100011111000000",
            "6":"0111010001100001111010001100010111000000",
            "7":"1111110001000010001000100010000100000000",
            "8":"0111010001100010111010001100010111000000",
            "9":"0111010001100010111100001010010011000000",

            " ":"0000000000000000000000000000000000000000",
            ".":"0000000000000000000000000001100011000000",
            ",":"0000000000000000000000110001100001000100",
            ":":"0000000000000000010000000000000010000000"
}


for i in lettres.keys():
  conversion=[]
  for j in lettres[i]:
      if j=="0": conversion.append(False)
      else: conversion.append(True)
  lettres[i]=conversion






def main():
  posx,posy=0,0
  dispos=sorted(list(lettres.keys()))
  for k in range(1,4):
    for i in range(len(dispos)):
        for j in range(len(lettres[dispos[i]])):
            x=posx+(k*6*i)%320+k*(j%5)
            y=posy+9*k*((k*9*i)//320)+k*(j//5)
            if lettres[dispos[i]][j]:
                for l in range(k):
                    for m in range(k):
                        set_pixel(x+l,y+m,(0,0,0))
    posy=y+9*k
    
def texte(chaine,posx=0,posy=0,taille=1,color=(0,0,0)):
  for i in range(len(chaine)):
      carac=chaine[i]
      if not carac in lettres.keys():
          carac=" "
      for j in range(len(lettres[carac])):
        x=posx+(taille*6*i)%320+taille*(j%5)
        y=posy+taille*(j//5)
        if lettres[carac][j]:
            for k in range(taille):
                for l in range(taille):
                    set_pixel(x+k,y+l,color)

def texte_bg(chaine,posx=0,posy=0,width=320,color=(0,0,0),taille=1,bg_color=(255,255,255),padding=10):
    fill_rect(posx,posy,width,9*(taille+(taille*6*len(chaine))//width)+2*padding,bg_color)
    texte(chaine,posx+padding,posy+padding,taille,color)


main()