]> git.sesse.net Git - vlc/blob - modules/misc/freetype.c
* modules/misc/freetype.c: don't use the pitch of the picture to calculate the text...
[vlc] / modules / misc / freetype.c
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 $
6  *
7  * Authors: Sigmund Augdal <sigmunau@idi.ntnu.no>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>
29
30 #include <vlc/vlc.h>
31 #include <vlc/vout.h>
32 #include <osd.h>
33 #include <math.h>
34
35 #ifdef HAVE_ERRNO_H
36 #   include <errno.h>
37 #endif
38
39 #include <ft2build.h>
40 #include FT_FREETYPE_H
41 #include FT_GLYPH_H
42
43 #ifdef SYS_DARWIN
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 */
49 #else
50 #define DEFAULT_FONT "/usr/share/fonts/truetype/freefont/FreeSerifBold.ttf"
51 #endif
52
53 #if defined(HAVE_ICONV)
54 #include <iconv.h>
55 #endif
56 #if defined(HAVE_FRIBIDI)
57 #include <fribidi/fribidi.h>
58 #endif
59
60 typedef struct line_desc_t line_desc_t;
61
62 /*****************************************************************************
63  * Local prototypes
64  *****************************************************************************/
65 static int  Create    ( vlc_object_t * );
66 static void Destroy   ( vlc_object_t * );
67
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 * );
79
80 static int  GetUnicodeCharFromUTF8( byte_t ** );
81
82 static line_desc_t *NewLine( byte_t * );
83
84 /*****************************************************************************
85  * Module descriptor
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" )
91
92 static char *ppsz_sizes[] = { "smaller", "small", "normal", "large", "larger", NULL};
93
94 vlc_module_begin();
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 );
103 vlc_module_end();
104
105 /**
106  * Private data in a subpicture. Describes a string.
107  */
108 struct subpicture_sys_t
109 {
110     int            i_x_margin;
111     int            i_y_margin;
112     int            i_width;
113     int            i_height;
114     int            i_flags;
115     /** The string associated with this subpicture */
116     byte_t        *psz_text;
117     line_desc_t   *p_lines;
118 };
119
120 struct line_desc_t
121 {
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;
126     int             i_height;
127     int             i_width;
128     line_desc_t    *p_next;
129 };
130
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
138 {
139     FT_Library     p_library;   /* handle to library     */
140     FT_Face        p_face;      /* handle to face object */
141     vlc_mutex_t   *lock;
142     vlc_bool_t     i_use_kerning;
143     uint8_t        pi_gamma[256];
144 };
145
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 )
153 {
154     vout_thread_t *p_vout = (vout_thread_t *)p_this;
155     char *psz_fontfile;
156     int i, i_error;
157     int i_font_factor = 0;
158     int i_fontsize = 0;
159     double gamma_inv = 1.0f / gamma_value;
160     vlc_value_t val;
161
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 )
165     {
166         msg_Err( p_vout, "out of memory" );
167         return VLC_ENOMEM;
168     }
169
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 );
173     }
174
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 );
180
181     /* Look what method was requested */
182     var_Get( p_vout, "freetype-font", &val );
183     psz_fontfile = val.psz_string;
184
185     if( !psz_fontfile || !*psz_fontfile )
186     {
187         if( psz_fontfile ) free( psz_fontfile );
188         psz_fontfile = (char *)malloc( PATH_MAX + 1 );
189 #ifdef WIN32
190         GetWindowsDirectory( psz_fontfile, PATH_MAX + 1 );
191         strcat( psz_fontfile, "\\fonts\\arial.ttf" );
192 #elif SYS_DARWIN
193         strcpy( psz_fontfile, DEFAULT_FONT );
194 #else
195         msg_Err( p_vout, "user didn't specify a font" );
196         free( p_vout->p_text_renderer_data );
197         return VLC_EGENERIC;
198 #endif
199     }
200
201     i_error = FT_Init_FreeType( &p_vout->p_text_renderer_data->p_library );
202     if( i_error )
203     {
204         msg_Err( p_vout, "couldn't initialize freetype" );
205         free( p_vout->p_text_renderer_data );
206         return VLC_EGENERIC;
207     }
208
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 )
213     {
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 );
218         return VLC_EGENERIC;
219     }
220     else if( i_error )
221     {
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 );
226         return VLC_EGENERIC;
227     }
228     if( psz_fontfile ) free( psz_fontfile );
229
230     i_error = FT_Select_Charmap( p_vout->p_text_renderer_data->p_face,
231                                  ft_encoding_unicode );
232     if( i_error )
233     {
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 );
238         return VLC_EGENERIC;
239     }
240
241     p_vout->p_text_renderer_data->i_use_kerning =
242         FT_HAS_KERNING(p_vout->p_text_renderer_data->p_face);
243
244     var_Get( p_vout, "freetype-rel-fontsize", &val );
245     
246     if( val.psz_string )
247     {
248         if( strncmp( val.psz_string, "smaller", 7 ) == 0 )
249         {
250             i_font_factor = 20;
251         }
252         else if( strncmp( val.psz_string, "small", 5 ) == 0 )
253         {
254             i_font_factor = 18;
255         }
256         else if( strncmp( val.psz_string, "normal", 6 ) == 0 )
257         {
258             i_font_factor = 16;
259         }
260         else if( strncmp( val.psz_string, "large", 5 ) == 0 )
261         {
262             i_font_factor = 12;
263         }
264         else if( strncmp( val.psz_string, "larger", 6 ) == 0 )
265         {
266             i_font_factor = 6;
267         }
268         else
269         {
270             var_Get( p_vout, "freetype-fontsize", &val );
271             i_fontsize = val.i_int;
272         }
273         if( i_font_factor )
274             i_fontsize = (int) p_vout->render.i_height / i_font_factor;
275         free( val.psz_string );
276     }
277     else
278     {
279         var_Get( p_vout, "freetype-fontsize", &val );
280         i_fontsize = val.i_int;
281     }
282
283     i_error = FT_Set_Pixel_Sizes( p_vout->p_text_renderer_data->p_face, 0,
284                                   i_fontsize );
285     if( i_error )
286     {
287         msg_Err( p_vout, "couldn't set font size to %d", i_fontsize );
288         free( p_vout->p_text_renderer_data );
289         return VLC_EGENERIC;
290     }
291     p_vout->pf_add_string = AddText;
292     return VLC_SUCCESS;
293 }
294
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 )
301 {
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 );
306 }
307
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 )
315 {
316     switch( p_vout->output.i_chroma )
317     {
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 );
323             break;
324 #if 0
325         /* RV16 target, scaling */
326         case VLC_FOURCC('R','V','1','6'):
327             RenderRV16( p_vout, p_pic, p_subpic );
328             break;
329 #endif
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 );
334             break;
335         /* NVidia or BeOS overlay, no scaling */
336         case VLC_FOURCC('Y','U','Y','2'):
337             RenderYUY2( p_vout, p_pic, p_subpic );
338             break;
339
340         default:
341             msg_Err( p_vout, "unknown chroma, can't render SPU" );
342             break;
343     }
344 }
345
346 /**
347  * Draw a string on a i420 (or similar) picture
348  */
349 static void RenderI420( vout_thread_t *p_vout, picture_t *p_pic,
350                     const subpicture_t *p_subpic )
351 {
352     subpicture_sys_t *p_string = p_subpic->p_sys;
353     int i_plane, x, y, pen_x, pen_y;
354     unsigned int i;
355     line_desc_t *p_line;
356
357     for( p_line = p_subpic->p_sys->p_lines; p_line != NULL; p_line = p_line->p_next )
358     {
359         for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
360         {
361             uint8_t *p_in;
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;
364
365             p_in = p_pic->p[ i_plane ].p_pixels;
366
367             if ( i_plane == 0 )
368             {
369                 if ( p_string->i_flags & OSD_ALIGN_BOTTOM )
370                 {
371                     pen_y = p_pic->p[ i_plane ].i_lines - p_string->i_height -
372                         p_string->i_y_margin;
373                 }
374                 else
375                 {
376                     pen_y = p_string->i_y_margin;
377                 }
378                 pen_y += p_vout->p_text_renderer_data->p_face->size->metrics.ascender >> 6;
379                 if ( p_string->i_flags & OSD_ALIGN_RIGHT )
380                 {
381                     pen_x = i_pic_width - p_line->i_width
382                         - p_string->i_x_margin;
383                 }
384                 else if ( p_string->i_flags & OSD_ALIGN_LEFT )
385                 {
386                     pen_x = p_string->i_x_margin;
387                 }
388                 else
389                 {
390                     pen_x = i_pic_width / 2 - p_line->i_width / 2
391                         + p_string->i_x_margin;
392                 }
393
394                 for( i = 0; p_line->pp_glyphs[i] != NULL; i++ )
395                 {
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++ )
400                     {
401                         for( x = 0; x < p_glyph->bitmap.width; x++ )
402                         {
403                             pen_y--;
404                             pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 );
405                             pen_y++; pen_x--;
406                             pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 );
407                             pen_x += 2;
408                             pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 );
409                             pen_y++; pen_x--;
410                             pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 );
411                             pen_y--;
412                         }
413                     }
414                     for(y = 0; y < p_glyph->bitmap.rows; y++ )
415                     {
416                         for( x = 0; x < p_glyph->bitmap.width; x++ )
417                         {
418                             pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 ) +
419                                 ( 255 * alpha >> 8 );
420                         }
421                     }
422 #undef alpha
423 #undef pixel
424                 }
425             }
426             else
427             {
428                 if ( p_string->i_flags & OSD_ALIGN_BOTTOM )
429                 {
430                     pen_y = p_pic->p[i_plane].i_lines - ( p_string->i_height>>1) -
431                         (p_string->i_y_margin>>1);
432                 }
433                 else
434                 {
435                     pen_y = p_string->i_y_margin >> 1;
436                 }
437                 pen_y += p_vout->p_text_renderer_data->p_face->size->metrics.ascender >> 7;
438                 if ( p_string->i_flags & OSD_ALIGN_RIGHT )
439                 {
440                     pen_x = i_pic_width - ( p_line->i_width >> 1 )
441                         - ( p_string->i_x_margin >> 1 );
442                 }
443                 else if ( p_string->i_flags & OSD_ALIGN_LEFT )
444                 {
445                     pen_x = p_string->i_x_margin >> 1;
446                 }
447                 else
448                 {
449                     pen_x = i_pic_width / 2 - p_line->i_width / 4
450                         + p_string->i_x_margin / 2;
451                 }
452
453                 for( i = 0; p_line->pp_glyphs[i] != NULL; i++ )
454                 {
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 )
459                     {
460                         for( x = 0; x < p_glyph->bitmap.width; x+=2 )
461                         {
462                             pixel = ( ( pixel * ( 0xFF - alpha ) ) >> 8 ) +
463                                 ( 0x80 * alpha >> 8 );
464 #undef alpha
465 #undef pixel
466                         }
467                     }
468                 }
469             }
470         }
471     }
472 }
473
474 /**
475  * Draw a string on a YUY2 picture
476  */
477 static void RenderYUY2( vout_thread_t *p_vout, picture_t *p_pic,
478                         const subpicture_t *p_subpic )
479 {
480     subpicture_sys_t *p_string = p_subpic->p_sys;
481     int x, y, pen_x, pen_y;
482     unsigned int i;
483     line_desc_t *p_line;
484
485     for( p_line = p_subpic->p_sys->p_lines; p_line != NULL;
486          p_line = p_line->p_next )
487     {
488         uint8_t *p_in;
489         int i_pic_pitch = p_pic->p[0].i_pitch;
490         int i_pic_width = p_pic->p[0].i_visible_pitch;
491
492         p_in = p_pic->p[0].p_pixels;
493         
494         if ( p_string->i_flags & OSD_ALIGN_BOTTOM )
495         {
496             pen_y = p_pic->p[0].i_lines - p_string->i_height -
497                 p_string->i_y_margin;
498         }
499         else
500         {
501             pen_y = p_string->i_y_margin;
502         }
503         pen_y += p_vout->p_text_renderer_data->p_face->size->metrics.ascender >> 6;
504         if ( p_string->i_flags & OSD_ALIGN_RIGHT )
505         {
506             pen_x = i_pic_width - p_line->i_width
507                 - p_string->i_x_margin;
508         }
509         else if ( p_string->i_flags & OSD_ALIGN_LEFT )
510         {
511             pen_x = p_string->i_x_margin;
512         }
513         else
514         {
515             pen_x = i_pic_width / 2 /2 - p_line->i_width / 2 + p_string->i_x_margin;
516         }
517         
518         for( i = 0; p_line->pp_glyphs[i] != NULL; i++ )
519         {
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++ )
524             {
525                 for( x = 0; x < p_glyph->bitmap.width; x++ )
526                 {
527                     pen_y--;
528                     pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 );
529                     pen_y++; pen_x--;
530                     pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 );
531                     pen_x += 2;
532                     pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 );
533                     pen_y++; pen_x--;
534                     pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 );
535                     pen_y--;
536                 }
537             }
538             for(y = 0; y < p_glyph->bitmap.rows; y++ )
539             {
540                 for( x = 0; x < p_glyph->bitmap.width; x++ )
541                 {
542                     pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 ) +
543                         ( 255 * alpha >> 8 );
544                 }
545             }
546 #undef alpha
547 #undef pixel
548         }
549     }
550 }
551
552 /**
553  * Draw a string on a RV32 picture
554  */
555 static void RenderRV32( vout_thread_t *p_vout, picture_t *p_pic,
556                     const subpicture_t *p_subpic )
557 {
558     subpicture_sys_t *p_string = p_subpic->p_sys;
559     int i_plane, x, y, pen_x, pen_y;
560     unsigned int i;
561     line_desc_t *p_line;
562
563     i_plane = 0;
564     
565     for( p_line = p_subpic->p_sys->p_lines; p_line != NULL; p_line = p_line->p_next )
566     {
567         uint8_t *p_in;
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;
570
571         p_in = p_pic->p[ i_plane ].p_pixels;
572         
573         if ( p_string->i_flags & OSD_ALIGN_BOTTOM )
574         {
575             pen_y = p_pic->p[ i_plane ].i_lines - p_string->i_height -
576                 p_string->i_y_margin;
577         }
578         else
579         {
580             pen_y = p_string->i_y_margin;
581         }
582         pen_y += p_vout->p_text_renderer_data->p_face->size->metrics.ascender >> 6;
583         if ( p_string->i_flags & OSD_ALIGN_RIGHT )
584         {
585             pen_x = i_pic_width - p_line->i_width
586                 - p_string->i_x_margin;
587         }
588         else if ( p_string->i_flags & OSD_ALIGN_LEFT )
589         {
590             pen_x = p_string->i_x_margin;
591         }
592         else
593         {
594             pen_x = i_pic_width / 2 / 4 - p_line->i_width / 2
595                 + p_string->i_x_margin;
596         }
597         
598         for( i = 0; p_line->pp_glyphs[i] != NULL; i++ )
599         {
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++ )
604             {
605                 for( x = 0; x < p_glyph->bitmap.width; x++ )
606                 {
607                     pen_y--;
608                     pixel( 0 ) = ( ( pixel( 0 ) * ( 255 - alpha ) ) >> 8 );
609                     pixel( 1 ) = ( ( pixel( 1 ) * ( 255 - alpha ) ) >> 8 );
610                     pixel( 2 ) = ( ( pixel( 2 ) * ( 255 - alpha ) ) >> 8 );
611                     pen_y++; pen_x--;
612                     pixel( 0 ) = ( ( pixel( 0 ) * ( 255 - alpha ) ) >> 8 );
613                     pixel( 1 ) = ( ( pixel( 1 ) * ( 255 - alpha ) ) >> 8 );
614                     pixel( 2 ) = ( ( pixel( 2 ) * ( 255 - alpha ) ) >> 8 );
615                     pen_x += 2;
616                     pixel( 0 ) = ( ( pixel( 0 ) * ( 255 - alpha ) ) >> 8 );
617                     pixel( 1 ) = ( ( pixel( 1 ) * ( 255 - alpha ) ) >> 8 );
618                     pixel( 2 ) = ( ( pixel( 2 ) * ( 255 - alpha ) ) >> 8 );
619                     pen_y++; pen_x--;
620                     pixel( 0 ) = ( ( pixel( 0 ) * ( 255 - alpha ) ) >> 8 );
621                     pixel( 1 ) = ( ( pixel( 1 ) * ( 255 - alpha ) ) >> 8 );
622                     pixel( 2 ) = ( ( pixel( 2 ) * ( 255 - alpha ) ) >> 8 );
623                     pen_y--;
624                 }
625             }
626             for(y = 0; y < p_glyph->bitmap.rows; y++ )
627             {
628                 for( x = 0; x < p_glyph->bitmap.width; x++ )
629                 {
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 );
636                 }
637             }
638 #undef alpha
639 #undef pixel
640         }
641     }
642 }
643
644 /**
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
649  */
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 )
653 {
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;
659     int i_string_length;
660     iconv_t iconv_handle;
661     
662     FT_BBox line;
663     FT_BBox glyph_size;
664     FT_Vector result;
665     FT_Glyph tmp_glyph;
666
667     /* Sanity check */
668     if ( !psz_string || !*psz_string )
669     {
670         return VLC_EGENERIC;
671     }
672
673     result.x = 0;
674     result.y = 0;
675     line.xMin = 0;
676     line.xMax = 0;
677     line.yMin = 0;
678     line.yMax = 0;
679
680     p_line = 0;
681     p_string = 0;
682     p_subpic = 0;
683
684     /* Create and initialize a subpicture */
685     p_subpic = vout_CreateSubPicture( p_vout, MEMORY_SUBPICTURE );
686     if ( p_subpic == NULL )
687     {
688         return VLC_EGENERIC;
689     }
690     p_subpic->p_sys = 0;
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;
695     if( i_stop == 0 )
696     {
697         p_subpic->b_ephemer = VLC_TRUE;
698     }
699     else
700     {
701         p_subpic->b_ephemer = VLC_FALSE;
702     }
703
704     /* Create and initialize private data for the subpicture */
705     p_string = malloc( sizeof(subpicture_sys_t) );
706     if ( p_string == NULL )
707     {
708         msg_Err( p_vout, "Out of memory" );
709         goto error;
710     }
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 );
717
718 #if defined(HAVE_ICONV)
719     p_unicode_string = malloc( ( strlen(psz_string) + 1 ) * sizeof(uint32_t) );
720     if( p_unicode_string == NULL )
721     {
722         msg_Err( p_vout, "Out of memory" );
723         goto error;
724     }
725 #if defined(WORDS_BIGENDIAN)
726     iconv_handle = iconv_open( "UCS-4BE", "UTF-8" );
727 #else    
728     iconv_handle = iconv_open( "UCS-4LE", "UTF-8" );
729 #endif
730     if( iconv_handle == (iconv_t)-1 )
731     {
732         msg_Warn( p_vout, "Unable to do convertion" );
733         goto error;
734     }
735
736     {
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 );
745         if( i_in_bytes )
746         {
747             msg_Warn( p_vout, "Failed to convert string to unicode (%s), bytes left %d", strerror(errno), i_in_bytes );
748             goto error;
749         }
750         *(uint32_t*)p_out_buffer = 0;
751         i_string_length = ( i_out_bytes - i_out_bytes_left ) / sizeof(uint32_t);
752     }
753         
754 #if defined(HAVE_FRIBIDI)
755     {
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;
764     }
765 #endif
766 #endif
767     
768     /* Calculate relative glyph positions and a bounding box for the
769      * entire string */
770     p_line = NewLine( psz_string );
771     if( p_line == NULL )
772     {
773         msg_Err( p_vout, "Out of memory" );
774         goto error;
775     }
776     p_string->p_lines = p_line;
777     i_pen_x = 0;
778     i_pen_y = 0;
779     i_previous = 0;
780     i = 0;
781
782 #define face p_vout->p_text_renderer_data->p_face
783 #define glyph face->glyph
784
785     while( *p_unicode_string )
786     {
787         i_char = *p_unicode_string++;
788         if ( i_char == '\r' ) /* ignore CR chars wherever they may be */
789         {
790             continue;
791         }
792
793         if ( i_char == '\n' )
794         {
795             p_next = NewLine( psz_string );
796             if( p_next == NULL )
797             {
798                 msg_Err( p_vout, "Out of memory" );
799                 goto error;
800             }
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;
805             p_line = p_next;
806             result.x = __MAX( result.x, line.xMax );
807             result.y += face->size->metrics.height >> 6;
808             i_pen_x = 0;
809             line.xMin = 0;
810             line.xMax = 0;
811             line.yMin = 0;
812             line.yMax = 0;
813             i_pen_y += face->size->metrics.height >> 6;
814             msg_Dbg( p_vout, "Creating new line, i is %d", i );
815             i = 0;
816             continue;
817         }
818
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
821             && i_previous )
822         {
823             FT_Vector delta;
824             FT_Get_Kerning( face, i_previous, i_glyph_index,
825                             ft_kerning_default, &delta );
826             i_pen_x += delta.x >> 6;
827
828         }
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 );
832         if ( i_error )
833         {
834             msg_Err( p_vout, "FT_Load_Glyph returned %d", i_error );
835             goto error;
836         }
837         i_error = FT_Get_Glyph( glyph, &tmp_glyph );
838         if ( i_error )
839         {
840             msg_Err( p_vout, "FT_Get_Glyph returned %d", i_error );
841             goto error;
842         }
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,
845                                       NULL, 1 );
846         if ( i_error ) continue;
847         p_line->pp_glyphs[ i ] = (FT_BitmapGlyph)tmp_glyph;
848
849         /* Do rest */
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 );
853
854         i_previous = i_glyph_index;
855         i_pen_x += glyph->advance.x >> 6;
856         i++;
857     }
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 );
866     return VLC_SUCCESS;
867
868 #undef face
869 #undef glyph
870
871  error:
872     FreeString( p_subpic );
873     vout_DestroySubPicture( p_vout, p_subpic );
874     return VLC_EGENERIC;
875 }
876
877 static void FreeString( subpicture_t *p_subpic )
878 {
879     unsigned int i;
880     subpicture_sys_t *p_string = p_subpic->p_sys;
881     line_desc_t *p_line, *p_next;
882
883     if( p_subpic->p_sys == NULL ) return;
884
885     for( p_line = p_string->p_lines; p_line != NULL; p_line = p_next )
886     {
887         p_next = p_line->p_next;
888         for( i = 0; p_line->pp_glyphs[ i ] != NULL; i++ )
889         {
890             FT_Done_Glyph( (FT_Glyph)p_line->pp_glyphs[ i ] );
891         }
892         free( p_line->pp_glyphs );
893         free( p_line->p_glyph_pos );
894         free( p_line );
895     }
896             
897     free( p_string->psz_text );
898     free( p_string );
899 }
900
901 /* convert one or more utf8 bytes into a unicode character */
902 static int GetUnicodeCharFromUTF8( byte_t **ppsz_utf8_string )
903 {
904     int i_remaining_bytes, i_char = 0;
905     if( ( **ppsz_utf8_string & 0xFC ) == 0xFC )
906     {
907         i_char = **ppsz_utf8_string & 1;
908         i_remaining_bytes = 5;
909     }
910     else if( ( **ppsz_utf8_string & 0xF8 ) == 0xF8 )
911     {
912         i_char = **ppsz_utf8_string & 3;
913         i_remaining_bytes = 4;
914     }
915     else if( ( **ppsz_utf8_string & 0xF0 ) == 0xF0 )
916     {
917         i_char = **ppsz_utf8_string & 7;
918         i_remaining_bytes = 3;
919     }
920     else if( ( **ppsz_utf8_string & 0xE0 ) == 0xE0 )
921     {
922         i_char = **ppsz_utf8_string & 15;
923         i_remaining_bytes = 2;
924     }
925     else if( ( **ppsz_utf8_string & 0xC0 ) == 0xC0 )
926     {
927         i_char = **ppsz_utf8_string & 31;
928         i_remaining_bytes = 1;
929     }
930     else
931     {
932         i_char = **ppsz_utf8_string;
933         i_remaining_bytes = 0;
934     }
935     while( i_remaining_bytes )
936     {
937         (*ppsz_utf8_string)++;
938         i_remaining_bytes--;
939         i_char = ( i_char << 6 ) + ( **ppsz_utf8_string & 0x3F );
940     }
941     (*ppsz_utf8_string)++;
942     return i_char;
943 }
944
945 static line_desc_t *NewLine( byte_t *psz_string )
946 {
947     int i_count;
948     line_desc_t *p_line = malloc( sizeof(line_desc_t) );
949     if( !p_line )
950     {
951         return NULL;
952     }
953     p_line->i_height = 0;
954     p_line->i_width = 0;
955     p_line->p_next = NULL;
956
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 );
960
961     p_line->pp_glyphs = malloc( sizeof(FT_BitmapGlyph)
962                                 * ( i_count + 1 ) );
963     if( p_line->pp_glyphs == NULL )
964     {
965         free( p_line );
966         return NULL;
967     }
968     p_line->p_glyph_pos = malloc( sizeof( FT_Vector )
969                                   * i_count + 1 );
970     if( p_line->p_glyph_pos == NULL )
971     {
972         free( p_line->pp_glyphs );
973         free( p_line );
974         return NULL;
975     }
976
977     return p_line;
978 }