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

win32text.c

00001 /*****************************************************************************
00002  * win32text.c : Text drawing routines using the TextOut win32 API
00003  *****************************************************************************
00004  * Copyright (C) 2002 - 2005 the VideoLAN team
00005  * $Id: freetype.c 10258 2005-03-10 13:37:29Z gbazin $
00006  *
00007  * Authors: Gildas Bazin <[email protected]>
00008  *
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00022  *****************************************************************************/
00023 
00024 /*****************************************************************************
00025  * Preamble
00026  *****************************************************************************/
00027 #include <stdlib.h>                                      /* malloc(), free() */
00028 #include <string.h>
00029 
00030 #include <vlc/vlc.h>
00031 #include <vlc/vout.h>
00032 #include "vlc_osd.h"
00033 #include "vlc_block.h"
00034 #include "vlc_filter.h"
00035 
00036 #include <math.h>
00037 
00038 /*****************************************************************************
00039  * Local prototypes
00040  *****************************************************************************/
00041 static int  Create ( vlc_object_t * );
00042 static void Destroy( vlc_object_t * );
00043 
00044 /* The RenderText call maps to pf_render_string, defined in vlc_filter.h */
00045 static int RenderText( filter_t *, subpicture_region_t *,
00046                        subpicture_region_t * );
00047 
00048 static int Render( filter_t *, subpicture_region_t *, uint8_t *, int, int);
00049 static int SetFont( filter_t *, int );
00050 
00051 /*****************************************************************************
00052  * Module descriptor
00053  *****************************************************************************/
00054 #define FONT_TEXT N_("Font")
00055 #define FONT_LONGTEXT N_("Font filename")
00056 #define FONTSIZE_TEXT N_("Font size in pixels")
00057 #define FONTSIZE_LONGTEXT N_("The size of the fonts used by the osd module. " \
00058     "If set to something different than 0 this option will override the " \
00059     "relative font size " )
00060 #define OPACITY_TEXT N_("Opacity, 0..255")
00061 #define OPACITY_LONGTEXT N_("The opacity (inverse of transparency) of " \
00062     "overlay text. 0 = transparent, 255 = totally opaque. " )
00063 #define COLOR_TEXT N_("Text Default Color")
00064 #define COLOR_LONGTEXT N_("The color of overlay text. 1 byte for each color, "\
00065     "hexadecimal. #000000 = all colors off, 0xFF0000 = just Red, " \
00066     "0xFFFFFF = all color on [White]" )
00067 #define FONTSIZER_TEXT N_("Font size")
00068 #define FONTSIZER_LONGTEXT N_("The size of the fonts used by the osd module" )
00069 
00070 static int   pi_sizes[] = { 20, 18, 16, 12, 6 };
00071 static char *ppsz_sizes_text[] = { N_("Smaller"), N_("Small"), N_("Normal"),
00072                                    N_("Large"), N_("Larger") };
00073 static int pi_color_values[] = {
00074   0x00000000, 0x00808080, 0x00C0C0C0, 0x00FFFFFF, 0x00800000,
00075   0x00FF0000, 0x00FF00FF, 0x00FFFF00, 0x00808000, 0x00008000, 0x00008080, 
00076   0x0000FF00, 0x00800080, 0x00000080, 0x000000FF, 0x0000FFFF }; 
00077 
00078 static char *ppsz_color_descriptions[] = {
00079   N_("Black"), N_("Gray"), N_("Silver"), N_("White"), N_("Maroon"),
00080   N_("Red"), N_("Fuchsia"), N_("Yellow"), N_("Olive"), N_("Green"), N_("Teal"),
00081   N_("Lime"), N_("Purple"), N_("Navy"), N_("Blue"), N_("Aqua") };
00082 
00083 vlc_module_begin();
00084     set_shortname( _("Text renderer"));
00085     set_description( _("Win32 font renderer") );
00086     set_category( CAT_VIDEO );
00087     set_subcategory( SUBCAT_VIDEO_SUBPIC );
00088 
00089     add_integer( "win32text-fontsize", 0, NULL, FONTSIZE_TEXT,
00090                  FONTSIZE_LONGTEXT, VLC_TRUE );
00091 
00092     /* opacity valid on 0..255, with default 255 = fully opaque */
00093     add_integer_with_range( "win32-opacity", 255, 0, 255, NULL,
00094         OPACITY_TEXT, OPACITY_LONGTEXT, VLC_FALSE );
00095 
00096     /* hook to the color values list, with default 0x00ffffff = white */
00097     add_integer( "win32text-color", 0x00FFFFFF, NULL, COLOR_TEXT,
00098                  COLOR_LONGTEXT, VLC_TRUE );
00099         change_integer_list( pi_color_values, ppsz_color_descriptions, 0 );
00100 
00101     add_integer( "win32text-rel-fontsize", 16, NULL, FONTSIZER_TEXT,
00102                  FONTSIZER_LONGTEXT, VLC_FALSE );
00103         change_integer_list( pi_sizes, ppsz_sizes_text, 0 );
00104 
00105     set_capability( "text renderer", 50 );
00106     add_shortcut( "text" );
00107     set_callbacks( Create, Destroy );
00108 vlc_module_end();
00109 
00110 /*****************************************************************************
00111  * filter_sys_t: win32text local data
00112  *****************************************************************************/
00113 struct filter_sys_t
00114 {
00115     uint8_t        i_font_opacity;
00116     int            i_font_color;
00117     int            i_font_size;
00118 
00119     int            i_default_font_size;
00120     int            i_display_height;
00121 
00122     HDC hcdc;
00123     HFONT hfont;
00124     HFONT hfont_bak;
00125     int i_logpy;
00126 };
00127 
00128 static uint8_t pi_gamma[16] =
00129   {0x00, 0x41, 0x52, 0x63, 0x84, 0x85, 0x96, 0xa7, 0xb8, 0xc9,
00130    0xca, 0xdb, 0xdc, 0xed, 0xee, 0xff};
00131 
00132 /*****************************************************************************
00133  * Create: creates the module
00134  *****************************************************************************/
00135 static int Create( vlc_object_t *p_this )
00136 {
00137     filter_t *p_filter = (filter_t *)p_this;
00138     filter_sys_t *p_sys;
00139     char *psz_fontfile = NULL;
00140     vlc_value_t val;
00141     HDC hdc;
00142 
00143     /* Allocate structure */
00144     p_filter->p_sys = p_sys = malloc( sizeof( filter_sys_t ) );
00145     if( !p_sys )
00146     {
00147         msg_Err( p_filter, "out of memory" );
00148         return VLC_ENOMEM;
00149     }
00150     p_sys->i_font_size = 0;
00151     p_sys->i_display_height = 0;
00152 
00153     var_Create( p_filter, "win32text-font",
00154                 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
00155     var_Create( p_filter, "win32text-fontsize",
00156                 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
00157     var_Create( p_filter, "win32text-rel-fontsize",
00158                 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
00159     var_Create( p_filter, "win32text-opacity",
00160                 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
00161     var_Get( p_filter, "win32text-opacity", &val );
00162     p_sys->i_font_opacity = __MAX( __MIN( val.i_int, 255 ), 0 );
00163     var_Create( p_filter, "win32text-color",
00164                 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
00165     var_Get( p_filter, "win32text-color", &val );
00166     p_sys->i_font_color = __MAX( __MIN( val.i_int, 0xFFFFFF ), 0 );
00167 
00168     p_sys->hfont = p_sys->hfont_bak = 0;
00169     hdc = GetDC( NULL );
00170     p_sys->hcdc = CreateCompatibleDC( hdc );
00171     p_sys->i_logpy = GetDeviceCaps( hdc, LOGPIXELSY );
00172     ReleaseDC( NULL, hdc );
00173     SetBkMode( p_sys->hcdc, TRANSPARENT );
00174 
00175     var_Get( p_filter, "win32text-fontsize", &val );
00176     p_sys->i_default_font_size = val.i_int;
00177     if( SetFont( p_filter, 0 ) != VLC_SUCCESS ) goto error;
00178 
00179     if( psz_fontfile ) free( psz_fontfile );
00180     p_filter->pf_render_text = RenderText;
00181     return VLC_SUCCESS;
00182 
00183  error:
00184     if( psz_fontfile ) free( psz_fontfile );
00185     free( p_sys );
00186     return VLC_EGENERIC;
00187 }
00188 
00189 /*****************************************************************************
00190  * Destroy: destroy the module
00191  *****************************************************************************/
00192 static void Destroy( vlc_object_t *p_this )
00193 {
00194     filter_t *p_filter = (filter_t *)p_this;
00195     filter_sys_t *p_sys = p_filter->p_sys;
00196 
00197     if( p_sys->hfont_bak ) SelectObject( p_sys->hcdc, p_sys->hfont_bak );
00198     if( p_sys->hfont ) DeleteObject( p_sys->hfont );
00199     DeleteDC( p_sys->hcdc );
00200     free( p_sys );
00201 }
00202 
00203 /*****************************************************************************
00204  * Render: place string in picture
00205  *****************************************************************************
00206  * This function merges the previously rendered win32text glyphs into a picture
00207  *****************************************************************************/
00208 static int Render( filter_t *p_filter, subpicture_region_t *p_region,
00209                    uint8_t *p_bitmap, int i_width, int i_height )
00210 {
00211     uint8_t *p_dst;
00212     video_format_t fmt;
00213     int i, i_pitch;
00214     subpicture_region_t *p_region_tmp;
00215     vlc_bool_t b_outline = VLC_TRUE;
00216 
00217     /* Create a new subpicture region */
00218     memset( &fmt, 0, sizeof(video_format_t) );
00219     fmt.i_chroma = VLC_FOURCC('Y','U','V','P');
00220     fmt.i_width = fmt.i_visible_width = i_width + (b_outline ? 4 : 0);
00221     fmt.i_height = fmt.i_visible_height = i_height + (b_outline ? 4 : 0);
00222     fmt.i_x_offset = fmt.i_y_offset = 0;
00223     p_region_tmp = spu_CreateRegion( p_filter, &fmt );
00224     if( !p_region_tmp )
00225     {
00226         msg_Err( p_filter, "cannot allocate SPU region" );
00227         return VLC_EGENERIC;
00228     }
00229 
00230     /* Build palette */
00231     fmt.p_palette->i_entries = 16;
00232     for( i = 0; i < fmt.p_palette->i_entries; i++ )
00233     {
00234         fmt.p_palette->palette[i][0] = pi_gamma[i];
00235         fmt.p_palette->palette[i][1] = 128;
00236         fmt.p_palette->palette[i][2] = 128;
00237         fmt.p_palette->palette[i][3] = pi_gamma[i];
00238     }
00239 
00240     p_region->fmt = p_region_tmp->fmt;
00241     p_region->picture = p_region_tmp->picture;
00242     free( p_region_tmp );
00243 
00244     p_dst = p_region->picture.Y_PIXELS;
00245     i_pitch = p_region->picture.Y_PITCH;
00246 
00247     if( b_outline )
00248     {
00249         memset( p_dst, 0, i_pitch * fmt.i_height );
00250         p_dst += p_region->picture.Y_PITCH * 2 + 2;
00251     }
00252 
00253     for( i = 0; i < i_height; i++ )
00254     {
00255         memcpy( p_dst, p_bitmap, i_width );
00256         p_bitmap += (i_width+3) & ~3;
00257         p_dst += i_pitch;
00258     }
00259 
00260     /* Outlining (find something better than nearest neighbour filtering ?) */
00261     if( b_outline )
00262     {
00263         uint8_t *p_top = p_dst; /* Use 1st line as a cache */
00264         uint8_t left, current;
00265         int x, y;
00266 
00267         p_dst = p_region->picture.Y_PIXELS;
00268 
00269         for( y = 1; y < (int)fmt.i_height - 1; y++ )
00270         {
00271             memcpy( p_top, p_dst, fmt.i_width );
00272             p_dst += i_pitch;
00273             left = 0;
00274 
00275             for( x = 1; x < (int)fmt.i_width - 1; x++ )
00276             {
00277                 current = p_dst[x];
00278                 p_dst[x] = ( 4 * (int)p_dst[x] + left + p_top[x] + p_dst[x+1] +
00279                              p_dst[x + i_pitch]) / 8;
00280                 left = current;
00281             }
00282         }
00283         memset( p_top, 0, fmt.i_width );
00284     }
00285 
00286     return VLC_SUCCESS;
00287 }
00288 
00289 static int RenderText( filter_t *p_filter, subpicture_region_t *p_region_out,
00290                        subpicture_region_t *p_region_in )
00291 {
00292     filter_sys_t *p_sys = p_filter->p_sys;
00293     int i_font_color, i_font_alpha, i_font_size;
00294     uint8_t *p_bitmap;
00295     TCHAR *psz_string;
00296     int i, i_width, i_height;
00297     HBITMAP bitmap, bitmap_bak;
00298     BITMAPINFO *p_bmi;
00299     RECT rect = {0};
00300     SIZE size;
00301 
00302     /* Sanity check */
00303     if( !p_region_in || !p_region_out ) return VLC_EGENERIC;
00304 #ifdef UNICODE
00305     psz_string = malloc( (strlen( p_region_in->psz_text )+1) * sizeof(TCHAR) );
00306     if( mbstowcs( psz_string, p_region_in->psz_text,
00307                   strlen( p_region_in->psz_text ) * sizeof(TCHAR) ) < 0 )
00308     {
00309         free( psz_string );
00310         return VLC_EGENERIC;
00311     }
00312 #else
00313     psz_string = strdup( p_region_in->psz_text );
00314 #endif
00315     if( !psz_string || !*psz_string ) return VLC_EGENERIC;
00316 
00317     i_font_color = __MAX( __MIN( p_region_in->i_text_color, 0xFFFFFF ), 0 );
00318     if( i_font_color == 0xFFFFFF ) i_font_color = p_sys->i_font_color;
00319 
00320     i_font_alpha = __MAX( __MIN( p_region_in->i_text_alpha, 255 ), 0 );
00321     if( !i_font_alpha ) i_font_alpha = 255 - p_sys->i_font_opacity;
00322 
00323     i_font_size  = __MAX( __MIN( p_region_in->i_text_size, 255 ), 0 );
00324 
00325     SetFont( p_filter, i_font_size );
00326 
00327     SetTextColor( p_sys->hcdc, RGB( (i_font_color >> 16) & 0xff,
00328                   (i_font_color >> 8) & 0xff, i_font_color & 0xff) );
00329 
00330     GetTextExtentExPoint( p_sys->hcdc, psz_string, _tcslen(psz_string),
00331                           0, 0, 0, &size );
00332     i_width = rect.right = size.cx; i_height = rect.bottom = size.cy;
00333 
00334     p_bmi = malloc(sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD)*16);
00335     memset( p_bmi, 0, sizeof(BITMAPINFOHEADER) );
00336     p_bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
00337     p_bmi->bmiHeader.biWidth = (i_width+3) & ~3;
00338     p_bmi->bmiHeader.biHeight = - i_height;
00339     p_bmi->bmiHeader.biPlanes = 1;
00340     p_bmi->bmiHeader.biBitCount = 8;
00341     p_bmi->bmiHeader.biCompression = BI_RGB;
00342     p_bmi->bmiHeader.biClrUsed = 16;
00343 
00344     for( i = 0; i < 16; i++ )
00345     {
00346         p_bmi->bmiColors[i].rgbBlue =
00347             p_bmi->bmiColors[i].rgbGreen =
00348                 p_bmi->bmiColors[i].rgbRed = pi_gamma[i];
00349     }
00350 
00351     bitmap = CreateDIBSection( p_sys->hcdc, p_bmi, DIB_RGB_COLORS,
00352                                (void **)&p_bitmap, NULL, 0 );
00353     if( !bitmap )
00354     {
00355         msg_Err( p_filter, "could not create bitmap" );
00356         return VLC_EGENERIC;
00357     }
00358 
00359     bitmap_bak = SelectObject( p_sys->hcdc, bitmap );
00360     FillRect( p_sys->hcdc, &rect, (HBRUSH)GetStockObject(BLACK_BRUSH) );
00361 
00362     //TextOut( p_sys->hcdc, 0, 0, psz_string, strlen(psz_string) );
00363     if( !DrawText( p_sys->hcdc, psz_string, -1, &rect, 0 ) )
00364     {
00365         msg_Err( p_filter, "could not draw text" );
00366     }
00367 
00368     p_region_out->i_x = p_region_in->i_x;
00369     p_region_out->i_y = p_region_in->i_y;
00370     Render( p_filter, p_region_out, p_bitmap, i_width, i_height );
00371 
00372     SelectObject( p_sys->hcdc, bitmap_bak );
00373     DeleteObject( bitmap );
00374     return VLC_SUCCESS;
00375 }
00376 
00377 static int SetFont( filter_t *p_filter, int i_size )
00378 {
00379     filter_sys_t *p_sys = p_filter->p_sys;
00380     LOGFONT logfont;
00381 
00382     if( i_size && i_size == p_sys->i_font_size ) return VLC_SUCCESS;
00383 
00384     if( !i_size )
00385     {
00386         vlc_value_t val;
00387 
00388         if( !p_sys->i_default_font_size &&
00389             p_sys->i_display_height == (int)p_filter->fmt_out.video.i_height )
00390             return VLC_SUCCESS;
00391 
00392         if( p_sys->i_default_font_size )
00393         {
00394             i_size = p_sys->i_default_font_size;
00395         }
00396         else
00397         {
00398             var_Get( p_filter, "win32text-rel-fontsize", &val );
00399             i_size = (int)p_filter->fmt_out.video.i_height / val.i_int;
00400             p_filter->p_sys->i_display_height =
00401                 p_filter->fmt_out.video.i_height;
00402         }
00403         if( i_size <= 0 )
00404         {
00405             msg_Warn( p_filter, "Invalid fontsize, using 12" );
00406             i_size = 12;
00407         }
00408 
00409         msg_Dbg( p_filter, "Using fontsize: %i", i_size );
00410     }
00411 
00412     p_sys->i_font_size = i_size;
00413 
00414     if( p_sys->hfont_bak ) SelectObject( p_sys->hcdc, p_sys->hfont_bak );
00415     if( p_sys->hfont ) DeleteObject( p_sys->hfont );
00416 
00417     i_size = i_size * (int64_t)p_sys->i_logpy / 72;
00418 
00419     logfont.lfHeight = i_size;
00420     logfont.lfWidth = 0;
00421     logfont.lfEscapement = 0;
00422     logfont.lfOrientation = 0;
00423     logfont.lfWeight = 0;
00424     logfont.lfItalic = FALSE;
00425     logfont.lfUnderline = FALSE;
00426     logfont.lfStrikeOut = FALSE;
00427     logfont.lfCharSet = ANSI_CHARSET;
00428     logfont.lfOutPrecision = OUT_DEFAULT_PRECIS;
00429     logfont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
00430     logfont.lfQuality = ANTIALIASED_QUALITY;
00431     logfont.lfPitchAndFamily = DEFAULT_PITCH;
00432     memcpy( logfont.lfFaceName, _T("Arial"), sizeof(_T("Arial")) );
00433 
00434     p_sys->hfont = CreateFontIndirect( &logfont );
00435 
00436     p_sys->hfont_bak = SelectObject( p_sys->hcdc, p_sys->hfont );
00437 
00438     return VLC_SUCCESS;
00439 }

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