changeset 20446:e8adc3778348

Split ass_configure() into several smaller functions. FontConfig initialization moved from ass_init() to ass_set_fonts().
author eugeni
date Thu, 26 Oct 2006 20:08:46 +0000
parents d357a3737702
children 1ca74f693bc0
files libass/ass.h libass/ass_mp.c libass/ass_mp.h libass/ass_render.c libmpcodecs/vf_ass.c libmpcodecs/vf_vo.c
diffstat 6 files changed, 126 insertions(+), 93 deletions(-) [+]
line wrap: on
line diff
--- a/libass/ass.h	Thu Oct 26 15:27:00 2006 +0000
+++ b/libass/ass.h	Thu Oct 26 20:08:46 2006 +0000
@@ -26,21 +26,6 @@
 /// Libass "library object". Contents are private.
 typedef struct ass_instance_s ass_instance_t;
 
-/// used in ass_configure
-typedef struct ass_settings_s {
-	int frame_width;
-	int frame_height;
-	double font_size_coeff; // font size multiplier
-	double line_spacing; // additional line spacing (in frame pixels)
-	int top_margin; // height of top margin. Everything except toptitles is shifted down by top_margin.
-	int bottom_margin; // height of bottom margin. (frame_height - top_margin - bottom_margin) is original video height.
-	int left_margin;
-	int right_margin;
-	int use_margins; // 0 - place all subtitles inside original frame
-	                 // 1 - use margins for placing toptitles and subtitles
-	double aspect; // frame aspect ratio, d_width / d_height.
-} ass_settings_t;
-
 /// a linked list of images produced by ass renderer
 typedef struct ass_image_s {
 	int w, h; // bitmap width/height
@@ -64,12 +49,12 @@
  */
 void ass_done(ass_instance_t* priv);
 
-/**
- * \brief configure the library
- * \param priv library handle
- * \param config struct with configuration parameters. Caller is free to reuse it after this function returns.
- */
-void ass_configure(ass_instance_t* priv, const ass_settings_t* config);
+void ass_set_frame_size(ass_instance_t* priv, int w, int h);
+void ass_set_margins(ass_instance_t* priv, int t, int b, int l, int r);
+void ass_set_use_margins(ass_instance_t* priv, int use);
+void ass_set_aspect_ratio(ass_instance_t* priv, double ar);
+void ass_set_font_scale(ass_instance_t* priv, double font_scale);
+int  ass_set_fonts(ass_instance_t* priv, const char* fonts_dir, const char* default_font, const char* default_family);
 
 /**
  * \brief render a frame, producing a list of ass_image_t
--- a/libass/ass_mp.c	Thu Oct 26 15:27:00 2006 +0000
+++ b/libass/ass_mp.c	Thu Oct 26 20:08:46 2006 +0000
@@ -197,3 +197,31 @@
 	return track;
 }
 
+char *get_path(char *);
+
+extern char *font_name;
+#ifdef HAVE_FONTCONFIG
+extern int font_fontconfig;
+#else
+static int font_fontconfig = 0;
+#endif
+
+void ass_configure(ass_instance_t* priv, int w, int h) {
+	char *dir, *path, *family;
+	ass_set_frame_size(priv, w, h);
+	ass_set_margins(priv, ass_top_margin, ass_bottom_margin, 0, 0);
+	ass_set_use_margins(priv, ass_use_margins);
+	ass_set_font_scale(priv, ass_font_scale);
+
+	dir = get_path("fonts");
+	if (!font_fontconfig && font_name) path = strdup(font_name);
+	else path = get_path("subfont.ttf");
+	if (font_fontconfig && font_name) family = strdup(font_name);
+	else family = 0;
+
+	ass_set_fonts(priv, dir, path, family);
+
+	free(dir);
+	free(path);
+	free(family);
+}
--- a/libass/ass_mp.h	Thu Oct 26 15:27:00 2006 +0000
+++ b/libass/ass_mp.h	Thu Oct 26 20:08:46 2006 +0000
@@ -39,5 +39,7 @@
 int ass_process_subtitle(ass_track_t* track, subtitle* sub);
 ass_track_t* ass_read_subdata(sub_data* subdata, double fps);
 
+void ass_configure(ass_instance_t* priv, int w, int h);
+
 #endif
 
--- a/libass/ass_render.c	Thu Oct 26 15:27:00 2006 +0000
+++ b/libass/ass_render.c	Thu Oct 26 20:08:46 2006 +0000
@@ -42,16 +42,24 @@
 #define MAX_GLYPHS 1000
 #define MAX_LINES 100
 
-char *get_path(char *);
+static int last_render_id = 0;
 
-extern char *font_name;
-#ifdef HAVE_FONTCONFIG
-extern int font_fontconfig;
-#else
-static int font_fontconfig = 0;
-#endif
-
-static int last_render_id = 0;
+typedef struct ass_settings_s {
+	int frame_width;
+	int frame_height;
+	double font_size_coeff; // font size multiplier
+	double line_spacing; // additional line spacing (in frame pixels)
+	int top_margin; // height of top margin. Everything except toptitles is shifted down by top_margin.
+	int bottom_margin; // height of bottom margin. (frame_height - top_margin - bottom_margin) is original video height.
+	int left_margin;
+	int right_margin;
+	int use_margins; // 0 - place all subtitles inside original frame
+	                 // 1 - use margins for placing toptitles and subtitles
+	double aspect; // frame aspect ratio, d_width / d_height.
+	char* fonts_dir;
+	char* default_font;
+	char* default_family;
+} ass_settings_t;
 
 struct ass_instance_s {
 	FT_Library library;
@@ -213,11 +221,7 @@
 
 ass_instance_t* ass_init(void)
 {
-	char* family = 0;
-	char* path = 0;
-	char* fonts_path = 0;
 	int error;
-	fc_instance_t* fc_priv;
 	FT_Library ft;
 	ass_instance_t* priv = 0;
 	
@@ -225,43 +229,21 @@
 	memset(&frame_context, 0, sizeof(frame_context));
 	memset(&text_info, 0, sizeof(text_info));
 
-	if (font_fontconfig && font_name)
-		family = strdup(font_name);
-	
-	if (!font_fontconfig && font_name)
-		path = strdup(font_name);
-	else
-		path = get_path("subfont.ttf");
-
-	fonts_path = get_path("fonts");
-	
-	fc_priv = fontconfig_init(fonts_path, family, path);
-
-	free(fonts_path);
-	if (path) free(path);
-	if (family) free(family);
-
-	if (!fc_priv)
-		goto ass_init_exit;
-	
 	error = FT_Init_FreeType( &ft );
 	if ( error ) { 
 		mp_msg(MSGT_GLOBAL, MSGL_FATAL, "FT_Init_FreeType failed\n");
-		fontconfig_done(fc_priv);
 		goto ass_init_exit;
 	}
 
 	priv = calloc(1, sizeof(ass_instance_t));
 	if (!priv) {
 		FT_Done_FreeType(ft);
-		fontconfig_done(fc_priv);
 		goto ass_init_exit;
 	}
 
 	priv->synth_priv = ass_synth_init();
 
 	priv->library = ft;
-	priv->fontconfig_priv = fc_priv;
 	// images_root and related stuff is zero-filled in calloc
 	
 	ass_face_cache_init();
@@ -1921,19 +1903,76 @@
 	return 0;
 }
 
-void ass_configure(ass_instance_t* priv, const ass_settings_t* config)
+static void ass_reconfigure(ass_instance_t* priv)
+{
+	priv->render_id = ++last_render_id;
+	ass_glyph_cache_reset();
+}
+
+void ass_set_frame_size(ass_instance_t* priv, int w, int h)
+{
+	if (priv->settings.frame_width != w || priv->settings.frame_height != h) {
+		priv->settings.frame_width = w;
+		priv->settings.frame_height = h;
+		if (priv->settings.aspect == 0.)
+			priv->settings.aspect = ((double)w) / h;
+		ass_reconfigure(priv);
+	}
+}
+
+void ass_set_margins(ass_instance_t* priv, int t, int b, int l, int r)
+{
+	if (priv->settings.left_margin != l ||
+	    priv->settings.right_margin != r ||
+	    priv->settings.top_margin != t ||
+	    priv->settings.bottom_margin != b) {
+		priv->settings.left_margin = l;
+		priv->settings.right_margin = r;
+		priv->settings.top_margin = t;
+		priv->settings.bottom_margin = b;
+		ass_reconfigure(priv);
+	}
+}
+
+void ass_set_use_margins(ass_instance_t* priv, int use)
 {
-	if (memcmp(&priv->settings, config, sizeof(ass_settings_t)) != 0) {
-		mp_msg(MSGT_GLOBAL, MSGL_V, "ass_configure: %d x %d; margins: l: %d, r: %d, t: %d, b: %d  \n",
-				config->frame_width, config->frame_height,
-				config->left_margin, config->right_margin, config->top_margin, config->bottom_margin);
+	priv->settings.use_margins = use;
+}
+
+void ass_set_aspect_ratio(ass_instance_t* priv, double ar)
+{
+	if (priv->settings.aspect != ar) {
+		priv->settings.aspect = ar;
+		ass_reconfigure(priv);
+	}
+}
 
-		priv->render_id = ++last_render_id;
-		memcpy(&priv->settings, config, sizeof(ass_settings_t));
-		ass_glyph_cache_reset();
+void ass_set_font_scale(ass_instance_t* priv, double font_scale)
+{
+	if (priv->settings.font_size_coeff != font_scale) {
+		priv->settings.font_size_coeff = font_scale;
+		ass_reconfigure(priv);
 	}
 }
 
+int ass_set_fonts(ass_instance_t* priv, const char* fonts_dir, const char* default_font, const char* default_family)
+{
+	if (priv->settings.fonts_dir)
+		free(priv->settings.fonts_dir);
+	if (priv->settings.default_font)
+		free(priv->settings.default_font);
+	if (priv->settings.default_family)
+		free(priv->settings.default_family);
+
+	priv->settings.fonts_dir = fonts_dir ? strdup(fonts_dir) : 0;
+	priv->settings.default_font = default_font ? strdup(default_font) : 0;
+	priv->settings.default_family = default_family ? strdup(default_family) : 0;
+
+	priv->fontconfig_priv = fontconfig_init(fonts_dir, default_family, default_font);
+
+	return !!priv->fontconfig_priv;
+}
+
 /**
  * \brief Start a new frame
  */
--- a/libmpcodecs/vf_ass.c	Thu Oct 26 15:27:00 2006 +0000
+++ b/libmpcodecs/vf_ass.c	Thu Oct 26 20:08:46 2006 +0000
@@ -77,8 +77,6 @@
 	int width, int height, int d_width, int d_height,
 	unsigned int flags, unsigned int outfmt)
 {
-	ass_settings_t settings;
-	
 	if (outfmt == IMGFMT_IF09) return 0;
 
 	vf->priv->outh = height + ass_top_margin + ass_bottom_margin;
@@ -94,17 +92,8 @@
 	vf->priv->dirty_rows = malloc(vf->priv->outh);
 	
 	if (vf->priv->ass_priv) {
-		memset(&settings, 0, sizeof(ass_settings_t));
-		settings.frame_width = vf->priv->outw;
-		settings.frame_height = vf->priv->outh;
-		settings.font_size_coeff = ass_font_scale;
-		settings.line_spacing = ass_line_spacing;
-		settings.top_margin = ass_top_margin;
-		settings.bottom_margin = ass_bottom_margin;
-		settings.use_margins = ass_use_margins;
-		settings.aspect = ((double)d_width) / d_height;
-		
-		ass_configure(vf->priv->ass_priv, &settings);
+		ass_configure(vf->priv->ass_priv, vf->priv->outw, vf->priv->outh);
+		ass_set_aspect_ratio(vf->priv->ass_priv, ((double)d_width) / d_height);
 	}
 
 	return vf_next_config(vf, vf->priv->outw, vf->priv->outh, d_width, d_height, flags, outfmt);
--- a/libmpcodecs/vf_vo.c	Thu Oct 26 15:27:00 2006 +0000
+++ b/libmpcodecs/vf_vo.c	Thu Oct 26 20:08:46 2006 +0000
@@ -30,7 +30,6 @@
     vf_vo_data_t* vf_vo_data;
 #ifdef USE_ASS
     ass_instance_t* ass_priv;
-    ass_settings_t ass_settings;
 #endif
 };
 #define video_out (vf->priv->vf_vo_data->vo)
@@ -70,11 +69,8 @@
 	return 0;
 
 #ifdef USE_ASS
-    if (vf->priv->ass_priv) {
-        vf->priv->ass_settings.font_size_coeff = ass_font_scale;
-        vf->priv->ass_settings.line_spacing = ass_line_spacing;
-        vf->priv->ass_settings.use_margins = ass_use_margins;
-    }
+    if (vf->priv->ass_priv)
+	ass_configure(vf->priv->ass_priv, width, height);
 #endif
 
     ++vo_config_count;
@@ -112,7 +108,8 @@
     case VFCTRL_INIT_EOSD:
     {
         vf->priv->ass_priv = ass_init();
-        return vf->priv->ass_priv ? CONTROL_TRUE : CONTROL_FALSE;
+        if (!vf->priv->ass_priv) return CONTROL_FALSE;
+        return CONTROL_TRUE;
     }
     case VFCTRL_DRAW_EOSD:
     {
@@ -121,18 +118,11 @@
         if (!vo_config_count || !vf->priv->ass_priv) return CONTROL_FALSE;
         if (sub_visibility && vf->priv->ass_priv && ass_track && (pts != MP_NOPTS_VALUE)) {
             mp_eosd_res_t res;
-            ass_settings_t* const settings = &vf->priv->ass_settings;
             memset(&res, 0, sizeof(res));
             if (video_out->control(VOCTRL_GET_EOSD_RES, &res) == VO_TRUE) {
-                settings->frame_width = res.w;
-                settings->frame_height = res.h;
-                settings->top_margin = res.mt;
-                settings->bottom_margin = res.mb;
-                settings->left_margin = res.ml;
-                settings->right_margin = res.mr;
-                settings->aspect = ((double)res.w) / res.h;
+                ass_set_frame_size(vf->priv->ass_priv, res.w, res.h);
+                ass_set_margins(vf->priv->ass_priv, res.mt, res.mb, res.ml, res.mr);
             }
-            ass_configure(vf->priv->ass_priv, settings);
 
             images = ass_render_frame(vf->priv->ass_priv, ass_track, (pts+sub_delay) * 1000 + .5);
         }