How to use custom font for rendering epub doc in MuPDF?

230 Views Asked by At

My question seems too simple but I searched and read many codes without success.
How can I use my font (non-builtin fonts) available to MuPDF in order to render EPUB document using that font?
I tried to load my font as follow without success:

// just after creating fz_context...

// registering my custom system font installation function
fz_install_load_system_font_funcs(m_ctx, [](fz_context *ctx, const char *name, int bold, int italic, int needs_exact_metrics) -> fz_font* {
    reinterpret_cast<const unsigned char*>(ba.constData()), ba.length(), 0, 0);
    fz_font *f = nullptr;
    fz_try(ctx)
    {
        // ignore any argument just for test...
        f = fz_new_font_from_file(ctx, "Ubuntu", "/Users/seyedmahdimousavi/Library/Fonts/Ubuntu-R.ttf", 0, 0);
        //f = fz_new_font_from_file(ctx, nullptr, "/Users/seyedmahdimousavi/Library/Fonts/Ubuntu-R.ttf", 0, 1);
                
    } fz_catch(ctx) {
        return NULL;
    }
    return f;
}, nullptr, nullptr);

// call it
fz_font *ff = fz_load_system_font(m_ctx, "Ubuntu", 0, 0, 0);

and I applied following style for epub doc:

* {font-family: "default", "Ubuntu" ! important; }

by following call:

fz_try(m_ctx)
{
    fz_set_user_css(m_ctx, myCssText);
}
fz_catch(m_ctx)
{
    //...
}

Thanks a lot

1

There are 1 best solutions below

3
ccxvii On

You can use @font-face declarations in your user CSS to define and load custom fonts for the EPUB engine.