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