]> git.sesse.net Git - vlc/blob - modules/misc/win32text.c
Merged OSD functionality on the same core functions. All OSD functionality is describ...
[vlc] / modules / misc / win32text.c
1 /*****************************************************************************
2  * win32text.c : Text drawing routines using the TextOut win32 API
3  *****************************************************************************
4  * Copyright (C) 2002 - 2005 the VideoLAN team
5  * $Id: freetype.c 10258 2005-03-10 13:37:29Z gbazin $
6  *
7  * Authors: Gildas Bazin <gbazin@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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>
29
30 #include <vlc/vlc.h>
31 #include <vlc/vout.h>
32 #include "vlc_osd.h"
33 #include "vlc_block.h"
34 #include "vlc_filter.h"
35
36 #include <math.h>
37
38 /*****************************************************************************
39  * Local prototypes
40  *****************************************************************************/
41 static int  Create ( vlc_object_t * );
42 static void Destroy( vlc_object_t * );
43
44 /* The RenderText call maps to pf_render_string, defined in vlc_filter.h */
45 static int RenderText( filter_t *, subpicture_region_t *,
46                        subpicture_region_t * );
47
48 static int Render( filter_t *, subpicture_region_t *, uint8_t *, int, int);
49 static int SetFont( filter_t *, int );
50
51 /*****************************************************************************
52  * Module descriptor
53  *****************************************************************************/
54 #define FONT_TEXT N_("Font")
55 #define FONT_LONGTEXT N_("Font filename")
56 #define FONTSIZE_TEXT N_("Font size in pixels")
57 #define FONTSIZE_LONGTEXT N_("The size of the fonts used by the osd module. " \
58     "If set to something different than 0 this option will override the " \
59     "relative font size " )
60 #define OPACITY_TEXT N_("Opacity, 0..255")
61 #define OPACITY_LONGTEXT N_("The opacity (inverse of transparency) of " \
62     "overlay text. 0 = transparent, 255 = totally opaque. " )
63 #define COLOR_TEXT N_("Text Default Color")
64 #define COLOR_LONGTEXT N_("The color of overlay text. 1 byte for each color, "\
65     "hexadecimal. #000000 = all colors off, 0xFF0000 = just Red, " \
66     "0xFFFFFF = all color on [White]" )
67 #define FONTSIZER_TEXT N_("Font size")
68 #define FONTSIZER_LONGTEXT N_("The size of the fonts used by the osd module" )
69
70 static int   pi_sizes[] = { 20, 18, 16, 12, 6 };
71 static char *ppsz_sizes_text[] = { N_("Smaller"), N_("Small"), N_("Normal"),
72                                    N_("Large"), N_("Larger") };
73 static int pi_color_values[] = {
74   0x00000000, 0x00808080, 0x00C0C0C0, 0x00FFFFFF, 0x00800000,
75   0x00FF0000, 0x00FF00FF, 0x00FFFF00, 0x00808000, 0x00008000, 0x00008080, 
76   0x0000FF00, 0x00800080, 0x00000080, 0x000000FF, 0x0000FFFF }; 
77
78 static char *ppsz_color_descriptions[] = {
79   N_("Black"), N_("Gray"), N_("Silver"), N_("White"), N_("Maroon"),
80   N_("Red"), N_("Fuchsia"), N_("Yellow"), N_("Olive"), N_("Green"), N_("Teal"),
81   N_("Lime"), N_("Purple"), N_("Navy"), N_("Blue"), N_("Aqua") };
82
83 vlc_module_begin();
84     set_shortname( _("Text renderer"));
85     set_description( _("Win32 font renderer") );
86     set_category( CAT_VIDEO );
87     set_subcategory( SUBCAT_VIDEO_SUBPIC );
88
89     add_integer( "win32text-fontsize", 0, NULL, FONTSIZE_TEXT,
90                  FONTSIZE_LONGTEXT, VLC_TRUE );
91
92     /* opacity valid on 0..255, with default 255 = fully opaque */
93     add_integer_with_range( "win32-opacity", 255, 0, 255, NULL,
94         OPACITY_TEXT, OPACITY_LONGTEXT, VLC_FALSE );
95
96     /* hook to the color values list, with default 0x00ffffff = white */
97     add_integer( "win32text-color", 0x00FFFFFF, NULL, COLOR_TEXT,
98                  COLOR_LONGTEXT, VLC_TRUE );
99         change_integer_list( pi_color_values, ppsz_color_descriptions, 0 );
100
101     add_integer( "win32text-rel-fontsize", 16, NULL, FONTSIZER_TEXT,
102                  FONTSIZER_LONGTEXT, VLC_FALSE );
103         change_integer_list( pi_sizes, ppsz_sizes_text, 0 );
104
105     set_capability( "text renderer", 50 );
106     add_shortcut( "text" );
107     set_callbacks( Create, Destroy );
108 vlc_module_end();
109
110 /*****************************************************************************
111  * filter_sys_t: win32text local data
112  *****************************************************************************/
113 struct filter_sys_t
114 {
115     uint8_t        i_font_opacity;
116     int            i_font_color;
117     int            i_font_size;
118
119     int            i_default_font_size;
120     int            i_display_height;
121
122     HDC hcdc;
123     HFONT hfont;
124     HFONT hfont_bak;
125     int i_logpy;
126 };
127
128 static uint8_t pi_gamma[16] =
129   {0x00, 0x41, 0x52, 0x63, 0x84, 0x85, 0x96, 0xa7, 0xb8, 0xc9,
130    0xca, 0xdb, 0xdc, 0xed, 0xee, 0xff};
131
132 /*****************************************************************************
133  * Create: creates the module
134  *****************************************************************************/
135 static int Create( vlc_object_t *p_this )
136 {
137     filter_t *p_filter = (filter_t *)p_this;
138     filter_sys_t *p_sys;
139     char *psz_fontfile = NULL;
140     vlc_value_t val;
141     HDC hdc;
142
143     /* Allocate structure */
144     p_filter->p_sys = p_sys = malloc( sizeof( filter_sys_t ) );
145     if( !p_sys )
146     {
147         msg_Err( p_filter, "out of memory" );
148         return VLC_ENOMEM;
149     }
150     p_sys->i_font_size = 0;
151     p_sys->i_display_height = 0;
152
153     var_Create( p_filter, "win32text-font",
154                 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
155     var_Create( p_filter, "win32text-fontsize",
156                 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
157     var_Create( p_filter, "win32text-rel-fontsize",
158                 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
159     var_Create( p_filter, "win32text-opacity",
160                 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
161     var_Get( p_filter, "win32text-opacity", &val );
162     p_sys->i_font_opacity = __MAX( __MIN( val.i_int, 255 ), 0 );
163     var_Create( p_filter, "win32text-color",
164                 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
165     var_Get( p_filter, "win32text-color", &val );
166     p_sys->i_font_color = __MAX( __MIN( val.i_int, 0xFFFFFF ), 0 );
167
168     p_sys->hfont = p_sys->hfont_bak = 0;
169     hdc = GetDC( NULL );
170     p_sys->hcdc = CreateCompatibleDC( hdc );
171     p_sys->i_logpy = GetDeviceCaps( hdc, LOGPIXELSY );
172     ReleaseDC( NULL, hdc );
173     SetBkMode( p_sys->hcdc, TRANSPARENT );
174
175     var_Get( p_filter, "win32text-fontsize", &val );
176     p_sys->i_default_font_size = val.i_int;
177     if( SetFont( p_filter, 0 ) != VLC_SUCCESS ) goto error;
178
179     if( psz_fontfile ) free( psz_fontfile );
180     p_filter->pf_render_text = RenderText;
181     return VLC_SUCCESS;
182
183  error:
184     if( psz_fontfile ) free( psz_fontfile );
185     free( p_sys );
186     return VLC_EGENERIC;
187 }
188
189 /*****************************************************************************
190  * Destroy: destroy the module
191  *****************************************************************************/
192 static void Destroy( vlc_object_t *p_this )
193 {
194     filter_t *p_filter = (filter_t *)p_this;
195     filter_sys_t *p_sys = p_filter->p_sys;
196
197     if( p_sys->hfont_bak ) SelectObject( p_sys->hcdc, p_sys->hfont_bak );
198     if( p_sys->hfont ) DeleteObject( p_sys->hfont );
199     DeleteDC( p_sys->hcdc );
200     free( p_sys );
201 }
202
203 /*****************************************************************************
204  * Render: place string in picture
205  *****************************************************************************
206  * This function merges the previously rendered win32text glyphs into a picture
207  *****************************************************************************/
208 static int Render( filter_t *p_filter, subpicture_region_t *p_region,
209                    uint8_t *p_bitmap, int i_width, int i_height )
210 {
211     uint8_t *p_dst;
212     video_format_t fmt;
213     int i, i_pitch;
214     subpicture_region_t *p_region_tmp;
215     vlc_bool_t b_outline = VLC_TRUE;
216
217     /* Create a new subpicture region */
218     memset( &fmt, 0, sizeof(video_format_t) );
219     fmt.i_chroma = VLC_FOURCC('Y','U','V','P');
220     fmt.i_width = fmt.i_visible_width = i_width + (b_outline ? 4 : 0);
221     fmt.i_height = fmt.i_visible_height = i_height + (b_outline ? 4 : 0);
222     fmt.i_x_offset = fmt.i_y_offset = 0;
223     p_region_tmp = spu_CreateRegion( p_filter, &fmt );
224     if( !p_region_tmp )
225     {
226         msg_Err( p_filter, "cannot allocate SPU region" );
227         return VLC_EGENERIC;
228     }
229
230     /* Build palette */
231     fmt.p_palette->i_entries = 16;
232     for( i = 0; i < fmt.p_palette->i_entries; i++ )
233     {
234         fmt.p_palette->palette[i][0] = pi_gamma[i];
235         fmt.p_palette->palette[i][1] = 128;
236         fmt.p_palette->palette[i][2] = 128;
237         fmt.p_palette->palette[i][3] = pi_gamma[i];
238     }
239
240     p_region->fmt = p_region_tmp->fmt;
241     p_region->picture = p_region_tmp->picture;
242     free( p_region_tmp );
243
244     p_dst = p_region->picture.Y_PIXELS;
245     i_pitch = p_region->picture.Y_PITCH;
246
247     if( b_outline )
248     {
249         memset( p_dst, 0, i_pitch * fmt.i_height );
250         p_dst += p_region->picture.Y_PITCH * 2 + 2;
251     }
252
253     for( i = 0; i < i_height; i++ )
254     {
255         memcpy( p_dst, p_bitmap, i_width );
256         p_bitmap += (i_width+3) & ~3;
257         p_dst += i_pitch;
258     }
259
260     /* Outlining (find something better than nearest neighbour filtering ?) */
261     if( b_outline )
262     {
263         uint8_t *p_top = p_dst; /* Use 1st line as a cache */
264         uint8_t left, current;
265         int x, y;
266
267         p_dst = p_region->picture.Y_PIXELS;
268
269         for( y = 1; y < (int)fmt.i_height - 1; y++ )
270         {
271             memcpy( p_top, p_dst, fmt.i_width );
272             p_dst += i_pitch;
273             left = 0;
274
275             for( x = 1; x < (int)fmt.i_width - 1; x++ )
276             {
277                 current = p_dst[x];
278                 p_dst[x] = ( 4 * (int)p_dst[x] + left + p_top[x] + p_dst[x+1] +
279                              p_dst[x + i_pitch]) / 8;
280                 left = current;
281             }
282         }
283         memset( p_top, 0, fmt.i_width );
284     }
285
286     return VLC_SUCCESS;
287 }
288
289 static int RenderText( filter_t *p_filter, subpicture_region_t *p_region_out,
290                        subpicture_region_t *p_region_in )
291 {
292     filter_sys_t *p_sys = p_filter->p_sys;
293     int i_font_color, i_font_alpha, i_font_size;
294     uint8_t *p_bitmap;
295     TCHAR *psz_string;
296     int i, i_width, i_height;
297     HBITMAP bitmap, bitmap_bak;
298     BITMAPINFO *p_bmi;
299     RECT rect = {0};
300     SIZE size;
301
302     /* Sanity check */
303     if( !p_region_in || !p_region_out ) return VLC_EGENERIC;
304 #ifdef UNICODE
305     psz_string = malloc( (strlen( p_region_in->psz_text )+1) * sizeof(TCHAR) );
306     if( mbstowcs( psz_string, p_region_in->psz_text,
307                   strlen( p_region_in->psz_text ) * sizeof(TCHAR) ) < 0 )
308     {
309         free( psz_string );
310         return VLC_EGENERIC;
311     }
312 #else
313     psz_string = strdup( p_region_in->psz_text );
314 #endif
315     if( !psz_string || !*psz_string ) return VLC_EGENERIC;
316
317     i_font_color = __MAX( __MIN( p_region_in->i_text_color, 0xFFFFFF ), 0 );
318     if( i_font_color == 0xFFFFFF ) i_font_color = p_sys->i_font_color;
319
320     i_font_alpha = __MAX( __MIN( p_region_in->i_text_alpha, 255 ), 0 );
321     if( !i_font_alpha ) i_font_alpha = 255 - p_sys->i_font_opacity;
322
323     i_font_size  = __MAX( __MIN( p_region_in->i_text_size, 255 ), 0 );
324
325     SetFont( p_filter, i_font_size );
326
327     SetTextColor( p_sys->hcdc, RGB( (i_font_color >> 16) & 0xff,
328                   (i_font_color >> 8) & 0xff, i_font_color & 0xff) );
329
330     GetTextExtentExPoint( p_sys->hcdc, psz_string, _tcslen(psz_string),
331                           0, 0, 0, &size );
332     i_width = rect.right = size.cx; i_height = rect.bottom = size.cy;
333
334     p_bmi = malloc(sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD)*16);
335     memset( p_bmi, 0, sizeof(BITMAPINFOHEADER) );
336     p_bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
337     p_bmi->bmiHeader.biWidth = (i_width+3) & ~3;
338     p_bmi->bmiHeader.biHeight = - i_height;
339     p_bmi->bmiHeader.biPlanes = 1;
340     p_bmi->bmiHeader.biBitCount = 8;
341     p_bmi->bmiHeader.biCompression = BI_RGB;
342     p_bmi->bmiHeader.biClrUsed = 16;
343
344     for( i = 0; i < 16; i++ )
345     {
346         p_bmi->bmiColors[i].rgbBlue =
347             p_bmi->bmiColors[i].rgbGreen =
348                 p_bmi->bmiColors[i].rgbRed = pi_gamma[i];
349     }
350
351     bitmap = CreateDIBSection( p_sys->hcdc, p_bmi, DIB_RGB_COLORS,
352                                (void **)&p_bitmap, NULL, 0 );
353     if( !bitmap )
354     {
355         msg_Err( p_filter, "could not create bitmap" );
356         return VLC_EGENERIC;
357     }
358
359     bitmap_bak = SelectObject( p_sys->hcdc, bitmap );
360     FillRect( p_sys->hcdc, &rect, (HBRUSH)GetStockObject(BLACK_BRUSH) );
361
362     //TextOut( p_sys->hcdc, 0, 0, psz_string, strlen(psz_string) );
363     if( !DrawText( p_sys->hcdc, psz_string, -1, &rect, 0 ) )
364     {
365         msg_Err( p_filter, "could not draw text" );
366     }
367
368     p_region_out->i_x = p_region_in->i_x;
369     p_region_out->i_y = p_region_in->i_y;
370     Render( p_filter, p_region_out, p_bitmap, i_width, i_height );
371
372     SelectObject( p_sys->hcdc, bitmap_bak );
373     DeleteObject( bitmap );
374     return VLC_SUCCESS;
375 }
376
377 static int SetFont( filter_t *p_filter, int i_size )
378 {
379     filter_sys_t *p_sys = p_filter->p_sys;
380     LOGFONT logfont;
381
382     if( i_size && i_size == p_sys->i_font_size ) return VLC_SUCCESS;
383
384     if( !i_size )
385     {
386         vlc_value_t val;
387
388         if( !p_sys->i_default_font_size &&
389             p_sys->i_display_height == (int)p_filter->fmt_out.video.i_height )
390             return VLC_SUCCESS;
391
392         if( p_sys->i_default_font_size )
393         {
394             i_size = p_sys->i_default_font_size;
395         }
396         else
397         {
398             var_Get( p_filter, "win32text-rel-fontsize", &val );
399             i_size = (int)p_filter->fmt_out.video.i_height / val.i_int;
400             p_filter->p_sys->i_display_height =
401                 p_filter->fmt_out.video.i_height;
402         }
403         if( i_size <= 0 )
404         {
405             msg_Warn( p_filter, "Invalid fontsize, using 12" );
406             i_size = 12;
407         }
408
409         msg_Dbg( p_filter, "Using fontsize: %i", i_size );
410     }
411
412     p_sys->i_font_size = i_size;
413
414     if( p_sys->hfont_bak ) SelectObject( p_sys->hcdc, p_sys->hfont_bak );
415     if( p_sys->hfont ) DeleteObject( p_sys->hfont );
416
417     i_size = i_size * (int64_t)p_sys->i_logpy / 72;
418
419     logfont.lfHeight = i_size;
420     logfont.lfWidth = 0;
421     logfont.lfEscapement = 0;
422     logfont.lfOrientation = 0;
423     logfont.lfWeight = 0;
424     logfont.lfItalic = FALSE;
425     logfont.lfUnderline = FALSE;
426     logfont.lfStrikeOut = FALSE;
427     logfont.lfCharSet = ANSI_CHARSET;
428     logfont.lfOutPrecision = OUT_DEFAULT_PRECIS;
429     logfont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
430     logfont.lfQuality = ANTIALIASED_QUALITY;
431     logfont.lfPitchAndFamily = DEFAULT_PITCH;
432     memcpy( logfont.lfFaceName, _T("Arial"), sizeof(_T("Arial")) );
433
434     p_sys->hfont = CreateFontIndirect( &logfont );
435
436     p_sys->hfont_bak = SelectObject( p_sys->hcdc, p_sys->hfont );
437
438     return VLC_SUCCESS;
439 }