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

Feature request: loading fonts from SPIFFS/LittleFS #30

Open
marcelstoer opened this issue May 8, 2023 · 10 comments
Open

Feature request: loading fonts from SPIFFS/LittleFS #30

marcelstoer opened this issue May 8, 2023 · 10 comments
Labels
progress: waiting for confirmation and close Waiting for confirmation and close

Comments

@marcelstoer
Copy link

Maybe I didn't quite get it yet but from what I understand OpenFontRender can only load fonts from SD card or binary (i.e. embedded in code).

Working with ESP32 it would be most helpful to be able to load fonts from the internal LittleFS file system.

@infrafast
Copy link

Hello I have same understanding and need. Loading from LittleFS Thanks!

@infrafast
Copy link

Here is the code I've used:

  // Open the font file
  File fontFile = LittleFS.open("/myfont.ttf", "r");
  if (!fontFile) {
    DEBUG_PRINT("Failed to open icon file");
  }

  // Read font file contents into a buffer
  size_t fileSize = fontFile.size();
  uint8_t* fontBuffer = (uint8_t*)malloc(fileSize);
  fontFile.read(fontBuffer, fileSize);
  fontFile.close();
  if (iconOfr.loadFont(fontBuffer, fileSize)) { 
    DEBUG_PRINT("\nIcon Initialise error"); 
  }

hope this helps

@takkaO
Copy link
Owner

takkaO commented May 8, 2024

@marcelstoer , @infrafast

OpenFontRender v1.2 now includes enhanced support for file systems.
Font loading from SPIFFS is also supported and can be enabled simply by using a preset.
Unfortunately, no LittleFS presets are provided yet, but it should be possible to support the LittleFS file system by implementing custom code.
Please see document and sample code for more info.
Welcome your feedback.

Sorry for the crazy slow response.

@takkaO takkaO added the progress: waiting for confirmation and close Waiting for confirmation and close label May 8, 2024
@infrafast
Copy link

Hi thanks a lot, I suggest you enapsulate the code I provided for littleFS in a method of your class then

@giri-v
Copy link

giri-v commented Jan 12, 2025

I tried this and while:
SPIFFS.exists("/Roboto.ttf") returns true
ofr.loadFont("/Roboto.ttf") fails with FT_Stream_Open: could not open `/Roboto.ttf'

I am using TFT_eSPI with PlatformIO on an ESP32 (although I'm not sure that is relevant)

@takkaO
Copy link
Owner

takkaO commented Jan 12, 2025

@giri-v

Could you share me your code?

@giri-v
Copy link

giri-v commented Jan 15, 2025

Here it is...
Init Code

 void initFS()
{
#ifndef LittleFS
    // Initialize SPIFFS
    if (!SPIFFS.begin(true))
    {
        Log.errorln("An Error has occurred while mounting SPIFFS");
        return;
    }
#else
    if (!LittleFS.begin())
    {
        Log.errorln("Flash FS initialisation failed!");
        while (1)
            yield(); // Stay here twiddling thumbs waiting
    }
    Log.infoln("Flash FS available!");

#endif
}

here is the code that executes in setup()

    ofr.setDrawer(tft);
    // ofr.loadFont(Roboto, sizeof(Roboto));
    if (SPIFFS.exists("/Roboto.ttf"))
    {
        Log.infoln("Loading font from file.");
        if (ofr.loadFont("/Roboto.ttf"))
        {
            Log.errorln("Failed to load font.");
            ofr.loadFont(Roboto, sizeof(Roboto));
        }
    }
    else
    {
        Log.errorln("Failed to load font.");
        ofr.loadFont(Roboto, sizeof(Roboto));
    }

I'm re-checking the validity of the data and partition just to be sure.

Thanks.

@takkaO
Copy link
Owner

takkaO commented Jan 16, 2025

@giri-v

Thank you share your code😄
Perhaps this is because the code for file access has not been constructed correctly.

Since access to the file system varies greatly from microcontroller to microcontroller, it is necessary to implement low-level functions for each microcontroller.
OpenFontRender provides several implementations as presets.

See examples/M5Stack/load_from_spiffs if you want to use SPIFFS with ESP32.

Unfortunately, there are no presets for LittleFS yet, so you have to create your own low-level functions. Please refer to examples/M5Stack/load_from_sd_without_preset for implementation.
( If you can provide me with a preset version of the LittleFS code, we will gladly merge it in. )

For a more detailed explanation, please refer to the documentation.

Also note that even if the implementation is correct, if the font file size is large, it may not be possible to load the file due to insufficient memory in the microcontroller.

@giri-v
Copy link

giri-v commented Jan 16, 2025

LittleFS code was provided just for completeness. It (LittleFS) is NOT defined in this case, I can confirm I am using SPIFFS as I can access other files normally. I'll try again.

@takkaO
Copy link
Owner

takkaO commented Jan 16, 2025

@giri-v

I understood that you are not using LittleFS.
Any further discussion would require you to share me minimal code that can reproduce the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
progress: waiting for confirmation and close Waiting for confirmation and close
Projects
None yet
Development

No branches or pull requests

4 participants