Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

freetype.c

00001 /*****************************************************************************
00002  * freetype.c : Put text on the video, using freetype2
00003  *****************************************************************************
00004  * Copyright (C) 2002 - 2005 the VideoLAN team
00005  * $Id: freetype.c 13026 2005-10-30 15:32:11Z gbazin $
00006  *
00007  * Authors: Sigmund Augdal Helberg <[email protected]>
00008  *          Gildas Bazin <[email protected]>
00009  *
00010  * This program is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version.
00014  *
00015  * This program is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00023  *****************************************************************************/
00024 
00025 /*****************************************************************************
00026  * Preamble
00027  *****************************************************************************/
00028 #include <stdlib.h>                                      /* malloc(), free() */
00029 #include <string.h>
00030 
00031 #ifdef HAVE_LINUX_LIMITS_H
00032 #   include <linux/limits.h>
00033 #endif
00034 
00035 #include <vlc/vlc.h>
00036 #include <vlc/vout.h>
00037 #include "vlc_osd.h"
00038 #include "vlc_block.h"
00039 #include "vlc_filter.h"
00040 
00041 #include <math.h>
00042 
00043 #ifdef HAVE_ERRNO_H
00044 #   include <errno.h>
00045 #endif
00046 
00047 #include <ft2build.h>
00048 #include FT_FREETYPE_H
00049 #include FT_GLYPH_H
00050 
00051 #ifdef SYS_DARWIN
00052 #define DEFAULT_FONT "/System/Library/Fonts/LucidaGrande.dfont"
00053 #elif defined( SYS_BEOS )
00054 #define DEFAULT_FONT "/boot/beos/etc/fonts/ttfonts/Swiss721.ttf"
00055 #elif defined( WIN32 )
00056 #define DEFAULT_FONT "" /* Default font found at run-time */
00057 #else
00058 #define DEFAULT_FONT "/usr/share/fonts/truetype/freefont/FreeSerifBold.ttf"
00059 #endif
00060 
00061 #if defined(HAVE_FRIBIDI)
00062 #include <fribidi/fribidi.h>
00063 #endif
00064 
00065 typedef struct line_desc_t line_desc_t;
00066 
00067 /*****************************************************************************
00068  * Local prototypes
00069  *****************************************************************************/
00070 static int  Create ( vlc_object_t * );
00071 static void Destroy( vlc_object_t * );
00072 
00073 /* The RenderText call maps to pf_render_string, defined in vlc_filter.h */
00074 static int RenderText( filter_t *, subpicture_region_t *,
00075                        subpicture_region_t * );
00076 static line_desc_t *NewLine( byte_t * );
00077 
00078 static int SetFontSize( filter_t *, int );
00079 
00080 /*****************************************************************************
00081  * Module descriptor
00082  *****************************************************************************/
00083 #define FONT_TEXT N_("Font")
00084 #define FONT_LONGTEXT N_("Font filename")
00085 #define FONTSIZE_TEXT N_("Font size in pixels")
00086 #define FONTSIZE_LONGTEXT N_("The size of the fonts used by the osd module. " \
00087     "If set to something different than 0 this option will override the " \
00088     "relative font size " )
00089 #define OPACITY_TEXT N_("Opacity, 0..255")
00090 #define OPACITY_LONGTEXT N_("The opacity (inverse of transparency) of " \
00091     "overlay text. 0 = transparent, 255 = totally opaque. " )
00092 #define COLOR_TEXT N_("Text Default Color")
00093 #define COLOR_LONGTEXT N_("The color of overlay text. 1 byte for each color, "\
00094     "hexadecimal. #000000 = all colors off, 0xFF0000 = just Red, " \
00095     "0xFFFFFF = all color on [White]" )
00096 #define FONTSIZER_TEXT N_("Font size")
00097 #define FONTSIZER_LONGTEXT N_("The size of the fonts used by the osd module" )
00098 
00099 static int   pi_sizes[] = { 20, 18, 16, 12, 6 };
00100 static char *ppsz_sizes_text[] = { N_("Smaller"), N_("Small"), N_("Normal"),
00101                                    N_("Large"), N_("Larger") };
00102 static int pi_color_values[] = {
00103   0x00000000, 0x00808080, 0x00C0C0C0, 0x00FFFFFF, 0x00800000,
00104   0x00FF0000, 0x00FF00FF, 0x00FFFF00, 0x00808000, 0x00008000, 0x00008080, 
00105   0x0000FF00, 0x00800080, 0x00000080, 0x000000FF, 0x0000FFFF }; 
00106 
00107 static char *ppsz_color_descriptions[] = {
00108   N_("Black"), N_("Gray"), N_("Silver"), N_("White"), N_("Maroon"),
00109   N_("Red"), N_("Fuchsia"), N_("Yellow"), N_("Olive"), N_("Green"), N_("Teal"),
00110   N_("Lime"), N_("Purple"), N_("Navy"), N_("Blue"), N_("Aqua") };
00111 
00112 vlc_module_begin();
00113     set_shortname( _("Text renderer"));
00114     set_description( _("Freetype2 font renderer") );
00115     set_category( CAT_VIDEO );
00116     set_subcategory( SUBCAT_VIDEO_SUBPIC );
00117 
00118     add_file( "freetype-font", DEFAULT_FONT, NULL, FONT_TEXT, FONT_LONGTEXT,
00119               VLC_FALSE );
00120 
00121     add_integer( "freetype-fontsize", 0, NULL, FONTSIZE_TEXT,
00122                  FONTSIZE_LONGTEXT, VLC_TRUE );
00123 
00124     /* opacity valid on 0..255, with default 255 = fully opaque */
00125     add_integer_with_range( "freetype-opacity", 255, 0, 255, NULL,
00126         OPACITY_TEXT, OPACITY_LONGTEXT, VLC_TRUE );
00127 
00128     /* hook to the color values list, with default 0x00ffffff = white */
00129     add_integer( "freetype-color", 0x00FFFFFF, NULL, COLOR_TEXT,
00130                  COLOR_LONGTEXT, VLC_FALSE );
00131         change_integer_list( pi_color_values, ppsz_color_descriptions, 0 );
00132 
00133     add_integer( "freetype-rel-fontsize", 16, NULL, FONTSIZER_TEXT,
00134                  FONTSIZER_LONGTEXT, VLC_FALSE );
00135         change_integer_list( pi_sizes, ppsz_sizes_text, 0 );
00136 
00137     set_capability( "text renderer", 100 );
00138     add_shortcut( "text" );
00139     set_callbacks( Create, Destroy );
00140 vlc_module_end();
00141 
00142 struct line_desc_t
00143 {
00145     FT_BitmapGlyph *pp_glyphs;
00147     FT_Vector      *p_glyph_pos;
00148 
00149     int             i_height;
00150     int             i_width;
00151     int             i_red, i_green, i_blue;
00152     int             i_alpha;
00153 
00154     line_desc_t    *p_next;
00155 };
00156 
00157 static int Render( filter_t *, subpicture_region_t *, line_desc_t *, int, int);
00158 static void FreeLines( line_desc_t * );
00159 static void FreeLine( line_desc_t * );
00160 
00161 /*****************************************************************************
00162  * filter_sys_t: freetype local data
00163  *****************************************************************************
00164  * This structure is part of the video output thread descriptor.
00165  * It describes the freetype specific properties of an output thread.
00166  *****************************************************************************/
00167 struct filter_sys_t
00168 {
00169     FT_Library     p_library;   /* handle to library     */
00170     FT_Face        p_face;      /* handle to face object */
00171     vlc_bool_t     i_use_kerning;
00172     uint8_t        i_font_opacity;
00173     int            i_font_color;
00174     int            i_font_size;
00175 
00176     int            i_default_font_size;
00177     int            i_display_height;
00178 };
00179 
00180 /*****************************************************************************
00181  * Create: allocates osd-text video thread output method
00182  *****************************************************************************
00183  * This function allocates and initializes a Clone vout method.
00184  *****************************************************************************/
00185 static int Create( vlc_object_t *p_this )
00186 {
00187     filter_t *p_filter = (filter_t *)p_this;
00188     filter_sys_t *p_sys;
00189     char *psz_fontfile = NULL;
00190     int i_error;
00191     vlc_value_t val;
00192 
00193     /* Allocate structure */
00194     p_filter->p_sys = p_sys = malloc( sizeof( filter_sys_t ) );
00195     if( !p_sys )
00196     {
00197         msg_Err( p_filter, "out of memory" );
00198         return VLC_ENOMEM;
00199     }
00200     p_sys->p_face = 0;
00201     p_sys->p_library = 0;
00202     p_sys->i_font_size = 0;
00203     p_sys->i_display_height = 0;
00204 
00205     var_Create( p_filter, "freetype-font",
00206                 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
00207     var_Create( p_filter, "freetype-fontsize",
00208                 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
00209     var_Create( p_filter, "freetype-rel-fontsize",
00210                 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
00211     var_Create( p_filter, "freetype-opacity",
00212                 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
00213     var_Get( p_filter, "freetype-opacity", &val );
00214     p_sys->i_font_opacity = __MAX( __MIN( val.i_int, 255 ), 0 );
00215     var_Create( p_filter, "freetype-color",
00216                 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
00217     var_Get( p_filter, "freetype-color", &val );
00218     p_sys->i_font_color = __MAX( __MIN( val.i_int, 0xFFFFFF ), 0 );
00219 
00220     /* Look what method was requested */
00221     var_Get( p_filter, "freetype-font", &val );
00222     psz_fontfile = val.psz_string;
00223     if( !psz_fontfile || !*psz_fontfile )
00224     {
00225         if( psz_fontfile ) free( psz_fontfile );
00226         psz_fontfile = (char *)malloc( PATH_MAX + 1 );
00227 #ifdef WIN32
00228         GetWindowsDirectory( psz_fontfile, PATH_MAX + 1 );
00229         strcat( psz_fontfile, "\\fonts\\arial.ttf" );
00230 #elif SYS_DARWIN
00231         strcpy( psz_fontfile, DEFAULT_FONT );
00232 #else
00233         msg_Err( p_filter, "user didn't specify a font" );
00234         goto error;
00235 #endif
00236     }
00237 
00238     i_error = FT_Init_FreeType( &p_sys->p_library );
00239     if( i_error )
00240     {
00241         msg_Err( p_filter, "couldn't initialize freetype" );
00242         goto error;
00243     }
00244 
00245     i_error = FT_New_Face( p_sys->p_library, psz_fontfile ? psz_fontfile : "",
00246                            0, &p_sys->p_face );
00247     if( i_error == FT_Err_Unknown_File_Format )
00248     {
00249         msg_Err( p_filter, "file %s have unknown format", psz_fontfile );
00250         goto error;
00251     }
00252     else if( i_error )
00253     {
00254         msg_Err( p_filter, "failed to load font file %s", psz_fontfile );
00255         goto error;
00256     }
00257 
00258     i_error = FT_Select_Charmap( p_sys->p_face, ft_encoding_unicode );
00259     if( i_error )
00260     {
00261         msg_Err( p_filter, "Font has no unicode translation table" );
00262         goto error;
00263     }
00264 
00265     p_sys->i_use_kerning = FT_HAS_KERNING( p_sys->p_face );
00266 
00267     var_Get( p_filter, "freetype-fontsize", &val );
00268     p_sys->i_default_font_size = val.i_int;
00269     if( SetFontSize( p_filter, 0 ) != VLC_SUCCESS ) goto error;
00270 
00271     if( psz_fontfile ) free( psz_fontfile );
00272     p_filter->pf_render_text = RenderText;
00273     return VLC_SUCCESS;
00274 
00275  error:
00276     if( p_sys->p_face ) FT_Done_Face( p_sys->p_face );
00277     if( p_sys->p_library ) FT_Done_FreeType( p_sys->p_library );
00278     if( psz_fontfile ) free( psz_fontfile );
00279     free( p_sys );
00280     return VLC_EGENERIC;
00281 }
00282 
00283 /*****************************************************************************
00284  * Destroy: destroy Clone video thread output method
00285  *****************************************************************************
00286  * Clean up all data and library connections
00287  *****************************************************************************/
00288 static void Destroy( vlc_object_t *p_this )
00289 {
00290     filter_t *p_filter = (filter_t *)p_this;
00291     filter_sys_t *p_sys = p_filter->p_sys;
00292 
00293     FT_Done_Face( p_sys->p_face );
00294     FT_Done_FreeType( p_sys->p_library );
00295     free( p_sys );
00296 }
00297 
00298 /*****************************************************************************
00299  * Render: place string in picture
00300  *****************************************************************************
00301  * This function merges the previously rendered freetype glyphs into a picture
00302  *****************************************************************************/
00303 static int Render( filter_t *p_filter, subpicture_region_t *p_region,
00304                    line_desc_t *p_line, int i_width, int i_height )
00305 {
00306     static uint8_t pi_gamma[16] =
00307         {0x00, 0x52, 0x84, 0x96, 0xb8, 0xca, 0xdc, 0xee, 0xff,
00308           0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
00309 
00310     uint8_t *p_dst;
00311     video_format_t fmt;
00312     int i, x, y, i_pitch;
00313     uint8_t i_y; /* YUV values, derived from incoming RGB */
00314     int8_t i_u, i_v;
00315     subpicture_region_t *p_region_tmp;
00316 
00317     /* Create a new subpicture region */
00318     memset( &fmt, 0, sizeof(video_format_t) );
00319     fmt.i_chroma = VLC_FOURCC('Y','U','V','P');
00320     fmt.i_aspect = 0;
00321     fmt.i_width = fmt.i_visible_width = i_width + 4;
00322     fmt.i_height = fmt.i_visible_height = i_height + 4;
00323     fmt.i_x_offset = fmt.i_y_offset = 0;
00324     p_region_tmp = spu_CreateRegion( p_filter, &fmt );
00325     if( !p_region_tmp )
00326     {
00327         msg_Err( p_filter, "cannot allocate SPU region" );
00328         return VLC_EGENERIC;
00329     }
00330 
00331     p_region->fmt = p_region_tmp->fmt;
00332     p_region->picture = p_region_tmp->picture;
00333     free( p_region_tmp );
00334 
00335     /* Calculate text color components */
00336     i_y = (uint8_t)(( 66 * p_line->i_red  + 129 * p_line->i_green +
00337                       25 * p_line->i_blue + 128) >> 8) +  16;
00338     i_u = (int8_t)(( -38 * p_line->i_red  -  74 * p_line->i_green +
00339                      112 * p_line->i_blue + 128) >> 8) + 128;
00340     i_v = (int8_t)(( 112 * p_line->i_red  -  94 * p_line->i_green -
00341                       18 * p_line->i_blue + 128) >> 8) + 128;
00342 
00343     /* Build palette */
00344     fmt.p_palette->i_entries = 16;
00345     for( i = 0; i < 8; i++ )
00346     {
00347         fmt.p_palette->palette[i][0] = 0;
00348         fmt.p_palette->palette[i][1] = 0x80;
00349         fmt.p_palette->palette[i][2] = 0x80;
00350         fmt.p_palette->palette[i][3] = pi_gamma[i];
00351         fmt.p_palette->palette[i][3] =
00352             (int)fmt.p_palette->palette[i][3] * (255 - p_line->i_alpha) / 255;
00353     }
00354     for( i = 8; i < fmt.p_palette->i_entries; i++ )
00355     {
00356         fmt.p_palette->palette[i][0] = i * 16 * i_y / 256;
00357         fmt.p_palette->palette[i][1] = i_u;
00358         fmt.p_palette->palette[i][2] = i_v;
00359         fmt.p_palette->palette[i][3] = pi_gamma[i];
00360         fmt.p_palette->palette[i][3] =
00361             (int)fmt.p_palette->palette[i][3] * (255 - p_line->i_alpha) / 255;
00362     }
00363 
00364     p_dst = p_region->picture.Y_PIXELS;
00365     i_pitch = p_region->picture.Y_PITCH;
00366 
00367     /* Initialize the region pixels */
00368     memset( p_dst, 0, i_pitch * p_region->fmt.i_height );
00369 
00370     for( ; p_line != NULL; p_line = p_line->p_next )
00371     {
00372         int i_glyph_tmax = 0;
00373         int i_bitmap_offset, i_offset, i_align_offset = 0;
00374         for( i = 0; p_line->pp_glyphs[i] != NULL; i++ )
00375         {
00376             FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ];
00377             i_glyph_tmax = __MAX( i_glyph_tmax, p_glyph->top );
00378         }
00379 
00380         if( p_line->i_width < i_width )
00381         {
00382             if( p_region->i_text_align == SUBPICTURE_ALIGN_RIGHT )
00383             {
00384                 i_align_offset = i_width - p_line->i_width;
00385             }
00386             else if( p_region->i_text_align != SUBPICTURE_ALIGN_LEFT )
00387             {
00388                 i_align_offset = ( i_width - p_line->i_width ) / 2;
00389             }
00390         }
00391 
00392         for( i = 0; p_line->pp_glyphs[i] != NULL; i++ )
00393         {
00394             FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ];
00395 
00396             i_offset = ( p_line->p_glyph_pos[ i ].y +
00397                 i_glyph_tmax - p_glyph->top + 2 ) *
00398                 i_pitch + p_line->p_glyph_pos[ i ].x + p_glyph->left + 2 +
00399                 i_align_offset;
00400 
00401             for( y = 0, i_bitmap_offset = 0; y < p_glyph->bitmap.rows; y++ )
00402             {
00403                 for( x = 0; x < p_glyph->bitmap.width; x++, i_bitmap_offset++ )
00404                 {
00405                     if( p_glyph->bitmap.buffer[i_bitmap_offset] )
00406                         p_dst[i_offset+x] =
00407                          ((int)p_glyph->bitmap.buffer[i_bitmap_offset] + 8)/16;
00408                 }
00409                 i_offset += i_pitch;
00410             }
00411         }
00412     }
00413 
00414     /* Outlining (find something better than nearest neighbour filtering ?) */
00415     if( 1 )
00416     {
00417         uint8_t *p_dst = p_region->picture.Y_PIXELS;
00418         uint8_t *p_top = p_dst; /* Use 1st line as a cache */
00419         uint8_t left, current;
00420 
00421         for( y = 1; y < (int)fmt.i_height - 1; y++ )
00422         {
00423             if( y > 1 ) memcpy( p_top, p_dst, fmt.i_width );
00424             p_dst += p_region->picture.Y_PITCH;
00425             left = 0;
00426 
00427             for( x = 1; x < (int)fmt.i_width - 1; x++ )
00428             {
00429                 current = p_dst[x];
00430                 p_dst[x] = ( 8 * (int)p_dst[x] + left + p_dst[x+1] + p_top[x -1]+ p_top[x] + p_top[x+1] +
00431                              p_dst[x -1 + p_region->picture.Y_PITCH ] + p_dst[x + p_region->picture.Y_PITCH] + p_dst[x + 1 + p_region->picture.Y_PITCH]) / 16;
00432                 left = current;
00433             }
00434         }
00435         memset( p_top, 0, fmt.i_width );
00436     }
00437 
00438     return VLC_SUCCESS;
00439 }
00440 
00447 static int RenderText( filter_t *p_filter, subpicture_region_t *p_region_out,
00448                        subpicture_region_t *p_region_in )
00449 {
00450     filter_sys_t *p_sys = p_filter->p_sys;
00451     line_desc_t  *p_lines = 0, *p_line = 0, *p_next = 0, *p_prev = 0;
00452     int i, i_pen_y, i_pen_x, i_error, i_glyph_index, i_previous;
00453     uint32_t *psz_unicode, *psz_unicode_orig = 0, i_char, *psz_line_start;
00454     int i_string_length;
00455     char *psz_string;
00456     vlc_iconv_t iconv_handle = (vlc_iconv_t)(-1);
00457     int i_font_color, i_font_alpha, i_font_size, i_red, i_green, i_blue;
00458 
00459     FT_BBox line;
00460     FT_BBox glyph_size;
00461     FT_Vector result;
00462     FT_Glyph tmp_glyph;
00463 
00464     /* Sanity check */
00465     if( !p_region_in || !p_region_out ) return VLC_EGENERIC;
00466     psz_string = p_region_in->psz_text;
00467     if( !psz_string || !*psz_string ) return VLC_EGENERIC;
00468 
00469     i_font_color = __MAX( __MIN( p_region_in->i_text_color, 0xFFFFFF ), 0 );
00470     if( i_font_color == 0xFFFFFF ) i_font_color = p_sys->i_font_color;
00471 
00472     i_font_alpha = __MAX( __MIN( p_region_in->i_text_alpha, 255 ), 0 );
00473     if( !i_font_alpha ) i_font_alpha = 255 - p_sys->i_font_opacity;
00474 
00475     i_font_size  = __MAX( __MIN( p_region_in->i_text_size, 255 ), 0 );
00476     SetFontSize( p_filter, i_font_size );
00477 
00478     i_red   = ( i_font_color & 0x00FF0000 ) >> 16;
00479     i_green = ( i_font_color & 0x0000FF00 ) >>  8;
00480     i_blue  =   i_font_color & 0x000000FF;
00481 
00482     result.x =  result.y = 0;
00483     line.xMin = line.xMax = line.yMin = line.yMax = 0;
00484 
00485     psz_unicode = psz_unicode_orig =
00486         malloc( ( strlen(psz_string) + 1 ) * sizeof(uint32_t) );
00487     if( psz_unicode == NULL )
00488     {
00489         msg_Err( p_filter, "out of memory" );
00490         goto error;
00491     }
00492 #if defined(WORDS_BIGENDIAN)
00493     iconv_handle = vlc_iconv_open( "UCS-4BE", "UTF-8" );
00494 #else
00495     iconv_handle = vlc_iconv_open( "UCS-4LE", "UTF-8" );
00496 #endif
00497     if( iconv_handle == (vlc_iconv_t)-1 )
00498     {
00499         msg_Warn( p_filter, "unable to do conversion" );
00500         goto error;
00501     }
00502 
00503     {
00504         char *p_in_buffer, *p_out_buffer;
00505         size_t i_in_bytes, i_out_bytes, i_out_bytes_left, i_ret;
00506         i_in_bytes = strlen( psz_string );
00507         i_out_bytes = i_in_bytes * sizeof( uint32_t );
00508         i_out_bytes_left = i_out_bytes;
00509         p_in_buffer = psz_string;
00510         p_out_buffer = (char *)psz_unicode;
00511         i_ret = vlc_iconv( iconv_handle, &p_in_buffer, &i_in_bytes,
00512                            &p_out_buffer, &i_out_bytes_left );
00513 
00514         vlc_iconv_close( iconv_handle );
00515 
00516         if( i_in_bytes )
00517         {
00518             msg_Warn( p_filter, "failed to convert string to unicode (%s), "
00519                       "bytes left %d", strerror(errno), i_in_bytes );
00520             goto error;
00521         }
00522         *(uint32_t*)p_out_buffer = 0;
00523         i_string_length = (i_out_bytes - i_out_bytes_left) / sizeof(uint32_t);
00524     }
00525 
00526 #if defined(HAVE_FRIBIDI)
00527     {
00528         uint32_t *p_fribidi_string;
00529         FriBidiCharType base_dir = FRIBIDI_TYPE_ON;
00530         p_fribidi_string = malloc( (i_string_length + 1) * sizeof(uint32_t) );
00531         fribidi_log2vis( (FriBidiChar*)psz_unicode, i_string_length,
00532                          &base_dir, (FriBidiChar*)p_fribidi_string, 0, 0, 0 );
00533         free( psz_unicode_orig );
00534         psz_unicode = psz_unicode_orig = p_fribidi_string;
00535         p_fribidi_string[ i_string_length ] = 0;
00536     }
00537 #endif
00538 
00539     /* Calculate relative glyph positions and a bounding box for the
00540      * entire string */
00541     if( !(p_line = NewLine( psz_string )) )
00542     {
00543         msg_Err( p_filter, "out of memory" );
00544         goto error;
00545     }
00546     p_lines = p_line;
00547     i_pen_x = i_pen_y = 0;
00548     i_previous = i = 0;
00549     psz_line_start = psz_unicode;
00550 
00551 #define face p_sys->p_face
00552 #define glyph face->glyph
00553 
00554     while( *psz_unicode )
00555     {
00556         i_char = *psz_unicode++;
00557         if( i_char == '\r' ) /* ignore CR chars wherever they may be */
00558         {
00559             continue;
00560         }
00561 
00562         if( i_char == '\n' )
00563         {
00564             psz_line_start = psz_unicode;
00565             if( !(p_next = NewLine( psz_string )) )
00566             {
00567                 msg_Err( p_filter, "out of memory" );
00568                 goto error;
00569             }
00570             p_line->p_next = p_next;
00571             p_line->i_width = line.xMax;
00572             p_line->i_height = face->size->metrics.height >> 6;
00573             p_line->pp_glyphs[ i ] = NULL;
00574             p_line->i_alpha = i_font_alpha;
00575             p_line->i_red = i_red;
00576             p_line->i_green = i_green;
00577             p_line->i_blue = i_blue;
00578             p_prev = p_line;
00579             p_line = p_next;
00580             result.x = __MAX( result.x, line.xMax );
00581             result.y += face->size->metrics.height >> 6;
00582             i_pen_x = 0;
00583             i_previous = i = 0;
00584             line.xMin = line.xMax = line.yMin = line.yMax = 0;
00585             i_pen_y += face->size->metrics.height >> 6;
00586 #if 0
00587             msg_Dbg( p_filter, "Creating new line, i is %d", i );
00588 #endif
00589             continue;
00590         }
00591 
00592         i_glyph_index = FT_Get_Char_Index( face, i_char );
00593         if( p_sys->i_use_kerning && i_glyph_index
00594             && i_previous )
00595         {
00596             FT_Vector delta;
00597             FT_Get_Kerning( face, i_previous, i_glyph_index,
00598                             ft_kerning_default, &delta );
00599             i_pen_x += delta.x >> 6;
00600 
00601         }
00602         p_line->p_glyph_pos[ i ].x = i_pen_x;
00603         p_line->p_glyph_pos[ i ].y = i_pen_y;
00604         i_error = FT_Load_Glyph( face, i_glyph_index, FT_LOAD_DEFAULT );
00605         if( i_error )
00606         {
00607             msg_Err( p_filter, "FT_Load_Glyph returned %d", i_error );
00608             goto error;
00609         }
00610         i_error = FT_Get_Glyph( glyph, &tmp_glyph );
00611         if( i_error )
00612         {
00613             msg_Err( p_filter, "FT_Get_Glyph returned %d", i_error );
00614             goto error;
00615         }
00616         FT_Glyph_Get_CBox( tmp_glyph, ft_glyph_bbox_pixels, &glyph_size );
00617         i_error = FT_Glyph_To_Bitmap( &tmp_glyph, ft_render_mode_normal, 0, 1);
00618         if( i_error )
00619         {
00620             FT_Done_Glyph( tmp_glyph );
00621             continue;
00622         }
00623         p_line->pp_glyphs[ i ] = (FT_BitmapGlyph)tmp_glyph;
00624 
00625         /* Do rest */
00626         line.xMax = p_line->p_glyph_pos[i].x + glyph_size.xMax -
00627             glyph_size.xMin + ((FT_BitmapGlyph)tmp_glyph)->left;
00628         if( line.xMax > (int)p_filter->fmt_out.video.i_visible_width - 20 )
00629         {
00630             p_line->pp_glyphs[ i ] = NULL;
00631             FreeLine( p_line );
00632             p_line = NewLine( psz_string );
00633             if( p_prev ) p_prev->p_next = p_line;
00634             else p_lines = p_line;
00635 
00636             while( psz_unicode > psz_line_start && *psz_unicode != ' ' )
00637             {
00638                 psz_unicode--;
00639             }
00640             if( psz_unicode == psz_line_start )
00641             {
00642                 msg_Warn( p_filter, "unbreakable string" );
00643                 goto error;
00644             }
00645             else
00646             {
00647                 *psz_unicode = '\n';
00648             }
00649             psz_unicode = psz_line_start;
00650             i_pen_x = 0;
00651             i_previous = i = 0;
00652             line.xMin = line.xMax = line.yMin = line.yMax = 0;
00653             continue;
00654         }
00655         line.yMax = __MAX( line.yMax, glyph_size.yMax );
00656         line.yMin = __MIN( line.yMin, glyph_size.yMin );
00657 
00658         i_previous = i_glyph_index;
00659         i_pen_x += glyph->advance.x >> 6;
00660         i++;
00661     }
00662 
00663     p_line->i_width = line.xMax;
00664     p_line->i_height = face->size->metrics.height >> 6;
00665     p_line->pp_glyphs[ i ] = NULL;
00666     p_line->i_alpha = i_font_alpha;
00667     p_line->i_red = i_red;
00668     p_line->i_green = i_green;
00669     p_line->i_blue = i_blue;
00670     result.x = __MAX( result.x, line.xMax );
00671     result.y += line.yMax - line.yMin;
00672 
00673 #undef face
00674 #undef glyph
00675 
00676     p_region_out->i_x = p_region_in->i_x;
00677     p_region_out->i_y = p_region_in->i_y;
00678 
00679     Render( p_filter, p_region_out, p_lines, result.x, result.y );
00680 
00681     if( psz_unicode_orig ) free( psz_unicode_orig );
00682     FreeLines( p_lines );
00683     return VLC_SUCCESS;
00684 
00685  error:
00686     if( psz_unicode_orig ) free( psz_unicode_orig );
00687     FreeLines( p_lines );
00688     return VLC_EGENERIC;
00689 }
00690 
00691 static void FreeLine( line_desc_t *p_line )
00692 {
00693     unsigned int i;
00694     for( i = 0; p_line->pp_glyphs[ i ] != NULL; i++ )
00695     {
00696         FT_Done_Glyph( (FT_Glyph)p_line->pp_glyphs[ i ] );
00697     }
00698     free( p_line->pp_glyphs );
00699     free( p_line->p_glyph_pos );
00700     free( p_line );
00701 }
00702 
00703 static void FreeLines( line_desc_t *p_lines )
00704 {
00705     line_desc_t *p_line, *p_next;
00706 
00707     if( !p_lines ) return;
00708 
00709     for( p_line = p_lines; p_line != NULL; p_line = p_next )
00710     {
00711         p_next = p_line->p_next;
00712         FreeLine( p_line );
00713     }
00714 }
00715 
00716 static line_desc_t *NewLine( byte_t *psz_string )
00717 {
00718     int i_count;
00719     line_desc_t *p_line = malloc( sizeof(line_desc_t) );
00720 
00721     if( !p_line ) return NULL;
00722     p_line->i_height = 0;
00723     p_line->i_width = 0;
00724     p_line->p_next = NULL;
00725 
00726     /* We don't use CountUtf8Characters() here because we are not acutally
00727      * sure the string is utf8. Better be safe than sorry. */
00728     i_count = strlen( psz_string );
00729 
00730     p_line->pp_glyphs = malloc( sizeof(FT_BitmapGlyph) * ( i_count + 1 ) );
00731     if( p_line->pp_glyphs == NULL )
00732     {
00733         free( p_line );
00734         return NULL;
00735     }
00736     p_line->pp_glyphs[0] = NULL;
00737 
00738     p_line->p_glyph_pos = malloc( sizeof( FT_Vector ) * i_count + 1 );
00739     if( p_line->p_glyph_pos == NULL )
00740     {
00741         free( p_line->pp_glyphs );
00742         free( p_line );
00743         return NULL;
00744     }
00745 
00746     return p_line;
00747 }
00748 
00749 static int SetFontSize( filter_t *p_filter, int i_size )
00750 {
00751     filter_sys_t *p_sys = p_filter->p_sys;
00752 
00753     if( i_size && i_size == p_sys->i_font_size ) return VLC_SUCCESS;
00754 
00755     if( !i_size )
00756     {
00757         vlc_value_t val;
00758 
00759         if( !p_sys->i_default_font_size &&
00760             p_sys->i_display_height == (int)p_filter->fmt_out.video.i_height )
00761             return VLC_SUCCESS;
00762 
00763         if( p_sys->i_default_font_size )
00764         {
00765             i_size = p_sys->i_default_font_size;
00766         }
00767         else
00768         {
00769             var_Get( p_filter, "freetype-rel-fontsize", &val );
00770             i_size = (int)p_filter->fmt_out.video.i_height / val.i_int;
00771             p_filter->p_sys->i_display_height =
00772                 p_filter->fmt_out.video.i_height;
00773         }
00774         if( i_size <= 0 )
00775         {
00776             msg_Warn( p_filter, "Invalid fontsize, using 12" );
00777             i_size = 12;
00778         }
00779 
00780         msg_Dbg( p_filter, "Using fontsize: %i", i_size );
00781     }
00782 
00783     p_sys->i_font_size = i_size;
00784 
00785     if( FT_Set_Pixel_Sizes( p_sys->p_face, 0, i_size ) )
00786     {
00787         msg_Err( p_filter, "couldn't set font size to %d", i_size );
00788         return VLC_EGENERIC;
00789     }
00790 
00791     return VLC_SUCCESS;
00792 }

Generated on Tue Dec 20 10:14:48 2005 for vlc-0.8.4a by  doxygen 1.4.2