Keep embedded fonts in ass_library_t and perform actual disk write
authoreugeni <eugeni@b3059339-0415-0410-9bf9-f77b7e298cf2>
Sun, 3 Dec 2006 17:59:13 +0000 (17:59 +0000)
committereugeni <eugeni@b3059339-0415-0410-9bf9-f77b7e298cf2>
Sun, 3 Dec 2006 17:59:13 +0000 (17:59 +0000)
in fontconfig initialization. This is required for some fontconfig-related
improvements.

git-svn-id: svn://git.mplayerhq.hu/mplayer/trunk@21474 b3059339-0415-0410-9bf9-f77b7e298cf2

libass/ass.c
libass/ass.h
libass/ass_cache.c
libass/ass_font.c
libass/ass_fontconfig.c
libass/ass_fontconfig.h
libass/ass_library.c
libass/ass_library.h
libass/ass_render.c

index c1568f0..d2c9523 100644 (file)
@@ -1046,86 +1046,6 @@ int ass_read_styles(ass_track_t* track, char* fname, char* codepage)
        return 0;
 }
 
-static char* validate_fname(char* name)
-{
-       char* fname;
-       char* p;
-       char* q;
-       unsigned code;
-       int sz = strlen(name);
-
-       q = fname = malloc(sz + 1);
-       p = name;
-       while (*p) {
-               code = utf8_get_char(&p);
-               if (code == 0)
-                       break;
-               if (    (code > 0x7F) ||
-                       (code == '\\') ||
-                       (code == '/') ||
-                       (code == ':') ||
-                       (code == '*') ||
-                       (code == '?') ||
-                       (code == '<') ||
-                       (code == '>') ||
-                       (code == '|') ||
-                       (code == 0))
-               {
-                       *q++ = '_';
-               } else {
-                       *q++ = code;
-               }
-               if (p - name > sz)
-                       break;
-       }
-       *q = 0;
-       return fname;
-}
-
-/**
- * \brief Add a memory font.
- * \param name attachment name
- * \param data binary font data
- * \param data_size data size
-*/ 
-void ass_add_font(ass_library_t* library, const char* name, char* data, int data_size)
-{
-       char buf[1000];
-       FILE* fp = 0;
-       int rc;
-       struct stat st;
-       char* fname;
-       const char* fonts_dir = library->fonts_dir;
-
-       if (!fonts_dir)
-               return;
-       rc = stat(fonts_dir, &st);
-       if (rc) {
-               int res;
-#ifndef __MINGW32__
-               res = mkdir(fonts_dir, 0700);
-#else
-               res = mkdir(fonts_dir);
-#endif
-               if (res) {
-                       mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FailedToCreateDirectory, fonts_dir);
-               }
-       } else if (!S_ISDIR(st.st_mode)) {
-               mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_NotADirectory, fonts_dir);
-       }
-       
-       fname = validate_fname((char*)name);
-
-       snprintf(buf, 1000, "%s/%s", fonts_dir, fname);
-       free(fname);
-
-       fp = fopen(buf, "wb");
-       if (!fp) return;
-
-       fwrite(data, data_size, 1, fp);
-       fclose(fp);
-}
-
 long long ass_step_sub(ass_track_t* track, long long now, int movement) {
        int i;
 
index 639b8cb..ba9a411 100644 (file)
@@ -182,7 +182,7 @@ int ass_read_styles(ass_track_t* track, char* fname, char* codepage);
  * \param data binary font data
  * \param data_size data size
 */
-void ass_add_font(ass_library_t* library, const char* name, char* data, int data_size);
+void ass_add_font(ass_library_t* library, char* name, char* data, int data_size);
 
 /**
  * \brief Calculates timeshift from now to the start of some other subtitle event, depending on movement parameter
index ef81fa9..338b2b4 100644 (file)
@@ -27,6 +27,7 @@
 #include <assert.h>
 
 #include "mputils.h"
+#include "ass.h"
 #include "ass_fontconfig.h"
 #include "ass_font.h"
 #include "ass_bitmap.h"
index 2a44c9f..176dbed 100644 (file)
@@ -26,6 +26,8 @@
 #include FT_SYNTHESIS_H
 #include FT_GLYPH_H
 
+#include "ass.h"
+#include "ass_library.h"
 #include "ass_font.h"
 #include "ass_bitmap.h"
 #include "ass_cache.h"
index 8cb6db2..c2348fa 100644 (file)
@@ -28,6 +28,8 @@
 #include <sys/stat.h>
 
 #include "mputils.h"
+#include "ass.h"
+#include "ass_library.h"
 #include "ass_fontconfig.h"
 
 #ifdef HAVE_FONTCONFIG
@@ -184,6 +186,89 @@ char* fontconfig_select(fc_instance_t* priv, const char* family, unsigned bold,
        return fontconfig_select_with_charset(priv, family, bold, italic, index, 0);
 }
 
+static char* validate_fname(char* name)
+{
+       char* fname;
+       char* p;
+       char* q;
+       unsigned code;
+       int sz = strlen(name);
+
+       q = fname = malloc(sz + 1);
+       p = name;
+       while (*p) {
+               code = utf8_get_char(&p);
+               if (code == 0)
+                       break;
+               if (    (code > 0x7F) ||
+                       (code == '\\') ||
+                       (code == '/') ||
+                       (code == ':') ||
+                       (code == '*') ||
+                       (code == '?') ||
+                       (code == '<') ||
+                       (code == '>') ||
+                       (code == '|') ||
+                       (code == 0))
+               {
+                       *q++ = '_';
+               } else {
+                       *q++ = code;
+               }
+               if (p - name > sz)
+                       break;
+       }
+       *q = 0;
+       return fname;
+}
+
+/**
+ * \brief Process embedded matroska font. Saves it to ~/.mplayer/fonts.
+ * \param name attachment name
+ * \param data binary font data
+ * \param data_size data size
+*/ 
+static void process_fontdata(fc_instance_t* priv, ass_library_t* library, int idx)
+{
+       char buf[1000];
+       FILE* fp = 0;
+       int rc;
+       struct stat st;
+       char* fname;
+       const char* name = library->fontdata[idx].name;
+       const char* data = library->fontdata[idx].data;
+       int data_size = library->fontdata[idx].size;
+       const char* fonts_dir = library->fonts_dir;
+
+       if (!fonts_dir)
+               return;
+       rc = stat(fonts_dir, &st);
+       if (rc) {
+               int res;
+#ifndef __MINGW32__
+               res = mkdir(fonts_dir, 0700);
+#else
+               res = mkdir(fonts_dir);
+#endif
+               if (res) {
+                       mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FailedToCreateDirectory, fonts_dir);
+               }
+       } else if (!S_ISDIR(st.st_mode)) {
+               mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_NotADirectory, fonts_dir);
+       }
+       
+       fname = validate_fname((char*)name);
+
+       snprintf(buf, 1000, "%s/%s", fonts_dir, fname);
+       free(fname);
+
+       fp = fopen(buf, "wb");
+       if (!fp) return;
+
+       fwrite(data, data_size, 1, fp);
+       fclose(fp);
+}
+
 /**
  * \brief Init fontconfig.
  * \param dir additional directoryu for fonts
@@ -191,11 +276,13 @@ char* fontconfig_select(fc_instance_t* priv, const char* family, unsigned bold,
  * \param path default font path
  * \return pointer to fontconfig private data
 */ 
-fc_instance_t* fontconfig_init(const char* dir, const char* family, const char* path)
+fc_instance_t* fontconfig_init(ass_library_t* library, const char* family, const char* path)
 {
        int rc;
        struct stat st;
        fc_instance_t* priv = calloc(1, sizeof(fc_instance_t));
+       const char* dir = library->fonts_dir;
+       int i;
        
        rc = FcInit();
        assert(rc);
@@ -206,6 +293,9 @@ fc_instance_t* fontconfig_init(const char* dir, const char* family, const char*
                return 0;
        }
 
+       for (i = 0; i < library->num_fontdata; ++i)
+               process_fontdata(priv, library, i);
+
        if (FcDirCacheValid((const FcChar8 *)dir) == FcFalse)
        {
                mp_msg(MSGT_ASS, MSGL_INFO, MSGTR_LIBASS_UpdatingFontCache);
@@ -265,7 +355,7 @@ char* fontconfig_select(fc_instance_t* priv, const char* family, unsigned bold,
        return priv->path_default;
 }
 
-fc_instance_t* fontconfig_init(const char* dir, const char* family, const char* path)
+fc_instance_t* fontconfig_init(ass_library_t* library, const char* family, const char* path)
 {
        fc_instance_t* priv;
 
index 9aa1746..c36d67b 100644 (file)
@@ -27,7 +27,7 @@
 
 typedef struct fc_instance_s fc_instance_t;
 
-fc_instance_t* fontconfig_init(const char* dir, const char* family, const char* path);
+fc_instance_t* fontconfig_init(ass_library_t* library, const char* family, const char* path);
 char* fontconfig_select(fc_instance_t* priv, const char* family, unsigned bold, unsigned italic, int* index);
 void fontconfig_done(fc_instance_t* priv);
 
index 1928e43..cb8152e 100644 (file)
@@ -71,3 +71,19 @@ void ass_set_style_overrides(ass_library_t* priv, char** list)
                *q = strdup(*p);
        priv->style_overrides[cnt] = NULL;
 }
+
+static void grow_array(void **array, int nelem, size_t elsize)
+{
+       if (!(nelem & 31))
+               *array = realloc(*array, (nelem + 32) * elsize);
+}
+
+void ass_add_font(ass_library_t* priv, char* name, char* data, int size)
+{
+       grow_array((void**)&priv->fontdata, priv->num_fontdata, sizeof(*priv->fontdata));
+       priv->fontdata[priv->num_fontdata].name = name;
+       priv->fontdata[priv->num_fontdata].data = data;
+       priv->fontdata[priv->num_fontdata].size = size;
+       priv->num_fontdata ++;
+}
+
index 7a16b5b..34a8b7a 100644 (file)
 #ifndef __ASS_LIBRARY_H__
 #define __ASS_LIBRARY_H__
 
+typedef struct ass_fontdata_s {
+       char* name;
+       char* data;
+       int size;
+} ass_fontdata_t;
+
 struct ass_library_s {
        char* fonts_dir;
        int extract_fonts;
        char** style_overrides;
+
+       ass_fontdata_t* fontdata;
+       int num_fontdata;
 };
 
 #endif
index b273b0f..ef9a5f7 100644 (file)
@@ -1928,7 +1928,7 @@ int ass_set_fonts(ass_renderer_t* priv, const char* default_font, const char* de
 
        if (priv->fontconfig_priv)
                fontconfig_done(priv->fontconfig_priv);
-       priv->fontconfig_priv = fontconfig_init(priv->library->fonts_dir, default_family, default_font);
+       priv->fontconfig_priv = fontconfig_init(priv->library, default_family, default_font);
 
        return !!priv->fontconfig_priv;
 }