From 8851f025e4b7ad05246a2a9c0bf453a0fa2b2ca0 Mon Sep 17 00:00:00 2001 From: Olli Wang Date: Mon, 17 Feb 2020 04:58:22 +0800 Subject: [PATCH] Support for loading a certain font face from a font file. This commit allows loading a certain font face from a font collection file such as TTC. --- src/fontstash.h | 27 ++++++++++++++++----------- src/nanovg.c | 16 +++++++++++++--- src/nanovg.h | 6 ++++++ 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/src/fontstash.h b/src/fontstash.h index 74cb175..0b07889 100644 --- a/src/fontstash.h +++ b/src/fontstash.h @@ -102,8 +102,8 @@ int fonsExpandAtlas(FONScontext* s, int width, int height); int fonsResetAtlas(FONScontext* stash, int width, int height); // Add fonts -int fonsAddFont(FONScontext* s, const char* name, const char* path); -int fonsAddFontMem(FONScontext* s, const char* name, unsigned char* data, int ndata, int freeData); +int fonsAddFont(FONScontext* s, const char* name, const char* path, int fontIndex); +int fonsAddFontMem(FONScontext* s, const char* name, unsigned char* data, int ndata, int freeData, int fontIndex); int fonsGetFontByName(FONScontext* s, const char* name); // State handling @@ -175,13 +175,13 @@ int fons__tt_done(FONScontext *context) return ftError == 0; } -int fons__tt_loadFont(FONScontext *context, FONSttFontImpl *font, unsigned char *data, int dataSize) +int fons__tt_loadFont(FONScontext *context, FONSttFontImpl *font, unsigned char *data, int dataSize, int fontIndex) { FT_Error ftError; FONS_NOTUSED(context); //font->font.userdata = stash; - ftError = FT_New_Memory_Face(ftLibrary, (const FT_Byte*)data, dataSize, 0, &font->font); + ftError = FT_New_Memory_Face(ftLibrary, (const FT_Byte*)data, dataSize, fontIndex, &font->font); return ftError == 0; } @@ -278,13 +278,18 @@ int fons__tt_done(FONScontext *context) return 1; } -int fons__tt_loadFont(FONScontext *context, FONSttFontImpl *font, unsigned char *data, int dataSize) +int fons__tt_loadFont(FONScontext *context, FONSttFontImpl *font, unsigned char *data, int dataSize, int fontIndex) { - int stbError; + int offset, stbError; FONS_NOTUSED(dataSize); font->font.userdata = context; - stbError = stbtt_InitFont(&font->font, data, 0); + offset = stbtt_GetFontOffsetForIndex(data, fontIndex); + if (offset == -1) { + stbError = 0; + } else { + stbError = stbtt_InitFont(&font->font, data, offset); + } return stbError; } @@ -890,7 +895,7 @@ static int fons__allocFont(FONScontext* stash) return FONS_INVALID; } -int fonsAddFont(FONScontext* stash, const char* name, const char* path) +int fonsAddFont(FONScontext* stash, const char* name, const char* path, int fontIndex) { FILE* fp = 0; int dataSize = 0; @@ -910,7 +915,7 @@ int fonsAddFont(FONScontext* stash, const char* name, const char* path) fp = 0; if (readed != dataSize) goto error; - return fonsAddFontMem(stash, name, data, dataSize, 1); + return fonsAddFontMem(stash, name, data, dataSize, 1, fontIndex); error: if (data) free(data); @@ -918,7 +923,7 @@ int fonsAddFont(FONScontext* stash, const char* name, const char* path) return FONS_INVALID; } -int fonsAddFontMem(FONScontext* stash, const char* name, unsigned char* data, int dataSize, int freeData) +int fonsAddFontMem(FONScontext* stash, const char* name, unsigned char* data, int dataSize, int freeData, int fontIndex) { int i, ascent, descent, fh, lineGap; FONSfont* font; @@ -943,7 +948,7 @@ int fonsAddFontMem(FONScontext* stash, const char* name, unsigned char* data, in // Init font stash->nscratch = 0; - if (!fons__tt_loadFont(stash, &font->font, data, dataSize)) goto error; + if (!fons__tt_loadFont(stash, &font->font, data, dataSize, fontIndex)) goto error; // Store normalized line height. The real line height is got // by multiplying the lineh by font size. diff --git a/src/nanovg.c b/src/nanovg.c index a28804f..cfdb8d2 100644 --- a/src/nanovg.c +++ b/src/nanovg.c @@ -2287,14 +2287,24 @@ void nvgStroke(NVGcontext* ctx) } // Add fonts -int nvgCreateFont(NVGcontext* ctx, const char* name, const char* path) +int nvgCreateFont(NVGcontext* ctx, const char* name, const char* filename) { - return fonsAddFont(ctx->fs, name, path); + return fonsAddFont(ctx->fs, name, filename, 0); +} + +int nvgCreateFontAtIndex(NVGcontext* ctx, const char* name, const char* filename, const int fontIndex) +{ + return fonsAddFont(ctx->fs, name, filename, fontIndex); } int nvgCreateFontMem(NVGcontext* ctx, const char* name, unsigned char* data, int ndata, int freeData) { - return fonsAddFontMem(ctx->fs, name, data, ndata, freeData); + return fonsAddFontMem(ctx->fs, name, data, ndata, freeData, 0); +} + +int nvgCreateFontMemAtIndex(NVGcontext* ctx, const char* name, unsigned char* data, int ndata, int freeData, const int fontIndex) +{ + return fonsAddFontMem(ctx->fs, name, data, ndata, freeData, fontIndex); } int nvgFindFont(NVGcontext* ctx, const char* name) diff --git a/src/nanovg.h b/src/nanovg.h index 2255d6a..2c01ff9 100644 --- a/src/nanovg.h +++ b/src/nanovg.h @@ -546,10 +546,16 @@ void nvgStroke(NVGcontext* ctx); // Returns handle to the font. int nvgCreateFont(NVGcontext* ctx, const char* name, const char* filename); +// fontIndex specifies which font face to load from a .ttf/.ttc file. +int nvgCreateFontAtIndex(NVGcontext* ctx, const char* name, const char* filename, const int fontIndex); + // Creates font by loading it from the specified memory chunk. // Returns handle to the font. int nvgCreateFontMem(NVGcontext* ctx, const char* name, unsigned char* data, int ndata, int freeData); +// fontIndex specifies which font face to load from a .ttf/.ttc file. +int nvgCreateFontMemAtIndex(NVGcontext* ctx, const char* name, unsigned char* data, int ndata, int freeData, const int fontIndex); + // Finds a loaded font of specified name, and returns handle to it, or -1 if the font is not found. int nvgFindFont(NVGcontext* ctx, const char* name);