]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - ft2.c
add missing args to Cmd_QuoteString in other files
[xonotic/darkplaces.git] / ft2.c
diff --git a/ft2.c b/ft2.c
index f5efcabbe30fb94465342df445ed522b4ccf499b..91a6f871cc4c6a6142c951813a1f844c6f14a466 100644 (file)
--- a/ft2.c
+++ b/ft2.c
@@ -152,6 +152,79 @@ typedef struct
 font_postprocess_t;
 static font_postprocess_t pp;
 
+typedef struct fontfilecache_s
+{
+       unsigned char *buf;
+       fs_offset_t len;
+       int refcount;
+       char path[MAX_QPATH];
+}
+fontfilecache_t;
+#define MAX_FONTFILES 8
+static fontfilecache_t fontfiles[MAX_FONTFILES];
+static const unsigned char *fontfilecache_LoadFile(const char *path, qboolean quiet, fs_offset_t *filesizepointer)
+{
+       int i;
+       unsigned char *buf;
+
+       for(i = 0; i < MAX_FONTFILES; ++i)
+       {
+               if(fontfiles[i].refcount > 0)
+                       if(!strcmp(path, fontfiles[i].path))
+                       {
+                               *filesizepointer = fontfiles[i].len;
+                               ++fontfiles[i].refcount;
+                               return fontfiles[i].buf;
+                       }
+       }
+
+       buf = FS_LoadFile(path, font_mempool, quiet, filesizepointer);
+       if(buf)
+       {
+               for(i = 0; i < MAX_FONTFILES; ++i)
+                       if(fontfiles[i].refcount <= 0)
+                       {
+                               strlcpy(fontfiles[i].path, path, sizeof(fontfiles[i].path));
+                               fontfiles[i].len = *filesizepointer;
+                               fontfiles[i].buf = buf;
+                               fontfiles[i].refcount = 1;
+                               return buf;
+                       }
+       }
+
+       return buf;
+}
+static void fontfilecache_Free(const unsigned char *buf)
+{
+       int i;
+       for(i = 0; i < MAX_FONTFILES; ++i)
+       {
+               if(fontfiles[i].refcount > 0)
+                       if(fontfiles[i].buf == buf)
+                       {
+                               if(--fontfiles[i].refcount <= 0)
+                               {
+                                       Mem_Free(fontfiles[i].buf);
+                                       fontfiles[i].buf = NULL;
+                               }
+                               return;
+                       }
+       }
+       // if we get here, it used regular allocation
+       Mem_Free((void *) buf);
+}
+static void fontfilecache_FreeAll(void)
+{
+       int i;
+       for(i = 0; i < MAX_FONTFILES; ++i)
+       {
+               if(fontfiles[i].refcount > 0)
+                       Mem_Free(fontfiles[i].buf);
+               fontfiles[i].buf = NULL;
+               fontfiles[i].refcount = 0;
+       }
+}
+
 /*
 ====================
 Font_CloseLibrary
@@ -161,6 +234,7 @@ Unload the FreeType2 DLL
 */
 void Font_CloseLibrary (void)
 {
+       fontfilecache_FreeAll();
        if (font_mempool)
                Mem_FreePool(&font_mempool);
        if (font_texturepool)
@@ -387,9 +461,13 @@ qboolean Font_LoadFont(const char *name, dp_font_t *dpfnt)
 
                if (!Font_LoadFile(dpfnt->fallbacks[i], dpfnt->fallback_faces[i], &dpfnt->settings, fb))
                {
-                       Con_Printf("Failed to allocate font for fallback %i of font %s\n", i, name);
+                       if(!FS_FileExists(va("%s.tga", dpfnt->fallbacks[i])))
+                       if(!FS_FileExists(va("%s.png", dpfnt->fallbacks[i])))
+                       if(!FS_FileExists(va("%s.jpg", dpfnt->fallbacks[i])))
+                       if(!FS_FileExists(va("%s.pcx", dpfnt->fallbacks[i])))
+                               Con_Printf("Failed to load font %s for fallback %i of font %s\n", dpfnt->fallbacks[i], i, name);
                        Mem_Free(fb);
-                       break;
+                       continue;
                }
                count = 0;
                for (s = 0; s < MAX_FONT_SIZES && dpfnt->req_sizes[s] >= 0; ++s)
@@ -444,7 +522,7 @@ static qboolean Font_LoadFile(const char *name, int _face, ft2_settings_t *setti
        char filename[MAX_QPATH];
        int status;
        size_t i;
-       unsigned char *data;
+       const unsigned char *data;
        fs_offset_t datasize;
 
        memset(font, 0, sizeof(*font));
@@ -466,18 +544,18 @@ static qboolean Font_LoadFile(const char *name, int _face, ft2_settings_t *setti
 
        // try load direct file
        memcpy(filename, name, namelen+1);
-       data = FS_LoadFile(filename, font_mempool, false, &datasize);
+       data = fontfilecache_LoadFile(filename, false, &datasize);
        // try load .ttf
        if (!data)
        {
                memcpy(filename + namelen, ".ttf", 5);
-               data = FS_LoadFile(filename, font_mempool, false, &datasize);
+               data = fontfilecache_LoadFile(filename, false, &datasize);
        }
        // try load .otf
        if (!data)
        {
                memcpy(filename + namelen, ".otf", 5);
-               data = FS_LoadFile(filename, font_mempool, false, &datasize);
+               data = fontfilecache_LoadFile(filename, false, &datasize);
        }
        // try load .pfb/afm
        if (!data)
@@ -485,12 +563,12 @@ static qboolean Font_LoadFile(const char *name, int _face, ft2_settings_t *setti
                ft2_attachment_t afm;
 
                memcpy(filename + namelen, ".pfb", 5);
-               data = FS_LoadFile(filename, font_mempool, false, &datasize);
+               data = fontfilecache_LoadFile(filename, false, &datasize);
 
                if (data)
                {
                        memcpy(filename + namelen, ".afm", 5);
-                       afm.data = FS_LoadFile(filename, font_mempool, false, &afm.size);
+                       afm.data = fontfilecache_LoadFile(filename, false, &afm.size);
 
                        if (afm.data)
                                Font_Attach(font, &afm);
@@ -510,6 +588,7 @@ static qboolean Font_LoadFile(const char *name, int _face, ft2_settings_t *setti
                _face = 0;
                status = qFT_New_Memory_Face(font_ft2lib, (FT_Bytes)data, datasize, 0, (FT_Face*)&font->face);
        }
+       font->data = data;
        if (status)
        {
                Con_Printf("ERROR: can't create face for %s\n"
@@ -794,8 +873,8 @@ static qboolean Font_LoadSize(ft2_font_t *font, float size, qboolean check_only)
 int Font_IndexForSize(ft2_font_t *font, float _fsize, float *outw, float *outh)
 {
        int match = -1;
-       int value = 1000000;
-       int nval;
+       float value = 1000000;
+       float nval;
        int matchsize = -10000;
        int m;
        float fsize_x, fsize_y;
@@ -825,7 +904,7 @@ int Font_IndexForSize(ft2_font_t *font, float _fsize, float *outw, float *outh)
                if (!maps[m])
                        continue;
                // "round up" to the bigger size if two equally-valued matches exist
-               nval = 0.5 * (abs(maps[m]->size - fsize_x) + abs(maps[m]->size - fsize_y));
+               nval = 0.5 * (fabs(maps[m]->size - fsize_x) + fabs(maps[m]->size - fsize_y));
                if (match == -1 || nval < value || (nval == value && matchsize < maps[m]->size))
                {
                        value = nval;
@@ -955,11 +1034,16 @@ static void UnloadMapRec(ft2_font_map_t *map)
 void Font_UnloadFont(ft2_font_t *font)
 {
        int i;
+
+       // unload fallbacks
+       if(font->next)
+               Font_UnloadFont(font->next);
+
        if (font->attachments && font->attachmentcount)
        {
-               for (i = 0; i < font->attachmentcount; ++i) {
+               for (i = 0; i < (int)font->attachmentcount; ++i) {
                        if (font->attachments[i].data)
-                               Mem_Free(font->attachments[i].data);
+                               fontfilecache_Free(font->attachments[i].data);
                }
                Mem_Free(font->attachments);
                font->attachmentcount = 0;
@@ -981,6 +1065,10 @@ void Font_UnloadFont(ft2_font_t *font)
                        font->face = NULL;
                }
        }
+       if (font->data) {
+           fontfilecache_Free(font->data);
+           font->data = NULL;
+       }
 }
 
 static float Font_SearchSize(ft2_font_t *font, FT_Face fontface, float size)