@@ -107,8 +107,34 @@ static inline void calcdim(double *margin, double *height, double *width, const
107107 * width = MAX (* width , lwidth ) + 2 * * margin ;
108108}
109109
110+ SFT * GetCachedFont (const char * font , double size ) {
111+ if (sft .font == NULL || strcmp (last_font_name , font ) != 0 || size != last_font_size ) {
112+ if (sft .font ) {
113+ sft_freefont (sft .font );
114+ }
115+ loadfont (& sft , font , size , & lmtx );
116+ strncpy (last_font_name , font , sizeof (last_font_name ) - 1 );
117+ last_font_name [sizeof (last_font_name ) - 1 ] = '\0' ;
118+ last_font_size = size ;
119+ //printf("TrueType Font %s size:%f loaded.\n",last_font_name,last_font_size);
120+ }
121+
122+ return & sft ;
123+ }
124+
125+ int FreeCachedFont () {
126+ if (sft .font ) {
127+ //printf("Unload TrueType font %s sft.font = %p\n",last_font_name, sft.font);
128+ sft_freefont (sft .font );
129+ return 1 ;
130+ }
131+
132+ return 0 ;
133+ }
134+
110135RECT measure_text (const char * font , double size , const char * text ) {
111- loadfont (& sft , font , size , & lmtx );
136+ //loadfont(&sft, font, size, &lmtx);
137+ GetCachedFont (font , size );//We will usually have only one font file. lets make it faster
112138
113139 double margin , height , width ;
114140 calcdim (& margin , & height , & width , text );
@@ -118,12 +144,13 @@ RECT measure_text(const char *font, double size, const char *text) {
118144 rect .height += rect .height & 1 ;
119145 rect .width += rect .width & 1 ;
120146
121- sft_freefont (sft .font );
147+ // sft_freefont(sft.font);
122148 return rect ;
123149}
124150
125151BITMAP raster_text (const char * font , double size , const char * text , uint16_t color ) {
126- loadfont (& sft , font , size , & lmtx );
152+ //loadfont(&sft, font, size, &lmtx);
153+ GetCachedFont (font , size );//We will usually have only one font file. lets make it faster
127154
128155 double margin , height , width ;
129156
@@ -175,6 +202,6 @@ BITMAP raster_text(const char *font, double size, const char *text, uint16_t col
175202 bitmap .pData = canvas .pixels ;
176203 bitmap .enPixelFormat = PIXEL_FORMAT_1555 ;
177204
178- sft_freefont (sft .font );
205+ // sft_freefont(sft.font);
179206 return bitmap ;
180207}
0 commit comments