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

Is it possible to load a new font from jsx ? #15

Open
rik43 opened this issue Aug 3, 2023 · 15 comments
Open

Is it possible to load a new font from jsx ? #15

rik43 opened this issue Aug 3, 2023 · 15 comments

Comments

@rik43
Copy link

rik43 commented Aug 3, 2023

For example, the default font has no Cyrillic support.

api for C:
https://docs.lvgl.io/master/overview/font.html

@derekstavis
Copy link
Collaborator

derekstavis commented Aug 4, 2023

Currently this is not supported through the JavaScript binding. You should be able though to go into the LVGL dependency that is vendored in this repo and follow these steps here to get a binary that replaces the builtin font with your custom glyphs.

@derekstavis
Copy link
Collaborator

PRs are welcome though. I feel this should be a fairly low lift, but we would need to build a separate Font API to handle this in JavaScript (font loading is not part of React APIs), similar to how Dimensions API work.

@rik43
Copy link
Author

rik43 commented Aug 4, 2023

follow these steps here

bad link

@derekstavis
Copy link
Collaborator

Sorry, here's the right link. Nothing special more than LVGL Font documentation... See the "Add a new font" section.

I tried to take a stab at a way to load fonts from JS, but it seems like LVGL FS is not supported in this binding. @kisvegabor is there a way to load fonts from a buffer instead of using LVGL FS?

@kisvegabor
Copy link
Member

is there a way to load fonts from a buffer instead of using LVGL FS?

There is no way for it at this moment. 🙁
Is it critical for the JS binding or rather just a nice to have?

@derekstavis
Copy link
Collaborator

Is it critical for the JS binding or rather just a nice to have?

Images are currently handled through a buffer, so I thought it would be nice to have similar handling for fonts, so that bindings that already have FS support could use their own APIs.

In the meantime, I will look into how to integrate with the LVGL FS API as an alternative. The benefit is that it should work for every platform supported by LVGL (for example, I'm aiming to have an ESP32 build target).

@kisvegabor
Copy link
Member

We can do 2 things:

  1. Add an lv_font_load_from_buffer() function: it would be faster than the file system as lv_fs_read doesn't have to copy anything, just return a pointer.
  2. Add a special file system driver that can read from a buffer. It's hacky, slower but generic.

I vote for 1). We could use some function pointers like read_cb which could point lv_fs_read or a buf_read function. This way we don't need to add a bunch of duplication.

What do you think?

@derekstavis
Copy link
Collaborator

derekstavis commented Aug 8, 2023

Option 1 sounds like the cleanest approach to me, as it allows to leverage virtually any loading method -- generating a header file with a a statically allocated buffer, loading from an HTTP source, loading from a native binding, etc -- especially if it doesn't require including and compiling the FS module/drivers

@kisvegabor
Copy link
Member

Great, we are on the same page then 🙂

Are you interested in adding this feature to LVGL? No problem if not, I can open an issue an see if someone picks it up.

@derekstavis
Copy link
Collaborator

Sounds like a fun little task to take on :) but right now kind of limited in bandwidth to ramp up in a whole new environment 😛

Feel free to open an issue! If someone picks up, great, otherwise I may pick it up in the future. There's a ton I still need to learn about the platform I'm targeting

@kisvegabor
Copy link
Member

Feel free to open an issue! If someone picks up, great, otherwise I may pick it up in the future.

Done 🙂 lvgl/lvgl#4448

@derekstavis
Copy link
Collaborator

derekstavis commented Aug 23, 2023

I got this working locally! Once lvgl/lvgl#4462 lands in LVGL master it should be possible to load fonts using an API similar to the one below:

image

More sizes and fonts:

Inter Poppins
image image

Open for feedback on the font loading and registering API:

  • The Font constructor takes a JS buffer and creates a new lv_font_t from the buffer that is returned to JS as an instance of Font.
  • The Font.load class method takes an URL, either local or remote, loads it asynchronously from the filesystem or network, and returns a new instance of Font.
  • The Font.register class method associates a font family and size to a Font instance so that styles can use font-family as a string and font-size as a number.

The caveat of this font loading work is that it will depend on LVGL master branch. This binding master currently points to LVGL v8. I genuinely do not have experience with the stability of LVGL master branch, so in the meantime I can push this into a separate LVGL master-compatible branch.

@kisvegabor
Copy link
Member

The caveat of this font loading work is that it will depend on LVGL master branch. This binding master currently points to LVGL v8. I genuinely do not have experience with the stability of LVGL master branch, so in the meantime I can push this into a separate LVGL master-compatible branch.

LVGL master is really not stable yet, so I recommend creating a temporal lvgl-v9-dev branch (or so).

@derekstavis
Copy link
Collaborator

The caveat of this font loading work is that it will depend on LVGL master branch. This binding master currently points to LVGL v8. I genuinely do not have experience with the stability of LVGL master branch, so in the meantime I can push this into a separate LVGL master-compatible branch.

LVGL master is really not stable yet, so I recommend creating a temporal lvgl-v9-dev branch (or so).

@kisvegabor is it common for features to be back ported to v8? If so, would it make sense to back port this API?

@kisvegabor
Copy link
Member

Normally we rarely backport features to v8 as we are not planning to create new v8 minor releases. However if it's important for the JS binding we can make an exception.

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

No branches or pull requests

3 participants