]> git.sesse.net Git - mlt/blob - src/modules/gtk2/producer_pango.c
fix pango producer in XML with no resource filename
[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         char *olcolor;
53         int   align;
54         int   pad;
55         int   outline;
56         char *markup;
57         char *text;
58         char *font;
59         char *family;
60         int   size;
61         int   style;
62         int   weight;
63 };
64
65 // special color type used by internal pango routines
66 typedef struct
67 {
68         uint8_t r, g, b, a;
69 } rgba_color;
70
71 // Forward declarations
72 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int index );
73 static void producer_close( mlt_producer parent );
74 static void pango_draw_background( GdkPixbuf *pixbuf, rgba_color bg );
75 static GdkPixbuf *pango_get_pixbuf( const char *markup, const char *text, const char *font,
76                 rgba_color fg, rgba_color bg, rgba_color ol, int pad, int align, char* family, int style, int weight, int size, int outline );
77 static void fill_pixbuf( GdkPixbuf* pixbuf, FT_Bitmap* bitmap, int w, int h, int pad, int align, rgba_color fg, rgba_color bg );
78 static void fill_pixbuf_with_outline( GdkPixbuf* pixbuf, FT_Bitmap* bitmap, int w, int h, int pad, int align, rgba_color fg, rgba_color bg, rgba_color ol, int outline );
79
80 /** Return nonzero if the two strings are equal, ignoring case, up to
81     the first n characters.
82 */
83 int strncaseeq(const char *s1, const char *s2, size_t n)
84 {
85         for ( ; n > 0; n--)
86         {
87                 if (tolower(*s1++) != tolower(*s2++))
88                         return 0;
89         }
90         return 1;
91 }
92
93 /** Parse the alignment property.
94 */
95
96 static int parse_alignment( char* align )
97 {
98         int ret = pango_align_left;
99
100         if ( align == NULL );
101         else if ( isdigit( align[ 0 ] ) )
102                 ret = atoi( align );
103         else if ( align[ 0 ] == 'c' || align[ 0 ] == 'm' )
104                 ret = pango_align_center;
105         else if ( align[ 0 ] == 'r' )
106                 ret = pango_align_right;
107
108         return ret;
109 }
110
111 /** Parse the style property.
112 */
113
114 static int parse_style( char* style )
115 {
116         int ret = PANGO_STYLE_NORMAL;
117         if( !strncmp(style, "italic", 6) )
118                 ret = PANGO_STYLE_ITALIC;
119         return ret;
120 }
121
122 static PangoFT2FontMap *fontmap = NULL;
123
124 mlt_producer producer_pango_init( const char *filename )
125 {
126         producer_pango this = calloc( 1, sizeof( struct producer_pango_s ) );
127         if ( this != NULL && mlt_producer_init( &this->parent, this ) == 0 )
128         {
129                 mlt_producer producer = &this->parent;
130
131                 pthread_mutex_lock( &pango_mutex );
132                 if ( fontmap == NULL )
133                         fontmap = (PangoFT2FontMap*) pango_ft2_font_map_new();
134                 g_type_init();
135                 pthread_mutex_unlock( &pango_mutex );
136
137                 producer->get_frame = producer_get_frame;
138                 producer->close = ( mlt_destructor )producer_close;
139
140                 // Get the properties interface
141                 mlt_properties properties = MLT_PRODUCER_PROPERTIES( &this->parent );
142
143                 // Set the default properties
144                 mlt_properties_set( properties, "fgcolour", "0xffffffff" );
145                 mlt_properties_set( properties, "bgcolour", "0x00000000" );
146                 mlt_properties_set( properties, "olcolour", "0x00000000" );
147                 mlt_properties_set_int( properties, "align", pango_align_left );
148                 mlt_properties_set_int( properties, "pad", 0 );
149                 mlt_properties_set_int( properties, "outline", 0 );
150                 mlt_properties_set( properties, "text", "" );
151                 mlt_properties_set( properties, "font", NULL );
152                 mlt_properties_set( properties, "family", "Sans" );
153                 mlt_properties_set_int( properties, "size", 48 );
154                 mlt_properties_set( properties, "style", "normal" );
155                 mlt_properties_set( properties, "encoding", "UTF-8" );
156                 mlt_properties_set_int( properties, "weight", PANGO_WEIGHT_NORMAL );
157                 mlt_properties_set_int( properties, "seekable", 1 );
158
159                 if ( filename == NULL || ( filename && ( !strcmp( filename, "" )
160                         || strstr( filename, "<producer>" )
161                         // workaround for old kdenlive countdown generator
162                         || strstr( filename, "&lt;producer&gt;" ) ) ) )
163                 {
164                         mlt_properties_set( properties, "markup", "" );
165                 }
166                 else if ( filename[ 0 ] == '+' || strstr( filename, "/+" ) )
167                 {
168                         char *copy = strdup( filename + 1 );
169                         char *markup = copy;
170                         if ( strstr( markup, "/+" ) )
171                                 markup = strstr( markup, "/+" ) + 2;
172                         ( *strrchr( markup, '.' ) ) = '\0';
173                         while ( strchr( markup, '~' ) )
174                                 ( *strchr( markup, '~' ) ) = '\n';
175                         mlt_properties_set( properties, "resource", filename );
176                         mlt_properties_set( properties, "markup", markup );
177                         free( copy );
178                 }
179                 else if ( strstr( filename, ".mpl" ) ) 
180                 {
181                         int i = 0;
182                         mlt_properties contents = mlt_properties_load( filename );
183                         mlt_geometry key_frames = mlt_geometry_init( );
184                         struct mlt_geometry_item_s item;
185                         mlt_properties_set( properties, "resource", filename );
186                         mlt_properties_set_data( properties, "contents", contents, 0, ( mlt_destructor )mlt_properties_close, NULL );
187                         mlt_properties_set_data( properties, "key_frames", key_frames, 0, ( mlt_destructor )mlt_geometry_close, NULL );
188
189                         // Make sure we have at least one entry
190                         if ( mlt_properties_get( contents, "0" ) == NULL )
191                                 mlt_properties_set( contents, "0", "" );
192
193                         for ( i = 0; i < mlt_properties_count( contents ); i ++ )
194                         {
195                                 char *name = mlt_properties_get_name( contents, i );
196                                 char *value = mlt_properties_get_value( contents, i );
197                                 while ( value != NULL && strchr( value, '~' ) )
198                                         ( *strchr( value, '~' ) ) = '\n';
199                                 item.frame = atoi( name );
200                                 mlt_geometry_insert( key_frames, &item );
201                         }
202                         mlt_geometry_interpolate( key_frames );
203                 }
204                 else
205                 {
206                         FILE *f = fopen( filename, "r" );
207                         if ( f != NULL )
208                         {
209                                 char line[81];
210                                 char *markup = NULL;
211                                 size_t size = 0;
212                                 line[80] = '\0';
213                                 
214                                 while ( fgets( line, 80, f ) )
215                                 {
216                                         size += strlen( line ) + 1;
217                                         if ( markup )
218                                         {
219                                                 markup = realloc( markup, size );
220                                                 if ( markup )
221                                                         strcat( markup, line );
222                                         }
223                                         else
224                                         {
225                                                 markup = strdup( line );
226                                         }
227                                 }
228                                 fclose( f );
229
230                                 if ( markup && markup[ strlen( markup ) - 1 ] == '\n' )
231                                         markup[ strlen( markup ) - 1 ] = '\0';
232
233                                 mlt_properties_set( properties, "resource", filename );
234                                 if ( markup )
235                                         mlt_properties_set( properties, "markup", markup );
236                                 else
237                                         mlt_properties_set( properties, "markup", "" );
238                                 free( markup );
239                         }
240                         else
241                         {
242                                 producer->close = NULL;
243                                 mlt_producer_close( producer );
244                                 producer = NULL;
245                                 free( this );
246                         }
247                 }
248
249                 return producer;
250         }
251         free( this );
252         return NULL;
253 }
254
255 static void set_string( char **string, const char *value, const char *fallback )
256 {
257         if ( value != NULL )
258         {
259                 free( *string );
260                 *string = strdup( value );
261         }
262         else if ( *string == NULL && fallback != NULL )
263         {
264                 *string = strdup( fallback );
265         }
266         else if ( *string != NULL && fallback == NULL )
267         {
268                 free( *string );
269                 *string = NULL;
270         }
271 }
272
273 rgba_color parse_color( char *color, unsigned int color_int )
274 {
275         rgba_color result = { 0xff, 0xff, 0xff, 0xff };
276
277         if ( !strcmp( color, "red" ) )
278         {
279                 result.r = 0xff;
280                 result.g = 0x00;
281                 result.b = 0x00;
282         }
283         else if ( !strcmp( color, "green" ) )
284         {
285                 result.r = 0x00;
286                 result.g = 0xff;
287                 result.b = 0x00;
288         }
289         else if ( !strcmp( color, "blue" ) )
290         {
291                 result.r = 0x00;
292                 result.g = 0x00;
293                 result.b = 0xff;
294         }
295         else if ( strcmp( color, "white" ) )
296         {
297                 result.r = ( color_int >> 24 ) & 0xff;
298                 result.g = ( color_int >> 16 ) & 0xff;
299                 result.b = ( color_int >> 8 ) & 0xff;
300                 result.a = ( color_int ) & 0xff;
301         }
302
303         return result;
304 }
305
306 /** Convert a string property to UTF-8
307 */
308 static int iconv_utf8( mlt_properties properties, const char *prop_name, const char* encoding )
309 {
310         char *text = mlt_properties_get( properties, prop_name );
311         int result = -1;
312         
313         iconv_t cd = iconv_open( "UTF-8", encoding );
314         if ( text && ( cd != ( iconv_t )-1 ) )
315         {
316                 char *inbuf_p = text;
317                 size_t inbuf_n = strlen( text );
318                 size_t outbuf_n = inbuf_n * 6;
319                 char *outbuf = mlt_pool_alloc( outbuf_n );
320                 char *outbuf_p = outbuf;
321                 
322                 memset( outbuf, 0, outbuf_n );
323
324                 if ( text != NULL && strcmp( text, "" ) && iconv( cd, &inbuf_p, &inbuf_n, &outbuf_p, &outbuf_n ) != -1 )
325                         mlt_properties_set( properties, prop_name, outbuf );
326                 else
327                         mlt_properties_set( properties, prop_name, "" );
328
329                 mlt_pool_release( outbuf );
330                 result = 0;
331         }
332         iconv_close( cd );
333         return result;
334 }
335
336 static void refresh_image( mlt_frame frame, int width, int height )
337 {
338         // Pixbuf
339         GdkPixbuf *pixbuf = mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "pixbuf", NULL );
340
341         // Obtain properties of frame
342         mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
343
344         // Obtain the producer pango for this frame
345         producer_pango this = mlt_properties_get_data( properties, "producer_pango", NULL );
346
347         // Obtain the producer 
348         mlt_producer producer = &this->parent;
349
350         // Obtain the producer properties
351         mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
352
353         // Get producer properties
354         char *fg = mlt_properties_get( producer_props, "fgcolour" );
355         char *bg = mlt_properties_get( producer_props, "bgcolour" );
356         char *ol = mlt_properties_get( producer_props, "olcolour" );
357         int align = parse_alignment( mlt_properties_get( producer_props, "align" ) );
358         int pad = mlt_properties_get_int( producer_props, "pad" );
359         int outline = mlt_properties_get_int( producer_props, "outline" );
360         char *markup = mlt_properties_get( producer_props, "markup" );
361         char *text = mlt_properties_get( producer_props, "text" );
362         char *font = mlt_properties_get( producer_props, "font" );
363         char *family = mlt_properties_get( producer_props, "family" );
364         int style = parse_style( mlt_properties_get( producer_props, "style" ) );
365         char *encoding = mlt_properties_get( producer_props, "encoding" );
366         int weight = mlt_properties_get_int( producer_props, "weight" );
367         int size = mlt_properties_get_int( producer_props, "size" );
368         int property_changed = 0;
369
370         if ( pixbuf == NULL )
371         {
372                 // Check for file support
373                 mlt_properties contents = mlt_properties_get_data( producer_props, "contents", NULL );
374                 mlt_geometry key_frames = mlt_properties_get_data( producer_props, "key_frames", NULL );
375                 struct mlt_geometry_item_s item;
376                 if ( contents != NULL )
377                 {
378                         char temp[ 20 ];
379                         mlt_geometry_prev_key( key_frames, &item, mlt_frame_original_position( frame ) );
380                         sprintf( temp, "%d", item.frame );
381                         markup = mlt_properties_get( contents, temp );
382                 }
383         
384                 // See if any properties changed
385                 property_changed = ( align != this->align );
386                 property_changed = property_changed || ( this->fgcolor == NULL || ( fg && strcmp( fg, this->fgcolor ) ) );
387                 property_changed = property_changed || ( this->bgcolor == NULL || ( bg && strcmp( bg, this->bgcolor ) ) );
388                 property_changed = property_changed || ( this->olcolor == NULL || ( ol && strcmp( ol, this->olcolor ) ) );
389                 property_changed = property_changed || ( pad != this->pad );
390                 property_changed = property_changed || ( outline != this->outline );
391                 property_changed = property_changed || ( markup && this->markup && strcmp( markup, this->markup ) );
392                 property_changed = property_changed || ( text && this->text && strcmp( text, this->text ) );
393                 property_changed = property_changed || ( font && this->font && strcmp( font, this->font ) );
394                 property_changed = property_changed || ( family && this->family && strcmp( family, this->family ) );
395                 property_changed = property_changed || ( weight != this->weight );
396                 property_changed = property_changed || ( style != this->style );
397                 property_changed = property_changed || ( size != this->size );
398
399                 // Save the properties for next comparison
400                 this->align = align;
401                 this->pad = pad;
402                 this->outline = outline;
403                 set_string( &this->fgcolor, fg, "0xffffffff" );
404                 set_string( &this->bgcolor, bg, "0x00000000" );
405                 set_string( &this->olcolor, ol, "0x00000000" );
406                 set_string( &this->markup, markup, NULL );
407                 set_string( &this->text, text, NULL );
408                 set_string( &this->font, font, NULL );
409                 set_string( &this->family, family, "Sans" );
410                 this->weight = weight;
411                 this->style = style;
412                 this->size = size;
413         }
414
415         if ( pixbuf == NULL && property_changed )
416         {
417                 rgba_color fgcolor = parse_color( this->fgcolor, mlt_properties_get_int( producer_props, "fgcolour" ) );
418                 rgba_color bgcolor = parse_color( this->bgcolor, mlt_properties_get_int( producer_props, "bgcolour" ) );
419                 rgba_color olcolor = parse_color( this->olcolor, mlt_properties_get_int( producer_props, "olcolour" ) );
420
421                 if ( this->pixbuf )
422                         g_object_unref( this->pixbuf );
423                 this->pixbuf = NULL;
424
425                 // Convert from specified encoding to UTF-8
426                 if ( encoding != NULL && !strncaseeq( encoding, "utf-8", 5 ) && !strncaseeq( encoding, "utf8", 4 ) )
427                 {
428                         if ( markup != NULL && iconv_utf8( producer_props, "markup", encoding ) != -1 )
429                         {
430                                 markup = mlt_properties_get( producer_props, "markup" );
431                                 set_string( &this->markup, markup, NULL );
432                         }
433                         if ( text != NULL && iconv_utf8( producer_props, "text", encoding ) != -1 )
434                         {
435                                 text = mlt_properties_get( producer_props, "text" );
436                                 set_string( &this->text, text, NULL );
437                         }
438                 }
439                 
440                 // Render the title
441                 pixbuf = pango_get_pixbuf( markup, text, font, fgcolor, bgcolor, olcolor, pad, align, family, style, weight, size, outline );
442
443                 if ( pixbuf != NULL )
444                 {
445                         // Register this pixbuf for destruction and reuse
446                         mlt_properties_set_data( producer_props, "pixbuf", pixbuf, 0, ( mlt_destructor )g_object_unref, NULL );
447                         g_object_ref( pixbuf );
448                         mlt_properties_set_data( MLT_FRAME_PROPERTIES( frame ), "pixbuf", pixbuf, 0, ( mlt_destructor )g_object_unref, NULL );
449
450                         mlt_properties_set_int( producer_props, "meta.media.width", gdk_pixbuf_get_width( pixbuf ) );
451                         mlt_properties_set_int( producer_props, "meta.media.height", gdk_pixbuf_get_height( pixbuf ) );
452
453                         // Store the width/height of the pixbuf temporarily
454                         this->width = gdk_pixbuf_get_width( pixbuf );
455                         this->height = gdk_pixbuf_get_height( pixbuf );
456                 }
457         }
458         else if ( pixbuf == NULL && width > 0 && ( this->pixbuf == NULL || width != this->width || height != this->height ) )
459         {
460                 if ( this->pixbuf )
461                         g_object_unref( this->pixbuf );
462                 this->pixbuf = NULL;
463                 pixbuf = mlt_properties_get_data( producer_props, "pixbuf", NULL );
464         }
465
466         // If we have a pixbuf and a valid width
467         if ( pixbuf && width > 0 )
468         {
469                 char *interps = mlt_properties_get( properties, "rescale.interp" );
470                 int interp = GDK_INTERP_BILINEAR;
471
472                 if ( strcmp( interps, "nearest" ) == 0 )
473                         interp = GDK_INTERP_NEAREST;
474                 else if ( strcmp( interps, "tiles" ) == 0 )
475                         interp = GDK_INTERP_TILES;
476                 else if ( strcmp( interps, "hyper" ) == 0 || strcmp( interps, "bicubic" ) == 0 )
477                         interp = GDK_INTERP_HYPER;
478
479 // fprintf(stderr,"%s: scaling from %dx%d to %dx%d\n", __FILE__, this->width, this->height, width, height);
480
481                 // Note - the original pixbuf is already safe and ready for destruction
482                 this->pixbuf = gdk_pixbuf_scale_simple( pixbuf, width, height, interp );
483
484                 // Store width and height
485                 this->width = width;
486                 this->height = height;
487         }
488
489         // Set width/height
490         mlt_properties_set_int( properties, "width", this->width );
491         mlt_properties_set_int( properties, "height", this->height );
492 }
493
494 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
495 {
496         int error = 0;
497         producer_pango this = ( producer_pango ) mlt_frame_pop_service( frame );
498
499         // Obtain properties of frame
500         mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
501
502         *width = mlt_properties_get_int( properties, "rescale_width" );
503         *height = mlt_properties_get_int( properties, "rescale_height" );
504
505         mlt_service_lock( MLT_PRODUCER_SERVICE( &this->parent ) );
506
507         // Refresh the image
508         pthread_mutex_lock( &pango_mutex );
509         refresh_image( frame, *width, *height );
510
511         // Get width and height
512         *width = this->width;
513         *height = this->height;
514         *format = mlt_image_rgb24a;
515
516         // Always clone here to allow 'animated' text
517         if ( this->pixbuf )
518         {
519                 // Clone the image
520                 int image_size = this->width * this->height * 4;
521                 *buffer = mlt_pool_alloc( image_size );
522                 memcpy( *buffer, gdk_pixbuf_get_pixels( this->pixbuf ), image_size );
523
524                 // Now update properties so we free the copy after
525                 mlt_frame_set_image( frame, *buffer, image_size, mlt_pool_release );
526         }
527         else
528         {
529                 error = 1;
530         }
531
532         pthread_mutex_unlock( &pango_mutex );
533         mlt_service_unlock( MLT_PRODUCER_SERVICE( &this->parent ) );
534
535         return error;
536 }
537
538 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
539 {
540         producer_pango this = producer->child;
541
542         // Fetch the producers properties
543         mlt_properties producer_properties = MLT_PRODUCER_PROPERTIES( producer );
544
545         // Generate a frame
546         *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
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
557         // Refresh the pango image
558         pthread_mutex_lock( &pango_mutex );
559         refresh_image( *frame, 0, 0 );
560         pthread_mutex_unlock( &pango_mutex );
561
562         // Set producer-specific frame properties
563         mlt_properties_set_int( properties, "progressive", 1 );
564         double force_ratio = mlt_properties_get_double( producer_properties, "force_aspect_ratio" );
565         if ( force_ratio > 0.0 )
566                 mlt_properties_set_double( properties, "aspect_ratio", force_ratio );
567         else
568                 mlt_properties_set_double( properties, "aspect_ratio", 1.0);
569
570         // Stack the get image callback
571         mlt_frame_push_service( *frame, this );
572         mlt_frame_push_get_image( *frame, producer_get_image );
573
574         // Calculate the next timecode
575         mlt_producer_prepare_next( producer );
576
577         return 0;
578 }
579
580 static void producer_close( mlt_producer parent )
581 {
582         producer_pango this = parent->child;
583         if ( this->pixbuf )
584                 g_object_unref( this->pixbuf );
585         free( this->fgcolor );
586         free( this->bgcolor );
587         free( this->olcolor );
588         free( this->markup );
589         free( this->text );
590         free( this->font );
591         free( this->family );
592         parent->close = NULL;
593         mlt_producer_close( parent );
594         free( this );
595 }
596
597 static void pango_draw_background( GdkPixbuf *pixbuf, rgba_color bg )
598 {
599         int ww = gdk_pixbuf_get_width( pixbuf );
600         int hh = gdk_pixbuf_get_height( pixbuf );
601         uint8_t *p = gdk_pixbuf_get_pixels( pixbuf );
602         int i, j;
603
604         for ( j = 0; j < hh; j++ )
605         {
606                 for ( i = 0; i < ww; i++ )
607                 {
608                         *p++ = bg.r;
609                         *p++ = bg.g;
610                         *p++ = bg.b;
611                         *p++ = bg.a;
612                 }
613         }
614 }
615
616 static GdkPixbuf *pango_get_pixbuf( const char *markup, const char *text, const char *font, rgba_color fg, rgba_color bg, rgba_color ol, int pad, int align, char* family, int style, int weight, int size, int outline )
617 {
618         PangoContext *context = pango_ft2_font_map_create_context( fontmap );
619         PangoLayout *layout = pango_layout_new( context );
620         int w, h;
621         GdkPixbuf *pixbuf = NULL;
622         FT_Bitmap bitmap;
623         PangoFontDescription *desc = NULL;
624
625         if( font )
626         {
627                 // Support for deprecated "font" property.
628                 desc = pango_font_description_from_string( font );
629                 pango_ft2_font_map_set_resolution( fontmap, 72, 72 );
630         }
631         else
632         {
633                 desc = pango_font_description_from_string( family );
634                 pango_font_description_set_size( desc, PANGO_SCALE * size );
635                 pango_font_description_set_style( desc, (PangoStyle) style );
636         }
637
638         pango_font_description_set_weight( desc, ( PangoWeight ) weight  );
639
640         pango_layout_set_width( layout, -1 ); // set wrapping constraints
641         pango_layout_set_font_description( layout, desc );
642         pango_layout_set_alignment( layout, ( PangoAlignment ) align  );
643         if ( markup != NULL && strcmp( markup, "" ) != 0 )
644         {
645                 pango_layout_set_markup( layout, markup, strlen( markup ) );
646         }
647         else if ( text != NULL && strcmp( text, "" ) != 0 )
648         {
649                 // Replace all ~'s with a line feed (silly convention, but handy)
650                 char *copy = strdup( text );
651                 while ( strchr( copy, '~' ) )
652                         ( *strchr( copy, '~' ) ) = '\n';
653                 pango_layout_set_text( layout, copy, strlen( copy ) );
654                 free( copy );
655         }
656         else
657         {
658                 // Pango doesn't like empty strings
659                 pango_layout_set_text( layout, "  ", 2 );
660         }
661         pango_layout_get_pixel_size( layout, &w, &h );
662
663         if ( pad == 0 )
664                 pad = 1;
665
666         pixbuf = gdk_pixbuf_new( GDK_COLORSPACE_RGB, TRUE /* has alpha */, 8, w + 2 * pad, h + 2 * pad );
667         pango_draw_background( pixbuf, bg );
668
669         bitmap.width     = w;
670         bitmap.pitch     = 32 * ( ( w + 31 ) / 31 );
671         bitmap.rows      = h;
672         bitmap.buffer    = mlt_pool_alloc( h * bitmap.pitch );
673         bitmap.num_grays = 256;
674         bitmap.pixel_mode = ft_pixel_mode_grays;
675
676         memset( bitmap.buffer, 0, h * bitmap.pitch );
677
678         pango_ft2_render_layout( &bitmap, layout, 0, 0 );
679
680         if ( outline )
681         {
682                 fill_pixbuf_with_outline( pixbuf, &bitmap, w, h, pad, align, fg, bg, ol, outline );
683         }
684         else
685         {
686                 fill_pixbuf( pixbuf, &bitmap, w, h, pad, align, fg, bg );
687         }
688
689         mlt_pool_release( bitmap.buffer );
690         pango_font_description_free( desc );
691         g_object_unref( layout );
692         g_object_unref( context );
693
694         return pixbuf;
695 }
696
697 static void fill_pixbuf( GdkPixbuf* pixbuf, FT_Bitmap* bitmap, int w, int h, int pad, int align, rgba_color fg, rgba_color bg )
698 {
699         int stride = gdk_pixbuf_get_rowstride( pixbuf );
700         uint8_t* src = bitmap->buffer;
701         int x = ( gdk_pixbuf_get_width( pixbuf ) - w - 2 * pad ) * align / 2 + pad;
702         uint8_t* dest = gdk_pixbuf_get_pixels( pixbuf ) + 4 * x + pad * stride;
703         int j = h;
704         int i = 0;
705         uint8_t *d, *s, a;
706
707         while( j -- )
708         {
709                 d = dest;
710                 s = src;
711                 i = w;
712                 while( i -- )
713                 {
714                         a = *s ++;
715                         *d++ = ( a * fg.r + ( 255 - a ) * bg.r ) >> 8;
716                         *d++ = ( a * fg.g + ( 255 - a ) * bg.g ) >> 8;
717                         *d++ = ( a * fg.b + ( 255 - a ) * bg.b ) >> 8;
718                         *d++ = ( a * fg.a + ( 255 - a ) * bg.a ) >> 8;
719                 }
720                 dest += stride;
721                 src += bitmap->pitch;
722         }
723 }
724
725 static void fill_pixbuf_with_outline( GdkPixbuf* pixbuf, FT_Bitmap* bitmap, int w, int h, int pad, int align, rgba_color fg, rgba_color bg, rgba_color ol, int outline )
726 {
727         int stride = gdk_pixbuf_get_rowstride( pixbuf );
728         int x = ( gdk_pixbuf_get_width( pixbuf ) - w - 2 * pad ) * align / 2 + pad;
729         uint8_t* dest = gdk_pixbuf_get_pixels( pixbuf ) + 4 * x + pad * stride;
730         int j ,i;
731         uint8_t *d = NULL;
732         float a_ol = 0;
733         float a_fg = 0;
734
735         for ( j = 0; j < h; j++ )
736         {
737                 d = dest;
738                 for ( i = 0; i < w; i++ )
739                 {
740 #define geta(x, y) (float) bitmap->buffer[ (y) * bitmap->pitch + (x) ] / 255.0
741
742                         a_ol = geta(i, j);
743                         // One pixel fake circle
744                         if ( i > 0 )
745                                 a_ol = MAX( a_ol, geta(i - 1, j) );
746                         if ( i < w - 1 )
747                                 a_ol = MAX( a_ol, geta(i + 1, j) );
748                         if ( j > 0 )
749                                 a_ol = MAX( a_ol, geta(i, j - 1) );
750                         if ( j < h - 1 )
751                                 a_ol = MAX( a_ol, geta(i, j + 1) );
752                         if ( outline >= 2 ) {
753                                 // Two pixels fake circle
754                                 if ( i > 1 ) {
755                                         a_ol = MAX( a_ol, geta(i - 2, j) );
756                                         if ( j > 0 )
757                                                 a_ol = MAX( a_ol, geta(i - 2, j - 1) );
758                                         if ( j < h - 1 )
759                                                 a_ol = MAX( a_ol, geta(i - 2, j + 1) );
760                                 }
761                                 if ( i > 0 ) {
762                                         if ( j > 0 )
763                                                 a_ol = MAX( a_ol, geta(i - 1, j - 1) );
764                                         if ( j > 1 )
765                                                 a_ol = MAX( a_ol, geta(i - 1, j - 2) );
766                                         if ( j < h - 1 )
767                                                 a_ol = MAX( a_ol, geta(i - 1, j + 1) );
768                                         if ( j < h - 2 )
769                                                 a_ol = MAX( a_ol, geta(i - 1, j + 2) );
770                                 }
771                                 if ( j > 1 )
772                                         a_ol = MAX( a_ol, geta(i, j - 2) );
773                                 if ( j < h - 2 )
774                                         a_ol = MAX( a_ol, geta(i, j + 2) );
775                                 if ( i < w - 1 ) {
776                                         if ( j > 0 )
777                                                 a_ol = MAX( a_ol, geta(i + 1, j - 1) );
778                                         if ( j > 1 )
779                                                 a_ol = MAX( a_ol, geta(i + 1, j - 2) );
780                                         if ( j < h - 1 )
781                                                 a_ol = MAX( a_ol, geta(i + 1, j + 1) );
782                                         if ( j < h - 2 )
783                                                 a_ol = MAX( a_ol, geta(i + 1, j + 2) );
784                                 }
785                                 if ( i < w - 2 ) {
786                                         a_ol = MAX( a_ol, geta(i + 2, j) );
787                                         if ( j > 0 )
788                                                 a_ol = MAX( a_ol, geta(i + 2, j - 1) );
789                                         if ( j < h - 1 )
790                                                 a_ol = MAX( a_ol, geta(i + 2, j + 1) );
791                                 }
792                         }
793                         if ( outline >= 3 ) {
794                                 // Three pixels fake circle
795                                 if ( i > 2 ) {
796                                         a_ol = MAX( a_ol, geta(i - 3, j) );
797                                         if ( j > 0 )
798                                                 a_ol = MAX( a_ol, geta(i - 3, j - 1) );
799                                         if ( j < h - 1 )
800                                                 a_ol = MAX( a_ol, geta(i - 3, j + 1) );
801                                 }
802                                 if ( i > 1 ) {
803                                         if ( j > 1 )
804                                                 a_ol = MAX( a_ol, geta(i - 2, j - 2) );
805                                         if ( j < h - 2 )
806                                                 a_ol = MAX( a_ol, geta(i - 2, j + 2) );
807                                 }
808                                 if ( i > 0 ) {
809                                         if ( j > 2 )
810                                                 a_ol = MAX( a_ol, geta(i - 1, j - 3) );
811                                         if ( j < h - 3 )
812                                                 a_ol = MAX( a_ol, geta(i - 1, j + 3) );
813                                 }
814                                 if ( j > 2 )
815                                         a_ol = MAX( a_ol, geta(i, j - 3) );
816                                 if ( j < h - 3 )
817                                         a_ol = MAX( a_ol, geta(i, j + 3) );
818                                 if ( i < w - 1 ) {
819                                         if ( j > 2 )
820                                                 a_ol = MAX( a_ol, geta(i + 1, j - 3) );
821                                         if ( j < h - 3 )
822                                                 a_ol = MAX( a_ol, geta(i + 1, j + 3) );
823                                 }
824                                 if ( i < w - 2 ) {
825                                         if ( j > 1 )
826                                                 a_ol = MAX( a_ol, geta(i + 2, j - 2) );
827                                         if ( j < h - 2 )
828                                                 a_ol = MAX( a_ol, geta(i + 2, j + 2) );
829                                 }
830                                 if ( i < w - 3 ) {
831                                         a_ol = MAX( a_ol, geta(i + 3, j) );
832                                         if ( j > 0 )
833                                                 a_ol = MAX( a_ol, geta(i + 3, j - 1) );
834                                         if ( j < h - 1 )
835                                                 a_ol = MAX( a_ol, geta(i + 3, j + 1) );
836                                 }
837                         }
838
839                         a_fg = ( float ) bitmap->buffer[ j * bitmap->pitch + i ] / 255.0;
840
841                         *d++ = ( int ) ( a_fg * fg.r + ( 1 - a_fg ) * ( a_ol * ol.r + ( 1 - a_ol ) * bg.r ) );
842                         *d++ = ( int ) ( a_fg * fg.g + ( 1 - a_fg ) * ( a_ol * ol.g + ( 1 - a_ol ) * bg.g ) );
843                         *d++ = ( int ) ( a_fg * fg.b + ( 1 - a_fg ) * ( a_ol * ol.b + ( 1 - a_ol ) * bg.b ) );
844                         *d++ = ( int ) ( a_fg * fg.a + ( 1 - a_fg ) * ( a_ol * ol.a + ( 1 - a_ol ) * bg.a ) );
845                 }
846                 dest += stride;
847         }
848 }