]> git.sesse.net Git - mlt/blob - src/modules/gtk2/producer_pango.c
producer_pango.c
[mlt] / src / modules / gtk2 / producer_pango.c
1 /*
2  * producer_pango.c -- a pango-based titler
3  * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4  * Author: Dan Dennedy <dan@dennedy.org>
5  *
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.
10  *
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.
15  *
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.
19  */
20
21 #include "producer_pango.h"
22 #include <framework/mlt_frame.h>
23 #include <framework/mlt_geometry.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <gdk-pixbuf/gdk-pixbuf.h>
27 #include <pango/pangoft2.h>
28 #include <freetype/freetype.h>
29 #include <iconv.h>
30 #include <pthread.h>
31
32 static pthread_mutex_t pango_mutex = PTHREAD_MUTEX_INITIALIZER;
33
34 struct producer_pango_s
35 {
36         struct mlt_producer_s parent;
37         int width;
38         int height;
39         uint8_t *image;
40         uint8_t *alpha;
41         char *fgcolor;
42         char *bgcolor;
43         int   align;
44         int   pad;
45         char *markup;
46         char *text;
47         char *font;
48         int weight;
49 };
50
51 // special color type used by internal pango routines
52 typedef struct
53 {
54         uint8_t r, g, b, a;
55 } rgba_color;
56
57 // Forward declarations
58 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int index );
59 static void producer_close( mlt_producer parent );
60 static void pango_draw_background( GdkPixbuf *pixbuf, rgba_color bg );
61 static GdkPixbuf *pango_get_pixbuf( const char *markup, const char *text, const char *font,
62         rgba_color fg, rgba_color bg, int pad, int align, int weight, int size );
63
64 /** Return nonzero if the two strings are equal, ignoring case, up to
65     the first n characters.
66 */
67 int strncaseeq(const char *s1, const char *s2, size_t n)
68 {
69         for ( ; n > 0; n--)
70         {
71                 if (tolower(*s1++) != tolower(*s2++))
72                         return 0;
73         }
74         return 1;
75 }
76
77 /** Parse the alignment property.
78 */
79
80 static int alignment_parse( char* align )
81 {
82         int ret = pango_align_left;
83
84         if ( align == NULL );
85         else if ( isdigit( align[ 0 ] ) )
86                 ret = atoi( align );
87         else if ( align[ 0 ] == 'c' || align[ 0 ] == 'm' )
88                 ret = pango_align_center;
89         else if ( align[ 0 ] == 'r' )
90                 ret = pango_align_right;
91
92         return ret;
93 }
94
95 static PangoFT2FontMap *fontmap = NULL;
96
97 mlt_producer producer_pango_init( const char *filename )
98 {
99         producer_pango this = calloc( sizeof( struct producer_pango_s ), 1 );
100         if ( this != NULL && mlt_producer_init( &this->parent, this ) == 0 )
101         {
102                 mlt_producer producer = &this->parent;
103
104                 pthread_mutex_lock( &pango_mutex );
105                 if ( fontmap == NULL )
106                         fontmap = (PangoFT2FontMap*) pango_ft2_font_map_new();
107                 g_type_init();
108                 pthread_mutex_unlock( &pango_mutex );
109
110                 producer->get_frame = producer_get_frame;
111                 producer->close = ( mlt_destructor )producer_close;
112
113                 // Get the properties interface
114                 mlt_properties properties = MLT_PRODUCER_PROPERTIES( &this->parent );
115
116                 // Set the default properties
117                 mlt_properties_set( properties, "fgcolour", "0xffffffff" );
118                 mlt_properties_set( properties, "bgcolour", "0x00000000" );
119                 mlt_properties_set_int( properties, "align", pango_align_left );
120                 mlt_properties_set_int( properties, "pad", 0 );
121                 mlt_properties_set( properties, "text", "" );
122                 mlt_properties_set( properties, "font", "Sans 48" );
123                 mlt_properties_set( properties, "encoding", "UTF-8" );
124                 mlt_properties_set_int( properties, "weight", PANGO_WEIGHT_NORMAL );
125
126                 if ( filename == NULL )
127                 {
128                         mlt_properties_set( properties, "markup", "" );
129                 }
130                 else if ( filename[ 0 ] == '+' || strstr( filename, "/+" ) )
131                 {
132                         char *copy = strdup( filename + 1 );
133                         char *markup = copy;
134                         if ( strstr( markup, "/+" ) )
135                                 markup = strstr( markup, "/+" ) + 2;
136                         ( *strrchr( markup, '.' ) ) = '\0';
137                         while ( strchr( markup, '~' ) )
138                                 ( *strchr( markup, '~' ) ) = '\n';
139                         mlt_properties_set( properties, "resource", ( char * )filename );
140                         mlt_properties_set( properties, "markup", markup );
141                         free( copy );
142                 }
143                 else if ( strstr( filename, ".mpl" ) ) 
144                 {
145                         int i = 0;
146                         mlt_properties contents = mlt_properties_load( filename );
147                         mlt_geometry key_frames = mlt_geometry_init( );
148                         struct mlt_geometry_item_s item;
149                         mlt_properties_set( properties, "resource", ( char * )filename );
150                         mlt_properties_set_data( properties, "contents", contents, 0, ( mlt_destructor )mlt_properties_close, NULL );
151                         mlt_properties_set_data( properties, "key_frames", key_frames, 0, ( mlt_destructor )mlt_geometry_close, NULL );
152
153                         // Make sure we have at least one entry
154                         if ( mlt_properties_get( contents, "0" ) == NULL )
155                                 mlt_properties_set( contents, "0", "" );
156
157                         for ( i = 0; i < mlt_properties_count( contents ); i ++ )
158                         {
159                                 char *name = mlt_properties_get_name( contents, i );
160                                 char *value = mlt_properties_get_value( contents, i );
161                                 while ( value != NULL && strchr( value, '~' ) )
162                                         ( *strchr( value, '~' ) ) = '\n';
163                                 item.frame = atoi( name );
164                                 mlt_geometry_insert( key_frames, &item );
165                         }
166                 }
167                 else
168                 {
169                         FILE *f = fopen( filename, "r" );
170                         if ( f != NULL )
171                         {
172                                 char line[81];
173                                 char *markup = NULL;
174                                 size_t size = 0;
175                                 line[80] = '\0';
176                                 
177                                 while ( fgets( line, 80, f ) )
178                                 {
179                                         size += strlen( line ) + 1;
180                                         if ( markup )
181                                         {
182                                                 markup = realloc( markup, size );
183                                                 strcat( markup, line );
184                                         }
185                                         else
186                                         {
187                                                 markup = strdup( line );
188                                         }
189                                 }
190                                 fclose( f );
191
192                                 if ( markup[ strlen( markup ) - 1 ] == '\n' ) 
193                                         markup[ strlen( markup ) - 1 ] = '\0';
194
195                                 mlt_properties_set( properties, "resource", ( char * ) filename );
196                                 mlt_properties_set( properties, "markup", ( char * ) ( markup == NULL ? "" : markup ) );
197                                 free( markup );
198                         }
199                         else
200                         {
201                                 mlt_properties_set( properties, "markup", "" );
202                         }
203                 }
204
205                 return producer;
206         }
207         free( this );
208         return NULL;
209 }
210
211 static void set_string( char **string, char *value, char *fallback )
212 {
213         if ( value != NULL )
214         {
215                 free( *string );
216                 *string = strdup( value );
217         }
218         else if ( *string == NULL && fallback != NULL )
219         {
220                 *string = strdup( fallback );
221         }
222         else if ( *string != NULL && fallback == NULL )
223         {
224                 free( *string );
225                 *string = NULL;
226         }
227 }
228
229 rgba_color parse_color( char *color )
230 {
231         rgba_color result = { 0xff, 0xff, 0xff, 0xff };
232
233         if ( !strncmp( color, "0x", 2 ) )
234         {
235                 unsigned int temp = 0;
236                 sscanf( color + 2, "%x", &temp );
237                 result.r = ( temp >> 24 ) & 0xff;
238                 result.g = ( temp >> 16 ) & 0xff;
239                 result.b = ( temp >> 8 ) & 0xff;
240                 result.a = ( temp ) & 0xff;
241         }
242         else if ( !strcmp( color, "red" ) )
243         {
244                 result.r = 0xff;
245                 result.g = 0x00;
246                 result.b = 0x00;
247         }
248         else if ( !strcmp( color, "green" ) )
249         {
250                 result.r = 0x00;
251                 result.g = 0xff;
252                 result.b = 0x00;
253         }
254         else if ( !strcmp( color, "blue" ) )
255         {
256                 result.r = 0x00;
257                 result.g = 0x00;
258                 result.b = 0xff;
259         }
260         else
261         {
262                 unsigned int temp = 0;
263                 sscanf( color, "%d", &temp );
264                 result.r = ( temp >> 24 ) & 0xff;
265                 result.g = ( temp >> 16 ) & 0xff;
266                 result.b = ( temp >> 8 ) & 0xff;
267                 result.a = ( temp ) & 0xff;
268         }
269
270         return result;
271 }
272
273 /** Convert a string property to UTF-8
274 */
275 static int iconv_utf8( mlt_properties properties, char *prop_name, const char* encoding )
276 {
277         char *text = mlt_properties_get( properties, prop_name );
278         int result = -1;
279         
280         iconv_t cd = iconv_open( "UTF-8", encoding );
281         if ( cd != ( iconv_t )-1 )
282         {
283                 char *inbuf_p = text;
284                 size_t inbuf_n = strlen( text );
285                 size_t outbuf_n = inbuf_n * 6;
286                 char *outbuf = mlt_pool_alloc( outbuf_n );
287                 char *outbuf_p = outbuf;
288                 
289                 memset( outbuf, 0, outbuf_n );
290
291                 if ( text != NULL && strcmp( text, "" ) && iconv( cd, &inbuf_p, &inbuf_n, &outbuf_p, &outbuf_n ) != -1 )
292                         mlt_properties_set( properties, prop_name, outbuf );
293                 else
294                         mlt_properties_set( properties, prop_name, "" );
295
296                 mlt_pool_release( outbuf );
297                 iconv_close( cd );
298                 result = 0;
299         }
300         return result;
301 }
302
303 static void refresh_image( mlt_frame frame, int width, int height )
304 {
305         // Pixbuf 
306         GdkPixbuf *pixbuf = NULL;
307
308         // Obtain properties of frame
309         mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
310
311         // Obtain the producer pango for this frame
312         producer_pango this = mlt_properties_get_data( properties, "producer_pango", NULL );
313
314         // Obtain the producer 
315         mlt_producer producer = &this->parent;
316
317         // Obtain the producer properties
318         mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
319
320         // Get producer properties
321         char *fg = mlt_properties_get( producer_props, "fgcolour" );
322         char *bg = mlt_properties_get( producer_props, "bgcolour" );
323         int align = alignment_parse( mlt_properties_get( producer_props, "align" ) );
324         int pad = mlt_properties_get_int( producer_props, "pad" );
325         char *markup = mlt_properties_get( producer_props, "markup" );
326         char *text = mlt_properties_get( producer_props, "text" );
327         char *font = mlt_properties_get( producer_props, "font" );
328         char *encoding = mlt_properties_get( producer_props, "encoding" );
329         int weight = mlt_properties_get_int( producer_props, "weight" );
330         int size = mlt_properties_get_int( producer_props, "size" );
331         int property_changed = ( align != this->align );
332
333         // Check for file support
334         int position = mlt_properties_get_position( properties, "pango_position" );
335         mlt_properties contents = mlt_properties_get_data( producer_props, "contents", NULL );
336         mlt_geometry key_frames = mlt_properties_get_data( producer_props, "key_frames", NULL );
337         struct mlt_geometry_item_s item;
338         if ( contents != NULL )
339         {
340                 char temp[ 20 ];
341                 mlt_geometry_prev_key( key_frames, &item, position );
342                 sprintf( temp, "%d", item.frame );
343                 markup = mlt_properties_get( contents, temp );
344         }
345         
346         // See if any properties changed
347         property_changed = property_changed || ( this->fgcolor == NULL || ( fg && strcmp( fg, this->fgcolor ) ) );
348         property_changed = property_changed || ( this->bgcolor == NULL || ( bg && strcmp( bg, this->bgcolor ) ) );
349         property_changed = property_changed || ( pad != this->pad );
350         property_changed = property_changed || ( markup && this->markup && strcmp( markup, this->markup ) );
351         property_changed = property_changed || ( text && this->text && strcmp( text, this->text ) );
352         property_changed = property_changed || ( font && this->font && strcmp( font, this->font ) );
353         property_changed = property_changed || ( weight != this->weight );
354
355         // Save the properties for next comparison
356         this->align = align;
357         this->pad = pad;
358         set_string( &this->fgcolor, fg, "0xffffffff" );
359         set_string( &this->bgcolor, bg, "0x00000000" );
360         set_string( &this->markup, markup, NULL );
361         set_string( &this->text, text, NULL );
362         set_string( &this->font, font, "Sans 48" );
363         this->weight = weight;
364
365         if ( property_changed )
366         {
367                 rgba_color fgcolor = parse_color( this->fgcolor );
368                 rgba_color bgcolor = parse_color( this->bgcolor );
369
370                 mlt_pool_release( this->image );
371                 mlt_pool_release( this->alpha );
372                 this->image = NULL;
373                 this->alpha = NULL;
374
375                 // Convert from specified encoding to UTF-8
376                 if ( encoding != NULL && !strncaseeq( encoding, "utf-8", 5 ) && !strncaseeq( encoding, "utf8", 4 ) )
377                 {
378                         if ( markup != NULL && iconv_utf8( producer_props, "markup", encoding ) != -1 )
379                         {
380                                 markup = mlt_properties_get( producer_props, "markup" );
381                                 set_string( &this->markup, markup, NULL );
382                         }
383                         if ( text != NULL && iconv_utf8( producer_props, "text", encoding ) != -1 )
384                         {
385                                 text = mlt_properties_get( producer_props, "text" );
386                                 set_string( &this->text, text, NULL );
387                         }
388                 }
389                 
390                 // Render the title
391                 pixbuf = pango_get_pixbuf( markup, text, font, fgcolor, bgcolor, pad, align, weight, size );
392
393                 if ( pixbuf != NULL )
394                 {
395                         // Register this pixbuf for destruction and reuse
396                         mlt_properties_set_data( producer_props, "pixbuf", pixbuf, 0, ( mlt_destructor )g_object_unref, NULL );
397
398                         mlt_properties_set_int( producer_props, "real_width", gdk_pixbuf_get_width( pixbuf ) );
399                         mlt_properties_set_int( producer_props, "real_height", gdk_pixbuf_get_height( pixbuf ) );
400
401                         // Store the width/height of the pixbuf temporarily
402                         this->width = gdk_pixbuf_get_width( pixbuf );
403                         this->height = gdk_pixbuf_get_height( pixbuf );
404                 }
405         }
406         else if ( width > 0 && ( this->image == NULL || width != this->width || height != this->height ) )
407         {
408                 mlt_pool_release( this->image );
409                 mlt_pool_release( this->alpha );
410                 this->image = NULL;
411                 this->alpha = NULL;
412
413                 pixbuf = mlt_properties_get_data( producer_props, "pixbuf", NULL );
414         }
415
416         // If we have a pixbuf and a valid width
417         if ( pixbuf && width > 0 )
418         {
419                 char *interps = mlt_properties_get( properties, "rescale.interp" );
420                 int interp = GDK_INTERP_BILINEAR;
421
422                 if ( strcmp( interps, "nearest" ) == 0 )
423                         interp = GDK_INTERP_NEAREST;
424                 else if ( strcmp( interps, "tiles" ) == 0 )
425                         interp = GDK_INTERP_TILES;
426                 else if ( strcmp( interps, "hyper" ) == 0 )
427                         interp = GDK_INTERP_HYPER;
428
429 //              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 );
430                         
431                 // Note - the original pixbuf is already safe and ready for destruction
432                 pixbuf = gdk_pixbuf_scale_simple( pixbuf, width, height, interp );
433
434                 // Store width and height
435                 this->width = width;
436                 this->height = height;
437
438                 // Allocate/define image
439                 this->image = mlt_pool_alloc( width * ( height + 1 ) * 2 );
440                 this->alpha = mlt_pool_alloc( this->width * this->height );
441
442                 // Convert the image
443                 mlt_convert_rgb24a_to_yuv422( gdk_pixbuf_get_pixels( pixbuf ),
444                                                                           this->width, this->height,
445                                                                           gdk_pixbuf_get_rowstride( pixbuf ),
446                                                                           this->image, this->alpha );
447
448                 // Finished with pixbuf now
449                 g_object_unref( pixbuf );
450         }
451
452         // Set width/height
453         mlt_properties_set_int( properties, "width", this->width );
454         mlt_properties_set_int( properties, "height", this->height );
455         mlt_properties_set_int( properties, "real_width", mlt_properties_get_int( producer_props, "real_width" ) );
456         mlt_properties_set_int( properties, "real_height", mlt_properties_get_int( producer_props, "real_height" ) );
457
458         // pass the image data without destructor
459         mlt_properties_set_data( properties, "image", this->image, this->width * ( this->height + 1 ) * 2, NULL, NULL );
460         mlt_properties_set_data( properties, "alpha", this->alpha, this->width * this->height, NULL, NULL );
461 }
462
463 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
464 {
465         // Obtain properties of frame
466         mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
467
468         // We need to know the size of the image to clone it
469         int image_size = 0;
470         int alpha_size = 0;
471
472         // Alpha channel
473         uint8_t *alpha = NULL;
474
475         *width = mlt_properties_get_int( properties, "rescale_width" );
476         *height = mlt_properties_get_int( properties, "rescale_height" );
477
478         // Refresh the image
479         pthread_mutex_lock( &pango_mutex );
480         refresh_image( frame, *width, *height );
481
482         // Get the image
483         *buffer = mlt_properties_get_data( properties, "image", &image_size );
484         alpha = mlt_properties_get_data( properties, "alpha", &alpha_size );
485
486         // Get width and height
487         *width = mlt_properties_get_int( properties, "width" );
488         *height = mlt_properties_get_int( properties, "height" );
489
490         // Always clone here to allow 'animated' text
491         if ( *buffer != NULL )
492         {
493                 // Clone the image and the alpha
494                 uint8_t *image_copy = mlt_pool_alloc( image_size );
495                 uint8_t *alpha_copy = mlt_pool_alloc( alpha_size );
496
497                 memcpy( image_copy, *buffer, image_size );
498
499                 // Copy or default the alpha
500                 if ( alpha != NULL )
501                         memcpy( alpha_copy, alpha, alpha_size );
502                 else
503                         memset( alpha_copy, 255, alpha_size );
504
505                 // Now update properties so we free the copy after
506                 mlt_properties_set_data( properties, "image", image_copy, image_size, mlt_pool_release, NULL );
507                 mlt_properties_set_data( properties, "alpha", alpha_copy, alpha_size, mlt_pool_release, NULL );
508
509                 // We're going to pass the copy on
510                 *buffer = image_copy;
511         }
512         else
513         {
514                 // TODO: Review all cases of invalid images
515                 *buffer = mlt_pool_alloc( 50 * 50 * 2 );
516                 mlt_properties_set_data( properties, "image", *buffer, image_size, mlt_pool_release, NULL );
517                 *width = 50;
518                 *height = 50;
519         }
520
521         pthread_mutex_unlock( &pango_mutex );
522
523         return 0;
524 }
525
526 static uint8_t *producer_get_alpha_mask( mlt_frame this )
527 {
528         // Obtain properties of frame
529         mlt_properties properties = MLT_FRAME_PROPERTIES( this );
530
531         // Return the alpha mask
532         return mlt_properties_get_data( properties, "alpha", NULL );
533 }
534
535 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
536 {
537         producer_pango this = producer->child;
538
539         // Generate a frame
540         *frame = mlt_frame_init( );
541
542         // Obtain properties of frame and producer
543         mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
544
545         // Set the producer on the frame properties
546         mlt_properties_set_data( properties, "producer_pango", this, 0, NULL, NULL );
547
548         // Update timecode on the frame we're creating
549         mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
550         mlt_properties_set_position( properties, "pango_position", mlt_producer_frame( producer ) );
551
552         // Refresh the pango image
553         pthread_mutex_lock( &pango_mutex );
554         refresh_image( *frame, 0, 0 );
555         pthread_mutex_unlock( &pango_mutex );
556
557         // Set producer-specific frame properties
558         mlt_properties_set_int( properties, "progressive", 1 );
559         mlt_properties_set_double( properties, "aspect_ratio", 1 );
560
561         // Set alpha call back
562         ( *frame )->get_alpha_mask = producer_get_alpha_mask;
563
564         // Stack the get image callback
565         mlt_frame_push_get_image( *frame, producer_get_image );
566
567         // Calculate the next timecode
568         mlt_producer_prepare_next( producer );
569
570         return 0;
571 }
572
573 static void producer_close( mlt_producer parent )
574 {
575         producer_pango this = parent->child;
576         mlt_pool_release( this->image );
577         mlt_pool_release( this->alpha );
578         free( this->fgcolor );
579         free( this->bgcolor );
580         free( this->markup );
581         free( this->text );
582         free( this->font );
583         parent->close = NULL;
584         mlt_producer_close( parent );
585         free( this );
586 }
587
588 static void pango_draw_background( GdkPixbuf *pixbuf, rgba_color bg )
589 {
590         int ww = gdk_pixbuf_get_width( pixbuf );
591         int hh = gdk_pixbuf_get_height( pixbuf );
592         uint8_t *p = gdk_pixbuf_get_pixels( pixbuf );
593         int i, j;
594
595         for ( j = 0; j < hh; j++ )
596         {
597                 for ( i = 0; i < ww; i++ )
598                 {
599                         *p++ = bg.r;
600                         *p++ = bg.g;
601                         *p++ = bg.b;
602                         *p++ = bg.a;
603                 }
604         }
605 }
606
607 static GdkPixbuf *pango_get_pixbuf( const char *markup, const char *text, const char *font, rgba_color fg, rgba_color bg, int pad, int align, int weight, int size )
608 {
609         PangoContext *context = pango_ft2_font_map_create_context( fontmap );
610         PangoLayout *layout = pango_layout_new( context );
611         int w, h, x;
612         int i, j;
613         GdkPixbuf *pixbuf = NULL;
614         FT_Bitmap bitmap;
615         uint8_t *src = NULL;
616         uint8_t* dest = NULL;
617         uint8_t *d, *s, a;
618         int stride;
619         PangoFontDescription *desc = pango_font_description_from_string( font );
620
621         pango_ft2_font_map_set_resolution( fontmap, 72, 72 );
622         pango_layout_set_width( layout, -1 ); // set wrapping constraints
623         pango_font_description_set_weight( desc, ( PangoWeight ) weight  );
624         if ( size != 0 )
625                 pango_font_description_set_size( desc, PANGO_SCALE * size );
626         pango_layout_set_font_description( layout, desc );
627 //      pango_layout_set_spacing( layout, space );
628         pango_layout_set_alignment( layout, ( PangoAlignment ) align  );
629         if ( markup != NULL && strcmp( markup, "" ) != 0 )
630                 pango_layout_set_markup( layout, markup, strlen( markup ) );
631         else if ( text != NULL && strcmp( text, "" ) != 0 )
632                 pango_layout_set_text( layout, text, strlen( text ) );
633         else
634                 pango_layout_set_text( layout, "  ", 2 );
635         pango_layout_get_pixel_size( layout, &w, &h );
636
637         pixbuf = gdk_pixbuf_new( GDK_COLORSPACE_RGB, TRUE /* has alpha */, 8, w + 2 * pad, h + 2 * pad );
638         pango_draw_background( pixbuf, bg );
639
640         stride = gdk_pixbuf_get_rowstride( pixbuf );
641
642         bitmap.width     = w;
643         bitmap.pitch     = 32 * ( ( w + 31 ) / 31 );
644         bitmap.rows      = h;
645         bitmap.buffer    = mlt_pool_alloc( h * bitmap.pitch );
646         bitmap.num_grays = 256;
647         bitmap.pixel_mode = ft_pixel_mode_grays;
648
649         memset( bitmap.buffer, 0, h * bitmap.pitch );
650
651         pango_ft2_render_layout( &bitmap, layout, 0, 0 );
652
653         src = bitmap.buffer;
654         x = ( gdk_pixbuf_get_width( pixbuf ) - w - 2 * pad ) * align / 2 + pad;
655         dest = gdk_pixbuf_get_pixels( pixbuf ) + 4 * x + pad * stride;
656         j = h;
657
658         while( j -- )
659         {
660                 d = dest;
661                 s = src;
662                 i = w;
663                 while( i -- )
664                 {
665                         a = *s ++;
666                         *d++ = ( a * fg.r + ( 255 - a ) * bg.r ) >> 8;
667                         *d++ = ( a * fg.g + ( 255 - a ) * bg.g ) >> 8;
668                         *d++ = ( a * fg.b + ( 255 - a ) * bg.b ) >> 8;
669                         *d++ = ( a * fg.a + ( 255 - a ) * bg.a ) >> 8;
670                 }
671                 dest += stride;
672                 src += bitmap.pitch;
673         }
674         mlt_pool_release( bitmap.buffer );
675         pango_font_description_free( desc );
676         g_object_unref( layout );
677         g_object_unref( context );
678
679         return pixbuf;
680 }