iHowell
iHowell

Reputation: 2447

How do I include a font file (.ttf) in my Android NDK project?

So I need to include a .ttf font file for Freetype font rendering in my Android NDK project. Where do I put it so that it will be put in the apk?

Upvotes: 2

Views: 1784

Answers (2)

Adrian
Adrian

Reputation: 89

I will add my solution, it's by passing the assetManager, and keep a reference to it in the c++ side. The font ttf file is stored in the assets folder inside a fonts folder.

FT_library library;
FT_Face fontFace;

AAsset* fontFile = AAssetManager_open(manager, "fonts/Roboto-Medium.ttf", AASSET_MODE_BUFFER);
off_t fontDataSize = AAsset_getLength(fontFile);

FT_Byte* fontData = new FT_Byte[fontDataSize];
AAsset_read(fontFile, fontData, (size_t) fontDataSize);
AAsset_close(fontFile);


if (FT_Init_FreeType(&library)) {
    LOGE("Could not load library");
}
if (FT_New_Memory_Face(library, (const FT_Byte*)fontData, (FT_Long)fontDataSize, 0, &fontFace)) {
    LOGE("Load memory failed");
}

To pass the assetManager just pass it as an object and convert on the C++ side.

AAssetManager *manager = AAssetManager_fromJava(javaEnv, assetObject);

Upvotes: 5

iHowell
iHowell

Reputation: 2447

In src/main/ I created another folder assets/ alongside the java/, jni/, and res/ folders where I placed the .ttf file and it was uploaded to the apk. I'm still trying to get Freetype to see it as the current command isn't getting it:

FT_New_Face(ft, "/assets/Arial.ttf", 0, &face);

But that's another question.

Upvotes: -1

Related Questions