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 i_fontsize = (int)p_filter->fmt_out.video.i_height / val.i_int;
252 msg_Dbg( p_filter, "Using fontsize: %i", i_fontsize);
254 i_error = FT_Set_Pixel_Sizes( p_sys->p_face, 0, i_fontsize );
257 msg_Err( p_filter, "couldn't set font size to %d", i_fontsize );
261 if( psz_fontfile ) free( psz_fontfile );
262 p_filter->pf_render_string = RenderText;
263 p_filter->p_sys = p_sys;
267 if( p_sys->p_face ) FT_Done_Face( p_sys->p_face );
268 if( p_sys->p_library ) FT_Done_FreeType( p_sys->p_library );
269 if( psz_fontfile ) free( psz_fontfile );
274 /*****************************************************************************
275 * Destroy: destroy Clone video thread output method
276 *****************************************************************************
277 * Clean up all data and library connections
278 *****************************************************************************/
279 static void Destroy( vlc_object_t *p_this )
281 filter_t *p_filter = (filter_t *)p_this;
282 filter_sys_t *p_sys = p_filter->p_sys;
283 FT_Done_Face( p_sys->p_face );
284 FT_Done_FreeType( p_sys->p_library );
288 /*****************************************************************************
289 * Render: place string in picture
290 *****************************************************************************
291 * This function merges the previously rendered freetype glyphs into a picture
292 *****************************************************************************/
293 static void Render( filter_t *p_filter, subpicture_t *p_spu,
294 subpicture_data_t *p_string )
296 filter_sys_t *p_sys = p_filter->p_sys;
298 uint8_t *p_y, *p_u, *p_v, *p_a;
300 int i, x, y, i_pitch;
302 /* Create a new subpicture region */
303 memset( &fmt, 0, sizeof(video_format_t) );
304 fmt.i_chroma = VLC_FOURCC('Y','U','V','A');
305 fmt.i_aspect = VOUT_ASPECT_FACTOR;
306 fmt.i_width = fmt.i_visible_width = p_string->i_width + 2;
307 fmt.i_height = fmt.i_visible_height = p_string->i_height + 2;
308 fmt.i_x_offset = fmt.i_y_offset = 0;
309 p_spu->p_region = p_spu->pf_create_region( VLC_OBJECT(p_filter), &fmt );
310 if( !p_spu->p_region )
312 msg_Err( p_filter, "cannot allocate SPU region" );
316 p_spu->p_region->i_x = p_spu->p_region->i_y = 0;
317 p_y = p_spu->p_region->picture.Y_PIXELS;
318 p_u = p_spu->p_region->picture.U_PIXELS;
319 p_v = p_spu->p_region->picture.V_PIXELS;
320 p_a = p_spu->p_region->picture.A_PIXELS;
321 i_pitch = p_spu->p_region->picture.Y_PITCH;
323 /* Initialize the region pixels (only the alpha will be changed later) */
324 memset( p_y, 0x00, i_pitch * p_spu->p_region->fmt.i_height );
325 memset( p_u, 0x80, i_pitch * p_spu->p_region->fmt.i_height );
326 memset( p_v, 0x80, i_pitch * p_spu->p_region->fmt.i_height );
327 memset( p_a, 0x00, i_pitch * p_spu->p_region->fmt.i_height );
329 #define pi_gamma p_sys->pi_gamma
331 for( p_line = p_string->p_lines; p_line != NULL; p_line = p_line->p_next )
333 int i_glyph_tmax = 0;
334 int i_bitmap_offset, i_offset;
335 for( i = 0; p_line->pp_glyphs[i] != NULL; i++ )
337 FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ];
338 i_glyph_tmax = __MAX( i_glyph_tmax, p_glyph->top );
341 for( i = 0; p_line->pp_glyphs[i] != NULL; i++ )
343 FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ];
345 i_offset = ( p_line->p_glyph_pos[ i ].y +
346 i_glyph_tmax - p_glyph->top + 1 ) *
347 i_pitch + p_line->p_glyph_pos[ i ].x + p_glyph->left + 1;
349 for( y = 0, i_bitmap_offset = 0; y < p_glyph->bitmap.rows; y++ )
351 for( x = 0; x < p_glyph->bitmap.width; x++, i_bitmap_offset++ )
353 if( !pi_gamma[p_glyph->bitmap.buffer[i_bitmap_offset]] )
357 p_a[i_offset + x] = ((uint16_t)p_a[i_offset + x] +
358 pi_gamma[p_glyph->bitmap.buffer[i_bitmap_offset]])/2;
359 i_offset += i_pitch; x--;
360 p_a[i_offset + x] = ((uint16_t)p_a[i_offset + x] +
361 pi_gamma[p_glyph->bitmap.buffer[i_bitmap_offset]])/2;
363 p_a[i_offset + x] = ((uint16_t)p_a[i_offset + x] +
364 pi_gamma[p_glyph->bitmap.buffer[i_bitmap_offset]])/2;
365 i_offset += i_pitch; x--;
366 p_a[i_offset + x] = ((uint16_t)p_a[i_offset + x] +
367 pi_gamma[p_glyph->bitmap.buffer[i_bitmap_offset]])/2;
373 i_offset = ( p_line->p_glyph_pos[ i ].y +
374 i_glyph_tmax - p_glyph->top ) *
375 i_pitch + p_line->p_glyph_pos[ i ].x + p_glyph->left + 1;
377 for( y = 0, i_bitmap_offset = 0; y < p_glyph->bitmap.rows; y++ )
379 for( x = 0; x < p_glyph->bitmap.width; x++, i_bitmap_offset++ )
382 pi_gamma[p_glyph->bitmap.buffer[i_bitmap_offset]];
393 * This function receives a string and creates a subpicture for it. It
394 * also calculates the size needed for this string, and renders the
395 * needed glyphs into memory. It is used as pf_add_string callback in
396 * the vout method by this module
398 static subpicture_t *RenderText( filter_t *p_filter, block_t *p_block )
400 filter_sys_t *p_sys = p_filter->p_sys;
401 subpicture_t *p_subpic = 0;
402 subpicture_data_t *p_string = 0;
403 line_desc_t *p_line = 0, *p_next = 0;
404 int i, i_pen_y, i_pen_x, i_error, i_glyph_index, i_previous;
405 uint32_t *p_unicode_string, i_char;
409 #if defined(HAVE_ICONV)
410 iconv_t iconv_handle;
419 if( !p_block ) return NULL;
420 psz_string = p_block->p_buffer;
421 if( !psz_string || !*psz_string ) goto error;
430 /* Create and initialize a subpicture */
431 p_subpic = p_filter->pf_spu_buffer_new( p_filter );
432 if( !p_subpic ) goto error;
434 p_subpic->i_start = p_block->i_pts;
435 p_subpic->i_stop = p_block->i_pts + p_block->i_length;
436 p_subpic->b_ephemer = (p_block->i_length == 0);
437 p_subpic->b_absolute = VLC_FALSE;
439 /* Create and initialize private data for the subpicture */
440 p_string = malloc( sizeof(subpicture_data_t) );
443 msg_Err( p_filter, "Out of memory" );
446 p_string->p_lines = 0;
447 p_string->psz_text = strdup( psz_string );
449 #if defined(HAVE_ICONV)
450 p_unicode_string = malloc( ( strlen(psz_string) + 1 ) * sizeof(uint32_t) );
451 if( p_unicode_string == NULL )
453 msg_Err( p_filter, "Out of memory" );
456 #if defined(WORDS_BIGENDIAN)
457 iconv_handle = iconv_open( "UCS-4BE", "UTF-8" );
459 iconv_handle = iconv_open( "UCS-4LE", "UTF-8" );
461 if( iconv_handle == (iconv_t)-1 )
463 msg_Warn( p_filter, "Unable to do convertion" );
468 char *p_in_buffer, *p_out_buffer;
469 size_t i_in_bytes, i_out_bytes, i_out_bytes_left, i_ret;
470 i_in_bytes = strlen( psz_string );
471 i_out_bytes = i_in_bytes * sizeof( uint32_t );
472 i_out_bytes_left = i_out_bytes;
473 p_in_buffer = psz_string;
474 p_out_buffer = (char *)p_unicode_string;
475 i_ret = iconv( iconv_handle, &p_in_buffer, &i_in_bytes,
476 &p_out_buffer, &i_out_bytes_left );
479 msg_Warn( p_filter, "Failed to convert string to unicode (%s), "
480 "bytes left %d", strerror(errno), i_in_bytes );
483 *(uint32_t*)p_out_buffer = 0;
484 i_string_length = (i_out_bytes - i_out_bytes_left) / sizeof(uint32_t);
487 #if defined(HAVE_FRIBIDI)
489 uint32_t *p_fribidi_string;
490 FriBidiCharType base_dir = FRIBIDI_TYPE_ON;
491 p_fribidi_string = malloc( (i_string_length + 1) * sizeof(uint32_t) );
492 fribidi_log2vis( (FriBidiChar*)p_unicode_string, i_string_length,
493 &base_dir, (FriBidiChar*)p_fribidi_string, 0, 0, 0 );
494 free( p_unicode_string );
495 p_unicode_string = p_fribidi_string;
496 p_fribidi_string[ i_string_length ] = 0;
501 /* Calculate relative glyph positions and a bounding box for the
503 p_line = NewLine( psz_string );
506 msg_Err( p_filter, "Out of memory" );
509 p_string->p_lines = p_line;
515 #define face p_sys->p_face
516 #define glyph face->glyph
518 while( *p_unicode_string )
520 i_char = *p_unicode_string++;
521 if( i_char == '\r' ) /* ignore CR chars wherever they may be */
528 p_next = NewLine( psz_string );
531 msg_Err( p_filter, "Out of memory" );
534 p_line->p_next = p_next;
535 p_line->i_width = line.xMax;
536 p_line->i_height = face->size->metrics.height >> 6;
537 p_line->pp_glyphs[ i ] = NULL;
539 result.x = __MAX( result.x, line.xMax );
540 result.y += face->size->metrics.height >> 6;
546 i_pen_y += face->size->metrics.height >> 6;
548 msg_Dbg( p_filter, "Creating new line, i is %d", i );
554 i_glyph_index = FT_Get_Char_Index( face, i_char );
555 if( p_sys->i_use_kerning && i_glyph_index
559 FT_Get_Kerning( face, i_previous, i_glyph_index,
560 ft_kerning_default, &delta );
561 i_pen_x += delta.x >> 6;
564 p_line->p_glyph_pos[ i ].x = i_pen_x;
565 p_line->p_glyph_pos[ i ].y = i_pen_y;
566 i_error = FT_Load_Glyph( face, i_glyph_index, FT_LOAD_DEFAULT );
569 msg_Err( p_filter, "FT_Load_Glyph returned %d", i_error );
572 i_error = FT_Get_Glyph( glyph, &tmp_glyph );
575 msg_Err( p_filter, "FT_Get_Glyph returned %d", i_error );
578 FT_Glyph_Get_CBox( tmp_glyph, ft_glyph_bbox_pixels, &glyph_size );
579 i_error = FT_Glyph_To_Bitmap( &tmp_glyph, ft_render_mode_normal,
581 if( i_error ) continue;
582 p_line->pp_glyphs[ i ] = (FT_BitmapGlyph)tmp_glyph;
585 line.xMax = p_line->p_glyph_pos[i].x + glyph_size.xMax - glyph_size.xMin;
586 line.yMax = __MAX( line.yMax, glyph_size.yMax );
587 line.yMin = __MIN( line.yMin, glyph_size.yMin );
589 i_previous = i_glyph_index;
590 i_pen_x += glyph->advance.x >> 6;
594 p_line->i_width = line.xMax;
595 p_line->i_height = face->size->metrics.height >> 6;
596 p_line->pp_glyphs[ i ] = NULL;
597 result.x = __MAX( result.x, line.xMax );
598 result.y += line.yMax - line.yMin;
599 p_string->i_height = result.y;
600 p_string->i_width = result.x;
605 Render( p_filter, p_subpic, p_string );
606 FreeString( p_string );
607 block_Release( p_block );
611 FreeString( p_string );
612 if( p_subpic ) p_filter->pf_spu_buffer_del( p_filter, p_subpic );
613 block_Release( p_block );
617 static void FreeString( subpicture_data_t *p_string )
620 line_desc_t *p_line, *p_next;
622 if( !p_string ) return;
624 for( p_line = p_string->p_lines; p_line != NULL; p_line = p_next )
626 p_next = p_line->p_next;
627 for( i = 0; p_line->pp_glyphs[ i ] != NULL; i++ )
629 FT_Done_Glyph( (FT_Glyph)p_line->pp_glyphs[ i ] );
631 free( p_line->pp_glyphs );
632 free( p_line->p_glyph_pos );
636 free( p_string->psz_text );
640 #if !defined( HAVE_ICONV )
641 /* convert one or more utf8 bytes into a unicode character */
642 static int GetUnicodeCharFromUTF8( byte_t **ppsz_utf8_string )
644 int i_remaining_bytes, i_char = 0;
645 if( ( **ppsz_utf8_string & 0xFC ) == 0xFC )
647 i_char = **ppsz_utf8_string & 1;
648 i_remaining_bytes = 5;
650 else if( ( **ppsz_utf8_string & 0xF8 ) == 0xF8 )
652 i_char = **ppsz_utf8_string & 3;
653 i_remaining_bytes = 4;
655 else if( ( **ppsz_utf8_string & 0xF0 ) == 0xF0 )
657 i_char = **ppsz_utf8_string & 7;
658 i_remaining_bytes = 3;
660 else if( ( **ppsz_utf8_string & 0xE0 ) == 0xE0 )
662 i_char = **ppsz_utf8_string & 15;
663 i_remaining_bytes = 2;
665 else if( ( **ppsz_utf8_string & 0xC0 ) == 0xC0 )
667 i_char = **ppsz_utf8_string & 31;
668 i_remaining_bytes = 1;
672 i_char = **ppsz_utf8_string;
673 i_remaining_bytes = 0;
675 while( i_remaining_bytes )
677 (*ppsz_utf8_string)++;
679 i_char = ( i_char << 6 ) + ( **ppsz_utf8_string & 0x3F );
681 (*ppsz_utf8_string)++;
686 static line_desc_t *NewLine( byte_t *psz_string )
689 line_desc_t *p_line = malloc( sizeof(line_desc_t) );
694 p_line->i_height = 0;
696 p_line->p_next = NULL;
698 /* We don't use CountUtf8Characters() here because we are not acutally
699 * sure the string is utf8. Better be safe than sorry. */
700 i_count = strlen( psz_string );
702 p_line->pp_glyphs = malloc( sizeof(FT_BitmapGlyph)
704 if( p_line->pp_glyphs == NULL )
709 p_line->p_glyph_pos = malloc( sizeof( FT_Vector )
711 if( p_line->p_glyph_pos == NULL )
713 free( p_line->pp_glyphs );