1 /*****************************************************************************
2 * freetype.c : Put text on the video, using freetype2
3 *****************************************************************************
4 * Copyright (C) 2002, 2003 VideoLAN
7 * Authors: Sigmund Augdal <sigmunau@idi.ntnu.no>
8 * Gildas Bazin <gbazin@videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #include <stdlib.h> /* malloc(), free() */
31 #ifdef HAVE_LINUX_LIMITS_H
32 # include <linux/limits.h>
38 #include "vlc_block.h"
39 #include "vlc_filter.h"
48 #include FT_FREETYPE_H
52 #define DEFAULT_FONT "/System/Library/Fonts/LucidaGrande.dfont"
53 #elif defined( SYS_BEOS )
54 #define DEFAULT_FONT "/boot/beos/etc/fonts/ttfonts/Swiss721.ttf"
55 #elif defined( WIN32 )
56 #define DEFAULT_FONT "" /* Default font found at run-time */
58 #define DEFAULT_FONT "/usr/share/fonts/truetype/freefont/FreeSerifBold.ttf"
61 #if defined(HAVE_ICONV)
64 #if defined(HAVE_FRIBIDI)
65 #include <fribidi/fribidi.h>
68 typedef struct line_desc_t line_desc_t;
70 /*****************************************************************************
72 *****************************************************************************/
73 static int Create ( vlc_object_t * );
74 static void Destroy( vlc_object_t * );
76 static subpicture_t *RenderText( filter_t *, block_t * );
78 #if !defined(HAVE_ICONV)
79 static int GetUnicodeCharFromUTF8( byte_t ** );
82 static line_desc_t *NewLine( byte_t * );
84 /*****************************************************************************
86 *****************************************************************************/
87 #define FONT_TEXT N_("Font")
88 #define FONT_LONGTEXT N_("Font filename")
89 #define FONTSIZE_TEXT N_("Font size in pixels")
90 #define FONTSIZE_LONGTEXT N_("The size of the fonts used by the osd module. " \
91 "If set to something different than 0 this option will override the " \
92 "relative font size " )
93 #define FONTSIZER_TEXT N_("Font size")
94 #define FONTSIZER_LONGTEXT N_("The size of the fonts used by the osd module" )
96 static int pi_sizes[] = { 20, 18, 16, 12, 6 };
97 static char *ppsz_sizes_text[] = { N_("Smaller"), N_("Small"), N_("Normal"),
98 N_("Large"), N_("Larger") };
101 set_description( _("freetype2 font renderer") );
103 add_file( "freetype-font", DEFAULT_FONT, NULL, FONT_TEXT, FONT_LONGTEXT,
105 add_integer( "freetype-fontsize", 0, NULL, FONTSIZE_TEXT,
106 FONTSIZE_LONGTEXT, VLC_TRUE );
107 add_integer( "freetype-rel-fontsize", 16, NULL, FONTSIZER_TEXT,
108 FONTSIZER_LONGTEXT, VLC_FALSE );
109 change_integer_list( pi_sizes, ppsz_sizes_text, 0 );
111 set_capability( "text renderer", 100 );
112 add_shortcut( "text" );
113 set_callbacks( Create, Destroy );
117 * Private data in a subpicture. Describes a string.
119 typedef struct subpicture_data_t
123 /** The string associated with this subpicture */
125 line_desc_t *p_lines;
131 /** NULL-terminated list of glyphs making the string */
132 FT_BitmapGlyph *pp_glyphs;
133 /** list of relative positions for the glyphs */
134 FT_Vector *p_glyph_pos;
140 static void Render ( filter_t *, subpicture_t *, subpicture_data_t * );
141 static void FreeString( subpicture_data_t * );
143 /*****************************************************************************
144 * filter_sys_t: freetype local data
145 *****************************************************************************
146 * This structure is part of the video output thread descriptor.
147 * It describes the freetype specific properties of an output thread.
148 *****************************************************************************/
151 FT_Library p_library; /* handle to library */
152 FT_Face p_face; /* handle to face object */
153 vlc_bool_t i_use_kerning;
154 uint8_t pi_gamma[256];
157 /*****************************************************************************
158 * Create: allocates osd-text video thread output method
159 *****************************************************************************
160 * This function allocates and initializes a Clone vout method.
161 *****************************************************************************/
162 #define gamma_value 2.0
163 static int Create( vlc_object_t *p_this )
165 filter_t *p_filter = (filter_t *)p_this;
167 char *psz_fontfile = NULL;
170 double gamma_inv = 1.0f / gamma_value;
173 /* Allocate structure */
174 p_sys = malloc( sizeof( filter_sys_t ) );
177 msg_Err( p_filter, "out of memory" );
181 p_sys->p_library = 0;
183 for( i = 0; i < 256; i++ )
186 (uint8_t)( pow( (double)i / 255.0f, gamma_inv) * 255.0f );
189 var_Create( p_filter, "freetype-font", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
190 var_Create( p_filter, "freetype-fontsize",
191 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
192 var_Create( p_filter, "freetype-rel-fontsize",
193 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
195 /* Look what method was requested */
196 var_Get( p_filter, "freetype-font", &val );
197 psz_fontfile = val.psz_string;
198 if( !psz_fontfile || !*psz_fontfile )
200 if( psz_fontfile ) free( psz_fontfile );
201 psz_fontfile = (char *)malloc( PATH_MAX + 1 );
203 GetWindowsDirectory( psz_fontfile, PATH_MAX + 1 );
204 strcat( psz_fontfile, "\\fonts\\arial.ttf" );
206 strcpy( psz_fontfile, DEFAULT_FONT );
208 msg_Err( p_filter, "user didn't specify a font" );
213 i_error = FT_Init_FreeType( &p_sys->p_library );
216 msg_Err( p_filter, "couldn't initialize freetype" );
220 i_error = FT_New_Face( p_sys->p_library, psz_fontfile ? psz_fontfile : "",
222 if( i_error == FT_Err_Unknown_File_Format )
224 msg_Err( p_filter, "file %s have unknown format", psz_fontfile );
229 msg_Err( p_filter, "failed to load font file %s", psz_fontfile );
233 i_error = FT_Select_Charmap( p_sys->p_face, ft_encoding_unicode );
236 msg_Err( p_filter, "Font has no unicode translation table" );
240 p_sys->i_use_kerning = FT_HAS_KERNING( p_sys->p_face );
242 var_Get( p_filter, "freetype-fontsize", &val );
245 i_fontsize = val.i_int;
249 var_Get( p_filter, "freetype-rel-fontsize", &val );
250 msg_Err( p_filter, "freetype-rel-font currenlty broken, FIXME" );
251 i_fontsize = (int) /*p_filter->render.i_height*/ 400 / val.i_int;
253 msg_Dbg( p_filter, "Using fontsize: %i", i_fontsize);
255 i_error = FT_Set_Pixel_Sizes( p_sys->p_face, 0, i_fontsize );
258 msg_Err( p_filter, "couldn't set font size to %d", i_fontsize );
262 if( psz_fontfile ) free( psz_fontfile );
263 p_filter->pf_render_string = RenderText;
264 p_filter->p_sys = p_sys;
268 if( p_sys->p_face ) FT_Done_Face( p_sys->p_face );
269 if( p_sys->p_library ) FT_Done_FreeType( p_sys->p_library );
270 if( psz_fontfile ) free( psz_fontfile );
275 /*****************************************************************************
276 * Destroy: destroy Clone video thread output method
277 *****************************************************************************
278 * Clean up all data and library connections
279 *****************************************************************************/
280 static void Destroy( vlc_object_t *p_this )
282 filter_t *p_filter = (filter_t *)p_this;
283 filter_sys_t *p_sys = p_filter->p_sys;
284 FT_Done_Face( p_sys->p_face );
285 FT_Done_FreeType( p_sys->p_library );
289 /*****************************************************************************
290 * Render: place string in picture
291 *****************************************************************************
292 * This function merges the previously rendered freetype glyphs into a picture
293 *****************************************************************************/
294 static void Render( filter_t *p_filter, subpicture_t *p_spu,
295 subpicture_data_t *p_string )
297 filter_sys_t *p_sys = p_filter->p_sys;
299 uint8_t *p_y, *p_u, *p_v, *p_a;
301 int i, x, y, i_pitch;
303 /* Create a new subpicture region */
304 memset( &fmt, 0, sizeof(video_format_t) );
305 fmt.i_chroma = VLC_FOURCC('Y','U','V','A');
306 fmt.i_aspect = VOUT_ASPECT_FACTOR;
307 fmt.i_width = fmt.i_visible_width = p_string->i_width + 2;
308 fmt.i_height = fmt.i_visible_height = p_string->i_height + 2;
309 fmt.i_x_offset = fmt.i_y_offset = 0;
310 p_spu->p_region = p_spu->pf_create_region( VLC_OBJECT(p_filter), &fmt );
311 if( !p_spu->p_region )
313 msg_Err( p_filter, "cannot allocate SPU region" );
317 p_spu->p_region->i_x = p_spu->p_region->i_y = 0;
318 p_y = p_spu->p_region->picture.Y_PIXELS;
319 p_u = p_spu->p_region->picture.U_PIXELS;
320 p_v = p_spu->p_region->picture.V_PIXELS;
321 p_a = p_spu->p_region->picture.A_PIXELS;
322 i_pitch = p_spu->p_region->picture.Y_PITCH;
324 /* Initialize the region pixels (only the alpha will be changed later) */
325 memset( p_y, 0x00, i_pitch * p_spu->p_region->fmt.i_height );
326 memset( p_u, 0x80, i_pitch * p_spu->p_region->fmt.i_height );
327 memset( p_v, 0x80, i_pitch * p_spu->p_region->fmt.i_height );
328 memset( p_a, 0x00, i_pitch * p_spu->p_region->fmt.i_height );
330 #define pi_gamma p_sys->pi_gamma
332 for( p_line = p_string->p_lines; p_line != NULL; p_line = p_line->p_next )
334 int i_glyph_tmax = 0;
335 int i_bitmap_offset, i_offset;
336 for( i = 0; p_line->pp_glyphs[i] != NULL; i++ )
338 FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ];
339 i_glyph_tmax = __MAX( i_glyph_tmax, p_glyph->top );
342 for( i = 0; p_line->pp_glyphs[i] != NULL; i++ )
344 FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ];
346 i_offset = ( p_line->p_glyph_pos[ i ].y +
347 i_glyph_tmax - p_glyph->top + 1 ) *
348 i_pitch + p_line->p_glyph_pos[ i ].x + p_glyph->left + 1;
350 for( y = 0, i_bitmap_offset = 0; y < p_glyph->bitmap.rows; y++ )
352 for( x = 0; x < p_glyph->bitmap.width; x++, i_bitmap_offset++ )
354 if( !pi_gamma[p_glyph->bitmap.buffer[i_bitmap_offset]] )
358 p_a[i_offset + x] = ((uint16_t)p_a[i_offset + x] +
359 pi_gamma[p_glyph->bitmap.buffer[i_bitmap_offset]])/2;
360 i_offset += i_pitch; x--;
361 p_a[i_offset + x] = ((uint16_t)p_a[i_offset + x] +
362 pi_gamma[p_glyph->bitmap.buffer[i_bitmap_offset]])/2;
364 p_a[i_offset + x] = ((uint16_t)p_a[i_offset + x] +
365 pi_gamma[p_glyph->bitmap.buffer[i_bitmap_offset]])/2;
366 i_offset += i_pitch; x--;
367 p_a[i_offset + x] = ((uint16_t)p_a[i_offset + x] +
368 pi_gamma[p_glyph->bitmap.buffer[i_bitmap_offset]])/2;
374 i_offset = ( p_line->p_glyph_pos[ i ].y +
375 i_glyph_tmax - p_glyph->top ) *
376 i_pitch + p_line->p_glyph_pos[ i ].x + p_glyph->left + 1;
378 for( y = 0, i_bitmap_offset = 0; y < p_glyph->bitmap.rows; y++ )
380 for( x = 0; x < p_glyph->bitmap.width; x++, i_bitmap_offset++ )
383 pi_gamma[p_glyph->bitmap.buffer[i_bitmap_offset]];
394 * This function receives a string and creates a subpicture for it. It
395 * also calculates the size needed for this string, and renders the
396 * needed glyphs into memory. It is used as pf_add_string callback in
397 * the vout method by this module
399 static subpicture_t *RenderText( filter_t *p_filter, block_t *p_block )
401 filter_sys_t *p_sys = p_filter->p_sys;
402 subpicture_t *p_subpic = 0;
403 subpicture_data_t *p_string = 0;
404 line_desc_t *p_line = 0, *p_next = 0;
405 int i, i_pen_y, i_pen_x, i_error, i_glyph_index, i_previous;
406 uint32_t *p_unicode_string, i_char;
410 #if defined(HAVE_ICONV)
411 iconv_t iconv_handle;
420 if( !p_block ) return NULL;
421 psz_string = p_block->p_buffer;
422 if( !psz_string || !*psz_string ) goto error;
431 /* Create and initialize a subpicture */
432 p_subpic = p_filter->pf_spu_buffer_new( p_filter );
433 if( !p_subpic ) goto error;
435 p_subpic->i_start = p_block->i_pts;
436 p_subpic->i_stop = p_block->i_pts + p_block->i_length;
437 p_subpic->b_ephemer = (p_block->i_length == 0);
438 p_subpic->b_absolute = VLC_FALSE;
440 /* Create and initialize private data for the subpicture */
441 p_string = malloc( sizeof(subpicture_data_t) );
444 msg_Err( p_filter, "Out of memory" );
447 p_string->p_lines = 0;
448 p_string->psz_text = strdup( psz_string );
450 #if defined(HAVE_ICONV)
451 p_unicode_string = malloc( ( strlen(psz_string) + 1 ) * sizeof(uint32_t) );
452 if( p_unicode_string == NULL )
454 msg_Err( p_filter, "Out of memory" );
457 #if defined(WORDS_BIGENDIAN)
458 iconv_handle = iconv_open( "UCS-4BE", "UTF-8" );
460 iconv_handle = iconv_open( "UCS-4LE", "UTF-8" );
462 if( iconv_handle == (iconv_t)-1 )
464 msg_Warn( p_filter, "Unable to do convertion" );
469 char *p_in_buffer, *p_out_buffer;
470 size_t i_in_bytes, i_out_bytes, i_out_bytes_left, i_ret;
471 i_in_bytes = strlen( psz_string );
472 i_out_bytes = i_in_bytes * sizeof( uint32_t );
473 i_out_bytes_left = i_out_bytes;
474 p_in_buffer = psz_string;
475 p_out_buffer = (char *)p_unicode_string;
476 i_ret = iconv( iconv_handle, &p_in_buffer, &i_in_bytes,
477 &p_out_buffer, &i_out_bytes_left );
480 msg_Warn( p_filter, "Failed to convert string to unicode (%s), "
481 "bytes left %d", strerror(errno), i_in_bytes );
484 *(uint32_t*)p_out_buffer = 0;
485 i_string_length = (i_out_bytes - i_out_bytes_left) / sizeof(uint32_t);
488 #if defined(HAVE_FRIBIDI)
490 uint32_t *p_fribidi_string;
491 FriBidiCharType base_dir = FRIBIDI_TYPE_ON;
492 p_fribidi_string = malloc( (i_string_length + 1) * sizeof(uint32_t) );
493 fribidi_log2vis( (FriBidiChar*)p_unicode_string, i_string_length,
494 &base_dir, (FriBidiChar*)p_fribidi_string, 0, 0, 0 );
495 free( p_unicode_string );
496 p_unicode_string = p_fribidi_string;
497 p_fribidi_string[ i_string_length ] = 0;
502 /* Calculate relative glyph positions and a bounding box for the
504 p_line = NewLine( psz_string );
507 msg_Err( p_filter, "Out of memory" );
510 p_string->p_lines = p_line;
516 #define face p_sys->p_face
517 #define glyph face->glyph
519 while( *p_unicode_string )
521 i_char = *p_unicode_string++;
522 if( i_char == '\r' ) /* ignore CR chars wherever they may be */
529 p_next = NewLine( psz_string );
532 msg_Err( p_filter, "Out of memory" );
535 p_line->p_next = p_next;
536 p_line->i_width = line.xMax;
537 p_line->i_height = face->size->metrics.height >> 6;
538 p_line->pp_glyphs[ i ] = NULL;
540 result.x = __MAX( result.x, line.xMax );
541 result.y += face->size->metrics.height >> 6;
547 i_pen_y += face->size->metrics.height >> 6;
549 msg_Dbg( p_filter, "Creating new line, i is %d", i );
555 i_glyph_index = FT_Get_Char_Index( face, i_char );
556 if( p_sys->i_use_kerning && i_glyph_index
560 FT_Get_Kerning( face, i_previous, i_glyph_index,
561 ft_kerning_default, &delta );
562 i_pen_x += delta.x >> 6;
565 p_line->p_glyph_pos[ i ].x = i_pen_x;
566 p_line->p_glyph_pos[ i ].y = i_pen_y;
567 i_error = FT_Load_Glyph( face, i_glyph_index, FT_LOAD_DEFAULT );
570 msg_Err( p_filter, "FT_Load_Glyph returned %d", i_error );
573 i_error = FT_Get_Glyph( glyph, &tmp_glyph );
576 msg_Err( p_filter, "FT_Get_Glyph returned %d", i_error );
579 FT_Glyph_Get_CBox( tmp_glyph, ft_glyph_bbox_pixels, &glyph_size );
580 i_error = FT_Glyph_To_Bitmap( &tmp_glyph, ft_render_mode_normal,
582 if( i_error ) continue;
583 p_line->pp_glyphs[ i ] = (FT_BitmapGlyph)tmp_glyph;
586 line.xMax = p_line->p_glyph_pos[i].x + glyph_size.xMax - glyph_size.xMin;
587 line.yMax = __MAX( line.yMax, glyph_size.yMax );
588 line.yMin = __MIN( line.yMin, glyph_size.yMin );
590 i_previous = i_glyph_index;
591 i_pen_x += glyph->advance.x >> 6;
595 p_line->i_width = line.xMax;
596 p_line->i_height = face->size->metrics.height >> 6;
597 p_line->pp_glyphs[ i ] = NULL;
598 result.x = __MAX( result.x, line.xMax );
599 result.y += line.yMax - line.yMin;
600 p_string->i_height = result.y;
601 p_string->i_width = result.x;
606 Render( p_filter, p_subpic, p_string );
607 FreeString( p_string );
608 block_Release( p_block );
612 FreeString( p_string );
613 if( p_subpic ) p_filter->pf_spu_buffer_del( p_filter, p_subpic );
614 block_Release( p_block );
618 static void FreeString( subpicture_data_t *p_string )
621 line_desc_t *p_line, *p_next;
623 if( !p_string ) return;
625 for( p_line = p_string->p_lines; p_line != NULL; p_line = p_next )
627 p_next = p_line->p_next;
628 for( i = 0; p_line->pp_glyphs[ i ] != NULL; i++ )
630 FT_Done_Glyph( (FT_Glyph)p_line->pp_glyphs[ i ] );
632 free( p_line->pp_glyphs );
633 free( p_line->p_glyph_pos );
637 free( p_string->psz_text );
641 #if !defined( HAVE_ICONV )
642 /* convert one or more utf8 bytes into a unicode character */
643 static int GetUnicodeCharFromUTF8( byte_t **ppsz_utf8_string )
645 int i_remaining_bytes, i_char = 0;
646 if( ( **ppsz_utf8_string & 0xFC ) == 0xFC )
648 i_char = **ppsz_utf8_string & 1;
649 i_remaining_bytes = 5;
651 else if( ( **ppsz_utf8_string & 0xF8 ) == 0xF8 )
653 i_char = **ppsz_utf8_string & 3;
654 i_remaining_bytes = 4;
656 else if( ( **ppsz_utf8_string & 0xF0 ) == 0xF0 )
658 i_char = **ppsz_utf8_string & 7;
659 i_remaining_bytes = 3;
661 else if( ( **ppsz_utf8_string & 0xE0 ) == 0xE0 )
663 i_char = **ppsz_utf8_string & 15;
664 i_remaining_bytes = 2;
666 else if( ( **ppsz_utf8_string & 0xC0 ) == 0xC0 )
668 i_char = **ppsz_utf8_string & 31;
669 i_remaining_bytes = 1;
673 i_char = **ppsz_utf8_string;
674 i_remaining_bytes = 0;
676 while( i_remaining_bytes )
678 (*ppsz_utf8_string)++;
680 i_char = ( i_char << 6 ) + ( **ppsz_utf8_string & 0x3F );
682 (*ppsz_utf8_string)++;
687 static line_desc_t *NewLine( byte_t *psz_string )
690 line_desc_t *p_line = malloc( sizeof(line_desc_t) );
695 p_line->i_height = 0;
697 p_line->p_next = NULL;
699 /* We don't use CountUtf8Characters() here because we are not acutally
700 * sure the string is utf8. Better be safe than sorry. */
701 i_count = strlen( psz_string );
703 p_line->pp_glyphs = malloc( sizeof(FT_BitmapGlyph)
705 if( p_line->pp_glyphs == NULL )
710 p_line->p_glyph_pos = malloc( sizeof( FT_Vector )
712 if( p_line->p_glyph_pos == NULL )
714 free( p_line->pp_glyphs );