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 $
7 * Authors: Sigmund Augdal <sigmunau@idi.ntnu.no>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
36 #include FT_FREETYPE_H
39 /*****************************************************************************
41 *****************************************************************************/
42 static int Create ( vlc_object_t * );
43 static void Destroy ( vlc_object_t * );
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 ** );
53 /*****************************************************************************
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" )
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 );
72 Describes a string to be displayed on the video, or a linked list of
75 struct subpicture_sys_t
82 /** The string associated with this subpicture */
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;
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
98 FT_Library p_library; /* handle to library */
99 FT_Face p_face; /* handle to face object */
101 vlc_bool_t i_use_kerning;
102 uint8_t pi_gamma[256];
104 /* more prototypes */
105 //static void ComputeBoundingBox( subpicture_sys_t * );
106 static void FreeString( subpicture_t * );
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 )
116 vout_thread_t *p_vout = (vout_thread_t *)p_this;
119 double gamma_inv = 1.0f / gamma_value;
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 )
125 msg_Err( p_vout, "out of memory" );
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]);
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 );
140 msg_Err( p_vout, "couldn't initialize freetype" );
141 free( p_vout->p_text_renderer_data );
144 i_error = FT_New_Face( p_vout->p_text_renderer_data->p_library,
146 &p_vout->p_text_renderer_data->p_face );
147 if( i_error == FT_Err_Unknown_File_Format )
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 );
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 );
161 i_error = FT_Select_Charmap( p_vout->p_text_renderer_data->p_face,
162 ft_encoding_unicode );
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 );
172 p_vout->p_text_renderer_data->i_use_kerning =
173 FT_HAS_KERNING(p_vout->p_text_renderer_data->p_face);
175 i_error = FT_Set_Pixel_Sizes( p_vout->p_text_renderer_data->p_face, 0,
176 config_GetInt( p_vout, "freetype-fontsize" ) );
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 );
184 p_vout->pf_add_string = AddText;
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 )
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 );
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 )
209 switch( p_vout->output.i_chroma )
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 );
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 );
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 );
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 );
235 msg_Err( p_vout, "unknown chroma, can't render SPU" );
240 static void RenderI420( vout_thread_t *p_vout, picture_t *p_pic,
241 const subpicture_t *p_subpic )
243 subpicture_sys_t *p_string = p_subpic->p_sys;
244 int i_plane, x, y, pen_x, pen_y;
247 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
250 int i_pitch = p_pic->p[i_plane].i_pitch;
252 p_in = p_pic->p[i_plane].p_pixels;
256 if ( p_string->i_flags & OSD_ALIGN_BOTTOM )
258 pen_y = p_pic->p[i_plane].i_lines - p_string->i_height -
259 p_string->i_y_margin;
263 pen_y = p_string->i_y_margin;
265 if ( p_string->i_flags & OSD_ALIGN_RIGHT )
267 pen_x = i_pitch - p_string->i_width
268 - p_string->i_x_margin;
272 pen_x = p_string->i_x_margin;
275 for( i = 0; p_string->pp_glyphs[i] != NULL; i++ )
277 if( p_string->pp_glyphs[i] )
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++ )
284 for( x = 0; x < p_glyph->bitmap.width; x++ )
287 // pixel = (pixel^alpha)^pixel;
288 pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 ) +
289 ( 255 * alpha >> 8 );
299 if ( p_string->i_flags & OSD_ALIGN_BOTTOM )
301 pen_y = p_pic->p[i_plane].i_lines - ( p_string->i_height>>1) -
302 (p_string->i_y_margin>>1);
306 pen_y = p_string->i_y_margin >> 1;
308 if ( p_string->i_flags & OSD_ALIGN_RIGHT )
310 pen_x = i_pitch - ( p_string->i_width >> 1 )
311 - ( p_string->i_x_margin >> 1 );
315 pen_x = p_string->i_x_margin >> 1;
318 for( i = 0; p_string->pp_glyphs[i] != NULL; i++ )
320 if( p_string->pp_glyphs[i] )
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 )
327 for( x = 0; x < p_glyph->bitmap.width; x+=2 )
330 // pixel = (pixel^alpha)^pixel;
331 pixel = ( ( pixel * ( 0xFF - alpha ) ) >> 8 ) +
332 ( 0x80 * alpha >> 8 );
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
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 )
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;
368 /* Create and initialize a subpicture */
369 p_subpic = vout_CreateSubPicture( p_vout, MEMORY_SUBPICTURE );
370 if ( p_subpic == NULL )
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;
380 p_subpic->b_ephemer = VLC_TRUE;
384 p_subpic->b_ephemer = VLC_FALSE;
387 /* Create and initialize private data for the subpicture */
388 p_string = malloc( sizeof(subpicture_sys_t) );
389 if ( p_string == NULL )
391 vout_DestroySubPicture( p_vout, p_subpic );
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;
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 )
404 msg_Err( p_vout, "Out of memory" );
407 p_string->p_glyph_pos = malloc( sizeof( FT_Vector )
408 * strlen( p_string->psz_text ) );
409 if( p_string->p_glyph_pos == NULL )
411 msg_Err( p_vout, "Out of memory" );
415 /* Calculate relative glyph positions and a bounding box for the
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' )
429 result.x = __MAX( result.x, line.xMax );
430 result.y += face->height >> 6;
435 i_pen_y += face->height >> 6;
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
443 FT_Get_Kerning( face, i_previous, i_glyph_index,
444 ft_kerning_default, &delta );
445 i_pen_x += delta.x >> 6;
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 );
453 msg_Err( p_vout, "FT_Load_Glyph returned %d", i_error );
456 i_error = FT_Get_Glyph( glyph, &tmp_glyph );
459 msg_Err( p_vout, "FT_Get_Glyph returned %d", i_error );
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],
467 if ( i_error ) continue;
468 p_string->pp_glyphs[ i ] = (FT_BitmapGlyph)tmp_glyph;
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 );
475 i_previous = i_glyph_index;
476 i_pen_x += glyph->advance.x >> 6;
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 );
493 static void FreeString( subpicture_t *p_subpic )
496 subpicture_sys_t *p_string = p_subpic->p_sys;
497 for ( i = 0; p_string->pp_glyphs[ i ] != NULL; i++ )
499 FT_Done_Glyph( (FT_Glyph)p_string->pp_glyphs[ i ] );
501 free( p_string->psz_text );
502 free( p_string->p_glyph_pos );
503 free( p_string->pp_glyphs );
507 /* convert one or more utf8 bytes into a unicode character */
508 static int GetUnicodeCharFromUTF8( byte_t **ppsz_utf8_string )
510 int i_remaining_bytes, i_char = 0;
511 if( ( **ppsz_utf8_string & 0xFC ) == 0xFC )
513 i_char = **ppsz_utf8_string & 1;
514 i_remaining_bytes = 5;
516 else if( ( **ppsz_utf8_string & 0xF8 ) == 0xF8 )
518 i_char = **ppsz_utf8_string & 3;
519 i_remaining_bytes = 4;
521 else if( ( **ppsz_utf8_string & 0xF0 ) == 0xF0 )
523 i_char = **ppsz_utf8_string & 7;
524 i_remaining_bytes = 3;
526 else if( ( **ppsz_utf8_string & 0xE0 ) == 0xE0 )
528 i_char = **ppsz_utf8_string & 15;
529 i_remaining_bytes = 2;
531 else if( ( **ppsz_utf8_string & 0xC0 ) == 0xC0 )
533 i_char = **ppsz_utf8_string & 31;
534 i_remaining_bytes = 1;
538 i_char = **ppsz_utf8_string;
539 i_remaining_bytes = 0;
541 while( i_remaining_bytes )
543 (*ppsz_utf8_string)++;
545 i_char = ( i_char << 6 ) + ( **ppsz_utf8_string & 0x3F );
547 (*ppsz_utf8_string)++;