-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
framebuf: Add support for additional fonts in text method #16470
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #16470 +/- ##
==========================================
+ Coverage 98.57% 98.59% +0.01%
==========================================
Files 164 167 +3
Lines 21349 21634 +285
==========================================
+ Hits 21045 21329 +284
- Misses 304 305 +1 ☔ View full report in Codecov by Sentry. |
Code size report:
|
ccfea6c
to
d65f680
Compare
For MCUs with small flash sizes, the code size is hardly acceptable. |
Thank you for your feedback, would it be possible to consider adding conditional compilation for this feature? |
Yes, compile switches will help. |
The maintainers would need evidence that any fonts are copyright-free. |
Thank you for your suggestion. I plan to explore the following options:
|
Thank you for bringing this to my attention. I would appreciate any guidance on how to provide the necessary proof. I can offer the original .pxf files created using PixelForge as evidence of the design process. My detailed workflow is as follows:
I hope this information helps clarify the process and demonstrates the originality of the font designs. Please let me know if there are any additional steps I should take. |
If you designed the fonts yourself there is no problem with copyright. The problem arises where people use tools to convert fonts published by others. Some fonts are commercial products protected by copyright. FYI step 3 above is not necessary: font-to-py can handle TTF files. |
Thank you for the information and for sharing micropython-font-to-py. I appreciate it! |
I have developed an approach starts with a regular proportional font (up to 5 columns) and adds additional information(3 bytes to utilize, still within the original 8 columns font bytes) for each column type, allowing conversion into three other font styles: regular & mono, bold & proportional, and bold & mono. There are 3 types of modifications:
And there are 5 types of columns:
Example transformations: a: This one is challenging; I need to redesign the font and try it later. b: 4 columns, 0 da, 1 m, 2 -, 3 dp
c: 4 columns, 0 dn, 1 m, 2 -, 3 - d: 4 columns, 0 dn, 1 m, 2 -, 3 da i: 2 columns, 0 -, 1 da j: 2 columns, 0 -, 1 da |
a0982f8
to
89775d0
Compare
Hello everyone, I've redesigned the fonts and their runtime conversion, but I've encountered a couple of issues:
I would appreciate any suggestions or assistance. Thank you! |
Unpacking the different fonts from a common data set required additional code, requiring space and execution time. |
I realize that not every MicroPython user will need to render characters on the screen, so it's best to avoid the extra code space. The initial idea is to use a compile-time switch to decide whether to support proportional fonts. If this option is disabled during compilation, only monospace fonts will be supported while maintaining interface compatibility. This can be achieved with simple conversion code, supporting only two font types: 6-pixel wide and 8-pixel wide monospace fonts. Ideally, each character's font information would only occupy 6 bytes, saving 2 bytes per character (from 8 to 6), thus leaving some space for interface compatibility and simple conversion code. |
For me the 6x8 font and the 6x8 proportional bold fonts look best, much better than the existing 8x8 font. So my preference would be to just replace the 8x8 by the 6x8 font with an compile option to enable the other fonts as well. The 6x8 font may be smaller than the existing 8x8 font, saving a few bytes of code. And for boards with lots of flash space it would not matter to have the extra fonts & code. |
Thank you for your feedback and suggestions! I'm glad to hear your thoughts on the font choices, and I appreciate your preference for the 6x8 font and the 6x8 proportional bold fonts over the existing 8x8 font.
|
Another option for providing fonts could be to proved the font directly as data from a buffer instead as font ID to the write text API. That font data could be contained in a file, which in case of the upcoming romfs file system could be directly referenced. In case of a regular file it would he to be loaded to e.g. a bytearray. |
Thank you for the suggestion. This approach seems somewhat similar to font-to-py, and I feel that its complexity and the shift in the overall approach might be a bit beyond my current capabilities. I've made some progress with the method we discussed earlier about using a compile switch to simplify the font types, hoping to keep it within the same or even smaller firmware size. |
There is always the option of using the writer class to render a Python font file to a framebuf. Via |
After adding the compile switch When configuring the compile switch in |
Signed-off-by: Eluli Zhang <elulis@gmail.com>
@robert-hh @peterhinch Please help me on this pull request. The firmware space issues was solved by adding a compile switch as the last comment shown. Thanks for any advices. |
I think the suggestion of @robert-hh is the way to approach this, because of its generality. Also because the font itself doesn't add to the firmware size. A suggested approach: The user can specify any font. The So I suggest that the user creates a file with a predefined name containing the font of choice. The C code looks for the file in ROMFS and, if found, accesses the bytes directly. In essence you are providing a minimal |
Summary
Added an optional
font_id
parameter to the FrameBuffer.text method. Thefont_id
allows selection among different font styles: 0, 1, 2, and 3. The method now returns the width of the drawn text in pixels. Updated the documentation to reflect the new font options:0
: z1mono8_6x8 — Monospace & Regular1
: z1mono8b_8x8 — Monospace & Bold (replacing the original petme128)2
: z1prop8_6x8 — Proportional & Regular3
: z1prop8b_8x8 — Proportional & BoldThe
font_z1fonts.h
replacesfont_petme128_8x8.h
at the same size (8 bytes for each character) and stores a proportional & regular font with additional information for runtime font conversion.This enhancement allows users to select from four different font styles. The method now also returns the width of the drawn text in pixels, which can be useful for layout calculations. The new fonts were designed using PixelForge and are released under the MIT license.
Testing
I verified the changes by running the
tests/extmod/framebuf1.py
script on theports/unix
build of MicroPython to ensure forward compatibility. The script and expected outputs were updated accordingly. Additionally, I built the firmware for the ESP32-C6 usingports/esp32
and tested it with a monochrome LCD, confirming that the new font functionality works as expected.Trade-offs and Alternatives
After adding the compile switch
MICROPY_PY_FRAMEBUF_ALL_FONTS
which is disabled by default, the firmware space finally reduced to below original size. At this point, the font preview for font_id 0/1/2/3 is as follows (equivalent to only having two fonts, 0/1):When configuring the compile switch in
mpconfigport.h
, the firmware space is expected to consume an additional 300 bytes. At this point, all four fonts are supported as the first demo preview shown.(edit, the following ports have enabled:
ports/unix
,ports/rp2
,ports/esp32
)