]> git.sesse.net Git - vlc/blobdiff - modules/misc/win32text.c
Adjustments to better match VLC coding style.
[vlc] / modules / misc / win32text.c
index 322d42a0f6e8f4a5e5436d2dc5662607d4557cf3..e3464674ed26ea50961fd1afe1b8455918d1fc0e 100644 (file)
@@ -1,10 +1,11 @@
 /*****************************************************************************
  * win32text.c : Text drawing routines using the TextOut win32 API
  *****************************************************************************
- * Copyright (C) 2002 - 2005 the VideoLAN team
+ * Copyright (C) 2002 - 2009 the VideoLAN team
  * $Id$
  *
  * Authors: Gildas Bazin <gbazin@videolan.org>
+ *          Pierre Ynard
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -29,7 +30,7 @@
 # include "config.h"
 #endif
 
-#include <vlc/vlc.h>
+#include <vlc_common.h>
 #include <vlc_plugin.h>
 #include <vlc_vout.h>
 #include <vlc_osd.h>
@@ -75,45 +76,45 @@ static int SetFont( filter_t *, int );
   "fonts that will be rendered on the video. If absolute font size is set, "\
    "relative size will be overriden." )
 
-static int   pi_sizes[] = { 20, 18, 16, 12, 6 };
-static char *ppsz_sizes_text[] = { N_("Smaller"), N_("Small"), N_("Normal"),
-                                   N_("Large"), N_("Larger") };
-static int pi_color_values[] = {
+static int const pi_sizes[] = { 20, 18, 16, 12, 6 };
+static char *const ppsz_sizes_text[] = {
+    N_("Smaller"), N_("Small"), N_("Normal"), N_("Large"), N_("Larger") };
+static const int pi_color_values[] = {
   0x00000000, 0x00808080, 0x00C0C0C0, 0x00FFFFFF, 0x00800000,
   0x00FF0000, 0x00FF00FF, 0x00FFFF00, 0x00808000, 0x00008000, 0x00008080,
   0x0000FF00, 0x00800080, 0x00000080, 0x000000FF, 0x0000FFFF };
 
-static char *ppsz_color_descriptions[] = {
+static const char *const ppsz_color_descriptions[] = {
   N_("Black"), N_("Gray"), N_("Silver"), N_("White"), N_("Maroon"),
   N_("Red"), N_("Fuchsia"), N_("Yellow"), N_("Olive"), N_("Green"), N_("Teal"),
   N_("Lime"), N_("Purple"), N_("Navy"), N_("Blue"), N_("Aqua") };
 
-vlc_module_begin();
-    set_shortname( _("Text renderer"));
-    set_description( _("Win32 font renderer") );
-    set_category( CAT_VIDEO );
-    set_subcategory( SUBCAT_VIDEO_SUBPIC );
+vlc_module_begin ()
+    set_shortname( N_("Text renderer"))
+    set_description( N_("Win32 font renderer") )
+    set_category( CAT_VIDEO )
+    set_subcategory( SUBCAT_VIDEO_SUBPIC )
 
     add_integer( "win32text-fontsize", 0, NULL, FONTSIZE_TEXT,
-                 FONTSIZE_LONGTEXT, true );
+                 FONTSIZE_LONGTEXT, true )
 
     /* opacity valid on 0..255, with default 255 = fully opaque */
     add_integer_with_range( "win32-opacity", 255, 0, 255, NULL,
-        OPACITY_TEXT, OPACITY_LONGTEXT, false );
+        OPACITY_TEXT, OPACITY_LONGTEXT, false )
 
     /* hook to the color values list, with default 0x00ffffff = white */
     add_integer( "win32text-color", 0x00FFFFFF, NULL, COLOR_TEXT,
-                 COLOR_LONGTEXT, true );
-        change_integer_list( pi_color_values, ppsz_color_descriptions, 0 );
+                 COLOR_LONGTEXT, true )
+        change_integer_list( pi_color_values, ppsz_color_descriptions, NULL );
 
     add_integer( "win32text-rel-fontsize", 16, NULL, FONTSIZER_TEXT,
-                 FONTSIZER_LONGTEXT, false );
-        change_integer_list( pi_sizes, ppsz_sizes_text, 0 );
+                 FONTSIZER_LONGTEXT, false )
+        change_integer_list( pi_sizes, ppsz_sizes_text, NULL );
 
-    set_capability( "text renderer", 50 );
-    add_shortcut( "text" );
-    set_callbacks( Create, Destroy );
-vlc_module_end();
+    set_capability( "text renderer", 50 )
+    add_shortcut( "text" )
+    set_callbacks( Create, Destroy )
+vlc_module_end ()
 
 /*****************************************************************************
  * filter_sys_t: win32text local data
@@ -133,7 +134,7 @@ struct filter_sys_t
     int i_logpy;
 };
 
-static uint8_t pi_gamma[16] =
+static const uint8_t pi_gamma[16] =
   {0x00, 0x41, 0x52, 0x63, 0x84, 0x85, 0x96, 0xa7, 0xb8, 0xc9,
    0xca, 0xdb, 0xdc, 0xed, 0xee, 0xff};
 
@@ -151,10 +152,7 @@ static int Create( vlc_object_t *p_this )
     /* Allocate structure */
     p_filter->p_sys = p_sys = malloc( sizeof( filter_sys_t ) );
     if( !p_sys )
-    {
-        msg_Err( p_filter, "out of memory" );
         return VLC_ENOMEM;
-    }
     p_sys->i_font_size = 0;
     p_sys->i_display_height = 0;
 
@@ -220,23 +218,19 @@ static int Render( filter_t *p_filter, subpicture_region_t *p_region,
     uint8_t *p_dst;
     video_format_t fmt;
     int i, i_pitch;
-    subpicture_region_t *p_region_tmp;
     bool b_outline = true;
 
     /* Create a new subpicture region */
     memset( &fmt, 0, sizeof(video_format_t) );
-    fmt.i_chroma = VLC_FOURCC('Y','U','V','P');
+    fmt.i_chroma = VLC_CODEC_YUVP;
     fmt.i_width = fmt.i_visible_width = i_width + (b_outline ? 4 : 0);
     fmt.i_height = fmt.i_visible_height = i_height + (b_outline ? 4 : 0);
     fmt.i_x_offset = fmt.i_y_offset = 0;
-    p_region_tmp = spu_CreateRegion( p_filter, &fmt );
-    if( !p_region_tmp )
-    {
-        msg_Err( p_filter, "cannot allocate SPU region" );
-        return VLC_EGENERIC;
-    }
 
     /* Build palette */
+    fmt.p_palette = calloc( 1, sizeof(*fmt.p_palette) );
+    if( !fmt.p_palette )
+        return VLC_EGENERIC;
     fmt.p_palette->i_entries = 16;
     for( i = 0; i < fmt.p_palette->i_entries; i++ )
     {
@@ -246,17 +240,21 @@ static int Render( filter_t *p_filter, subpicture_region_t *p_region,
         fmt.p_palette->palette[i][3] = pi_gamma[i];
     }
 
-    p_region->fmt = p_region_tmp->fmt;
-    p_region->picture = p_region_tmp->picture;
-    free( p_region_tmp );
+    p_region->p_picture = picture_NewFromFormat( &fmt );
+    if( !p_region->p_picture )
+    {
+        free( fmt.p_palette );
+        return VLC_EGENERIC;
+    }
+    p_region->fmt = fmt;
 
-    p_dst = p_region->picture.Y_PIXELS;
-    i_pitch = p_region->picture.Y_PITCH;
+    p_dst = p_region->p_picture->Y_PIXELS;
+    i_pitch = p_region->p_picture->Y_PITCH;
 
     if( b_outline )
     {
         memset( p_dst, 0, i_pitch * fmt.i_height );
-        p_dst += p_region->picture.Y_PITCH * 2 + 2;
+        p_dst += p_region->p_picture->Y_PITCH * 2 + 2;
     }
 
     for( i = 0; i < i_height; i++ )
@@ -273,7 +271,7 @@ static int Render( filter_t *p_filter, subpicture_region_t *p_region,
         uint8_t left, current;
         int x, y;
 
-        p_dst = p_region->picture.Y_PIXELS;
+        p_dst = p_region->p_picture->Y_PIXELS;
 
         for( y = 1; y < (int)fmt.i_height - 1; y++ )
         {
@@ -305,13 +303,17 @@ static int RenderText( filter_t *p_filter, subpicture_region_t *p_region_out,
     int i, i_width, i_height;
     HBITMAP bitmap, bitmap_bak;
     BITMAPINFO *p_bmi;
-    RECT rect = {0};
-    SIZE size;
+    RECT rect = { 0, 0, 0, 0 };
 
     /* Sanity check */
     if( !p_region_in || !p_region_out ) return VLC_EGENERIC;
-#ifdef UNICODE
+    if( !p_region_in->psz_text || !*p_region_in->psz_text )
+        return VLC_EGENERIC;
+
     psz_string = malloc( (strlen( p_region_in->psz_text )+1) * sizeof(TCHAR) );
+    if( !psz_string )
+        return VLC_ENOMEM;
+#ifdef UNICODE
     if( mbstowcs( psz_string, p_region_in->psz_text,
                   strlen( p_region_in->psz_text ) * sizeof(TCHAR) ) < 0 )
     {
@@ -319,9 +321,13 @@ static int RenderText( filter_t *p_filter, subpicture_region_t *p_region_out,
         return VLC_EGENERIC;
     }
 #else
-    psz_string = strdup( p_region_in->psz_text );
+    strcpy( psz_string, p_region_in->psz_text );
 #endif
-    if( !psz_string || !*psz_string ) return VLC_EGENERIC;
+    if( !*psz_string )
+    {
+        free( psz_string );
+        return VLC_EGENERIC;
+    }
 
     if( p_region_in->p_style )
     {
@@ -341,9 +347,9 @@ static int RenderText( filter_t *p_filter, subpicture_region_t *p_region_out,
     SetTextColor( p_sys->hcdc, RGB( (i_font_color >> 16) & 0xff,
                   (i_font_color >> 8) & 0xff, i_font_color & 0xff) );
 
-    GetTextExtentExPoint( p_sys->hcdc, psz_string, _tcslen(psz_string),
-                          0, 0, 0, &size );
-    i_width = rect.right = size.cx; i_height = rect.bottom = size.cy;
+    DrawText( p_sys->hcdc, psz_string, -1, &rect,
+              DT_CALCRECT | DT_CENTER | DT_NOPREFIX );
+    i_width = rect.right; i_height = rect.bottom;
 
     p_bmi = malloc(sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD)*16);
     memset( p_bmi, 0, sizeof(BITMAPINFOHEADER) );
@@ -367,14 +373,15 @@ static int RenderText( filter_t *p_filter, subpicture_region_t *p_region_out,
     if( !bitmap )
     {
         msg_Err( p_filter, "could not create bitmap" );
+        free( psz_string );
         return VLC_EGENERIC;
     }
 
     bitmap_bak = SelectObject( p_sys->hcdc, bitmap );
     FillRect( p_sys->hcdc, &rect, (HBRUSH)GetStockObject(BLACK_BRUSH) );
 
-    //TextOut( p_sys->hcdc, 0, 0, psz_string, strlen(psz_string) );
-    if( !DrawText( p_sys->hcdc, psz_string, -1, &rect, 0 ) )
+    if( !DrawText( p_sys->hcdc, psz_string, -1, &rect,
+                   DT_CENTER | DT_NOPREFIX ) )
     {
         msg_Err( p_filter, "could not draw text" );
     }
@@ -385,6 +392,7 @@ static int RenderText( filter_t *p_filter, subpicture_region_t *p_region_out,
 
     SelectObject( p_sys->hcdc, bitmap_bak );
     DeleteObject( bitmap );
+    free( psz_string );
     return VLC_SUCCESS;
 }