]> git.sesse.net Git - vlc/blob - modules/misc/freetype.c
* modules/misc/freetype.c: compilation fix for windows
[vlc] / modules / misc / freetype.c
1 /*****************************************************************************
2  * freetype.c : Put text on the video, using freetype2
3  *****************************************************************************
4  * Copyright (C) 2002, 2003 VideoLAN
5  * $Id: freetype.c,v 1.5 2003/07/20 16:56:58 ipkiss Exp $
6  *
7  * Authors: Sigmund Augdal <sigmunau@idi.ntnu.no>
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 <osd.h>
33 #include <math.h>
34
35 #include <ft2build.h>
36 #include FT_FREETYPE_H
37 #include FT_GLYPH_H
38
39 /*****************************************************************************
40  * Local prototypes
41  *****************************************************************************/
42 static int  Create    ( vlc_object_t * );
43 static void Destroy   ( vlc_object_t * );
44
45 static void Render    ( vout_thread_t *, picture_t *,
46                         const subpicture_t * );
47 static void RenderI420( vout_thread_t *, picture_t *,
48                         const subpicture_t * );
49 static int  AddText   ( vout_thread_t *, byte_t *, text_style_t *, int,
50                         int, int, mtime_t, mtime_t );
51 static int GetUnicodeCharFromUTF8( byte_t ** );
52
53 /*****************************************************************************
54  * Module descriptor
55  *****************************************************************************/
56 #define FONT_TEXT N_("Font")
57 #define FONT_LONGTEXT N_("Filename of Font")
58 #define FONTSIZE_TEXT N_("Font size")
59 #define FONTSIZE_LONGTEXT N_("The size of the fonts used by the osd module" )
60
61 vlc_module_begin();
62     add_category_hint( N_("Fonts"), NULL, VLC_FALSE );
63     add_file( "freetype-font", "", NULL, FONT_TEXT, FONT_LONGTEXT, VLC_FALSE );
64     add_integer( "freetype-fontsize", 16, NULL, FONTSIZE_TEXT, FONTSIZE_LONGTEXT, VLC_FALSE );
65     set_description( _("freetype2 font renderer") );
66     set_capability( "text renderer", 100 );
67     add_shortcut( "text" );
68     set_callbacks( Create, Destroy );
69 vlc_module_end();
70
71 /**
72  Describes a string to be displayed on the video, or a linked list of
73  such
74 */
75 struct subpicture_sys_t
76 {
77     int            i_x_margin;
78     int            i_y_margin;
79     int            i_width;
80     int            i_height;
81     int            i_flags;
82     /** The string associated with this subpicture */
83     byte_t        *psz_text;
84     /** NULL-terminated list of glyphs making the string */
85     FT_BitmapGlyph      *pp_glyphs;
86     /** list of relative positions for the glyphs */
87     FT_Vector     *p_glyph_pos;
88 };
89
90 /*****************************************************************************
91  * vout_sys_t: osd_text local data
92  *****************************************************************************
93  * This structure is part of the video output thread descriptor.
94  * It describes the osd-text specific properties of an output thread.
95  *****************************************************************************/
96 struct text_renderer_sys_t
97 {
98     FT_Library     p_library;   /* handle to library     */
99     FT_Face        p_face;      /* handle to face object */
100     vlc_mutex_t   *lock;
101     vlc_bool_t     i_use_kerning;
102     uint8_t        pi_gamma[256];
103 };
104 /* more prototypes */
105 //static void ComputeBoundingBox( subpicture_sys_t * );
106 static void FreeString( subpicture_t * );
107
108 /*****************************************************************************
109  * Create: allocates osd-text video thread output method
110  *****************************************************************************
111  * This function allocates and initializes a Clone vout method.
112  *****************************************************************************/
113 #define gamma_value 2.0
114 static int Create( vlc_object_t *p_this )
115 {
116     vout_thread_t *p_vout = (vout_thread_t *)p_this;
117     char *psz_fontfile;
118     int i, i_error;
119     double gamma_inv = 1.0f / gamma_value;
120
121     /* Allocate structure */
122     p_vout->p_text_renderer_data = malloc( sizeof( text_renderer_sys_t ) );
123     if( p_vout->p_text_renderer_data == NULL )
124     {
125         msg_Err( p_vout, "out of memory" );
126         return VLC_ENOMEM;
127     }
128
129     for (i = 0; i < 256; i++) {
130         p_vout->p_text_renderer_data->pi_gamma[i] =
131             (uint8_t)( pow( (double)i / 255.0f, gamma_inv) * 255.0f );
132         //msg_Dbg( p_vout, "%d", p_vout->p_text_renderer_data->pi_gamma[i]);
133     }
134
135     /* Look what method was requested */
136     psz_fontfile = config_GetPsz( p_vout, "freetype-font" );
137     i_error = FT_Init_FreeType( &p_vout->p_text_renderer_data->p_library );
138     if( i_error )
139     {
140         msg_Err( p_vout, "couldn't initialize freetype" );
141         free( p_vout->p_text_renderer_data );
142         return VLC_EGENERIC;
143     }
144     i_error = FT_New_Face( p_vout->p_text_renderer_data->p_library,
145                            psz_fontfile, 0,
146                            &p_vout->p_text_renderer_data->p_face );
147     if( i_error == FT_Err_Unknown_File_Format )
148     {
149         msg_Err( p_vout, "file %s have unknown format", psz_fontfile );
150         FT_Done_FreeType( p_vout->p_text_renderer_data->p_library );
151         free( p_vout->p_text_renderer_data );
152         return VLC_EGENERIC;
153     }
154     else if( i_error )
155     {
156         msg_Err( p_vout, "failed to load font file" );
157         FT_Done_FreeType( p_vout->p_text_renderer_data->p_library );
158         free( p_vout->p_text_renderer_data );
159         return VLC_EGENERIC;
160     }
161     i_error = FT_Select_Charmap( p_vout->p_text_renderer_data->p_face,
162                                  ft_encoding_unicode );
163     if ( i_error )
164     {
165         msg_Err( p_vout, "Font has no unicode translation table" );
166         FT_Done_Face( p_vout->p_text_renderer_data->p_face );
167         FT_Done_FreeType( p_vout->p_text_renderer_data->p_library );
168         free( p_vout->p_text_renderer_data );
169         return VLC_EGENERIC;
170     }
171
172     p_vout->p_text_renderer_data->i_use_kerning =
173         FT_HAS_KERNING(p_vout->p_text_renderer_data->p_face);
174
175     i_error = FT_Set_Pixel_Sizes( p_vout->p_text_renderer_data->p_face, 0,
176                                   config_GetInt( p_vout, "freetype-fontsize" ) );
177     if( i_error )
178     {
179         msg_Err( p_vout, "couldn't set font size to %d",
180                  config_GetInt( p_vout, "osd-fontsize" ) );
181         free( p_vout->p_text_renderer_data );
182         return VLC_EGENERIC;
183     }
184     p_vout->pf_add_string = AddText;
185     return VLC_SUCCESS;
186 }
187
188 /*****************************************************************************
189  * Destroy: destroy Clone video thread output method
190  *****************************************************************************
191  * Clean up all data and library connections
192  *****************************************************************************/
193 static void Destroy( vlc_object_t *p_this )
194 {
195     vout_thread_t *p_vout = (vout_thread_t *)p_this;
196     FT_Done_Face( p_vout->p_text_renderer_data->p_face );
197     FT_Done_FreeType( p_vout->p_text_renderer_data->p_library );
198     free( p_vout->p_text_renderer_data );
199 }
200
201 /*****************************************************************************
202  * Render: place string in picture
203  *****************************************************************************
204  * This function merges the previously rendered freetype glyphs into a picture
205  *****************************************************************************/
206 static void Render( vout_thread_t *p_vout, picture_t *p_pic,
207                     const subpicture_t *p_subpic )
208 {
209     switch( p_vout->output.i_chroma )
210     {
211         /* I420 target, no scaling */
212         case VLC_FOURCC('I','4','2','0'):
213         case VLC_FOURCC('I','Y','U','V'):
214         case VLC_FOURCC('Y','V','1','2'):
215             RenderI420( p_vout, p_pic, p_subpic );
216             break;
217 #if 0
218         /* RV16 target, scaling */
219         case VLC_FOURCC('R','V','1','6'):
220             RenderRV16( p_vout, p_pic, p_spu, p_spu->p_sys->b_crop );
221             break;
222
223         /* RV32 target, scaling */
224         case VLC_FOURCC('R','V','2','4'):
225         case VLC_FOURCC('R','V','3','2'):
226             RenderRV32( p_vout, p_pic, p_spu, p_spu->p_sys->b_crop );
227             break;
228
229         /* NVidia overlay, no scaling */
230         case VLC_FOURCC('Y','U','Y','2'):
231             RenderYUY2( p_vout, p_pic, p_spu, p_spu->p_sys->b_crop );
232             break;
233 #endif
234         default:
235             msg_Err( p_vout, "unknown chroma, can't render SPU" );
236             break;
237     }
238 }
239
240 static void RenderI420( vout_thread_t *p_vout, picture_t *p_pic,
241                     const subpicture_t *p_subpic )
242 {
243     subpicture_sys_t *p_string = p_subpic->p_sys;
244     int i_plane, x, y, pen_x, pen_y;
245     unsigned int i;
246
247     for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
248     {
249         uint8_t *p_in;
250         int i_pitch = p_pic->p[i_plane].i_pitch;
251
252         p_in = p_pic->p[i_plane].p_pixels;
253
254         if ( i_plane == 0 )
255         {
256             if ( p_string->i_flags & OSD_ALIGN_BOTTOM )
257             {
258                 pen_y = p_pic->p[i_plane].i_lines - p_string->i_height -
259                     p_string->i_y_margin;
260             }
261             else
262             {
263                 pen_y = p_string->i_y_margin;
264             }
265             if ( p_string->i_flags & OSD_ALIGN_RIGHT )
266             {
267                 pen_x = i_pitch - p_string->i_width
268                     - p_string->i_x_margin;
269             }
270             else
271             {
272                 pen_x = p_string->i_x_margin;
273             }
274
275             for( i = 0; p_string->pp_glyphs[i] != NULL; i++ )
276             {
277                 if( p_string->pp_glyphs[i] )
278                 {
279                     FT_BitmapGlyph p_glyph = p_string->pp_glyphs[ i ];
280 #define alpha p_vout->p_text_renderer_data->pi_gamma[ p_glyph->bitmap.buffer[ x + y * p_glyph->bitmap.width ] ]
281 #define pixel p_in[ ( p_string->p_glyph_pos[ i ].y + pen_y + y - p_glyph->top ) * i_pitch+x + pen_x + p_string->p_glyph_pos[ i ].x + p_glyph->left ]
282                     for(y = 0; y < p_glyph->bitmap.rows; y++ )
283                     {
284                         for( x = 0; x < p_glyph->bitmap.width; x++ )
285                         {
286                             // pixel = alpha;
287                             // pixel = (pixel^alpha)^pixel;
288                             pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 ) +
289                                 ( 255 * alpha >> 8 );
290 #undef alpha
291 #undef pixel
292                         }
293                     }
294                 }
295             }
296         }
297         else
298         {
299             if ( p_string->i_flags & OSD_ALIGN_BOTTOM )
300             {
301                 pen_y = p_pic->p[i_plane].i_lines - ( p_string->i_height>>1) -
302                     (p_string->i_y_margin>>1);
303             }
304             else
305             {
306                 pen_y = p_string->i_y_margin >> 1;
307             }
308             if ( p_string->i_flags & OSD_ALIGN_RIGHT )
309             {
310                 pen_x = i_pitch - ( p_string->i_width >> 1 )
311                     - ( p_string->i_x_margin >> 1 );
312             }
313             else
314             {
315                 pen_x = p_string->i_x_margin >> 1;
316             }
317
318             for( i = 0; p_string->pp_glyphs[i] != NULL; i++ )
319             {
320                 if( p_string->pp_glyphs[i] )
321                 {
322                     FT_BitmapGlyph p_glyph = p_string->pp_glyphs[ i ];
323 #define alpha p_vout->p_text_renderer_data->pi_gamma[ p_glyph->bitmap.buffer[ ( x + y * p_glyph->bitmap.width ) ] ]
324 #define pixel p_in[ ( (p_string->p_glyph_pos[ i ].y>>1) + pen_y + (y>>1) -  ( p_glyph->top >> 1 ) ) * i_pitch + ( x >> 1 ) + pen_x + ( p_string->p_glyph_pos[ i ].x >> 1 ) + ( p_glyph->left >>1) ]
325                     for( y = 0; y < p_glyph->bitmap.rows; y+=2 )
326                     {
327                         for( x = 0; x < p_glyph->bitmap.width; x+=2 )
328                         {
329                             // pixel = alpha;
330                             // pixel = (pixel^alpha)^pixel;
331                             pixel = ( ( pixel * ( 0xFF - alpha ) ) >> 8 ) +
332                                 ( 0x80 * alpha >> 8 );
333 #undef alpha
334 #undef pixel
335                         }
336                     }
337                 }
338             }
339         }
340     }
341 }
342
343 /**
344  * This function receives a string and creates a subpicture for it. It
345  * also calculates the size needed for this string, and renders the
346  * needed glyphs into memory. It is used as pf_add_string callback in
347  * the vout method by this module
348  */
349 static int AddText ( vout_thread_t *p_vout, byte_t *psz_string,
350                      text_style_t *p_style, int i_flags, int i_hmargin,
351                      int i_vmargin, mtime_t i_start, mtime_t i_stop )
352 {
353     subpicture_sys_t *p_string;
354     int i, i_pen_y, i_pen_x, i_error, i_glyph_index, i_previous, i_char;
355     subpicture_t *p_subpic;
356     FT_BBox line;
357     FT_BBox glyph_size;
358     FT_Vector result;
359     FT_Glyph tmp_glyph;
360
361     result.x = 0;
362     result.y = 0;
363     line.xMin = 0;
364     line.xMax = 0;
365     line.yMin = 0;
366     line.yMax = 0;
367
368     /* Create and initialize a subpicture */
369     p_subpic = vout_CreateSubPicture( p_vout, MEMORY_SUBPICTURE );
370     if ( p_subpic == NULL )
371     {
372         return VLC_EGENERIC;
373     }
374     p_subpic->pf_render = Render;
375     p_subpic->pf_destroy = FreeString;
376     p_subpic->i_start = i_start;
377     p_subpic->i_stop = i_stop;
378     if( i_stop == 0 )
379     {
380         p_subpic->b_ephemer = VLC_TRUE;
381     }
382     else
383     {
384         p_subpic->b_ephemer = VLC_FALSE;
385     }
386
387     /* Create and initialize private data for the subpicture */
388     p_string = malloc( sizeof(subpicture_sys_t) );
389     if ( p_string == NULL )
390     {
391         vout_DestroySubPicture( p_vout, p_subpic );
392         return VLC_ENOMEM;
393     }
394     p_subpic->p_sys = p_string;
395     p_string->i_flags = i_flags;
396     p_string->i_x_margin = i_hmargin;
397     p_string->i_y_margin = i_vmargin;
398
399     p_string->psz_text = strdup( psz_string );
400     p_string->pp_glyphs = malloc( sizeof(FT_GlyphSlot)
401                                   * ( strlen( p_string->psz_text ) + 1 ) );
402     if( p_string->pp_glyphs == NULL )
403     {
404         msg_Err( p_vout, "Out of memory" );
405         return VLC_ENOMEM;
406     }
407     p_string->p_glyph_pos = malloc( sizeof( FT_Vector )
408                                   * strlen( p_string->psz_text ) );
409     if( p_string->p_glyph_pos == NULL )
410     {
411         msg_Err( p_vout, "Out of memory" );
412         return VLC_ENOMEM;
413     }
414
415     /* Calculate relative glyph positions and a bounding box for the
416      * entire string */
417     i_pen_x = 0;
418     i_pen_y = 0;
419     i_previous = 0;
420     i = 0;
421     while( *psz_string )
422     {
423         i_char = GetUnicodeCharFromUTF8( &psz_string );
424 #define face p_vout->p_text_renderer_data->p_face
425 #define glyph face->glyph
426         if ( i_char == '\n' )
427         {
428             i_pen_x = 0;
429             result.x = __MAX( result.x, line.xMax );
430             result.y += face->height >> 6;
431             line.xMin = 0;
432             line.xMax = 0;
433             line.yMin = 0;
434             line.yMax = 0;
435             i_pen_y += face->height >> 6;
436             continue;
437         }
438         i_glyph_index = FT_Get_Char_Index( face, i_char );
439         if ( p_vout->p_text_renderer_data->i_use_kerning && i_glyph_index
440             && i_previous )
441         {
442             FT_Vector delta;
443             FT_Get_Kerning( face, i_previous, i_glyph_index,
444                             ft_kerning_default, &delta );
445             i_pen_x += delta.x >> 6;
446
447         }
448         p_string->p_glyph_pos[ i ].x = i_pen_x;
449         p_string->p_glyph_pos[ i ].y = i_pen_y;
450         i_error = FT_Load_Glyph( face, i_glyph_index, FT_LOAD_DEFAULT );
451         if ( i_error )
452         {
453             msg_Err( p_vout, "FT_Load_Glyph returned %d", i_error );
454             return VLC_EGENERIC;
455         }
456         i_error = FT_Get_Glyph( glyph, &tmp_glyph );
457         if ( i_error )
458         {
459             msg_Err( p_vout, "FT_Get_Glyph returned %d", i_error );
460             return VLC_EGENERIC;
461         }
462         FT_Glyph_Get_CBox( tmp_glyph, ft_glyph_bbox_pixels, &glyph_size );
463         i_error = FT_Glyph_To_Bitmap( &tmp_glyph,
464                                       ft_render_mode_normal,
465                                       &p_string->p_glyph_pos[i],
466                                       1 );
467         if ( i_error ) continue;
468         p_string->pp_glyphs[ i ] = (FT_BitmapGlyph)tmp_glyph;
469
470         /* Do rest */
471         line.xMax = p_string->p_glyph_pos[i].x + glyph_size.xMax - glyph_size.xMin;
472         line.yMax = __MAX( line.yMax, glyph_size.yMax );
473         line.yMin = __MIN( line.yMin, glyph_size.yMin );
474
475         i_previous = i_glyph_index;
476         i_pen_x += glyph->advance.x >> 6;
477         i++;
478     }
479     p_string->pp_glyphs[i] = NULL;
480     result.x = __MAX( result.x, line.xMax );
481     result.y += line.yMax - line.yMin;
482     p_string->i_height = result.y;
483     p_string->i_width = result.x;
484     msg_Dbg( p_vout, "string height is %d, width is %d",
485              p_string->i_height, p_string->i_width );
486     msg_Dbg( p_vout, "adding string \"%s\" at (%d,%d) start_date "I64Fd
487              " end_date" I64Fd, p_string->psz_text, p_string->i_x_margin,
488              p_string->i_y_margin, i_start, i_stop );
489     vout_DisplaySubPicture( p_vout, p_subpic );
490     return VLC_SUCCESS;
491 }
492
493 static void FreeString( subpicture_t *p_subpic )
494 {
495     unsigned int i;
496     subpicture_sys_t *p_string = p_subpic->p_sys;
497     for ( i = 0; p_string->pp_glyphs[ i ] != NULL; i++ )
498     {
499         FT_Done_Glyph( (FT_Glyph)p_string->pp_glyphs[ i ] );
500     }
501     free( p_string->psz_text );
502     free( p_string->p_glyph_pos );
503     free( p_string->pp_glyphs );
504     free( p_string );
505 }
506
507 /* convert one or more utf8 bytes into a unicode character */
508 static int GetUnicodeCharFromUTF8( byte_t **ppsz_utf8_string )
509 {
510     int i_remaining_bytes, i_char = 0;
511     if( ( **ppsz_utf8_string & 0xFC ) == 0xFC )
512     {
513         i_char = **ppsz_utf8_string & 1;
514         i_remaining_bytes = 5;
515     }
516     else if( ( **ppsz_utf8_string & 0xF8 ) == 0xF8 )
517     {
518         i_char = **ppsz_utf8_string & 3;
519         i_remaining_bytes = 4;
520     }
521     else if( ( **ppsz_utf8_string & 0xF0 ) == 0xF0 )
522     {
523         i_char = **ppsz_utf8_string & 7;
524         i_remaining_bytes = 3;
525     }
526     else if( ( **ppsz_utf8_string & 0xE0 ) == 0xE0 )
527     {
528         i_char = **ppsz_utf8_string & 15;
529         i_remaining_bytes = 2;
530     }
531     else if( ( **ppsz_utf8_string & 0xC0 ) == 0xC0 )
532     {
533         i_char = **ppsz_utf8_string & 31;
534         i_remaining_bytes = 1;
535     }
536     else
537     {
538         i_char = **ppsz_utf8_string;
539         i_remaining_bytes = 0;
540     }
541     while( i_remaining_bytes )
542     {
543         (*ppsz_utf8_string)++;
544         i_remaining_bytes--;
545         i_char = ( i_char << 6 ) + ( **ppsz_utf8_string & 0x3F );
546     }
547     (*ppsz_utf8_string)++;
548     return i_char;
549 }