]> git.sesse.net Git - vlc/blob - src/video_output/video_text.c
Merge branch 1.0-bugfix
[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
28 #include <vlc_common.h>
29 #include <vlc_vout.h>
30 #include <vlc_block.h>
31 #include <vlc_filter.h>
32 #include <vlc_osd.h>
33
34 /**
35  * \brief Show text on the video for some time
36  * \param p_vout pointer to the vout the text is to be showed on
37  * \param i_channel Subpicture channel
38  * \param psz_string The text to be shown
39  * \param p_style Pointer to a struct with text style info (it is duplicated if non NULL)
40  * \param i_flags flags for alignment and such
41  * \param i_hmargin horizontal margin in pixels
42  * \param i_vmargin vertical margin in pixels
43  * \param i_duration Amount of time the text is to be shown.
44  */
45 int vout_ShowTextRelative( vout_thread_t *p_vout, int i_channel,
46                            char *psz_string, const text_style_t *p_style,
47                            int i_flags, int i_hmargin, int i_vmargin,
48                            mtime_t i_duration )
49 {
50     mtime_t i_now = mdate();
51
52     return vout_ShowTextAbsolute( p_vout, i_channel, psz_string,
53                                   p_style, i_flags, i_hmargin, i_vmargin,
54                                   i_now, i_now + i_duration );
55 }
56
57 /**
58  * \brief Show text on the video from a given start date to a given end date
59  * \param p_vout pointer to the vout the text is to be showed on
60  * \param i_channel Subpicture channel
61  * \param psz_string The text to be shown
62  * \param p_style Pointer to a struct with text style info (it is duplicated if non NULL)
63  * \param i_flags flags for alignment and such
64  * \param i_hmargin horizontal margin in pixels
65  * \param i_vmargin vertical margin in pixels
66  * \param i_start the time when this string is to appear on the video
67  * \param i_stop the time when this string should stop to be displayed
68  *               if this is 0 the string will be shown untill the next string
69  *               is about to be shown
70  */
71 int vout_ShowTextAbsolute( vout_thread_t *p_vout, int i_channel,
72                            const char *psz_string, const text_style_t *p_style,
73                            int i_flags, int i_hmargin, int i_vmargin,
74                            mtime_t i_start, mtime_t i_stop )
75 {
76     subpicture_t *p_spu;
77     video_format_t fmt;
78
79     if( !psz_string ) return VLC_EGENERIC;
80
81     p_spu = subpicture_New();
82     if( !p_spu )
83         return VLC_EGENERIC;
84
85     p_spu->i_channel = i_channel;
86     p_spu->i_start = i_start;
87     p_spu->i_stop = i_stop;
88     p_spu->b_ephemer = true;
89     p_spu->b_absolute = false;
90     p_spu->b_fade = true;
91
92
93     /* Create a new subpicture region */
94     memset( &fmt, 0, sizeof(video_format_t) );
95     fmt.i_chroma = VLC_CODEC_TEXT;
96     fmt.i_aspect = 0;
97     fmt.i_width = fmt.i_height = 0;
98     fmt.i_x_offset = fmt.i_y_offset = 0;
99     p_spu->p_region = subpicture_region_New( &fmt );
100     if( !p_spu->p_region )
101     {
102         msg_Err( p_vout, "cannot allocate SPU region" );
103         subpicture_Delete( p_spu );
104         return VLC_EGENERIC;
105     }
106
107     p_spu->p_region->psz_text = strdup( psz_string );
108     p_spu->p_region->i_align = i_flags & SUBPICTURE_ALIGN_MASK;
109     p_spu->p_region->i_x = i_hmargin;
110     p_spu->p_region->i_y = i_vmargin;
111     if( p_style )
112         p_spu->p_region->p_style = text_style_Duplicate( p_style );
113
114     spu_DisplaySubpicture( p_vout->p_spu, p_spu );
115
116     return VLC_SUCCESS;
117 }
118
119
120 /**
121  * \brief Write an informative message at the default location,
122  *        for the default duration and only if the OSD option is enabled.
123  * \param p_caller The object that called the function.
124  * \param i_channel Subpicture channel
125  * \param psz_format printf style formatting
126  **/
127 void __vout_OSDMessage( vlc_object_t *p_caller, int i_channel,
128                         const char *psz_format, ... )
129 {
130     vout_thread_t *p_vout;
131     char *psz_string = NULL;
132     va_list args;
133
134     if( !config_GetInt( p_caller, "osd" ) ) return;
135
136     p_vout = vlc_object_find( p_caller, VLC_OBJECT_VOUT, FIND_ANYWHERE );
137     if( p_vout )
138     {
139         va_start( args, psz_format );
140         if( vasprintf( &psz_string, psz_format, args ) != -1 )
141         {
142             vout_ShowTextRelative( p_vout, i_channel, psz_string, NULL,
143                                    OSD_ALIGN_TOP|OSD_ALIGN_RIGHT,
144                                    30 + p_vout->fmt_in.i_width
145                                       - p_vout->fmt_in.i_visible_width
146                                       - p_vout->fmt_in.i_x_offset,
147                                    20 + p_vout->fmt_in.i_y_offset, 1000000 );
148             free( psz_string );
149         }
150         vlc_object_release( p_vout );
151         va_end( args );
152     }
153 }
154
155 /* */
156 text_style_t *text_style_New( void )
157 {
158     text_style_t *p_style = calloc( 1, sizeof(*p_style) );
159     if( !p_style )
160         return NULL;
161
162     /* initialize to default text style */
163     p_style->psz_fontname = NULL;
164     p_style->i_font_size = 22;
165     p_style->i_font_color = 0xffffff;
166     p_style->i_font_alpha = 0xff;
167     p_style->i_style_flags = STYLE_OUTLINE;
168     p_style->i_outline_color = 0x000000;
169     p_style->i_outline_alpha = 0xff;
170     p_style->i_shadow_color = 0x000000;
171     p_style->i_shadow_alpha = 0xff;
172     p_style->i_background_color = 0xffffff;
173     p_style->i_background_alpha = 0x80;
174     p_style->i_karaoke_background_color = 0xffffff;
175     p_style->i_karaoke_background_alpha = 0xff;
176     p_style->i_outline_width = 1;
177     p_style->i_shadow_width = 0;
178     p_style->i_spacing = -1;
179
180     return p_style;
181 }
182
183 text_style_t *text_style_Copy( text_style_t *p_dst, const text_style_t *p_src )
184 {
185     if( !p_src )
186         return p_dst;
187
188     /* */
189     if( p_dst->psz_fontname )
190         free( p_dst->psz_fontname );
191
192     /* */
193     *p_dst = *p_src;
194
195     /* */
196     if( p_dst->psz_fontname )
197         p_dst->psz_fontname = strdup( p_dst->psz_fontname );
198
199     return p_dst;
200 }
201
202 text_style_t *text_style_Duplicate( const text_style_t *p_src )
203 {
204     if( !p_src )
205         return NULL;
206
207     text_style_t *p_dst = calloc( 1, sizeof(*p_dst) );
208     if( p_dst )
209         text_style_Copy( p_dst, p_src );
210     return p_dst;
211 }
212
213 void text_style_Delete( text_style_t *p_style )
214 {
215     if( p_style )
216         free( p_style->psz_fontname );
217     free( p_style );
218 }
219