Page 1 of 1
Font width for OLED?
Posted: Tue Jul 19, 2022 9:33 pm
by jumperwire
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?
Re: Font width for OLED?
Posted: Wed Jul 20, 2022 2:51 am
by jimmo
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.
Re: Font width for OLED?
Posted: Wed Jul 20, 2022 8:40 am
by jumperwire
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?
Re: Font width for OLED?
Posted: Wed Jul 20, 2022 9:15 am
by Roberthh
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)
Re: Font width for OLED?
Posted: Wed Jul 20, 2022 9:16 am
by jumperwire
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']
Re: Font width for OLED?
Posted: Wed Jul 20, 2022 9:27 am
by Roberthh
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) ?
Re: Font width for OLED?
Posted: Wed Jul 20, 2022 9:30 am
by jumperwire
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']
Re: Font width for OLED?
Posted: Wed Jul 20, 2022 1:48 pm
by jumperwire
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?
Re: Font width for OLED?
Posted: Wed Jul 20, 2022 2:16 pm
by Roberthh
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-
Re: Font width for OLED?
Posted: Wed Jul 20, 2022 3:13 pm
by scruss
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.