]> git.sesse.net Git - vlc/blobdiff - src/video_output/video_text.c
Some heavy changes today:
[vlc] / src / video_output / video_text.c
index 4117c919333468dd3d2e7c2ccef4b13af6e259aa..c0dbd82a2fa59ab6346e4e0e375d853f62df1284 100644 (file)
@@ -1,9 +1,11 @@
 /*****************************************************************************
  * video_text.c : text manipulation functions
  *****************************************************************************
- * Copyright (C) 1999, 2000 VideoLAN
+ * Copyright (C) 1999-2001 VideoLAN
+ * $Id: video_text.c,v 1.33 2001/12/30 07:09:56 sam Exp $
  *
- * Authors:
+ * Authors: Vincent Seguin <seguin@via.ecp.fr>
+ *          Samuel Hocevar <sam@zoy.org>
  *
  * 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
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#include "defs.h"
-
 #include <errno.h>                                                  /* errno */
 #include <stdlib.h>                                                /* free() */
+#include <stdio.h>                                              /* sprintf() */
 #include <string.h>                                            /* strerror() */
 #include <fcntl.h>                                                 /* open() */
-#include <unistd.h>                                       /* read(), close() */
 
-#include "config.h"
-#include "common.h"
-#include "video_text.h"
+#include <videolan/vlc.h>
+
+#ifdef HAVE_UNISTD_H
+#   include <unistd.h>                                    /* read(), close() */
+#elif defined( _MSC_VER ) && defined( _WIN32 )
+#   include <io.h>
+#endif
+
+#if defined( WIN32 )
+#   include <io.h>
+#endif
 
-#include "intf_msg.h"
+#include "video_text.h"
 
 /*****************************************************************************
  * vout_font_t: bitmap font
@@ -203,23 +211,66 @@ static void PutByte32( u32 *p_pic, int i_byte, byte_t i_char, byte_t i_border,
  *****************************************************************************/
 vout_font_t *vout_LoadFont( const char *psz_name )
 {
+    static char * path[] = { "share", DATA_PATH, NULL, NULL };
+
+    char **             ppsz_path = path;
+    char *              psz_file;
+#if defined( SYS_BEOS ) || defined( SYS_DARWIN )
+    char *              psz_vlcpath = system_GetProgramPath();
+    int                 i_vlclen = strlen( psz_vlcpath );
+#endif
     int                 i_char, i_line;        /* character and line indexes */
-    int                 i_file;                               /* source file */
+    int                 i_file = -1;                          /* source file */
     byte_t              pi_buffer[2];                         /* file buffer */
     vout_font_t *       p_font;                           /* the font itself */
 
-    /* Open file */
-    i_file = open( psz_name, O_RDONLY );
+    for( ; *ppsz_path != NULL ; ppsz_path++ )
+    {
+#if defined( SYS_BEOS ) || defined( SYS_DARWIN )
+        /* Under BeOS, we need to add beos_GetProgramPath() to access
+         * files under the current directory */
+        if( strncmp( *ppsz_path, "/", 1 ) )
+        {
+            psz_file = malloc( strlen( psz_name ) + strlen( *ppsz_path )
+                                + i_vlclen + 3 );
+            if( psz_file == NULL )
+            {
+                continue;
+            }
+            sprintf( psz_file, "%s/%s/%s", psz_vlcpath, *ppsz_path, psz_name );
+        }
+        else
+#endif
+        {
+            psz_file = malloc( strlen( psz_name ) + strlen( *ppsz_path ) + 2 );
+            if( psz_file == NULL )
+            {
+                continue;
+            }
+            sprintf( psz_file, "%s/%s", *ppsz_path, psz_name );
+        }
+
+        /* Open file */
+        i_file = open( psz_file, O_RDONLY );
+        free( psz_file );
+
+        if( i_file != -1 )
+        {
+            break;
+        }
+    }
+
     if( i_file == -1 )
     {
-        intf_DbgMsg("vout: can't open file '%s' (%s)\n", psz_name, strerror(errno));
+        intf_DbgMsg( "vout error: can't open file '%s' (%s)",
+                     psz_name, strerror(errno) );
         return( NULL );
     }
 
     /* Read magick number */
     if( read( i_file, pi_buffer, 2 ) != 2 )
     {
-        intf_ErrMsg("vout error: unexpected end of file '%s'\n", psz_name );
+        intf_ErrMsg("vout error: unexpected end of file '%s'", psz_name );
         close( i_file );
         return( NULL );
     }
@@ -228,7 +279,8 @@ vout_font_t *vout_LoadFont( const char *psz_name )
     p_font = malloc( sizeof( vout_font_t ) );
     if( p_font == NULL )
     {
-        intf_ErrMsg("vout error: %s\n", strerror(ENOMEM));
+        intf_ErrMsg( "vout error: cannot allocate vout_font_t (%s)",
+                     strerror(ENOMEM) );
         close( i_file );
         return( NULL );
     }
@@ -245,7 +297,7 @@ vout_font_t *vout_LoadFont( const char *psz_name )
         /* Read font header - two bytes indicate the font properties */
         if( read( i_file, pi_buffer, 2 ) != 2)
         {
-            intf_ErrMsg("error: unexpected end of file '%s'\n", psz_name );
+            intf_ErrMsg( "vout error: unexpected end of file '%s'", psz_name );
             free( p_font );
             close( i_file );
             return( NULL );
@@ -265,7 +317,8 @@ vout_font_t *vout_LoadFont( const char *psz_name )
         p_font->p_data = malloc( 2 * 256 * pi_buffer[1] );
         if( p_font->p_data == NULL )
         {
-            intf_ErrMsg("error: %s\n", strerror(ENOMEM));
+            intf_ErrMsg( "vout error: cannot allocate font space (%s)",
+                         strerror(ENOMEM) );
             free( p_font );
             close( i_file );
             return( NULL );
@@ -274,7 +327,7 @@ vout_font_t *vout_LoadFont( const char *psz_name )
         /* Copy raw data */
         if( read( i_file, p_font->p_data, 256 * pi_buffer[1] ) != 256 * pi_buffer[1] )
         {
-            intf_ErrMsg("error: unexpected end of file '%s'\n", psz_name );
+            intf_ErrMsg("vout error: unexpected end of file '%s'", psz_name );
             free( p_font->p_data );
             free( p_font );
             close( i_file );
@@ -300,7 +353,7 @@ vout_font_t *vout_LoadFont( const char *psz_name )
 
         break;
     default:
-        intf_ErrMsg("error: file '%s' has an unknown format\n", psz_name );
+        intf_ErrMsg("vout error: file '%s' has an unknown format", psz_name );
         free( p_font );
         close( i_file );
         return( NULL );
@@ -308,7 +361,7 @@ vout_font_t *vout_LoadFont( const char *psz_name )
     }
 
 
-    intf_DbgMsg( "loaded %s: type %d, %d-%dx%d\n", psz_name, p_font->i_type,
+    intf_DbgMsg( "loaded %s: type %d, %d-%dx%d", psz_name, p_font->i_type,
                  p_font->i_width, p_font->i_interspacing, p_font->i_height );
     return( p_font );
 }
@@ -320,7 +373,13 @@ vout_font_t *vout_LoadFont( const char *psz_name )
  *****************************************************************************/
 void vout_UnloadFont( vout_font_t *p_font )
 {
-    intf_DbgMsg( "vout: unloading font %p\n", p_font );
+    /* If no font was loaded, do nothing */
+    if( p_font == NULL )
+    {
+        return;
+    }
+
+    intf_DbgMsg( "vout: unloading font %p", p_font );
     free( p_font->p_data );
     free( p_font );
 }
@@ -333,6 +392,13 @@ void vout_UnloadFont( vout_font_t *p_font )
  *****************************************************************************/
 void vout_TextSize( vout_font_t *p_font, int i_style, const char *psz_text, int *pi_width, int *pi_height )
 {
+    /* If no font was loaded, do nothing */
+    if( p_font == NULL )
+    {
+        *pi_width = *pi_height = 0;
+        return;
+    }
+
     switch( p_font->i_type )
     {
     case VOUT_FIXED_FONT:
@@ -346,7 +412,7 @@ void vout_TextSize( vout_font_t *p_font, int i_style, const char *psz_text, int
         break;
 #ifdef DEBUG
     default:
-        intf_DbgMsg("error: unknown font type %d\n", p_font->i_type );
+        intf_ErrMsg("error: unknown font type %d", p_font->i_type );
         break;
 #endif
     }
@@ -359,7 +425,7 @@ void vout_TextSize( vout_font_t *p_font, int i_style, const char *psz_text, int
  * previously loaded bitmap font.
  *****************************************************************************/
 void vout_Print( vout_font_t *p_font, byte_t *p_pic, int i_bytes_per_pixel, int i_bytes_per_line,
-                 u32 i_char_color, u32 i_border_color, u32 i_bg_color, int i_style, const char *psz_text )
+                 u32 i_char_color, u32 i_border_color, u32 i_bg_color, int i_style, const char *psz_text, int i_percent)
 {
     byte_t      *p_char, *p_border;        /* character and border mask data */
     int         i_char_mask, i_border_mask, i_bg_mask;              /* masks */
@@ -367,8 +433,15 @@ void vout_Print( vout_font_t *p_font, byte_t *p_pic, int i_bytes_per_pixel, int
     int         i_byte;                         /* current byte in character */
     int         i_interspacing;                  /* offset between two chars */
     int         i_font_bytes_per_line, i_font_height;     /* font properties */
+    int         i_position, i_end;                      /* current position  */
     vout_put_byte_t *p_PutByte;                          /* PutByte function */
 
+    /* If no font was loaded, do nothing */
+    if( p_font == NULL )
+    {
+        return;
+    }
+
     /* FIXME: background: can be something else that whole byte ?? */
 
     /* Select output function */
@@ -400,8 +473,14 @@ void vout_Print( vout_font_t *p_font, byte_t *p_pic, int i_bytes_per_pixel, int
                                                      p_font->i_interspacing * 2 :
                                                      p_font->i_interspacing);
 
+    /* compute where to stop... */
+    i_end = (int) (i_percent * strlen(psz_text) / I64C(100));
+    if(i_end > strlen(psz_text))
+        i_end = strlen(psz_text);
+    
+    
     /* Print text */
-    for( ; *psz_text != '\0'; psz_text++ )
+    for( i_position = 0; i_position < i_end; i_position++ ,psz_text++ )
     {
         /* Check that the character is valid */
         if( (*psz_text >= p_font->i_first) && (*psz_text <= p_font->i_last) )
@@ -448,11 +527,12 @@ void vout_Print( vout_font_t *p_font, byte_t *p_pic, int i_bytes_per_pixel, int
                 break;
 #ifdef DEBUG
             default:
-                intf_DbgMsg("error: unknown font type %d\n", p_font->i_type );
+                intf_ErrMsg("error: unknown font type %d", p_font->i_type );
                 break;
 #endif
             }
         }
+    
     }
 }