1 /*****************************************************************************
2 * freetype.c : Put text on the video, using freetype2
3 *****************************************************************************
4 * Copyright (C) 2002, 2003 VideoLAN
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 *, int, char *, text_style_t *,
81 int, 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_("Font filename")
96 #define FONTSIZE_TEXT N_("Font size in pixels")
97 #define FONTSIZE_LONGTEXT N_("The size of the fonts used by the osd module. " \
98 "If set to something different than 0 this option will override the " \
99 "relative font size " )
100 #define FONTSIZER_TEXT N_("Font size")
101 #define FONTSIZER_LONGTEXT N_("The size of the fonts used by the osd module" )
103 static int pi_sizes[] = { 20, 18, 16, 12, 6 };
104 static char *ppsz_sizes_text[] = { N_("Smaller"), N_("Small"), N_("Normal"),
105 N_("Large"), N_("Larger") };
108 set_description( _("freetype2 font renderer") );
110 add_file( "freetype-font", DEFAULT_FONT, NULL, FONT_TEXT, FONT_LONGTEXT,
112 add_integer( "freetype-fontsize", 0, NULL, FONTSIZE_TEXT,
113 FONTSIZE_LONGTEXT, VLC_TRUE );
114 add_integer( "freetype-rel-fontsize", 16, NULL, FONTSIZER_TEXT,
115 FONTSIZER_LONGTEXT, VLC_FALSE );
116 change_integer_list( pi_sizes, ppsz_sizes_text, 0 );
118 set_capability( "text renderer", 100 );
119 add_shortcut( "text" );
120 set_callbacks( Create, Destroy );
124 * Private data in a subpicture. Describes a string.
126 struct subpicture_sys_t
133 /** The string associated with this subpicture */
135 line_desc_t *p_lines;
140 /** NULL-terminated list of glyphs making the string */
141 FT_BitmapGlyph *pp_glyphs;
142 /** list of relative positions for the glyphs */
143 FT_Vector *p_glyph_pos;
149 /*****************************************************************************
150 * text_renderer_sys_t: freetype local data
151 *****************************************************************************
152 * This structure is part of the video output thread descriptor.
153 * It describes the freetype specific properties of an output thread.
154 *****************************************************************************/
155 struct text_renderer_sys_t
157 FT_Library p_library; /* handle to library */
158 FT_Face p_face; /* handle to face object */
160 vlc_bool_t i_use_kerning;
161 uint8_t pi_gamma[256];
164 /*****************************************************************************
165 * Create: allocates osd-text video thread output method
166 *****************************************************************************
167 * This function allocates and initializes a Clone vout method.
168 *****************************************************************************/
169 #define gamma_value 2.0
170 static int Create( vlc_object_t *p_this )
172 vout_thread_t *p_vout = (vout_thread_t *)p_this;
176 double gamma_inv = 1.0f / gamma_value;
179 /* Allocate structure */
180 p_vout->p_text_renderer_data = malloc( sizeof( text_renderer_sys_t ) );
181 if( p_vout->p_text_renderer_data == NULL )
183 msg_Err( p_vout, "out of memory" );
187 for (i = 0; i < 256; i++) {
188 p_vout->p_text_renderer_data->pi_gamma[i] =
189 (uint8_t)( pow( (double)i / 255.0f, gamma_inv) * 255.0f );
192 var_Create( p_vout, "freetype-font", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
193 var_Create( p_vout, "freetype-fontsize",
194 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
195 var_Create( p_vout, "freetype-rel-fontsize",
196 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
198 /* Look what method was requested */
199 var_Get( p_vout, "freetype-font", &val );
200 psz_fontfile = val.psz_string;
202 if( !psz_fontfile || !*psz_fontfile )
204 if( psz_fontfile ) free( psz_fontfile );
205 psz_fontfile = (char *)malloc( PATH_MAX + 1 );
207 GetWindowsDirectory( psz_fontfile, PATH_MAX + 1 );
208 strcat( psz_fontfile, "\\fonts\\arial.ttf" );
210 strcpy( psz_fontfile, DEFAULT_FONT );
212 msg_Err( p_vout, "user didn't specify a font" );
213 free( p_vout->p_text_renderer_data );
218 i_error = FT_Init_FreeType( &p_vout->p_text_renderer_data->p_library );
221 msg_Err( p_vout, "couldn't initialize freetype" );
222 free( p_vout->p_text_renderer_data );
226 i_error = FT_New_Face( p_vout->p_text_renderer_data->p_library,
227 psz_fontfile ? psz_fontfile : "", 0,
228 &p_vout->p_text_renderer_data->p_face );
229 if( i_error == FT_Err_Unknown_File_Format )
231 msg_Err( p_vout, "file %s have unknown format", psz_fontfile );
232 FT_Done_FreeType( p_vout->p_text_renderer_data->p_library );
233 free( p_vout->p_text_renderer_data );
234 if( psz_fontfile ) free( psz_fontfile );
239 msg_Err( p_vout, "failed to load font file %s", psz_fontfile );
240 FT_Done_FreeType( p_vout->p_text_renderer_data->p_library );
241 free( p_vout->p_text_renderer_data );
242 if( psz_fontfile ) free( psz_fontfile );
245 if( psz_fontfile ) free( psz_fontfile );
247 i_error = FT_Select_Charmap( p_vout->p_text_renderer_data->p_face,
248 ft_encoding_unicode );
251 msg_Err( p_vout, "Font has no unicode translation table" );
252 FT_Done_Face( p_vout->p_text_renderer_data->p_face );
253 FT_Done_FreeType( p_vout->p_text_renderer_data->p_library );
254 free( p_vout->p_text_renderer_data );
258 p_vout->p_text_renderer_data->i_use_kerning =
259 FT_HAS_KERNING(p_vout->p_text_renderer_data->p_face);
261 var_Get( p_vout, "freetype-fontsize", &val );
265 i_fontsize = val.i_int;
269 var_Get( p_vout, "freetype-rel-fontsize", &val );
270 i_fontsize = (int) p_vout->render.i_height / val.i_int;
272 msg_Dbg( p_vout, "Using fontsize: %i", i_fontsize);
274 i_error = FT_Set_Pixel_Sizes( p_vout->p_text_renderer_data->p_face, 0,
278 msg_Err( p_vout, "couldn't set font size to %d", i_fontsize );
279 free( p_vout->p_text_renderer_data );
282 p_vout->pf_add_string = AddText;
286 /*****************************************************************************
287 * Destroy: destroy Clone video thread output method
288 *****************************************************************************
289 * Clean up all data and library connections
290 *****************************************************************************/
291 static void Destroy( vlc_object_t *p_this )
293 vout_thread_t *p_vout = (vout_thread_t *)p_this;
294 FT_Done_Face( p_vout->p_text_renderer_data->p_face );
295 FT_Done_FreeType( p_vout->p_text_renderer_data->p_library );
296 free( p_vout->p_text_renderer_data );
299 /*****************************************************************************
300 * Render: place string in picture
301 *****************************************************************************
302 * This function merges the previously rendered freetype glyphs into a picture
303 *****************************************************************************/
304 static void Render( vout_thread_t *p_vout, picture_t *p_pic,
305 const subpicture_t *p_subpic )
307 switch( p_vout->output.i_chroma )
309 /* I420 target, no scaling */
310 case VLC_FOURCC('I','4','2','0'):
311 case VLC_FOURCC('I','Y','U','V'):
312 case VLC_FOURCC('Y','V','1','2'):
313 RenderI420( p_vout, p_pic, p_subpic );
316 /* RV16 target, scaling */
317 case VLC_FOURCC('R','V','1','6'):
318 RenderRV16( p_vout, p_pic, p_subpic );
321 /* RV32 target, scaling */
322 case VLC_FOURCC('R','V','2','4'):
323 case VLC_FOURCC('R','V','3','2'):
324 RenderRV32( p_vout, p_pic, p_subpic );
326 /* NVidia or BeOS overlay, no scaling */
327 case VLC_FOURCC('Y','U','Y','2'):
328 RenderYUY2( p_vout, p_pic, p_subpic );
332 msg_Err( p_vout, "unknown chroma, can't render SPU" );
338 * Draw a string on a i420 (or similar) picture
340 static void RenderI420( vout_thread_t *p_vout, picture_t *p_pic,
341 const subpicture_t *p_subpic )
343 subpicture_sys_t *p_string = p_subpic->p_sys;
344 int i_plane, x, y, pen_x, pen_y;
348 for( p_line = p_subpic->p_sys->p_lines; p_line != NULL;
349 p_line = p_line->p_next )
351 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
353 uint8_t *p_in = p_pic->p[ i_plane ].p_pixels;
354 int i_pic_pitch = p_pic->p[ i_plane ].i_pitch;
355 int i_pic_width = p_pic->p[ i_plane ].i_visible_pitch;
359 if( p_string->i_flags & OSD_ALIGN_BOTTOM )
361 pen_y = p_pic->p[ i_plane ].i_lines - p_string->i_height -
362 p_string->i_y_margin;
366 pen_y = p_string->i_y_margin;
368 pen_y += p_vout->p_text_renderer_data->p_face->size->metrics.ascender >> 6;
370 if( p_string->i_flags & OSD_ALIGN_RIGHT )
372 pen_x = i_pic_width - p_line->i_width
373 - p_string->i_x_margin;
375 else if( p_string->i_flags & OSD_ALIGN_LEFT )
377 pen_x = p_string->i_x_margin;
381 pen_x = i_pic_width / 2 - p_line->i_width / 2
382 + p_string->i_x_margin;
385 for( i = 0; p_line->pp_glyphs[i] != NULL; i++ )
387 FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ];
388 int i_alpha_offset = -1;
389 int i_offset = ( p_line->p_glyph_pos[ i ].y + pen_y -
390 p_glyph->top ) * i_pic_pitch +
391 p_line->p_glyph_pos[ i ].x + p_glyph->left;
393 #define alpha p_vout->p_text_renderer_data->pi_gamma[ p_glyph->bitmap.buffer[ i_alpha_offset ] ]
394 #define pixel p_in[ i_offset + x + pen_x ]
396 for( y = 0; y < p_glyph->bitmap.rows; y++ )
398 for( x = 0; x < p_glyph->bitmap.width; x++ )
401 if( alpha == 0 ) continue;
403 pen_y--; i_offset -= i_pic_pitch;
404 pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 );
405 pen_y++; i_offset += i_pic_pitch; pen_x--;
406 pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 );
408 pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 );
409 pen_y++; i_offset += i_pic_pitch; pen_x--;
410 pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 );
411 pen_y--; i_offset -= i_pic_pitch;
413 i_offset += i_pic_pitch;
417 i_offset = ( p_line->p_glyph_pos[ i ].y + pen_y -
418 p_glyph->top ) * i_pic_pitch +
419 p_line->p_glyph_pos[ i ].x + p_glyph->left;
420 for( y = 0; y < p_glyph->bitmap.rows; y++ )
422 for( x = 0; x < p_glyph->bitmap.width; x++ )
425 if( alpha == 0 ) continue;
427 pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 ) +
428 ( 255 * alpha >> 8 );
430 i_offset += i_pic_pitch;
438 if ( p_string->i_flags & OSD_ALIGN_BOTTOM )
440 pen_y = p_pic->p[i_plane].i_lines - (p_string->i_height>>1)
441 - (p_string->i_y_margin>>1);
445 pen_y = p_string->i_y_margin >> 1;
447 pen_y += p_vout->p_text_renderer_data->p_face->size->metrics.ascender >> 7;
448 if ( p_string->i_flags & OSD_ALIGN_RIGHT )
450 pen_x = i_pic_width - ( p_line->i_width >> 1 )
451 - ( p_string->i_x_margin >> 1 );
453 else if ( p_string->i_flags & OSD_ALIGN_LEFT )
455 pen_x = p_string->i_x_margin >> 1;
459 pen_x = i_pic_width / 2 - p_line->i_width / 4
460 + p_string->i_x_margin / 2;
463 for( i = 0; p_line->pp_glyphs[i] != NULL; i++ )
465 FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ];
466 #define alpha p_vout->p_text_renderer_data->pi_gamma[ p_glyph->bitmap.buffer[ ( x + y * p_glyph->bitmap.width ) ] ]
467 #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) ]
468 for( y = 0; y < p_glyph->bitmap.rows; y+=2 )
470 for( x = 0; x < p_glyph->bitmap.width; x+=2 )
472 if( alpha == 0 ) continue;
474 pixel = ( ( pixel * ( 0xFF - alpha ) ) >> 8 ) +
475 ( 0x80 * alpha >> 8 );
487 * Draw a string on a YUY2 picture
489 static void RenderYUY2( vout_thread_t *p_vout, picture_t *p_pic,
490 const subpicture_t *p_subpic )
492 subpicture_sys_t *p_string = p_subpic->p_sys;
493 int x, y, pen_x, pen_y;
497 for( p_line = p_subpic->p_sys->p_lines; p_line != NULL;
498 p_line = p_line->p_next )
501 int i_pic_pitch = p_pic->p[0].i_pitch;
502 int i_pic_width = p_pic->p[0].i_visible_pitch;
504 p_in = p_pic->p[0].p_pixels;
506 if ( p_string->i_flags & OSD_ALIGN_BOTTOM )
508 pen_y = p_pic->p[0].i_lines - p_string->i_height -
509 p_string->i_y_margin;
513 pen_y = p_string->i_y_margin;
515 pen_y += p_vout->p_text_renderer_data->p_face->size->metrics.ascender >> 6;
516 if ( p_string->i_flags & OSD_ALIGN_RIGHT )
518 pen_x = i_pic_width - p_line->i_width
519 - p_string->i_x_margin;
521 else if ( p_string->i_flags & OSD_ALIGN_LEFT )
523 pen_x = p_string->i_x_margin;
527 pen_x = i_pic_width / 2 /2 - p_line->i_width / 2 + p_string->i_x_margin;
530 for( i = 0; p_line->pp_glyphs[i] != NULL; i++ )
532 FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ];
533 #define alpha p_vout->p_text_renderer_data->pi_gamma[ p_glyph->bitmap.buffer[ x + y * p_glyph->bitmap.width ] ]
534 #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 ) ]
535 for(y = 0; y < p_glyph->bitmap.rows; y++ )
537 for( x = 0; x < p_glyph->bitmap.width; x++ )
539 if( alpha == 0 ) continue;
542 pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 );
544 pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 );
546 pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 );
548 pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 );
552 for(y = 0; y < p_glyph->bitmap.rows; y++ )
554 for( x = 0; x < p_glyph->bitmap.width; x++ )
556 if( alpha == 0 ) continue;
558 pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 ) +
559 ( 255 * alpha >> 8 );
569 * Draw a string on a RV32 picture
571 static void RenderRV32( vout_thread_t *p_vout, picture_t *p_pic,
572 const subpicture_t *p_subpic )
574 subpicture_sys_t *p_string = p_subpic->p_sys;
575 int i_plane, x, y, pen_x, pen_y;
581 for( p_line = p_subpic->p_sys->p_lines; p_line != NULL; p_line = p_line->p_next )
584 int i_pic_pitch = p_pic->p[ i_plane ].i_pitch;
585 int i_pic_width = p_pic->p[ i_plane ].i_visible_pitch;
587 p_in = p_pic->p[ i_plane ].p_pixels;
589 if ( p_string->i_flags & OSD_ALIGN_BOTTOM )
591 pen_y = p_pic->p[ i_plane ].i_lines - p_string->i_height -
592 p_string->i_y_margin;
596 pen_y = p_string->i_y_margin;
598 pen_y += p_vout->p_text_renderer_data->p_face->size->metrics.ascender >> 6;
599 if ( p_string->i_flags & OSD_ALIGN_RIGHT )
601 pen_x = i_pic_width - p_line->i_width
602 - p_string->i_x_margin;
604 else if ( p_string->i_flags & OSD_ALIGN_LEFT )
606 pen_x = p_string->i_x_margin;
610 pen_x = i_pic_width / 2 / 4 - p_line->i_width / 2
611 + p_string->i_x_margin;
614 for( i = 0; p_line->pp_glyphs[i] != NULL; i++ )
616 FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ];
617 #define alpha p_vout->p_text_renderer_data->pi_gamma[ p_glyph->bitmap.buffer[ x + y * p_glyph->bitmap.width ] ]
618 #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 ]
619 for(y = 0; y < p_glyph->bitmap.rows; y++ )
621 for( x = 0; x < p_glyph->bitmap.width; x++ )
623 if( alpha == 0 ) continue;
626 pixel( 0 ) = ( ( pixel( 0 ) * ( 255 - alpha ) ) >> 8 );
627 pixel( 1 ) = ( ( pixel( 1 ) * ( 255 - alpha ) ) >> 8 );
628 pixel( 2 ) = ( ( pixel( 2 ) * ( 255 - alpha ) ) >> 8 );
630 pixel( 0 ) = ( ( pixel( 0 ) * ( 255 - alpha ) ) >> 8 );
631 pixel( 1 ) = ( ( pixel( 1 ) * ( 255 - alpha ) ) >> 8 );
632 pixel( 2 ) = ( ( pixel( 2 ) * ( 255 - alpha ) ) >> 8 );
634 pixel( 0 ) = ( ( pixel( 0 ) * ( 255 - alpha ) ) >> 8 );
635 pixel( 1 ) = ( ( pixel( 1 ) * ( 255 - alpha ) ) >> 8 );
636 pixel( 2 ) = ( ( pixel( 2 ) * ( 255 - alpha ) ) >> 8 );
638 pixel( 0 ) = ( ( pixel( 0 ) * ( 255 - alpha ) ) >> 8 );
639 pixel( 1 ) = ( ( pixel( 1 ) * ( 255 - alpha ) ) >> 8 );
640 pixel( 2 ) = ( ( pixel( 2 ) * ( 255 - alpha ) ) >> 8 );
644 for(y = 0; y < p_glyph->bitmap.rows; y++ )
646 for( x = 0; x < p_glyph->bitmap.width; x++ )
648 if( alpha == 0 ) continue;
650 pixel( 0 ) = ( ( pixel( 0 ) * ( 255 - alpha ) ) >> 8 ) +
651 ( 255 * alpha >> 8 );
652 pixel( 1 ) = ( ( pixel( 1 ) * ( 255 - alpha ) ) >> 8 ) +
653 ( 255 * alpha >> 8 );
654 pixel( 2 ) = ( ( pixel( 2 ) * ( 255 - alpha ) ) >> 8 ) +
655 ( 255 * alpha >> 8 );
665 * This function receives a string and creates a subpicture for it. It
666 * also calculates the size needed for this string, and renders the
667 * needed glyphs into memory. It is used as pf_add_string callback in
668 * the vout method by this module
670 static subpicture_t *AddText ( vout_thread_t *p_vout, int i_channel,
671 char *psz_string, text_style_t *p_style, int i_flags,
672 int i_hmargin, int i_vmargin, mtime_t i_start,
675 subpicture_sys_t *p_string;
676 int i, i_pen_y, i_pen_x, i_error, i_glyph_index, i_previous;
677 subpicture_t *p_subpic;
678 line_desc_t *p_line, *p_next;
679 uint32_t *p_unicode_string, i_char;
682 #if defined(HAVE_ICONV)
683 iconv_t iconv_handle;
692 if ( !psz_string || !*psz_string )
708 /* Create and initialize a subpicture */
709 p_subpic = vout_CreateSubPicture( p_vout, i_channel, TEXT_CONTENT,
711 if ( p_subpic == NULL )
716 p_subpic->pf_render = Render;
717 p_subpic->pf_destroy = FreeString;
718 p_subpic->i_start = i_start;
719 p_subpic->i_stop = i_stop;
722 p_subpic->b_ephemer = VLC_TRUE;
726 p_subpic->b_ephemer = VLC_FALSE;
729 /* Create and initialize private data for the subpicture */
730 p_string = malloc( sizeof(subpicture_sys_t) );
731 if ( p_string == NULL )
733 msg_Err( p_vout, "Out of memory" );
736 p_subpic->p_sys = p_string;
737 p_string->i_flags = i_flags;
738 p_string->i_x_margin = i_hmargin;
739 p_string->i_y_margin = i_vmargin;
740 p_string->p_lines = 0;
741 p_string->psz_text = strdup( psz_string );
743 #if defined(HAVE_ICONV)
744 p_unicode_string = malloc( ( strlen(psz_string) + 1 ) * sizeof(uint32_t) );
745 if( p_unicode_string == NULL )
747 msg_Err( p_vout, "Out of memory" );
750 #if defined(WORDS_BIGENDIAN)
751 iconv_handle = iconv_open( "UCS-4BE", "UTF-8" );
753 iconv_handle = iconv_open( "UCS-4LE", "UTF-8" );
755 if( iconv_handle == (iconv_t)-1 )
757 msg_Warn( p_vout, "Unable to do convertion" );
762 char *p_in_buffer, *p_out_buffer;
763 size_t i_in_bytes, i_out_bytes, i_out_bytes_left, i_ret;
764 i_in_bytes = strlen( psz_string );
765 i_out_bytes = i_in_bytes * sizeof( uint32_t );
766 i_out_bytes_left = i_out_bytes;
767 p_in_buffer = psz_string;
768 p_out_buffer = (char *)p_unicode_string;
769 i_ret = iconv( iconv_handle, &p_in_buffer, &i_in_bytes, &p_out_buffer, &i_out_bytes_left );
772 msg_Warn( p_vout, "Failed to convert string to unicode (%s), bytes left %d", strerror(errno), i_in_bytes );
775 *(uint32_t*)p_out_buffer = 0;
776 i_string_length = ( i_out_bytes - i_out_bytes_left ) / sizeof(uint32_t);
779 #if defined(HAVE_FRIBIDI)
781 uint32_t *p_fribidi_string;
782 FriBidiCharType base_dir = FRIBIDI_TYPE_ON;
783 p_fribidi_string = malloc( ( i_string_length + 1 ) * sizeof(uint32_t) );
784 fribidi_log2vis( (FriBidiChar*)p_unicode_string, i_string_length,
785 &base_dir, (FriBidiChar*)p_fribidi_string, NULL, NULL,
787 free( p_unicode_string );
788 p_unicode_string = p_fribidi_string;
789 p_fribidi_string[ i_string_length ] = 0;
794 /* Calculate relative glyph positions and a bounding box for the
796 p_line = NewLine( psz_string );
799 msg_Err( p_vout, "Out of memory" );
802 p_string->p_lines = p_line;
808 #define face p_vout->p_text_renderer_data->p_face
809 #define glyph face->glyph
811 while( *p_unicode_string )
813 i_char = *p_unicode_string++;
814 if ( i_char == '\r' ) /* ignore CR chars wherever they may be */
819 if ( i_char == '\n' )
821 p_next = NewLine( psz_string );
824 msg_Err( p_vout, "Out of memory" );
827 p_line->p_next = p_next;
828 p_line->i_width = line.xMax;
829 p_line->i_height = face->size->metrics.height >> 6;
830 p_line->pp_glyphs[ i ] = NULL;
832 result.x = __MAX( result.x, line.xMax );
833 result.y += face->size->metrics.height >> 6;
839 i_pen_y += face->size->metrics.height >> 6;
840 msg_Dbg( p_vout, "Creating new line, i is %d", i );
845 i_glyph_index = FT_Get_Char_Index( face, i_char );
846 if ( p_vout->p_text_renderer_data->i_use_kerning && i_glyph_index
850 FT_Get_Kerning( face, i_previous, i_glyph_index,
851 ft_kerning_default, &delta );
852 i_pen_x += delta.x >> 6;
855 p_line->p_glyph_pos[ i ].x = i_pen_x;
856 p_line->p_glyph_pos[ i ].y = i_pen_y;
857 i_error = FT_Load_Glyph( face, i_glyph_index, FT_LOAD_DEFAULT );
860 msg_Err( p_vout, "FT_Load_Glyph returned %d", i_error );
863 i_error = FT_Get_Glyph( glyph, &tmp_glyph );
866 msg_Err( p_vout, "FT_Get_Glyph returned %d", i_error );
869 FT_Glyph_Get_CBox( tmp_glyph, ft_glyph_bbox_pixels, &glyph_size );
870 i_error = FT_Glyph_To_Bitmap( &tmp_glyph, ft_render_mode_normal,
872 if ( i_error ) continue;
873 p_line->pp_glyphs[ i ] = (FT_BitmapGlyph)tmp_glyph;
876 line.xMax = p_line->p_glyph_pos[i].x + glyph_size.xMax - glyph_size.xMin;
877 line.yMax = __MAX( line.yMax, glyph_size.yMax );
878 line.yMin = __MIN( line.yMin, glyph_size.yMin );
880 i_previous = i_glyph_index;
881 i_pen_x += glyph->advance.x >> 6;
884 p_line->i_width = line.xMax;
885 p_line->i_height = face->size->metrics.height >> 6;
886 p_line->pp_glyphs[ i ] = NULL;
887 result.x = __MAX( result.x, line.xMax );
888 result.y += line.yMax - line.yMin;
889 p_string->i_height = result.y;
890 p_string->i_width = result.x;
891 vout_DisplaySubPicture( p_vout, p_subpic );
898 FreeString( p_subpic );
899 vout_DestroySubPicture( p_vout, p_subpic );
903 static void FreeString( subpicture_t *p_subpic )
906 subpicture_sys_t *p_string = p_subpic->p_sys;
907 line_desc_t *p_line, *p_next;
909 if( p_subpic->p_sys == NULL ) return;
911 for( p_line = p_string->p_lines; p_line != NULL; p_line = p_next )
913 p_next = p_line->p_next;
914 for( i = 0; p_line->pp_glyphs[ i ] != NULL; i++ )
916 FT_Done_Glyph( (FT_Glyph)p_line->pp_glyphs[ i ] );
918 free( p_line->pp_glyphs );
919 free( p_line->p_glyph_pos );
923 free( p_string->psz_text );
927 #if !defined( HAVE_ICONV )
928 /* convert one or more utf8 bytes into a unicode character */
929 static int GetUnicodeCharFromUTF8( byte_t **ppsz_utf8_string )
931 int i_remaining_bytes, i_char = 0;
932 if( ( **ppsz_utf8_string & 0xFC ) == 0xFC )
934 i_char = **ppsz_utf8_string & 1;
935 i_remaining_bytes = 5;
937 else if( ( **ppsz_utf8_string & 0xF8 ) == 0xF8 )
939 i_char = **ppsz_utf8_string & 3;
940 i_remaining_bytes = 4;
942 else if( ( **ppsz_utf8_string & 0xF0 ) == 0xF0 )
944 i_char = **ppsz_utf8_string & 7;
945 i_remaining_bytes = 3;
947 else if( ( **ppsz_utf8_string & 0xE0 ) == 0xE0 )
949 i_char = **ppsz_utf8_string & 15;
950 i_remaining_bytes = 2;
952 else if( ( **ppsz_utf8_string & 0xC0 ) == 0xC0 )
954 i_char = **ppsz_utf8_string & 31;
955 i_remaining_bytes = 1;
959 i_char = **ppsz_utf8_string;
960 i_remaining_bytes = 0;
962 while( i_remaining_bytes )
964 (*ppsz_utf8_string)++;
966 i_char = ( i_char << 6 ) + ( **ppsz_utf8_string & 0x3F );
968 (*ppsz_utf8_string)++;
973 static line_desc_t *NewLine( byte_t *psz_string )
976 line_desc_t *p_line = malloc( sizeof(line_desc_t) );
981 p_line->i_height = 0;
983 p_line->p_next = NULL;
985 /* We don't use CountUtf8Characters() here because we are not acutally
986 * sure the string is utf8. Better be safe than sorry. */
987 i_count = strlen( psz_string );
989 p_line->pp_glyphs = malloc( sizeof(FT_BitmapGlyph)
991 if( p_line->pp_glyphs == NULL )
996 p_line->p_glyph_pos = malloc( sizeof( FT_Vector )
998 if( p_line->p_glyph_pos == NULL )
1000 free( p_line->pp_glyphs );