pop-os / cosmic-text

Pure Rust multi-line text handling

Home Page:https://pop-os.github.io/cosmic-text/cosmic_text/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Android: Load fonts from /system/fonts by default?

hrydgard opened this issue · comments

On Android, cosmic-text will crash by default since it doesn't find any fonts. Once you do add fonts though, it works just fine, which is great.

On every Android device, a set of default fonts is located in /system/fonts, which is user-readable. This does not appear to be documented by the Android project officially anywhere - nevertheless, it's a fact that you'll find these fonts in that folder on every device, and there surely are lots of apps out there that depend on this.

Here's all I needed to do to get basic font families to work on Android (although there might also be some work to be done with fallbacks):

        let mut font_system = cosmic_text::FontSystem::new();

        #[cfg(target_os = "android")]
        {
            font_system.db_mut().load_fonts_dir("/system/fonts");
            font_system.db_mut().set_sans_serif_family("Roboto");
            font_system.db_mut().set_serif_family("Noto Serif");
            font_system.db_mut().set_monospace_family("Droid Sans Mono"); // Cutive Mono looks more printer-like
            font_system.db_mut().set_cursive_family("Dancing Script");
            font_system.db_mut().set_fantasy_family("Dancing Script");
        }

        // ... use the FontSystem as normal

If the project doesn't want to add this, at least this Github issue will now be searchable to find this workaround.

I'm not sure exactly how to set up the CJK fallback fonts, though. I've logged the info about the found faces from a PocoPhone F1 here.

You could make a pr to fontdb to add this folder

Right, I suppose this issue belongs more in the fontdb repo, although the fallback code is in the cosmic-text repo: https://github.com/pop-os/cosmic-text/tree/main/src/font/fallback

Yes, there may need to be custom fallback code in cosmic-text, but the system font directories should be added in fontdb so other projects using it find the fonts as well.