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