Skip to content

Commit

Permalink
Merge pull request #621 from olliwang/freetype
Browse files Browse the repository at this point in the history
Fixes FreeType errors when dealing with multiple NVGcontext objects.
  • Loading branch information
memononen authored Dec 2, 2021
2 parents e75cf72 + 0b37350 commit 645e914
Showing 1 changed file with 143 additions and 135 deletions.
278 changes: 143 additions & 135 deletions src/fontstash.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,21 +157,160 @@ struct FONSttFontImpl {
};
typedef struct FONSttFontImpl FONSttFontImpl;

static FT_Library ftLibrary;
#else

#define STB_TRUETYPE_IMPLEMENTATION

#include "stb_truetype.h"

struct FONSttFontImpl {
stbtt_fontinfo font;
};
typedef struct FONSttFontImpl FONSttFontImpl;

#endif

#ifndef FONS_SCRATCH_BUF_SIZE
# define FONS_SCRATCH_BUF_SIZE 96000
#endif
#ifndef FONS_HASH_LUT_SIZE
# define FONS_HASH_LUT_SIZE 256
#endif
#ifndef FONS_INIT_FONTS
# define FONS_INIT_FONTS 4
#endif
#ifndef FONS_INIT_GLYPHS
# define FONS_INIT_GLYPHS 256
#endif
#ifndef FONS_INIT_ATLAS_NODES
# define FONS_INIT_ATLAS_NODES 256
#endif
#ifndef FONS_VERTEX_COUNT
# define FONS_VERTEX_COUNT 1024
#endif
#ifndef FONS_MAX_STATES
# define FONS_MAX_STATES 20
#endif
#ifndef FONS_MAX_FALLBACKS
# define FONS_MAX_FALLBACKS 20
#endif

static unsigned int fons__hashint(unsigned int a)
{
a += ~(a<<15);
a ^= (a>>10);
a += (a<<3);
a ^= (a>>6);
a += ~(a<<11);
a ^= (a>>16);
return a;
}

static int fons__mini(int a, int b)
{
return a < b ? a : b;
}

static int fons__maxi(int a, int b)
{
return a > b ? a : b;
}

struct FONSglyph
{
unsigned int codepoint;
int index;
int next;
short size, blur;
short x0,y0,x1,y1;
short xadv,xoff,yoff;
};
typedef struct FONSglyph FONSglyph;

struct FONSfont
{
FONSttFontImpl font;
char name[64];
unsigned char* data;
int dataSize;
unsigned char freeData;
float ascender;
float descender;
float lineh;
FONSglyph* glyphs;
int cglyphs;
int nglyphs;
int lut[FONS_HASH_LUT_SIZE];
int fallbacks[FONS_MAX_FALLBACKS];
int nfallbacks;
};
typedef struct FONSfont FONSfont;

struct FONSstate
{
int font;
int align;
float size;
unsigned int color;
float blur;
float spacing;
};
typedef struct FONSstate FONSstate;

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

struct FONSatlas
{
int width, height;
FONSatlasNode* nodes;
int nnodes;
int cnodes;
};
typedef struct FONSatlas FONSatlas;

struct FONScontext
{
FONSparams params;
float itw,ith;
unsigned char* texData;
int dirtyRect[4];
FONSfont** fonts;
FONSatlas* atlas;
int cfonts;
int nfonts;
float verts[FONS_VERTEX_COUNT*2];
float tcoords[FONS_VERTEX_COUNT*2];
unsigned int colors[FONS_VERTEX_COUNT];
int nverts;
unsigned char* scratch;
int nscratch;
FONSstate states[FONS_MAX_STATES];
int nstates;
void (*handleError)(void* uptr, int error, int val);
void* errorUptr;
#ifdef FONS_USE_FREETYPE
FT_Library ftLibrary;
#endif
};

#ifdef FONS_USE_FREETYPE

int fons__tt_init(FONScontext *context)
{
FT_Error ftError;
FONS_NOTUSED(context);
ftError = FT_Init_FreeType(&ftLibrary);
ftError = FT_Init_FreeType(&context->ftLibrary);
return ftError == 0;
}

int fons__tt_done(FONScontext *context)
{
FT_Error ftError;
FONS_NOTUSED(context);
ftError = FT_Done_FreeType(ftLibrary);
ftError = FT_Done_FreeType(context->ftLibrary);
return ftError == 0;
}

Expand All @@ -180,8 +319,7 @@ int fons__tt_loadFont(FONScontext *context, FONSttFontImpl *font, unsigned char
FT_Error ftError;
FONS_NOTUSED(context);

//font->font.userdata = stash;
ftError = FT_New_Memory_Face(ftLibrary, (const FT_Byte*)data, dataSize, fontIndex, &font->font);
ftError = FT_New_Memory_Face(context->ftLibrary, (const FT_Byte*)data, dataSize, fontIndex, &font->font);
return ftError == 0;
}

Expand Down Expand Up @@ -254,17 +392,10 @@ int fons__tt_getGlyphKernAdvance(FONSttFontImpl *font, int glyph1, int glyph2)

#else

#define STB_TRUETYPE_IMPLEMENTATION
static void* fons__tmpalloc(size_t size, void* up);
static void fons__tmpfree(void* ptr, void* up);
#define STBTT_malloc(x,u) fons__tmpalloc(x,u)
#define STBTT_free(x,u) fons__tmpfree(x,u)
#include "stb_truetype.h"

struct FONSttFontImpl {
stbtt_fontinfo font;
};
typedef struct FONSttFontImpl FONSttFontImpl;

int fons__tt_init(FONScontext *context)
{
Expand Down Expand Up @@ -330,129 +461,6 @@ int fons__tt_getGlyphKernAdvance(FONSttFontImpl *font, int glyph1, int glyph2)

#endif

#ifndef FONS_SCRATCH_BUF_SIZE
# define FONS_SCRATCH_BUF_SIZE 96000
#endif
#ifndef FONS_HASH_LUT_SIZE
# define FONS_HASH_LUT_SIZE 256
#endif
#ifndef FONS_INIT_FONTS
# define FONS_INIT_FONTS 4
#endif
#ifndef FONS_INIT_GLYPHS
# define FONS_INIT_GLYPHS 256
#endif
#ifndef FONS_INIT_ATLAS_NODES
# define FONS_INIT_ATLAS_NODES 256
#endif
#ifndef FONS_VERTEX_COUNT
# define FONS_VERTEX_COUNT 1024
#endif
#ifndef FONS_MAX_STATES
# define FONS_MAX_STATES 20
#endif
#ifndef FONS_MAX_FALLBACKS
# define FONS_MAX_FALLBACKS 20
#endif

static unsigned int fons__hashint(unsigned int a)
{
a += ~(a<<15);
a ^= (a>>10);
a += (a<<3);
a ^= (a>>6);
a += ~(a<<11);
a ^= (a>>16);
return a;
}

static int fons__mini(int a, int b)
{
return a < b ? a : b;
}

static int fons__maxi(int a, int b)
{
return a > b ? a : b;
}

struct FONSglyph
{
unsigned int codepoint;
int index;
int next;
short size, blur;
short x0,y0,x1,y1;
short xadv,xoff,yoff;
};
typedef struct FONSglyph FONSglyph;

struct FONSfont
{
FONSttFontImpl font;
char name[64];
unsigned char* data;
int dataSize;
unsigned char freeData;
float ascender;
float descender;
float lineh;
FONSglyph* glyphs;
int cglyphs;
int nglyphs;
int lut[FONS_HASH_LUT_SIZE];
int fallbacks[FONS_MAX_FALLBACKS];
int nfallbacks;
};
typedef struct FONSfont FONSfont;

struct FONSstate
{
int font;
int align;
float size;
unsigned int color;
float blur;
float spacing;
};
typedef struct FONSstate FONSstate;

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

struct FONSatlas
{
int width, height;
FONSatlasNode* nodes;
int nnodes;
int cnodes;
};
typedef struct FONSatlas FONSatlas;

struct FONScontext
{
FONSparams params;
float itw,ith;
unsigned char* texData;
int dirtyRect[4];
FONSfont** fonts;
FONSatlas* atlas;
int cfonts;
int nfonts;
float verts[FONS_VERTEX_COUNT*2];
float tcoords[FONS_VERTEX_COUNT*2];
unsigned int colors[FONS_VERTEX_COUNT];
int nverts;
unsigned char* scratch;
int nscratch;
FONSstate states[FONS_MAX_STATES];
int nstates;
void (*handleError)(void* uptr, int error, int val);
void* errorUptr;
};

#ifdef STB_TRUETYPE_IMPLEMENTATION

static void* fons__tmpalloc(size_t size, void* up)
Expand Down

0 comments on commit 645e914

Please sign in to comment.