Skip to content

Commit

Permalink
Add option to set up a global font cache with the multi threaded conf…
Browse files Browse the repository at this point in the history
…iguration
  • Loading branch information
szekerest committed Dec 10, 2017
1 parent def90bf commit b74fd63
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions fontcache.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ struct ft_thread_cache{
ft_cache cache;
};
ft_thread_cache *ft_caches;
int use_global_ft_cache;
#else
ft_cache global_ft_cache;
#endif
Expand Down Expand Up @@ -102,9 +103,12 @@ ft_cache* msGetFontCache() {
#ifndef USE_THREAD
return &global_ft_cache;
#else
void* nThreadId = msGetThreadId();
void* nThreadId = 0;
ft_thread_cache *prev = NULL, *cur = ft_caches;

if (!use_global_ft_cache)
nThreadId = msGetThreadId();

if( cur != NULL && cur->thread_id == nThreadId )
return &cur->cache;

Expand Down Expand Up @@ -155,6 +159,12 @@ void msFontCacheSetup() {
ft_cache *c = msGetFontCache();
msInitFontCache(c);
#else
char* use_global_cache = getenv("MS_USE_GLOBAL_FT_CACHE");
if (use_global_cache)
use_global_ft_cache = atoi(use_global_cache);
else
use_global_ft_cache = 0;

ft_caches = NULL;
#endif
}
Expand Down Expand Up @@ -202,6 +212,10 @@ face_element* msGetFontFace(char *key, fontSetObj *fontset) {
if(!key) {
key = MS_DEFAULT_FONT_KEY;
}
#ifdef USE_THREAD
if (use_global_ft_cache)
msAcquireLock(TLOCK_TTF);
#endif
UT_HASH_FIND_STR(cache->face_cache,key,fc);
if(!fc) {
const char *fontfile = NULL;
Expand All @@ -211,6 +225,10 @@ face_element* msGetFontFace(char *key, fontSetObj *fontset) {
if(!fontfile) {
msSetError(MS_MISCERR, "Could not find font with key \"%s\" in fontset", "msGetFontFace()", key);
free(fc);
#ifdef USE_THREAD
if (use_global_ft_cache)
msReleaseLock(TLOCK_TTF);
#endif
return NULL;
}
error = FT_New_Face(cache->library,fontfile,0, &(fc->face));
Expand All @@ -220,6 +238,10 @@ face_element* msGetFontFace(char *key, fontSetObj *fontset) {
if(error) {
msSetError(MS_MISCERR, "Freetype was unable to load font file \"%s\" for key \"%s\"", "msGetFontFace()", fontfile, key);
free(fc);
#ifdef USE_THREAD
if (use_global_ft_cache)
msReleaseLock(TLOCK_TTF);
#endif
return NULL;
}
if(!fc->face->charmap) {
Expand All @@ -231,7 +253,10 @@ face_element* msGetFontFace(char *key, fontSetObj *fontset) {
fc->font = msStrdup(key);
UT_HASH_ADD_KEYPTR(hh,cache->face_cache,fc->font, strlen(key), fc);
}

#ifdef USE_THREAD
if (use_global_ft_cache)
msReleaseLock(TLOCK_TTF);
#endif
return fc;
}

Expand Down Expand Up @@ -271,6 +296,10 @@ outline_element* msGetGlyphOutline(face_element *face, glyph_element *glyph) {
ft_cache *cache = msGetFontCache();
memset(&key,0,sizeof(outline_element_key));
key.glyph = glyph;
#ifdef USE_THREAD
if (use_global_ft_cache)
msAcquireLock(TLOCK_TTF);
#endif
UT_HASH_FIND(hh,face->outline_cache,&key, sizeof(outline_element_key),oc);
if(!oc) {
FT_Matrix matrix;
Expand All @@ -287,6 +316,10 @@ outline_element* msGetGlyphOutline(face_element *face, glyph_element *glyph) {
error = FT_Load_Glyph(face->face,glyph->key.codepoint,FT_LOAD_DEFAULT/*|FT_LOAD_IGNORE_TRANSFORM*/|FT_LOAD_NO_HINTING|FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH);
if(error) {
msSetError(MS_MISCERR, "unable to load glyph %ud for font \"%s\"", "msGetGlyphByIndex()",glyph->key.codepoint, face->font);
#ifdef USE_THREAD
if (use_global_ft_cache)
msReleaseLock(TLOCK_TTF);
#endif
return NULL;
}
error = FT_Outline_New(cache->library, face->face->glyph->outline.n_points,
Expand All @@ -295,6 +328,10 @@ outline_element* msGetGlyphOutline(face_element *face, glyph_element *glyph) {
oc->key = key;
UT_HASH_ADD(hh,face->outline_cache,key,sizeof(outline_element_key), oc);
}
#ifdef USE_THREAD
if (use_global_ft_cache)
msReleaseLock(TLOCK_TTF);
#endif
return oc;
}

Expand Down

0 comments on commit b74fd63

Please sign in to comment.