]> git.sesse.net Git - vlc/blob - modules/misc/freetype.c
* include/vlc_filter.h, vlc_video.h, src/video_output/vout_subpictures.c: new simpler...
[vlc] / modules / misc / freetype.c
1 /*****************************************************************************
2  * freetype.c : Put text on the video, using freetype2
3  *****************************************************************************
4  * Copyright (C) 2002 - 2005 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 /* The RenderText call maps to pf_render_string, defined in vlc_filter.h */
74 static int RenderText( filter_t *, subpicture_region_t *,
75                        subpicture_region_t * );
76 static line_desc_t *NewLine( byte_t * );
77
78 /*****************************************************************************
79  * Module descriptor
80  *****************************************************************************/
81 #define FONT_TEXT N_("Font")
82 #define FONT_LONGTEXT N_("Font filename")
83 #define FONTSIZE_TEXT N_("Font size in pixels")
84 #define FONTSIZE_LONGTEXT N_("The size of the fonts used by the osd module. " \
85     "If set to something different than 0 this option will override the " \
86     "relative font size " )
87 #define OPACITY_TEXT N_("Opacity, 0..255")
88 #define OPACITY_LONGTEXT N_("The opacity (inverse of transparency) of " \
89     "overlay text. 0 = transparent, 255 = totally opaque. " )
90 #define COLOR_TEXT N_("Text Default Color")
91 #define COLOR_LONGTEXT N_("The color of overlay text. 1 byte for each color, "\
92     "hexadecimal. #000000 = all colors off, 0xFF0000 = just Red, " \
93     "0xFFFFFF = all color on [White]" )
94 #define FONTSIZER_TEXT N_("Font size")
95 #define FONTSIZER_LONGTEXT N_("The size of the fonts used by the osd module" )
96
97 static int   pi_sizes[] = { 20, 18, 16, 12, 6 };
98 static char *ppsz_sizes_text[] = { N_("Smaller"), N_("Small"), N_("Normal"),
99                                    N_("Large"), N_("Larger") };
100 static int pi_color_values[] = {
101   0x00000000, 0x00808080, 0x00C0C0C0, 0x00FFFFFF, 0x00800000,
102   0x00FF0000, 0x00FF00FF, 0x00FFFF00, 0x00808000, 0x00008000, 0x00008080, 
103   0x0000FF00, 0x00800080, 0x00000080, 0x000000FF, 0x0000FFFF }; 
104
105 static char *ppsz_color_descriptions[] = {
106   N_("Black"), N_("Gray"), N_("Silver"), N_("White"), N_("Maroon"),
107   N_("Red"), N_("Fuchsia"), N_("Yellow"), N_("Olive"), N_("Green"), N_("Teal"),
108   N_("Lime"), N_("Purple"), N_("Navy"), N_("Blue"), N_("Aqua") };
109
110 vlc_module_begin();
111     set_shortname( _("Freetype"));
112     set_description( _("Freetype2 font renderer") );
113     set_category( CAT_VIDEO );
114     set_subcategory( SUBCAT_VIDEO_TEXT );
115
116     add_file( "freetype-font", DEFAULT_FONT, NULL, FONT_TEXT, FONT_LONGTEXT,
117               VLC_FALSE );
118
119     add_integer( "freetype-fontsize", 0, NULL, FONTSIZE_TEXT,
120                  FONTSIZE_LONGTEXT, VLC_TRUE );
121
122     /* opacity valid on 0..255, with default 255 = fully opaque */
123     add_integer_with_range( "freetype-opacity", 255, 0, 255, NULL,
124         OPACITY_TEXT, OPACITY_LONGTEXT, VLC_FALSE );
125
126     /* hook to the color values list, with default 0x00ffffff = white */
127     add_integer( "freetype-color", 0x00FFFFFF, NULL, COLOR_TEXT,
128                  COLOR_LONGTEXT, VLC_TRUE );
129         change_integer_list( pi_color_values, ppsz_color_descriptions, 0 );
130
131     add_integer( "freetype-rel-fontsize", 16, NULL, FONTSIZER_TEXT,
132                  FONTSIZER_LONGTEXT, VLC_FALSE );
133         change_integer_list( pi_sizes, ppsz_sizes_text, 0 );
134
135     set_capability( "text renderer", 100 );
136     add_shortcut( "text" );
137     set_callbacks( Create, Destroy );
138 vlc_module_end();
139
140 struct line_desc_t
141 {
142     /** NULL-terminated list of glyphs making the string */
143     FT_BitmapGlyph *pp_glyphs;
144     /** list of relative positions for the glyphs */
145     FT_Vector      *p_glyph_pos;
146
147     int             i_height;
148     int             i_width;
149     int             i_red, i_green, i_blue;
150     int             i_alpha;
151
152     line_desc_t    *p_next;
153 };
154
155 static int Render( filter_t *, subpicture_region_t *, line_desc_t *, int, int);
156 static void FreeLines( line_desc_t * );
157 static void FreeLine( line_desc_t * );
158
159 /*****************************************************************************
160  * filter_sys_t: freetype local data
161  *****************************************************************************
162  * This structure is part of the video output thread descriptor.
163  * It describes the freetype specific properties of an output thread.
164  *****************************************************************************/
165 struct filter_sys_t
166 {
167     FT_Library     p_library;   /* handle to library     */
168     FT_Face        p_face;      /* handle to face object */
169     vlc_bool_t     i_use_kerning;
170     uint8_t        i_font_opacity;
171     int            i_font_color;
172     int            i_font_size;
173 };
174
175 /*****************************************************************************
176  * Create: allocates osd-text video thread output method
177  *****************************************************************************
178  * This function allocates and initializes a Clone vout method.
179  *****************************************************************************/
180 static int Create( vlc_object_t *p_this )
181 {
182     filter_t *p_filter = (filter_t *)p_this;
183     filter_sys_t *p_sys;
184     char *psz_fontfile = NULL;
185     int i_error;
186     vlc_value_t val;
187
188     /* Allocate structure */
189     p_sys = malloc( sizeof( filter_sys_t ) );
190     if( !p_sys )
191     {
192         msg_Err( p_filter, "out of memory" );
193         return VLC_ENOMEM;
194     }
195     p_sys->p_face = 0;
196     p_sys->p_library = 0;
197     p_sys->i_font_size = 0;
198
199     var_Create( p_filter, "freetype-font",
200                 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
201     var_Create( p_filter, "freetype-fontsize",
202                 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
203     var_Create( p_filter, "freetype-rel-fontsize",
204                 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
205     var_Create( p_filter, "freetype-opacity",
206                 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
207     var_Get( p_filter, "freetype-opacity", &val );
208     p_sys->i_font_opacity = __MAX( __MIN( val.i_int, 255 ), 0 );
209     var_Create( p_filter, "freetype-color",
210                 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
211     var_Get( p_filter, "freetype-color", &val );
212     p_sys->i_font_color = __MIN( val.i_int, 0xFFFFFF );
213
214     /* Look what method was requested */
215     var_Get( p_filter, "freetype-font", &val );
216     psz_fontfile = val.psz_string;
217     if( !psz_fontfile || !*psz_fontfile )
218     {
219         if( psz_fontfile ) free( psz_fontfile );
220         psz_fontfile = (char *)malloc( PATH_MAX + 1 );
221 #ifdef WIN32
222         GetWindowsDirectory( psz_fontfile, PATH_MAX + 1 );
223         strcat( psz_fontfile, "\\fonts\\arial.ttf" );
224 #elif SYS_DARWIN
225         strcpy( psz_fontfile, DEFAULT_FONT );
226 #else
227         msg_Err( p_filter, "user didn't specify a font" );
228         goto error;
229 #endif
230     }
231
232     i_error = FT_Init_FreeType( &p_sys->p_library );
233     if( i_error )
234     {
235         msg_Err( p_filter, "couldn't initialize freetype" );
236         goto error;
237     }
238
239     i_error = FT_New_Face( p_sys->p_library, psz_fontfile ? psz_fontfile : "",
240                            0, &p_sys->p_face );
241     if( i_error == FT_Err_Unknown_File_Format )
242     {
243         msg_Err( p_filter, "file %s have unknown format", psz_fontfile );
244         goto error;
245     }
246     else if( i_error )
247     {
248         msg_Err( p_filter, "failed to load font file %s", psz_fontfile );
249         goto error;
250     }
251
252     i_error = FT_Select_Charmap( p_sys->p_face, ft_encoding_unicode );
253     if( i_error )
254     {
255         msg_Err( p_filter, "Font has no unicode translation table" );
256         goto error;
257     }
258
259     p_sys->i_use_kerning = FT_HAS_KERNING( p_sys->p_face );
260
261     
262     var_Get( p_filter, "freetype-fontsize", &val );
263     if( val.i_int )
264     {
265         p_sys->i_font_size = val.i_int;
266     }
267     else
268     {
269         var_Get( p_filter, "freetype-rel-fontsize", &val );
270         p_sys->i_font_size = (int)p_filter->fmt_out.video.i_height / val.i_int;
271     }
272     if( p_sys->i_font_size <= 0 )
273     {
274         msg_Warn( p_filter, "Invalid fontsize, using 12" );
275         p_sys->i_font_size = 12;
276     }
277     msg_Dbg( p_filter, "Using fontsize: %i", p_sys->i_font_size );
278
279     i_error = FT_Set_Pixel_Sizes( p_sys->p_face, 0, p_sys->i_font_size );
280     if( i_error )
281     {
282         msg_Err( p_filter, "couldn't set font size to %d", p_sys->i_font_size );
283         goto error;
284     }
285
286     if( psz_fontfile ) free( psz_fontfile );
287     p_filter->pf_render_text = RenderText;
288     p_filter->p_sys = p_sys;
289     return VLC_SUCCESS;
290
291  error:
292     if( p_sys->p_face ) FT_Done_Face( p_sys->p_face );
293     if( p_sys->p_library ) FT_Done_FreeType( p_sys->p_library );
294     if( psz_fontfile ) free( psz_fontfile );
295     free( p_sys );
296     return VLC_EGENERIC;
297 }
298
299 /*****************************************************************************
300  * Destroy: destroy Clone video thread output method
301  *****************************************************************************
302  * Clean up all data and library connections
303  *****************************************************************************/
304 static void Destroy( vlc_object_t *p_this )
305 {
306     filter_t *p_filter = (filter_t *)p_this;
307     filter_sys_t *p_sys = p_filter->p_sys;
308
309     FT_Done_Face( p_sys->p_face );
310     FT_Done_FreeType( p_sys->p_library );
311     free( p_sys );
312 }
313
314 /*****************************************************************************
315  * Render: place string in picture
316  *****************************************************************************
317  * This function merges the previously rendered freetype glyphs into a picture
318  *****************************************************************************/
319 static int Render( filter_t *p_filter, subpicture_region_t *p_region,
320                    line_desc_t *p_line, int i_width, int i_height )
321 {
322     uint8_t *p_dst;
323     video_format_t fmt;
324     int i, x, y, i_pitch;
325     uint8_t i_y; /* YUV values, derived from incoming RGB */
326     int8_t i_u, i_v;
327     subpicture_region_t *p_region_tmp;
328
329     /* Create a new subpicture region */
330     memset( &fmt, 0, sizeof(video_format_t) );
331     fmt.i_chroma = VLC_FOURCC('Y','U','V','P');
332     fmt.i_aspect = VOUT_ASPECT_FACTOR;
333     fmt.i_width = fmt.i_visible_width = i_width + 2;
334     fmt.i_height = fmt.i_visible_height = i_height + 2;
335     fmt.i_x_offset = fmt.i_y_offset = 0;
336     p_region_tmp = spu_CreateRegion( p_filter, &fmt );
337     if( !p_region_tmp )
338     {
339         msg_Err( p_filter, "cannot allocate SPU region" );
340         return VLC_EGENERIC;
341     }
342
343     p_region->fmt = p_region_tmp->fmt;
344     p_region->picture = p_region_tmp->picture;
345     free( p_region_tmp );
346
347     /* Calculate text color components */
348     i_y = (uint8_t)(( 66 * p_line->i_red  + 129 * p_line->i_green +
349                       25 * p_line->i_blue + 128) >> 8) +  16;
350     i_u = (int8_t)(( -38 * p_line->i_red  -  74 * p_line->i_green +
351                      112 * p_line->i_blue + 128) >> 8) + 128;
352     i_v = (int8_t)(( 112 * p_line->i_red  -  94 * p_line->i_green -
353                       18 * p_line->i_blue + 128) >> 8) + 128;
354
355     /* Build palette */
356     fmt.p_palette->i_entries = 256;
357     for( i = 0; i < fmt.p_palette->i_entries; i++ )
358     {
359         fmt.p_palette->palette[i][0] = i * i_y / 256;
360         fmt.p_palette->palette[i][1] = i_u;
361         fmt.p_palette->palette[i][2] = i_v;
362         fmt.p_palette->palette[i][3] =
363             i + (256 - i) * (256 / (255 - p_line->i_alpha)) / 16;
364     }
365     fmt.p_palette->palette[0][3] = 0;
366
367     p_dst = p_region->picture.Y_PIXELS;
368     i_pitch = p_region->picture.Y_PITCH;
369
370     /* Initialize the region pixels */
371     memset( p_dst, 0, i_pitch * p_region->fmt.i_height );
372
373     for( ; p_line != NULL; p_line = p_line->p_next )
374     {
375         int i_glyph_tmax = 0;
376         int i_bitmap_offset, i_offset;
377         for( i = 0; p_line->pp_glyphs[i] != NULL; i++ )
378         {
379             FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ];
380             i_glyph_tmax = __MAX( i_glyph_tmax, p_glyph->top );
381         }
382
383         for( i = 0; p_line->pp_glyphs[i] != NULL; i++ )
384         {
385             FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ];
386
387             i_offset = ( p_line->p_glyph_pos[ i ].y +
388                 i_glyph_tmax - p_glyph->top + 1 ) *
389                 i_pitch + p_line->p_glyph_pos[ i ].x + p_glyph->left + 1;
390
391             for( y = 0, i_bitmap_offset = 0; y < p_glyph->bitmap.rows; y++ )
392             {
393                 for( x = 0; x < p_glyph->bitmap.width; x++, i_bitmap_offset++ )
394                 {
395                     if( !p_glyph->bitmap.buffer[i_bitmap_offset] )
396                         continue;
397
398                     i_offset -= i_pitch;
399                     p_dst[i_offset + x] = ((uint16_t)p_dst[i_offset + x]*2 +
400                       p_glyph->bitmap.buffer[i_bitmap_offset])/3;
401                     i_offset += i_pitch; x--;
402                     p_dst[i_offset + x] = ((uint16_t)p_dst[i_offset + x]*2 +
403                       p_glyph->bitmap.buffer[i_bitmap_offset])/3;
404                     x += 2;
405                     p_dst[i_offset + x] = ((uint16_t)p_dst[i_offset + x]*2 +
406                       p_glyph->bitmap.buffer[i_bitmap_offset])/3;
407                     i_offset += i_pitch; x--;
408                     p_dst[i_offset + x] = ((uint16_t)p_dst[i_offset + x]*2 +
409                       p_glyph->bitmap.buffer[i_bitmap_offset])/3;
410                     i_offset -= i_pitch;
411                 }
412                 i_offset += i_pitch;
413             }
414
415             i_offset = ( p_line->p_glyph_pos[ i ].y +
416                 i_glyph_tmax - p_glyph->top + 1 ) *
417                 i_pitch + p_line->p_glyph_pos[ i ].x + p_glyph->left + 1;
418
419             for( y = 0, i_bitmap_offset = 0; y < p_glyph->bitmap.rows; y++ )
420             {
421                for( x = 0; x < p_glyph->bitmap.width; x++, i_bitmap_offset++ )
422                {
423                  if( p_glyph->bitmap.buffer[i_bitmap_offset] > 16 )
424                    p_dst[i_offset+x] = p_glyph->bitmap.buffer[i_bitmap_offset];
425                }
426                i_offset += i_pitch;
427             }
428         }
429     }
430
431     return VLC_SUCCESS;
432 }
433
434 /**
435  * This function renders a text subpicture region into another one.
436  * It also calculates the size needed for this string, and renders the
437  * needed glyphs into memory. It is used as pf_add_string callback in
438  * the vout method by this module
439  */
440 static int RenderText( filter_t *p_filter, subpicture_region_t *p_region_out,
441                        subpicture_region_t *p_region_in )
442 {
443     filter_sys_t *p_sys = p_filter->p_sys;
444     line_desc_t  *p_lines = 0, *p_line = 0, *p_next = 0, *p_prev = 0;
445     int i, i_pen_y, i_pen_x, i_error, i_glyph_index, i_previous;
446     uint32_t *psz_unicode, *psz_unicode_orig = 0, i_char, *psz_line_start;
447     int i_string_length;
448     char *psz_string;
449     vlc_iconv_t iconv_handle = (vlc_iconv_t)(-1);
450     int i_font_color, i_font_alpha, i_font_size, i_red, i_green, i_blue;
451
452     FT_BBox line;
453     FT_BBox glyph_size;
454     FT_Vector result;
455     FT_Glyph tmp_glyph;
456
457     /* Sanity check */
458     if( !p_region_in || !p_region_out ) return VLC_EGENERIC;
459     psz_string = p_region_in->psz_text;
460     if( !psz_string || !*psz_string ) return VLC_EGENERIC;
461
462     i_font_color = 0xFFFFFF;//p_region_in->i_text_color;
463     if( i_font_color == 0xFFFFFF ) i_font_color = p_sys->i_font_color;
464
465     i_font_alpha = p_region_in->i_text_alpha;
466     if( !i_font_alpha ) i_font_alpha = 255 - p_sys->i_font_opacity;
467
468     i_font_size  = p_region_in->i_text_size;
469     if( !i_font_size ) i_font_size  = p_sys->i_font_size;
470
471     i_red   = ( i_font_color & 0x00FF0000 ) >> 16;
472     i_green = ( i_font_color & 0x0000FF00 ) >>  8;
473     i_blue  =   i_font_color & 0x000000FF;
474
475     result.x =  result.y = 0;
476     line.xMin = line.xMax = line.yMin = line.yMax = 0;
477
478     psz_unicode = psz_unicode_orig =
479         malloc( ( strlen(psz_string) + 1 ) * sizeof(uint32_t) );
480     if( psz_unicode == NULL )
481     {
482         msg_Err( p_filter, "out of memory" );
483         goto error;
484     }
485 #if defined(WORDS_BIGENDIAN)
486     iconv_handle = vlc_iconv_open( "UCS-4BE", "UTF-8" );
487 #else
488     iconv_handle = vlc_iconv_open( "UCS-4LE", "UTF-8" );
489 #endif
490     if( iconv_handle == (vlc_iconv_t)-1 )
491     {
492         msg_Warn( p_filter, "unable to do conversion" );
493         goto error;
494     }
495
496     /* Set up the glyphs for the desired font size.  By definition,
497        p_sys->i_font_size is a valid value, else the initial Create would
498        have failed. Using -1 as a flag to use the freetype-fontsize */
499     if ( i_font_size < 0 )  
500     {
501             FT_Set_Pixel_Sizes( p_sys->p_face, 0, p_sys->i_font_size );
502     }
503     else          
504     {
505         i_error = FT_Set_Pixel_Sizes( p_sys->p_face, 0, i_font_size );
506         if( i_error )
507         {
508             msg_Warn( p_filter, "Invalid font size to RenderText, using %d", 
509                       p_sys->i_font_size );
510             FT_Set_Pixel_Sizes( p_sys->p_face, 0, p_sys->i_font_size );
511         }
512     }
513
514     {
515         char *p_in_buffer, *p_out_buffer;
516         size_t i_in_bytes, i_out_bytes, i_out_bytes_left, i_ret;
517         i_in_bytes = strlen( psz_string );
518         i_out_bytes = i_in_bytes * sizeof( uint32_t );
519         i_out_bytes_left = i_out_bytes;
520         p_in_buffer = psz_string;
521         p_out_buffer = (char *)psz_unicode;
522         i_ret = vlc_iconv( iconv_handle, &p_in_buffer, &i_in_bytes,
523                            &p_out_buffer, &i_out_bytes_left );
524
525         vlc_iconv_close( iconv_handle );
526
527         if( i_in_bytes )
528         {
529             msg_Warn( p_filter, "failed to convert string to unicode (%s), "
530                       "bytes left %d", strerror(errno), i_in_bytes );
531             goto error;
532         }
533         *(uint32_t*)p_out_buffer = 0;
534         i_string_length = (i_out_bytes - i_out_bytes_left) / sizeof(uint32_t);
535     }
536
537 #if defined(HAVE_FRIBIDI)
538     {
539         uint32_t *p_fribidi_string;
540         FriBidiCharType base_dir = FRIBIDI_TYPE_ON;
541         p_fribidi_string = malloc( (i_string_length + 1) * sizeof(uint32_t) );
542         fribidi_log2vis( (FriBidiChar*)psz_unicode, i_string_length,
543                          &base_dir, (FriBidiChar*)p_fribidi_string, 0, 0, 0 );
544         free( psz_unicode_orig );
545         psz_unicode = psz_unicode_orig = p_fribidi_string;
546         p_fribidi_string[ i_string_length ] = 0;
547     }
548 #endif
549
550     /* Calculate relative glyph positions and a bounding box for the
551      * entire string */
552     if( !(p_line = NewLine( psz_string )) )
553     {
554         msg_Err( p_filter, "out of memory" );
555         goto error;
556     }
557     p_lines = p_line;
558     i_pen_x = i_pen_y = 0;
559     i_previous = i = 0;
560     psz_line_start = psz_unicode;
561
562 #define face p_sys->p_face
563 #define glyph face->glyph
564
565     while( *psz_unicode )
566     {
567         i_char = *psz_unicode++;
568         if( i_char == '\r' ) /* ignore CR chars wherever they may be */
569         {
570             continue;
571         }
572
573         if( i_char == '\n' )
574         {
575             psz_line_start = psz_unicode;
576             if( !(p_next = NewLine( psz_string )) )
577             {
578                 msg_Err( p_filter, "out of memory" );
579                 goto error;
580             }
581             p_line->p_next = p_next;
582             p_line->i_width = line.xMax;
583             p_line->i_height = face->size->metrics.height >> 6;
584             p_line->pp_glyphs[ i ] = NULL;
585             p_line->i_alpha = i_font_alpha;
586             p_line->i_red = i_red;
587             p_line->i_green = i_green;
588             p_line->i_blue = i_blue;
589             p_prev = p_line;
590             p_line = p_next;
591             result.x = __MAX( result.x, line.xMax );
592             result.y += face->size->metrics.height >> 6;
593             i_pen_x = 0;
594             i_previous = i = 0;
595             line.xMin = line.xMax = line.yMin = line.yMax = 0;
596             i_pen_y += face->size->metrics.height >> 6;
597 #if 0
598             msg_Dbg( p_filter, "Creating new line, i is %d", i );
599 #endif
600             continue;
601         }
602
603         i_glyph_index = FT_Get_Char_Index( face, i_char );
604         if( p_sys->i_use_kerning && i_glyph_index
605             && i_previous )
606         {
607             FT_Vector delta;
608             FT_Get_Kerning( face, i_previous, i_glyph_index,
609                             ft_kerning_default, &delta );
610             i_pen_x += delta.x >> 6;
611
612         }
613         p_line->p_glyph_pos[ i ].x = i_pen_x;
614         p_line->p_glyph_pos[ i ].y = i_pen_y;
615         i_error = FT_Load_Glyph( face, i_glyph_index, FT_LOAD_DEFAULT );
616         if( i_error )
617         {
618             msg_Err( p_filter, "FT_Load_Glyph returned %d", i_error );
619             goto error;
620         }
621         i_error = FT_Get_Glyph( glyph, &tmp_glyph );
622         if( i_error )
623         {
624             msg_Err( p_filter, "FT_Get_Glyph returned %d", i_error );
625             goto error;
626         }
627         FT_Glyph_Get_CBox( tmp_glyph, ft_glyph_bbox_pixels, &glyph_size );
628         i_error = FT_Glyph_To_Bitmap( &tmp_glyph, ft_render_mode_normal, 0, 1);
629         if( i_error ) continue;
630         p_line->pp_glyphs[ i ] = (FT_BitmapGlyph)tmp_glyph;
631
632         /* Do rest */
633         line.xMax = p_line->p_glyph_pos[i].x + glyph_size.xMax -
634             glyph_size.xMin + ((FT_BitmapGlyph)tmp_glyph)->left;
635         if( line.xMax > (int)p_filter->fmt_out.video.i_visible_width - 20 )
636         {
637             p_line->pp_glyphs[ i ] = NULL;
638             FreeLine( p_line );
639             p_line = NewLine( psz_string );
640             if( p_prev ) p_prev->p_next = p_line;
641             else p_lines = p_line;
642
643             while( psz_unicode > psz_line_start && *psz_unicode != ' ' )
644             {
645                 psz_unicode--;
646             }
647             if( psz_unicode == psz_line_start )
648             {
649                 msg_Warn( p_filter, "unbreakable string" );
650                 goto error;
651             }
652             else
653             {
654                 *psz_unicode = '\n';
655             }
656             psz_unicode = psz_line_start;
657             i_pen_x = 0;
658             i_previous = i = 0;
659             line.xMin = line.xMax = line.yMin = line.yMax = 0;
660             continue;
661         }
662         line.yMax = __MAX( line.yMax, glyph_size.yMax );
663         line.yMin = __MIN( line.yMin, glyph_size.yMin );
664
665         i_previous = i_glyph_index;
666         i_pen_x += glyph->advance.x >> 6;
667         i++;
668     }
669
670     p_line->i_width = line.xMax;
671     p_line->i_height = face->size->metrics.height >> 6;
672     p_line->pp_glyphs[ i ] = NULL;
673     p_line->i_alpha = i_font_alpha;
674     p_line->i_red = i_red;
675     p_line->i_green = i_green;
676     p_line->i_blue = i_blue;
677     result.x = __MAX( result.x, line.xMax );
678     result.y += line.yMax - line.yMin;
679
680 #undef face
681 #undef glyph
682
683     p_region_out->i_x = p_region_in->i_x;
684     p_region_out->i_y = p_region_in->i_y;
685
686     Render( p_filter, p_region_out, p_line, result.x, result.y );
687
688     if( psz_unicode_orig ) free( psz_unicode_orig );
689     FreeLines( p_lines );
690     return VLC_SUCCESS;
691
692  error:
693     if( psz_unicode_orig ) free( psz_unicode_orig );
694     FreeLines( p_lines );
695     return VLC_EGENERIC;
696 }
697
698 static void FreeLine( line_desc_t *p_line )
699 {
700     unsigned int i;
701     for( i = 0; p_line->pp_glyphs[ i ] != NULL; i++ )
702     {
703         FT_Done_Glyph( (FT_Glyph)p_line->pp_glyphs[ i ] );
704     }
705     free( p_line->pp_glyphs );
706     free( p_line->p_glyph_pos );
707     free( p_line );
708 }
709
710 static void FreeLines( line_desc_t *p_lines )
711 {
712     line_desc_t *p_line, *p_next;
713
714     if( !p_lines ) return;
715
716     for( p_line = p_lines; p_line != NULL; p_line = p_next )
717     {
718         p_next = p_line->p_next;
719         FreeLine( p_line );
720     }
721 }
722
723 static line_desc_t *NewLine( byte_t *psz_string )
724 {
725     int i_count;
726     line_desc_t *p_line = malloc( sizeof(line_desc_t) );
727
728     if( !p_line ) return NULL;
729     p_line->i_height = 0;
730     p_line->i_width = 0;
731     p_line->p_next = NULL;
732
733     /* We don't use CountUtf8Characters() here because we are not acutally
734      * sure the string is utf8. Better be safe than sorry. */
735     i_count = strlen( psz_string );
736
737     p_line->pp_glyphs = malloc( sizeof(FT_BitmapGlyph) * ( i_count + 1 ) );
738     if( p_line->pp_glyphs == NULL )
739     {
740         free( p_line );
741         return NULL;
742     }
743     p_line->pp_glyphs[0] = NULL;
744
745     p_line->p_glyph_pos = malloc( sizeof( FT_Vector ) * i_count + 1 );
746     if( p_line->p_glyph_pos == NULL )
747     {
748         free( p_line->pp_glyphs );
749         free( p_line );
750         return NULL;
751     }
752
753     return p_line;
754 }