]> granicus.if.org Git - libass/commitdiff
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://svn.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 c1568f07fd9316328a24c760c81976717eec66ba..d2c9523c7eadad32c1c05e99a62946fee9f95003 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 639b8cb40ce20c00304692b5289a703491a810d6..ba9a41104b6fc2e4eb822996f4c29b229ecae549 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 ef81fa97286f451418255d428ad75340015c4fc8..338b2b42fd893969e7623a606b40cf29a33430e5 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 2a44c9fc2ccb45acc707dfeaf09912837ba806fa..176dbed6badd4c71307dc57a112ecccf26c77449 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 8cb6db205eb3316cbbf92d844e44d376f0ae54c7..c2348fa6d895cf8e2db45608c9098b01f7c58a4d 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 9aa1746908963020b8a46b164d4d2f78b3999f25..c36d67be46fc38280f6d6fb440d6331437f7fee4 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 1928e4361c0e21ce134098f847d04ed8f14d749e..cb8152e9ba60d2865c84c346ee1515d9e80a50aa 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 7a16b5b8f48692e6e59f9d71d6414252101e6b6f..34a8b7a034d69dac4fed7dfa9ca5268a96390cbe 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 b273b0f107754bdf85ec3ee8875579052e208fd2..ef9a5f7bcbc73617a8d68076c24b312464cba6b7 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;
 }