changeset 21458:7af6c25a0cfc

Keep embedded fonts in ass_library_t and perform actual disk write in fontconfig initialization. This is required for some fontconfig-related improvements.
author eugeni
date Sun, 03 Dec 2006 17:59:13 +0000
parents af4c8fd34494
children 44b94d5d70e5
files 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
diffstat 9 files changed, 123 insertions(+), 85 deletions(-) [+]
line wrap: on
line diff
--- a/libass/ass.c	Sun Dec 03 17:46:37 2006 +0000
+++ b/libass/ass.c	Sun Dec 03 17:59:13 2006 +0000
@@ -1046,86 +1046,6 @@
 	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;
 
--- a/libass/ass.h	Sun Dec 03 17:46:37 2006 +0000
+++ b/libass/ass.h	Sun Dec 03 17:59:13 2006 +0000
@@ -182,7 +182,7 @@
  * \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
--- a/libass/ass_cache.c	Sun Dec 03 17:46:37 2006 +0000
+++ b/libass/ass_cache.c	Sun Dec 03 17:59:13 2006 +0000
@@ -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"
--- a/libass/ass_font.c	Sun Dec 03 17:46:37 2006 +0000
+++ b/libass/ass_font.c	Sun Dec 03 17:59:13 2006 +0000
@@ -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"
--- a/libass/ass_fontconfig.c	Sun Dec 03 17:46:37 2006 +0000
+++ b/libass/ass_fontconfig.c	Sun Dec 03 17:59:13 2006 +0000
@@ -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 @@
 	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 @@
  * \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 @@
 		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 @@
 	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;
 
--- a/libass/ass_fontconfig.h	Sun Dec 03 17:46:37 2006 +0000
+++ b/libass/ass_fontconfig.h	Sun Dec 03 17:59:13 2006 +0000
@@ -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);
 
--- a/libass/ass_library.c	Sun Dec 03 17:46:37 2006 +0000
+++ b/libass/ass_library.c	Sun Dec 03 17:59:13 2006 +0000
@@ -71,3 +71,19 @@
 		*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 ++;
+}
+
--- a/libass/ass_library.h	Sun Dec 03 17:46:37 2006 +0000
+++ b/libass/ass_library.h	Sun Dec 03 17:59:13 2006 +0000
@@ -21,10 +21,19 @@
 #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
--- a/libass/ass_render.c	Sun Dec 03 17:46:37 2006 +0000
+++ b/libass/ass_render.c	Sun Dec 03 17:59:13 2006 +0000
@@ -1928,7 +1928,7 @@
 
 	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;
 }