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

Some preformance fixes #45

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 2 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
117 changes: 58 additions & 59 deletions src/fontstash.h
Original file line number Diff line number Diff line change
Expand Up @@ -358,15 +358,8 @@ static unsigned int fons__hashint(unsigned int a)
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;
}
#define fons__mini(a, b) ((a < b) ? (a) : (b));
#define fons__maxi(a, b) ((a > b) ? (a) : (b));

struct FONSglyph
{
Expand Down Expand Up @@ -437,7 +430,7 @@ struct FONScontext
float tcoords[FONS_VERTEX_COUNT*2];
unsigned int colors[FONS_VERTEX_COUNT];
int nverts;
unsigned char* scratch;
unsigned char scratch[FONS_SCRATCH_BUF_SIZE];
int nscratch;
FONSstate states[FONS_MAX_STATES];
int nstates;
Expand All @@ -455,9 +448,9 @@ static void* fons__tmpalloc(size_t size, void* up)
// 16-byte align the returned pointer
size = (size + 0xf) & ~0xf;

if (stash->nscratch+(int)size > FONS_SCRATCH_BUF_SIZE) {
if (stash->nscratch + (int)size > FONS_SCRATCH_BUF_SIZE) {
if (stash->handleError)
stash->handleError(stash->errorUptr, FONS_SCRATCH_FULL, stash->nscratch+(int)size);
stash->handleError(stash->errorUptr, FONS_SCRATCH_FULL, stash->nscratch + (int)size);
return NULL;
}
ptr = stash->scratch + stash->nscratch;
Expand Down Expand Up @@ -529,15 +522,15 @@ static FONSatlas* fons__allocAtlas(int w, int h, int nnodes)
// Allocate memory for the font stash.
atlas = (FONSatlas*)malloc(sizeof(FONSatlas));
if (atlas == NULL) goto error;
memset(atlas, 0, sizeof(FONSatlas));
//memset(atlas, 0, sizeof(FONSatlas));

atlas->width = w;
atlas->height = h;

// Allocate space for skyline nodes
atlas->nodes = (FONSatlasNode*)malloc(sizeof(FONSatlasNode) * nnodes);
if (atlas->nodes == NULL) goto error;
memset(atlas->nodes, 0, sizeof(FONSatlasNode) * nnodes);
//memset(atlas->nodes, 0, sizeof(FONSatlasNode) * nnodes);
atlas->nnodes = 0;
atlas->cnodes = nnodes;

Expand Down Expand Up @@ -610,7 +603,7 @@ static int fons__atlasAddSkylineLevel(FONSatlas* atlas, int idx, int x, int y, i
int i;

// Insert new node
if (fons__atlasInsertNode(atlas, idx, x, y+h, w) == 0)
if (fons__atlasInsertNode(atlas, idx, x, y + h, w) == 0)
return 0;

// Delete skyline segments that fall under the shadow of the new segment.
Expand Down Expand Up @@ -718,18 +711,25 @@ static void fons__addWhiteRect(FONScontext* stash, int w, int h)

FONScontext* fonsCreateInternal(FONSparams* params)
{
FONScontext* stash = NULL;

FONScontext* stash;
// Allocate memory for the font stash.
stash = (FONScontext*)malloc(sizeof(FONScontext));
if (stash == NULL) goto error;
memset(stash, 0, sizeof(FONScontext));
if (stash == NULL) return NULL;

/*
I don't think this is required
so it'd probably be better not to have it

memset(stash, 0, sizeof(FONScontext));
*/

stash->params = *params;

// Allocate scratch buffer.
stash->scratch = (unsigned char*)malloc(FONS_SCRATCH_BUF_SIZE);
if (stash->scratch == NULL) goto error;
/*
removed allocation of scratch buffer
since it's just a static size anyway
*/

// Initialize implementation library
if (!fons__tt_init(stash)) goto error;
Expand All @@ -745,7 +745,7 @@ FONScontext* fonsCreateInternal(FONSparams* params)
// Allocate space for fonts.
stash->fonts = (FONSfont**)malloc(sizeof(FONSfont*) * FONS_INIT_FONTS);
if (stash->fonts == NULL) goto error;
memset(stash->fonts, 0, sizeof(FONSfont*) * FONS_INIT_FONTS);
//memset(stash->fonts, 0, sizeof(FONSfont*) * FONS_INIT_FONTS);
stash->cfonts = FONS_INIT_FONTS;
stash->nfonts = 0;

Expand All @@ -754,7 +754,7 @@ FONScontext* fonsCreateInternal(FONSparams* params)
stash->ith = 1.0f/stash->params.height;
stash->texData = (unsigned char*)malloc(stash->params.width * stash->params.height);
if (stash->texData == NULL) goto error;
memset(stash->texData, 0, stash->params.width * stash->params.height);
//memset(stash->texData, 0, stash->params.width * stash->params.height);

stash->dirtyRect[0] = stash->params.width;
stash->dirtyRect[1] = stash->params.height;
Expand Down Expand Up @@ -856,7 +856,6 @@ static void fons__freeFont(FONSfont* font)
{
if (font == NULL) return;
if (font->glyphs) free(font->glyphs);
if (font->freeData && font->data) free(font->data);
free(font);
}

Expand Down Expand Up @@ -891,24 +890,25 @@ static FILE* fons__fopen(const char* filename, const char* mode)
{
#ifdef _WIN32
int len = 0;
int fileLen = strlen(filename);
int modeLen = strlen(mode);

wchar_t wpath[MAX_PATH];
wchar_t wmode[MAX_PATH];
FILE* f;

if (fileLen == 0)
return NULL;
if (modeLen == 0)
return NULL;
len = MultiByteToWideChar(CP_UTF8, 0, filename, fileLen, wpath, fileLen);
if (len >= MAX_PATH)
/*
based on `stb_image.h`
the strlen isn't required
*/

len = MultiByteToWideChar(CP_UTF8, 0, filename, fileLen, wpath, sizeof(wFilename)/sizeof(*wFilename));
if (len >= MAX_PATH || len == 0)
return NULL;
wpath[len] = L'\0';
len = MultiByteToWideChar(CP_UTF8, 0, mode, modeLen, wmode, modeLen);
if (len >= MAX_PATH)
len = MultiByteToWideChar(CP_UTF8, 0, mode, modeLen, wmode, sizeof(wMode)/sizeof(*wMode)));
if (len >= MAX_PATH || len == 0)
return NULL;
wmode[len] = L'\0';

f = _wfopen(wpath, wmode);
return f;
#else
Expand All @@ -920,15 +920,14 @@ int fonsAddFont(FONScontext* stash, const char* name, const char* path)
{
FILE* fp = 0;
int dataSize = 0, readed;
unsigned char* data = NULL;

// Read in the font data.
fp = fons__fopen(path, "rb");
if (fp == NULL) goto error;
if (fp == NULL) return FONS_INVALID;
fseek(fp,0,SEEK_END);
dataSize = (int)ftell(fp);
fseek(fp,0,SEEK_SET);
data = (unsigned char*)malloc(dataSize);

unsigned char data[dataSize];
ColleagueRiley marked this conversation as resolved.
Show resolved Hide resolved
if (data == NULL) goto error;
readed = fread(data, 1, dataSize, fp);
fclose(fp);
Expand All @@ -938,7 +937,6 @@ int fonsAddFont(FONScontext* stash, const char* name, const char* path)
return fonsAddFontMem(stash, name, data, dataSize, 1);

error:
if (data) free(data);
if (fp) fclose(fp);
return FONS_INVALID;
}
Expand Down Expand Up @@ -988,10 +986,17 @@ int fonsAddFontMem(FONScontext* stash, const char* name, unsigned char* data, in

int fonsGetFontByName(FONScontext* s, const char* name)
{
int i;
int i, j;
for (i = 0; i < s->nfonts; i++) {
if (strcmp(s->fonts[i]->name, name) == 0)
return i;
/*
this way is slightly faster than strcmp
because it'd end sooner if the strings are not equal
*/

for (j = 0; name[j] != s->fonts[i]->name[j]; j++)
if (name[j] == '\0')
return i;

}
return FONS_INVALID;
}
Expand Down Expand Up @@ -1324,9 +1329,6 @@ FONS_DEF float fonsDrawText(FONScontext* stash,

scale = fons__tt_getPixelHeightScale(&font->font, (float)isize/10.0f);

if (end == NULL)
end = str + strlen(str);

// Align horizontally
if (state->align & FONS_ALIGN_LEFT) {
// empty
Expand All @@ -1340,7 +1342,12 @@ FONS_DEF float fonsDrawText(FONScontext* stash,
// Align vertically.
y += fons__getVertAlign(stash, font, state->align, isize);

for (; str != end; ++str) {
/*
strlen is already doing a loop like this
so there's no point in doing it twice if end == NULL
*/

for (; (end != NULL && str != end) || (end == NULL && *str != '\0'); ++str) {
if (fons__decutf8(&utf8state, &codepoint, *(const unsigned char*)str))
continue;
glyph = fons__getGlyph(stash, font, codepoint, isize, iblur);
Expand Down Expand Up @@ -1371,7 +1378,7 @@ FONS_DEF int fonsTextIterInit(FONScontext* stash, FONStextIter* iter,
FONSstate* state = fons__getState(stash);
float width;

memset(iter, 0, sizeof(*iter));
//memset(iter, 0, sizeof(*iter));

if (stash == NULL) return 0;
if (state->font < 0 || state->font >= stash->nfonts) return 0;
Expand All @@ -1395,9 +1402,6 @@ FONS_DEF int fonsTextIterInit(FONScontext* stash, FONStextIter* iter,
// Align vertically.
y += fons__getVertAlign(stash, iter->font, state->align, iter->isize);

if (end == NULL)
end = str + strlen(str);

iter->x = iter->nextx = x;
iter->y = iter->nexty = y;
iter->spacing = state->spacing;
Expand Down Expand Up @@ -1517,10 +1521,7 @@ FONS_DEF float fonsTextBounds(FONScontext* stash,
miny = maxy = y;
startx = x;

if (end == NULL)
end = str + strlen(str);

for (; str != end; ++str) {
for (; (end && str != end) || (!end && *str != '\0'); ++str) {
if (fons__decutf8(&utf8state, &codepoint, *(const unsigned char*)str))
continue;
glyph = fons__getGlyph(stash, font, codepoint, isize, iblur);
Expand Down Expand Up @@ -1646,7 +1647,6 @@ FONS_DEF void fonsDeleteInternal(FONScontext* stash)
if (stash->atlas) fons__deleteAtlas(stash->atlas);
if (stash->fonts) free(stash->fonts);
if (stash->texData) free(stash->texData);
if (stash->scratch) free(stash->scratch);
free(stash);
}

Expand All @@ -1667,7 +1667,6 @@ FONS_DEF void fonsGetAtlasSize(FONScontext* stash, int* width, int* height)
FONS_DEF int fonsExpandAtlas(FONScontext* stash, int width, int height)
{
int i, maxy = 0;
unsigned char* data = NULL;
if (stash == NULL) return 0;

width = fons__maxi(width, stash->params.width);
Expand All @@ -1685,7 +1684,7 @@ FONS_DEF int fonsExpandAtlas(FONScontext* stash, int width, int height)
return 0;
}
// Copy old texture data over.
data = (unsigned char*)malloc(width * height);
unsigned char data[width * height];
if (data == NULL)
return 0;
for (i = 0; i < stash->params.height; i++) {
Expand Down Expand Up @@ -1740,7 +1739,7 @@ FONS_DEF int fonsResetAtlas(FONScontext* stash, int width, int height)
// Clear texture data.
stash->texData = (unsigned char*)realloc(stash->texData, width * height);
if (stash->texData == NULL) return 0;
memset(stash->texData, 0, width * height);
//memset(stash->texData, 0, width * height);

// Reset dirty rect
stash->dirtyRect[0] = width;
Expand Down