1 /*****************************************************************************
2 * freetype.c : Put text on the video, using freetype2
3 *****************************************************************************
4 * Copyright (C) 2002, 2003 VideoLAN
5 * $Id: freetype.c,v 1.30 2003/10/29 12:23:50 gbazin 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() */
40 #include FT_FREETYPE_H
44 #define DEFAULT_FONT "/System/Library/Fonts/LucidaGrande.dfont"
45 #elif defined( SYS_BEOS )
46 #define DEFAULT_FONT "/boot/beos/etc/fonts/ttfonts/Swiss721.ttf"
47 #elif defined( WIN32 )
48 #define DEFAULT_FONT "" /* Default font found at run-time */
50 #define DEFAULT_FONT "/usr/share/fonts/truetype/freefont/FreeSerifBold.ttf"
53 #if defined(HAVE_ICONV)
56 #if defined(HAVE_FRIBIDI)
57 #include <fribidi/fribidi.h>
60 typedef struct line_desc_t line_desc_t;
62 /*****************************************************************************
64 *****************************************************************************/
65 static int Create ( vlc_object_t * );
66 static void Destroy ( vlc_object_t * );
68 static void Render ( vout_thread_t *, picture_t *,
69 const subpicture_t * );
70 static void RenderI420( vout_thread_t *, picture_t *,
71 const subpicture_t * );
72 static void RenderYUY2( vout_thread_t *, picture_t *,
73 const subpicture_t * );
74 static void RenderRV32( vout_thread_t *, picture_t *,
75 const subpicture_t * );
76 static int AddText ( vout_thread_t *, byte_t *, text_style_t *, int,
77 int, int, mtime_t, mtime_t );
78 static void FreeString( subpicture_t * );
80 static int GetUnicodeCharFromUTF8( byte_t ** );
82 static line_desc_t *NewLine( byte_t * );
84 /*****************************************************************************
86 *****************************************************************************/
87 #define FONT_TEXT N_("Font")
88 #define FONT_LONGTEXT N_("Filename of Font")
89 #define FONTSIZE_TEXT N_("Font size")
90 #define FONTSIZE_LONGTEXT N_("The size of the fonts used by the osd module" )
92 static char *ppsz_sizes[] = { "smaller", "small", "normal", "large", "larger", NULL};
95 add_category_hint( N_("Fonts"), NULL, VLC_FALSE );
96 add_file( "freetype-font", DEFAULT_FONT, NULL, FONT_TEXT, FONT_LONGTEXT, VLC_FALSE );
97 add_integer( "freetype-fontsize", 16, NULL, FONTSIZE_TEXT, FONTSIZE_LONGTEXT, VLC_TRUE );
98 add_string_from_list( "freetype-rel-fontsize", "normal", ppsz_sizes, NULL, FONTSIZE_TEXT, FONTSIZE_LONGTEXT, VLC_FALSE );
99 set_description( _("freetype2 font renderer") );
100 set_capability( "text renderer", 100 );
101 add_shortcut( "text" );
102 set_callbacks( Create, Destroy );
106 * Private data in a subpicture. Describes a string.
108 struct subpicture_sys_t
115 /** The string associated with this subpicture */
117 line_desc_t *p_lines;
122 /** NULL-terminated list of glyphs making the string */
123 FT_BitmapGlyph *pp_glyphs;
124 /** list of relative positions for the glyphs */
125 FT_Vector *p_glyph_pos;
131 /*****************************************************************************
132 * text_renderer_sys_t: freetype local data
133 *****************************************************************************
134 * This structure is part of the video output thread descriptor.
135 * It describes the freetype specific properties of an output thread.
136 *****************************************************************************/
137 struct text_renderer_sys_t
139 FT_Library p_library; /* handle to library */
140 FT_Face p_face; /* handle to face object */
142 vlc_bool_t i_use_kerning;
143 uint8_t pi_gamma[256];
146 /*****************************************************************************
147 * Create: allocates osd-text video thread output method
148 *****************************************************************************
149 * This function allocates and initializes a Clone vout method.
150 *****************************************************************************/
151 #define gamma_value 2.0
152 static int Create( vlc_object_t *p_this )
154 vout_thread_t *p_vout = (vout_thread_t *)p_this;
157 int i_font_factor = 0;
159 double gamma_inv = 1.0f / gamma_value;
162 /* Allocate structure */
163 p_vout->p_text_renderer_data = malloc( sizeof( text_renderer_sys_t ) );
164 if( p_vout->p_text_renderer_data == NULL )
166 msg_Err( p_vout, "out of memory" );
170 for (i = 0; i < 256; i++) {
171 p_vout->p_text_renderer_data->pi_gamma[i] =
172 (uint8_t)( pow( (double)i / 255.0f, gamma_inv) * 255.0f );
175 var_Create( p_vout, "freetype-font", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
176 var_Create( p_vout, "freetype-fontsize",
177 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
178 var_Create( p_vout, "freetype-rel-fontsize",
179 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
181 /* Look what method was requested */
182 var_Get( p_vout, "freetype-font", &val );
183 psz_fontfile = val.psz_string;
185 if( !psz_fontfile || !*psz_fontfile )
187 if( psz_fontfile ) free( psz_fontfile );
188 psz_fontfile = (char *)malloc( PATH_MAX + 1 );
190 GetWindowsDirectory( psz_fontfile, PATH_MAX + 1 );
191 strcat( psz_fontfile, "\\fonts\\arial.ttf" );
193 strcpy( psz_fontfile, DEFAULT_FONT );
195 msg_Err( p_vout, "user didn't specify a font" );
196 free( p_vout->p_text_renderer_data );
201 i_error = FT_Init_FreeType( &p_vout->p_text_renderer_data->p_library );
204 msg_Err( p_vout, "couldn't initialize freetype" );
205 free( p_vout->p_text_renderer_data );
209 i_error = FT_New_Face( p_vout->p_text_renderer_data->p_library,
210 psz_fontfile ? psz_fontfile : "", 0,
211 &p_vout->p_text_renderer_data->p_face );
212 if( i_error == FT_Err_Unknown_File_Format )
214 msg_Err( p_vout, "file %s have unknown format", psz_fontfile );
215 FT_Done_FreeType( p_vout->p_text_renderer_data->p_library );
216 free( p_vout->p_text_renderer_data );
217 if( psz_fontfile ) free( psz_fontfile );
222 msg_Err( p_vout, "failed to load font file %s", psz_fontfile );
223 FT_Done_FreeType( p_vout->p_text_renderer_data->p_library );
224 free( p_vout->p_text_renderer_data );
225 if( psz_fontfile ) free( psz_fontfile );
228 if( psz_fontfile ) free( psz_fontfile );
230 i_error = FT_Select_Charmap( p_vout->p_text_renderer_data->p_face,
231 ft_encoding_unicode );
234 msg_Err( p_vout, "Font has no unicode translation table" );
235 FT_Done_Face( p_vout->p_text_renderer_data->p_face );
236 FT_Done_FreeType( p_vout->p_text_renderer_data->p_library );
237 free( p_vout->p_text_renderer_data );
241 p_vout->p_text_renderer_data->i_use_kerning =
242 FT_HAS_KERNING(p_vout->p_text_renderer_data->p_face);
244 var_Get( p_vout, "freetype-rel-fontsize", &val );
248 if( strncmp( val.psz_string, "smaller", 7 ) == 0 )
252 else if( strncmp( val.psz_string, "small", 5 ) == 0 )
256 else if( strncmp( val.psz_string, "normal", 6 ) == 0 )
260 else if( strncmp( val.psz_string, "large", 5 ) == 0 )
264 else if( strncmp( val.psz_string, "larger", 6 ) == 0 )
270 var_Get( p_vout, "freetype-fontsize", &val );
271 i_fontsize = val.i_int;
274 i_fontsize = (int) p_vout->render.i_height / i_font_factor;
275 free( val.psz_string );
279 var_Get( p_vout, "freetype-fontsize", &val );
280 i_fontsize = val.i_int;
283 i_error = FT_Set_Pixel_Sizes( p_vout->p_text_renderer_data->p_face, 0,
287 msg_Err( p_vout, "couldn't set font size to %d", i_fontsize );
288 free( p_vout->p_text_renderer_data );
291 p_vout->pf_add_string = AddText;
295 /*****************************************************************************
296 * Destroy: destroy Clone video thread output method
297 *****************************************************************************
298 * Clean up all data and library connections
299 *****************************************************************************/
300 static void Destroy( vlc_object_t *p_this )
302 vout_thread_t *p_vout = (vout_thread_t *)p_this;
303 FT_Done_Face( p_vout->p_text_renderer_data->p_face );
304 FT_Done_FreeType( p_vout->p_text_renderer_data->p_library );
305 free( p_vout->p_text_renderer_data );
308 /*****************************************************************************
309 * Render: place string in picture
310 *****************************************************************************
311 * This function merges the previously rendered freetype glyphs into a picture
312 *****************************************************************************/
313 static void Render( vout_thread_t *p_vout, picture_t *p_pic,
314 const subpicture_t *p_subpic )
316 switch( p_vout->output.i_chroma )
318 /* I420 target, no scaling */
319 case VLC_FOURCC('I','4','2','0'):
320 case VLC_FOURCC('I','Y','U','V'):
321 case VLC_FOURCC('Y','V','1','2'):
322 RenderI420( p_vout, p_pic, p_subpic );
325 /* RV16 target, scaling */
326 case VLC_FOURCC('R','V','1','6'):
327 RenderRV16( p_vout, p_pic, p_subpic );
330 /* RV32 target, scaling */
331 case VLC_FOURCC('R','V','2','4'):
332 case VLC_FOURCC('R','V','3','2'):
333 RenderRV32( p_vout, p_pic, p_subpic );
335 /* NVidia or BeOS overlay, no scaling */
336 case VLC_FOURCC('Y','U','Y','2'):
337 RenderYUY2( p_vout, p_pic, p_subpic );
341 msg_Err( p_vout, "unknown chroma, can't render SPU" );
347 * Draw a string on a i420 (or similar) picture
349 static void RenderI420( vout_thread_t *p_vout, picture_t *p_pic,
350 const subpicture_t *p_subpic )
352 subpicture_sys_t *p_string = p_subpic->p_sys;
353 int i_plane, x, y, pen_x, pen_y;
357 for( p_line = p_subpic->p_sys->p_lines; p_line != NULL; p_line = p_line->p_next )
359 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
362 int i_pic_pitch = p_pic->p[ i_plane ].i_pitch;
363 int i_pic_width = p_pic->p[ i_plane ].i_visible_pitch;
365 p_in = p_pic->p[ i_plane ].p_pixels;
369 if ( p_string->i_flags & OSD_ALIGN_BOTTOM )
371 pen_y = p_pic->p[ i_plane ].i_lines - p_string->i_height -
372 p_string->i_y_margin;
376 pen_y = p_string->i_y_margin;
378 pen_y += p_vout->p_text_renderer_data->p_face->size->metrics.ascender >> 6;
379 if ( p_string->i_flags & OSD_ALIGN_RIGHT )
381 pen_x = i_pic_width - p_line->i_width
382 - p_string->i_x_margin;
384 else if ( p_string->i_flags & OSD_ALIGN_LEFT )
386 pen_x = p_string->i_x_margin;
390 pen_x = i_pic_width / 2 - p_line->i_width / 2
391 + p_string->i_x_margin;
394 for( i = 0; p_line->pp_glyphs[i] != NULL; i++ )
396 FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ];
397 #define alpha p_vout->p_text_renderer_data->pi_gamma[ p_glyph->bitmap.buffer[ x + y * p_glyph->bitmap.width ] ]
398 #define pixel p_in[ ( p_line->p_glyph_pos[ i ].y + pen_y + y - p_glyph->top ) * i_pic_pitch + x + pen_x + p_line->p_glyph_pos[ i ].x + p_glyph->left ]
399 for(y = 0; y < p_glyph->bitmap.rows; y++ )
401 for( x = 0; x < p_glyph->bitmap.width; x++ )
404 pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 );
406 pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 );
408 pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 );
410 pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 );
414 for(y = 0; y < p_glyph->bitmap.rows; y++ )
416 for( x = 0; x < p_glyph->bitmap.width; x++ )
418 pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 ) +
419 ( 255 * alpha >> 8 );
428 if ( p_string->i_flags & OSD_ALIGN_BOTTOM )
430 pen_y = p_pic->p[i_plane].i_lines - ( p_string->i_height>>1) -
431 (p_string->i_y_margin>>1);
435 pen_y = p_string->i_y_margin >> 1;
437 pen_y += p_vout->p_text_renderer_data->p_face->size->metrics.ascender >> 7;
438 if ( p_string->i_flags & OSD_ALIGN_RIGHT )
440 pen_x = i_pic_width - ( p_line->i_width >> 1 )
441 - ( p_string->i_x_margin >> 1 );
443 else if ( p_string->i_flags & OSD_ALIGN_LEFT )
445 pen_x = p_string->i_x_margin >> 1;
449 pen_x = i_pic_width / 2 - p_line->i_width / 4
450 + p_string->i_x_margin / 2;
453 for( i = 0; p_line->pp_glyphs[i] != NULL; i++ )
455 FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ];
456 #define alpha p_vout->p_text_renderer_data->pi_gamma[ p_glyph->bitmap.buffer[ ( x + y * p_glyph->bitmap.width ) ] ]
457 #define pixel p_in[ ( ( p_line->p_glyph_pos[ i ].y >> 1 ) + pen_y + ( y >> 1 ) - ( p_glyph->top >> 1 ) ) * i_pic_pitch + ( x >> 1 ) + pen_x + ( p_line->p_glyph_pos[ i ].x >> 1 ) + ( p_glyph->left >>1) ]
458 for( y = 0; y < p_glyph->bitmap.rows; y+=2 )
460 for( x = 0; x < p_glyph->bitmap.width; x+=2 )
462 pixel = ( ( pixel * ( 0xFF - alpha ) ) >> 8 ) +
463 ( 0x80 * alpha >> 8 );
475 * Draw a string on a YUY2 picture
477 static void RenderYUY2( vout_thread_t *p_vout, picture_t *p_pic,
478 const subpicture_t *p_subpic )
480 subpicture_sys_t *p_string = p_subpic->p_sys;
481 int x, y, pen_x, pen_y;
485 for( p_line = p_subpic->p_sys->p_lines; p_line != NULL;
486 p_line = p_line->p_next )
489 int i_pic_pitch = p_pic->p[0].i_pitch;
490 int i_pic_width = p_pic->p[0].i_visible_pitch;
492 p_in = p_pic->p[0].p_pixels;
494 if ( p_string->i_flags & OSD_ALIGN_BOTTOM )
496 pen_y = p_pic->p[0].i_lines - p_string->i_height -
497 p_string->i_y_margin;
501 pen_y = p_string->i_y_margin;
503 pen_y += p_vout->p_text_renderer_data->p_face->size->metrics.ascender >> 6;
504 if ( p_string->i_flags & OSD_ALIGN_RIGHT )
506 pen_x = i_pic_width - p_line->i_width
507 - p_string->i_x_margin;
509 else if ( p_string->i_flags & OSD_ALIGN_LEFT )
511 pen_x = p_string->i_x_margin;
515 pen_x = i_pic_width / 2 /2 - p_line->i_width / 2 + p_string->i_x_margin;
518 for( i = 0; p_line->pp_glyphs[i] != NULL; i++ )
520 FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ];
521 #define alpha p_vout->p_text_renderer_data->pi_gamma[ p_glyph->bitmap.buffer[ x + y * p_glyph->bitmap.width ] ]
522 #define pixel p_in[ ( p_line->p_glyph_pos[ i ].y + pen_y + y - p_glyph->top ) * i_pic_pitch + 2 * ( x + pen_x + p_line->p_glyph_pos[ i ].x + p_glyph->left ) ]
523 for(y = 0; y < p_glyph->bitmap.rows; y++ )
525 for( x = 0; x < p_glyph->bitmap.width; x++ )
528 pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 );
530 pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 );
532 pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 );
534 pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 );
538 for(y = 0; y < p_glyph->bitmap.rows; y++ )
540 for( x = 0; x < p_glyph->bitmap.width; x++ )
542 pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 ) +
543 ( 255 * alpha >> 8 );
553 * Draw a string on a RV32 picture
555 static void RenderRV32( vout_thread_t *p_vout, picture_t *p_pic,
556 const subpicture_t *p_subpic )
558 subpicture_sys_t *p_string = p_subpic->p_sys;
559 int i_plane, x, y, pen_x, pen_y;
565 for( p_line = p_subpic->p_sys->p_lines; p_line != NULL; p_line = p_line->p_next )
568 int i_pic_pitch = p_pic->p[ i_plane ].i_pitch;
569 int i_pic_width = p_pic->p[ i_plane ].i_visible_pitch;
571 p_in = p_pic->p[ i_plane ].p_pixels;
573 if ( p_string->i_flags & OSD_ALIGN_BOTTOM )
575 pen_y = p_pic->p[ i_plane ].i_lines - p_string->i_height -
576 p_string->i_y_margin;
580 pen_y = p_string->i_y_margin;
582 pen_y += p_vout->p_text_renderer_data->p_face->size->metrics.ascender >> 6;
583 if ( p_string->i_flags & OSD_ALIGN_RIGHT )
585 pen_x = i_pic_width - p_line->i_width
586 - p_string->i_x_margin;
588 else if ( p_string->i_flags & OSD_ALIGN_LEFT )
590 pen_x = p_string->i_x_margin;
594 pen_x = i_pic_width / 2 / 4 - p_line->i_width / 2
595 + p_string->i_x_margin;
598 for( i = 0; p_line->pp_glyphs[i] != NULL; i++ )
600 FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ];
601 #define alpha p_vout->p_text_renderer_data->pi_gamma[ p_glyph->bitmap.buffer[ x + y * p_glyph->bitmap.width ] ]
602 #define pixel( c ) p_in[ ( p_line->p_glyph_pos[ i ].y + pen_y + y - p_glyph->top ) * i_pic_pitch + ( x + pen_x + p_line->p_glyph_pos[ i ].x + p_glyph->left ) * 4 + c ]
603 for(y = 0; y < p_glyph->bitmap.rows; y++ )
605 for( x = 0; x < p_glyph->bitmap.width; x++ )
608 pixel( 0 ) = ( ( pixel( 0 ) * ( 255 - alpha ) ) >> 8 );
609 pixel( 1 ) = ( ( pixel( 1 ) * ( 255 - alpha ) ) >> 8 );
610 pixel( 2 ) = ( ( pixel( 2 ) * ( 255 - alpha ) ) >> 8 );
612 pixel( 0 ) = ( ( pixel( 0 ) * ( 255 - alpha ) ) >> 8 );
613 pixel( 1 ) = ( ( pixel( 1 ) * ( 255 - alpha ) ) >> 8 );
614 pixel( 2 ) = ( ( pixel( 2 ) * ( 255 - alpha ) ) >> 8 );
616 pixel( 0 ) = ( ( pixel( 0 ) * ( 255 - alpha ) ) >> 8 );
617 pixel( 1 ) = ( ( pixel( 1 ) * ( 255 - alpha ) ) >> 8 );
618 pixel( 2 ) = ( ( pixel( 2 ) * ( 255 - alpha ) ) >> 8 );
620 pixel( 0 ) = ( ( pixel( 0 ) * ( 255 - alpha ) ) >> 8 );
621 pixel( 1 ) = ( ( pixel( 1 ) * ( 255 - alpha ) ) >> 8 );
622 pixel( 2 ) = ( ( pixel( 2 ) * ( 255 - alpha ) ) >> 8 );
626 for(y = 0; y < p_glyph->bitmap.rows; y++ )
628 for( x = 0; x < p_glyph->bitmap.width; x++ )
630 pixel( 0 ) = ( ( pixel( 0 ) * ( 255 - alpha ) ) >> 8 ) +
631 ( 255 * alpha >> 8 );
632 pixel( 1 ) = ( ( pixel( 1 ) * ( 255 - alpha ) ) >> 8 ) +
633 ( 255 * alpha >> 8 );
634 pixel( 2 ) = ( ( pixel( 2 ) * ( 255 - alpha ) ) >> 8 ) +
635 ( 255 * alpha >> 8 );
645 * This function receives a string and creates a subpicture for it. It
646 * also calculates the size needed for this string, and renders the
647 * needed glyphs into memory. It is used as pf_add_string callback in
648 * the vout method by this module
650 static int AddText ( vout_thread_t *p_vout, byte_t *psz_string,
651 text_style_t *p_style, int i_flags, int i_hmargin,
652 int i_vmargin, mtime_t i_start, mtime_t i_stop )
654 subpicture_sys_t *p_string;
655 int i, i_pen_y, i_pen_x, i_error, i_glyph_index, i_previous;
656 subpicture_t *p_subpic;
657 line_desc_t *p_line, *p_next;
658 uint32_t *p_unicode_string, i_char;
660 iconv_t iconv_handle;
668 if ( !psz_string || !*psz_string )
684 /* Create and initialize a subpicture */
685 p_subpic = vout_CreateSubPicture( p_vout, MEMORY_SUBPICTURE );
686 if ( p_subpic == NULL )
691 p_subpic->pf_render = Render;
692 p_subpic->pf_destroy = FreeString;
693 p_subpic->i_start = i_start;
694 p_subpic->i_stop = i_stop;
697 p_subpic->b_ephemer = VLC_TRUE;
701 p_subpic->b_ephemer = VLC_FALSE;
704 /* Create and initialize private data for the subpicture */
705 p_string = malloc( sizeof(subpicture_sys_t) );
706 if ( p_string == NULL )
708 msg_Err( p_vout, "Out of memory" );
711 p_subpic->p_sys = p_string;
712 p_string->i_flags = i_flags;
713 p_string->i_x_margin = i_hmargin;
714 p_string->i_y_margin = i_vmargin;
715 p_string->p_lines = 0;
716 p_string->psz_text = strdup( psz_string );
718 #if defined(HAVE_ICONV)
719 p_unicode_string = malloc( ( strlen(psz_string) + 1 ) * sizeof(uint32_t) );
720 if( p_unicode_string == NULL )
722 msg_Err( p_vout, "Out of memory" );
725 #if defined(WORDS_BIGENDIAN)
726 iconv_handle = iconv_open( "UCS-4BE", "UTF-8" );
728 iconv_handle = iconv_open( "UCS-4LE", "UTF-8" );
730 if( iconv_handle == (iconv_t)-1 )
732 msg_Warn( p_vout, "Unable to do convertion" );
737 char *p_in_buffer, *p_out_buffer;
738 size_t i_in_bytes, i_out_bytes, i_out_bytes_left, i_ret;
739 i_in_bytes = strlen( psz_string );
740 i_out_bytes = i_in_bytes * sizeof( uint32_t );
741 i_out_bytes_left = i_out_bytes;
742 p_in_buffer = psz_string;
743 p_out_buffer = (char *)p_unicode_string;
744 i_ret = iconv( iconv_handle, &p_in_buffer, &i_in_bytes, &p_out_buffer, &i_out_bytes_left );
747 msg_Warn( p_vout, "Failed to convert string to unicode (%s), bytes left %d", strerror(errno), i_in_bytes );
750 *(uint32_t*)p_out_buffer = 0;
751 i_string_length = ( i_out_bytes - i_out_bytes_left ) / sizeof(uint32_t);
754 #if defined(HAVE_FRIBIDI)
756 uint32_t *p_fribidi_string;
757 FriBidiCharType base_dir = FRIBIDI_TYPE_ON;
758 p_fribidi_string = malloc( ( i_string_length + 1 ) * sizeof(uint32_t) );
759 fribidi_log2vis( p_unicode_string, i_string_length, &base_dir,
760 p_fribidi_string, NULL, NULL, NULL );
761 free( p_unicode_string );
762 p_unicode_string = p_fribidi_string;
763 p_fribidi_string[ i_string_length ] = 0;
768 /* Calculate relative glyph positions and a bounding box for the
770 p_line = NewLine( psz_string );
773 msg_Err( p_vout, "Out of memory" );
776 p_string->p_lines = p_line;
782 #define face p_vout->p_text_renderer_data->p_face
783 #define glyph face->glyph
785 while( *p_unicode_string )
787 i_char = *p_unicode_string++;
788 if ( i_char == '\r' ) /* ignore CR chars wherever they may be */
793 if ( i_char == '\n' )
795 p_next = NewLine( psz_string );
798 msg_Err( p_vout, "Out of memory" );
801 p_line->p_next = p_next;
802 p_line->i_width = line.xMax;
803 p_line->i_height = face->size->metrics.height >> 6;
804 p_line->pp_glyphs[ i ] = NULL;
806 result.x = __MAX( result.x, line.xMax );
807 result.y += face->size->metrics.height >> 6;
813 i_pen_y += face->size->metrics.height >> 6;
814 msg_Dbg( p_vout, "Creating new line, i is %d", i );
819 i_glyph_index = FT_Get_Char_Index( face, i_char );
820 if ( p_vout->p_text_renderer_data->i_use_kerning && i_glyph_index
824 FT_Get_Kerning( face, i_previous, i_glyph_index,
825 ft_kerning_default, &delta );
826 i_pen_x += delta.x >> 6;
829 p_line->p_glyph_pos[ i ].x = i_pen_x;
830 p_line->p_glyph_pos[ i ].y = i_pen_y;
831 i_error = FT_Load_Glyph( face, i_glyph_index, FT_LOAD_DEFAULT );
834 msg_Err( p_vout, "FT_Load_Glyph returned %d", i_error );
837 i_error = FT_Get_Glyph( glyph, &tmp_glyph );
840 msg_Err( p_vout, "FT_Get_Glyph returned %d", i_error );
843 FT_Glyph_Get_CBox( tmp_glyph, ft_glyph_bbox_pixels, &glyph_size );
844 i_error = FT_Glyph_To_Bitmap( &tmp_glyph, ft_render_mode_normal,
846 if ( i_error ) continue;
847 p_line->pp_glyphs[ i ] = (FT_BitmapGlyph)tmp_glyph;
850 line.xMax = p_line->p_glyph_pos[i].x + glyph_size.xMax - glyph_size.xMin;
851 line.yMax = __MAX( line.yMax, glyph_size.yMax );
852 line.yMin = __MIN( line.yMin, glyph_size.yMin );
854 i_previous = i_glyph_index;
855 i_pen_x += glyph->advance.x >> 6;
858 p_line->i_width = line.xMax;
859 p_line->i_height = face->size->metrics.height >> 6;
860 p_line->pp_glyphs[ i ] = NULL;
861 result.x = __MAX( result.x, line.xMax );
862 result.y += line.yMax - line.yMin;
863 p_string->i_height = result.y;
864 p_string->i_width = result.x;
865 vout_DisplaySubPicture( p_vout, p_subpic );
872 FreeString( p_subpic );
873 vout_DestroySubPicture( p_vout, p_subpic );
877 static void FreeString( subpicture_t *p_subpic )
880 subpicture_sys_t *p_string = p_subpic->p_sys;
881 line_desc_t *p_line, *p_next;
883 if( p_subpic->p_sys == NULL ) return;
885 for( p_line = p_string->p_lines; p_line != NULL; p_line = p_next )
887 p_next = p_line->p_next;
888 for( i = 0; p_line->pp_glyphs[ i ] != NULL; i++ )
890 FT_Done_Glyph( (FT_Glyph)p_line->pp_glyphs[ i ] );
892 free( p_line->pp_glyphs );
893 free( p_line->p_glyph_pos );
897 free( p_string->psz_text );
901 /* convert one or more utf8 bytes into a unicode character */
902 static int GetUnicodeCharFromUTF8( byte_t **ppsz_utf8_string )
904 int i_remaining_bytes, i_char = 0;
905 if( ( **ppsz_utf8_string & 0xFC ) == 0xFC )
907 i_char = **ppsz_utf8_string & 1;
908 i_remaining_bytes = 5;
910 else if( ( **ppsz_utf8_string & 0xF8 ) == 0xF8 )
912 i_char = **ppsz_utf8_string & 3;
913 i_remaining_bytes = 4;
915 else if( ( **ppsz_utf8_string & 0xF0 ) == 0xF0 )
917 i_char = **ppsz_utf8_string & 7;
918 i_remaining_bytes = 3;
920 else if( ( **ppsz_utf8_string & 0xE0 ) == 0xE0 )
922 i_char = **ppsz_utf8_string & 15;
923 i_remaining_bytes = 2;
925 else if( ( **ppsz_utf8_string & 0xC0 ) == 0xC0 )
927 i_char = **ppsz_utf8_string & 31;
928 i_remaining_bytes = 1;
932 i_char = **ppsz_utf8_string;
933 i_remaining_bytes = 0;
935 while( i_remaining_bytes )
937 (*ppsz_utf8_string)++;
939 i_char = ( i_char << 6 ) + ( **ppsz_utf8_string & 0x3F );
941 (*ppsz_utf8_string)++;
945 static line_desc_t *NewLine( byte_t *psz_string )
948 line_desc_t *p_line = malloc( sizeof(line_desc_t) );
953 p_line->i_height = 0;
955 p_line->p_next = NULL;
957 /* We don't use CountUtf8Characters() here because we are not acutally
958 * sure the string is utf8. Better be safe than sorry. */
959 i_count = strlen( psz_string );
961 p_line->pp_glyphs = malloc( sizeof(FT_BitmapGlyph)
963 if( p_line->pp_glyphs == NULL )
968 p_line->p_glyph_pos = malloc( sizeof( FT_Vector )
970 if( p_line->p_glyph_pos == NULL )
972 free( p_line->pp_glyphs );