Skip to content

Rumidom/Micropython_Fontlib

Repository files navigation

Micropython Fontlib

A micropython library for displaying 1-bit bitmap fonts, and 1-bit bitmap sprites, it was first tested on a monocrome screen (pcd8544 nokia screen) but should work with any monocrome screen through framebuffer

How to use it

Using a font

Add fontlib.py and a 1bit font .bmp (Should follow the same formatting of the bmp files on the fonts folder) file to your micropython device, then use the library to modify a framebuffer:

import framebuf
import fontlib
import ssd1306
from machine import Pin,I2C

screen_width = 128
screen_height = 32

#ESP8266 scl=Pin(4),sda=Pin(5)
#ESP32 C3 scl=Pin(9),sda=Pin(8)
i2c = I2C(scl=Pin(9),sda=Pin(8))
oled = ssd1306.SSD1306_I2C(screen_width, screen_height, i2c)

spce = 1 # characters spacing
pos_x = 0 # X position on the frame buffer to print the text
pos_y = 0 # Y position on the frame buffer to print the text

five = fontlib.font("five (5,5).bmp") # Loads font to ram 

fbuf = framebuf.FrameBuffer(oled.buffer, screen_width, screen_height, framebuf.MONO_VLSB)
fbuf.fill(0)
fontlib.prt("The Quick Gray",pos_x,pos_y,spce,fbuf,five) # prints text using font
oled.show()

see the examples folder to learn how to use it with diferent displays.

Bliting a Sprite

import framebuf
import fontlib
import ssd1306
from machine import Pin,I2C

screen_width = 128
screen_height = 32

#ESP8266 scl=Pin(4),sda=Pin(5)
#ESP32 C3 scl=Pin(9),sda=Pin(8)
i2c = I2C(scl=Pin(9),sda=Pin(8))
oled = ssd1306.SSD1306_I2C(screen_width, screen_height, i2c)

fbuf = framebuf.FrameBuffer(oled.buffer, screen_width, screen_height, framebuf.MONO_VLSB)
fbuf.fill(0)

pos_x = 0 # X position on the frame buffer to blit sprite
pos_y = 0 # Y position on the frame buffer to blit sprite
drawBitmap('image.bmp',pos_x,pos_y,invert=False,fbuf):

oled.show()

How to create new fonts

Most image editors should have a 1bit bmp option when saving bitmaps, I recommend Paint.net, draw 1 pixel white padding around each letters, the file name should include the character size, like the fonts found in the fonts folder. on paint.net if you "save as" and choose bmp it will prompt you with "saving configuration" choose the 1bit option. alternatively you can create the font as a normal bmp file and convert it using Pillow:

from PIL import Image

img = Image.open('input_image.png')
img = img.convert('1')
img.save('output_1bit.bmp')

TODO

  • Load fonts directly from 1bit bitmaps
  • Support for portuguese special characters (ç,á,é,í,ó,ú,â,ê,ô,ã,õ)(Ç,Á,É,Í,Ó,Ú,Â,Ê,Ô,Ã,Õ).
  • Support for color screens

Available fonts:

Futuristic 5X7 [84x48 1.5 Inch Nokia 5110 LCD Screen]:

Five 5X5 (Made by the Author) [84x48 1.5 Inch Nokia 5110 LCD Screen]:

Oldschool 5X7 [84x48 1.5 Inch Nokia 5110 LCD Screen]:

Cellphone 5X7 [84x48 1.5 Inch Nokia 5110 LCD Screen]:

Icons 5X7 (Made by the Author) [84x48 1.5 Inch Nokia 5110 LCD Screen]:

IBM BIOS 8x8 [120x120 1.28 Inch TFT round Screen]:

IBM BIOS 16x16 [240x240 1.3 Inch TFT Screen]:

LICENSE:

this project is MIT licensed

Support

ko-fi

About

micropython library for using 1bit bitmaps as fonts

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published