]> git.sesse.net Git - vlc/blob - src/video_output/video_text.c
Moved out text_style_* to src/misc/text_style.c
[vlc] / src / video_output / video_text.c
1 /*****************************************************************************
2  * video_text.c : text manipulation functions
3  *****************************************************************************
4  * Copyright (C) 1999-2007 the VideoLAN team
5  * $Id$
6  *
7  * Author: Sigmund Augdal Helberg <dnumgis@videolan.org>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27 #include <assert.h>
28
29 #include <vlc_common.h>
30 #include <vlc_vout.h>
31 #include <vlc_block.h>
32 #include <vlc_filter.h>
33 #include <vlc_osd.h>
34
35 /* TODO remove access to private vout data */
36 #include "vout_internal.h"
37
38 /**
39  * \brief Show text on the video for some time
40  * \param p_vout pointer to the vout the text is to be showed on
41  * \param i_channel Subpicture channel
42  * \param psz_string The text to be shown
43  * \param p_style Pointer to a struct with text style info (it is duplicated if non NULL)
44  * \param i_flags flags for alignment and such
45  * \param i_hmargin horizontal margin in pixels
46  * \param i_vmargin vertical margin in pixels
47  * \param i_duration Amount of time the text is to be shown.
48  */
49 int vout_ShowTextRelative( vout_thread_t *p_vout, int i_channel,
50                            char *psz_string, const text_style_t *p_style,
51                            int i_flags, int i_hmargin, int i_vmargin,
52                            mtime_t i_duration )
53 {
54     mtime_t i_now = mdate();
55
56     return vout_ShowTextAbsolute( p_vout, i_channel, psz_string,
57                                   p_style, i_flags, i_hmargin, i_vmargin,
58                                   i_now, i_now + i_duration );
59 }
60
61 /**
62  * \brief Show text on the video from a given start date to a given end date
63  * \param p_vout pointer to the vout the text is to be showed on
64  * \param i_channel Subpicture channel
65  * \param psz_string The text to be shown
66  * \param p_style Pointer to a struct with text style info (it is duplicated if non NULL)
67  * \param i_flags flags for alignment and such
68  * \param i_hmargin horizontal margin in pixels
69  * \param i_vmargin vertical margin in pixels
70  * \param i_start the time when this string is to appear on the video
71  * \param i_stop the time when this string should stop to be displayed
72  *               if this is 0 the string will be shown untill the next string
73  *               is about to be shown
74  */
75 int vout_ShowTextAbsolute( vout_thread_t *p_vout, int i_channel,
76                            const char *psz_string, const text_style_t *p_style,
77                            int i_flags, int i_hmargin, int i_vmargin,
78                            mtime_t i_start, mtime_t i_stop )
79 {
80     subpicture_t *p_spu;
81     video_format_t fmt;
82
83     if( !psz_string ) return VLC_EGENERIC;
84
85     p_spu = subpicture_New( NULL );
86     if( !p_spu )
87         return VLC_EGENERIC;
88
89     p_spu->i_channel = i_channel;
90     p_spu->i_start = i_start;
91     p_spu->i_stop = i_stop;
92     p_spu->b_ephemer = true;
93     p_spu->b_absolute = false;
94     p_spu->b_fade = true;
95
96
97     /* Create a new subpicture region */
98     memset( &fmt, 0, sizeof(video_format_t) );
99     fmt.i_chroma = VLC_CODEC_TEXT;
100     fmt.i_width = fmt.i_height = 0;
101     fmt.i_x_offset = fmt.i_y_offset = 0;
102     p_spu->p_region = subpicture_region_New( &fmt );
103     if( !p_spu->p_region )
104     {
105         msg_Err( p_vout, "cannot allocate SPU region" );
106         subpicture_Delete( p_spu );
107         return VLC_EGENERIC;
108     }
109
110     p_spu->p_region->psz_text = strdup( psz_string );
111     p_spu->p_region->i_align = i_flags & SUBPICTURE_ALIGN_MASK;
112     p_spu->p_region->i_x = i_hmargin;
113     p_spu->p_region->i_y = i_vmargin;
114     if( p_style )
115         p_spu->p_region->p_style = text_style_Duplicate( p_style );
116
117     spu_DisplaySubpicture( vout_GetSpu( p_vout ), p_spu );
118
119     return VLC_SUCCESS;
120 }
121
122 #undef vout_OSDMessage
123 /**
124  * \brief Write an informative message at the default location,
125  *        for the default duration and only if the OSD option is enabled.
126  * \param p_caller The object that called the function.
127  * \param i_channel Subpicture channel
128  * \param psz_format printf style formatting
129  **/
130 void vout_OSDMessage( vlc_object_t *p_caller, int i_channel,
131                       const char *psz_format, ... )
132 {
133     vout_thread_t *p_vout;
134     char *psz_string = NULL;
135     va_list args;
136
137     if( !var_InheritBool( p_caller, "osd" ) ) return;
138
139     p_vout = vlc_object_find( p_caller, VLC_OBJECT_VOUT, FIND_ANYWHERE );
140     if( p_vout )
141     {
142         va_start( args, psz_format );
143         if( vasprintf( &psz_string, psz_format, args ) != -1 )
144         {
145             vout_ShowTextRelative( p_vout, i_channel, psz_string, NULL,
146                                    SUBPICTURE_ALIGN_TOP|SUBPICTURE_ALIGN_RIGHT,
147                                    30 + p_vout->p->fmt_in.i_width
148                                       - p_vout->p->fmt_in.i_visible_width
149                                       - p_vout->p->fmt_in.i_x_offset,
150                                    20 + p_vout->p->fmt_in.i_y_offset, 1000000 );
151             free( psz_string );
152         }
153         vlc_object_release( p_vout );
154         va_end( args );
155     }
156 }
157