]> git.sesse.net Git - mlt/blob - src/modules/gtk2/producer_pango.c
Cleanup license declarations and remove dv1394d references.
[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 "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 = mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "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 = 0;
332
333         if ( pixbuf == NULL )
334         {
335                 // Check for file support
336                 int position = mlt_properties_get_position( properties, "pango_position" );
337                 mlt_properties contents = mlt_properties_get_data( producer_props, "contents", NULL );
338                 mlt_geometry key_frames = mlt_properties_get_data( producer_props, "key_frames", NULL );
339                 struct mlt_geometry_item_s item;
340                 if ( contents != NULL )
341                 {
342                         char temp[ 20 ];
343                         mlt_geometry_prev_key( key_frames, &item, position );
344                         sprintf( temp, "%d", item.frame );
345                         markup = mlt_properties_get( contents, temp );
346                 }
347         
348                 // See if any properties changed
349                 property_changed = ( align != this->align );
350                 property_changed = property_changed || ( this->fgcolor == NULL || ( fg && strcmp( fg, this->fgcolor ) ) );
351                 property_changed = property_changed || ( this->bgcolor == NULL || ( bg && strcmp( bg, this->bgcolor ) ) );
352                 property_changed = property_changed || ( pad != this->pad );
353                 property_changed = property_changed || ( markup && this->markup && strcmp( markup, this->markup ) );
354                 property_changed = property_changed || ( text && this->text && strcmp( text, this->text ) );
355                 property_changed = property_changed || ( font && this->font && strcmp( font, this->font ) );
356                 property_changed = property_changed || ( weight != this->weight );
357
358                 // Save the properties for next comparison
359                 this->align = align;
360                 this->pad = pad;
361                 set_string( &this->fgcolor, fg, "0xffffffff" );
362                 set_string( &this->bgcolor, bg, "0x00000000" );
363                 set_string( &this->markup, markup, NULL );
364                 set_string( &this->text, text, NULL );
365                 set_string( &this->font, font, "Sans 48" );
366                 this->weight = weight;
367         }
368
369         if ( pixbuf == NULL && property_changed )
370         {
371                 rgba_color fgcolor = parse_color( this->fgcolor );
372                 rgba_color bgcolor = parse_color( this->bgcolor );
373
374                 mlt_pool_release( this->image );
375                 mlt_pool_release( this->alpha );
376                 this->image = NULL;
377                 this->alpha = NULL;
378
379                 // Convert from specified encoding to UTF-8
380                 if ( encoding != NULL && !strncaseeq( encoding, "utf-8", 5 ) && !strncaseeq( encoding, "utf8", 4 ) )
381                 {
382                         if ( markup != NULL && iconv_utf8( producer_props, "markup", encoding ) != -1 )
383                         {
384                                 markup = mlt_properties_get( producer_props, "markup" );
385                                 set_string( &this->markup, markup, NULL );
386                         }
387                         if ( text != NULL && iconv_utf8( producer_props, "text", encoding ) != -1 )
388                         {
389                                 text = mlt_properties_get( producer_props, "text" );
390                                 set_string( &this->text, text, NULL );
391                         }
392                 }
393                 
394                 // Render the title
395                 pixbuf = pango_get_pixbuf( markup, text, font, fgcolor, bgcolor, pad, align, weight, size );
396
397                 if ( pixbuf != NULL )
398                 {
399                         // Register this pixbuf for destruction and reuse
400                         mlt_properties_set_data( producer_props, "pixbuf", pixbuf, 0, ( mlt_destructor )g_object_unref, NULL );
401                         g_object_ref( pixbuf );
402                         mlt_properties_set_data( MLT_FRAME_PROPERTIES( frame ), "pixbuf", pixbuf, 0, ( mlt_destructor )g_object_unref, NULL );
403
404                         mlt_properties_set_int( producer_props, "real_width", gdk_pixbuf_get_width( pixbuf ) );
405                         mlt_properties_set_int( producer_props, "real_height", gdk_pixbuf_get_height( pixbuf ) );
406
407                         // Store the width/height of the pixbuf temporarily
408                         this->width = gdk_pixbuf_get_width( pixbuf );
409                         this->height = gdk_pixbuf_get_height( pixbuf );
410                 }
411         }
412         else if ( pixbuf == NULL && ( width > 0 && ( this->image == NULL || width != this->width || height != this->height ) ) )
413         {
414                 mlt_pool_release( this->image );
415                 mlt_pool_release( this->alpha );
416                 this->image = NULL;
417                 this->alpha = NULL;
418
419                 pixbuf = mlt_properties_get_data( producer_props, "pixbuf", NULL );
420         }
421
422         // If we have a pixbuf and a valid width
423         if ( pixbuf && width > 0 )
424         {
425                 char *interps = mlt_properties_get( properties, "rescale.interp" );
426                 int interp = GDK_INTERP_BILINEAR;
427
428                 if ( strcmp( interps, "nearest" ) == 0 )
429                         interp = GDK_INTERP_NEAREST;
430                 else if ( strcmp( interps, "tiles" ) == 0 )
431                         interp = GDK_INTERP_TILES;
432                 else if ( strcmp( interps, "hyper" ) == 0 )
433                         interp = GDK_INTERP_HYPER;
434
435 //              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 );
436                         
437                 // Note - the original pixbuf is already safe and ready for destruction
438                 pixbuf = gdk_pixbuf_scale_simple( pixbuf, width, height, interp );
439
440                 // Store width and height
441                 this->width = width;
442                 this->height = height;
443
444                 // Allocate/define image
445                 this->image = mlt_pool_alloc( width * ( height + 1 ) * 2 );
446                 this->alpha = mlt_pool_alloc( this->width * this->height );
447
448                 // Convert the image
449                 mlt_convert_rgb24a_to_yuv422( gdk_pixbuf_get_pixels( pixbuf ),
450                                                                           this->width, this->height,
451                                                                           gdk_pixbuf_get_rowstride( pixbuf ),
452                                                                           this->image, this->alpha );
453
454                 // Finished with pixbuf now
455                 g_object_unref( pixbuf );
456         }
457
458         // Set width/height
459         mlt_properties_set_int( properties, "width", this->width );
460         mlt_properties_set_int( properties, "height", this->height );
461         mlt_properties_set_int( properties, "real_width", mlt_properties_get_int( producer_props, "real_width" ) );
462         mlt_properties_set_int( properties, "real_height", mlt_properties_get_int( producer_props, "real_height" ) );
463
464         // pass the image data without destructor
465         mlt_properties_set_data( properties, "image", this->image, this->width * ( this->height + 1 ) * 2, NULL, NULL );
466         mlt_properties_set_data( properties, "alpha", this->alpha, this->width * this->height, NULL, NULL );
467 }
468
469 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
470 {
471         // Obtain properties of frame
472         mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
473
474         // We need to know the size of the image to clone it
475         int image_size = 0;
476         int alpha_size = 0;
477
478         // Alpha channel
479         uint8_t *alpha = NULL;
480
481         *width = mlt_properties_get_int( properties, "rescale_width" );
482         *height = mlt_properties_get_int( properties, "rescale_height" );
483
484         // Refresh the image
485         pthread_mutex_lock( &pango_mutex );
486         refresh_image( frame, *width, *height );
487
488         // Get the image
489         *buffer = mlt_properties_get_data( properties, "image", &image_size );
490         alpha = mlt_properties_get_data( properties, "alpha", &alpha_size );
491
492         // Get width and height
493         *width = mlt_properties_get_int( properties, "width" );
494         *height = mlt_properties_get_int( properties, "height" );
495
496         // Always clone here to allow 'animated' text
497         if ( *buffer != NULL )
498         {
499                 // Clone the image and the alpha
500                 uint8_t *image_copy = mlt_pool_alloc( image_size );
501                 uint8_t *alpha_copy = mlt_pool_alloc( alpha_size );
502
503                 memcpy( image_copy, *buffer, image_size );
504
505                 // Copy or default the alpha
506                 if ( alpha != NULL )
507                         memcpy( alpha_copy, alpha, alpha_size );
508                 else
509                         memset( alpha_copy, 255, alpha_size );
510
511                 // Now update properties so we free the copy after
512                 mlt_properties_set_data( properties, "image", image_copy, image_size, mlt_pool_release, NULL );
513                 mlt_properties_set_data( properties, "alpha", alpha_copy, alpha_size, mlt_pool_release, NULL );
514
515                 // We're going to pass the copy on
516                 *buffer = image_copy;
517         }
518         else
519         {
520                 // TODO: Review all cases of invalid images
521                 *buffer = mlt_pool_alloc( 50 * 50 * 2 );
522                 mlt_properties_set_data( properties, "image", *buffer, image_size, mlt_pool_release, NULL );
523                 *width = 50;
524                 *height = 50;
525         }
526
527         pthread_mutex_unlock( &pango_mutex );
528
529         return 0;
530 }
531
532 static uint8_t *producer_get_alpha_mask( mlt_frame this )
533 {
534         // Obtain properties of frame
535         mlt_properties properties = MLT_FRAME_PROPERTIES( this );
536
537         // Return the alpha mask
538         return mlt_properties_get_data( properties, "alpha", NULL );
539 }
540
541 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
542 {
543         producer_pango this = producer->child;
544
545         // Generate a frame
546         *frame = mlt_frame_init( );
547
548         // Obtain properties of frame and producer
549         mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
550
551         // Set the producer on the frame properties
552         mlt_properties_set_data( properties, "producer_pango", this, 0, NULL, NULL );
553
554         // Update timecode on the frame we're creating
555         mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
556         mlt_properties_set_position( properties, "pango_position", mlt_producer_frame( producer ) );
557
558         // Refresh the pango image
559         pthread_mutex_lock( &pango_mutex );
560         refresh_image( *frame, 0, 0 );
561         pthread_mutex_unlock( &pango_mutex );
562
563         // Set producer-specific frame properties
564         mlt_properties_set_int( properties, "progressive", 1 );
565         mlt_properties_set_double( properties, "aspect_ratio", 1 );
566
567         // Set alpha call back
568         ( *frame )->get_alpha_mask = producer_get_alpha_mask;
569
570         // Stack the get image callback
571         mlt_frame_push_get_image( *frame, producer_get_image );
572
573         // Calculate the next timecode
574         mlt_producer_prepare_next( producer );
575
576         return 0;
577 }
578
579 static void producer_close( mlt_producer parent )
580 {
581         producer_pango this = parent->child;
582         mlt_pool_release( this->image );
583         mlt_pool_release( this->alpha );
584         free( this->fgcolor );
585         free( this->bgcolor );
586         free( this->markup );
587         free( this->text );
588         free( this->font );
589         parent->close = NULL;
590         mlt_producer_close( parent );
591         free( this );
592 }
593
594 static void pango_draw_background( GdkPixbuf *pixbuf, rgba_color bg )
595 {
596         int ww = gdk_pixbuf_get_width( pixbuf );
597         int hh = gdk_pixbuf_get_height( pixbuf );
598         uint8_t *p = gdk_pixbuf_get_pixels( pixbuf );
599         int i, j;
600
601         for ( j = 0; j < hh; j++ )
602         {
603                 for ( i = 0; i < ww; i++ )
604                 {
605                         *p++ = bg.r;
606                         *p++ = bg.g;
607                         *p++ = bg.b;
608                         *p++ = bg.a;
609                 }
610         }
611 }
612
613 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 )
614 {
615         PangoContext *context = pango_ft2_font_map_create_context( fontmap );
616         PangoLayout *layout = pango_layout_new( context );
617         int w, h, x;
618         int i, j;
619         GdkPixbuf *pixbuf = NULL;
620         FT_Bitmap bitmap;
621         uint8_t *src = NULL;
622         uint8_t* dest = NULL;
623         uint8_t *d, *s, a;
624         int stride;
625         PangoFontDescription *desc = pango_font_description_from_string( font );
626
627         pango_ft2_font_map_set_resolution( fontmap, 72, 72 );
628         pango_layout_set_width( layout, -1 ); // set wrapping constraints
629         pango_font_description_set_weight( desc, ( PangoWeight ) weight  );
630         if ( size != 0 )
631                 pango_font_description_set_size( desc, PANGO_SCALE * size );
632         pango_layout_set_font_description( layout, desc );
633 //      pango_layout_set_spacing( layout, space );
634         pango_layout_set_alignment( layout, ( PangoAlignment ) align  );
635         if ( markup != NULL && strcmp( markup, "" ) != 0 )
636         {
637                 pango_layout_set_markup( layout, markup, strlen( markup ) );
638         }
639         else if ( text != NULL && strcmp( text, "" ) != 0 )
640         {
641                 // Replace all ~'s with a line feed (silly convention, but handy)
642                 char *copy = strdup( text );
643                 while ( strchr( copy, '~' ) )
644                         ( *strchr( copy, '~' ) ) = '\n';
645                 pango_layout_set_text( layout, copy, strlen( copy ) );
646                 free( copy );
647         }
648         else
649         {
650                 // Pango doesn't like empty strings
651                 pango_layout_set_text( layout, "  ", 2 );
652         }
653         pango_layout_get_pixel_size( layout, &w, &h );
654
655         pixbuf = gdk_pixbuf_new( GDK_COLORSPACE_RGB, TRUE /* has alpha */, 8, w + 2 * pad, h + 2 * pad );
656         pango_draw_background( pixbuf, bg );
657
658         stride = gdk_pixbuf_get_rowstride( pixbuf );
659
660         bitmap.width     = w;
661         bitmap.pitch     = 32 * ( ( w + 31 ) / 31 );
662         bitmap.rows      = h;
663         bitmap.buffer    = mlt_pool_alloc( h * bitmap.pitch );
664         bitmap.num_grays = 256;
665         bitmap.pixel_mode = ft_pixel_mode_grays;
666
667         memset( bitmap.buffer, 0, h * bitmap.pitch );
668
669         pango_ft2_render_layout( &bitmap, layout, 0, 0 );
670
671         src = bitmap.buffer;
672         x = ( gdk_pixbuf_get_width( pixbuf ) - w - 2 * pad ) * align / 2 + pad;
673         dest = gdk_pixbuf_get_pixels( pixbuf ) + 4 * x + pad * stride;
674         j = h;
675
676         while( j -- )
677         {
678                 d = dest;
679                 s = src;
680                 i = w;
681                 while( i -- )
682                 {
683                         a = *s ++;
684                         *d++ = ( a * fg.r + ( 255 - a ) * bg.r ) >> 8;
685                         *d++ = ( a * fg.g + ( 255 - a ) * bg.g ) >> 8;
686                         *d++ = ( a * fg.b + ( 255 - a ) * bg.b ) >> 8;
687                         *d++ = ( a * fg.a + ( 255 - a ) * bg.a ) >> 8;
688                 }
689                 dest += stride;
690                 src += bitmap.pitch;
691         }
692         mlt_pool_release( bitmap.buffer );
693         pango_font_description_free( desc );
694         g_object_unref( layout );
695         g_object_unref( context );
696
697         return pixbuf;
698 }