lv_binding_js icon indicating copy to clipboard operation
lv_binding_js copied to clipboard

Is it possible to load a new font from jsx ?

Open rik43 opened this issue 1 year ago • 15 comments

For example, the default font has no Cyrillic support.

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

rik43 avatar Aug 03 '23 21:08 rik43

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 avatar Aug 04 '23 02:08 derekstavis

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.

derekstavis avatar Aug 04 '23 02:08 derekstavis

follow these steps here

bad link

rik43 avatar Aug 04 '23 05:08 rik43

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?

derekstavis avatar Aug 07 '23 00:08 derekstavis

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

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

kisvegabor avatar Aug 07 '23 14:08 kisvegabor

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).

derekstavis avatar Aug 07 '23 16:08 derekstavis

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?

kisvegabor avatar Aug 08 '23 08:08 kisvegabor

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

derekstavis avatar Aug 08 '23 16:08 derekstavis

Great, we are on the same page then :slightly_smiling_face:

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.

kisvegabor avatar Aug 09 '23 12:08 kisvegabor

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

derekstavis avatar Aug 10 '23 04:08 derekstavis

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

Done :slightly_smiling_face: https://github.com/lvgl/lvgl/issues/4448

kisvegabor avatar Aug 11 '23 18:08 kisvegabor

I got this working locally! Once https://github.com/lvgl/lvgl/pull/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.

derekstavis avatar Aug 23 '23 06:08 derekstavis

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 avatar Aug 23 '23 12:08 kisvegabor

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?

derekstavis avatar Aug 23 '23 16:08 derekstavis

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.

kisvegabor avatar Aug 25 '23 19:08 kisvegabor