Skip to content
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

Changing font format to LVGL embedded format and adding a python tool to create fonts #168

Open
wants to merge 33 commits into
base: dev
Choose a base branch
from

Conversation

raphatex
Copy link

@raphatex raphatex commented Nov 18, 2024

  • Adding diacritics in translations.h
  • TODO : Creating a python tool for font generation
  • TODO : Updating Fonts

(Actually I innitially wanted to generate fonts with the .exe and then create the .py for later use but If think I'm gonna start with the .py)

Copy link
Author

@raphatex raphatex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually "Creating : converter.py" sorry I messed the commit message

@raphatex
Copy link
Author

raphatex commented Nov 19, 2024

So I have created a very crude python font converter
Todo:

  • improve quality (~the same as the exe if possible)
  • make it more user friendly
  • Make a GUI

@ducalex
Copy link
Owner

ducalex commented Nov 19, 2024

I'm impressed that you've already made a python tool, this is nice!

Now that retro-go will ship with a generator, I think we should add the .ttf (or whatever) fonts to the repo to preserve as reference if someone wants to regenerate the .c fonts with more glyphs or sizes.

@raphatex
Copy link
Author

Actually I cheated a bit, I used ChatGPT to help me with the .ttf font extraction ;)
But understanding the custom encoding for the output took me a few hours 😅
I documented all my research directly at the beginning of the python converter for future dev.

Right now the tool does not make results as good as the .exe I still have to do some adjustments.

I agree to add the .tff in the repo !

adding settings fields for the user
@raphatex
Copy link
Author

raphatex commented Nov 19, 2024

I created a UI with a font renderer ! To make it easier to change font settings

image

@ducalex
Copy link
Owner

ducalex commented Nov 20, 2024

Oh wow, having a preview is a game changer! I remember having to flash retro-go 50+ times back when I was trying to find the perfect parameters for the .exe generator...

I might delete some of them later...
@raphatex
Copy link
Author

raphatex commented Nov 20, 2024

Yeah absolutely I've been through this struggle too with Open Sans but now it's gonna be way faster ^^
I'm currently trying to add a way to directly "edit" the c font like Paint software directly on the canvas that would be the cherry on the cake for the tool !

@ducalex ducalex force-pushed the dev branch 3 times, most recently from c696780 to 2e778fa Compare November 20, 2024 18:52
@raphatex
Copy link
Author

I was looking on the web for inspirations from other tools and I came across LVGL font converter
https://lvgl.io/tools/fontconverter
This tool uses a special type to store the "header" for each character and precomputes the index of the first byte of each character.
I feel like this approach is better than Retro-Go's one in terms of performance. Only issue is that it uses a bit more memory to store the indexes (probably around 16bit(2 Bytes) more per character)
In the end we should loose around 161*2 = 322 Bytes / 2.5Kbit per font or 1.6KBytes / 12.8Kbit for 5 fonts.

So I was wondering if it was worth the try since we are making our own tool ?

/*-----------------
 *    BITMAPS
 *----------------*/

/*Store the image of the glyphs*/
static LV_ATTRIBUTE_LARGE_CONST const uint8_t glyph_bitmap[] = {
    /* U+0020 " " */
    0x0,

    /* U+0021 "!" */
    0xff, 0xcf,

    /* U+0022 "\"" */
    0xff, 0xf0,

    /* U+0023 "#" */
    0x16, 0x14, 0x7f, 0x24, 0x24, 0xfe, 0x28, 0x68,

    /* U+0024 "$" */
    0x10, 0xf9, 0x43, 0x87, 0xc3, 0xc5, 0xbe, 0x10,

    /* U+0025 "%" */
    0x71, 0x1b, 0x23, 0x68, 0x3a, 0x0, 0x5c, 0x16,
    0xc4, 0xd8, 0x8e,

    /* U+0026 "&" */
    0x3c, 0x30, 0x1c, 0x6, 0xf, 0xb6, 0x7b, 0x18,
    0xfe,

    /* U+0027 "'" */
    0xfc,

    /* U+0028 "(" */
    0x6f, 0x6d, 0xb3, 0x60,

    /* U+0029 ")" */
    0xd9, 0xb6, 0xde, 0xc0,
};

/*---------------------
 *  GLYPH DESCRIPTION
 *--------------------*/

static const lv_font_fmt_txt_glyph_dsc_t glyph_dsc[] = {
    {.bitmap_index = 0, .adv_w = 0, .box_w = 0, .box_h = 0, .ofs_x = 0, .ofs_y = 0} /* id = 0 reserved */,
    {.bitmap_index = 0, .adv_w = 61, .box_w = 1, .box_h = 1, .ofs_x = 0, .ofs_y = 0},
    {.bitmap_index = 1, .adv_w = 80, .box_w = 2, .box_h = 8, .ofs_x = 2, .ofs_y = 0},
    {.bitmap_index = 3, .adv_w = 92, .box_w = 4, .box_h = 3, .ofs_x = 1, .ofs_y = 5},
    {.bitmap_index = 5, .adv_w = 147, .box_w = 8, .box_h = 8, .ofs_x = 1, .ofs_y = 0},
    {.bitmap_index = 13, .adv_w = 122, .box_w = 7, .box_h = 9, .ofs_x = 0, .ofs_y = -1},
    {.bitmap_index = 21, .adv_w = 176, .box_w = 11, .box_h = 8, .ofs_x = 1, .ofs_y = 0},
    {.bitmap_index = 32, .adv_w = 153, .box_w = 9, .box_h = 8, .ofs_x = 1, .ofs_y = 0},
    {.bitmap_index = 41, .adv_w = 54, .box_w = 2, .box_h = 3, .ofs_x = 1, .ofs_y = 5},
    {.bitmap_index = 42, .adv_w = 80, .box_w = 3, .box_h = 9, .ofs_x = 1, .ofs_y = -1},
    {.bitmap_index = 46, .adv_w = 80, .box_w = 3, .box_h = 9, .ofs_x = 1, .ofs_y = -1},
    {.bitmap_index = 50, .adv_w = 92, .box_w = 5, .box_h = 5, .ofs_x = 0, .ofs_y = 3},
    {.bitmap_index = 54, .adv_w = 147, .box_w = 7, .box_h = 6, .ofs_x = 1, .ofs_y = 0},
    {.bitmap_index = 60, .adv_w = 67, .box_w = 3, .box_h = 3, .ofs_x = 0, .ofs_y = -2},
    {.bitmap_index = 62, .adv_w = 73, .box_w = 3, .box_h = 1, .ofs_x = 1, .ofs_y = 3},
    {.bitmap_index = 63, .adv_w = 67, .box_w = 2, .box_h = 2, .ofs_x = 1, .ofs_y = 0}
};

@raphatex raphatex changed the title Adding diacritics support in th UI Adding diacritics support in the UI Nov 22, 2024
@ducalex
Copy link
Owner

ducalex commented Nov 27, 2024

I feel like this approach is better than Retro-Go's one in terms of performance. Only issue is that it uses a bit more memory to store the indexes (probably around 16bit(2 Bytes) more per character) In the end we should loose around 161*2 = 322 Bytes / 2.5Kbit per font or 1.6KBytes / 12.8Kbit for 5 fonts.

So I was wondering if it was worth the try since we are making our own tool ?

Yes, if you feel there is a better way then you can rewrite our font format and renderer to something completely different. And if you convert the basic font to your new format then we could eventually ditch the two other rendering modes as well.

It would be nice to have a single format for all fonts at last, that is fully documented and with a generator.

The size of the font in flash doesn't bother me, as long as your new renderer doesn't use more stack space and isn't slower than the current one, I will accept it happily :) If anything a 16bit index seems like it would be a win, we could add more like languages like cyrillic glyphs or even add some custom icons to replace things like the wifi/battery icons.

If you do decide to write a new renderer, I'd request you update your PR description to match this new goal (for clarity).

@ducalex ducalex force-pushed the dev branch 4 times, most recently from f2d4a67 to 1069d3a Compare November 30, 2024 19:11
@raphatex raphatex changed the title Adding diacritics support in the UI Changing font format to LVGL embedded format and adding a python tool to create fonts Dec 10, 2024
Copy link
Author

@raphatex raphatex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I didn't had enough time in the past days but I will try to work on the new font system during this week !

@ducalex
Copy link
Owner

ducalex commented Dec 18, 2024

I'm going to tag a new retro-go release before merging this PR. I want to be sure that this PR is merged in a known stable base because every time I touch the rendering code it introduces subtle bugs and this time won't be any different I'm sure...

What's the status on your side? Do you feel this PR is ready for review from my side?

@raphatex
Copy link
Author

raphatex commented Dec 18, 2024

I'm going to tag a new retro-go release before merging this PR. I want to be sure that this PR is merged in a known stable base because every time I touch the rendering code it introduces subtle bugs and this time won't be any different I'm sure...

What's the status on your side? Do you feel this PR is ready for review from my side?

On my side :

  • the renderer code is done (I might do some adjustment on this line: yOffset = glyph_dsc[c].ofs_y-2)
  • the C font editor python program is 99% working but in the future i'd like to improve it to get more personnalization options (+ icons creation)
  • the font converter is working and produces decent results but not 100% pixel perfect. I have to find the right settings for the advance_width but it's just a matter of time.

Lastly I have to write some documentation on the format

@ducalex ducalex force-pushed the dev branch 4 times, most recently from 93c53f4 to 48a164a Compare December 24, 2024 18:43
@raphatex
Copy link
Author

I updated the font editor so we can now select a single character on the preview and edit it, here is a screenshot:

image

I now have to add more personalizations options...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants