Font width for OLED?

Showroom for MicroPython related hardware projects.
Target audience: Users wanting to show off their project!
Post Reply
jumperwire
Posts: 7
Joined: Tue Jul 12, 2022 6:01 pm

Font width for OLED?

Post by jumperwire » Tue Jul 19, 2022 9:33 pm

Hello,

I am trying to implement text scroll for long text lines on OLED display.
To do that, I need to determine the width of the given text.
I am using the following library and fonts

Code: Select all

import upip
upip.install("micropython-oled")

from oled import Write, SSD1306_I2C
from oled.fonts import ubuntu_mono_15, ubuntu_mono_20
...
display = SSD1306_I2C(WIDTH, HEIGHT, i2c)
d20 = Write(display, ubuntu_mono_20)
d20.text("Herzlich Glückwunsch",0,0)

How can I determine text width and if there is no such way, how can I determine the character width for the given font?

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Font width for OLED?

Post by jimmo » Wed Jul 20, 2022 2:51 am

jumperwire wrote:
Tue Jul 19, 2022 9:33 pm
How can I determine text width and if there is no such way, how can I determine the character width for the given font?
https://docs.micropython.org/en/latest/ ... uffer.text

The font is a fixed-size and there's no automatic line breaking. So the length of the text is always len(text)*8.

jumperwire
Posts: 7
Joined: Tue Jul 12, 2022 6:01 pm

Re: Font width for OLED?

Post by jumperwire » Wed Jul 20, 2022 8:40 am

jumperwire wrote:
Tue Jul 19, 2022 9:33 pm
The font is a fixed-size and there's no automatic line breaking. So the length of the text is always len(text)*8.
So the library is using other way, because I clearly see the difference between ubuntu 15 and 20.
With 20 only 13 characters fit in 128pixel OLED. With 15 more than 18 characters.

Any idea?

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Font width for OLED?

Post by Roberthh » Wed Jul 20, 2022 9:15 am

You do not use the built-in fonts, but the font driver of @pythoncoder or derived from it. As far as UI recall, the driver has a method for telling the pixel width of a character or a string. What is the result of

import oled
dir(oled)

jumperwire
Posts: 7
Joined: Tue Jul 12, 2022 6:01 pm

Re: Font width for OLED?

Post by jumperwire » Wed Jul 20, 2022 9:16 am

Roberthh wrote:
Wed Jul 20, 2022 9:15 am
import oled
dir(oled)

Code: Select all

>>> import oled
>>> dir(oled)
['__class__', '__name__', 'write', '__file__', '__path__', 'I2C', 'Pin', 'framebuf', 'time', 'gfx', 'ssd1306', 'lazy', 'fonts', 'GFX', 'SET_CONTRAST', 'SET_ENTIRE_ON', 'SET_NORM_INV', 'SET_DISP', 'SET_MEM_ADDR', 'SET_COL_ADDR', 'SET_PAGE_ADDR', 'SET_DISP_START_LINE', 'SET_SEG_REMAP', 'SET_MUX_RATIO', 'SET_COM_OUT_DIR', 'SET_DISP_OFFSET', 'SET_COM_PIN_CFG', 'SET_DISP_CLK_DIV', 'SET_PRECHARGE', 'SET_VCOM_DESEL', 'SET_CHARGE_PUMP', 'SSD1306', 'SSD1306_I2C', 'SSD1306_SPI', 'Write', 'ubuntu_mono_15', 'ubuntu_mono_20', 'Oled_i2c']

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Font width for OLED?

Post by Roberthh » Wed Jul 20, 2022 9:27 am

No, it seems to be a different library. Where did you get it from? And what is the result of:

dir(oled.ubuntu_mono_15) ?

jumperwire
Posts: 7
Joined: Tue Jul 12, 2022 6:01 pm

Re: Font width for OLED?

Post by jumperwire » Wed Jul 20, 2022 9:30 am

Roberthh wrote:
Wed Jul 20, 2022 9:27 am
No, it seems to be a different library. Where did you get it from? And what is the result of:

dir(oled.ubuntu_mono_15) ?
I followed https://espresso-ide.readthedocs.io/pro ... arted.html
I just used upip.install("micropython-oled").

Code: Select all

>>> dir(oled.ubuntu_mono_15)
['__class__', '__name__', '__file__', '_FONT']
>>> dir(oled.ubuntu_mono_20)
['__class__', '__name__', '__file__', '_FONT']

jumperwire
Posts: 7
Joined: Tue Jul 12, 2022 6:01 pm

Re: Font width for OLED?

Post by jumperwire » Wed Jul 20, 2022 1:48 pm

I will try to approach from the other end.

Could anyone suggest micropython oled library which supports custom font and have some way to determine string/character width?

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Font width for OLED?

Post by Roberthh » Wed Jul 20, 2022 2:16 pm

This is a good choice: https://github.com/peterhinch/micropython-font-to-py of @pythoncoder.
It is first of all a tool to convert ttf font files into python data structures. The repository contains a writer class, which makes use of these fonts and offers a stringlen method. The good thing is, that you can use any fonts. including variable spaced ones.
And it has a detailed documentation-

User avatar
scruss
Posts: 360
Joined: Sat Aug 12, 2017 2:27 pm
Location: Toronto, Canada
Contact:

Re: Font width for OLED?

Post by scruss » Wed Jul 20, 2022 3:13 pm

looking at the files in oled/fonts/ from that package, I'm reasonably sure that the character width is the first field of each entry. For ubuntu_mono_15, that's 7 px. ubuntu_mono_20 is 10 px wide. For the proportional fonts in the distribution, the width is different for every character.

Post Reply