Reduce frequency of diskfont open/close

This commit is contained in:
Chris Young 2016-11-27 14:57:32 +00:00
parent f8f802cda5
commit 2043223788
3 changed files with 32 additions and 14 deletions

View File

@ -115,6 +115,8 @@ void ami_font_fini(void)
{
if(nsoption_bool(bitmap_fonts) == false) {
ami_font_bullet_fini();
} else {
ami_font_diskfont_fini();
}
}

View File

@ -39,6 +39,9 @@
#define MAX_FONT_NAME_SIZE 33
static plot_font_style_t *prev_fstyle = NULL;
static struct TextFont *prev_font = NULL;
static struct TextFont *ami_font_bm_open(struct RastPort *rp, const plot_font_style_t *fstyle)
{
struct TextFont *bmfont = NULL;
@ -46,6 +49,15 @@ static struct TextFont *ami_font_bm_open(struct RastPort *rp, const plot_font_st
char *fontname;
char font[MAX_FONT_NAME_SIZE];
if((prev_fstyle != NULL) && (prev_font != NULL) &&
(fstyle->family == prev_fstyle->family) &&
(fstyle->size == prev_fstyle->size) &&
(fstyle->flags == prev_fstyle->flags) &&
(fstyle->weight == prev_fstyle->weight)) {
LOG("(using current font)");
return prev_font;
}
if(rp == NULL) return NULL;
tattr.ta_Flags = 0;
@ -87,16 +99,19 @@ static struct TextFont *ami_font_bm_open(struct RastPort *rp, const plot_font_st
tattr.ta_Name = font;
tattr.ta_YSize = fstyle->size / FONT_SIZE_SCALE;
LOG("font: %s/%d", tattr.ta_Name, tattr.ta_YSize);
if(prev_font != NULL) CloseFont(prev_font);
if((bmfont = OpenDiskFont(&tattr))) {
SetRPAttrs(rp, RPTAG_Font, bmfont, TAG_DONE);
}
return bmfont;
}
if(prev_fstyle != NULL) {
memcpy(prev_fstyle, fstyle, sizeof(plot_font_style_t));
prev_font = bmfont;
}
static void ami_font_bm_close(struct TextFont *bmfont)
{
CloseFont(bmfont);
return bmfont;
}
static size_t ami_font_bm_convert_local_to_utf8_offset(const char *utf8string, size_t length, UWORD offset)
@ -125,15 +140,12 @@ static nserror amiga_bm_nsfont_width(const plot_font_style_t *fstyle,
if(bmfont == NULL) return NSERROR_INVALID;
if(utf8_to_local_encoding(string, length, &localtext) != NSERROR_OK) {
ami_font_bm_close(bmfont);
return NSERROR_INVALID;
}
*width = TextLength(glob->rp, localtext, (UWORD)strlen(localtext));
free(localtext);
ami_font_bm_close(bmfont);
return NSERROR_OK;
}
@ -164,7 +176,6 @@ static nserror amiga_bm_nsfont_position_in_string(const plot_font_style_t *fstyl
if(bmfont == NULL) return NSERROR_INVALID;
if(utf8_to_local_encoding(string, length, &localtext) != NSERROR_OK) {
ami_font_bm_close(bmfont);
return NSERROR_INVALID;
}
@ -174,7 +185,6 @@ static nserror amiga_bm_nsfont_position_in_string(const plot_font_style_t *fstyl
*actual_x = extent.te_Extent.MaxX;
free(localtext);
ami_font_bm_close(bmfont);
return NSERROR_OK;
}
@ -218,7 +228,6 @@ static nserror amiga_bm_nsfont_split(const plot_font_style_t *fstyle,
if(bmfont == NULL) return NSERROR_INVALID;
if(utf8_to_local_encoding(string, length, &localtext) != NSERROR_OK) {
ami_font_bm_close(bmfont);
return NSERROR_INVALID;
}
@ -252,7 +261,6 @@ static nserror amiga_bm_nsfont_split(const plot_font_style_t *fstyle,
}
free(localtext);
ami_font_bm_close(bmfont);
return NSERROR_OK;
}
@ -273,8 +281,6 @@ static ULONG amiga_bm_nsfont_text(struct RastPort *rp, const char *string, ULONG
free(localtext);
}
ami_font_bm_close(bmfont);
return 0;
}
@ -289,5 +295,14 @@ void ami_font_diskfont_init(void)
{
/* Set up table */
ami_nsfont = &ami_font_diskfont_table;
/* Alloc space to hold currently open font - doesn't matter if this fails */
prev_fstyle = calloc(1, sizeof(plot_font_style_t));
}
void ami_font_diskfont_fini(void)
{
if(prev_font != NULL) CloseFont(prev_font);
if(prev_fstyle != NULL) free(prev_fstyle);
}

View File

@ -19,5 +19,6 @@
#ifndef AMIGA_FONT_DISKFONT_H
#define AMIGA_FONT_DISKFONT_H
void ami_font_diskfont_init(void);
void ami_font_diskfont_fini(void);
#endif