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