]> git.sesse.net Git - vlc/blob - modules/misc/freetype.c
Improvements to preferences
[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$
6  *
7  * Authors: Sigmund Augdal <sigmunau@idi.ntnu.no>
8  *          Gildas Bazin <gbazin@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>                                      /* malloc(), free() */
29 #include <string.h>
30
31 #ifdef HAVE_LINUX_LIMITS_H
32 #   include <linux/limits.h>
33 #endif
34
35 #include <vlc/vlc.h>
36 #include <vlc/vout.h>
37 #include "osd.h"
38 #include "vlc_block.h"
39 #include "vlc_filter.h"
40
41 #include <math.h>
42
43 #ifdef HAVE_ERRNO_H
44 #   include <errno.h>
45 #endif
46
47 #include <ft2build.h>
48 #include FT_FREETYPE_H
49 #include FT_GLYPH_H
50
51 #ifdef SYS_DARWIN
52 #define DEFAULT_FONT "/System/Library/Fonts/LucidaGrande.dfont"
53 #elif defined( SYS_BEOS )
54 #define DEFAULT_FONT "/boot/beos/etc/fonts/ttfonts/Swiss721.ttf"
55 #elif defined( WIN32 )
56 #define DEFAULT_FONT "" /* Default font found at run-time */
57 #else
58 #define DEFAULT_FONT "/usr/share/fonts/truetype/freefont/FreeSerifBold.ttf"
59 #endif
60
61 #if defined(HAVE_FRIBIDI)
62 #include <fribidi/fribidi.h>
63 #endif
64
65 typedef struct line_desc_t line_desc_t;
66
67 /*****************************************************************************
68  * Local prototypes
69  *****************************************************************************/
70 static int  Create ( vlc_object_t * );
71 static void Destroy( vlc_object_t * );
72
73 static subpicture_t *RenderText( filter_t *, block_t * );
74 static line_desc_t *NewLine( byte_t * );
75
76 /*****************************************************************************
77  * Module descriptor
78  *****************************************************************************/
79 #define FONT_TEXT N_("Font")
80 #define FONT_LONGTEXT N_("Font filename")
81 #define FONTSIZE_TEXT N_("Font size in pixels")
82 #define FONTSIZE_LONGTEXT N_("The size of the fonts used by the osd module. " \
83     "If set to something different than 0 this option will override the " \
84     "relative font size " )
85 #define FONTSIZER_TEXT N_("Font size")
86 #define FONTSIZER_LONGTEXT N_("The size of the fonts used by the osd module" )
87
88 static int   pi_sizes[] = { 20, 18, 16, 12, 6 };
89 static char *ppsz_sizes_text[] = { N_("Smaller"), N_("Small"), N_("Normal"),
90                                    N_("Large"), N_("Larger") };
91
92 vlc_module_begin();
93     set_description( _("freetype2 font renderer") );
94     set_category( CAT_VIDEO );
95     set_subcategory( SUBCAT_VIDEO_TEXT );
96
97     add_file( "freetype-font", DEFAULT_FONT, NULL, FONT_TEXT, FONT_LONGTEXT,
98               VLC_FALSE );
99     add_integer( "freetype-fontsize", 0, NULL, FONTSIZE_TEXT,
100                  FONTSIZE_LONGTEXT, VLC_TRUE );
101     add_integer( "freetype-rel-fontsize", 16, NULL, FONTSIZER_TEXT,
102                  FONTSIZER_LONGTEXT, VLC_FALSE );
103         change_integer_list( pi_sizes, ppsz_sizes_text, 0 );
104
105     set_capability( "text renderer", 100 );
106     add_shortcut( "text" );
107     set_callbacks( Create, Destroy );
108 vlc_module_end();
109
110 /**
111  * Private data in a subpicture. Describes a string.
112  */
113 typedef struct subpicture_data_t
114 {
115     int            i_width;
116     int            i_height;
117     /** The string associated with this subpicture */
118     byte_t        *psz_text;
119     line_desc_t   *p_lines;
120
121 } subpicture_data_t;
122
123 struct line_desc_t
124 {
125     /** NULL-terminated list of glyphs making the string */
126     FT_BitmapGlyph *pp_glyphs;
127     /** list of relative positions for the glyphs */
128     FT_Vector      *p_glyph_pos;
129     int             i_height;
130     int             i_width;
131     line_desc_t    *p_next;
132 };
133
134 static void Render    ( filter_t *, subpicture_t *, subpicture_data_t * );
135 static void FreeString( subpicture_data_t * );
136 static void FreeLine( line_desc_t * );
137
138 /*****************************************************************************
139  * filter_sys_t: freetype local data
140  *****************************************************************************
141  * This structure is part of the video output thread descriptor.
142  * It describes the freetype specific properties of an output thread.
143  *****************************************************************************/
144 struct filter_sys_t
145 {
146     FT_Library     p_library;   /* handle to library     */
147     FT_Face        p_face;      /* handle to face object */
148     vlc_bool_t     i_use_kerning;
149     uint8_t        pi_gamma[256];
150 };
151
152 /*****************************************************************************
153  * Create: allocates osd-text video thread output method
154  *****************************************************************************
155  * This function allocates and initializes a Clone vout method.
156  *****************************************************************************/
157 static int Create( vlc_object_t *p_this )
158 {
159     filter_t *p_filter = (filter_t *)p_this;
160     filter_sys_t *p_sys;
161     char *psz_fontfile = NULL;
162     int i, i_error;
163     int i_fontsize = 0;
164     vlc_value_t val;
165
166     /* Allocate structure */
167     p_sys = malloc( sizeof( filter_sys_t ) );
168     if( !p_sys )
169     {
170         msg_Err( p_filter, "out of memory" );
171         return VLC_ENOMEM;
172     }
173     p_sys->p_face = 0;
174     p_sys->p_library = 0;
175
176     for( i = 0; i < 256; i++ )
177     {
178         p_sys->pi_gamma[i] = (uint8_t)( pow( (double)i * 255.0f, 0.5f ) );
179     }
180
181     var_Create( p_filter, "freetype-font",
182                 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
183     var_Create( p_filter, "freetype-fontsize",
184                 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
185     var_Create( p_filter, "freetype-rel-fontsize",
186                 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
187
188     /* Look what method was requested */
189     var_Get( p_filter, "freetype-font", &val );
190     psz_fontfile = val.psz_string;
191     if( !psz_fontfile || !*psz_fontfile )
192     {
193         if( psz_fontfile ) free( psz_fontfile );
194         psz_fontfile = (char *)malloc( PATH_MAX + 1 );
195 #ifdef WIN32
196         GetWindowsDirectory( psz_fontfile, PATH_MAX + 1 );
197         strcat( psz_fontfile, "\\fonts\\arial.ttf" );
198 #elif SYS_DARWIN
199         strcpy( psz_fontfile, DEFAULT_FONT );
200 #else
201         msg_Err( p_filter, "user didn't specify a font" );
202         goto error;
203 #endif
204     }
205
206     i_error = FT_Init_FreeType( &p_sys->p_library );
207     if( i_error )
208     {
209         msg_Err( p_filter, "couldn't initialize freetype" );
210         goto error;
211     }
212
213     i_error = FT_New_Face( p_sys->p_library, psz_fontfile ? psz_fontfile : "",
214                            0, &p_sys->p_face );
215     if( i_error == FT_Err_Unknown_File_Format )
216     {
217         msg_Err( p_filter, "file %s have unknown format", psz_fontfile );
218         goto error;
219     }
220     else if( i_error )
221     {
222         msg_Err( p_filter, "failed to load font file %s", psz_fontfile );
223         goto error;
224     }
225
226     i_error = FT_Select_Charmap( p_sys->p_face, ft_encoding_unicode );
227     if( i_error )
228     {
229         msg_Err( p_filter, "Font has no unicode translation table" );
230         goto error;
231     }
232
233     p_sys->i_use_kerning = FT_HAS_KERNING( p_sys->p_face );
234
235     var_Get( p_filter, "freetype-fontsize", &val );
236     if( val.i_int )
237     {
238         i_fontsize = val.i_int;
239     }
240     else
241     {
242         var_Get( p_filter, "freetype-rel-fontsize", &val );
243         i_fontsize = (int)p_filter->fmt_out.video.i_height / val.i_int;
244     }
245     if( i_fontsize <= 0 )
246     {
247         msg_Warn( p_filter, "Invalid fontsize, using 12" );
248         i_fontsize = 12;
249     }
250     msg_Dbg( p_filter, "Using fontsize: %i", i_fontsize);
251
252     i_error = FT_Set_Pixel_Sizes( p_sys->p_face, 0, i_fontsize );
253     if( i_error )
254     {
255         msg_Err( p_filter, "couldn't set font size to %d", i_fontsize );
256         goto error;
257     }
258
259     if( psz_fontfile ) free( psz_fontfile );
260     p_filter->pf_render_string = RenderText;
261     p_filter->p_sys = p_sys;
262     return VLC_SUCCESS;
263
264  error:
265     if( p_sys->p_face ) FT_Done_Face( p_sys->p_face );
266     if( p_sys->p_library ) FT_Done_FreeType( p_sys->p_library );
267     if( psz_fontfile ) free( psz_fontfile );
268     free( p_sys );
269     return VLC_EGENERIC;
270 }
271
272 /*****************************************************************************
273  * Destroy: destroy Clone video thread output method
274  *****************************************************************************
275  * Clean up all data and library connections
276  *****************************************************************************/
277 static void Destroy( vlc_object_t *p_this )
278 {
279     filter_t *p_filter = (filter_t *)p_this;
280     filter_sys_t *p_sys = p_filter->p_sys;
281     FT_Done_Face( p_sys->p_face );
282     FT_Done_FreeType( p_sys->p_library );
283     free( p_sys );
284 }
285
286 /*****************************************************************************
287  * Render: place string in picture
288  *****************************************************************************
289  * This function merges the previously rendered freetype glyphs into a picture
290  *****************************************************************************/
291 static void Render( filter_t *p_filter, subpicture_t *p_spu,
292                     subpicture_data_t *p_string )
293 {
294     filter_sys_t *p_sys = p_filter->p_sys;
295     line_desc_t *p_line;
296     uint8_t *p_y, *p_u, *p_v, *p_a;
297     video_format_t fmt;
298     int i, x, y, i_pitch;
299
300     /* Create a new subpicture region */
301     memset( &fmt, 0, sizeof(video_format_t) );
302     fmt.i_chroma = VLC_FOURCC('Y','U','V','A');
303     fmt.i_aspect = VOUT_ASPECT_FACTOR;
304     fmt.i_width = fmt.i_visible_width = p_string->i_width + 2;
305     fmt.i_height = fmt.i_visible_height = p_string->i_height + 2;
306     fmt.i_x_offset = fmt.i_y_offset = 0;
307     p_spu->p_region = p_spu->pf_create_region( VLC_OBJECT(p_filter), &fmt );
308     if( !p_spu->p_region )
309     {
310         msg_Err( p_filter, "cannot allocate SPU region" );
311         return;
312     }
313
314     p_spu->p_region->i_x = p_spu->p_region->i_y = 0;
315     p_y = p_spu->p_region->picture.Y_PIXELS;
316     p_u = p_spu->p_region->picture.U_PIXELS;
317     p_v = p_spu->p_region->picture.V_PIXELS;
318     p_a = p_spu->p_region->picture.A_PIXELS;
319     i_pitch = p_spu->p_region->picture.Y_PITCH;
320
321     /* Initialize the region pixels (only the alpha will be changed later) */
322     memset( p_y, 0x00, i_pitch * p_spu->p_region->fmt.i_height );
323     memset( p_u, 0x80, i_pitch * p_spu->p_region->fmt.i_height );
324     memset( p_v, 0x80, i_pitch * p_spu->p_region->fmt.i_height );
325     memset( p_a, 0x00, i_pitch * p_spu->p_region->fmt.i_height );
326
327 #define pi_gamma p_sys->pi_gamma
328
329     for( p_line = p_string->p_lines; p_line != NULL; p_line = p_line->p_next )
330     {
331         int i_glyph_tmax = 0;
332         int i_bitmap_offset, i_offset;
333         for( i = 0; p_line->pp_glyphs[i] != NULL; i++ )
334         {
335             FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ];
336             i_glyph_tmax = __MAX( i_glyph_tmax, p_glyph->top );
337         }
338
339         for( i = 0; p_line->pp_glyphs[i] != NULL; i++ )
340         {
341             FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ];
342
343             i_offset = ( p_line->p_glyph_pos[ i ].y +
344                 i_glyph_tmax - p_glyph->top + 1 ) *
345                 i_pitch + p_line->p_glyph_pos[ i ].x + p_glyph->left + 1;
346
347             for( y = 0, i_bitmap_offset = 0; y < p_glyph->bitmap.rows; y++ )
348             {
349                 for( x = 0; x < p_glyph->bitmap.width; x++, i_bitmap_offset++ )
350                 {
351                     if( !pi_gamma[p_glyph->bitmap.buffer[i_bitmap_offset]] )
352                         continue;
353
354                     i_offset -= i_pitch;
355                     p_a[i_offset + x] = ((uint16_t)p_a[i_offset + x] +
356                       pi_gamma[p_glyph->bitmap.buffer[i_bitmap_offset]])/2;
357                     i_offset += i_pitch; x--;
358                     p_a[i_offset + x] = ((uint16_t)p_a[i_offset + x] +
359                       pi_gamma[p_glyph->bitmap.buffer[i_bitmap_offset]])/2;
360                     x += 2;
361                     p_a[i_offset + x] = ((uint16_t)p_a[i_offset + x] +
362                       pi_gamma[p_glyph->bitmap.buffer[i_bitmap_offset]])/2;
363                     i_offset += i_pitch; x--;
364                     p_a[i_offset + x] = ((uint16_t)p_a[i_offset + x] +
365                       pi_gamma[p_glyph->bitmap.buffer[i_bitmap_offset]])/2;
366                     i_offset -= i_pitch;
367                 }
368                 i_offset += i_pitch;
369             }
370
371             i_offset = ( p_line->p_glyph_pos[ i ].y +
372                 i_glyph_tmax - p_glyph->top + 1 ) *
373                 i_pitch + p_line->p_glyph_pos[ i ].x + p_glyph->left + 1;
374
375             for( y = 0, i_bitmap_offset = 0; y < p_glyph->bitmap.rows; y++ )
376             {
377                for( x = 0; x < p_glyph->bitmap.width; x++, i_bitmap_offset++ )
378                {
379                    p_y[i_offset + x] =
380                        pi_gamma[p_glyph->bitmap.buffer[i_bitmap_offset]];
381                }
382                i_offset += i_pitch;
383             }
384
385 #undef pi_gamma
386         }
387     }
388 }
389
390 /**
391  * This function receives a string and creates a subpicture for it. It
392  * also calculates the size needed for this string, and renders the
393  * needed glyphs into memory. It is used as pf_add_string callback in
394  * the vout method by this module
395  */
396 static subpicture_t *RenderText( filter_t *p_filter, block_t *p_block )
397 {
398     filter_sys_t *p_sys = p_filter->p_sys;
399     subpicture_t *p_subpic = 0;
400     subpicture_data_t *p_string = 0;
401     line_desc_t  *p_line = 0, *p_next = 0, *p_prev = 0;
402     int i, i_pen_y, i_pen_x, i_error, i_glyph_index, i_previous;
403     uint32_t *psz_unicode, *psz_unicode_orig = 0, i_char, *psz_line_start;
404     int i_string_length;
405     char *psz_string;
406     vlc_iconv_t iconv_handle = (vlc_iconv_t)(-1);
407
408     FT_BBox line;
409     FT_BBox glyph_size;
410     FT_Vector result;
411     FT_Glyph tmp_glyph;
412
413     /* Sanity check */
414     if( !p_block ) return NULL;
415     psz_string = p_block->p_buffer;
416     if( !psz_string || !*psz_string ) goto error;
417
418     result.x = 0;
419     result.y = 0;
420     line.xMin = 0;
421     line.xMax = 0;
422     line.yMin = 0;
423     line.yMax = 0;
424
425     /* Create and initialize a subpicture */
426     p_subpic = p_filter->pf_sub_buffer_new( p_filter );
427     if( !p_subpic ) goto error;
428
429     p_subpic->i_start = p_block->i_pts;
430     p_subpic->i_stop = p_block->i_pts + p_block->i_length;
431     p_subpic->b_ephemer = (p_block->i_length == 0);
432     p_subpic->b_absolute = VLC_FALSE;
433
434     /* Create and initialize private data for the subpicture */
435     p_string = malloc( sizeof(subpicture_data_t) );
436     if( !p_string )
437     {
438         msg_Err( p_filter, "out of memory" );
439         goto error;
440     }
441     p_string->p_lines = 0;
442     p_string->psz_text = strdup( psz_string );
443
444     psz_unicode = psz_unicode_orig =
445         malloc( ( strlen(psz_string) + 1 ) * sizeof(uint32_t) );
446     if( psz_unicode == NULL )
447     {
448         msg_Err( p_filter, "out of memory" );
449         goto error;
450     }
451 #if defined(WORDS_BIGENDIAN)
452     iconv_handle = vlc_iconv_open( "UCS-4BE", "UTF-8" );
453 #else
454     iconv_handle = vlc_iconv_open( "UCS-4LE", "UTF-8" );
455 #endif
456     if( iconv_handle == (vlc_iconv_t)-1 )
457     {
458         msg_Warn( p_filter, "unable to do convertion" );
459         goto error;
460     }
461
462     {
463         char *p_in_buffer, *p_out_buffer;
464         size_t i_in_bytes, i_out_bytes, i_out_bytes_left, i_ret;
465         i_in_bytes = strlen( psz_string );
466         i_out_bytes = i_in_bytes * sizeof( uint32_t );
467         i_out_bytes_left = i_out_bytes;
468         p_in_buffer = psz_string;
469         p_out_buffer = (char *)psz_unicode;
470         i_ret = vlc_iconv( iconv_handle, &p_in_buffer, &i_in_bytes,
471                            &p_out_buffer, &i_out_bytes_left );
472
473         vlc_iconv_close( iconv_handle );
474
475         if( i_in_bytes )
476         {
477             msg_Warn( p_filter, "failed to convert string to unicode (%s), "
478                       "bytes left %d", strerror(errno), i_in_bytes );
479             goto error;
480         }
481         *(uint32_t*)p_out_buffer = 0;
482         i_string_length = (i_out_bytes - i_out_bytes_left) / sizeof(uint32_t);
483     }
484
485 #if defined(HAVE_FRIBIDI)
486     {
487         uint32_t *p_fribidi_string;
488         FriBidiCharType base_dir = FRIBIDI_TYPE_ON;
489         p_fribidi_string = malloc( (i_string_length + 1) * sizeof(uint32_t) );
490         fribidi_log2vis( (FriBidiChar*)psz_unicode, i_string_length,
491                          &base_dir, (FriBidiChar*)p_fribidi_string, 0, 0, 0 );
492         free( psz_unicode_orig );
493         psz_unicode = psz_unicode_orig = p_fribidi_string;
494         p_fribidi_string[ i_string_length ] = 0;
495     }
496 #endif
497
498     /* Calculate relative glyph positions and a bounding box for the
499      * entire string */
500     p_line = NewLine( psz_string );
501     if( p_line == NULL )
502     {
503         msg_Err( p_filter, "out of memory" );
504         goto error;
505     }
506     p_string->p_lines = p_line;
507     i_pen_x = 0;
508     i_pen_y = 0;
509     i_previous = 0;
510     i = 0;
511     psz_line_start = psz_unicode;
512
513 #define face p_sys->p_face
514 #define glyph face->glyph
515
516     while( *psz_unicode )
517     {
518         i_char = *psz_unicode++;
519         if( i_char == '\r' ) /* ignore CR chars wherever they may be */
520         {
521             continue;
522         }
523
524         if( i_char == '\n' )
525         {
526             psz_line_start = psz_unicode;
527             p_next = NewLine( psz_string );
528             if( p_next == NULL )
529             {
530                 msg_Err( p_filter, "out of memory" );
531                 goto error;
532             }
533             p_line->p_next = p_next;
534             p_line->i_width = line.xMax;
535             p_line->i_height = face->size->metrics.height >> 6;
536             p_line->pp_glyphs[ i ] = NULL;
537             p_prev = p_line;
538             p_line = p_next;
539             result.x = __MAX( result.x, line.xMax );
540             result.y += face->size->metrics.height >> 6;
541             i_pen_x = 0;
542             i_previous = 0;
543             line.xMin = 0;
544             line.xMax = 0;
545             line.yMin = 0;
546             line.yMax = 0;
547             i_pen_y += face->size->metrics.height >> 6;
548 #if 0
549             msg_Dbg( p_filter, "Creating new line, i is %d", i );
550 #endif
551             i = 0;
552             continue;
553         }
554
555         i_glyph_index = FT_Get_Char_Index( face, i_char );
556         if( p_sys->i_use_kerning && i_glyph_index
557             && i_previous )
558         {
559             FT_Vector delta;
560             FT_Get_Kerning( face, i_previous, i_glyph_index,
561                             ft_kerning_default, &delta );
562             i_pen_x += delta.x >> 6;
563
564         }
565         p_line->p_glyph_pos[ i ].x = i_pen_x;
566         p_line->p_glyph_pos[ i ].y = i_pen_y;
567         i_error = FT_Load_Glyph( face, i_glyph_index, FT_LOAD_DEFAULT );
568         if( i_error )
569         {
570             msg_Err( p_filter, "FT_Load_Glyph returned %d", i_error );
571             goto error;
572         }
573         i_error = FT_Get_Glyph( glyph, &tmp_glyph );
574         if( i_error )
575         {
576             msg_Err( p_filter, "FT_Get_Glyph returned %d", i_error );
577             goto error;
578         }
579         FT_Glyph_Get_CBox( tmp_glyph, ft_glyph_bbox_pixels, &glyph_size );
580         i_error = FT_Glyph_To_Bitmap( &tmp_glyph, ft_render_mode_normal,
581                                       NULL, 1 );
582         if( i_error ) continue;
583         p_line->pp_glyphs[ i ] = (FT_BitmapGlyph)tmp_glyph;
584
585         /* Do rest */
586         line.xMax = p_line->p_glyph_pos[i].x + glyph_size.xMax - glyph_size.xMin + ((FT_BitmapGlyph)tmp_glyph)->left;
587         if( line.xMax > p_filter->fmt_out.video.i_visible_width - 20 )
588         {
589             p_line->pp_glyphs[ i ] = NULL;
590             FreeLine( p_line );
591             p_line = NewLine( psz_string );
592             if( p_prev )
593             {
594                 p_prev->p_next = p_line;
595             }
596             else
597             {
598                 p_string->p_lines = p_line;
599             }
600             while( psz_unicode > psz_line_start && *psz_unicode != ' ' )
601             {
602                 psz_unicode--;
603             }
604             if( psz_unicode == psz_line_start )
605             {
606                 msg_Warn( p_filter, "unbreakable string" );
607                 goto error;
608             }
609             else
610             {
611
612                 *psz_unicode = '\n';
613             }
614             psz_unicode = psz_line_start;
615             i_pen_x = 0;
616             i_previous = 0;
617             line.xMin = 0;
618             line.xMax = 0;
619             line.yMin = 0;
620             line.yMax = 0;
621             i = 0;
622             continue;
623         }
624         line.yMax = __MAX( line.yMax, glyph_size.yMax );
625         line.yMin = __MIN( line.yMin, glyph_size.yMin );
626
627         i_previous = i_glyph_index;
628         i_pen_x += glyph->advance.x >> 6;
629         i++;
630     }
631
632     p_line->i_width = line.xMax;
633     p_line->i_height = face->size->metrics.height >> 6;
634     p_line->pp_glyphs[ i ] = NULL;
635     result.x = __MAX( result.x, line.xMax );
636     result.y += line.yMax - line.yMin;
637     p_string->i_height = result.y;
638     p_string->i_width = result.x;
639
640 #undef face
641 #undef glyph
642
643     Render( p_filter, p_subpic, p_string );
644     FreeString( p_string );
645     block_Release( p_block );
646     if( psz_unicode_orig ) free( psz_unicode_orig );
647     return p_subpic;
648
649  error:
650     FreeString( p_string );
651     if( p_subpic ) p_filter->pf_sub_buffer_del( p_filter, p_subpic );
652     block_Release( p_block );
653     if( psz_unicode_orig ) free( psz_unicode_orig );
654     return NULL;
655 }
656
657 static void FreeLine( line_desc_t *p_line )
658 {
659     unsigned int i;
660     for( i = 0; p_line->pp_glyphs[ i ] != NULL; i++ )
661     {
662         FT_Done_Glyph( (FT_Glyph)p_line->pp_glyphs[ i ] );
663     }
664     free( p_line->pp_glyphs );
665     free( p_line->p_glyph_pos );
666     free( p_line );
667 }
668
669 static void FreeString( subpicture_data_t *p_string )
670 {
671     line_desc_t *p_line, *p_next;
672
673     if( !p_string ) return;
674
675     for( p_line = p_string->p_lines; p_line != NULL; p_line = p_next )
676     {
677         p_next = p_line->p_next;
678         FreeLine( p_line );
679     }
680
681     free( p_string->psz_text );
682     free( p_string );
683 }
684
685 static line_desc_t *NewLine( byte_t *psz_string )
686 {
687     int i_count;
688     line_desc_t *p_line = malloc( sizeof(line_desc_t) );
689     if( !p_line )
690     {
691         return NULL;
692     }
693     p_line->i_height = 0;
694     p_line->i_width = 0;
695     p_line->p_next = NULL;
696
697     /* We don't use CountUtf8Characters() here because we are not acutally
698      * sure the string is utf8. Better be safe than sorry. */
699     i_count = strlen( psz_string );
700
701     p_line->pp_glyphs = malloc( sizeof(FT_BitmapGlyph)
702                                 * ( i_count + 1 ) );
703     if( p_line->pp_glyphs == NULL )
704     {
705         free( p_line );
706         return NULL;
707     }
708     p_line->pp_glyphs[0] = NULL;
709
710     p_line->p_glyph_pos = malloc( sizeof( FT_Vector )
711                                   * i_count + 1 );
712     if( p_line->p_glyph_pos == NULL )
713     {
714         free( p_line->pp_glyphs );
715         free( p_line );
716         return NULL;
717     }
718
719     return p_line;
720 }