]> git.sesse.net Git - vlc/blob - modules/misc/freetype.c
modules/misc/freetype.c:
[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.18 2003/08/17 15:22:49 sigmunau 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 #include <ft2build.h>
36 #include FT_FREETYPE_H
37 #include FT_GLYPH_H
38
39 #ifdef SYS_DARWIN
40 #define DEFAULT_FONT "/System/Library/Fonts/LucidaGrande.dfont"
41 #elif defined( SYS_BEOS )
42 #define DEFAULT_FONT "/boot/beos/etc/fonts/ttfonts/Swiss721.ttf"
43 #else
44 #define DEFAULT_FONT ""
45 #endif
46
47 typedef struct line_desc_t line_desc_t;
48
49 /*****************************************************************************
50  * Local prototypes
51  *****************************************************************************/
52 static int  Create    ( vlc_object_t * );
53 static void Destroy   ( vlc_object_t * );
54
55 static void Render    ( vout_thread_t *, picture_t *,
56                         const subpicture_t * );
57 static void RenderI420( vout_thread_t *, picture_t *,
58                         const subpicture_t * );
59 static void RenderYUY2( vout_thread_t *, picture_t *,
60                         const subpicture_t * );
61 static void RenderRV32( vout_thread_t *, picture_t *,
62                         const subpicture_t * );
63 static int  AddText   ( vout_thread_t *, byte_t *, text_style_t *, int,
64                         int, int, mtime_t, mtime_t );
65 static void FreeString( subpicture_t * );
66
67 static int  GetUnicodeCharFromUTF8( byte_t ** );
68
69 static line_desc_t *NewLine( byte_t * );
70
71 /*****************************************************************************
72  * Module descriptor
73  *****************************************************************************/
74 #define FONT_TEXT N_("Font")
75 #define FONT_LONGTEXT N_("Filename of Font")
76 #define FONTSIZE_TEXT N_("Font size")
77 #define FONTSIZE_LONGTEXT N_("The size of the fonts used by the osd module" )
78
79 vlc_module_begin();
80     add_category_hint( N_("Fonts"), NULL, VLC_FALSE );
81     add_file( "freetype-font", DEFAULT_FONT, NULL, FONT_TEXT, FONT_LONGTEXT, VLC_FALSE );
82     add_integer( "freetype-fontsize", 16, NULL, FONTSIZE_TEXT, FONTSIZE_LONGTEXT, VLC_FALSE );
83     set_description( _("freetype2 font renderer") );
84     set_capability( "text renderer", 100 );
85     add_shortcut( "text" );
86     set_callbacks( Create, Destroy );
87 vlc_module_end();
88
89 /**
90  * Private data in a aubpicture. Describes a string.
91  */
92 struct subpicture_sys_t
93 {
94     int            i_x_margin;
95     int            i_y_margin;
96     int            i_width;
97     int            i_height;
98     int            i_flags;
99     /** The string associated with this subpicture */
100     byte_t        *psz_text;
101     line_desc_t   *p_lines;
102 };
103
104 struct line_desc_t
105 {
106     /** NULL-terminated list of glyphs making the string */
107     FT_BitmapGlyph *pp_glyphs;
108     /** list of relative positions for the glyphs */
109     FT_Vector      *p_glyph_pos;
110     int             i_height;
111     int             i_width;
112     line_desc_t    *p_next;
113 };
114
115 /*****************************************************************************
116  * text_remderer_sys_t: freetype local data
117  *****************************************************************************
118  * This structure is part of the video output thread descriptor.
119  * It describes the freetype specific properties of an output thread.
120  *****************************************************************************/
121 struct text_renderer_sys_t
122 {
123     FT_Library     p_library;   /* handle to library     */
124     FT_Face        p_face;      /* handle to face object */
125     vlc_mutex_t   *lock;
126     vlc_bool_t     i_use_kerning;
127     uint8_t        pi_gamma[256];
128 };
129
130 /*****************************************************************************
131  * Create: allocates osd-text video thread output method
132  *****************************************************************************
133  * This function allocates and initializes a Clone vout method.
134  *****************************************************************************/
135 #define gamma_value 2.0
136 static int Create( vlc_object_t *p_this )
137 {
138     vout_thread_t *p_vout = (vout_thread_t *)p_this;
139     char *psz_fontfile;
140     int i, i_error;
141     double gamma_inv = 1.0f / gamma_value;
142     vlc_value_t val;
143
144     /* Allocate structure */
145     p_vout->p_text_renderer_data = malloc( sizeof( text_renderer_sys_t ) );
146     if( p_vout->p_text_renderer_data == NULL )
147     {
148         msg_Err( p_vout, "out of memory" );
149         return VLC_ENOMEM;
150     }
151
152     for (i = 0; i < 256; i++) {
153         p_vout->p_text_renderer_data->pi_gamma[i] =
154             (uint8_t)( pow( (double)i / 255.0f, gamma_inv) * 255.0f );
155     }
156
157     var_Create( p_vout, "freetype-font", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
158     var_Create( p_vout, "freetype-fontsize",
159                 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
160
161     /* Look what method was requested */
162     var_Get( p_vout, "freetype-font", &val );
163     psz_fontfile = val.psz_string;
164
165     if( !psz_fontfile || !*psz_fontfile )
166     {
167         if( psz_fontfile ) free( psz_fontfile );
168         psz_fontfile = (char *)malloc( PATH_MAX + 1 );
169 #ifdef WIN32
170         GetWindowsDirectory( psz_fontfile, PATH_MAX + 1 );
171         strcat( psz_fontfile, "\\fonts\\arial.ttf" );
172 #elif SYS_DARWIN
173         strcpy( psz_fontfile, DEFAULT_FONT );
174 #else
175         msg_Err( p_vout, "user didn't specify a font" );
176         free( p_vout->p_text_renderer_data );
177         return VLC_EGENERIC;
178 #endif
179     }
180
181     i_error = FT_Init_FreeType( &p_vout->p_text_renderer_data->p_library );
182     if( i_error )
183     {
184         msg_Err( p_vout, "couldn't initialize freetype" );
185         free( p_vout->p_text_renderer_data );
186         return VLC_EGENERIC;
187     }
188
189     i_error = FT_New_Face( p_vout->p_text_renderer_data->p_library,
190                            psz_fontfile ? psz_fontfile : "", 0,
191                            &p_vout->p_text_renderer_data->p_face );
192     if( i_error == FT_Err_Unknown_File_Format )
193     {
194         msg_Err( p_vout, "file %s have unknown format", psz_fontfile );
195         FT_Done_FreeType( p_vout->p_text_renderer_data->p_library );
196         free( p_vout->p_text_renderer_data );
197         if( psz_fontfile ) free( psz_fontfile );
198         return VLC_EGENERIC;
199     }
200     else if( i_error )
201     {
202         msg_Err( p_vout, "failed to load font file %s", psz_fontfile );
203         FT_Done_FreeType( p_vout->p_text_renderer_data->p_library );
204         free( p_vout->p_text_renderer_data );
205         if( psz_fontfile ) free( psz_fontfile );
206         return VLC_EGENERIC;
207     }
208     if( psz_fontfile ) free( psz_fontfile );
209
210     i_error = FT_Select_Charmap( p_vout->p_text_renderer_data->p_face,
211                                  ft_encoding_unicode );
212     if ( i_error )
213     {
214         msg_Err( p_vout, "Font has no unicode translation table" );
215         FT_Done_Face( p_vout->p_text_renderer_data->p_face );
216         FT_Done_FreeType( p_vout->p_text_renderer_data->p_library );
217         free( p_vout->p_text_renderer_data );
218         return VLC_EGENERIC;
219     }
220
221     p_vout->p_text_renderer_data->i_use_kerning =
222         FT_HAS_KERNING(p_vout->p_text_renderer_data->p_face);
223     var_Get( p_vout, "freetype-fontsize", &val );
224
225     i_error = FT_Set_Pixel_Sizes( p_vout->p_text_renderer_data->p_face, 0,
226                                   val.i_int );
227     if( i_error )
228     {
229         msg_Err( p_vout, "couldn't set font size to %d", val.i_int );
230         free( p_vout->p_text_renderer_data );
231         return VLC_EGENERIC;
232     }
233     p_vout->pf_add_string = AddText;
234     return VLC_SUCCESS;
235 }
236
237 /*****************************************************************************
238  * Destroy: destroy Clone video thread output method
239  *****************************************************************************
240  * Clean up all data and library connections
241  *****************************************************************************/
242 static void Destroy( vlc_object_t *p_this )
243 {
244     vout_thread_t *p_vout = (vout_thread_t *)p_this;
245     FT_Done_Face( p_vout->p_text_renderer_data->p_face );
246     FT_Done_FreeType( p_vout->p_text_renderer_data->p_library );
247     free( p_vout->p_text_renderer_data );
248 }
249
250 /*****************************************************************************
251  * Render: place string in picture
252  *****************************************************************************
253  * This function merges the previously rendered freetype glyphs into a picture
254  *****************************************************************************/
255 static void Render( vout_thread_t *p_vout, picture_t *p_pic,
256                     const subpicture_t *p_subpic )
257 {
258     switch( p_vout->output.i_chroma )
259     {
260         /* I420 target, no scaling */
261         case VLC_FOURCC('I','4','2','0'):
262         case VLC_FOURCC('I','Y','U','V'):
263         case VLC_FOURCC('Y','V','1','2'):
264             RenderI420( p_vout, p_pic, p_subpic );
265             break;
266 #if 0
267         /* RV16 target, scaling */
268         case VLC_FOURCC('R','V','1','6'):
269             RenderRV16( p_vout, p_pic, p_subpic );
270             break;
271 #endif
272         /* RV32 target, scaling */
273         case VLC_FOURCC('R','V','2','4'):
274         case VLC_FOURCC('R','V','3','2'):
275             RenderRV32( p_vout, p_pic, p_subpic );
276             break;
277         /* NVidia or BeOS overlay, no scaling */
278         case VLC_FOURCC('Y','U','Y','2'):
279             RenderYUY2( p_vout, p_pic, p_subpic );
280             break;
281
282         default:
283             msg_Err( p_vout, "unknown chroma, can't render SPU" );
284             break;
285     }
286 }
287
288 /**
289  * Draw a string on a i420 (or similar) picture
290  */
291 static void RenderI420( vout_thread_t *p_vout, picture_t *p_pic,
292                     const subpicture_t *p_subpic )
293 {
294     subpicture_sys_t *p_string = p_subpic->p_sys;
295     int i_plane, x, y, pen_x, pen_y;
296     unsigned int i;
297     line_desc_t *p_line;
298
299     for( p_line = p_subpic->p_sys->p_lines; p_line != NULL; p_line = p_line->p_next )
300     {
301         for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
302         {
303             uint8_t *p_in;
304             int i_pitch = p_pic->p[ i_plane ].i_pitch;
305
306             p_in = p_pic->p[ i_plane ].p_pixels;
307
308             if ( i_plane == 0 )
309             {
310                 if ( p_string->i_flags & OSD_ALIGN_BOTTOM )
311                 {
312                     pen_y = p_pic->p[ i_plane ].i_lines - p_string->i_height -
313                         p_string->i_y_margin;
314                 }
315                 else
316                 {
317                     pen_y = p_string->i_y_margin;
318                 }
319                 pen_y += p_vout->p_text_renderer_data->p_face->size->metrics.ascender >> 6;
320                 if ( p_string->i_flags & OSD_ALIGN_RIGHT )
321                 {
322                     pen_x = i_pitch - p_line->i_width
323                         - p_string->i_x_margin;
324                 }
325                 else if ( p_string->i_flags & OSD_ALIGN_LEFT )
326                 {
327                     pen_x = p_string->i_x_margin;
328                 }
329                 else
330                 {
331                     pen_x = i_pitch / 2 - p_line->i_width / 2
332                         + p_string->i_x_margin;
333                 }
334
335                 for( i = 0; p_line->pp_glyphs[i] != NULL; i++ )
336                 {
337                     FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ];
338 #define alpha p_vout->p_text_renderer_data->pi_gamma[ p_glyph->bitmap.buffer[ x + y * p_glyph->bitmap.width ] ]
339 #define pixel p_in[ ( p_line->p_glyph_pos[ i ].y + pen_y + y - p_glyph->top ) * i_pitch + x + pen_x + p_line->p_glyph_pos[ i ].x + p_glyph->left ]
340                     for(y = 0; y < p_glyph->bitmap.rows; y++ )
341                     {
342                         for( x = 0; x < p_glyph->bitmap.width; x++ )
343                         {
344                             pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 ) +
345                                 ( 255 * alpha >> 8 );
346 #undef alpha
347 #undef pixel
348                         }
349                     }
350                 }
351             }
352             else
353             {
354                 if ( p_string->i_flags & OSD_ALIGN_BOTTOM )
355                 {
356                     pen_y = p_pic->p[i_plane].i_lines - ( p_string->i_height>>1) -
357                         (p_string->i_y_margin>>1);
358                 }
359                 else
360                 {
361                     pen_y = p_string->i_y_margin >> 1;
362                 }
363                 pen_y += p_vout->p_text_renderer_data->p_face->size->metrics.ascender >> 7;
364                 if ( p_string->i_flags & OSD_ALIGN_RIGHT )
365                 {
366                     pen_x = i_pitch - ( p_line->i_width >> 1 )
367                         - ( p_string->i_x_margin >> 1 );
368                 }
369                 else if ( p_string->i_flags & OSD_ALIGN_LEFT )
370                 {
371                     pen_x = p_string->i_x_margin >> 1;
372                 }
373                 else
374                 {
375                     pen_x = i_pitch / 2 - p_line->i_width / 4
376                         + p_string->i_x_margin / 2;
377                 }
378
379                 for( i = 0; p_line->pp_glyphs[i] != NULL; i++ )
380                 {
381                     FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ];
382 #define alpha p_vout->p_text_renderer_data->pi_gamma[ p_glyph->bitmap.buffer[ ( x + y * p_glyph->bitmap.width ) ] ]
383 #define pixel p_in[ ( ( p_line->p_glyph_pos[ i ].y >> 1 ) + pen_y + ( y >> 1 ) -  ( p_glyph->top >> 1 ) ) * i_pitch + ( x >> 1 ) + pen_x + ( p_line->p_glyph_pos[ i ].x >> 1 ) + ( p_glyph->left >>1) ]
384                     for( y = 0; y < p_glyph->bitmap.rows; y+=2 )
385                     {
386                         for( x = 0; x < p_glyph->bitmap.width; x+=2 )
387                         {
388                             pixel = ( ( pixel * ( 0xFF - alpha ) ) >> 8 ) +
389                                 ( 0x80 * alpha >> 8 );
390 #undef alpha
391 #undef pixel
392                         }
393                     }
394                 }
395             }
396         }
397     }
398 }
399
400 /**
401  * Draw a string on a YUY2 picture
402  */
403 static void RenderYUY2( vout_thread_t *p_vout, picture_t *p_pic,
404                         const subpicture_t *p_subpic )
405 {
406     subpicture_sys_t *p_string = p_subpic->p_sys;
407     int x, y, pen_x, pen_y;
408     unsigned int i;
409     line_desc_t *p_line;
410
411     for( p_line = p_subpic->p_sys->p_lines; p_line != NULL;
412          p_line = p_line->p_next )
413     {
414         uint8_t *p_in;
415         int i_pitch = p_pic->p[0].i_pitch;
416         
417         p_in = p_pic->p[0].p_pixels;
418         
419         if ( p_string->i_flags & OSD_ALIGN_BOTTOM )
420         {
421             pen_y = p_pic->p[0].i_lines - p_string->i_height -
422                 p_string->i_y_margin;
423         }
424         else
425         {
426             pen_y = p_string->i_y_margin;
427         }
428         pen_y += p_vout->p_text_renderer_data->p_face->size->metrics.ascender >> 6;
429         if ( p_string->i_flags & OSD_ALIGN_RIGHT )
430         {
431             pen_x = i_pitch - p_line->i_width
432                 - p_string->i_x_margin;
433         }
434         else if ( p_string->i_flags & OSD_ALIGN_LEFT )
435         {
436             pen_x = p_string->i_x_margin;
437         }
438         else
439         {
440             pen_x = i_pitch / 2 - p_line->i_width / 2 + p_string->i_x_margin;
441         }
442         
443         for( i = 0; p_line->pp_glyphs[i] != NULL; i++ )
444         {
445             FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ];
446 #define alpha p_vout->p_text_renderer_data->pi_gamma[ p_glyph->bitmap.buffer[ x + y * p_glyph->bitmap.width ] ]
447 #define pixel p_in[ ( p_line->p_glyph_pos[ i ].y + pen_y + y - p_glyph->top ) * i_pitch + 2 * ( x + pen_x + p_line->p_glyph_pos[ i ].x + p_glyph->left ) ]
448             for(y = 0; y < p_glyph->bitmap.rows; y++ )
449             {
450                 for( x = 0; x < p_glyph->bitmap.width; x++ )
451                 {
452                     pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 ) +
453                         ( 255 * alpha >> 8 );
454 #undef alpha
455 #undef pixel
456                 }
457             }
458         }
459     }
460 }
461
462 /**
463  * Draw a string on a RV32 picture
464  */
465 static void RenderRV32( vout_thread_t *p_vout, picture_t *p_pic,
466                     const subpicture_t *p_subpic )
467 {
468     subpicture_sys_t *p_string = p_subpic->p_sys;
469     int i_plane, x, y, pen_x, pen_y;
470     unsigned int i;
471     line_desc_t *p_line;
472
473     i_plane = 0;
474     
475     for( p_line = p_subpic->p_sys->p_lines; p_line != NULL; p_line = p_line->p_next )
476     {
477         uint8_t *p_in;
478         int i_pitch = p_pic->p[ i_plane ].i_pitch;
479         
480         p_in = p_pic->p[ i_plane ].p_pixels;
481         
482         if ( p_string->i_flags & OSD_ALIGN_BOTTOM )
483         {
484             pen_y = p_pic->p[ i_plane ].i_lines - p_string->i_height -
485                 p_string->i_y_margin;
486         }
487         else
488         {
489             pen_y = p_string->i_y_margin;
490         }
491         pen_y += p_vout->p_text_renderer_data->p_face->size->metrics.ascender >> 6;
492         if ( p_string->i_flags & OSD_ALIGN_RIGHT )
493         {
494             pen_x = i_pitch - p_line->i_width
495                 - p_string->i_x_margin;
496         }
497         else if ( p_string->i_flags & OSD_ALIGN_LEFT )
498         {
499             pen_x = p_string->i_x_margin;
500         }
501         else
502         {
503             pen_x = i_pitch / 2 - p_line->i_width / 2
504                 + p_string->i_x_margin;
505         }
506         
507         for( i = 0; p_line->pp_glyphs[i] != NULL; i++ )
508         {
509             FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ];
510 #define alpha p_vout->p_text_renderer_data->pi_gamma[ p_glyph->bitmap.buffer[ x + y * p_glyph->bitmap.width ] ]
511 #define pixel( c ) p_in[ ( p_line->p_glyph_pos[ i ].y + pen_y + y - p_glyph->top ) * i_pitch + ( x + pen_x + p_line->p_glyph_pos[ i ].x + p_glyph->left ) * 4 + c ]
512             for(y = 0; y < p_glyph->bitmap.rows; y++ )
513             {
514                 for( x = 0; x < p_glyph->bitmap.width; x++ )
515                 {
516                     pixel( 0 ) = ( ( pixel( 0 ) * ( 255 - alpha ) ) >> 8 ) +
517                         ( 255 * alpha >> 8 );
518                     pixel( 1 ) = ( ( pixel( 1 ) * ( 255 - alpha ) ) >> 8 ) +
519                         ( 255 * alpha >> 8 );
520                     pixel( 2 ) = ( ( pixel( 2 ) * ( 255 - alpha ) ) >> 8 ) +
521                         ( 255 * alpha >> 8 );
522 #undef alpha
523 #undef pixel
524                 }
525             }
526         }
527     }
528 }
529
530 /**
531  * This function receives a string and creates a subpicture for it. It
532  * also calculates the size needed for this string, and renders the
533  * needed glyphs into memory. It is used as pf_add_string callback in
534  * the vout method by this module
535  */
536 static int AddText ( vout_thread_t *p_vout, byte_t *psz_string,
537                      text_style_t *p_style, int i_flags, int i_hmargin,
538                      int i_vmargin, mtime_t i_start, mtime_t i_stop )
539 {
540     subpicture_sys_t *p_string;
541     int i, i_pen_y, i_pen_x, i_error, i_glyph_index, i_previous, i_char;
542     subpicture_t *p_subpic;
543     line_desc_t  *p_line,  *p_next;
544
545     FT_BBox line;
546     FT_BBox glyph_size;
547     FT_Vector result;
548     FT_Glyph tmp_glyph;
549
550     /* Sanity check */
551     if ( !psz_string || !*psz_string )
552     {
553         return VLC_EGENERIC;
554     }
555
556     result.x = 0;
557     result.y = 0;
558     line.xMin = 0;
559     line.xMax = 0;
560     line.yMin = 0;
561     line.yMax = 0;
562
563     p_line = 0;
564     p_string = 0;
565     p_subpic = 0;
566
567     /* Create and initialize a subpicture */
568     p_subpic = vout_CreateSubPicture( p_vout, MEMORY_SUBPICTURE );
569     if ( p_subpic == NULL )
570     {
571         return VLC_EGENERIC;
572     }
573     p_subpic->p_sys = 0;
574     p_subpic->pf_render = Render;
575     p_subpic->pf_destroy = FreeString;
576     p_subpic->i_start = i_start;
577     p_subpic->i_stop = i_stop;
578     if( i_stop == 0 )
579     {
580         p_subpic->b_ephemer = VLC_TRUE;
581     }
582     else
583     {
584         p_subpic->b_ephemer = VLC_FALSE;
585     }
586
587     /* Create and initialize private data for the subpicture */
588     p_string = malloc( sizeof(subpicture_sys_t) );
589     if ( p_string == NULL )
590     {
591         msg_Err( p_vout, "Out of memory" );
592         goto error;
593     }
594     p_subpic->p_sys = p_string;
595     p_string->i_flags = i_flags;
596     p_string->i_x_margin = i_hmargin;
597     p_string->i_y_margin = i_vmargin;
598     p_string->p_lines = 0;
599     p_string->psz_text = strdup( psz_string );
600
601     /* Calculate relative glyph positions and a bounding box for the
602      * entire string */
603     p_line = NewLine( psz_string );
604     if( p_line == NULL )
605     {
606         msg_Err( p_vout, "Out of memory" );
607         goto error;
608     }
609     p_string->p_lines = p_line;
610     i_pen_x = 0;
611     i_pen_y = 0;
612     i_previous = 0;
613     i = 0;
614
615 #define face p_vout->p_text_renderer_data->p_face
616 #define glyph face->glyph
617
618     while( *psz_string )
619     {
620         i_char = GetUnicodeCharFromUTF8( &psz_string );
621
622         if ( i_char == '\r' ) /* ignore CR chars wherever they may be */
623         {
624             continue;
625         }
626
627         if ( i_char == '\n' )
628         {
629             p_next = NewLine( psz_string );
630             if( p_next == NULL )
631             {
632                 msg_Err( p_vout, "Out of memory" );
633                 goto error;
634             }
635             p_line->p_next = p_next;
636             p_line->i_width = line.xMax;
637             p_line->i_height = face->size->metrics.height >> 6;
638             p_line->pp_glyphs[ i ] = NULL;
639             p_line = p_next;
640             result.x = __MAX( result.x, line.xMax );
641             result.y += face->size->metrics.height >> 6;
642             i_pen_x = 0;
643             line.xMin = 0;
644             line.xMax = 0;
645             line.yMin = 0;
646             line.yMax = 0;
647             i_pen_y += face->size->metrics.height >> 6;
648             msg_Dbg( p_vout, "Creating new line, i is %d", i );
649             i = 0;
650             continue;
651         }
652
653         i_glyph_index = FT_Get_Char_Index( face, i_char );
654         if ( p_vout->p_text_renderer_data->i_use_kerning && i_glyph_index
655             && i_previous )
656         {
657             FT_Vector delta;
658             FT_Get_Kerning( face, i_previous, i_glyph_index,
659                             ft_kerning_default, &delta );
660             i_pen_x += delta.x >> 6;
661
662         }
663         p_line->p_glyph_pos[ i ].x = i_pen_x;
664         p_line->p_glyph_pos[ i ].y = i_pen_y;
665         i_error = FT_Load_Glyph( face, i_glyph_index, FT_LOAD_DEFAULT );
666         if ( i_error )
667         {
668             msg_Err( p_vout, "FT_Load_Glyph returned %d", i_error );
669             goto error;
670         }
671         i_error = FT_Get_Glyph( glyph, &tmp_glyph );
672         if ( i_error )
673         {
674             msg_Err( p_vout, "FT_Get_Glyph returned %d", i_error );
675             goto error;
676         }
677         FT_Glyph_Get_CBox( tmp_glyph, ft_glyph_bbox_pixels, &glyph_size );
678         i_error = FT_Glyph_To_Bitmap( &tmp_glyph, ft_render_mode_normal,
679                                       NULL, 1 );
680         if ( i_error ) continue;
681         p_line->pp_glyphs[ i ] = (FT_BitmapGlyph)tmp_glyph;
682
683         /* Do rest */
684         line.xMax = p_line->p_glyph_pos[i].x + glyph_size.xMax - glyph_size.xMin;
685         line.yMax = __MAX( line.yMax, glyph_size.yMax );
686         line.yMin = __MIN( line.yMin, glyph_size.yMin );
687
688         i_previous = i_glyph_index;
689         i_pen_x += glyph->advance.x >> 6;
690         i++;
691     }
692     p_line->i_width = line.xMax;
693     p_line->i_height = face->size->metrics.height >> 6;
694     p_line->pp_glyphs[ i ] = NULL;
695     result.x = __MAX( result.x, line.xMax );
696     result.y += line.yMax - line.yMin;
697     p_string->i_height = result.y;
698     p_string->i_width = result.x;
699     msg_Dbg( p_vout, "string height is %d, width is %d",
700              p_string->i_height, p_string->i_width );
701     msg_Dbg( p_vout, "adding string \"%s\" at (%d,%d) start_date "I64Fd
702              " end_date" I64Fd, p_string->psz_text, p_string->i_x_margin,
703              p_string->i_y_margin, i_start, i_stop );
704     vout_DisplaySubPicture( p_vout, p_subpic );
705     return VLC_SUCCESS;
706
707 #undef face
708 #undef glyph
709
710  error:
711     FreeString( p_subpic );
712     vout_DestroySubPicture( p_vout, p_subpic );
713     return VLC_EGENERIC;
714 }
715
716 static void FreeString( subpicture_t *p_subpic )
717 {
718     unsigned int i;
719     subpicture_sys_t *p_string = p_subpic->p_sys;
720     line_desc_t *p_line, *p_next;
721
722     if( p_subpic->p_sys == NULL ) return;
723
724     for( p_line = p_string->p_lines; p_line != NULL; p_line = p_next )
725     {
726         p_next = p_line->p_next;
727         for( i = 0; p_line->pp_glyphs[ i ] != NULL; i++ )
728         {
729             FT_Done_Glyph( (FT_Glyph)p_line->pp_glyphs[ i ] );
730         }
731         free( p_line->pp_glyphs );
732         free( p_line->p_glyph_pos );
733         free( p_line );
734     }
735             
736     free( p_string->psz_text );
737     free( p_string );
738 }
739
740 /* convert one or more utf8 bytes into a unicode character */
741 static int GetUnicodeCharFromUTF8( byte_t **ppsz_utf8_string )
742 {
743     int i_remaining_bytes, i_char = 0;
744     if( ( **ppsz_utf8_string & 0xFC ) == 0xFC )
745     {
746         i_char = **ppsz_utf8_string & 1;
747         i_remaining_bytes = 5;
748     }
749     else if( ( **ppsz_utf8_string & 0xF8 ) == 0xF8 )
750     {
751         i_char = **ppsz_utf8_string & 3;
752         i_remaining_bytes = 4;
753     }
754     else if( ( **ppsz_utf8_string & 0xF0 ) == 0xF0 )
755     {
756         i_char = **ppsz_utf8_string & 7;
757         i_remaining_bytes = 3;
758     }
759     else if( ( **ppsz_utf8_string & 0xE0 ) == 0xE0 )
760     {
761         i_char = **ppsz_utf8_string & 15;
762         i_remaining_bytes = 2;
763     }
764     else if( ( **ppsz_utf8_string & 0xC0 ) == 0xC0 )
765     {
766         i_char = **ppsz_utf8_string & 31;
767         i_remaining_bytes = 1;
768     }
769     else
770     {
771         i_char = **ppsz_utf8_string;
772         i_remaining_bytes = 0;
773     }
774     while( i_remaining_bytes )
775     {
776         (*ppsz_utf8_string)++;
777         i_remaining_bytes--;
778         i_char = ( i_char << 6 ) + ( **ppsz_utf8_string & 0x3F );
779     }
780     (*ppsz_utf8_string)++;
781     return i_char;
782 }
783
784 static line_desc_t *NewLine( byte_t *psz_string )
785 {
786     int i_count;
787     line_desc_t *p_line = malloc( sizeof(line_desc_t) );
788     if( !p_line )
789     {
790         return NULL;
791     }
792     p_line->i_height = 0;
793     p_line->i_width = 0;
794     p_line->p_next = NULL;
795
796     /* We don't use CountUtf8Characters() here because we are not acutally
797      * sure the string is utf8. Better be safe than sorry. */
798     i_count = strlen( psz_string );
799
800     p_line->pp_glyphs = malloc( sizeof(FT_BitmapGlyph)
801                                 * ( i_count + 1 ) );
802     if( p_line->pp_glyphs == NULL )
803     {
804         free( p_line );
805         return NULL;
806     }
807     p_line->p_glyph_pos = malloc( sizeof( FT_Vector )
808                                   * i_count + 1 );
809     if( p_line->p_glyph_pos == NULL )
810     {
811         free( p_line->pp_glyphs );
812         free( p_line );
813         return NULL;
814     }
815
816     return p_line;
817 }