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

Add functionality for loading specific font faces from .ttc files #540

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ NanoVG is small antialiased vector graphics rendering library for OpenGL. It has

## Screenshot

![screenshot of some text rendered witht the sample program](/example/screenshot-01.png?raw=true)
![screenshot of some text rendered with the sample program](/example/screenshot-01.png?raw=true)

Usage
=====
Expand Down
31 changes: 18 additions & 13 deletions src/fontstash.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 faceIdx);
int fonsAddFontMem(FONScontext* s, const char* name, unsigned char* data, int ndata, int faceIdx, int freeData);
int fonsGetFontByName(FONScontext* s, const char* name);

// State handling
Expand Down Expand Up @@ -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 faceIdx)
{
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, faceIdx, &font->font);
return ftError == 0;
}

Expand Down Expand Up @@ -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 faceIdx)
{
int stbError;
FONS_NOTUSED(dataSize);

int offset = stbtt_GetFontOffsetForIndex(data, faceIdx);

if (offset < 0)
return 0;

font->font.userdata = context;
stbError = stbtt_InitFont(&font->font, data, 0);
stbError = stbtt_InitFont(&font->font, data, offset);
return stbError;
}

Expand Down Expand Up @@ -413,7 +418,7 @@ struct FONSstate
typedef struct FONSstate FONSstate;

struct FONSatlasNode {
short x, y, width;
short x, y, width;
};
typedef struct FONSatlasNode FONSatlasNode;

Expand Down Expand Up @@ -504,11 +509,11 @@ static unsigned int fons__decutf8(unsigned int* state, unsigned int* codep, unsi
12,12,12,12,12,12,12,24,12,12,12,12, 12,24,12,12,12,12,12,12,12,24,12,12,
12,12,12,12,12,12,12,36,12,36,12,12, 12,36,12,12,12,12,12,36,12,36,12,12,
12,36,12,12,12,12,12,12,12,12,12,12,
};
};

unsigned int type = utf8d[byte];

*codep = (*state != FONS_UTF8_ACCEPT) ?
*codep = (*state != FONS_UTF8_ACCEPT) ?
(byte & 0x3fu) | (*codep << 6) :
(0xff >> type) & (byte);

Expand Down Expand Up @@ -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 faceIdx)
{
FILE* fp = 0;
int dataSize = 0;
Expand All @@ -910,15 +915,15 @@ 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, faceIdx, 1);

error:
if (data) free(data);
if (fp) fclose(fp);
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 faceIdx, int freeData)
{
int i, ascent, descent, fh, lineGap;
FONSfont* font;
Expand All @@ -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, faceIdx)) goto error;

// Store normalized line height. The real line height is got
// by multiplying the lineh by font size.
Expand Down
30 changes: 20 additions & 10 deletions src/nanovg.c
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ NVGcontext* nvgCreateInternal(NVGparams* params)

NVGparams* nvgInternalParams(NVGcontext* ctx)
{
return &ctx->params;
return &ctx->params;
}

void nvgDeleteInternal(NVGcontext* ctx)
Expand Down Expand Up @@ -1989,13 +1989,13 @@ void nvgBezierTo(NVGcontext* ctx, float c1x, float c1y, float c2x, float c2y, fl

void nvgQuadTo(NVGcontext* ctx, float cx, float cy, float x, float y)
{
float x0 = ctx->commandx;
float y0 = ctx->commandy;
float vals[] = { NVG_BEZIERTO,
x0 + 2.0f/3.0f*(cx - x0), y0 + 2.0f/3.0f*(cy - y0),
x + 2.0f/3.0f*(cx - x), y + 2.0f/3.0f*(cy - y),
x, y };
nvg__appendCommands(ctx, vals, NVG_COUNTOF(vals));
float x0 = ctx->commandx;
float y0 = ctx->commandy;
float vals[] = { NVG_BEZIERTO,
x0 + 2.0f/3.0f*(cx - x0), y0 + 2.0f/3.0f*(cy - y0),
x + 2.0f/3.0f*(cx - x), y + 2.0f/3.0f*(cy - y),
x, y };
nvg__appendCommands(ctx, vals, NVG_COUNTOF(vals));
}

void nvgArcTo(NVGcontext* ctx, float x1, float y1, float x2, float y2, float radius)
Expand Down Expand Up @@ -2289,12 +2289,22 @@ void nvgStroke(NVGcontext* ctx)
// Add fonts
int nvgCreateFont(NVGcontext* ctx, const char* name, const char* path)
{
return fonsAddFont(ctx->fs, name, path);
return nvgCreateFontFace(ctx, name, path, 0);
}

int nvgCreateFontFace(NVGcontext* ctx, const char* name, const char* path, int faceIdx)
{
return fonsAddFont(ctx->fs, name, path, faceIdx);
}

int nvgCreateFontMem(NVGcontext* ctx, const char* name, unsigned char* data, int ndata, int freeData)
{
return fonsAddFontMem(ctx->fs, name, data, ndata, freeData);
return nvgCreateFontFaceMem(ctx, name, data, ndata, 0, freeData);
}

int nvgCreateFontFaceMem(NVGcontext* ctx, const char* name, unsigned char* data, int ndata, int faceIdx, int freeData)
{
return fonsAddFontMem(ctx->fs, name, data, ndata, faceIdx, freeData);
}

int nvgFindFont(NVGcontext* ctx, const char* name)
Expand Down
10 changes: 9 additions & 1 deletion src/nanovg.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ struct NVGtextRow {
typedef struct NVGtextRow NVGtextRow;

enum NVGimageFlags {
NVG_IMAGE_GENERATE_MIPMAPS = 1<<0, // Generate mipmaps during creation of the image.
NVG_IMAGE_GENERATE_MIPMAPS = 1<<0, // Generate mipmaps during creation of the image.
NVG_IMAGE_REPEATX = 1<<1, // Repeat image in X direction.
NVG_IMAGE_REPEATY = 1<<2, // Repeat image in Y direction.
NVG_IMAGE_FLIPY = 1<<3, // Flips (inverses) image in Y direction when rendered.
Expand Down Expand Up @@ -546,10 +546,18 @@ void nvgStroke(NVGcontext* ctx);
// Returns handle to the font.
int nvgCreateFont(NVGcontext* ctx, const char* name, const char* filename);

// Creates font by loading it from the disk from specified file name, loading a specific face by index.
// Returns handle to the font.
int nvgCreateFontFace(NVGcontext* ctx, const char* name, const char* filename, int faceIdx);

// 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);

// Creates font by loading it from the specified memory chunk, loading a specific face by index.
// Returns handle to the font.
int nvgCreateFontFaceMem(NVGcontext* ctx, const char* name, unsigned char* data, int ndata, int faceIdx, int freeData);

// 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);

Expand Down