1 /*****************************************************************************
2 * freetype.c : Put text on the video, using freetype2
3 *****************************************************************************
4 * Copyright (C) 2002 - 2005 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_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 subpicture_region_t *RenderText( filter_t *, subpicture_t *,
75 subpicture_region_t* );
76 static line_desc_t *NewLine( byte_t * );
78 /*****************************************************************************
80 *****************************************************************************/
81 #define FONT_TEXT N_("Font")
82 #define FONT_LONGTEXT N_("Font filename")
83 #define FONTSIZE_TEXT N_("Font size in pixels")
84 #define FONTSIZE_LONGTEXT N_("The size of the fonts used by the osd module. " \
85 "If set to something different than 0 this option will override the " \
86 "relative font size " )
87 #define OPACITY_TEXT N_("Opacity, 0..255")
88 #define OPACITY_LONGTEXT N_("The opacity (inverse of transparency) of overlay text. " \
89 "1 = transparent, 255 = totally opaque. " )
90 #define COLOR_TEXT N_("Text Default Color")
91 #define COLOR_LONGTEXT N_("The color of overlay text. 1 byte for each color, hexadecimal." \
92 "#000000 = all colors off, 0xFF0000 = just Red, 0xFFFFFF = all color on [White]" )
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") };
99 static int pi_color_values[] = {
100 0x00000000, 0x00808080, 0x00C0C0C0, 0x00FFFFFF, 0x00800000,
101 0x00FF0000, 0x00FF00FF, 0x00FFFF00, 0x00808000, 0x00008000, 0x00008080,
102 0x0000FF00, 0x00800080, 0x00000080, 0x000000FF, 0x0000FFFF };
104 static char *ppsz_color_descriptions[] = {
105 N_("Black"), N_("Gray"), N_("Silver"), N_("White"), N_("Maroon"),
106 N_("Red"), N_("Fuchsia"), N_("Yellow"), N_("Olive"), N_("Green"), N_("Teal"),
107 N_("Lime"), N_("Purple"), N_("Navy"), N_("Blue"), N_("Aqua") };
110 set_shortname( _("Freetype"));
111 set_description( _("Freetype2 font renderer") );
112 set_category( CAT_VIDEO );
113 set_subcategory( SUBCAT_VIDEO_TEXT );
115 add_file( "freetype-font", DEFAULT_FONT, NULL, FONT_TEXT, FONT_LONGTEXT,
118 add_integer( "freetype-fontsize", 0, NULL, FONTSIZE_TEXT,
119 FONTSIZE_LONGTEXT, VLC_TRUE );
121 /* opacity valid on 0..255, with default 255 = fully opaque */
122 add_integer_with_range( "freetype-opacity", 255, 0, 255, NULL,
123 OPACITY_TEXT, OPACITY_LONGTEXT, VLC_FALSE );
125 /* hook to the color values list, with default 0x00ffffff = white */
126 add_integer( "freetype-color", 0x00FFFFFF, NULL, COLOR_TEXT,
127 COLOR_LONGTEXT, VLC_TRUE );
128 change_integer_list( pi_color_values, ppsz_color_descriptions, 0 );
130 add_integer( "freetype-rel-fontsize", 16, NULL, FONTSIZER_TEXT,
131 FONTSIZER_LONGTEXT, VLC_FALSE );
132 change_integer_list( pi_sizes, ppsz_sizes_text, 0 );
134 set_capability( "text renderer", 100 );
135 add_shortcut( "text" );
136 set_callbacks( Create, Destroy );
140 * Private data in a subpicture. Describes a string.
142 typedef struct subpicture_data_t
146 /** The string associated with this subpicture */
148 line_desc_t *p_lines;
154 /** NULL-terminated list of glyphs making the string */
155 FT_BitmapGlyph *pp_glyphs;
156 /** list of relative positions for the glyphs */
157 FT_Vector *p_glyph_pos;
163 static subpicture_region_t *Render( filter_t *, subpicture_t *, subpicture_data_t *, uint8_t, int, int, int );
164 static void FreeString( subpicture_data_t * );
165 static void FreeLine( line_desc_t * );
167 /*****************************************************************************
168 * filter_sys_t: freetype local data
169 *****************************************************************************
170 * This structure is part of the video output thread descriptor.
171 * It describes the freetype specific properties of an output thread.
172 *****************************************************************************/
175 FT_Library p_library; /* handle to library */
176 FT_Face p_face; /* handle to face object */
177 vlc_bool_t i_use_kerning;
178 uint8_t i_font_opacity; /* freetype-opacity */
179 int i_font_color; /* freetype-color */
180 int i_red, i_blue, i_green; /* function vars to render */
182 uint8_t i_opacity; /* function var to render */
183 uint8_t pi_gamma[256];
186 /*****************************************************************************
187 * Create: allocates osd-text video thread output method
188 *****************************************************************************
189 * This function allocates and initializes a Clone vout method.
190 *****************************************************************************/
191 static int Create( vlc_object_t *p_this )
193 filter_t *p_filter = (filter_t *)p_this;
195 char *psz_fontfile = NULL;
199 /* Allocate structure */
200 p_sys = malloc( sizeof( filter_sys_t ) );
203 msg_Err( p_filter, "out of memory" );
207 p_sys->p_library = 0;
208 p_sys->i_font_size = 0;
210 for( i = 0; i < 256; i++ ) p_sys->pi_gamma[i] = i + (256 - i) / 16;
211 p_sys->pi_gamma[0] = 0;
213 var_Create( p_filter, "freetype-font",
214 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
215 var_Create( p_filter, "freetype-fontsize",
216 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
217 var_Create( p_filter, "freetype-rel-fontsize",
218 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
219 var_Create( p_filter, "freetype-opacity",
220 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
221 var_Get( p_filter, "freetype-opacity", &val );
222 p_sys->i_font_opacity = __MAX( __MIN( val.i_int, 255 ), 0 );
223 var_Create( p_filter, "freetype-color",
224 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
225 var_Get( p_filter, "freetype-color", &val );
226 if( ( val.i_int > -1 ) && ( val.i_int < 0x01000000 ) ) /* valid range */
228 p_sys->i_font_color = val.i_int;
230 else msg_Warn(p_filter, "Invalid freetype color specified, using white [0xFFFFFF]");
232 /* Look what method was requested */
233 var_Get( p_filter, "freetype-font", &val );
234 psz_fontfile = val.psz_string;
235 if( !psz_fontfile || !*psz_fontfile )
237 if( psz_fontfile ) free( psz_fontfile );
238 psz_fontfile = (char *)malloc( PATH_MAX + 1 );
240 GetWindowsDirectory( psz_fontfile, PATH_MAX + 1 );
241 strcat( psz_fontfile, "\\fonts\\arial.ttf" );
243 strcpy( psz_fontfile, DEFAULT_FONT );
245 msg_Err( p_filter, "user didn't specify a font" );
250 i_error = FT_Init_FreeType( &p_sys->p_library );
253 msg_Err( p_filter, "couldn't initialize freetype" );
257 i_error = FT_New_Face( p_sys->p_library, psz_fontfile ? psz_fontfile : "",
259 if( i_error == FT_Err_Unknown_File_Format )
261 msg_Err( p_filter, "file %s have unknown format", psz_fontfile );
266 msg_Err( p_filter, "failed to load font file %s", psz_fontfile );
270 i_error = FT_Select_Charmap( p_sys->p_face, ft_encoding_unicode );
273 msg_Err( p_filter, "Font has no unicode translation table" );
277 p_sys->i_use_kerning = FT_HAS_KERNING( p_sys->p_face );
280 var_Get( p_filter, "freetype-fontsize", &val );
283 p_sys->i_font_size = val.i_int;
287 var_Get( p_filter, "freetype-rel-fontsize", &val );
288 p_sys->i_font_size = (int)p_filter->fmt_out.video.i_height / val.i_int;
290 if( p_sys->i_font_size <= 0 )
292 msg_Warn( p_filter, "Invalid fontsize, using 12" );
293 p_sys->i_font_size = 12;
295 msg_Dbg( p_filter, "Using fontsize: %i", p_sys->i_font_size);
297 i_error = FT_Set_Pixel_Sizes( p_sys->p_face, 0, p_sys->i_font_size );
300 msg_Err( p_filter, "couldn't set font size to %d", p_sys->i_font_size );
304 if( psz_fontfile ) free( psz_fontfile );
305 p_filter->pf_render_string = RenderText;
306 p_filter->p_sys = p_sys;
310 if( p_sys->p_face ) FT_Done_Face( p_sys->p_face );
311 if( p_sys->p_library ) FT_Done_FreeType( p_sys->p_library );
312 if( psz_fontfile ) free( psz_fontfile );
317 /*****************************************************************************
318 * Destroy: destroy Clone video thread output method
319 *****************************************************************************
320 * Clean up all data and library connections
321 *****************************************************************************/
322 static void Destroy( vlc_object_t *p_this )
324 filter_t *p_filter = (filter_t *)p_this;
325 filter_sys_t *p_sys = p_filter->p_sys;
327 FT_Done_Face( p_sys->p_face );
328 FT_Done_FreeType( p_sys->p_library );
332 /*****************************************************************************
333 * Render: place string in picture
334 *****************************************************************************
335 * This function merges the previously rendered freetype glyphs into a picture
336 *****************************************************************************/
337 static subpicture_region_t *Render( filter_t *p_filter, subpicture_t *p_spu,
338 subpicture_data_t *p_string,
340 int red, int green, int blue)
342 filter_sys_t *p_sys = p_filter->p_sys;
344 uint8_t *p_y, *p_u, *p_v, *p_a;
346 int i, x, y, i_pitch;
347 uint8_t i_y, i_u, i_v; /* YUV values, derived from incoming RGB */
348 subpicture_region_t *p_region;
350 /* Calculate text color components */
351 i_y = (uint8_t)(( 66 * red + 129 * green + 25 * blue + 128) >> 8) + 16;
352 i_u = (uint8_t)(( -38 * red - 74 * green + 112 * blue + 128) >> 8) + 128;
353 i_v = (uint8_t)(( 112 * red - 94 * green - 18 * blue + 128) >> 8) + 128;
355 /* Create a new subpicture region */
356 memset( &fmt, 0, sizeof(video_format_t) );
357 fmt.i_chroma = VLC_FOURCC('Y','U','V','A');
358 fmt.i_aspect = VOUT_ASPECT_FACTOR;
359 fmt.i_width = fmt.i_visible_width = p_string->i_width + 2;
360 fmt.i_height = fmt.i_visible_height = p_string->i_height + 2;
361 fmt.i_x_offset = fmt.i_y_offset = 0;
362 p_region = p_spu->pf_create_region( VLC_OBJECT(p_filter), &fmt );
365 msg_Err( p_filter, "cannot allocate SPU region" );
369 p_region->i_x = p_region->i_y = 0;
370 p_y = p_region->picture.Y_PIXELS;
371 p_u = p_region->picture.U_PIXELS;
372 p_v = p_region->picture.V_PIXELS;
373 p_a = p_region->picture.A_PIXELS;
374 i_pitch = p_region->picture.Y_PITCH;
376 /* Initialize the region pixels */
377 memset( p_y, 0x00, i_pitch * p_region->fmt.i_height );
378 memset( p_u, 0x80, i_pitch * p_region->fmt.i_height );
379 memset( p_v, 0x80, i_pitch * p_region->fmt.i_height );
380 memset( p_a, 0x00, i_pitch * p_region->fmt.i_height );
382 #define pi_gamma p_sys->pi_gamma
384 for( p_line = p_string->p_lines; p_line != NULL; p_line = p_line->p_next )
386 int i_glyph_tmax = 0;
387 int i_bitmap_offset, i_offset;
388 for( i = 0; p_line->pp_glyphs[i] != NULL; i++ )
390 FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ];
391 i_glyph_tmax = __MAX( i_glyph_tmax, p_glyph->top );
394 for( i = 0; p_line->pp_glyphs[i] != NULL; i++ )
396 FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ];
398 i_offset = ( p_line->p_glyph_pos[ i ].y +
399 i_glyph_tmax - p_glyph->top + 1 ) *
400 i_pitch + p_line->p_glyph_pos[ i ].x + p_glyph->left + 1;
402 for( y = 0, i_bitmap_offset = 0; y < p_glyph->bitmap.rows; y++ )
404 for( x = 0; x < p_glyph->bitmap.width; x++, i_bitmap_offset++ )
406 if( !pi_gamma[p_glyph->bitmap.buffer[i_bitmap_offset]] )
410 p_a[i_offset + x] = ((uint16_t)p_a[i_offset + x] +
411 pi_gamma[p_glyph->bitmap.buffer[i_bitmap_offset]])*opacity/512;
412 i_offset += i_pitch; x--;
413 p_a[i_offset + x] = ((uint16_t)p_a[i_offset + x] +
414 pi_gamma[p_glyph->bitmap.buffer[i_bitmap_offset]])*opacity/512;
416 p_a[i_offset + x] = ((uint16_t)p_a[i_offset + x] +
417 pi_gamma[p_glyph->bitmap.buffer[i_bitmap_offset]])*opacity/512;
418 i_offset += i_pitch; x--;
419 p_a[i_offset + x] = ((uint16_t)p_a[i_offset + x] +
420 pi_gamma[p_glyph->bitmap.buffer[i_bitmap_offset]])*opacity/512;
426 i_offset = ( p_line->p_glyph_pos[ i ].y +
427 i_glyph_tmax - p_glyph->top + 1 ) *
428 i_pitch + p_line->p_glyph_pos[ i ].x + p_glyph->left + 1;
430 for( y = 0, i_bitmap_offset = 0; y < p_glyph->bitmap.rows; y++ )
432 for( x = 0; x < p_glyph->bitmap.width; x++, i_bitmap_offset++ )
434 p_y[i_offset + x] = i_y*opacity*pi_gamma[p_glyph->bitmap.buffer[i_bitmap_offset]]/(256*256);
435 p_u[i_offset + x] = i_u;
436 p_v[i_offset + x] = i_v;
448 * This function receives a string and creates a subpicture for it. It
449 * also calculates the size needed for this string, and renders the
450 * needed glyphs into memory. It is used as pf_add_string callback in
451 * the vout method by this module
453 static subpicture_region_t *RenderText( filter_t *p_filter,
454 subpicture_t *p_subpic,
455 subpicture_region_t *p_region )
457 filter_sys_t *p_sys = p_filter->p_sys;
458 subpicture_data_t *p_string = 0;
459 line_desc_t *p_line = 0, *p_next = 0, *p_prev = 0;
460 int i, i_pen_y, i_pen_x, i_error, i_glyph_index, i_previous;
461 uint32_t *psz_unicode, *psz_unicode_orig = 0, i_char, *psz_line_start;
464 vlc_iconv_t iconv_handle = (vlc_iconv_t)(-1);
465 int i_font_color, i_font_opacity, i_font_size;
466 subpicture_region_t *p_res;
474 if( !p_region ) return NULL;
475 psz_string = p_region->psz_text;
476 if( !psz_string || !*psz_string ) goto error;
477 i_font_color = p_region->i_font_color;
478 i_font_opacity = p_region->i_font_opacity;
479 i_font_size = p_region->i_font_size;
488 /* Create and initialize private data for the subpicture */
489 p_string = malloc( sizeof(subpicture_data_t) );
492 msg_Err( p_filter, "out of memory" );
495 p_string->p_lines = 0;
496 p_string->psz_text = strdup( psz_string );
498 psz_unicode = psz_unicode_orig =
499 malloc( ( strlen(psz_string) + 1 ) * sizeof(uint32_t) );
500 if( psz_unicode == NULL )
502 msg_Err( p_filter, "out of memory" );
505 #if defined(WORDS_BIGENDIAN)
506 iconv_handle = vlc_iconv_open( "UCS-4BE", "UTF-8" );
508 iconv_handle = vlc_iconv_open( "UCS-4LE", "UTF-8" );
510 if( iconv_handle == (vlc_iconv_t)-1 )
512 msg_Warn( p_filter, "unable to do conversion" );
516 /* Set up the glyphs for the desired font size. By definition,
517 p_sys->i_font_size is a valid value, else the initial Create would
518 have failed. Using -1 as a flag to use the freetype-fontsize */
519 if ( i_font_size < 0 )
521 FT_Set_Pixel_Sizes( p_sys->p_face, 0, p_sys->i_font_size );
525 i_error = FT_Set_Pixel_Sizes( p_sys->p_face, 0, i_font_size );
528 msg_Warn( p_filter, "Invalid font size to RenderText, using %d",
529 p_sys->i_font_size );
530 FT_Set_Pixel_Sizes( p_sys->p_face, 0, p_sys->i_font_size );
535 char *p_in_buffer, *p_out_buffer;
536 size_t i_in_bytes, i_out_bytes, i_out_bytes_left, i_ret;
537 i_in_bytes = strlen( psz_string );
538 i_out_bytes = i_in_bytes * sizeof( uint32_t );
539 i_out_bytes_left = i_out_bytes;
540 p_in_buffer = psz_string;
541 p_out_buffer = (char *)psz_unicode;
542 i_ret = vlc_iconv( iconv_handle, &p_in_buffer, &i_in_bytes,
543 &p_out_buffer, &i_out_bytes_left );
545 vlc_iconv_close( iconv_handle );
549 msg_Warn( p_filter, "failed to convert string to unicode (%s), "
550 "bytes left %d", strerror(errno), i_in_bytes );
553 *(uint32_t*)p_out_buffer = 0;
554 i_string_length = (i_out_bytes - i_out_bytes_left) / sizeof(uint32_t);
557 #if defined(HAVE_FRIBIDI)
559 uint32_t *p_fribidi_string;
560 FriBidiCharType base_dir = FRIBIDI_TYPE_ON;
561 p_fribidi_string = malloc( (i_string_length + 1) * sizeof(uint32_t) );
562 fribidi_log2vis( (FriBidiChar*)psz_unicode, i_string_length,
563 &base_dir, (FriBidiChar*)p_fribidi_string, 0, 0, 0 );
564 free( psz_unicode_orig );
565 psz_unicode = psz_unicode_orig = p_fribidi_string;
566 p_fribidi_string[ i_string_length ] = 0;
570 /* Calculate relative glyph positions and a bounding box for the
572 p_line = NewLine( psz_string );
575 msg_Err( p_filter, "out of memory" );
578 p_string->p_lines = p_line;
583 psz_line_start = psz_unicode;
585 #define face p_sys->p_face
586 #define glyph face->glyph
588 while( *psz_unicode )
590 i_char = *psz_unicode++;
591 if( i_char == '\r' ) /* ignore CR chars wherever they may be */
598 psz_line_start = psz_unicode;
599 p_next = NewLine( psz_string );
602 msg_Err( p_filter, "out of memory" );
605 p_line->p_next = p_next;
606 p_line->i_width = line.xMax;
607 p_line->i_height = face->size->metrics.height >> 6;
608 p_line->pp_glyphs[ i ] = NULL;
611 result.x = __MAX( result.x, line.xMax );
612 result.y += face->size->metrics.height >> 6;
619 i_pen_y += face->size->metrics.height >> 6;
621 msg_Dbg( p_filter, "Creating new line, i is %d", i );
627 i_glyph_index = FT_Get_Char_Index( face, i_char );
628 if( p_sys->i_use_kerning && i_glyph_index
632 FT_Get_Kerning( face, i_previous, i_glyph_index,
633 ft_kerning_default, &delta );
634 i_pen_x += delta.x >> 6;
637 p_line->p_glyph_pos[ i ].x = i_pen_x;
638 p_line->p_glyph_pos[ i ].y = i_pen_y;
639 i_error = FT_Load_Glyph( face, i_glyph_index, FT_LOAD_DEFAULT );
642 msg_Err( p_filter, "FT_Load_Glyph returned %d", i_error );
645 i_error = FT_Get_Glyph( glyph, &tmp_glyph );
648 msg_Err( p_filter, "FT_Get_Glyph returned %d", i_error );
651 FT_Glyph_Get_CBox( tmp_glyph, ft_glyph_bbox_pixels, &glyph_size );
652 i_error = FT_Glyph_To_Bitmap( &tmp_glyph, ft_render_mode_normal,
654 if( i_error ) continue;
655 p_line->pp_glyphs[ i ] = (FT_BitmapGlyph)tmp_glyph;
658 line.xMax = p_line->p_glyph_pos[i].x + glyph_size.xMax - glyph_size.xMin + ((FT_BitmapGlyph)tmp_glyph)->left;
659 if( line.xMax > p_filter->fmt_out.video.i_visible_width - 20 )
661 p_line->pp_glyphs[ i ] = NULL;
663 p_line = NewLine( psz_string );
666 p_prev->p_next = p_line;
670 p_string->p_lines = p_line;
672 while( psz_unicode > psz_line_start && *psz_unicode != ' ' )
676 if( psz_unicode == psz_line_start )
678 msg_Warn( p_filter, "unbreakable string" );
686 psz_unicode = psz_line_start;
696 line.yMax = __MAX( line.yMax, glyph_size.yMax );
697 line.yMin = __MIN( line.yMin, glyph_size.yMin );
699 i_previous = i_glyph_index;
700 i_pen_x += glyph->advance.x >> 6;
704 p_line->i_width = line.xMax;
705 p_line->i_height = face->size->metrics.height >> 6;
706 p_line->pp_glyphs[ i ] = NULL;
707 result.x = __MAX( result.x, line.xMax );
708 result.y += line.yMax - line.yMin;
709 p_string->i_height = result.y;
710 p_string->i_width = result.x;
714 /* check to see whether to use the default color/opacity, or another one: */
715 if( i_font_color < 0 )
717 p_sys->i_blue = p_sys->i_font_color & 0x000000FF;
718 p_sys->i_green = ( p_sys->i_font_color & 0x0000FF00 ) >> 8;
719 p_sys->i_red = ( p_sys->i_font_color & 0x00FF0000 ) >> 16;
723 p_sys->i_blue = i_font_color & 0x000000FF;
724 p_sys->i_green = ( i_font_color & 0x0000FF00 ) >> 8;
725 p_sys->i_red = ( i_font_color & 0x00FF0000 ) >> 16;
727 if( i_font_opacity < 0 )
729 p_sys->i_opacity = p_sys->i_font_opacity;
733 p_sys->i_opacity = (uint8_t)( i_font_opacity & 0x000000FF );
736 p_res = Render( p_filter, p_subpic, p_string, p_sys->i_opacity,
737 p_sys->i_red, p_sys->i_green, p_sys->i_blue );
738 FreeString( p_string );
739 if( psz_unicode_orig ) free( psz_unicode_orig );
743 FreeString( p_string );
744 if( p_subpic ) p_filter->pf_sub_buffer_del( p_filter, p_subpic );
745 if( psz_unicode_orig ) free( psz_unicode_orig );
749 static void FreeLine( line_desc_t *p_line )
752 for( i = 0; p_line->pp_glyphs[ i ] != NULL; i++ )
754 FT_Done_Glyph( (FT_Glyph)p_line->pp_glyphs[ i ] );
756 free( p_line->pp_glyphs );
757 free( p_line->p_glyph_pos );
761 static void FreeString( subpicture_data_t *p_string )
763 line_desc_t *p_line, *p_next;
765 if( !p_string ) return;
767 for( p_line = p_string->p_lines; p_line != NULL; p_line = p_next )
769 p_next = p_line->p_next;
773 free( p_string->psz_text );
777 static line_desc_t *NewLine( byte_t *psz_string )
780 line_desc_t *p_line = malloc( sizeof(line_desc_t) );
785 p_line->i_height = 0;
787 p_line->p_next = NULL;
789 /* We don't use CountUtf8Characters() here because we are not acutally
790 * sure the string is utf8. Better be safe than sorry. */
791 i_count = strlen( psz_string );
793 p_line->pp_glyphs = malloc( sizeof(FT_BitmapGlyph)
795 if( p_line->pp_glyphs == NULL )
800 p_line->pp_glyphs[0] = NULL;
802 p_line->p_glyph_pos = malloc( sizeof( FT_Vector )
804 if( p_line->p_glyph_pos == NULL )
806 free( p_line->pp_glyphs );