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