changeset 23981:705628816d98

Factor out common code from ass_font_new and ass_font_reselect.
author eugeni
date Fri, 03 Aug 2007 14:10:53 +0000
parents 27bac14b4ce4
children 17b5fa69243c
files libass/ass_font.c libass/ass_font.h
diffstat 2 files changed, 55 insertions(+), 66 deletions(-) [+]
line wrap: on
line diff
--- a/libass/ass_font.c	Fri Aug 03 13:43:11 2007 +0000
+++ b/libass/ass_font.c	Fri Aug 03 14:10:53 2007 +0000
@@ -87,46 +87,65 @@
 	return -1;
 }
 
+static void face_set_size(FT_Face face, double size);
+
+/**
+ * \brief Select a face with the given charcode and add it to ass_font_t
+ * \return index of the new face in font->faces, -1 if failed
+ */
+static int add_face(void* fc_priv, ass_font_t* font, uint32_t ch)
+{
+	char* path;
+	int index;
+	FT_Face face;
+	int error;
+	int mem_idx;
+	
+	if (font->n_faces == ASS_FONT_MAX_FACES)
+		return -1;
+	
+	path = fontconfig_select(fc_priv, font->desc.family, font->desc.bold,
+					      font->desc.italic, &index, ch);
+
+	mem_idx = find_font(font->library, path);
+	if (mem_idx >= 0) {
+		error = FT_New_Memory_Face(font->ftlibrary, (unsigned char*)font->library->fontdata[mem_idx].data,
+					   font->library->fontdata[mem_idx].size, 0, &face);
+		if (error) {
+			mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorOpeningMemoryFont, path);
+			return -1;
+		}
+	} else {
+		error = FT_New_Face(font->ftlibrary, path, index, &face);
+		if (error) {
+			mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorOpeningFont, path, index);
+			return -1;
+		}
+	}
+	charmap_magic(face);
+	
+	font->faces[font->n_faces++] = face;
+	update_transform(font);
+	face_set_size(face, font->size);
+	return font->n_faces - 1;
+}
+
 /**
  * \brief Create a new ass_font_t according to "desc" argument
  */
 ass_font_t* ass_font_new(ass_library_t* library, FT_Library ftlibrary, void* fc_priv, ass_font_desc_t* desc)
 {
-	char* path;
-	int index;
-	FT_Face face;
 	int error;
 	ass_font_t* fontp;
 	ass_font_t font;
-	int mem_idx;
 
 	fontp = ass_font_cache_find(desc);
 	if (fontp)
 		return fontp;
 	
-	path = fontconfig_select(fc_priv, desc->family, desc->bold, desc->italic, &index, 0);
-	
-	mem_idx = find_font(library, path);
-	if (mem_idx >= 0) {
-		error = FT_New_Memory_Face(ftlibrary, (unsigned char*)library->fontdata[mem_idx].data,
-					   library->fontdata[mem_idx].size, 0, &face);
-		if (error) {
-			mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorOpeningMemoryFont, path);
-			return 0;
-		}
-	} else {
-		error = FT_New_Face(ftlibrary, path, index, &face);
-		if (error) {
-			mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorOpeningFont, path, index);
-			return 0;
-		}
-	}
-
-	charmap_magic(face);
-	
+	font.library = library;
 	font.ftlibrary = ftlibrary;
-	font.faces[0] = face;
-	font.n_faces = 1;
+	font.n_faces = 0;
 	font.desc.family = strdup(desc->family);
 	font.desc.bold = desc->bold;
 	font.desc.italic = desc->italic;
@@ -135,7 +154,12 @@
 	font.v.x = font.v.y = 0;
 	font.size = 0.;
 
-	return ass_font_cache_add(&font);
+	error = add_face(fc_priv, &font, 0);
+	if (error == -1) {
+		free(font.desc.family);
+		return 0;
+	} else
+		return ass_font_cache_add(&font);
 }
 
 /**
@@ -189,43 +213,6 @@
 	}
 }
 
-#ifdef HAVE_FONTCONFIG
-/**
- * \brief Select a new FT_Face with the given character
- * The new face is added to the end of font->faces.
- **/
-static void ass_font_reselect(void* fontconfig_priv, ass_font_t* font, uint32_t ch)
-{
-	char* path;
-	int index;
-	FT_Face face;
-	int error;
-
-	if (font->n_faces == ASS_FONT_MAX_FACES)
-		return;
-	
-	path = fontconfig_select(fontconfig_priv, font->desc.family, font->desc.bold,
-					      font->desc.italic, &index, ch);
-
-	error = FT_New_Face(font->ftlibrary, path, index, &face);
-	if (error) {
-		mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorOpeningFont, path, index);
-		return;
-	}
-	charmap_magic(face);
-
-	error = FT_Get_Char_Index(face, ch);
-	if (error == 0) { // the new font face is not better then the old one
-		FT_Done_Face(face);
-		return;
-	}
-
-	font->faces[font->n_faces++] = face;
-	update_transform(font);
-	FT_Set_Pixel_Sizes(face, 0, (int)font->size);
-}
-#endif
-
 /**
  * \brief Get maximal font ascender and descender.
  * \param ch character code
@@ -279,10 +266,11 @@
 
 #ifdef HAVE_FONTCONFIG
 	if (index == 0) {
+		int face_idx;
 		mp_msg(MSGT_ASS, MSGL_INFO, MSGTR_LIBASS_GlyphNotFoundReselectingFont,
 		       ch, font->desc.family, font->desc.bold, font->desc.italic);
-		ass_font_reselect(fontconfig_priv, font, ch);
-		face = font->faces[font->n_faces - 1];
+		face_idx = add_face(fontconfig_priv, font, ch);
+		face = font->faces[face_idx];
 		index = FT_Get_Char_Index(face, ch);
 		if (index == 0) {
 			mp_msg(MSGT_ASS, MSGL_ERR, MSGTR_LIBASS_GlyphNotFound,
--- a/libass/ass_font.h	Fri Aug 03 13:43:11 2007 +0000
+++ b/libass/ass_font.h	Fri Aug 03 14:10:53 2007 +0000
@@ -31,6 +31,7 @@
 
 typedef struct ass_font_s {
 	ass_font_desc_t desc;
+	ass_library_t* library;
 	FT_Library ftlibrary;
 	FT_Face faces[ASS_FONT_MAX_FACES];
 	int n_faces;