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