tune fontcache functions: use devfont name instead of family and style

This commit is contained in:
Vincent Wei
2019-03-04 11:37:50 +08:00
parent 508130afdc
commit e43bc07a63
3 changed files with 130 additions and 124 deletions
+26 -61
View File
@@ -85,15 +85,11 @@ typedef struct _HashDirectory {
*hashDir : the hash directory pointer
lruQueueDummyHead : head node of lru queue
makeHashKeyFunc : the function which make hash key */
typedef struct cache {
typedef struct _FontCache {
/* Information about font */
char family[LEN_LOGFONT_NAME_FIELD + 1];
// VincentWei: no need to check charset for TrueType
//char charset[LEN_LOGFONT_NAME_FIELD + 1];
int render_style; // render style
char df_name[LEN_UNIDEVFONT_NAME + 1];
int fontsize;
int rotation;
int refers;
int blkSize;
int nBlk;
@@ -103,7 +99,7 @@ typedef struct cache {
HashDirectory *hashDir;
LruQueueDummyHead lruQueueDummyHead;
MakeHashKeyFunc makeHashKeyFunc;
} cache_t;
} FontCache;
/* Least-Recently-Used-Order
global cache LRU queue */
@@ -111,7 +107,7 @@ typedef struct _CacheQueueNode
{
/* hold global cache queue
max cache node maybe 10 */
cache_t cache;
FontCache cache;
struct _CacheQueueNode *prevCache;
struct _CacheQueueNode *nextCache;
}CacheQueueNode;
@@ -149,7 +145,7 @@ AppendCache(CacheQueueNode *new)
/* create a new LRU-Queue. */
static int
CreateCacheLruQueue(cache_t *cache, int nblk)
CreateCacheLruQueue(FontCache *cache, int nblk)
{
int i;
MemBlk *blk;
@@ -221,7 +217,7 @@ InitCache(CacheQueueNode *new, int ndir, int nblock)
static void
LruQueueBufferSplit(cache_t *cache, int bufsize)
LruQueueBufferSplit(FontCache *cache, int bufsize)
{
MemBlk *blk;
unsigned char *buff;
@@ -236,7 +232,7 @@ LruQueueBufferSplit(cache_t *cache, int bufsize)
}
static void
DestroyCache(cache_t *cache)
DestroyCache(FontCache *cache)
{
if (cache->hashDir != NULL) {
free(cache->hashDir);
@@ -325,7 +321,7 @@ GetCache(int nblock, int ndir)
lru-queue and also Hash queue(anywhere in hash q).
Relink the lru queue and hash queue */
static MemBlk *
PickOffBlk(cache_t *cache)
PickOffBlk(FontCache *cache)
{
MemBlk *blk;
@@ -352,7 +348,7 @@ PickOffBlk(cache_t *cache)
}
static void
AppendBlk(cache_t *cache, MemBlk *blk, int key)
AppendBlk(FontCache *cache, MemBlk *blk, int key)
{
/* first, append the blcok at the end of the
lru-queue. */
@@ -381,7 +377,7 @@ AppendBlk(cache_t *cache, MemBlk *blk, int key)
*size : if found the data, how long is the data(return val)
reutrn = pointer to the data */
TTFCACHEINFO *
__mg_ttc_search(HCACHE hCache, unsigned short unicode, int *size)
__mg_ttc_search(HCACHE hCache, Uchar32 unicode, int *size)
{
int key;
CacheQueueNode *ca;
@@ -399,7 +395,8 @@ __mg_ttc_search(HCACHE hCache, unsigned short unicode, int *size)
for ( ; blk != &(ca->cache.hashDir[key].hashHead);
blk = blk->hashNext) {
if (*(unsigned short *)(blk->data) == unicode) {
TTFCACHEINFO* tci = (TTFCACHEINFO*)blk->data;
if (tci->unicode == unicode) {
/* If found the blk, we should pick off it
and link it at the end of LRU-q */
*size = blk->len;
@@ -409,17 +406,18 @@ __mg_ttc_search(HCACHE hCache, unsigned short unicode, int *size)
blk->lruPrev = ca->cache.lruQueueDummyHead.lruPrev;
(ca->cache.lruQueueDummyHead.lruPrev)->lruNext = blk;
ca->cache.lruQueueDummyHead.lruPrev = blk;
return blk->data;
return tci;
}
}
return NULL;
}
/* add data into a speicial cache
/* add data into the specific cache
hCache : which cache ,
*data : your data pointer
size : your data's bytes
key : hash key of your data */
key : hash key of your data
*/
int
__mg_ttc_write(HCACHE hCache, TTFCACHEINFO *data, int size)
{
@@ -434,7 +432,7 @@ __mg_ttc_write(HCACHE hCache, TTFCACHEINFO *data, int size)
if (ca->cache.blkSize < size) {
return -1;
}
key = ca->cache.makeHashKeyFunc(*(unsigned short *)data);
key = ca->cache.makeHashKeyFunc(data->unicode);
/* pick off a block from lru queue */
if ((blk = PickOffBlk(&ca->cache)) == NULL) {
@@ -461,12 +459,11 @@ __mg_ttc_write(HCACHE hCache, TTFCACHEINFO *data, int size)
ndir : how many hash entries in the cache
makeHashKey : the function which make the hash key */
HCACHE
__mg_ttc_create(char *family, char *charset,
DWORD style, int size, int rotation,
__mg_ttc_create(const char *df_name, int size, int rotation,
int nblk, int blksize, int ndir, MakeHashKeyFunc makeHashKey)
{
CacheQueueNode *temp;
if (family == NULL || /* charset == NULL || */makeHashKey == NULL) {
if (df_name == NULL || makeHashKey == NULL) {
return (HCACHE)(0);
}
if (nblk <= 0 || blksize <= 0 || size <= 0 || ndir <= 0) {
@@ -480,9 +477,8 @@ __mg_ttc_create(char *family, char *charset,
return (HCACHE)(0);
}
strncpy(temp->cache.family, family, LEN_LOGFONT_NAME_FIELD);
//strncpy(temp->cache.charset, charset, LEN_LOGFONT_NAME_FIELD);
temp->cache.render_style = (style & FS_RENDER_MASK);
memset(temp->cache.df_name, 0, LEN_UNIDEVFONT_NAME + 1);
strncpy(temp->cache.df_name, df_name, LEN_UNIDEVFONT_NAME);
temp->cache.fontsize = size;
temp->cache.rotation = rotation;
temp->cache.blkSize = blksize;
@@ -575,8 +571,7 @@ __mg_ttc_sys_deinit(void)
HCACHE
__mg_ttc_is_exist(char *family, char *charset,
DWORD style, int size, int rotation)
__mg_ttc_is_exist(const char *df_name, int size, int rotation)
{
CacheQueueNode *p;
@@ -584,44 +579,16 @@ __mg_ttc_is_exist(char *family, char *charset,
return (HCACHE)0;
}
#if 0 // VincentWei: Use FS_RENDER_MASK insead (3.4.0)
/* If can not match font name*/
p = __mg_globalCache.queueDummyHead.nextCache;
while (p != &__mg_globalCache.queueDummyHead) {
if (strncmp(p->cache.family, family, LEN_LOGFONT_NAME_FIELD) == 0) {
if (strncmp(p->cache.charset, charset, LEN_LOGFONT_NAME_FIELD) == 0) {
if (p->cache.fontsize == size) {
int s = style & 0x000000FF;
if ((s == FS_WEIGHT_BOOK || s == FS_WEIGHT_DEMIBOLD || s == FS_WEIGHT_SUBPIXEL) &&
(p->cache.style == FS_WEIGHT_BOOK || p->cache.style == FS_WEIGHT_DEMIBOLD ||
p->cache.style == FS_WEIGHT_SUBPIXEL)) {
return (HCACHE) p;
}
if ((s != FS_WEIGHT_BOOK && s != FS_WEIGHT_DEMIBOLD && s != FS_WEIGHT_SUBPIXEL) &&
(p->cache.style != FS_WEIGHT_BOOK && p->cache.style != FS_WEIGHT_DEMIBOLD &&
p->cache.style != FS_WEIGHT_SUBPIXEL)) {
return (HCACHE) p;
}
}
}
p = p->nextCache;
}
#else
p = __mg_globalCache.queueDummyHead.nextCache;
while (p != &__mg_globalCache.queueDummyHead) {
if (strncmp(p->cache.family, family, LEN_LOGFONT_NAME_FIELD) == 0
#if 0 // VincentWei: do not need to check charset.
&& strncmp(p->cache.charset, charset, LEN_LOGFONT_NAME_FIELD) == 0
#endif
if (strncmp(p->cache.df_name, df_name, LEN_UNIDEVFONT_NAME) == 0
&& p->cache.fontsize == size
&& p->cache.rotation == rotation
&& (style & FS_RENDER_MASK) == p->cache.render_style) {
&& p->cache.rotation == rotation) {
return (HCACHE) p;
}
p = p->nextCache;
}
#endif
return (HCACHE)0;
}
@@ -675,15 +642,13 @@ void __mg_ttc_dump_all(void)
}
}
void __mg_ttc_dump(cache_t *cache)
void __mg_ttc_dump(FontCache *cache)
{
int i, j, k;
MemBlk *ptr;
#if 0
DP(("&cache = %p\n", cache));
DP(("cache.family = %s\n", cache->family));
DP(("cache.charset = %s\n", cache->charset));
DP(("cache.style = %d\n", cache->style));
DP(("cache.df_name = %s\n", cache->df_name));
DP(("cache.fontsize = %d\n", cache->fontsize));
DP(("cache.blkSize = %d\n", cache->blkSize));
DP(("cache.refers = %d\n", cache->refers));
+92 -51
View File
@@ -170,14 +170,13 @@ static void free_raster_bitmap_buffer (void)
/*************** TrueType on FreeType font operations ************************/
#ifdef DEBUG_GLYPH_DATA
static void
print_bitmap(char* bits, int width, int height, int pitch)
static inline void
print_bitmap_mono(const BYTE* bits, int width, int height, int pitch)
{
int y = 0;
int x = 0;
char* p_line_head;
char* p_cur_char;
const BYTE* p_line_head;
const BYTE* p_cur_char;
for (y = 0, p_line_head = bits; y < height; y++)
{
@@ -195,12 +194,12 @@ print_bitmap(char* bits, int width, int height, int pitch)
}
}
static void
print_bitmap_grey (void* buffer, int width, int rows, int pitch)
static inline void
print_bitmap_grey (const void* buffer, int width, int rows, int pitch)
{
int i;
int j;
BYTE* p = (BYTE*)buffer;
const BYTE* p = (BYTE*)buffer;
printf("*******************************************\n");
for (i = 0; i < rows; i++) {
@@ -211,7 +210,6 @@ print_bitmap_grey (void* buffer, int width, int rows, int pitch)
}
printf("*******************************************\n");
}
#endif
static DWORD get_glyph_bmptype (LOGFONT* logfont, DEVFONT* devfont)
{
@@ -375,7 +373,7 @@ get_glyph_bbox (LOGFONT* logfont, DEVFONT* devfont,
uni_char, &datasize);
if (cache_info != NULL) {
DP(("\nBBOX Hit!! %d\n", bbox_hit++));
DP(("%s: BBOX Hit!! %d\n", __FUNCTION__, bbox_hit++));
ft_inst_info->bbox = cache_info->bbox;
ft_inst_info->advance = cache_info->advance;
@@ -390,7 +388,8 @@ get_glyph_bbox (LOGFONT* logfont, DEVFONT* devfont,
FT_UNLOCK(&ft_lock);
return (int)(cache_info->bbox.xMax - cache_info->bbox.xMin);
}
DP(("\nBBOX Non - Hit! %d, %d\n", bbox_nohit++, uni_char));
DP(("%s: BBOX Non - Hit! %d, %d\n",
__FUNCTION__, bbox_nohit++, uni_char));
}
#endif /* _MGFONT_TTF_CACHE */
@@ -462,9 +461,10 @@ get_glyph_bbox (LOGFONT* logfont, DEVFONT* devfont,
cache_info.bitmap = NULL;
cache_info.pitch = 0;
cache_info.flag = FALSE;
DP(("Should Write %d to cache length = %d, bitmap size = %d, cache = %p\n",
cache_info.unicode, sizeof(TTFCACHEINFO) + size, size,
ft_inst_info->cache));
DP(("%s: Should Write %d to cache length = %d, bitmap size = %d, cache = %p\n",
__FUNCTION__,
cache_info.unicode, (int)(size + sizeof(TTFCACHEINFO)), size,
ft_inst_info->cache));
__mg_ttc_write(ft_inst_info->cache,
&cache_info, sizeof(TTFCACHEINFO) + size);
@@ -518,15 +518,20 @@ char_bitmap_pixmap (LOGFONT* logfont, DEVFONT* devfont,
uni_char, &datasize);
if (cacheinfo != NULL && cacheinfo->flag == TRUE) {
DP(("Bitmap Hit!! %d\n", bitmap_hit++));
DP(("%s: Bitmap Hit!! %d\n", __FUNCTION__, bitmap_hit++));
if (pitch)
*pitch = cacheinfo->pitch;
if (sz && (cacheinfo->height != sz->cy || cacheinfo->width != sz->cx)) {
_ERR_PRINTF("FreeType2: cached BITMAP size does not match BBOX\n");
sz->cx = cacheinfo->width;
sz->cy = cacheinfo->height;
}
FT_UNLOCK(&ft_lock);
return cacheinfo->bitmap;
}
DP(("Bitmap Non hit %d, %d\n", bitmap_nohit++, uni_char));
DP(("%s: Bitmap Non hit %d, %d\n", __FUNCTION__, bitmap_nohit++, uni_char));
}
#endif /* _MGFONT_TTF_CACHE */
@@ -567,7 +572,7 @@ char_bitmap_pixmap (LOGFONT* logfont, DEVFONT* devfont,
if (ft_inst_info->cache) {
TTFCACHEINFO * pcache;
int datasize;
int size = source->rows * (*pitch);
int size = source->rows * source->pitch;
pcache = __mg_ttc_search(ft_inst_info->cache,
uni_char, &datasize);
@@ -580,23 +585,41 @@ char_bitmap_pixmap (LOGFONT* logfont, DEVFONT* devfont,
cache_info.advance = ft_inst_info->advance;
cache_info.delta = ft_inst_info->delta;
cache_info.bitmap = source->buffer;
cache_info.pitch = *pitch;
cache_info.pitch = source->pitch;
cache_info.width = source->width;
cache_info.height = source->rows;
cache_info.flag = TRUE;
DP(("Should Write %d to cache length = %d, bitmap size = %d, cache = %p\n",
cache_info.unicode, sizeof(TTFCACHEINFO) + size, size,
ft_inst_info->cache));
DP(("%s: Write 0x%x to cache length = %d, bitmap size = %d, cache = %p\n",
__FUNCTION__,
cache_info.unicode, (int)(sizeof(TTFCACHEINFO) + size), size,
ft_inst_info->cache));
__mg_ttc_write(ft_inst_info->cache,
&cache_info, sizeof(TTFCACHEINFO) + size);
}
else {
if (pcache->flag == FALSE) {
pcache->pitch = *pitch;
pcache->pitch = source->pitch;
pcache->width = source->width;
pcache->height = source->rows;
memcpy(pcache->bitmap, source->buffer, size);
pcache->flag = TRUE;
DP(("%s: Write bitmap data for 0x%x to cache, bitmap size = %d, cache = %p\n",
__FUNCTION__,
uni_char, size, ft_inst_info->cache));
}
/* VincentWei: override the bbox.w and bbox.h with bitmap */
if (sz && (source->rows != sz->cy || source->width != sz->cx)) {
_ERR_PRINTF("FreeType2: BITMAP size does not match BBOX\n");
sz->cx = source->width;
sz->cy = source->rows;
}
#ifdef _DEBUG
print_bitmap_mono(source->buffer, source->width, source->rows, source->pitch);
#endif
FT_Done_Glyph (ft_inst_info->glyph);
FT_UNLOCK(&ft_lock);
return pcache->bitmap;
@@ -613,6 +636,8 @@ char_bitmap_pixmap (LOGFONT* logfont, DEVFONT* devfont,
sz->cy = source->rows;
}
print_bitmap_mono(buffer, source->width, source->rows, source->pitch);
FT_Done_Glyph (ft_inst_info->glyph);
FT_UNLOCK(&ft_lock);
return buffer;
@@ -685,7 +710,7 @@ get_glyph_advance (LOGFONT* logfont, DEVFONT* devfont,
}
#ifdef _MGFONT_TTF_CACHE
static inline int make_hash_key(unsigned short data)
static inline int make_hash_key(Uchar32 data)
{
return ((int)data % 37);
}
@@ -807,8 +832,8 @@ new_instance (LOGFONT* logfont, DEVFONT* devfont, BOOL need_sbc_font)
/* if unmask non-cache and no rotation */
if (!(logfont->style & FS_OTHER_TTFNOCACHE)) {
HCACHE hCache = __mg_ttc_is_exist(logfont->family, logfont->charset,
logfont->style, logfont->size, logfont->rotation);
HCACHE hCache = __mg_ttc_is_exist(devfont->name,
logfont->size, logfont->rotation);
DP(("__mg_ttc_is_exist() return %p\n", hCache));
/* No this style's cache */
if (hCache == 0) {
@@ -829,18 +854,17 @@ new_instance (LOGFONT* logfont, DEVFONT* devfont, BOOL need_sbc_font)
blksize += sizeof(TTFCACHEINFO);
DP(("BITMAP Space = %d, Whole Space = %d\n",
blksize - sizeof(TTFCACHEINFO), blksize));
(int)(blksize - sizeof(TTFCACHEINFO)), blksize));
blksize = (blksize + 3) & -4;
nblk = ( _MGTTF_CACHE_SIZE * 1024 )/ blksize;
DP(("[Before New a Cache], col = %d, row = %d, blksize = %d, "
"blksize(bitmap) = %d, nblk = %d\n",
rows, col, blksize, blksize-sizeof(TTFCACHEINFO), nblk));
rows, col, blksize, (int)(blksize-sizeof(TTFCACHEINFO)), nblk));
ft_inst_info->cache = __mg_ttc_create(logfont->family,
logfont->charset, logfont->style, logfont->size,
logfont->rotation, nblk , blksize, _TTF_HASH_NDIR,
make_hash_key);
ft_inst_info->cache = __mg_ttc_create(devfont->name,
logfont->size, logfont->rotation,
nblk , blksize, _TTF_HASH_NDIR, make_hash_key);
DP(("__mg_ttc_create() return %p\n", ft_inst_info->cache));
} else {
@@ -945,7 +969,8 @@ static int is_rotatable (LOGFONT* logfont, DEVFONT* devfont, int rot_desired)
}
/************************ Create/Destroy FreeType font ***********************/
static void* load_font_data (DEVFONT* devfont, const char* font_name, const char *file_name)
static void* load_font_data (DEVFONT* devfont,
const char* font_name, const char *file_name)
{
int i;
FT_Error error;
@@ -954,27 +979,32 @@ static void* load_font_data (DEVFONT* devfont, const char* font_name, const char
FTFACEINFO* ft_face_info = (FTFACEINFO*) calloc (1, sizeof(FTFACEINFO));
FT2_DATA* ft_data = (FT2_DATA*) calloc(1, sizeof(FTFACEINFO));
if (ft_face_info == NULL || ft_data == NULL) {
goto error_free;
}
FT_LOCK(&ft_lock);
#ifdef _MGFONT_TTF_CACHE
strcpy (ft_face_info->filepathname, file_name);
ft_face_info->filepathname = strdup (file_name);
if (ft_face_info->filepathname == NULL)
goto error;
ft_face_info->face_index = 0;
error = FT_New_Face (ft_library, file_name, ft_face_info->face_index, &face);
error = FT_New_Face(ft_library, file_name, ft_face_info->face_index, &face);
#else
error = FT_New_Face (ft_library, file_name, 0, &ft_face_info->face);
error = FT_New_Face(ft_library, file_name, 0, &ft_face_info->face);
face = ft_face_info->face;
#endif
if (error == FT_Err_Unknown_File_Format) {
_MG_PRINTF ("FONT>FT2: the font file could be opened and read, but it\n\
...appears that its font format is unsupported \n");
_ERR_PRINTF ("FONT>FT2: bad file format: %s\n", file_name);
goto error;
}
else if (error) {
_MG_PRINTF ("FONT>FT2: another error code means that the font file \n\
... could not be opened or read. or simply that it is\
broken ... \n ");
_ERR_PRINTF ("FONT>FT2: failed to open the font file: %s (%d)\n",
file_name, error);
goto error;
}
@@ -988,8 +1018,8 @@ static void* load_font_data (DEVFONT* devfont, const char* font_name, const char
&& (charmap->encoding_id == TT_APPLE_ID_DEFAULT))) {
error = FT_Set_Charmap (face, charmap);
if (error) {
_MG_PRINTF ("FONT>FT2: can not set \
UNICODE CharMap, error is %d \n", error);
_ERR_PRINTF ("FONT>FT2: can not set UNICODE CharMap (%d)\n",
error);
goto error;
}
#ifdef _MGFONT_TTF_CACHE
@@ -1000,7 +1030,7 @@ static void* load_font_data (DEVFONT* devfont, const char* font_name, const char
}
if (i == face->num_charmaps) {
_MG_PRINTF ("FONT>FT2: No UNICODE CharMap\n");
_ERR_PRINTF ("FONT>FT2: No UNICODE CharMap: %s\n", file_name);
goto error;
}
@@ -1045,20 +1075,31 @@ static void* load_font_data (DEVFONT* devfont, const char* font_name, const char
error:
FT_UNLOCK(&ft_lock);
free (ft_data);
free (ft_face_info);
error_free:
if (ft_data)
free (ft_data);
if (ft_face_info) {
if (ft_face_info->filepathname)
free(ft_face_info->filepathname);
free(ft_face_info);
}
return NULL;
}
static void unload_font_data (DEVFONT* devfont, void* data)
{
FT2_DATA* ft_data = (FT2_DATA*)data;
#ifndef _MGFONT_TTF_CACHE
FT_Done_Face ((ft_data->ft_face_info)->face);
#ifdef _MGFONT_TTF_CACHE
free(ft_data->ft_face_info->filepathname);
#else
FT_Done_Face((ft_data->ft_face_info)->face);
#endif
free (ft_data->ft_face_info);
free (ft_data->ft_inst_info);
free (ft_data);
free(ft_data->ft_face_info);
free(ft_data->ft_inst_info);
free(ft_data);
}
static void ShowErr (const char* message , int error)
+12 -12
View File
@@ -63,14 +63,13 @@ extern "C" {
#endif
#ifdef _MGFONT_TTF_CACHE
typedef unsigned long HCACHE;
typedef int (* MakeHashKeyFunc)(unsigned short unicode);
typedef void* HCACHE;
typedef int (* MakeHashKeyFunc)(Uchar32 unicode);
#endif
typedef struct tagFTFACEINFO {
#ifdef _MGFONT_TTF_CACHE
/*should be MAX_PATH*/
char filepathname[256];
char* filepathname;
int face_index;
int cmap_index;
#else
@@ -115,32 +114,33 @@ typedef struct tagFTINSTANCEINFO {
#define _TTF_HASH_NDIR 37
typedef struct tagTTFCACHEINFO {
unsigned short unicode;
short glyph_code;
Uchar32 unicode;
FT_UInt glyph_code;
FT_Vector advance;
FT_BBox bbox;
FT_Vector delta;
int flag;
int pitch;
int width;
int height;
void *bitmap;
} TTFCACHEINFO, *PTTFCACHEINFO;
extern HCACHE __mg_ttc_create(char *family, char *charset,
DWORD style, int size, int rotation,
extern HCACHE __mg_ttc_create(const char *df_name, int size, int rotation,
int nblk, int blksize, int ndir, MakeHashKeyFunc makeHashKey);
extern HCACHE __mg_ttc_is_exist(const char *df_name, int size, int rotation);
extern int __mg_ttc_write(HCACHE hCache, TTFCACHEINFO *data, int size);
extern void __mg_ttc_release(HCACHE hCache);
extern int __mg_ttc_sys_init(int maxCache, int cacheSize);
extern void __mg_ttc_sys_deinit(void);
extern TTFCACHEINFO *__mg_ttc_search(HCACHE hCache,
unsigned short unicode, int *size);
extern HCACHE __mg_ttc_is_exist(char *family, char *charset,
DWORD style, int size, int rotation);
Uchar32 unicode, int *size);
extern void __mg_ttc_refer(HCACHE hCache);
#endif
#undef TTF_DBG
#define TTF_DBG
#ifndef TTF_DBG
#define DP(x)