2 * producer_pango.c -- a pango-based titler
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Dan Dennedy <dan@dennedy.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include "producer_pango.h"
22 #include <framework/mlt_frame.h>
25 #include <gdk-pixbuf/gdk-pixbuf.h>
26 #include <pango/pangoft2.h>
27 #include <freetype/freetype.h>
30 struct producer_pango_s
32 struct mlt_producer_s parent;
46 // special color type used by internal pango routines
52 // Forward declarations
53 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int index );
54 static void producer_close( mlt_producer parent );
55 static void pango_draw_background( GdkPixbuf *pixbuf, rgba_color bg );
56 static GdkPixbuf *pango_get_pixbuf( const char *markup, const char *text, const char *font,
57 rgba_color fg, rgba_color bg, int pad, int align );
59 /** Return nonzero if the two strings are equal, ignoring case, up to
60 the first n characters.
62 int strncaseeq(const char *s1, const char *s2, size_t n)
66 if (tolower(*s1++) != tolower(*s2++))
72 /** Parse the alignment property.
75 static int alignment_parse( char* align )
77 int ret = pango_align_left;
80 else if ( isdigit( align[ 0 ] ) )
82 else if ( align[ 0 ] == 'c' || align[ 0 ] == 'm' )
83 ret = pango_align_center;
84 else if ( align[ 0 ] == 'r' )
85 ret = pango_align_right;
90 mlt_producer producer_pango_init( const char *filename )
92 producer_pango this = calloc( sizeof( struct producer_pango_s ), 1 );
93 if ( this != NULL && mlt_producer_init( &this->parent, this ) == 0 )
95 mlt_producer producer = &this->parent;
97 producer->get_frame = producer_get_frame;
98 producer->close = ( mlt_destructor )producer_close;
100 // This is required to initialise gdk-pixbuf
103 // Get the properties interface
104 mlt_properties properties = MLT_PRODUCER_PROPERTIES( &this->parent );
106 // Set the default properties
107 mlt_properties_set( properties, "fgcolour", "0xffffffff" );
108 mlt_properties_set( properties, "bgcolour", "0x00000000" );
109 mlt_properties_set_int( properties, "align", pango_align_left );
110 mlt_properties_set_int( properties, "pad", 0 );
111 mlt_properties_set( properties, "text", "" );
112 mlt_properties_set( properties, "font", "Sans 48" );
113 mlt_properties_set( properties, "encoding", "UTF-8" );
115 if ( filename == NULL )
117 mlt_properties_set( properties, "markup", "" );
119 else if ( filename[ 0 ] == '+' || strstr( filename, "/+" ) )
121 char *copy = strdup( filename + 1 );
123 if ( strstr( markup, "/+" ) )
124 markup = strstr( markup, "/+" ) + 2;
125 ( *strrchr( markup, '.' ) ) = '\0';
126 while ( strchr( markup, '~' ) )
127 ( *strchr( markup, '~' ) ) = '\n';
128 mlt_properties_set( properties, "resource", ( char * )filename );
129 mlt_properties_set( properties, "markup", markup );
134 FILE *f = fopen( filename, "r" );
142 while ( fgets( line, 80, f ) )
144 size += strlen( line ) + 1;
147 markup = realloc( markup, size );
148 strcat( markup, line );
152 markup = strdup( line );
157 if ( markup[ strlen( markup ) - 1 ] == '\n' )
158 markup[ strlen( markup ) - 1 ] = '\0';
160 mlt_properties_set( properties, "resource", ( char * ) filename );
161 mlt_properties_set( properties, "markup", ( char * ) ( markup == NULL ? "" : markup ) );
166 mlt_properties_set( properties, "markup", "" );
176 static void set_string( char **string, char *value, char *fallback )
181 *string = strdup( value );
183 else if ( *string == NULL && fallback != NULL )
185 *string = strdup( fallback );
187 else if ( *string != NULL && fallback == NULL )
194 rgba_color parse_color( char *color )
196 rgba_color result = { 0xff, 0xff, 0xff, 0xff };
198 if ( !strncmp( color, "0x", 2 ) )
200 unsigned int temp = 0;
201 sscanf( color + 2, "%x", &temp );
202 result.r = ( temp >> 24 ) & 0xff;
203 result.g = ( temp >> 16 ) & 0xff;
204 result.b = ( temp >> 8 ) & 0xff;
205 result.a = ( temp ) & 0xff;
207 else if ( !strcmp( color, "red" ) )
213 else if ( !strcmp( color, "green" ) )
219 else if ( !strcmp( color, "blue" ) )
227 unsigned int temp = 0;
228 sscanf( color, "%d", &temp );
229 result.r = ( temp >> 24 ) & 0xff;
230 result.g = ( temp >> 16 ) & 0xff;
231 result.b = ( temp >> 8 ) & 0xff;
232 result.a = ( temp ) & 0xff;
238 /** Convert a string property to UTF-8
240 static int iconv_utf8( mlt_properties properties, char *prop_name, const char* encoding )
242 char *text = mlt_properties_get( properties, prop_name );
245 iconv_t cd = iconv_open( "UTF-8", encoding );
246 if ( cd != ( iconv_t )-1 )
248 char *inbuf_p = strdup( text );
249 size_t inbuf_n = strlen( text );
250 size_t outbuf_n = inbuf_n * 6;
251 char *outbuf = mlt_pool_alloc( outbuf_n );
252 char *outbuf_p = outbuf;
254 if ( iconv( cd, &inbuf_p, &inbuf_n, &outbuf_p, &outbuf_n ) != -1 )
256 outbuf[ outbuf_n ] = 0;
257 mlt_properties_set( properties, prop_name, outbuf );
260 mlt_pool_release( outbuf );
266 static void refresh_image( mlt_frame frame, int width, int height )
269 GdkPixbuf *pixbuf = NULL;
271 // Obtain properties of frame
272 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
274 // Obtain the producer pango for this frame
275 producer_pango this = mlt_properties_get_data( properties, "producer_pango", NULL );
277 // Obtain the producer
278 mlt_producer producer = &this->parent;
280 // Obtain the producer properties
281 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
283 // Get producer properties
284 char *fg = mlt_properties_get( producer_props, "fgcolour" );
285 char *bg = mlt_properties_get( producer_props, "bgcolour" );
286 int align = alignment_parse( mlt_properties_get( producer_props, "align" ) );
287 int pad = mlt_properties_get_int( producer_props, "pad" );
288 char *markup = mlt_properties_get( producer_props, "markup" );
289 char *text = mlt_properties_get( producer_props, "text" );
290 char *font = mlt_properties_get( producer_props, "font" );
291 char *encoding = mlt_properties_get( producer_props, "encoding" );
293 // See if any properties changed
294 int property_changed = ( align != this->align );
295 property_changed = property_changed || ( this->fgcolor == NULL || ( fg && strcmp( fg, this->fgcolor ) ) );
296 property_changed = property_changed || ( this->bgcolor == NULL || ( bg && strcmp( bg, this->bgcolor ) ) );
297 property_changed = property_changed || ( pad != this->pad );
298 property_changed = property_changed || ( markup && this->markup && strcmp( markup, this->markup ) );
299 property_changed = property_changed || ( text && this->text && strcmp( text, this->text ) );
300 property_changed = property_changed || ( font && this->font && strcmp( font, this->font ) );
302 // Save the properties for next comparison
305 set_string( &this->fgcolor, fg, "0xffffffff" );
306 set_string( &this->bgcolor, bg, "0x00000000" );
307 set_string( &this->markup, markup, NULL );
308 set_string( &this->text, text, NULL );
309 set_string( &this->font, font, "Sans 48" );
311 if ( property_changed )
313 rgba_color fgcolor = parse_color( this->fgcolor );
314 rgba_color bgcolor = parse_color( this->bgcolor );
316 mlt_pool_release( this->image );
317 mlt_pool_release( this->alpha );
321 // Convert from specified encoding to UTF-8
322 if ( encoding != NULL && !strncaseeq( encoding, "utf-8", 5 ) && !strncaseeq( encoding, "utf8", 4 ) )
324 if ( markup != NULL && iconv_utf8( producer_props, "markup", encoding ) != -1 )
326 markup = mlt_properties_get( producer_props, "markup" );
327 set_string( &this->markup, markup, NULL );
329 if ( text != NULL && iconv_utf8( producer_props, "text", encoding ) != -1 )
331 text = mlt_properties_get( producer_props, "text" );
332 set_string( &this->text, text, NULL );
337 pixbuf = pango_get_pixbuf( markup, text, font, fgcolor, bgcolor, pad, align );
339 if ( pixbuf != NULL )
341 // Register this pixbuf for destruction and reuse
342 mlt_properties_set_data( producer_props, "pixbuf", pixbuf, 0, ( mlt_destructor )g_object_unref, NULL );
344 mlt_properties_set_int( producer_props, "real_width", gdk_pixbuf_get_width( pixbuf ) );
345 mlt_properties_set_int( producer_props, "real_height", gdk_pixbuf_get_height( pixbuf ) );
347 // Store the width/height of the pixbuf temporarily
348 this->width = gdk_pixbuf_get_width( pixbuf );
349 this->height = gdk_pixbuf_get_height( pixbuf );
352 else if ( width > 0 && ( this->image == NULL || width != this->width || height != this->height ) )
354 mlt_pool_release( this->image );
355 mlt_pool_release( this->alpha );
359 pixbuf = mlt_properties_get_data( producer_props, "pixbuf", NULL );
362 // If we have a pixbuf and a valid width
363 if ( pixbuf && width > 0 )
365 char *interps = mlt_properties_get( properties, "rescale.interp" );
366 int interp = GDK_INTERP_BILINEAR;
368 if ( strcmp( interps, "nearest" ) == 0 )
369 interp = GDK_INTERP_NEAREST;
370 else if ( strcmp( interps, "tiles" ) == 0 )
371 interp = GDK_INTERP_TILES;
372 else if ( strcmp( interps, "hyper" ) == 0 )
373 interp = GDK_INTERP_HYPER;
375 // fprintf( stderr, "SCALING PANGO from %dx%d to %dx%d was %dx%d\n", gdk_pixbuf_get_width( pixbuf ), gdk_pixbuf_get_height( pixbuf ), width, height, this->width, this->height );
377 // Note - the original pixbuf is already safe and ready for destruction
378 pixbuf = gdk_pixbuf_scale_simple( pixbuf, width, height, interp );
380 // Store width and height
382 this->height = height;
384 // Allocate/define image
385 this->image = mlt_pool_alloc( width * ( height + 1 ) * 2 );
386 this->alpha = mlt_pool_alloc( this->width * this->height );
389 mlt_convert_rgb24a_to_yuv422( gdk_pixbuf_get_pixels( pixbuf ),
390 this->width, this->height,
391 gdk_pixbuf_get_rowstride( pixbuf ),
392 this->image, this->alpha );
394 // Finished with pixbuf now
395 g_object_unref( pixbuf );
399 mlt_properties_set_int( properties, "width", this->width );
400 mlt_properties_set_int( properties, "height", this->height );
401 mlt_properties_set_int( properties, "real_width", mlt_properties_get_int( producer_props, "real_width" ) );
402 mlt_properties_set_int( properties, "real_height", mlt_properties_get_int( producer_props, "real_height" ) );
404 // pass the image data without destructor
405 mlt_properties_set_data( properties, "image", this->image, this->width * ( this->height + 1 ) * 2, NULL, NULL );
406 mlt_properties_set_data( properties, "alpha", this->alpha, this->width * this->height, NULL, NULL );
409 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
411 // Obtain properties of frame
412 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
414 *width = mlt_properties_get_int( properties, "rescale_width" );
415 *height = mlt_properties_get_int( properties, "rescale_height" );
418 refresh_image( frame, *width, *height );
421 //mlt_producer this = mlt_properties_get_data( properties, "producer_pango", NULL );
422 //*format = ( mlt_properties_get_int( MLT_PRODUCER_PROPERTIES( this ), "bpp" ) == 4 ) ? mlt_image_rgb24a : mlt_image_rgb24;
424 // May need to know the size of the image to clone it
428 uint8_t *image = mlt_properties_get_data( properties, "image", &size );
430 // Get width and height
431 *width = mlt_properties_get_int( properties, "width" );
432 *height = mlt_properties_get_int( properties, "height" );
434 // Clone if necessary
438 uint8_t *copy = mlt_pool_alloc( size );
440 memcpy( copy, image, size );
442 // We're going to pass the copy on
445 // Now update properties so we free the copy after
446 mlt_properties_set_data( properties, "image", copy, size, ( mlt_destructor )mlt_pool_release, NULL );
455 static uint8_t *producer_get_alpha_mask( mlt_frame this )
457 // Obtain properties of frame
458 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
460 // Return the alpha mask
461 return mlt_properties_get_data( properties, "alpha", NULL );
464 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
466 producer_pango this = producer->child;
469 *frame = mlt_frame_init( );
471 // Obtain properties of frame and producer
472 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
474 // Set the producer on the frame properties
475 mlt_properties_set_data( properties, "producer_pango", this, 0, NULL, NULL );
477 // Refresh the pango image
478 refresh_image( *frame, 0, 0 );
480 // Set producer-specific frame properties
481 mlt_properties_set_int( properties, "progressive", 1 );
482 mlt_properties_set_double( properties, "aspect_ratio", 1 );
484 // Set alpha call back
485 ( *frame )->get_alpha_mask = producer_get_alpha_mask;
487 // Stack the get image callback
488 mlt_frame_push_get_image( *frame, producer_get_image );
490 // Update timecode on the frame we're creating
491 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
493 // Calculate the next timecode
494 mlt_producer_prepare_next( producer );
499 static void producer_close( mlt_producer parent )
501 producer_pango this = parent->child;
502 mlt_pool_release( this->image );
503 mlt_pool_release( this->alpha );
504 free( this->fgcolor );
505 free( this->bgcolor );
506 free( this->markup );
509 parent->close = NULL;
510 mlt_producer_close( parent );
514 static void pango_draw_background( GdkPixbuf *pixbuf, rgba_color bg )
516 int ww = gdk_pixbuf_get_width( pixbuf );
517 int hh = gdk_pixbuf_get_height( pixbuf );
518 uint8_t *p = gdk_pixbuf_get_pixels( pixbuf );
521 for ( j = 0; j < hh; j++ )
523 for ( i = 0; i < ww; i++ )
533 static GdkPixbuf *pango_get_pixbuf( const char *markup, const char *text, const char *font, rgba_color fg, rgba_color bg, int pad, int align )
535 PangoFT2FontMap *fontmap = (PangoFT2FontMap*) pango_ft2_font_map_new();
536 PangoContext *context = pango_ft2_font_map_create_context( fontmap );
537 PangoLayout *layout = pango_layout_new( context );
540 GdkPixbuf *pixbuf = NULL;
543 uint8_t* dest = NULL;
546 pango_ft2_font_map_set_resolution( fontmap, 72, 72 );
547 pango_layout_set_width( layout, -1 ); // set wrapping constraints
548 pango_layout_set_font_description( layout, pango_font_description_from_string( font ) );
549 // pango_layout_set_spacing( layout, space );
550 pango_layout_set_alignment( layout, ( PangoAlignment ) align );
551 if ( markup != NULL && strcmp( markup, "" ) != 0 )
552 pango_layout_set_markup( layout, markup, strlen( markup ) );
553 else if ( text != NULL && strcmp( text, "" ) != 0 )
554 pango_layout_set_text( layout, text, strlen( text ) );
556 pango_layout_set_text( layout, " ", 2 );
557 pango_layout_get_pixel_size( layout, &w, &h );
559 pixbuf = gdk_pixbuf_new( GDK_COLORSPACE_RGB, TRUE /* has alpha */, 8, w + 2 * pad, h + 2 * pad );
560 pango_draw_background( pixbuf, bg );
562 stride = gdk_pixbuf_get_rowstride( pixbuf );
565 bitmap.pitch = 32 * ( ( w + 31 ) / 31 );
567 bitmap.buffer = mlt_pool_alloc( h * bitmap.pitch );
568 bitmap.num_grays = 256;
569 bitmap.pixel_mode = ft_pixel_mode_grays;
571 memset( bitmap.buffer, 0, h * bitmap.pitch );
573 pango_ft2_render_layout( &bitmap, layout, 0, 0 );
576 x = ( gdk_pixbuf_get_width( pixbuf ) - w - 2 * pad ) * align / 2 + pad;
577 dest = gdk_pixbuf_get_pixels( pixbuf ) + 4 * x + pad * stride;
578 for ( j = 0; j < h; j++ )
581 for ( i = 0; i < w; i++ )
583 float a = ( float ) bitmap.buffer[ j * bitmap.pitch + i ] / 255.0;
584 *d++ = ( int ) ( a * fg.r + ( 1 - a ) * bg.r );
585 *d++ = ( int ) ( a * fg.g + ( 1 - a ) * bg.g );
586 *d++ = ( int ) ( a * fg.b + ( 1 - a ) * bg.b );
587 *d++ = ( int ) ( a * fg.a + ( 1 - a ) * bg.a );
591 mlt_pool_release( bitmap.buffer );
592 g_object_unref( layout );
593 g_object_unref( context );
594 g_object_unref( fontmap );