esp-idf-ssd1306/BdfFontDemo/README.md

320 lines
7.6 KiB
Markdown
Raw Normal View History

2024-09-10 18:35:03 -04:00
# BdfFontDemo for SSD1306
2024-09-10 20:56:52 -04:00
![BdfFontDemo-1](https://github.com/user-attachments/assets/df733de1-b5b8-4142-8123-5a8f992246ff)
2024-09-10 18:35:03 -04:00
![BdfFontDemo-2](https://github.com/user-attachments/assets/224d12c4-8ecf-4116-868a-8f11ca480122)
![BdfFontDemo-3](https://github.com/user-attachments/assets/4dbb6f39-ae0f-4ca2-9e55-ba32476b018e)
u8g2 is a great Library for monochrome displays.
2024-09-10 18:36:24 -04:00
This library contains a lot of BDF format fonts.
2024-09-10 18:35:03 -04:00
This project is a demo that displays BDF format fonts.
# How to use BDF Font
2024-09-10 18:39:39 -04:00
- Download the BDF font file.
Thank you olikraus for releasing useful software.
2024-09-10 23:47:07 -04:00
```
2024-09-11 19:33:46 -04:00
cd $HOME
2024-09-10 23:47:07 -04:00
git clone https://github.com/olikraus/u8g2
ls $HOME/u8g2/tools/font/bdf
```
2024-09-10 18:35:03 -04:00
- Convert BDF font files to header files.
2024-09-11 00:29:34 -04:00
I based it on [this](https://github.com/pixelmatix/bdf2c).
2024-09-10 18:38:28 -04:00
Thank you Pixelmatix for releasing useful software.
2024-09-10 18:36:24 -04:00
```
2024-09-10 19:10:27 -04:00
cd esp-idf-ssd1306/BdfFontDemo
2024-09-10 18:36:24 -04:00
cc -o bdf2c bdf2c.c
2024-09-10 22:34:38 -04:00
```
2024-09-10 18:36:24 -04:00
2024-09-10 22:34:38 -04:00
Specify the font variable name with the -n option.
```
2024-09-10 18:36:24 -04:00
./bdf2c -n ncenR12 -b < $HOME/u8g2-hal-esp-idf/examples/i2c/components/u8g2/tools/font/bdf/ncenR12.bdf > main/ncenR12.h
./bdf2c -n timR12 -b < $HOME/u8g2-hal-esp-idf/examples/i2c/components/u8g2/tools/font/bdf/timR12.bdf > main/timR12.h
./bdf2c -n emoticons -b < $HOME/u8g2-hal-esp-idf/examples/i2c/components/u8g2/tools/font/emoticons/emoticons21.bdf > main/emoticons.h
./bdf2c -n Scroll_o_Sprites -b < $HOME/u8g2-hal-esp-idf/examples/i2c/components/u8g2/tools/font/pbm/Scroll-o-Sprites.bdf > main/Scroll-o-Sprites.h
```
2024-09-10 18:35:03 -04:00
2024-09-10 22:39:53 -04:00
Characters that cannot be used in C language variable names will result in an error during compilation.
This is unacceptable.
```
./bdf2c -n Scroll-o_Sprites -b < $HOME/u8g2-hal-esp-idf/examples/i2c/components/u8g2/tools/font/pbm/Scroll-o-Sprites.bdf > main/Scroll-o-Sprites.h
```
2024-09-10 22:34:38 -04:00
2024-09-10 18:35:03 -04:00
- BDF font files can be viewed with the following command.
2024-09-10 18:36:24 -04:00
```
./bdf2c -B < $HOME/u8g2-hal-esp-idf/examples/i2c/components/u8g2/tools/font/bdf/ncenR12.bdf | more
./bdf2c -B < $HOME/u8g2-hal-esp-idf/examples/i2c/components/u8g2/tools/font/bdf/timR12.bdf | more
./bdf2c -B < $HOME/u8g2-hal-esp-idf/examples/i2c/components/u8g2/tools/font/emoticons/emoticons21.bdf | more
./bdf2c -B < $HOME/u8g2-hal-esp-idf/examples/i2c/components/u8g2/tools/font/pbm/Scroll-o-Sprites.bdf | more
```
2024-09-10 18:35:03 -04:00
2024-09-10 18:38:28 -04:00
- Add header files to main.c.
2024-09-10 18:36:24 -04:00
```
#include "ncenR12.h"
#include "timR12.h"
#include "emoticons.h"
#include "Scroll-o-Sprites.h"
```
2024-09-10 18:35:03 -04:00
- Show BDF Font
2024-09-10 18:36:24 -04:00
```
ssd1306_clear_screen(&dev, false);
ssd1306_contrast(&dev, 0xff);
2024-09-10 18:43:20 -04:00
show_bdf_font_text(&dev, __ncenR12_bitmap__, "Hello World", 0, 0);
2024-09-10 18:36:24 -04:00
```
2024-09-10 18:43:20 -04:00
# Font display position
The font's display position starts from the upper left corner of this image.
Therefore, there will be some white space above the font.
```
________,________,________,
________,________,________,
________,________,________,
________,________,________,
________,________,________,
________,________,________,
________,________,________,
________,________,________,
2024-09-10 20:46:34 -04:00
______XX,X_______,________,
_____X__,_X______,________,
____X___,_XX_____,________,
____XX__,_XX_____,________,
____XX__,_XX_____,________,
________,XX______,________,
________,X_______,________,
_______X,________,________,
______X_,__X_____,________,
_____X__,__X_____,________,
____XXXX,XXX_____,________,
____XXXX,XXX_____,________,
2024-09-10 18:43:20 -04:00
________,________,________,
________,________,________,
________,________,________,
________,________,________,
________,________,________,
________,________,________,
2024-09-10 18:53:56 -04:00
```
2024-09-10 19:20:46 -04:00
# Font bounding box and character bounding box
2024-09-10 19:20:03 -04:00
BDF font file has a font bounding box (FONTBOUNDINGBOX).
2024-09-10 21:33:54 -04:00
This FONTBOUNDINGBOX value indicates that all fonts must have a 3-pixel margin to the left and a 6-pixel margin below the font.
2024-09-12 03:28:45 -04:00
By providing a FONTBOUNDINGBOX, this can omit the left edge and top and bottom whitespace from the font pattern.
2024-09-10 19:20:03 -04:00
```
FONTBOUNDINGBOX 21 26 -3 -6
```
2024-09-10 21:38:51 -04:00
Each character has a character-specific bounding box (BBX).
This BBX value indicates that there should be a 1 pixel margin to the left of this character.
2024-09-10 21:31:51 -04:00
By providing a character-specific bounding box (BBX), this can omit the leftmost blank space.
2024-09-10 18:53:56 -04:00
```
STARTCHAR d
ENCODING 100
SWIDTH 574 0
DWIDTH 10 0
BBX 8 12 1 0
BITMAP
0E
06
06
06
3E
66
C6
C6
C6
C6
66
3B
ENDCHAR
```
2024-09-10 21:31:51 -04:00
Fonts that take character-specific bounding box (BBX) into consideration change as follows.
2024-09-10 21:42:13 -04:00
This creates a 1 pixel blank space on the left edge.
2024-09-10 19:02:46 -04:00
```
STARTCHAR d
ENCODING 100
SWIDTH 574 0
2024-09-10 20:46:34 -04:00
DWIDTH 11 0
2024-09-10 19:02:46 -04:00
BBX 9 12 0 0
BITMAP
07
03
03
03
1F
33
63
63
63
63
33
1D8
ENDCHAR
```
2024-09-10 21:31:51 -04:00
Fonts that take character-specific bounding box (BBX) and font bounding box (FONTBOUNDINGBOX) into consideration change as follows.
2024-09-10 19:30:48 -04:00
```
STARTCHAR d
ENCODING 100
SWIDTH 574 0
2024-09-10 20:46:34 -04:00
DWIDTH 14 0
BBX 9 12 0 0
2024-09-10 19:30:48 -04:00
BITMAP
00
00
00
00
00
00
00
2024-09-10 19:34:23 -04:00
00
2024-09-10 20:46:34 -04:00
00E0
0060
0060
0060
03E0
0660
0C60
0C60
0C60
0C60
0660
030B
2024-09-10 19:30:48 -04:00
00
00
00
00
00
00
ENDCHAR
```
2024-09-10 21:44:52 -04:00
Both the font bounding box (FONTBOUNDINGBOX) and character-specific bounding box (BBX) determine pixels position.
2024-09-10 21:42:13 -04:00
This creates a 4 pixel blank space on the left edge.
This creates a 6 pixel blank space on the buttomt edge.
2024-09-10 21:31:51 -04:00
This image takes into account the font bounding box (FONTBOUNDINGBOX) and character-specific bounding box (BBX).
2024-09-10 18:53:56 -04:00
```
________,________,________,
________,________,________,
________,________,________,
________,________,________,
________,________,________,
________,________,________,
________,________,________,
2024-09-10 19:34:23 -04:00
________,________,________,
2024-09-10 20:46:34 -04:00
________,XXX_____,________,
________,_XX_____,________,
________,_XX_____,________,
________,_XX_____,________,
______XX,XXX_____,________,
_____XX_,_XX_____,________,
____XX__,_XX_____,________,
____XX__,_XX_____,________,
____XX__,_XX_____,________,
____XX__,_XX_____,________,
_____XX_,_XX_____,________,
______XX,X_XX____,________,
2024-09-10 18:53:56 -04:00
________,________,________,
________,________,________,
________,________,________,
________,________,________,
________,________,________,
________,________,________,
```
2024-09-10 22:05:41 -04:00
# Heder file format
This indicates the width of the FONTBOUNDINGBOX.
2024-09-10 22:06:05 -04:00
```
2024-09-10 22:05:41 -04:00
// fontboundingbox_width
21,
```
This indicates the height of the FONTBOUNDINGBOX.
```
// fontboundingbox_height
26,
```
This shows the font information for the characters.
```
// 'd'
100,14,8,12,1,0,39,8,20,0x00,0xe0,0x00,0x00,---------------,0x00,0x00,0x00,
| | | | | | | | | |
| | | | | | | | | +- Font pattern
| | | | | | | | +---- Last row in Y direction
| | | | | | | +------ First row in Y direction
| | | | | | +--------- Number of font patterns
| | | | | +----------- BBX Y offset
| | | | +------------- BBX X offset
| | | +---------------- BBX Y pixel
| | +------------------ BBX X pixel
| +--------------------- Font width pixel
+------------------------ Encoding
2024-09-10 22:09:59 -04:00
```
2024-09-10 22:11:02 -04:00
You can find out the width (in bytes) of this font using the following calculation:
2024-09-10 22:09:59 -04:00
```
{Number of font patterns} / ({Last row in Y direction} - {First row in Y direction} + 1)
```
2024-09-10 22:05:41 -04:00
2024-09-10 22:09:59 -04:00
In ths case:
2024-09-10 22:05:41 -04:00
```
2024-09-10 22:10:08 -04:00
39 / (20 - 8 + 1) = 3[bytes]
2024-09-10 22:09:59 -04:00
```
2024-09-10 22:13:59 -04:00
The font for lines 8 to 20 will be as follows:
2024-09-10 22:20:56 -04:00
This is a bitmap of 24*13 pixels.
2024-09-10 22:13:59 -04:00
```
0x00,0xe0,0x00,
0x00,0x60,0x00,
0x00,0x60,0x00,
0x00,0x60,0x00,
0x03,0xe0,0x00,
0x06,0x60,0x00,
0x0c,0x60,0x00,
0x0c,0x60,0x00,
0x0c,0x60,0x00,
0x0c,0x60,0x00,
0x06,0x60,0x00,
0x03,0xb0,0x00,
0x00,0x00,0x00,
```
2024-09-10 22:26:12 -04:00
You can display this bitmap with the following function.
```
ssd1306_bitmaps(&dev, xpos, ypos+bdf_font.y_start, bitmap, 24, 13, false);
```
2024-09-10 22:09:59 -04:00
2024-09-10 22:05:41 -04:00
This marks the end of the font.
```
// EOF
0,0,0,0,0,0,0,0,0
```
2024-09-10 23:05:07 -04:00
2024-09-10 23:08:08 -04:00
# BDF Font file editor
2024-09-10 23:05:07 -04:00
I use [this](http://hukka.ncn.fi/?fony).
2024-09-10 23:11:40 -04:00
It's work fine on Windows10.
2024-09-10 23:07:25 -04:00
There are probably many more.
2024-09-10 23:05:07 -04:00
- Open BDF font file.
File->Import->BDF Font.
2024-09-10 23:06:01 -04:00
2024-09-10 23:05:07 -04:00
![fony](https://github.com/user-attachments/assets/18c71509-9a66-4b23-a2cf-6ecc66bf5920)
- Save BDF font file.
2024-09-10 23:08:53 -04:00
File->Export->BDF Font.
2024-09-10 23:05:07 -04:00