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