changeset 21317:dcfd069efd8f

Move ass_font_t allocation to ass_font.h.
author eugeni
date Mon, 27 Nov 2006 17:13:52 +0000
parents c07ce830b435
children c012ba4bf229
files libass/ass_cache.c libass/ass_cache.h libass/ass_font.c libass/ass_font.h libass/ass_render.c
diffstat 5 files changed, 37 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/libass/ass_cache.c	Mon Nov 27 16:56:57 2006 +0000
+++ b/libass/ass_cache.c	Mon Nov 27 17:13:52 2006 +0000
@@ -34,7 +34,7 @@
 
 #define MAX_FONT_CACHE_SIZE 100
 
-static ass_font_t* font_cache;
+static ass_font_t** font_cache;
 static int font_cache_size;
 
 static int font_compare(ass_font_desc_t* a, ass_font_desc_t* b) {
@@ -48,39 +48,40 @@
 }
 
 /**
- * \brief Get a face object, either from cache or created through FreeType+FontConfig.
- * \param library FreeType library object
- * \param fontconfig_priv fontconfig private data
+ * \brief Get a face struct from cache.
  * \param desc required face description
- * \return new font struct
-*/ 
-ass_font_t* ass_new_font(FT_Library library, void* fontconfig_priv, ass_font_desc_t* desc)
+ * \return font struct
+*/
+ass_font_t* ass_font_cache_find(ass_font_desc_t* desc)
 {
 	int i;
-	ass_font_t* item;
-	int error;
 	
 	for (i=0; i<font_cache_size; ++i)
-		if (font_compare(desc, &(font_cache[i].desc)))
-			return font_cache + i;
+		if (font_compare(desc, &(font_cache[i]->desc)))
+			return font_cache[i];
+
+	return 0;
+}
 
+/**
+ * \brief Add a face struct to cache.
+ * \param font font struct
+*/
+void ass_font_cache_add(ass_font_t* font)
+{
 	if (font_cache_size == MAX_FONT_CACHE_SIZE) {
 		mp_msg(MSGT_ASS, MSGL_FATAL, MSGTR_LIBASS_TooManyFonts);
-		return 0;
+		// FIXME: possible memory leak
+		return;
 	}
 
-	item = font_cache + font_cache_size;
-	error = ass_font_init(library, fontconfig_priv, item, desc);
-	if (error) // FIXME: mp_msg
-		return 0;
+	font_cache[font_cache_size] = font;
 	font_cache_size++;
-
-	return item;
 }
 
 void ass_font_cache_init(void)
 {
-	font_cache = calloc(MAX_FONT_CACHE_SIZE, sizeof(ass_font_t));
+	font_cache = calloc(MAX_FONT_CACHE_SIZE, sizeof(ass_font_t*));
 	font_cache_size = 0;
 }
 
@@ -88,7 +89,7 @@
 {
 	int i;
 	for (i = 0; i < font_cache_size; ++i) {
-		ass_font_t* item = font_cache + i;
+		ass_font_t* item = font_cache[i];
 		ass_font_free(item);
 	}
 	free(font_cache);
--- a/libass/ass_cache.h	Mon Nov 27 16:56:57 2006 +0000
+++ b/libass/ass_cache.h	Mon Nov 27 17:13:52 2006 +0000
@@ -44,7 +44,8 @@
 } ass_font_t;
 
 void ass_font_cache_init(void);
-ass_font_t* ass_new_font(FT_Library library, void* fontconfig_priv, ass_font_desc_t* desc);
+ass_font_t* ass_font_cache_find(ass_font_desc_t* desc);
+void ass_font_cache_add(ass_font_t* font);
 void ass_font_cache_done(void);
 
 
--- a/libass/ass_font.c	Mon Nov 27 16:56:57 2006 +0000
+++ b/libass/ass_font.c	Mon Nov 27 17:13:52 2006 +0000
@@ -48,23 +48,29 @@
 	}
 }
 
-int ass_font_init(FT_Library ftlibrary, void* fc_priv, ass_font_t* font, ass_font_desc_t* desc)
+ass_font_t* ass_font_new(FT_Library ftlibrary, void* fc_priv, ass_font_desc_t* desc)
 {
 	char* path;
 	int index;
 	FT_Face face;
 	int error;
+	ass_font_t* font;
+
+	font = ass_font_cache_find(desc);
+	if (font)
+		return font;
 	
 	path = fontconfig_select(fc_priv, desc->family, desc->bold, desc->italic, &index);
 	
 	error = FT_New_Face(ftlibrary, path, index, &face);
 	if (error) {
 		mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorOpeningFont, path, index);
-		return 1;
+		return 0;
 	}
 
 	charmap_magic(face);
 	
+	font = calloc(1, sizeof(ass_font_t));
 	font->path = strdup(path);
 	font->index = index;
 	font->face = face;
@@ -77,7 +83,9 @@
 	font->v.x = font->v.y = 0;
 	font->size = 0;
 
-	return 0;
+	ass_font_cache_add(font);
+	
+	return font;
 }
 
 void ass_font_set_transform(ass_font_t* font, FT_Matrix* m, FT_Vector* v)
@@ -133,4 +141,5 @@
 	if (font->face) FT_Done_Face(font->face);
 	if (font->path) free(font->path);
 	if (font->desc.family) free(font->desc.family);
+	free(font);
 }
--- a/libass/ass_font.h	Mon Nov 27 16:56:57 2006 +0000
+++ b/libass/ass_font.h	Mon Nov 27 17:13:52 2006 +0000
@@ -29,7 +29,7 @@
 #include "ass_bitmap.h"
 #include "ass_cache.h"
 
-int ass_font_init(FT_Library ftlibrary, void* fc_priv, ass_font_t* font, ass_font_desc_t* desc);
+ass_font_t* ass_font_new(FT_Library ftlibrary, void* fc_priv, ass_font_desc_t* desc);
 void ass_font_set_transform(ass_font_t* font, FT_Matrix* m, FT_Vector* v);
 void ass_font_set_size(ass_font_t* font, int size);
 FT_Glyph ass_font_get_glyph(void* fontconfig_priv, ass_font_t* font, uint32_t ch);
--- a/libass/ass_render.c	Mon Nov 27 16:56:57 2006 +0000
+++ b/libass/ass_render.c	Mon Nov 27 17:13:52 2006 +0000
@@ -561,7 +561,7 @@
 	else if (val == 1) val = 110; //italic
 	desc.italic = val;
 
-	render_context.font = ass_new_font(priv->ftlibrary, priv->fontconfig_priv, &desc);
+	render_context.font = ass_font_new(priv->ftlibrary, priv->fontconfig_priv, &desc);
 	
 	if (render_context.font)
 		change_font_size(render_context.font_size);