1 /*****************************************************************************
2 * freetype.c : Put text on the video, using freetype2
3 *****************************************************************************
4 * Copyright (C) 2002, 2003 VideoLAN
5 * $Id: freetype.c,v 1.6 2003/07/20 21:41:13 hartman 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
40 #define DEFAULT_FONT "/System/Library/Fonts/LucidaGrande.dfont"
42 #define DEFAULT_FONT ""
45 /*****************************************************************************
47 *****************************************************************************/
48 static int Create ( vlc_object_t * );
49 static void Destroy ( vlc_object_t * );
51 static void Render ( vout_thread_t *, picture_t *,
52 const subpicture_t * );
53 static void RenderI420( vout_thread_t *, picture_t *,
54 const subpicture_t * );
55 static int AddText ( vout_thread_t *, byte_t *, text_style_t *, int,
56 int, int, mtime_t, mtime_t );
57 static int GetUnicodeCharFromUTF8( byte_t ** );
59 /*****************************************************************************
61 *****************************************************************************/
62 #define FONT_TEXT N_("Font")
63 #define FONT_LONGTEXT N_("Filename of Font")
64 #define FONTSIZE_TEXT N_("Font size")
65 #define FONTSIZE_LONGTEXT N_("The size of the fonts used by the osd module" )
68 add_category_hint( N_("Fonts"), NULL, VLC_FALSE );
69 add_file( "freetype-font", DEFAULT_FONT, NULL, FONT_TEXT, FONT_LONGTEXT, VLC_FALSE );
70 add_integer( "freetype-fontsize", 16, NULL, FONTSIZE_TEXT, FONTSIZE_LONGTEXT, VLC_FALSE );
71 set_description( _("freetype2 font renderer") );
72 set_capability( "text renderer", 100 );
73 add_shortcut( "text" );
74 set_callbacks( Create, Destroy );
78 Describes a string to be displayed on the video, or a linked list of
81 struct subpicture_sys_t
88 /** The string associated with this subpicture */
90 /** NULL-terminated list of glyphs making the string */
91 FT_BitmapGlyph *pp_glyphs;
92 /** list of relative positions for the glyphs */
93 FT_Vector *p_glyph_pos;
96 /*****************************************************************************
97 * vout_sys_t: osd_text local data
98 *****************************************************************************
99 * This structure is part of the video output thread descriptor.
100 * It describes the osd-text specific properties of an output thread.
101 *****************************************************************************/
102 struct text_renderer_sys_t
104 FT_Library p_library; /* handle to library */
105 FT_Face p_face; /* handle to face object */
107 vlc_bool_t i_use_kerning;
108 uint8_t pi_gamma[256];
110 /* more prototypes */
111 //static void ComputeBoundingBox( subpicture_sys_t * );
112 static void FreeString( subpicture_t * );
114 /*****************************************************************************
115 * Create: allocates osd-text video thread output method
116 *****************************************************************************
117 * This function allocates and initializes a Clone vout method.
118 *****************************************************************************/
119 #define gamma_value 2.0
120 static int Create( vlc_object_t *p_this )
122 vout_thread_t *p_vout = (vout_thread_t *)p_this;
125 double gamma_inv = 1.0f / gamma_value;
127 /* Allocate structure */
128 p_vout->p_text_renderer_data = malloc( sizeof( text_renderer_sys_t ) );
129 if( p_vout->p_text_renderer_data == NULL )
131 msg_Err( p_vout, "out of memory" );
135 for (i = 0; i < 256; i++) {
136 p_vout->p_text_renderer_data->pi_gamma[i] =
137 (uint8_t)( pow( (double)i / 255.0f, gamma_inv) * 255.0f );
138 //msg_Dbg( p_vout, "%d", p_vout->p_text_renderer_data->pi_gamma[i]);
141 /* Look what method was requested */
142 psz_fontfile = config_GetPsz( p_vout, "freetype-font" );
143 i_error = FT_Init_FreeType( &p_vout->p_text_renderer_data->p_library );
146 msg_Err( p_vout, "couldn't initialize freetype" );
147 free( p_vout->p_text_renderer_data );
150 i_error = FT_New_Face( p_vout->p_text_renderer_data->p_library,
152 &p_vout->p_text_renderer_data->p_face );
153 if( i_error == FT_Err_Unknown_File_Format )
155 msg_Err( p_vout, "file %s have unknown format", psz_fontfile );
156 FT_Done_FreeType( p_vout->p_text_renderer_data->p_library );
157 free( p_vout->p_text_renderer_data );
162 msg_Err( p_vout, "failed to load font file" );
163 FT_Done_FreeType( p_vout->p_text_renderer_data->p_library );
164 free( p_vout->p_text_renderer_data );
167 i_error = FT_Select_Charmap( p_vout->p_text_renderer_data->p_face,
168 ft_encoding_unicode );
171 msg_Err( p_vout, "Font has no unicode translation table" );
172 FT_Done_Face( p_vout->p_text_renderer_data->p_face );
173 FT_Done_FreeType( p_vout->p_text_renderer_data->p_library );
174 free( p_vout->p_text_renderer_data );
178 p_vout->p_text_renderer_data->i_use_kerning =
179 FT_HAS_KERNING(p_vout->p_text_renderer_data->p_face);
181 i_error = FT_Set_Pixel_Sizes( p_vout->p_text_renderer_data->p_face, 0,
182 config_GetInt( p_vout, "freetype-fontsize" ) );
185 msg_Err( p_vout, "couldn't set font size to %d",
186 config_GetInt( p_vout, "osd-fontsize" ) );
187 free( p_vout->p_text_renderer_data );
190 p_vout->pf_add_string = AddText;
194 /*****************************************************************************
195 * Destroy: destroy Clone video thread output method
196 *****************************************************************************
197 * Clean up all data and library connections
198 *****************************************************************************/
199 static void Destroy( vlc_object_t *p_this )
201 vout_thread_t *p_vout = (vout_thread_t *)p_this;
202 FT_Done_Face( p_vout->p_text_renderer_data->p_face );
203 FT_Done_FreeType( p_vout->p_text_renderer_data->p_library );
204 free( p_vout->p_text_renderer_data );
207 /*****************************************************************************
208 * Render: place string in picture
209 *****************************************************************************
210 * This function merges the previously rendered freetype glyphs into a picture
211 *****************************************************************************/
212 static void Render( vout_thread_t *p_vout, picture_t *p_pic,
213 const subpicture_t *p_subpic )
215 switch( p_vout->output.i_chroma )
217 /* I420 target, no scaling */
218 case VLC_FOURCC('I','4','2','0'):
219 case VLC_FOURCC('I','Y','U','V'):
220 case VLC_FOURCC('Y','V','1','2'):
221 RenderI420( p_vout, p_pic, p_subpic );
224 /* RV16 target, scaling */
225 case VLC_FOURCC('R','V','1','6'):
226 RenderRV16( p_vout, p_pic, p_spu, p_spu->p_sys->b_crop );
229 /* RV32 target, scaling */
230 case VLC_FOURCC('R','V','2','4'):
231 case VLC_FOURCC('R','V','3','2'):
232 RenderRV32( p_vout, p_pic, p_spu, p_spu->p_sys->b_crop );
235 /* NVidia overlay, no scaling */
236 case VLC_FOURCC('Y','U','Y','2'):
237 RenderYUY2( p_vout, p_pic, p_spu, p_spu->p_sys->b_crop );
241 msg_Err( p_vout, "unknown chroma, can't render SPU" );
246 static void RenderI420( vout_thread_t *p_vout, picture_t *p_pic,
247 const subpicture_t *p_subpic )
249 subpicture_sys_t *p_string = p_subpic->p_sys;
250 int i_plane, x, y, pen_x, pen_y;
253 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
256 int i_pitch = p_pic->p[i_plane].i_pitch;
258 p_in = p_pic->p[i_plane].p_pixels;
262 if ( p_string->i_flags & OSD_ALIGN_BOTTOM )
264 pen_y = p_pic->p[i_plane].i_lines - p_string->i_height -
265 p_string->i_y_margin;
269 pen_y = p_string->i_y_margin;
271 if ( p_string->i_flags & OSD_ALIGN_RIGHT )
273 pen_x = i_pitch - p_string->i_width
274 - p_string->i_x_margin;
278 pen_x = p_string->i_x_margin;
281 for( i = 0; p_string->pp_glyphs[i] != NULL; i++ )
283 if( p_string->pp_glyphs[i] )
285 FT_BitmapGlyph p_glyph = p_string->pp_glyphs[ i ];
286 #define alpha p_vout->p_text_renderer_data->pi_gamma[ p_glyph->bitmap.buffer[ x + y * p_glyph->bitmap.width ] ]
287 #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 ]
288 for(y = 0; y < p_glyph->bitmap.rows; y++ )
290 for( x = 0; x < p_glyph->bitmap.width; x++ )
293 // pixel = (pixel^alpha)^pixel;
294 pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 ) +
295 ( 255 * alpha >> 8 );
305 if ( p_string->i_flags & OSD_ALIGN_BOTTOM )
307 pen_y = p_pic->p[i_plane].i_lines - ( p_string->i_height>>1) -
308 (p_string->i_y_margin>>1);
312 pen_y = p_string->i_y_margin >> 1;
314 if ( p_string->i_flags & OSD_ALIGN_RIGHT )
316 pen_x = i_pitch - ( p_string->i_width >> 1 )
317 - ( p_string->i_x_margin >> 1 );
321 pen_x = p_string->i_x_margin >> 1;
324 for( i = 0; p_string->pp_glyphs[i] != NULL; i++ )
326 if( p_string->pp_glyphs[i] )
328 FT_BitmapGlyph p_glyph = p_string->pp_glyphs[ i ];
329 #define alpha p_vout->p_text_renderer_data->pi_gamma[ p_glyph->bitmap.buffer[ ( x + y * p_glyph->bitmap.width ) ] ]
330 #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) ]
331 for( y = 0; y < p_glyph->bitmap.rows; y+=2 )
333 for( x = 0; x < p_glyph->bitmap.width; x+=2 )
336 // pixel = (pixel^alpha)^pixel;
337 pixel = ( ( pixel * ( 0xFF - alpha ) ) >> 8 ) +
338 ( 0x80 * alpha >> 8 );
350 * This function receives a string and creates a subpicture for it. It
351 * also calculates the size needed for this string, and renders the
352 * needed glyphs into memory. It is used as pf_add_string callback in
353 * the vout method by this module
355 static int AddText ( vout_thread_t *p_vout, byte_t *psz_string,
356 text_style_t *p_style, int i_flags, int i_hmargin,
357 int i_vmargin, mtime_t i_start, mtime_t i_stop )
359 subpicture_sys_t *p_string;
360 int i, i_pen_y, i_pen_x, i_error, i_glyph_index, i_previous, i_char;
361 subpicture_t *p_subpic;
374 /* Create and initialize a subpicture */
375 p_subpic = vout_CreateSubPicture( p_vout, MEMORY_SUBPICTURE );
376 if ( p_subpic == NULL )
380 p_subpic->pf_render = Render;
381 p_subpic->pf_destroy = FreeString;
382 p_subpic->i_start = i_start;
383 p_subpic->i_stop = i_stop;
386 p_subpic->b_ephemer = VLC_TRUE;
390 p_subpic->b_ephemer = VLC_FALSE;
393 /* Create and initialize private data for the subpicture */
394 p_string = malloc( sizeof(subpicture_sys_t) );
395 if ( p_string == NULL )
397 vout_DestroySubPicture( p_vout, p_subpic );
400 p_subpic->p_sys = p_string;
401 p_string->i_flags = i_flags;
402 p_string->i_x_margin = i_hmargin;
403 p_string->i_y_margin = i_vmargin;
405 p_string->psz_text = strdup( psz_string );
406 p_string->pp_glyphs = malloc( sizeof(FT_GlyphSlot)
407 * ( strlen( p_string->psz_text ) + 1 ) );
408 if( p_string->pp_glyphs == NULL )
410 msg_Err( p_vout, "Out of memory" );
413 p_string->p_glyph_pos = malloc( sizeof( FT_Vector )
414 * strlen( p_string->psz_text ) );
415 if( p_string->p_glyph_pos == NULL )
417 msg_Err( p_vout, "Out of memory" );
421 /* Calculate relative glyph positions and a bounding box for the
429 i_char = GetUnicodeCharFromUTF8( &psz_string );
430 #define face p_vout->p_text_renderer_data->p_face
431 #define glyph face->glyph
432 if ( i_char == '\n' )
435 result.x = __MAX( result.x, line.xMax );
436 result.y += face->height >> 6;
441 i_pen_y += face->height >> 6;
444 i_glyph_index = FT_Get_Char_Index( face, i_char );
445 if ( p_vout->p_text_renderer_data->i_use_kerning && i_glyph_index
449 FT_Get_Kerning( face, i_previous, i_glyph_index,
450 ft_kerning_default, &delta );
451 i_pen_x += delta.x >> 6;
454 p_string->p_glyph_pos[ i ].x = i_pen_x;
455 p_string->p_glyph_pos[ i ].y = i_pen_y;
456 i_error = FT_Load_Glyph( face, i_glyph_index, FT_LOAD_DEFAULT );
459 msg_Err( p_vout, "FT_Load_Glyph returned %d", i_error );
462 i_error = FT_Get_Glyph( glyph, &tmp_glyph );
465 msg_Err( p_vout, "FT_Get_Glyph returned %d", i_error );
468 FT_Glyph_Get_CBox( tmp_glyph, ft_glyph_bbox_pixels, &glyph_size );
469 i_error = FT_Glyph_To_Bitmap( &tmp_glyph,
470 ft_render_mode_normal,
471 &p_string->p_glyph_pos[i],
473 if ( i_error ) continue;
474 p_string->pp_glyphs[ i ] = (FT_BitmapGlyph)tmp_glyph;
477 line.xMax = p_string->p_glyph_pos[i].x + glyph_size.xMax - glyph_size.xMin;
478 line.yMax = __MAX( line.yMax, glyph_size.yMax );
479 line.yMin = __MIN( line.yMin, glyph_size.yMin );
481 i_previous = i_glyph_index;
482 i_pen_x += glyph->advance.x >> 6;
485 p_string->pp_glyphs[i] = NULL;
486 result.x = __MAX( result.x, line.xMax );
487 result.y += line.yMax - line.yMin;
488 p_string->i_height = result.y;
489 p_string->i_width = result.x;
490 msg_Dbg( p_vout, "string height is %d, width is %d",
491 p_string->i_height, p_string->i_width );
492 msg_Dbg( p_vout, "adding string \"%s\" at (%d,%d) start_date "I64Fd
493 " end_date" I64Fd, p_string->psz_text, p_string->i_x_margin,
494 p_string->i_y_margin, i_start, i_stop );
495 vout_DisplaySubPicture( p_vout, p_subpic );
499 static void FreeString( subpicture_t *p_subpic )
502 subpicture_sys_t *p_string = p_subpic->p_sys;
503 for ( i = 0; p_string->pp_glyphs[ i ] != NULL; i++ )
505 FT_Done_Glyph( (FT_Glyph)p_string->pp_glyphs[ i ] );
507 free( p_string->psz_text );
508 free( p_string->p_glyph_pos );
509 free( p_string->pp_glyphs );
513 /* convert one or more utf8 bytes into a unicode character */
514 static int GetUnicodeCharFromUTF8( byte_t **ppsz_utf8_string )
516 int i_remaining_bytes, i_char = 0;
517 if( ( **ppsz_utf8_string & 0xFC ) == 0xFC )
519 i_char = **ppsz_utf8_string & 1;
520 i_remaining_bytes = 5;
522 else if( ( **ppsz_utf8_string & 0xF8 ) == 0xF8 )
524 i_char = **ppsz_utf8_string & 3;
525 i_remaining_bytes = 4;
527 else if( ( **ppsz_utf8_string & 0xF0 ) == 0xF0 )
529 i_char = **ppsz_utf8_string & 7;
530 i_remaining_bytes = 3;
532 else if( ( **ppsz_utf8_string & 0xE0 ) == 0xE0 )
534 i_char = **ppsz_utf8_string & 15;
535 i_remaining_bytes = 2;
537 else if( ( **ppsz_utf8_string & 0xC0 ) == 0xC0 )
539 i_char = **ppsz_utf8_string & 31;
540 i_remaining_bytes = 1;
544 i_char = **ppsz_utf8_string;
545 i_remaining_bytes = 0;
547 while( i_remaining_bytes )
549 (*ppsz_utf8_string)++;
551 i_char = ( i_char << 6 ) + ( **ppsz_utf8_string & 0x3F );
553 (*ppsz_utf8_string)++;