1 /*****************************************************************************
2 * freetype.c : Put text on the video, using freetype2
3 *****************************************************************************
4 * Copyright (C) 2002 - 2005 the VideoLAN team
7 * Authors: Sigmund Augdal Helberg <dnumgis@videolan.org>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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_FRIBIDI)
62 #include <fribidi/fribidi.h>
65 typedef struct line_desc_t line_desc_t;
67 /*****************************************************************************
69 *****************************************************************************/
70 static int Create ( vlc_object_t * );
71 static void Destroy( vlc_object_t * );
73 /* The RenderText call maps to pf_render_string, defined in vlc_filter.h */
74 static int RenderText( filter_t *, subpicture_region_t *,
75 subpicture_region_t * );
76 static line_desc_t *NewLine( byte_t * );
78 static int SetFontSize( filter_t *, int );
80 /*****************************************************************************
82 *****************************************************************************/
83 #define FONT_TEXT N_("Font")
84 #define FONT_LONGTEXT N_("Font filename")
85 #define FONTSIZE_TEXT N_("Font size in pixels")
86 #define FONTSIZE_LONGTEXT N_("The size of the fonts used by the osd module. " \
87 "If set to something different than 0 this option will override the " \
88 "relative font size " )
89 #define OPACITY_TEXT N_("Opacity, 0..255")
90 #define OPACITY_LONGTEXT N_("The opacity (inverse of transparency) of " \
91 "overlay text. 0 = transparent, 255 = totally opaque. " )
92 #define COLOR_TEXT N_("Text Default Color")
93 #define COLOR_LONGTEXT N_("The color of overlay text. 1 byte for each color, "\
94 "hexadecimal. #000000 = all colors off, 0xFF0000 = just Red, " \
95 "0xFFFFFF = all color on [White]" )
96 #define FONTSIZER_TEXT N_("Font size")
97 #define FONTSIZER_LONGTEXT N_("The size of the fonts used by the osd module" )
99 static int pi_sizes[] = { 20, 18, 16, 12, 6 };
100 static char *ppsz_sizes_text[] = { N_("Smaller"), N_("Small"), N_("Normal"),
101 N_("Large"), N_("Larger") };
102 #define YUVP_TEXT N_("Use yuvp renderer")
103 #define YUVP_LONGTEXT N_("Render into paletized YUV. Needed to encode into dvbsubs")
104 #define EFFECT_TEXT N_("Font Effect")
105 #define EFFECT_LONGTEXT N_("Select effects to apply to rendered text")
107 #define EFFECT_BACKGROUND 1
108 #define EFFECT_OUTLINE 2
109 #define EFFECT_OUTLINE_FAT 3
111 static int pi_effects[] = { 1, 2, 3 };
112 static char *ppsz_effects_text[] = { N_("Background"),N_("Outline"),
114 static int pi_color_values[] = {
115 0x00000000, 0x00808080, 0x00C0C0C0, 0x00FFFFFF, 0x00800000,
116 0x00FF0000, 0x00FF00FF, 0x00FFFF00, 0x00808000, 0x00008000, 0x00008080,
117 0x0000FF00, 0x00800080, 0x00000080, 0x000000FF, 0x0000FFFF };
119 static char *ppsz_color_descriptions[] = {
120 N_("Black"), N_("Gray"), N_("Silver"), N_("White"), N_("Maroon"),
121 N_("Red"), N_("Fuchsia"), N_("Yellow"), N_("Olive"), N_("Green"), N_("Teal"),
122 N_("Lime"), N_("Purple"), N_("Navy"), N_("Blue"), N_("Aqua") };
125 set_shortname( _("Text renderer"));
126 set_description( _("Freetype2 font renderer") );
127 set_category( CAT_VIDEO );
128 set_subcategory( SUBCAT_VIDEO_SUBPIC );
130 add_file( "freetype-font", DEFAULT_FONT, NULL, FONT_TEXT, FONT_LONGTEXT,
133 add_integer( "freetype-fontsize", 0, NULL, FONTSIZE_TEXT,
134 FONTSIZE_LONGTEXT, VLC_TRUE );
136 /* opacity valid on 0..255, with default 255 = fully opaque */
137 add_integer_with_range( "freetype-opacity", 255, 0, 255, NULL,
138 OPACITY_TEXT, OPACITY_LONGTEXT, VLC_TRUE );
140 /* hook to the color values list, with default 0x00ffffff = white */
141 add_integer( "freetype-color", 0x00FFFFFF, NULL, COLOR_TEXT,
142 COLOR_LONGTEXT, VLC_FALSE );
143 change_integer_list( pi_color_values, ppsz_color_descriptions, 0 );
145 add_integer( "freetype-rel-fontsize", 16, NULL, FONTSIZER_TEXT,
146 FONTSIZER_LONGTEXT, VLC_FALSE );
147 change_integer_list( pi_sizes, ppsz_sizes_text, 0 );
148 add_integer( "freetype-effect", 2, NULL, EFFECT_TEXT,
149 EFFECT_LONGTEXT, VLC_FALSE );
150 change_integer_list( pi_effects, ppsz_effects_text, 0 );
152 add_bool( "freetype-yuvp", 0, NULL, YUVP_TEXT,
153 YUVP_LONGTEXT, VLC_TRUE );
154 set_capability( "text renderer", 100 );
155 add_shortcut( "text" );
156 set_callbacks( Create, Destroy );
161 /** NULL-terminated list of glyphs making the string */
162 FT_BitmapGlyph *pp_glyphs;
163 /** list of relative positions for the glyphs */
164 FT_Vector *p_glyph_pos;
168 int i_red, i_green, i_blue;
174 static int Render( filter_t *, subpicture_region_t *, line_desc_t *, int, int);
175 static void FreeLines( line_desc_t * );
176 static void FreeLine( line_desc_t * );
178 /*****************************************************************************
179 * filter_sys_t: freetype local data
180 *****************************************************************************
181 * This structure is part of the video output thread descriptor.
182 * It describes the freetype specific properties of an output thread.
183 *****************************************************************************/
186 FT_Library p_library; /* handle to library */
187 FT_Face p_face; /* handle to face object */
188 vlc_bool_t i_use_kerning;
189 uint8_t i_font_opacity;
194 int i_default_font_size;
195 int i_display_height;
198 /*****************************************************************************
199 * Create: allocates osd-text video thread output method
200 *****************************************************************************
201 * This function allocates and initializes a Clone vout method.
202 *****************************************************************************/
203 static int Create( vlc_object_t *p_this )
205 filter_t *p_filter = (filter_t *)p_this;
207 char *psz_fontfile = NULL;
211 /* Allocate structure */
212 p_filter->p_sys = p_sys = malloc( sizeof( filter_sys_t ) );
215 msg_Err( p_filter, "out of memory" );
219 p_sys->p_library = 0;
220 p_sys->i_font_size = 0;
221 p_sys->i_display_height = 0;
223 var_Create( p_filter, "freetype-font",
224 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
225 var_Create( p_filter, "freetype-fontsize",
226 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
227 var_Create( p_filter, "freetype-rel-fontsize",
228 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
229 var_Create( p_filter, "freetype-opacity",
230 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
231 var_Create( p_filter, "freetype-effect",
232 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
233 var_Get( p_filter, "freetype-opacity", &val );
234 p_sys->i_font_opacity = __MAX( __MIN( val.i_int, 255 ), 0 );
235 var_Create( p_filter, "freetype-color",
236 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
237 var_Get( p_filter, "freetype-color", &val );
238 p_sys->i_font_color = __MAX( __MIN( val.i_int, 0xFFFFFF ), 0 );
239 p_sys->i_effect = var_GetInteger( p_filter, "freetype-effect" );
241 /* Look what method was requested */
242 var_Get( p_filter, "freetype-font", &val );
243 psz_fontfile = val.psz_string;
244 if( !psz_fontfile || !*psz_fontfile )
246 if( psz_fontfile ) free( psz_fontfile );
247 psz_fontfile = (char *)malloc( PATH_MAX + 1 );
249 GetWindowsDirectory( psz_fontfile, PATH_MAX + 1 );
250 strcat( psz_fontfile, "\\fonts\\arial.ttf" );
252 strcpy( psz_fontfile, DEFAULT_FONT );
254 msg_Err( p_filter, "user didn't specify a font" );
259 i_error = FT_Init_FreeType( &p_sys->p_library );
262 msg_Err( p_filter, "couldn't initialize freetype" );
266 i_error = FT_New_Face( p_sys->p_library, psz_fontfile ? psz_fontfile : "",
268 if( i_error == FT_Err_Unknown_File_Format )
270 msg_Err( p_filter, "file %s have unknown format", psz_fontfile );
275 msg_Err( p_filter, "failed to load font file %s", psz_fontfile );
279 i_error = FT_Select_Charmap( p_sys->p_face, ft_encoding_unicode );
282 msg_Err( p_filter, "Font has no unicode translation table" );
286 p_sys->i_use_kerning = FT_HAS_KERNING( p_sys->p_face );
288 var_Get( p_filter, "freetype-fontsize", &val );
289 p_sys->i_default_font_size = val.i_int;
290 if( SetFontSize( p_filter, 0 ) != VLC_SUCCESS ) goto error;
292 if( psz_fontfile ) free( psz_fontfile );
293 p_filter->pf_render_text = RenderText;
297 if( p_sys->p_face ) FT_Done_Face( p_sys->p_face );
298 if( p_sys->p_library ) FT_Done_FreeType( p_sys->p_library );
299 if( psz_fontfile ) free( psz_fontfile );
304 /*****************************************************************************
305 * Destroy: destroy Clone video thread output method
306 *****************************************************************************
307 * Clean up all data and library connections
308 *****************************************************************************/
309 static void Destroy( vlc_object_t *p_this )
311 filter_t *p_filter = (filter_t *)p_this;
312 filter_sys_t *p_sys = p_filter->p_sys;
314 FT_Done_Face( p_sys->p_face );
315 FT_Done_FreeType( p_sys->p_library );
319 /*****************************************************************************
320 * Render: place string in picture
321 *****************************************************************************
322 * This function merges the previously rendered freetype glyphs into a picture
323 *****************************************************************************/
324 static int Render( filter_t *p_filter, subpicture_region_t *p_region,
325 line_desc_t *p_line, int i_width, int i_height )
327 static uint8_t pi_gamma[16] =
328 {0x00, 0x52, 0x84, 0x96, 0xb8, 0xca, 0xdc, 0xee, 0xff,
329 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
333 int i, x, y, i_pitch;
334 uint8_t i_y; /* YUV values, derived from incoming RGB */
336 subpicture_region_t *p_region_tmp;
338 /* Create a new subpicture region */
339 memset( &fmt, 0, sizeof(video_format_t) );
340 fmt.i_chroma = VLC_FOURCC('Y','U','V','P');
342 fmt.i_width = fmt.i_visible_width = i_width + 4;
343 fmt.i_height = fmt.i_visible_height = i_height + 4;
344 fmt.i_x_offset = fmt.i_y_offset = 0;
345 p_region_tmp = spu_CreateRegion( p_filter, &fmt );
348 msg_Err( p_filter, "cannot allocate SPU region" );
352 p_region->fmt = p_region_tmp->fmt;
353 p_region->picture = p_region_tmp->picture;
354 free( p_region_tmp );
356 /* Calculate text color components */
357 i_y = (uint8_t)(( 66 * p_line->i_red + 129 * p_line->i_green +
358 25 * p_line->i_blue + 128) >> 8) + 16;
359 i_u = (int8_t)(( -38 * p_line->i_red - 74 * p_line->i_green +
360 112 * p_line->i_blue + 128) >> 8) + 128;
361 i_v = (int8_t)(( 112 * p_line->i_red - 94 * p_line->i_green -
362 18 * p_line->i_blue + 128) >> 8) + 128;
365 fmt.p_palette->i_entries = 16;
366 for( i = 0; i < 8; i++ )
368 fmt.p_palette->palette[i][0] = 0;
369 fmt.p_palette->palette[i][1] = 0x80;
370 fmt.p_palette->palette[i][2] = 0x80;
371 fmt.p_palette->palette[i][3] = pi_gamma[i];
372 fmt.p_palette->palette[i][3] =
373 (int)fmt.p_palette->palette[i][3] * (255 - p_line->i_alpha) / 255;
375 for( i = 8; i < fmt.p_palette->i_entries; i++ )
377 fmt.p_palette->palette[i][0] = i * 16 * i_y / 256;
378 fmt.p_palette->palette[i][1] = i_u;
379 fmt.p_palette->palette[i][2] = i_v;
380 fmt.p_palette->palette[i][3] = pi_gamma[i];
381 fmt.p_palette->palette[i][3] =
382 (int)fmt.p_palette->palette[i][3] * (255 - p_line->i_alpha) / 255;
385 p_dst = p_region->picture.Y_PIXELS;
386 i_pitch = p_region->picture.Y_PITCH;
388 /* Initialize the region pixels */
389 memset( p_dst, 0, i_pitch * p_region->fmt.i_height );
391 for( ; p_line != NULL; p_line = p_line->p_next )
393 int i_glyph_tmax = 0;
394 int i_bitmap_offset, i_offset, i_align_offset = 0;
395 for( i = 0; p_line->pp_glyphs[i] != NULL; i++ )
397 FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ];
398 i_glyph_tmax = __MAX( i_glyph_tmax, p_glyph->top );
401 if( p_line->i_width < i_width )
403 if( p_region->i_text_align == SUBPICTURE_ALIGN_RIGHT )
405 i_align_offset = i_width - p_line->i_width;
407 else if( p_region->i_text_align != SUBPICTURE_ALIGN_LEFT )
409 i_align_offset = ( i_width - p_line->i_width ) / 2;
413 for( i = 0; p_line->pp_glyphs[i] != NULL; i++ )
415 FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ];
417 i_offset = ( p_line->p_glyph_pos[ i ].y +
418 i_glyph_tmax - p_glyph->top + 2 ) *
419 i_pitch + p_line->p_glyph_pos[ i ].x + p_glyph->left + 2 +
422 for( y = 0, i_bitmap_offset = 0; y < p_glyph->bitmap.rows; y++ )
424 for( x = 0; x < p_glyph->bitmap.width; x++, i_bitmap_offset++ )
426 if( p_glyph->bitmap.buffer[i_bitmap_offset] )
428 ((int)p_glyph->bitmap.buffer[i_bitmap_offset] + 8)/16;
435 /* Outlining (find something better than nearest neighbour filtering ?) */
438 uint8_t *p_dst = p_region->picture.Y_PIXELS;
439 uint8_t *p_top = p_dst; /* Use 1st line as a cache */
440 uint8_t left, current;
442 for( y = 1; y < (int)fmt.i_height - 1; y++ )
444 if( y > 1 ) memcpy( p_top, p_dst, fmt.i_width );
445 p_dst += p_region->picture.Y_PITCH;
448 for( x = 1; x < (int)fmt.i_width - 1; x++ )
451 p_dst[x] = ( 8 * (int)p_dst[x] + left + p_dst[x+1] + p_top[x -1]+ p_top[x] + p_top[x+1] +
452 p_dst[x -1 + p_region->picture.Y_PITCH ] + p_dst[x + p_region->picture.Y_PITCH] + p_dst[x + 1 + p_region->picture.Y_PITCH]) / 16;
456 memset( p_top, 0, fmt.i_width );
462 static void DrawBlack( line_desc_t *p_line, int i_width, subpicture_region_t *p_region, int xoffset, int yoffset )
464 uint8_t *p_dst = p_region->picture.A_PIXELS;
465 int i_pitch = p_region->picture.A_PITCH;
468 for( ; p_line != NULL; p_line = p_line->p_next )
470 int i_glyph_tmax=0, i = 0;
471 int i_bitmap_offset, i_offset, i_align_offset = 0;
472 for( i = 0; p_line->pp_glyphs[i] != NULL; i++ )
474 FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ];
475 i_glyph_tmax = __MAX( i_glyph_tmax, p_glyph->top );
478 if( p_line->i_width < i_width )
480 if( p_region->i_text_align == SUBPICTURE_ALIGN_RIGHT )
482 i_align_offset = i_width - p_line->i_width;
484 else if( p_region->i_text_align != SUBPICTURE_ALIGN_LEFT )
486 i_align_offset = ( i_width - p_line->i_width ) / 2;
490 for( i = 0; p_line->pp_glyphs[i] != NULL; i++ )
492 FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ];
494 i_offset = ( p_line->p_glyph_pos[ i ].y +
495 i_glyph_tmax - p_glyph->top + 3 + yoffset ) *
496 i_pitch + p_line->p_glyph_pos[ i ].x + p_glyph->left + 3 +
497 i_align_offset +xoffset;
499 for( y = 0, i_bitmap_offset = 0; y < p_glyph->bitmap.rows; y++ )
501 for( x = 0; x < p_glyph->bitmap.width; x++, i_bitmap_offset++ )
503 if( p_glyph->bitmap.buffer[i_bitmap_offset] )
504 if( p_dst[i_offset+x] <
505 ((int)p_glyph->bitmap.buffer[i_bitmap_offset]) )
507 ((int)p_glyph->bitmap.buffer[i_bitmap_offset]);
516 /*****************************************************************************
517 * Render: place string in picture
518 *****************************************************************************
519 * This function merges the previously rendered freetype glyphs into a picture
520 *****************************************************************************/
521 static int RenderYUVA( filter_t *p_filter, subpicture_region_t *p_region,
522 line_desc_t *p_line, int i_width, int i_height )
524 static uint8_t pi_gamma[16] =
525 {0x00, 0x52, 0x84, 0x96, 0xb8, 0xca, 0xdc, 0xee, 0xff,
526 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
528 uint8_t *p_dst_y,*p_dst_u,*p_dst_v,*p_dst_a;
530 int i, x, y, i_pitch, i_alpha;
531 uint8_t i_y, i_u, i_v; /* YUV values, derived from incoming RGB */
532 subpicture_region_t *p_region_tmp;
534 if( i_width == 0 || i_height == 0 )
537 /* Create a new subpicture region */
538 memset( &fmt, 0, sizeof(video_format_t) );
539 fmt.i_chroma = VLC_FOURCC('Y','U','V','A');
541 fmt.i_width = fmt.i_visible_width = i_width + 6;
542 fmt.i_height = fmt.i_visible_height = i_height + 6;
543 fmt.i_x_offset = fmt.i_y_offset = 0;
544 p_region_tmp = spu_CreateRegion( p_filter, &fmt );
547 msg_Err( p_filter, "cannot allocate SPU region" );
551 p_region->fmt = p_region_tmp->fmt;
552 p_region->picture = p_region_tmp->picture;
553 free( p_region_tmp );
555 /* Calculate text color components */
556 i_y = (uint8_t)__MIN(abs( 2104 * p_line->i_red + 4130 * p_line->i_green +
557 802 * p_line->i_blue + 4096 + 131072 ) >> 13, 235);
558 i_u = (uint8_t)__MIN(abs( -1214 * p_line->i_red + -2384 * p_line->i_green +
559 3598 * p_line->i_blue + 4096 + 1048576) >> 13, 240);
560 i_v = (uint8_t)__MIN(abs( 3598 * p_line->i_red + -3013 * p_line->i_green +
561 -585 * p_line->i_blue + 4096 + 1048576) >> 13, 240);
562 i_alpha = p_line->i_alpha;
564 p_dst_y = p_region->picture.Y_PIXELS;
565 p_dst_u = p_region->picture.U_PIXELS;
566 p_dst_v = p_region->picture.V_PIXELS;
567 p_dst_a = p_region->picture.A_PIXELS;
568 i_pitch = p_region->picture.A_PITCH;
570 /* Initialize the region pixels */
571 if( p_filter->p_sys->i_effect != EFFECT_BACKGROUND )
573 memset( p_dst_y, 0x00, i_pitch * p_region->fmt.i_height );
574 memset( p_dst_u, 0x80, i_pitch * p_region->fmt.i_height );
575 memset( p_dst_v, 0x80, i_pitch * p_region->fmt.i_height );
576 memset( p_dst_a, 0, i_pitch * p_region->fmt.i_height );
580 memset( p_dst_y, 0x0, i_pitch * p_region->fmt.i_height );
581 memset( p_dst_u, 0x80, i_pitch * p_region->fmt.i_height );
582 memset( p_dst_v, 0x80, i_pitch * p_region->fmt.i_height );
583 memset( p_dst_a, 0x80, i_pitch * p_region->fmt.i_height );
585 if( p_filter->p_sys->i_effect == EFFECT_OUTLINE ||
586 p_filter->p_sys->i_effect == EFFECT_OUTLINE_FAT )
588 DrawBlack( p_line, i_width, p_region, 0, 0);
589 DrawBlack( p_line, i_width, p_region, -1, 0);
590 DrawBlack( p_line, i_width, p_region, 0, -1);
591 DrawBlack( p_line, i_width, p_region, 1, 0);
592 DrawBlack( p_line, i_width, p_region, 0, 1);
595 if( p_filter->p_sys->i_effect == EFFECT_OUTLINE_FAT )
597 DrawBlack( p_line, i_width, p_region, -1, -1);
598 DrawBlack( p_line, i_width, p_region, -1, 1);
599 DrawBlack( p_line, i_width, p_region, 1, -1);
600 DrawBlack( p_line, i_width, p_region, 1, 1);
602 DrawBlack( p_line, i_width, p_region, -2, 0);
603 DrawBlack( p_line, i_width, p_region, 0, -2);
604 DrawBlack( p_line, i_width, p_region, 2, 0);
605 DrawBlack( p_line, i_width, p_region, 0, 2);
607 DrawBlack( p_line, i_width, p_region, -2, -2);
608 DrawBlack( p_line, i_width, p_region, -2, 2);
609 DrawBlack( p_line, i_width, p_region, 2, -2);
610 DrawBlack( p_line, i_width, p_region, 2, 2);
612 DrawBlack( p_line, i_width, p_region, -3, 0);
613 DrawBlack( p_line, i_width, p_region, 0, -3);
614 DrawBlack( p_line, i_width, p_region, 3, 0);
615 DrawBlack( p_line, i_width, p_region, 0, 3);
618 for( ; p_line != NULL; p_line = p_line->p_next )
620 int i_glyph_tmax = 0;
621 int i_bitmap_offset, i_offset, i_align_offset = 0;
622 for( i = 0; p_line->pp_glyphs[i] != NULL; i++ )
624 FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ];
625 i_glyph_tmax = __MAX( i_glyph_tmax, p_glyph->top );
628 if( p_line->i_width < i_width )
630 if( p_region->i_text_align == SUBPICTURE_ALIGN_RIGHT )
632 i_align_offset = i_width - p_line->i_width;
634 else if( p_region->i_text_align != SUBPICTURE_ALIGN_LEFT )
636 i_align_offset = ( i_width - p_line->i_width ) / 2;
640 for( i = 0; p_line->pp_glyphs[i] != NULL; i++ )
642 FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ];
644 i_offset = ( p_line->p_glyph_pos[ i ].y +
645 i_glyph_tmax - p_glyph->top + 3 ) *
646 i_pitch + p_line->p_glyph_pos[ i ].x + p_glyph->left + 3 +
649 for( y = 0, i_bitmap_offset = 0; y < p_glyph->bitmap.rows; y++ )
651 for( x = 0; x < p_glyph->bitmap.width; x++, i_bitmap_offset++ )
653 if( p_glyph->bitmap.buffer[i_bitmap_offset] )
655 p_dst_y[i_offset+x] = ((p_dst_y[i_offset+x] *(255-(int)p_glyph->bitmap.buffer[i_bitmap_offset])) +
656 i_y * ((int)p_glyph->bitmap.buffer[i_bitmap_offset])) >> 8;
658 p_dst_u[i_offset+x] = i_u;
659 p_dst_v[i_offset+x] = i_v;
661 if( p_filter->p_sys->i_effect == EFFECT_BACKGROUND )
662 p_dst_a[i_offset+x] = 0xff;
670 /* Apply the alpha setting */
671 for( i = 0; i < fmt.i_height * i_pitch; i++ )
672 p_dst_a[i] = p_dst_a[i] * (255 - i_alpha) / 255;
678 * This function renders a text subpicture region into another one.
679 * It also calculates the size needed for this string, and renders the
680 * needed glyphs into memory. It is used as pf_add_string callback in
681 * the vout method by this module
683 static int RenderText( filter_t *p_filter, subpicture_region_t *p_region_out,
684 subpicture_region_t *p_region_in )
686 filter_sys_t *p_sys = p_filter->p_sys;
687 line_desc_t *p_lines = 0, *p_line = 0, *p_next = 0, *p_prev = 0;
688 int i, i_pen_y, i_pen_x, i_error, i_glyph_index, i_previous;
689 uint32_t *psz_unicode, *psz_unicode_orig = 0, i_char, *psz_line_start;
692 vlc_iconv_t iconv_handle = (vlc_iconv_t)(-1);
693 int i_font_color, i_font_alpha, i_font_size, i_red, i_green, i_blue;
701 if( !p_region_in || !p_region_out ) return VLC_EGENERIC;
702 psz_string = p_region_in->psz_text;
703 if( !psz_string || !*psz_string ) return VLC_EGENERIC;
705 i_font_color = __MAX( __MIN( p_region_in->i_text_color, 0xFFFFFF ), 0 );
706 if( i_font_color == 0xFFFFFF ) i_font_color = p_sys->i_font_color;
708 i_font_alpha = __MAX( __MIN( p_region_in->i_text_alpha, 255 ), 0 );
709 if( !i_font_alpha ) i_font_alpha = 255 - p_sys->i_font_opacity;
711 i_font_size = __MAX( __MIN( p_region_in->i_text_size, 255 ), 0 );
712 SetFontSize( p_filter, i_font_size );
714 i_red = ( i_font_color & 0x00FF0000 ) >> 16;
715 i_green = ( i_font_color & 0x0000FF00 ) >> 8;
716 i_blue = i_font_color & 0x000000FF;
718 result.x = result.y = 0;
719 line.xMin = line.xMax = line.yMin = line.yMax = 0;
721 psz_unicode = psz_unicode_orig =
722 malloc( ( strlen(psz_string) + 1 ) * sizeof(uint32_t) );
723 if( psz_unicode == NULL )
725 msg_Err( p_filter, "out of memory" );
728 #if defined(WORDS_BIGENDIAN)
729 iconv_handle = vlc_iconv_open( "UCS-4BE", "UTF-8" );
731 iconv_handle = vlc_iconv_open( "UCS-4LE", "UTF-8" );
733 if( iconv_handle == (vlc_iconv_t)-1 )
735 msg_Warn( p_filter, "unable to do conversion" );
740 char *p_in_buffer, *p_out_buffer;
741 size_t i_in_bytes, i_out_bytes, i_out_bytes_left, i_ret;
742 i_in_bytes = strlen( psz_string );
743 i_out_bytes = i_in_bytes * sizeof( uint32_t );
744 i_out_bytes_left = i_out_bytes;
745 p_in_buffer = psz_string;
746 p_out_buffer = (char *)psz_unicode;
747 i_ret = vlc_iconv( iconv_handle, &p_in_buffer, &i_in_bytes,
748 &p_out_buffer, &i_out_bytes_left );
750 vlc_iconv_close( iconv_handle );
754 msg_Warn( p_filter, "failed to convert string to unicode (%s), "
755 "bytes left %d", strerror(errno), i_in_bytes );
758 *(uint32_t*)p_out_buffer = 0;
759 i_string_length = (i_out_bytes - i_out_bytes_left) / sizeof(uint32_t);
762 #if defined(HAVE_FRIBIDI)
764 uint32_t *p_fribidi_string;
765 int start_pos, pos = 0;
767 p_fribidi_string = malloc( (i_string_length + 1) * sizeof(uint32_t) );
769 /* Do bidi conversion line-by-line */
770 while(pos < i_string_length)
772 while(pos < i_string_length) {
773 i_char = psz_unicode[pos];
774 if (i_char != '\r' && i_char != '\n')
776 p_fribidi_string[pos] = i_char;
780 while(pos < i_string_length) {
781 i_char = psz_unicode[pos];
782 if (i_char == '\r' || i_char == '\n')
788 FriBidiCharType base_dir = FRIBIDI_TYPE_LTR;
789 fribidi_log2vis((FriBidiChar*)psz_unicode + start_pos, pos - start_pos,
790 &base_dir, (FriBidiChar*)p_fribidi_string + start_pos, 0, 0, 0);
794 free( psz_unicode_orig );
795 psz_unicode = psz_unicode_orig = p_fribidi_string;
796 p_fribidi_string[ i_string_length ] = 0;
800 /* Calculate relative glyph positions and a bounding box for the
802 if( !(p_line = NewLine( psz_string )) )
804 msg_Err( p_filter, "out of memory" );
808 i_pen_x = i_pen_y = 0;
810 psz_line_start = psz_unicode;
812 #define face p_sys->p_face
813 #define glyph face->glyph
815 while( *psz_unicode )
817 i_char = *psz_unicode++;
818 if( i_char == '\r' ) /* ignore CR chars wherever they may be */
825 psz_line_start = psz_unicode;
826 if( !(p_next = NewLine( psz_string )) )
828 msg_Err( p_filter, "out of memory" );
831 p_line->p_next = p_next;
832 p_line->i_width = line.xMax;
833 p_line->i_height = face->size->metrics.height >> 6;
834 p_line->pp_glyphs[ i ] = NULL;
835 p_line->i_alpha = i_font_alpha;
836 p_line->i_red = i_red;
837 p_line->i_green = i_green;
838 p_line->i_blue = i_blue;
841 result.x = __MAX( result.x, line.xMax );
842 result.y += face->size->metrics.height >> 6;
845 line.xMin = line.xMax = line.yMin = line.yMax = 0;
846 i_pen_y += face->size->metrics.height >> 6;
848 msg_Dbg( p_filter, "Creating new line, i is %d", i );
853 i_glyph_index = FT_Get_Char_Index( face, i_char );
854 if( p_sys->i_use_kerning && i_glyph_index
858 FT_Get_Kerning( face, i_previous, i_glyph_index,
859 ft_kerning_default, &delta );
860 i_pen_x += delta.x >> 6;
863 p_line->p_glyph_pos[ i ].x = i_pen_x;
864 p_line->p_glyph_pos[ i ].y = i_pen_y;
865 i_error = FT_Load_Glyph( face, i_glyph_index, FT_LOAD_DEFAULT );
868 msg_Err( p_filter, "FT_Load_Glyph returned %d", i_error );
871 i_error = FT_Get_Glyph( glyph, &tmp_glyph );
874 msg_Err( p_filter, "FT_Get_Glyph returned %d", i_error );
877 FT_Glyph_Get_CBox( tmp_glyph, ft_glyph_bbox_pixels, &glyph_size );
878 i_error = FT_Glyph_To_Bitmap( &tmp_glyph, ft_render_mode_normal, 0, 1);
881 FT_Done_Glyph( tmp_glyph );
884 p_line->pp_glyphs[ i ] = (FT_BitmapGlyph)tmp_glyph;
887 line.xMax = p_line->p_glyph_pos[i].x + glyph_size.xMax -
888 glyph_size.xMin + ((FT_BitmapGlyph)tmp_glyph)->left;
889 if( line.xMax > (int)p_filter->fmt_out.video.i_visible_width - 20 )
891 p_line->pp_glyphs[ i ] = NULL;
893 p_line = NewLine( psz_string );
894 if( p_prev ) p_prev->p_next = p_line;
895 else p_lines = p_line;
897 while( psz_unicode > psz_line_start && *psz_unicode != ' ' )
901 if( psz_unicode == psz_line_start )
903 msg_Warn( p_filter, "unbreakable string" );
910 psz_unicode = psz_line_start;
913 line.xMin = line.xMax = line.yMin = line.yMax = 0;
916 line.yMax = __MAX( line.yMax, glyph_size.yMax );
917 line.yMin = __MIN( line.yMin, glyph_size.yMin );
919 i_previous = i_glyph_index;
920 i_pen_x += glyph->advance.x >> 6;
924 p_line->i_width = line.xMax;
925 p_line->i_height = face->size->metrics.height >> 6;
926 p_line->pp_glyphs[ i ] = NULL;
927 p_line->i_alpha = i_font_alpha;
928 p_line->i_red = i_red;
929 p_line->i_green = i_green;
930 p_line->i_blue = i_blue;
931 result.x = __MAX( result.x, line.xMax );
932 result.y += line.yMax - line.yMin;
937 p_region_out->i_x = p_region_in->i_x;
938 p_region_out->i_y = p_region_in->i_y;
940 if( config_GetInt( p_filter, "freetype-yuvp" ) )
941 Render( p_filter, p_region_out, p_lines, result.x, result.y );
943 RenderYUVA( p_filter, p_region_out, p_lines, result.x, result.y );
945 if( psz_unicode_orig ) free( psz_unicode_orig );
946 FreeLines( p_lines );
950 if( psz_unicode_orig ) free( psz_unicode_orig );
951 FreeLines( p_lines );
955 static void FreeLine( line_desc_t *p_line )
958 for( i = 0; p_line->pp_glyphs[ i ] != NULL; i++ )
960 FT_Done_Glyph( (FT_Glyph)p_line->pp_glyphs[ i ] );
962 free( p_line->pp_glyphs );
963 free( p_line->p_glyph_pos );
967 static void FreeLines( line_desc_t *p_lines )
969 line_desc_t *p_line, *p_next;
971 if( !p_lines ) return;
973 for( p_line = p_lines; p_line != NULL; p_line = p_next )
975 p_next = p_line->p_next;
980 static line_desc_t *NewLine( byte_t *psz_string )
983 line_desc_t *p_line = malloc( sizeof(line_desc_t) );
985 if( !p_line ) return NULL;
986 p_line->i_height = 0;
988 p_line->p_next = NULL;
990 /* We don't use CountUtf8Characters() here because we are not acutally
991 * sure the string is utf8. Better be safe than sorry. */
992 i_count = strlen( psz_string );
994 p_line->pp_glyphs = malloc( sizeof(FT_BitmapGlyph) * ( i_count + 1 ) );
995 if( p_line->pp_glyphs == NULL )
1000 p_line->pp_glyphs[0] = NULL;
1002 p_line->p_glyph_pos = malloc( sizeof( FT_Vector ) * i_count + 1 );
1003 if( p_line->p_glyph_pos == NULL )
1005 free( p_line->pp_glyphs );
1013 static int SetFontSize( filter_t *p_filter, int i_size )
1015 filter_sys_t *p_sys = p_filter->p_sys;
1017 if( i_size && i_size == p_sys->i_font_size ) return VLC_SUCCESS;
1023 if( !p_sys->i_default_font_size &&
1024 p_sys->i_display_height == (int)p_filter->fmt_out.video.i_height )
1027 if( p_sys->i_default_font_size )
1029 i_size = p_sys->i_default_font_size;
1033 var_Get( p_filter, "freetype-rel-fontsize", &val );
1034 i_size = (int)p_filter->fmt_out.video.i_height / val.i_int;
1035 p_filter->p_sys->i_display_height =
1036 p_filter->fmt_out.video.i_height;
1040 msg_Warn( p_filter, "Invalid fontsize, using 12" );
1044 msg_Dbg( p_filter, "Using fontsize: %i", i_size );
1047 p_sys->i_font_size = i_size;
1049 if( FT_Set_Pixel_Sizes( p_sys->p_face, 0, i_size ) )
1051 msg_Err( p_filter, "couldn't set font size to %d", i_size );
1052 return VLC_EGENERIC;