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