1 /*****************************************************************************
2 * freetype.c : Put text on the video, using freetype2
3 *****************************************************************************
4 * Copyright (C) 2002, 2003 VideoLAN
5 * $Id: freetype.c,v 1.23 2003/09/30 16:41:13 hartman Exp $
7 * Authors: Sigmund Augdal <sigmunau@idi.ntnu.no>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
36 #include FT_FREETYPE_H
40 #define DEFAULT_FONT "/System/Library/Fonts/LucidaGrande.dfont"
41 #elif defined( SYS_BEOS )
42 #define DEFAULT_FONT "/boot/beos/etc/fonts/ttfonts/Swiss721.ttf"
44 #define DEFAULT_FONT ""
47 typedef struct line_desc_t line_desc_t;
49 /*****************************************************************************
51 *****************************************************************************/
52 static int Create ( vlc_object_t * );
53 static void Destroy ( vlc_object_t * );
55 static void Render ( vout_thread_t *, picture_t *,
56 const subpicture_t * );
57 static void RenderI420( vout_thread_t *, picture_t *,
58 const subpicture_t * );
59 static void RenderYUY2( vout_thread_t *, picture_t *,
60 const subpicture_t * );
61 static void RenderRV32( vout_thread_t *, picture_t *,
62 const subpicture_t * );
63 static int AddText ( vout_thread_t *, byte_t *, text_style_t *, int,
64 int, int, mtime_t, mtime_t );
65 static void FreeString( subpicture_t * );
67 static int GetUnicodeCharFromUTF8( byte_t ** );
69 static line_desc_t *NewLine( byte_t * );
71 /*****************************************************************************
73 *****************************************************************************/
74 #define FONT_TEXT N_("Font")
75 #define FONT_LONGTEXT N_("Filename of Font")
76 #define FONTSIZE_TEXT N_("Font size")
77 #define FONTSIZE_LONGTEXT N_("The size of the fonts used by the osd module" )
79 static char *ppsz_sizes[] = { "smaller", "small", "normal", "large", "larger", NULL};
82 add_category_hint( N_("Fonts"), NULL, VLC_FALSE );
83 add_file( "freetype-font", DEFAULT_FONT, NULL, FONT_TEXT, FONT_LONGTEXT, VLC_FALSE );
84 add_integer( "freetype-fontsize", 16, NULL, FONTSIZE_TEXT, FONTSIZE_LONGTEXT, VLC_TRUE );
85 add_string_from_list( "freetype-rel-fontsize", "normal", ppsz_sizes, NULL, FONTSIZE_TEXT, FONTSIZE_LONGTEXT, VLC_FALSE );
86 set_description( _("freetype2 font renderer") );
87 set_capability( "text renderer", 100 );
88 add_shortcut( "text" );
89 set_callbacks( Create, Destroy );
93 * Private data in a aubpicture. Describes a string.
95 struct subpicture_sys_t
102 /** The string associated with this subpicture */
104 line_desc_t *p_lines;
109 /** NULL-terminated list of glyphs making the string */
110 FT_BitmapGlyph *pp_glyphs;
111 /** list of relative positions for the glyphs */
112 FT_Vector *p_glyph_pos;
118 /*****************************************************************************
119 * text_remderer_sys_t: freetype local data
120 *****************************************************************************
121 * This structure is part of the video output thread descriptor.
122 * It describes the freetype specific properties of an output thread.
123 *****************************************************************************/
124 struct text_renderer_sys_t
126 FT_Library p_library; /* handle to library */
127 FT_Face p_face; /* handle to face object */
129 vlc_bool_t i_use_kerning;
130 uint8_t pi_gamma[256];
133 /*****************************************************************************
134 * Create: allocates osd-text video thread output method
135 *****************************************************************************
136 * This function allocates and initializes a Clone vout method.
137 *****************************************************************************/
138 #define gamma_value 2.0
139 static int Create( vlc_object_t *p_this )
141 vout_thread_t *p_vout = (vout_thread_t *)p_this;
144 int i_font_factor = 0;
146 double gamma_inv = 1.0f / gamma_value;
149 /* Allocate structure */
150 p_vout->p_text_renderer_data = malloc( sizeof( text_renderer_sys_t ) );
151 if( p_vout->p_text_renderer_data == NULL )
153 msg_Err( p_vout, "out of memory" );
157 for (i = 0; i < 256; i++) {
158 p_vout->p_text_renderer_data->pi_gamma[i] =
159 (uint8_t)( pow( (double)i / 255.0f, gamma_inv) * 255.0f );
162 var_Create( p_vout, "freetype-font", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
163 var_Create( p_vout, "freetype-fontsize",
164 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
165 var_Create( p_vout, "freetype-rel-fontsize",
166 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
168 /* Look what method was requested */
169 var_Get( p_vout, "freetype-font", &val );
170 psz_fontfile = val.psz_string;
172 if( !psz_fontfile || !*psz_fontfile )
174 if( psz_fontfile ) free( psz_fontfile );
175 psz_fontfile = (char *)malloc( PATH_MAX + 1 );
177 GetWindowsDirectory( psz_fontfile, PATH_MAX + 1 );
178 strcat( psz_fontfile, "\\fonts\\arial.ttf" );
180 strcpy( psz_fontfile, DEFAULT_FONT );
182 msg_Err( p_vout, "user didn't specify a font" );
183 free( p_vout->p_text_renderer_data );
188 i_error = FT_Init_FreeType( &p_vout->p_text_renderer_data->p_library );
191 msg_Err( p_vout, "couldn't initialize freetype" );
192 free( p_vout->p_text_renderer_data );
196 i_error = FT_New_Face( p_vout->p_text_renderer_data->p_library,
197 psz_fontfile ? psz_fontfile : "", 0,
198 &p_vout->p_text_renderer_data->p_face );
199 if( i_error == FT_Err_Unknown_File_Format )
201 msg_Err( p_vout, "file %s have unknown format", psz_fontfile );
202 FT_Done_FreeType( p_vout->p_text_renderer_data->p_library );
203 free( p_vout->p_text_renderer_data );
204 if( psz_fontfile ) free( psz_fontfile );
209 msg_Err( p_vout, "failed to load font file %s", psz_fontfile );
210 FT_Done_FreeType( p_vout->p_text_renderer_data->p_library );
211 free( p_vout->p_text_renderer_data );
212 if( psz_fontfile ) free( psz_fontfile );
215 if( psz_fontfile ) free( psz_fontfile );
217 i_error = FT_Select_Charmap( p_vout->p_text_renderer_data->p_face,
218 ft_encoding_unicode );
221 msg_Err( p_vout, "Font has no unicode translation table" );
222 FT_Done_Face( p_vout->p_text_renderer_data->p_face );
223 FT_Done_FreeType( p_vout->p_text_renderer_data->p_library );
224 free( p_vout->p_text_renderer_data );
228 p_vout->p_text_renderer_data->i_use_kerning =
229 FT_HAS_KERNING(p_vout->p_text_renderer_data->p_face);
231 var_Get( p_vout, "freetype-rel-fontsize", &val );
235 if( strncmp( val.psz_string, "smaller", 7 ) == 0 )
239 else if( strncmp( val.psz_string, "small", 5 ) == 0 )
243 else if( strncmp( val.psz_string, "normal", 6 ) == 0 )
247 else if( strncmp( val.psz_string, "large", 5 ) == 0 )
251 else if( strncmp( val.psz_string, "larger", 6 ) == 0 )
257 var_Get( p_vout, "freetype-fontsize", &val );
258 i_fontsize = val.i_int;
261 i_fontsize = (int) p_vout->render.i_height / i_font_factor;
262 free( val.psz_string );
266 var_Get( p_vout, "freetype-fontsize", &val );
267 i_fontsize = val.i_int;
270 i_error = FT_Set_Pixel_Sizes( p_vout->p_text_renderer_data->p_face, 0,
274 msg_Err( p_vout, "couldn't set font size to %d", i_fontsize );
275 free( p_vout->p_text_renderer_data );
278 p_vout->pf_add_string = AddText;
282 /*****************************************************************************
283 * Destroy: destroy Clone video thread output method
284 *****************************************************************************
285 * Clean up all data and library connections
286 *****************************************************************************/
287 static void Destroy( vlc_object_t *p_this )
289 vout_thread_t *p_vout = (vout_thread_t *)p_this;
290 FT_Done_Face( p_vout->p_text_renderer_data->p_face );
291 FT_Done_FreeType( p_vout->p_text_renderer_data->p_library );
292 free( p_vout->p_text_renderer_data );
295 /*****************************************************************************
296 * Render: place string in picture
297 *****************************************************************************
298 * This function merges the previously rendered freetype glyphs into a picture
299 *****************************************************************************/
300 static void Render( vout_thread_t *p_vout, picture_t *p_pic,
301 const subpicture_t *p_subpic )
303 switch( p_vout->output.i_chroma )
305 /* I420 target, no scaling */
306 case VLC_FOURCC('I','4','2','0'):
307 case VLC_FOURCC('I','Y','U','V'):
308 case VLC_FOURCC('Y','V','1','2'):
309 RenderI420( p_vout, p_pic, p_subpic );
312 /* RV16 target, scaling */
313 case VLC_FOURCC('R','V','1','6'):
314 RenderRV16( p_vout, p_pic, p_subpic );
317 /* RV32 target, scaling */
318 case VLC_FOURCC('R','V','2','4'):
319 case VLC_FOURCC('R','V','3','2'):
320 RenderRV32( p_vout, p_pic, p_subpic );
322 /* NVidia or BeOS overlay, no scaling */
323 case VLC_FOURCC('Y','U','Y','2'):
324 RenderYUY2( p_vout, p_pic, p_subpic );
328 msg_Err( p_vout, "unknown chroma, can't render SPU" );
334 * Draw a string on a i420 (or similar) picture
336 static void RenderI420( vout_thread_t *p_vout, picture_t *p_pic,
337 const subpicture_t *p_subpic )
339 subpicture_sys_t *p_string = p_subpic->p_sys;
340 int i_plane, x, y, pen_x, pen_y;
344 for( p_line = p_subpic->p_sys->p_lines; p_line != NULL; p_line = p_line->p_next )
346 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
349 int i_pitch = p_pic->p[ i_plane ].i_pitch;
351 p_in = p_pic->p[ i_plane ].p_pixels;
355 if ( p_string->i_flags & OSD_ALIGN_BOTTOM )
357 pen_y = p_pic->p[ i_plane ].i_lines - p_string->i_height -
358 p_string->i_y_margin;
362 pen_y = p_string->i_y_margin;
364 pen_y += p_vout->p_text_renderer_data->p_face->size->metrics.ascender >> 6;
365 if ( p_string->i_flags & OSD_ALIGN_RIGHT )
367 pen_x = i_pitch - p_line->i_width
368 - p_string->i_x_margin;
370 else if ( p_string->i_flags & OSD_ALIGN_LEFT )
372 pen_x = p_string->i_x_margin;
376 pen_x = i_pitch / 2 - p_line->i_width / 2
377 + p_string->i_x_margin;
380 for( i = 0; p_line->pp_glyphs[i] != NULL; i++ )
382 FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ];
383 #define alpha p_vout->p_text_renderer_data->pi_gamma[ p_glyph->bitmap.buffer[ x + y * p_glyph->bitmap.width ] ]
384 #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 ]
385 for(y = 0; y < p_glyph->bitmap.rows; y++ )
387 for( x = 0; x < p_glyph->bitmap.width; x++ )
390 pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 );
392 pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 );
394 pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 );
396 pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 );
400 for(y = 0; y < p_glyph->bitmap.rows; y++ )
402 for( x = 0; x < p_glyph->bitmap.width; x++ )
404 pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 ) +
405 ( 255 * alpha >> 8 );
414 if ( p_string->i_flags & OSD_ALIGN_BOTTOM )
416 pen_y = p_pic->p[i_plane].i_lines - ( p_string->i_height>>1) -
417 (p_string->i_y_margin>>1);
421 pen_y = p_string->i_y_margin >> 1;
423 pen_y += p_vout->p_text_renderer_data->p_face->size->metrics.ascender >> 7;
424 if ( p_string->i_flags & OSD_ALIGN_RIGHT )
426 pen_x = i_pitch - ( p_line->i_width >> 1 )
427 - ( p_string->i_x_margin >> 1 );
429 else if ( p_string->i_flags & OSD_ALIGN_LEFT )
431 pen_x = p_string->i_x_margin >> 1;
435 pen_x = i_pitch / 2 - p_line->i_width / 4
436 + p_string->i_x_margin / 2;
439 for( i = 0; p_line->pp_glyphs[i] != NULL; i++ )
441 FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ];
442 #define alpha p_vout->p_text_renderer_data->pi_gamma[ p_glyph->bitmap.buffer[ ( x + y * p_glyph->bitmap.width ) ] ]
443 #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) ]
444 for( y = 0; y < p_glyph->bitmap.rows; y+=2 )
446 for( x = 0; x < p_glyph->bitmap.width; x+=2 )
448 pixel = ( ( pixel * ( 0xFF - alpha ) ) >> 8 ) +
449 ( 0x80 * alpha >> 8 );
461 * Draw a string on a YUY2 picture
463 static void RenderYUY2( vout_thread_t *p_vout, picture_t *p_pic,
464 const subpicture_t *p_subpic )
466 subpicture_sys_t *p_string = p_subpic->p_sys;
467 int x, y, pen_x, pen_y;
471 for( p_line = p_subpic->p_sys->p_lines; p_line != NULL;
472 p_line = p_line->p_next )
475 int i_pitch = p_pic->p[0].i_pitch;
477 p_in = p_pic->p[0].p_pixels;
479 if ( p_string->i_flags & OSD_ALIGN_BOTTOM )
481 pen_y = p_pic->p[0].i_lines - p_string->i_height -
482 p_string->i_y_margin;
486 pen_y = p_string->i_y_margin;
488 pen_y += p_vout->p_text_renderer_data->p_face->size->metrics.ascender >> 6;
489 if ( p_string->i_flags & OSD_ALIGN_RIGHT )
491 pen_x = i_pitch - p_line->i_width
492 - p_string->i_x_margin;
494 else if ( p_string->i_flags & OSD_ALIGN_LEFT )
496 pen_x = p_string->i_x_margin;
500 pen_x = i_pitch / 2 /2 - p_line->i_width / 2 + p_string->i_x_margin;
503 for( i = 0; p_line->pp_glyphs[i] != NULL; i++ )
505 FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ];
506 #define alpha p_vout->p_text_renderer_data->pi_gamma[ p_glyph->bitmap.buffer[ x + y * p_glyph->bitmap.width ] ]
507 #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 ) ]
508 for(y = 0; y < p_glyph->bitmap.rows; y++ )
510 for( x = 0; x < p_glyph->bitmap.width; x++ )
513 pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 );
515 pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 );
517 pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 );
519 pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 );
523 for(y = 0; y < p_glyph->bitmap.rows; y++ )
525 for( x = 0; x < p_glyph->bitmap.width; x++ )
527 pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 ) +
528 ( 255 * alpha >> 8 );
538 * Draw a string on a RV32 picture
540 static void RenderRV32( vout_thread_t *p_vout, picture_t *p_pic,
541 const subpicture_t *p_subpic )
543 subpicture_sys_t *p_string = p_subpic->p_sys;
544 int i_plane, x, y, pen_x, pen_y;
550 for( p_line = p_subpic->p_sys->p_lines; p_line != NULL; p_line = p_line->p_next )
553 int i_pitch = p_pic->p[ i_plane ].i_pitch;
555 p_in = p_pic->p[ i_plane ].p_pixels;
557 if ( p_string->i_flags & OSD_ALIGN_BOTTOM )
559 pen_y = p_pic->p[ i_plane ].i_lines - p_string->i_height -
560 p_string->i_y_margin;
564 pen_y = p_string->i_y_margin;
566 pen_y += p_vout->p_text_renderer_data->p_face->size->metrics.ascender >> 6;
567 if ( p_string->i_flags & OSD_ALIGN_RIGHT )
569 pen_x = i_pitch - p_line->i_width
570 - p_string->i_x_margin;
572 else if ( p_string->i_flags & OSD_ALIGN_LEFT )
574 pen_x = p_string->i_x_margin;
578 pen_x = i_pitch / 2 / 4 - p_line->i_width / 2
579 + p_string->i_x_margin;
582 for( i = 0; p_line->pp_glyphs[i] != NULL; i++ )
584 FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ];
585 #define alpha p_vout->p_text_renderer_data->pi_gamma[ p_glyph->bitmap.buffer[ x + y * p_glyph->bitmap.width ] ]
586 #define pixel( c ) 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 ) * 4 + c ]
587 for(y = 0; y < p_glyph->bitmap.rows; y++ )
589 for( x = 0; x < p_glyph->bitmap.width; x++ )
592 pixel( 0 ) = ( ( pixel( 0 ) * ( 255 - alpha ) ) >> 8 );
593 pixel( 1 ) = ( ( pixel( 1 ) * ( 255 - alpha ) ) >> 8 );
594 pixel( 2 ) = ( ( pixel( 2 ) * ( 255 - alpha ) ) >> 8 );
596 pixel( 0 ) = ( ( pixel( 0 ) * ( 255 - alpha ) ) >> 8 );
597 pixel( 1 ) = ( ( pixel( 1 ) * ( 255 - alpha ) ) >> 8 );
598 pixel( 2 ) = ( ( pixel( 2 ) * ( 255 - alpha ) ) >> 8 );
600 pixel( 0 ) = ( ( pixel( 0 ) * ( 255 - alpha ) ) >> 8 );
601 pixel( 1 ) = ( ( pixel( 1 ) * ( 255 - alpha ) ) >> 8 );
602 pixel( 2 ) = ( ( pixel( 2 ) * ( 255 - alpha ) ) >> 8 );
604 pixel( 0 ) = ( ( pixel( 0 ) * ( 255 - alpha ) ) >> 8 );
605 pixel( 1 ) = ( ( pixel( 1 ) * ( 255 - alpha ) ) >> 8 );
606 pixel( 2 ) = ( ( pixel( 2 ) * ( 255 - alpha ) ) >> 8 );
610 for(y = 0; y < p_glyph->bitmap.rows; y++ )
612 for( x = 0; x < p_glyph->bitmap.width; x++ )
614 pixel( 0 ) = ( ( pixel( 0 ) * ( 255 - alpha ) ) >> 8 ) +
615 ( 255 * alpha >> 8 );
616 pixel( 1 ) = ( ( pixel( 1 ) * ( 255 - alpha ) ) >> 8 ) +
617 ( 255 * alpha >> 8 );
618 pixel( 2 ) = ( ( pixel( 2 ) * ( 255 - alpha ) ) >> 8 ) +
619 ( 255 * alpha >> 8 );
629 * This function receives a string and creates a subpicture for it. It
630 * also calculates the size needed for this string, and renders the
631 * needed glyphs into memory. It is used as pf_add_string callback in
632 * the vout method by this module
634 static int AddText ( vout_thread_t *p_vout, byte_t *psz_string,
635 text_style_t *p_style, int i_flags, int i_hmargin,
636 int i_vmargin, mtime_t i_start, mtime_t i_stop )
638 subpicture_sys_t *p_string;
639 int i, i_pen_y, i_pen_x, i_error, i_glyph_index, i_previous, i_char;
640 subpicture_t *p_subpic;
641 line_desc_t *p_line, *p_next;
649 if ( !psz_string || !*psz_string )
665 /* Create and initialize a subpicture */
666 p_subpic = vout_CreateSubPicture( p_vout, MEMORY_SUBPICTURE );
667 if ( p_subpic == NULL )
672 p_subpic->pf_render = Render;
673 p_subpic->pf_destroy = FreeString;
674 p_subpic->i_start = i_start;
675 p_subpic->i_stop = i_stop;
678 p_subpic->b_ephemer = VLC_TRUE;
682 p_subpic->b_ephemer = VLC_FALSE;
685 /* Create and initialize private data for the subpicture */
686 p_string = malloc( sizeof(subpicture_sys_t) );
687 if ( p_string == NULL )
689 msg_Err( p_vout, "Out of memory" );
692 p_subpic->p_sys = p_string;
693 p_string->i_flags = i_flags;
694 p_string->i_x_margin = i_hmargin;
695 p_string->i_y_margin = i_vmargin;
696 p_string->p_lines = 0;
697 p_string->psz_text = strdup( psz_string );
699 /* Calculate relative glyph positions and a bounding box for the
701 p_line = NewLine( psz_string );
704 msg_Err( p_vout, "Out of memory" );
707 p_string->p_lines = p_line;
713 #define face p_vout->p_text_renderer_data->p_face
714 #define glyph face->glyph
718 i_char = GetUnicodeCharFromUTF8( &psz_string );
720 if ( i_char == '\r' ) /* ignore CR chars wherever they may be */
725 if ( i_char == '\n' )
727 p_next = NewLine( psz_string );
730 msg_Err( p_vout, "Out of memory" );
733 p_line->p_next = p_next;
734 p_line->i_width = line.xMax;
735 p_line->i_height = face->size->metrics.height >> 6;
736 p_line->pp_glyphs[ i ] = NULL;
738 result.x = __MAX( result.x, line.xMax );
739 result.y += face->size->metrics.height >> 6;
745 i_pen_y += face->size->metrics.height >> 6;
746 msg_Dbg( p_vout, "Creating new line, i is %d", i );
751 i_glyph_index = FT_Get_Char_Index( face, i_char );
752 if ( p_vout->p_text_renderer_data->i_use_kerning && i_glyph_index
756 FT_Get_Kerning( face, i_previous, i_glyph_index,
757 ft_kerning_default, &delta );
758 i_pen_x += delta.x >> 6;
761 p_line->p_glyph_pos[ i ].x = i_pen_x;
762 p_line->p_glyph_pos[ i ].y = i_pen_y;
763 i_error = FT_Load_Glyph( face, i_glyph_index, FT_LOAD_DEFAULT );
766 msg_Err( p_vout, "FT_Load_Glyph returned %d", i_error );
769 i_error = FT_Get_Glyph( glyph, &tmp_glyph );
772 msg_Err( p_vout, "FT_Get_Glyph returned %d", i_error );
775 FT_Glyph_Get_CBox( tmp_glyph, ft_glyph_bbox_pixels, &glyph_size );
776 i_error = FT_Glyph_To_Bitmap( &tmp_glyph, ft_render_mode_normal,
778 if ( i_error ) continue;
779 p_line->pp_glyphs[ i ] = (FT_BitmapGlyph)tmp_glyph;
782 line.xMax = p_line->p_glyph_pos[i].x + glyph_size.xMax - glyph_size.xMin;
783 line.yMax = __MAX( line.yMax, glyph_size.yMax );
784 line.yMin = __MIN( line.yMin, glyph_size.yMin );
786 i_previous = i_glyph_index;
787 i_pen_x += glyph->advance.x >> 6;
790 p_line->i_width = line.xMax;
791 p_line->i_height = face->size->metrics.height >> 6;
792 p_line->pp_glyphs[ i ] = NULL;
793 result.x = __MAX( result.x, line.xMax );
794 result.y += line.yMax - line.yMin;
795 p_string->i_height = result.y;
796 p_string->i_width = result.x;
797 vout_DisplaySubPicture( p_vout, p_subpic );
804 FreeString( p_subpic );
805 vout_DestroySubPicture( p_vout, p_subpic );
809 static void FreeString( subpicture_t *p_subpic )
812 subpicture_sys_t *p_string = p_subpic->p_sys;
813 line_desc_t *p_line, *p_next;
815 if( p_subpic->p_sys == NULL ) return;
817 for( p_line = p_string->p_lines; p_line != NULL; p_line = p_next )
819 p_next = p_line->p_next;
820 for( i = 0; p_line->pp_glyphs[ i ] != NULL; i++ )
822 FT_Done_Glyph( (FT_Glyph)p_line->pp_glyphs[ i ] );
824 free( p_line->pp_glyphs );
825 free( p_line->p_glyph_pos );
829 free( p_string->psz_text );
833 /* convert one or more utf8 bytes into a unicode character */
834 static int GetUnicodeCharFromUTF8( byte_t **ppsz_utf8_string )
836 int i_remaining_bytes, i_char = 0;
837 if( ( **ppsz_utf8_string & 0xFC ) == 0xFC )
839 i_char = **ppsz_utf8_string & 1;
840 i_remaining_bytes = 5;
842 else if( ( **ppsz_utf8_string & 0xF8 ) == 0xF8 )
844 i_char = **ppsz_utf8_string & 3;
845 i_remaining_bytes = 4;
847 else if( ( **ppsz_utf8_string & 0xF0 ) == 0xF0 )
849 i_char = **ppsz_utf8_string & 7;
850 i_remaining_bytes = 3;
852 else if( ( **ppsz_utf8_string & 0xE0 ) == 0xE0 )
854 i_char = **ppsz_utf8_string & 15;
855 i_remaining_bytes = 2;
857 else if( ( **ppsz_utf8_string & 0xC0 ) == 0xC0 )
859 i_char = **ppsz_utf8_string & 31;
860 i_remaining_bytes = 1;
864 i_char = **ppsz_utf8_string;
865 i_remaining_bytes = 0;
867 while( i_remaining_bytes )
869 (*ppsz_utf8_string)++;
871 i_char = ( i_char << 6 ) + ( **ppsz_utf8_string & 0x3F );
873 (*ppsz_utf8_string)++;
877 static line_desc_t *NewLine( byte_t *psz_string )
880 line_desc_t *p_line = malloc( sizeof(line_desc_t) );
885 p_line->i_height = 0;
887 p_line->p_next = NULL;
889 /* We don't use CountUtf8Characters() here because we are not acutally
890 * sure the string is utf8. Better be safe than sorry. */
891 i_count = strlen( psz_string );
893 p_line->pp_glyphs = malloc( sizeof(FT_BitmapGlyph)
895 if( p_line->pp_glyphs == NULL )
900 p_line->p_glyph_pos = malloc( sizeof( FT_Vector )
902 if( p_line->p_glyph_pos == NULL )
904 free( p_line->pp_glyphs );