modules/misc/freetype.c:

  - calculate and store size information for each line independently.
  - misc other fixes
parent f79c8d43
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* freetype.c : Put text on the video, using freetype2 * freetype.c : Put text on the video, using freetype2
***************************************************************************** *****************************************************************************
* Copyright (C) 2002, 2003 VideoLAN * Copyright (C) 2002, 2003 VideoLAN
* $Id: freetype.c,v 1.14 2003/07/26 18:54:20 titer Exp $ * $Id: freetype.c,v 1.15 2003/07/31 17:03:46 sigmunau Exp $
* *
* Authors: Sigmund Augdal <sigmunau@idi.ntnu.no> * Authors: Sigmund Augdal <sigmunau@idi.ntnu.no>
* *
...@@ -44,6 +44,8 @@ ...@@ -44,6 +44,8 @@
#define DEFAULT_FONT "" #define DEFAULT_FONT ""
#endif #endif
typedef struct line_desc_t line_desc_t;
/***************************************************************************** /*****************************************************************************
* Local prototypes * Local prototypes
*****************************************************************************/ *****************************************************************************/
...@@ -59,8 +61,10 @@ static void RenderYUY2( vout_thread_t *, picture_t *, ...@@ -59,8 +61,10 @@ static void RenderYUY2( vout_thread_t *, picture_t *,
static int AddText ( vout_thread_t *, byte_t *, text_style_t *, int, static int AddText ( vout_thread_t *, byte_t *, text_style_t *, int,
int, int, mtime_t, mtime_t ); int, int, mtime_t, mtime_t );
static int GetUnicodeCharFromUTF8( byte_t ** ); static int GetUnicodeCharFromUTF8( byte_t ** );
static int CountUtf8Characters( byte_t * );
static void FreeString( subpicture_t * ); static void FreeString( subpicture_t * );
static line_desc_t *NewLine( byte_t * );
/***************************************************************************** /*****************************************************************************
* Module descriptor * Module descriptor
*****************************************************************************/ *****************************************************************************/
...@@ -91,10 +95,18 @@ struct subpicture_sys_t ...@@ -91,10 +95,18 @@ struct subpicture_sys_t
int i_flags; int i_flags;
/** The string associated with this subpicture */ /** The string associated with this subpicture */
byte_t *psz_text; byte_t *psz_text;
line_desc_t *p_lines;
};
struct line_desc_t
{
/** NULL-terminated list of glyphs making the string */ /** NULL-terminated list of glyphs making the string */
FT_BitmapGlyph *pp_glyphs; FT_BitmapGlyph *pp_glyphs;
/** list of relative positions for the glyphs */ /** list of relative positions for the glyphs */
FT_Vector *p_glyph_pos; FT_Vector *p_glyph_pos;
int i_height;
int i_width;
line_desc_t *p_next;
}; };
/***************************************************************************** /*****************************************************************************
...@@ -279,43 +291,44 @@ static void RenderI420( vout_thread_t *p_vout, picture_t *p_pic, ...@@ -279,43 +291,44 @@ static void RenderI420( vout_thread_t *p_vout, picture_t *p_pic,
subpicture_sys_t *p_string = p_subpic->p_sys; subpicture_sys_t *p_string = p_subpic->p_sys;
int i_plane, x, y, pen_x, pen_y; int i_plane, x, y, pen_x, pen_y;
unsigned int i; unsigned int i;
line_desc_t *p_line;
for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ ) for( p_line = p_subpic->p_sys->p_lines; p_line != NULL; p_line = p_line->p_next )
{ {
uint8_t *p_in; for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
int i_pitch = p_pic->p[i_plane].i_pitch; {
uint8_t *p_in;
int i_pitch = p_pic->p[ i_plane ].i_pitch;
p_in = p_pic->p[i_plane].p_pixels; p_in = p_pic->p[ i_plane ].p_pixels;
if ( i_plane == 0 ) if ( i_plane == 0 )
{
if ( p_string->i_flags & OSD_ALIGN_BOTTOM )
{
pen_y = p_pic->p[i_plane].i_lines - p_string->i_height -
p_string->i_y_margin;
}
else
{ {
pen_y = p_string->i_y_margin; if ( p_string->i_flags & OSD_ALIGN_BOTTOM )
} {
pen_y += p_vout->p_text_renderer_data->p_face->size->metrics.height / 100; pen_y = p_pic->p[ i_plane ].i_lines - p_string->i_height -
if ( p_string->i_flags & OSD_ALIGN_RIGHT ) p_string->i_y_margin;
{ }
pen_x = i_pitch - p_string->i_width else
- p_string->i_x_margin; {
} pen_y = p_string->i_y_margin;
else }
{ pen_y += p_vout->p_text_renderer_data->p_face->size->metrics.ascender >> 6;
pen_x = p_string->i_x_margin; if ( p_string->i_flags & OSD_ALIGN_RIGHT )
} {
pen_x = i_pitch - p_line->i_width
- p_string->i_x_margin;
}
else
{
pen_x = p_string->i_x_margin;
}
for( i = 0; p_string->pp_glyphs[i] != NULL; i++ ) for( i = 0; p_line->pp_glyphs[i] != NULL; i++ )
{
if( p_string->pp_glyphs[i] )
{ {
FT_BitmapGlyph p_glyph = p_string->pp_glyphs[ i ]; FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ];
#define alpha p_vout->p_text_renderer_data->pi_gamma[ p_glyph->bitmap.buffer[ x + y * p_glyph->bitmap.width ] ] #define alpha p_vout->p_text_renderer_data->pi_gamma[ p_glyph->bitmap.buffer[ x + y * p_glyph->bitmap.width ] ]
#define pixel p_in[ ( p_string->p_glyph_pos[ i ].y + pen_y + y - p_glyph->top ) * i_pitch+x + pen_x + p_string->p_glyph_pos[ i ].x + p_glyph->left ] #define pixel p_in[ ( p_line->p_glyph_pos[ i ].y + pen_y + y - p_glyph->top ) * i_pitch+x + pen_x + p_line->p_glyph_pos[ i ].x + p_glyph->left ]
for(y = 0; y < p_glyph->bitmap.rows; y++ ) for(y = 0; y < p_glyph->bitmap.rows; y++ )
{ {
for( x = 0; x < p_glyph->bitmap.width; x++ ) for( x = 0; x < p_glyph->bitmap.width; x++ )
...@@ -328,36 +341,33 @@ static void RenderI420( vout_thread_t *p_vout, picture_t *p_pic, ...@@ -328,36 +341,33 @@ static void RenderI420( vout_thread_t *p_vout, picture_t *p_pic,
} }
} }
} }
}
else
{
if ( p_string->i_flags & OSD_ALIGN_BOTTOM )
{
pen_y = p_pic->p[i_plane].i_lines - ( p_string->i_height>>1) -
(p_string->i_y_margin>>1);
}
else else
{ {
pen_y = p_string->i_y_margin >> 1; if ( p_string->i_flags & OSD_ALIGN_BOTTOM )
} {
pen_y += p_vout->p_text_renderer_data->p_face->size->metrics.height / 200; pen_y = p_pic->p[i_plane].i_lines - ( p_string->i_height>>1) -
if ( p_string->i_flags & OSD_ALIGN_RIGHT ) (p_string->i_y_margin>>1);
{ }
pen_x = i_pitch - ( p_string->i_width >> 1 ) else
- ( p_string->i_x_margin >> 1 ); {
} pen_y = p_string->i_y_margin >> 1;
else }
{ pen_y += p_vout->p_text_renderer_data->p_face->size->metrics.ascender >> 7;
pen_x = p_string->i_x_margin >> 1; if ( p_string->i_flags & OSD_ALIGN_RIGHT )
} {
pen_x = i_pitch - ( p_line->i_width >> 1 )
- ( p_string->i_x_margin >> 1 );
}
else
{
pen_x = p_string->i_x_margin >> 1;
}
for( i = 0; p_string->pp_glyphs[i] != NULL; i++ ) for( i = 0; p_line->pp_glyphs[i] != NULL; i++ )
{
if( p_string->pp_glyphs[i] )
{ {
FT_BitmapGlyph p_glyph = p_string->pp_glyphs[ i ]; FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ];
#define alpha p_vout->p_text_renderer_data->pi_gamma[ p_glyph->bitmap.buffer[ ( x + y * p_glyph->bitmap.width ) ] ] #define alpha p_vout->p_text_renderer_data->pi_gamma[ p_glyph->bitmap.buffer[ ( x + y * p_glyph->bitmap.width ) ] ]
#define pixel p_in[ ( (p_string->p_glyph_pos[ i ].y>>1) + pen_y + (y>>1) - ( p_glyph->top >> 1 ) ) * i_pitch + ( x >> 1 ) + pen_x + ( p_string->p_glyph_pos[ i ].x >> 1 ) + ( p_glyph->left >>1) ] #define pixel p_in[ ( ( p_line->p_glyph_pos[ i ].y >> 1 ) + pen_y + ( y >> 1 ) - ( p_glyph->top >> 1 ) ) * i_pitch + ( x >> 1 ) + pen_x + ( p_line->p_glyph_pos[ i ].x >> 1 ) + ( p_glyph->left >>1) ]
for( y = 0; y < p_glyph->bitmap.rows; y+=2 ) for( y = 0; y < p_glyph->bitmap.rows; y+=2 )
{ {
for( x = 0; x < p_glyph->bitmap.width; x+=2 ) for( x = 0; x < p_glyph->bitmap.width; x+=2 )
...@@ -381,47 +391,48 @@ static void RenderYUY2( vout_thread_t *p_vout, picture_t *p_pic, ...@@ -381,47 +391,48 @@ static void RenderYUY2( vout_thread_t *p_vout, picture_t *p_pic,
const subpicture_t *p_subpic ) const subpicture_t *p_subpic )
{ {
subpicture_sys_t *p_string = p_subpic->p_sys; subpicture_sys_t *p_string = p_subpic->p_sys;
int x, y, pen_x, pen_y; int i_plane, x, y, pen_x, pen_y;
unsigned int i; unsigned int i;
line_desc_t *p_line;
uint8_t *p_in; for( p_line = p_subpic->p_sys->p_lines; p_line != NULL; p_line = p_line->p_next )
int i_pitch = p_pic->p[0].i_pitch;
p_in = p_pic->p->p_pixels;
if ( p_string->i_flags & OSD_ALIGN_BOTTOM )
{ {
pen_y = p_pic->p->i_lines - p_string->i_height - uint8_t *p_in;
int i_pitch = p_pic->p[ i_plane ].i_pitch;
p_in = p_pic->p[ i_plane ].p_pixels;
if ( p_string->i_flags & OSD_ALIGN_BOTTOM )
{
pen_y = p_pic->p[ i_plane ].i_lines - p_string->i_height -
p_string->i_y_margin; p_string->i_y_margin;
} }
else else
{
pen_y = p_string->i_y_margin;
}
pen_y += p_vout->p_text_renderer_data->p_face->size->metrics.height / 100;
if ( p_string->i_flags & OSD_ALIGN_RIGHT )
{
pen_x = i_pitch - p_string->i_width - p_string->i_x_margin;
}
else
{
pen_x = p_string->i_x_margin;
}
/* TODO : set U & V bytes */
for( i = 0; p_string->pp_glyphs[i] != NULL; i++ )
{
if( p_string->pp_glyphs[i] )
{ {
FT_BitmapGlyph p_glyph = p_string->pp_glyphs[ i ]; pen_y = p_string->i_y_margin;
}
pen_y += p_vout->p_text_renderer_data->p_face->size->metrics.ascender >> 6;
if ( p_string->i_flags & OSD_ALIGN_RIGHT )
{
pen_x = i_pitch - p_line->i_width
- p_string->i_x_margin;
}
else
{
pen_x = p_string->i_x_margin;
}
for( i = 0; p_line->pp_glyphs[i] != NULL; i++ )
{
FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ];
#define alpha p_vout->p_text_renderer_data->pi_gamma[ p_glyph->bitmap.buffer[ x + y * p_glyph->bitmap.width ] ] #define alpha p_vout->p_text_renderer_data->pi_gamma[ p_glyph->bitmap.buffer[ x + y * p_glyph->bitmap.width ] ]
#define pixel p_in[ ( p_string->p_glyph_pos[ i ].y + pen_y + y - p_glyph->top ) * i_pitch + 2 * ( x + pen_x + p_string->p_glyph_pos[ i ].x + p_glyph->left ) ] #define pixel p_in[ ( p_line->p_glyph_pos[ i ].y + pen_y + y - p_glyph->top ) * i_pitch + 2 * ( x + pen_x + p_line->p_glyph_pos[ i ].x + p_glyph->left ) ]
for( y = 0; y < p_glyph->bitmap.rows; y++ ) for(y = 0; y < p_glyph->bitmap.rows; y++ )
{ {
for( x = 0; x < p_glyph->bitmap.width; x++ ) for( x = 0; x < p_glyph->bitmap.width; x++ )
{ {
pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 ) + pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 ) +
( 255 * alpha >> 8 ); ( 255 * alpha >> 8 );
#undef alpha #undef alpha
#undef pixel #undef pixel
} }
...@@ -444,6 +455,8 @@ static int AddText ( vout_thread_t *p_vout, byte_t *psz_string, ...@@ -444,6 +455,8 @@ static int AddText ( vout_thread_t *p_vout, byte_t *psz_string,
subpicture_sys_t *p_string; subpicture_sys_t *p_string;
int i, i_pen_y, i_pen_x, i_error, i_glyph_index, i_previous, i_char; int i, i_pen_y, i_pen_x, i_error, i_glyph_index, i_previous, i_char;
subpicture_t *p_subpic; subpicture_t *p_subpic;
line_desc_t *p_line, *p_next;
FT_BBox line; FT_BBox line;
FT_BBox glyph_size; FT_BBox glyph_size;
FT_Vector result; FT_Vector result;
...@@ -455,7 +468,6 @@ static int AddText ( vout_thread_t *p_vout, byte_t *psz_string, ...@@ -455,7 +468,6 @@ static int AddText ( vout_thread_t *p_vout, byte_t *psz_string,
line.xMax = 0; line.xMax = 0;
line.yMin = 0; line.yMin = 0;
line.yMax = 0; line.yMax = 0;
/* Create and initialize a subpicture */ /* Create and initialize a subpicture */
p_subpic = vout_CreateSubPicture( p_vout, MEMORY_SUBPICTURE ); p_subpic = vout_CreateSubPicture( p_vout, MEMORY_SUBPICTURE );
if ( p_subpic == NULL ) if ( p_subpic == NULL )
...@@ -488,23 +500,16 @@ static int AddText ( vout_thread_t *p_vout, byte_t *psz_string, ...@@ -488,23 +500,16 @@ static int AddText ( vout_thread_t *p_vout, byte_t *psz_string,
p_string->i_y_margin = i_vmargin; p_string->i_y_margin = i_vmargin;
p_string->psz_text = strdup( psz_string ); p_string->psz_text = strdup( psz_string );
p_string->pp_glyphs = malloc( sizeof(FT_GlyphSlot)
* ( strlen( p_string->psz_text ) + 1 ) ); /* Calculate relative glyph positions and a bounding box for the
if( p_string->pp_glyphs == NULL ) * entire string */
{ p_line = NewLine( psz_string );
msg_Err( p_vout, "Out of memory" ); if( p_line == NULL )
return VLC_ENOMEM;
}
p_string->p_glyph_pos = malloc( sizeof( FT_Vector )
* strlen( p_string->psz_text ) );
if( p_string->p_glyph_pos == NULL )
{ {
msg_Err( p_vout, "Out of memory" ); msg_Err( p_vout, "Out of memory" );
return VLC_ENOMEM; return VLC_ENOMEM;
} }
p_string->p_lines = p_line;
/* Calculate relative glyph positions and a bounding box for the
* entire string */
i_pen_x = 0; i_pen_x = 0;
i_pen_y = 0; i_pen_y = 0;
i_previous = 0; i_previous = 0;
...@@ -520,14 +525,27 @@ static int AddText ( vout_thread_t *p_vout, byte_t *psz_string, ...@@ -520,14 +525,27 @@ static int AddText ( vout_thread_t *p_vout, byte_t *psz_string,
} }
if ( i_char == '\n' ) if ( i_char == '\n' )
{ {
i_pen_x = 0; p_next = NewLine( psz_string );
if( p_next == NULL )
{
msg_Err( p_vout, "Out of memory" );
return VLC_ENOMEM;
}
p_line->p_next = p_next;
p_line->i_width = line.xMax;
p_line->i_height = face->size->metrics.height >> 6;
p_line->pp_glyphs[ i ] = NULL;
p_line = p_next;
result.x = __MAX( result.x, line.xMax ); result.x = __MAX( result.x, line.xMax );
result.y += face->size->metrics.height / 100; result.y += face->size->metrics.height >> 6;
i_pen_x = 0;
line.xMin = 0; line.xMin = 0;
line.xMax = 0; line.xMax = 0;
line.yMin = 0; line.yMin = 0;
line.yMax = 0; line.yMax = 0;
i_pen_y += face->size->metrics.height / 100; i_pen_y += face->size->metrics.height >> 6;
msg_Dbg( p_vout, "Creating new line, i is %d", i );
i = 0;
continue; continue;
} }
i_glyph_index = FT_Get_Char_Index( face, i_char ); i_glyph_index = FT_Get_Char_Index( face, i_char );
...@@ -540,8 +558,8 @@ static int AddText ( vout_thread_t *p_vout, byte_t *psz_string, ...@@ -540,8 +558,8 @@ static int AddText ( vout_thread_t *p_vout, byte_t *psz_string,
i_pen_x += delta.x >> 6; i_pen_x += delta.x >> 6;
} }
p_string->p_glyph_pos[ i ].x = i_pen_x; p_line->p_glyph_pos[ i ].x = i_pen_x;
p_string->p_glyph_pos[ i ].y = i_pen_y; p_line->p_glyph_pos[ i ].y = i_pen_y;
i_error = FT_Load_Glyph( face, i_glyph_index, FT_LOAD_DEFAULT ); i_error = FT_Load_Glyph( face, i_glyph_index, FT_LOAD_DEFAULT );
if ( i_error ) if ( i_error )
{ {
...@@ -555,15 +573,13 @@ static int AddText ( vout_thread_t *p_vout, byte_t *psz_string, ...@@ -555,15 +573,13 @@ static int AddText ( vout_thread_t *p_vout, byte_t *psz_string,
return VLC_EGENERIC; return VLC_EGENERIC;
} }
FT_Glyph_Get_CBox( tmp_glyph, ft_glyph_bbox_pixels, &glyph_size ); FT_Glyph_Get_CBox( tmp_glyph, ft_glyph_bbox_pixels, &glyph_size );
i_error = FT_Glyph_To_Bitmap( &tmp_glyph, i_error = FT_Glyph_To_Bitmap( &tmp_glyph, ft_render_mode_normal,
ft_render_mode_normal, NULL, 1 );
NULL,
1 );
if ( i_error ) continue; if ( i_error ) continue;
p_string->pp_glyphs[ i ] = (FT_BitmapGlyph)tmp_glyph; p_line->pp_glyphs[ i ] = (FT_BitmapGlyph)tmp_glyph;
/* Do rest */ /* Do rest */
line.xMax = p_string->p_glyph_pos[i].x + glyph_size.xMax - glyph_size.xMin; line.xMax = p_line->p_glyph_pos[i].x + glyph_size.xMax - glyph_size.xMin;
line.yMax = __MAX( line.yMax, glyph_size.yMax ); line.yMax = __MAX( line.yMax, glyph_size.yMax );
line.yMin = __MIN( line.yMin, glyph_size.yMin ); line.yMin = __MIN( line.yMin, glyph_size.yMin );
...@@ -571,7 +587,9 @@ static int AddText ( vout_thread_t *p_vout, byte_t *psz_string, ...@@ -571,7 +587,9 @@ static int AddText ( vout_thread_t *p_vout, byte_t *psz_string,
i_pen_x += glyph->advance.x >> 6; i_pen_x += glyph->advance.x >> 6;
i++; i++;
} }
p_string->pp_glyphs[i] = NULL; p_line->i_width = line.xMax;
p_line->i_height = face->size->metrics.height >> 6;
p_line->pp_glyphs[ i ] = NULL;
result.x = __MAX( result.x, line.xMax ); result.x = __MAX( result.x, line.xMax );
result.y += line.yMax - line.yMin; result.y += line.yMax - line.yMin;
p_string->i_height = result.y; p_string->i_height = result.y;
...@@ -589,13 +607,20 @@ static void FreeString( subpicture_t *p_subpic ) ...@@ -589,13 +607,20 @@ static void FreeString( subpicture_t *p_subpic )
{ {
unsigned int i; unsigned int i;
subpicture_sys_t *p_string = p_subpic->p_sys; subpicture_sys_t *p_string = p_subpic->p_sys;
for ( i = 0; p_string->pp_glyphs[ i ] != NULL; i++ ) line_desc_t *p_line, *p_next;
for( p_line = p_string->p_lines; p_line != NULL; p_line = p_next )
{ {
FT_Done_Glyph( (FT_Glyph)p_string->pp_glyphs[ i ] ); p_next = p_line->p_next;
for( i = 0; p_line->pp_glyphs[ i ] != NULL; i++ )
{
FT_Done_Glyph( (FT_Glyph)p_line->pp_glyphs[ i ] );
}
free( p_line->pp_glyphs );
free( p_line->p_glyph_pos );
free( p_line );
} }
free( p_string->psz_text ); free( p_string->psz_text );
free( p_string->p_glyph_pos );
free( p_string->pp_glyphs );
free( p_string ); free( p_string );
} }
...@@ -642,3 +667,46 @@ static int GetUnicodeCharFromUTF8( byte_t **ppsz_utf8_string ) ...@@ -642,3 +667,46 @@ static int GetUnicodeCharFromUTF8( byte_t **ppsz_utf8_string )
(*ppsz_utf8_string)++; (*ppsz_utf8_string)++;
return i_char; return i_char;
} }
static int CountUtf8Characters( byte_t *psz_string )
{
int i, i_num;
i = 0;
i_num = 0;
while( psz_string[ i ] != 0 && psz_string[ i ] != 10 )
{
if( psz_string[ i ] != 13 && psz_string[ i ] < 0x80 )
{
i_num++;
}
i++;
}
printf( "number of characters in line is %d\n", i_num );
return i_num;
}
static line_desc_t *NewLine( byte_t *psz_string )
{
line_desc_t *p_line;
p_line = malloc( sizeof(line_desc_t) );
if( !p_line )
{
return NULL;
}
p_line->i_height = 0;
p_line->i_width = 0;
p_line->p_next = NULL;
p_line->pp_glyphs = malloc( sizeof(FT_BitmapGlyph)
* ( CountUtf8Characters( psz_string ) + 1 ) );
if( p_line->pp_glyphs == NULL )
{
return NULL;
}
p_line->p_glyph_pos = malloc( sizeof( FT_Vector )
* CountUtf8Characters( psz_string ) );
if( p_line->p_glyph_pos == NULL )
{
return NULL;
}
return p_line;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment