1 /*****************************************************************************
2 * freetype.c : Put text on the video, using freetype2
3 *****************************************************************************
4 * Copyright (C) 2002, 2003 VideoLAN
5 * $Id: freetype.c,v 1.38 2003/12/08 17:48:13 yoann 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() */
30 #ifdef HAVE_LINUX_LIMITS_H
31 # include <linux/limits.h>
44 #include FT_FREETYPE_H
48 #define DEFAULT_FONT "/System/Library/Fonts/LucidaGrande.dfont"
49 #elif defined( SYS_BEOS )
50 #define DEFAULT_FONT "/boot/beos/etc/fonts/ttfonts/Swiss721.ttf"
51 #elif defined( WIN32 )
52 #define DEFAULT_FONT "" /* Default font found at run-time */
54 #define DEFAULT_FONT "/usr/share/fonts/truetype/freefont/FreeSerifBold.ttf"
57 #if defined(HAVE_ICONV)
60 #if defined(HAVE_FRIBIDI)
61 #include <fribidi/fribidi.h>
64 typedef struct line_desc_t line_desc_t;
66 /*****************************************************************************
68 *****************************************************************************/
69 static int Create ( vlc_object_t * );
70 static void Destroy ( vlc_object_t * );
72 static void Render ( vout_thread_t *, picture_t *,
73 const subpicture_t * );
74 static void RenderI420( vout_thread_t *, picture_t *,
75 const subpicture_t * );
76 static void RenderYUY2( vout_thread_t *, picture_t *,
77 const subpicture_t * );
78 static void RenderRV32( vout_thread_t *, picture_t *,
79 const subpicture_t * );
80 static subpicture_t *AddText ( vout_thread_t *, char *, text_style_t *, int,
81 int, int, mtime_t, mtime_t );
83 static void FreeString( subpicture_t * );
85 #if !defined(HAVE_ICONV)
86 static int GetUnicodeCharFromUTF8( byte_t ** );
89 static line_desc_t *NewLine( byte_t * );
91 /*****************************************************************************
93 *****************************************************************************/
94 #define FONT_TEXT N_("Font")
95 #define FONT_LONGTEXT N_("Filename of Font")
96 #define FONTSIZE_TEXT N_("Font size in pixels")
97 #define FONTSIZE_LONGTEXT N_("The size of the fonts used by the osd module. If \
98 set to something different than 0 this option will override the relative font size " )
99 #define FONTSIZER_TEXT N_("Font size")
100 #define FONTSIZER_LONGTEXT N_("The size of the fonts used by the osd module" )
102 static int pi_sizes[] = { 20, 18, 16, 12, 6 };
103 static char *ppsz_sizes_text[] = { N_("Smaller"), N_("Small"), N_("Normal"),
104 N_("Large"), N_("Larger") };
107 add_category_hint( N_("Fonts"), NULL, VLC_FALSE );
108 add_file( "freetype-font", DEFAULT_FONT, NULL, FONT_TEXT, FONT_LONGTEXT, VLC_FALSE );
109 add_integer( "freetype-fontsize", 0, NULL, FONTSIZE_TEXT, FONTSIZE_LONGTEXT, VLC_TRUE );
110 add_integer( "freetype-rel-fontsize", 16, NULL, FONTSIZER_TEXT, FONTSIZER_LONGTEXT,
112 change_integer_list( pi_sizes, ppsz_sizes_text, 0 );
113 set_description( _("freetype2 font renderer") );
114 set_capability( "text renderer", 100 );
115 add_shortcut( "text" );
116 set_callbacks( Create, Destroy );
120 * Private data in a subpicture. Describes a string.
122 struct subpicture_sys_t
129 /** The string associated with this subpicture */
131 line_desc_t *p_lines;
136 /** NULL-terminated list of glyphs making the string */
137 FT_BitmapGlyph *pp_glyphs;
138 /** list of relative positions for the glyphs */
139 FT_Vector *p_glyph_pos;
145 /*****************************************************************************
146 * text_renderer_sys_t: freetype local data
147 *****************************************************************************
148 * This structure is part of the video output thread descriptor.
149 * It describes the freetype specific properties of an output thread.
150 *****************************************************************************/
151 struct text_renderer_sys_t
153 FT_Library p_library; /* handle to library */
154 FT_Face p_face; /* handle to face object */
156 vlc_bool_t i_use_kerning;
157 uint8_t pi_gamma[256];
160 /*****************************************************************************
161 * Create: allocates osd-text video thread output method
162 *****************************************************************************
163 * This function allocates and initializes a Clone vout method.
164 *****************************************************************************/
165 #define gamma_value 2.0
166 static int Create( vlc_object_t *p_this )
168 vout_thread_t *p_vout = (vout_thread_t *)p_this;
172 double gamma_inv = 1.0f / gamma_value;
175 /* Allocate structure */
176 p_vout->p_text_renderer_data = malloc( sizeof( text_renderer_sys_t ) );
177 if( p_vout->p_text_renderer_data == NULL )
179 msg_Err( p_vout, "out of memory" );
183 for (i = 0; i < 256; i++) {
184 p_vout->p_text_renderer_data->pi_gamma[i] =
185 (uint8_t)( pow( (double)i / 255.0f, gamma_inv) * 255.0f );
188 var_Create( p_vout, "freetype-font", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
189 var_Create( p_vout, "freetype-fontsize",
190 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
191 var_Create( p_vout, "freetype-rel-fontsize",
192 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
194 /* Look what method was requested */
195 var_Get( p_vout, "freetype-font", &val );
196 psz_fontfile = val.psz_string;
198 if( !psz_fontfile || !*psz_fontfile )
200 if( psz_fontfile ) free( psz_fontfile );
201 psz_fontfile = (char *)malloc( PATH_MAX + 1 );
203 GetWindowsDirectory( psz_fontfile, PATH_MAX + 1 );
204 strcat( psz_fontfile, "\\fonts\\arial.ttf" );
206 strcpy( psz_fontfile, DEFAULT_FONT );
208 msg_Err( p_vout, "user didn't specify a font" );
209 free( p_vout->p_text_renderer_data );
214 i_error = FT_Init_FreeType( &p_vout->p_text_renderer_data->p_library );
217 msg_Err( p_vout, "couldn't initialize freetype" );
218 free( p_vout->p_text_renderer_data );
222 i_error = FT_New_Face( p_vout->p_text_renderer_data->p_library,
223 psz_fontfile ? psz_fontfile : "", 0,
224 &p_vout->p_text_renderer_data->p_face );
225 if( i_error == FT_Err_Unknown_File_Format )
227 msg_Err( p_vout, "file %s have unknown format", psz_fontfile );
228 FT_Done_FreeType( p_vout->p_text_renderer_data->p_library );
229 free( p_vout->p_text_renderer_data );
230 if( psz_fontfile ) free( psz_fontfile );
235 msg_Err( p_vout, "failed to load font file %s", psz_fontfile );
236 FT_Done_FreeType( p_vout->p_text_renderer_data->p_library );
237 free( p_vout->p_text_renderer_data );
238 if( psz_fontfile ) free( psz_fontfile );
241 if( psz_fontfile ) free( psz_fontfile );
243 i_error = FT_Select_Charmap( p_vout->p_text_renderer_data->p_face,
244 ft_encoding_unicode );
247 msg_Err( p_vout, "Font has no unicode translation table" );
248 FT_Done_Face( p_vout->p_text_renderer_data->p_face );
249 FT_Done_FreeType( p_vout->p_text_renderer_data->p_library );
250 free( p_vout->p_text_renderer_data );
254 p_vout->p_text_renderer_data->i_use_kerning =
255 FT_HAS_KERNING(p_vout->p_text_renderer_data->p_face);
257 var_Get( p_vout, "freetype-fontsize", &val );
261 i_fontsize = val.i_int;
265 var_Get( p_vout, "freetype-rel-fontsize", &val );
266 i_fontsize = (int) p_vout->render.i_height / val.i_int;
268 msg_Dbg( p_vout, "Using fontsize: %i", i_fontsize);
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_pic_pitch = p_pic->p[ i_plane ].i_pitch;
350 int i_pic_width = p_pic->p[ i_plane ].i_visible_pitch;
352 p_in = p_pic->p[ i_plane ].p_pixels;
356 if ( p_string->i_flags & OSD_ALIGN_BOTTOM )
358 pen_y = p_pic->p[ i_plane ].i_lines - p_string->i_height -
359 p_string->i_y_margin;
363 pen_y = p_string->i_y_margin;
365 pen_y += p_vout->p_text_renderer_data->p_face->size->metrics.ascender >> 6;
366 if ( p_string->i_flags & OSD_ALIGN_RIGHT )
368 pen_x = i_pic_width - p_line->i_width
369 - p_string->i_x_margin;
371 else if ( p_string->i_flags & OSD_ALIGN_LEFT )
373 pen_x = p_string->i_x_margin;
377 pen_x = i_pic_width / 2 - p_line->i_width / 2
378 + p_string->i_x_margin;
381 for( i = 0; p_line->pp_glyphs[i] != NULL; i++ )
383 FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ];
384 #define alpha p_vout->p_text_renderer_data->pi_gamma[ p_glyph->bitmap.buffer[ x + y * p_glyph->bitmap.width ] ]
385 #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 ]
386 for(y = 0; y < p_glyph->bitmap.rows; y++ )
388 for( x = 0; x < p_glyph->bitmap.width; x++ )
391 pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 );
393 pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 );
395 pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 );
397 pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 );
401 for(y = 0; y < p_glyph->bitmap.rows; y++ )
403 for( x = 0; x < p_glyph->bitmap.width; x++ )
405 pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 ) +
406 ( 255 * alpha >> 8 );
415 if ( p_string->i_flags & OSD_ALIGN_BOTTOM )
417 pen_y = p_pic->p[i_plane].i_lines - ( p_string->i_height>>1) -
418 (p_string->i_y_margin>>1);
422 pen_y = p_string->i_y_margin >> 1;
424 pen_y += p_vout->p_text_renderer_data->p_face->size->metrics.ascender >> 7;
425 if ( p_string->i_flags & OSD_ALIGN_RIGHT )
427 pen_x = i_pic_width - ( p_line->i_width >> 1 )
428 - ( p_string->i_x_margin >> 1 );
430 else if ( p_string->i_flags & OSD_ALIGN_LEFT )
432 pen_x = p_string->i_x_margin >> 1;
436 pen_x = i_pic_width / 2 - p_line->i_width / 4
437 + p_string->i_x_margin / 2;
440 for( i = 0; p_line->pp_glyphs[i] != NULL; i++ )
442 FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ];
443 #define alpha p_vout->p_text_renderer_data->pi_gamma[ p_glyph->bitmap.buffer[ ( x + y * p_glyph->bitmap.width ) ] ]
444 #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) ]
445 for( y = 0; y < p_glyph->bitmap.rows; y+=2 )
447 for( x = 0; x < p_glyph->bitmap.width; x+=2 )
449 pixel = ( ( pixel * ( 0xFF - alpha ) ) >> 8 ) +
450 ( 0x80 * alpha >> 8 );
462 * Draw a string on a YUY2 picture
464 static void RenderYUY2( vout_thread_t *p_vout, picture_t *p_pic,
465 const subpicture_t *p_subpic )
467 subpicture_sys_t *p_string = p_subpic->p_sys;
468 int x, y, pen_x, pen_y;
472 for( p_line = p_subpic->p_sys->p_lines; p_line != NULL;
473 p_line = p_line->p_next )
476 int i_pic_pitch = p_pic->p[0].i_pitch;
477 int i_pic_width = p_pic->p[0].i_visible_pitch;
479 p_in = p_pic->p[0].p_pixels;
481 if ( p_string->i_flags & OSD_ALIGN_BOTTOM )
483 pen_y = p_pic->p[0].i_lines - p_string->i_height -
484 p_string->i_y_margin;
488 pen_y = p_string->i_y_margin;
490 pen_y += p_vout->p_text_renderer_data->p_face->size->metrics.ascender >> 6;
491 if ( p_string->i_flags & OSD_ALIGN_RIGHT )
493 pen_x = i_pic_width - p_line->i_width
494 - p_string->i_x_margin;
496 else if ( p_string->i_flags & OSD_ALIGN_LEFT )
498 pen_x = p_string->i_x_margin;
502 pen_x = i_pic_width / 2 /2 - p_line->i_width / 2 + p_string->i_x_margin;
505 for( i = 0; p_line->pp_glyphs[i] != NULL; i++ )
507 FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ];
508 #define alpha p_vout->p_text_renderer_data->pi_gamma[ p_glyph->bitmap.buffer[ x + y * p_glyph->bitmap.width ] ]
509 #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 ) ]
510 for(y = 0; y < p_glyph->bitmap.rows; y++ )
512 for( x = 0; x < p_glyph->bitmap.width; x++ )
515 pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 );
517 pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 );
519 pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 );
521 pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 );
525 for(y = 0; y < p_glyph->bitmap.rows; y++ )
527 for( x = 0; x < p_glyph->bitmap.width; x++ )
529 pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 ) +
530 ( 255 * alpha >> 8 );
540 * Draw a string on a RV32 picture
542 static void RenderRV32( vout_thread_t *p_vout, picture_t *p_pic,
543 const subpicture_t *p_subpic )
545 subpicture_sys_t *p_string = p_subpic->p_sys;
546 int i_plane, x, y, pen_x, pen_y;
552 for( p_line = p_subpic->p_sys->p_lines; p_line != NULL; p_line = p_line->p_next )
555 int i_pic_pitch = p_pic->p[ i_plane ].i_pitch;
556 int i_pic_width = p_pic->p[ i_plane ].i_visible_pitch;
558 p_in = p_pic->p[ i_plane ].p_pixels;
560 if ( p_string->i_flags & OSD_ALIGN_BOTTOM )
562 pen_y = p_pic->p[ i_plane ].i_lines - p_string->i_height -
563 p_string->i_y_margin;
567 pen_y = p_string->i_y_margin;
569 pen_y += p_vout->p_text_renderer_data->p_face->size->metrics.ascender >> 6;
570 if ( p_string->i_flags & OSD_ALIGN_RIGHT )
572 pen_x = i_pic_width - p_line->i_width
573 - p_string->i_x_margin;
575 else if ( p_string->i_flags & OSD_ALIGN_LEFT )
577 pen_x = p_string->i_x_margin;
581 pen_x = i_pic_width / 2 / 4 - p_line->i_width / 2
582 + p_string->i_x_margin;
585 for( i = 0; p_line->pp_glyphs[i] != NULL; i++ )
587 FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ];
588 #define alpha p_vout->p_text_renderer_data->pi_gamma[ p_glyph->bitmap.buffer[ x + y * p_glyph->bitmap.width ] ]
589 #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 ]
590 for(y = 0; y < p_glyph->bitmap.rows; y++ )
592 for( x = 0; x < p_glyph->bitmap.width; x++ )
595 pixel( 0 ) = ( ( pixel( 0 ) * ( 255 - alpha ) ) >> 8 );
596 pixel( 1 ) = ( ( pixel( 1 ) * ( 255 - alpha ) ) >> 8 );
597 pixel( 2 ) = ( ( pixel( 2 ) * ( 255 - alpha ) ) >> 8 );
599 pixel( 0 ) = ( ( pixel( 0 ) * ( 255 - alpha ) ) >> 8 );
600 pixel( 1 ) = ( ( pixel( 1 ) * ( 255 - alpha ) ) >> 8 );
601 pixel( 2 ) = ( ( pixel( 2 ) * ( 255 - alpha ) ) >> 8 );
603 pixel( 0 ) = ( ( pixel( 0 ) * ( 255 - alpha ) ) >> 8 );
604 pixel( 1 ) = ( ( pixel( 1 ) * ( 255 - alpha ) ) >> 8 );
605 pixel( 2 ) = ( ( pixel( 2 ) * ( 255 - alpha ) ) >> 8 );
607 pixel( 0 ) = ( ( pixel( 0 ) * ( 255 - alpha ) ) >> 8 );
608 pixel( 1 ) = ( ( pixel( 1 ) * ( 255 - alpha ) ) >> 8 );
609 pixel( 2 ) = ( ( pixel( 2 ) * ( 255 - alpha ) ) >> 8 );
613 for(y = 0; y < p_glyph->bitmap.rows; y++ )
615 for( x = 0; x < p_glyph->bitmap.width; x++ )
617 pixel( 0 ) = ( ( pixel( 0 ) * ( 255 - alpha ) ) >> 8 ) +
618 ( 255 * alpha >> 8 );
619 pixel( 1 ) = ( ( pixel( 1 ) * ( 255 - alpha ) ) >> 8 ) +
620 ( 255 * alpha >> 8 );
621 pixel( 2 ) = ( ( pixel( 2 ) * ( 255 - alpha ) ) >> 8 ) +
622 ( 255 * alpha >> 8 );
632 * This function receives a string and creates a subpicture for it. It
633 * also calculates the size needed for this string, and renders the
634 * needed glyphs into memory. It is used as pf_add_string callback in
635 * the vout method by this module
637 static subpicture_t *AddText ( vout_thread_t *p_vout, char *psz_string,
638 text_style_t *p_style, int i_flags, int i_hmargin,
639 int i_vmargin, mtime_t i_start, mtime_t i_stop )
641 subpicture_sys_t *p_string;
642 int i, i_pen_y, i_pen_x, i_error, i_glyph_index, i_previous;
643 subpicture_t *p_subpic;
644 line_desc_t *p_line, *p_next;
645 uint32_t *p_unicode_string, i_char;
647 iconv_t iconv_handle;
655 if ( !psz_string || !*psz_string )
671 /* Create and initialize a subpicture */
672 p_subpic = vout_CreateSubPicture( p_vout, MEMORY_SUBPICTURE );
673 if ( p_subpic == NULL )
678 p_subpic->pf_render = Render;
679 p_subpic->pf_destroy = FreeString;
680 p_subpic->i_start = i_start;
681 p_subpic->i_stop = i_stop;
684 p_subpic->b_ephemer = VLC_TRUE;
688 p_subpic->b_ephemer = VLC_FALSE;
691 /* Create and initialize private data for the subpicture */
692 p_string = malloc( sizeof(subpicture_sys_t) );
693 if ( p_string == NULL )
695 msg_Err( p_vout, "Out of memory" );
698 p_subpic->p_sys = p_string;
699 p_string->i_flags = i_flags;
700 p_string->i_x_margin = i_hmargin;
701 p_string->i_y_margin = i_vmargin;
702 p_string->p_lines = 0;
703 p_string->psz_text = strdup( psz_string );
705 #if defined(HAVE_ICONV)
706 p_unicode_string = malloc( ( strlen(psz_string) + 1 ) * sizeof(uint32_t) );
707 if( p_unicode_string == NULL )
709 msg_Err( p_vout, "Out of memory" );
712 #if defined(WORDS_BIGENDIAN)
713 iconv_handle = iconv_open( "UCS-4BE", "UTF-8" );
715 iconv_handle = iconv_open( "UCS-4LE", "UTF-8" );
717 if( iconv_handle == (iconv_t)-1 )
719 msg_Warn( p_vout, "Unable to do convertion" );
724 char *p_in_buffer, *p_out_buffer;
725 size_t i_in_bytes, i_out_bytes, i_out_bytes_left, i_ret;
726 i_in_bytes = strlen( psz_string );
727 i_out_bytes = i_in_bytes * sizeof( uint32_t );
728 i_out_bytes_left = i_out_bytes;
729 p_in_buffer = psz_string;
730 p_out_buffer = (char *)p_unicode_string;
731 i_ret = iconv( iconv_handle, &p_in_buffer, &i_in_bytes, &p_out_buffer, &i_out_bytes_left );
734 msg_Warn( p_vout, "Failed to convert string to unicode (%s), bytes left %d", strerror(errno), i_in_bytes );
737 *(uint32_t*)p_out_buffer = 0;
738 i_string_length = ( i_out_bytes - i_out_bytes_left ) / sizeof(uint32_t);
741 #if defined(HAVE_FRIBIDI)
743 uint32_t *p_fribidi_string;
744 FriBidiCharType base_dir = FRIBIDI_TYPE_ON;
745 p_fribidi_string = malloc( ( i_string_length + 1 ) * sizeof(uint32_t) );
746 fribidi_log2vis( (FriBidiChar*)p_unicode_string, i_string_length,
747 &base_dir, (FriBidiChar*)p_fribidi_string, NULL, NULL,
749 free( p_unicode_string );
750 p_unicode_string = p_fribidi_string;
751 p_fribidi_string[ i_string_length ] = 0;
756 /* Calculate relative glyph positions and a bounding box for the
758 p_line = NewLine( psz_string );
761 msg_Err( p_vout, "Out of memory" );
764 p_string->p_lines = p_line;
770 #define face p_vout->p_text_renderer_data->p_face
771 #define glyph face->glyph
773 while( *p_unicode_string )
775 i_char = *p_unicode_string++;
776 if ( i_char == '\r' ) /* ignore CR chars wherever they may be */
781 if ( i_char == '\n' )
783 p_next = NewLine( psz_string );
786 msg_Err( p_vout, "Out of memory" );
789 p_line->p_next = p_next;
790 p_line->i_width = line.xMax;
791 p_line->i_height = face->size->metrics.height >> 6;
792 p_line->pp_glyphs[ i ] = NULL;
794 result.x = __MAX( result.x, line.xMax );
795 result.y += face->size->metrics.height >> 6;
801 i_pen_y += face->size->metrics.height >> 6;
802 msg_Dbg( p_vout, "Creating new line, i is %d", i );
807 i_glyph_index = FT_Get_Char_Index( face, i_char );
808 if ( p_vout->p_text_renderer_data->i_use_kerning && i_glyph_index
812 FT_Get_Kerning( face, i_previous, i_glyph_index,
813 ft_kerning_default, &delta );
814 i_pen_x += delta.x >> 6;
817 p_line->p_glyph_pos[ i ].x = i_pen_x;
818 p_line->p_glyph_pos[ i ].y = i_pen_y;
819 i_error = FT_Load_Glyph( face, i_glyph_index, FT_LOAD_DEFAULT );
822 msg_Err( p_vout, "FT_Load_Glyph returned %d", i_error );
825 i_error = FT_Get_Glyph( glyph, &tmp_glyph );
828 msg_Err( p_vout, "FT_Get_Glyph returned %d", i_error );
831 FT_Glyph_Get_CBox( tmp_glyph, ft_glyph_bbox_pixels, &glyph_size );
832 i_error = FT_Glyph_To_Bitmap( &tmp_glyph, ft_render_mode_normal,
834 if ( i_error ) continue;
835 p_line->pp_glyphs[ i ] = (FT_BitmapGlyph)tmp_glyph;
838 line.xMax = p_line->p_glyph_pos[i].x + glyph_size.xMax - glyph_size.xMin;
839 line.yMax = __MAX( line.yMax, glyph_size.yMax );
840 line.yMin = __MIN( line.yMin, glyph_size.yMin );
842 i_previous = i_glyph_index;
843 i_pen_x += glyph->advance.x >> 6;
846 p_line->i_width = line.xMax;
847 p_line->i_height = face->size->metrics.height >> 6;
848 p_line->pp_glyphs[ i ] = NULL;
849 result.x = __MAX( result.x, line.xMax );
850 result.y += line.yMax - line.yMin;
851 p_string->i_height = result.y;
852 p_string->i_width = result.x;
853 vout_DisplaySubPicture( p_vout, p_subpic );
860 FreeString( p_subpic );
861 vout_DestroySubPicture( p_vout, p_subpic );
865 static void FreeString( subpicture_t *p_subpic )
868 subpicture_sys_t *p_string = p_subpic->p_sys;
869 line_desc_t *p_line, *p_next;
871 if( p_subpic->p_sys == NULL ) return;
873 for( p_line = p_string->p_lines; p_line != NULL; p_line = p_next )
875 p_next = p_line->p_next;
876 for( i = 0; p_line->pp_glyphs[ i ] != NULL; i++ )
878 FT_Done_Glyph( (FT_Glyph)p_line->pp_glyphs[ i ] );
880 free( p_line->pp_glyphs );
881 free( p_line->p_glyph_pos );
885 free( p_string->psz_text );
889 #if !defined( HAVE_ICONV )
890 /* convert one or more utf8 bytes into a unicode character */
891 static int GetUnicodeCharFromUTF8( byte_t **ppsz_utf8_string )
893 int i_remaining_bytes, i_char = 0;
894 if( ( **ppsz_utf8_string & 0xFC ) == 0xFC )
896 i_char = **ppsz_utf8_string & 1;
897 i_remaining_bytes = 5;
899 else if( ( **ppsz_utf8_string & 0xF8 ) == 0xF8 )
901 i_char = **ppsz_utf8_string & 3;
902 i_remaining_bytes = 4;
904 else if( ( **ppsz_utf8_string & 0xF0 ) == 0xF0 )
906 i_char = **ppsz_utf8_string & 7;
907 i_remaining_bytes = 3;
909 else if( ( **ppsz_utf8_string & 0xE0 ) == 0xE0 )
911 i_char = **ppsz_utf8_string & 15;
912 i_remaining_bytes = 2;
914 else if( ( **ppsz_utf8_string & 0xC0 ) == 0xC0 )
916 i_char = **ppsz_utf8_string & 31;
917 i_remaining_bytes = 1;
921 i_char = **ppsz_utf8_string;
922 i_remaining_bytes = 0;
924 while( i_remaining_bytes )
926 (*ppsz_utf8_string)++;
928 i_char = ( i_char << 6 ) + ( **ppsz_utf8_string & 0x3F );
930 (*ppsz_utf8_string)++;
935 static line_desc_t *NewLine( byte_t *psz_string )
938 line_desc_t *p_line = malloc( sizeof(line_desc_t) );
943 p_line->i_height = 0;
945 p_line->p_next = NULL;
947 /* We don't use CountUtf8Characters() here because we are not acutally
948 * sure the string is utf8. Better be safe than sorry. */
949 i_count = strlen( psz_string );
951 p_line->pp_glyphs = malloc( sizeof(FT_BitmapGlyph)
953 if( p_line->pp_glyphs == NULL )
958 p_line->p_glyph_pos = malloc( sizeof( FT_Vector )
960 if( p_line->p_glyph_pos == NULL )
962 free( p_line->pp_glyphs );