changeset 23328:99ac5d381aed

Correct font size in libass. Values from TrueType OS/2 table are used to reproduce VSFilter behaviour. Magic 0.8 multiplier and scaling for the fractional part of font size are not needed anymore.
author eugeni
date Sat, 19 May 2007 14:11:41 +0000
parents d7af89e69e92
children 4d0dd033b167
files libass/ass_font.c libass/ass_mp.c libass/ass_render.c
diffstat 3 files changed, 28 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/libass/ass_font.c	Sat May 19 12:57:17 2007 +0000
+++ b/libass/ass_font.c	Sat May 19 14:11:41 2007 +0000
@@ -25,6 +25,7 @@
 #include FT_FREETYPE_H
 #include FT_SYNTHESIS_H
 #include FT_GLYPH_H
+#include FT_TRUETYPE_TABLES_H
 
 #include "ass.h"
 #include "ass_library.h"
@@ -67,9 +68,8 @@
 {
 	int i;
 	FT_Matrix m;
-	double size_scale = font->size / (int)font->size;
-	m.xx = double_to_d16(font->scale_x * size_scale);
-	m.yy = double_to_d16(font->scale_y * size_scale);
+	m.xx = double_to_d16(font->scale_x);
+	m.yy = double_to_d16(font->scale_y);
 	m.xy = m.yx = 0;
 	for (i = 0; i < font->n_faces; ++i)
 		FT_Set_Transform(font->faces[i], &m, &font->v);
@@ -154,6 +154,28 @@
 	update_transform(font);
 }
 
+static void face_set_size(FT_Face face, double size)
+{
+	TT_HoriHeader *hori = FT_Get_Sfnt_Table(face, ft_sfnt_hhea);
+	TT_OS2 *os2 = FT_Get_Sfnt_Table(face, ft_sfnt_os2);
+	double mscale = 1.;
+	FT_Size_RequestRec rq;
+	FT_Size_Metrics *m = &face->size->metrics;
+	// VSFilter uses metrics from TrueType OS/2 table
+	// The idea was borrowed from asa (http://asa.diac24.net)
+	if (hori && os2)
+		mscale = ((double)(hori->Ascender - hori->Descender)) / (os2->usWinAscent + os2->usWinDescent);
+	memset(&rq, 0, sizeof(rq));
+	rq.type = FT_SIZE_REQUEST_TYPE_REAL_DIM;
+	rq.width = 0;
+	rq.height = double_to_d6(size * mscale);
+	rq.horiResolution = rq.vertResolution = 0;
+	FT_Request_Size(face, &rq);
+	m->ascender /= mscale;
+	m->descender /= mscale;
+	m->height /= mscale;
+}
+
 /**
  * \brief Set font size
  **/
@@ -163,8 +185,7 @@
 	if (font->size != size) {
 		font->size = size;
 		for (i = 0; i < font->n_faces; ++i)
-			FT_Set_Pixel_Sizes(font->faces[i], 0, (int)size);
-		update_transform(font);
+			face_set_size(font->faces[i], size);
 	}
 }
 
--- a/libass/ass_mp.c	Sat May 19 12:57:17 2007 +0000
+++ b/libass/ass_mp.c	Sat May 19 14:11:41 2007 +0000
@@ -67,7 +67,6 @@
 static char* sub_cp = 0;
 #endif
 
-extern double ass_internal_font_size_coeff; 
 extern void process_force_style(ass_track_t* track);
 
 ass_track_t* ass_default_track(ass_library_t* library) {
@@ -92,7 +91,7 @@
 		style->Name = strdup("Default");
 		style->FontName = (font_fontconfig && font_name) ? strdup(font_name) : strdup("Sans");
 
-		fs = track->PlayResY * text_font_scale_factor / 100. / ass_internal_font_size_coeff;
+		fs = track->PlayResY * text_font_scale_factor / 100.;
 		// approximate autoscale coefficients
 		if (subtitle_autoscale == 2)
 			fs *= 1.3;
--- a/libass/ass_render.c	Sat May 19 12:57:17 2007 +0000
+++ b/libass/ass_render.c	Sat May 19 14:11:41 2007 +0000
@@ -506,8 +506,6 @@
 		return 0;
 }
 
-double ass_internal_font_size_coeff = 0.8;
-
 static void change_font_size(double sz)
 {
 	double size = sz * frame_context.font_scale;
@@ -2103,7 +2101,7 @@
 
 	ass_lazy_track_init();
 	
-	frame_context.font_scale = global_settings->font_size_coeff * ass_internal_font_size_coeff *
+	frame_context.font_scale = global_settings->font_size_coeff *
 	                           frame_context.orig_height / frame_context.track->PlayResY;
 	frame_context.border_scale = ((double)frame_context.orig_height) / frame_context.track->PlayResY;