+ Mutex protection (temporary work around of SMP systems)
[melted] / 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 program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program 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
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21 #include "producer_pango.h"
22 #include <framework/mlt_frame.h>
23 #include <framework/mlt_geometry.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <gdk-pixbuf/gdk-pixbuf.h>
27 #include <pango/pangoft2.h>
28 #include <freetype/freetype.h>
29 #include <iconv.h>
30 #include <pthread.h>
31
32 static pthread_mutex_t pango_mutex = PTHREAD_MUTEX_INITIALIZER;
33
34 struct producer_pango_s
35 {
36 struct mlt_producer_s parent;
37 int width;
38 int height;
39 uint8_t *image;
40 uint8_t *alpha;
41 char *fgcolor;
42 char *bgcolor;
43 int align;
44 int pad;
45 char *markup;
46 char *text;
47 char *font;
48 int weight;
49 };
50
51 // special color type used by internal pango routines
52 typedef struct
53 {
54 uint8_t r, g, b, a;
55 } rgba_color;
56
57 // Forward declarations
58 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int index );
59 static void producer_close( mlt_producer parent );
60 static void pango_draw_background( GdkPixbuf *pixbuf, rgba_color bg );
61 static GdkPixbuf *pango_get_pixbuf( const char *markup, const char *text, const char *font,
62 rgba_color fg, rgba_color bg, int pad, int align, int weight, int size );
63
64 /** Return nonzero if the two strings are equal, ignoring case, up to
65 the first n characters.
66 */
67 int strncaseeq(const char *s1, const char *s2, size_t n)
68 {
69 for ( ; n > 0; n--)
70 {
71 if (tolower(*s1++) != tolower(*s2++))
72 return 0;
73 }
74 return 1;
75 }
76
77 /** Parse the alignment property.
78 */
79
80 static int alignment_parse( char* align )
81 {
82 int ret = pango_align_left;
83
84 if ( align == NULL );
85 else if ( isdigit( align[ 0 ] ) )
86 ret = atoi( align );
87 else if ( align[ 0 ] == 'c' || align[ 0 ] == 'm' )
88 ret = pango_align_center;
89 else if ( align[ 0 ] == 'r' )
90 ret = pango_align_right;
91
92 return ret;
93 }
94
95 static PangoFT2FontMap *fontmap = NULL;
96
97 mlt_producer producer_pango_init( const char *filename )
98 {
99 producer_pango this = calloc( sizeof( struct producer_pango_s ), 1 );
100 if ( this != NULL && mlt_producer_init( &this->parent, this ) == 0 )
101 {
102 mlt_producer producer = &this->parent;
103
104 pthread_mutex_lock( &pango_mutex );
105 if ( fontmap == NULL )
106 fontmap = (PangoFT2FontMap*) pango_ft2_font_map_new();
107 g_type_init();
108 pthread_mutex_unlock( &pango_mutex );
109
110 producer->get_frame = producer_get_frame;
111 producer->close = ( mlt_destructor )producer_close;
112
113 // Get the properties interface
114 mlt_properties properties = MLT_PRODUCER_PROPERTIES( &this->parent );
115
116 // Set the default properties
117 mlt_properties_set( properties, "fgcolour", "0xffffffff" );
118 mlt_properties_set( properties, "bgcolour", "0x00000000" );
119 mlt_properties_set_int( properties, "align", pango_align_left );
120 mlt_properties_set_int( properties, "pad", 0 );
121 mlt_properties_set( properties, "text", "" );
122 mlt_properties_set( properties, "font", "Sans 48" );
123 mlt_properties_set( properties, "encoding", "UTF-8" );
124 mlt_properties_set_int( properties, "weight", PANGO_WEIGHT_NORMAL );
125
126 if ( filename == NULL )
127 {
128 mlt_properties_set( properties, "markup", "" );
129 }
130 else if ( filename[ 0 ] == '+' || strstr( filename, "/+" ) )
131 {
132 char *copy = strdup( filename + 1 );
133 char *markup = copy;
134 if ( strstr( markup, "/+" ) )
135 markup = strstr( markup, "/+" ) + 2;
136 ( *strrchr( markup, '.' ) ) = '\0';
137 while ( strchr( markup, '~' ) )
138 ( *strchr( markup, '~' ) ) = '\n';
139 mlt_properties_set( properties, "resource", ( char * )filename );
140 mlt_properties_set( properties, "markup", markup );
141 free( copy );
142 }
143 else if ( strstr( filename, ".mpl" ) )
144 {
145 int i = 0;
146 mlt_properties contents = mlt_properties_load( filename );
147 mlt_geometry key_frames = mlt_geometry_init( );
148 struct mlt_geometry_item_s item;
149 mlt_properties_set_data( properties, "contents", contents, 0, ( mlt_destructor )mlt_properties_close, NULL );
150 mlt_properties_set_data( properties, "key_frames", key_frames, 0, ( mlt_destructor )mlt_geometry_close, NULL );
151
152 // Make sure we have at least one entry
153 if ( mlt_properties_get( contents, "0" ) == NULL )
154 mlt_properties_set( contents, "0", "" );
155
156 for ( i = 0; i < mlt_properties_count( contents ); i ++ )
157 {
158 char *name = mlt_properties_get_name( contents, i );
159 char *value = mlt_properties_get_value( contents, i );
160 while ( value != NULL && strchr( value, '~' ) )
161 ( *strchr( value, '~' ) ) = '\n';
162 item.frame = atoi( name );
163 mlt_geometry_insert( key_frames, &item );
164 }
165 }
166 else
167 {
168 FILE *f = fopen( filename, "r" );
169 if ( f != NULL )
170 {
171 char line[81];
172 char *markup = NULL;
173 size_t size = 0;
174 line[80] = '\0';
175
176 while ( fgets( line, 80, f ) )
177 {
178 size += strlen( line ) + 1;
179 if ( markup )
180 {
181 markup = realloc( markup, size );
182 strcat( markup, line );
183 }
184 else
185 {
186 markup = strdup( line );
187 }
188 }
189 fclose( f );
190
191 if ( markup[ strlen( markup ) - 1 ] == '\n' )
192 markup[ strlen( markup ) - 1 ] = '\0';
193
194 mlt_properties_set( properties, "resource", ( char * ) filename );
195 mlt_properties_set( properties, "markup", ( char * ) ( markup == NULL ? "" : markup ) );
196 free( markup );
197 }
198 else
199 {
200 mlt_properties_set( properties, "markup", "" );
201 }
202 }
203
204 return producer;
205 }
206 free( this );
207 return NULL;
208 }
209
210 static void set_string( char **string, char *value, char *fallback )
211 {
212 if ( value != NULL )
213 {
214 free( *string );
215 *string = strdup( value );
216 }
217 else if ( *string == NULL && fallback != NULL )
218 {
219 *string = strdup( fallback );
220 }
221 else if ( *string != NULL && fallback == NULL )
222 {
223 free( *string );
224 *string = NULL;
225 }
226 }
227
228 rgba_color parse_color( char *color )
229 {
230 rgba_color result = { 0xff, 0xff, 0xff, 0xff };
231
232 if ( !strncmp( color, "0x", 2 ) )
233 {
234 unsigned int temp = 0;
235 sscanf( color + 2, "%x", &temp );
236 result.r = ( temp >> 24 ) & 0xff;
237 result.g = ( temp >> 16 ) & 0xff;
238 result.b = ( temp >> 8 ) & 0xff;
239 result.a = ( temp ) & 0xff;
240 }
241 else if ( !strcmp( color, "red" ) )
242 {
243 result.r = 0xff;
244 result.g = 0x00;
245 result.b = 0x00;
246 }
247 else if ( !strcmp( color, "green" ) )
248 {
249 result.r = 0x00;
250 result.g = 0xff;
251 result.b = 0x00;
252 }
253 else if ( !strcmp( color, "blue" ) )
254 {
255 result.r = 0x00;
256 result.g = 0x00;
257 result.b = 0xff;
258 }
259 else
260 {
261 unsigned int temp = 0;
262 sscanf( color, "%d", &temp );
263 result.r = ( temp >> 24 ) & 0xff;
264 result.g = ( temp >> 16 ) & 0xff;
265 result.b = ( temp >> 8 ) & 0xff;
266 result.a = ( temp ) & 0xff;
267 }
268
269 return result;
270 }
271
272 /** Convert a string property to UTF-8
273 */
274 static int iconv_utf8( mlt_properties properties, char *prop_name, const char* encoding )
275 {
276 char *text = mlt_properties_get( properties, prop_name );
277 int result = -1;
278
279 iconv_t cd = iconv_open( "UTF-8", encoding );
280 if ( cd != ( iconv_t )-1 )
281 {
282 char *inbuf_p = text;
283 size_t inbuf_n = strlen( text );
284 size_t outbuf_n = inbuf_n * 6;
285 char *outbuf = mlt_pool_alloc( outbuf_n );
286 char *outbuf_p = outbuf;
287
288 memset( outbuf, 0, outbuf_n );
289
290 if ( text != NULL && strcmp( text, "" ) && iconv( cd, &inbuf_p, &inbuf_n, &outbuf_p, &outbuf_n ) != -1 )
291 mlt_properties_set( properties, prop_name, outbuf );
292 else
293 mlt_properties_set( properties, prop_name, "" );
294
295 mlt_pool_release( outbuf );
296 iconv_close( cd );
297 result = 0;
298 }
299 return result;
300 }
301
302 static void refresh_image( mlt_frame frame, int width, int height )
303 {
304 // Pixbuf
305 GdkPixbuf *pixbuf = NULL;
306
307 // Obtain properties of frame
308 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
309
310 // Obtain the producer pango for this frame
311 producer_pango this = mlt_properties_get_data( properties, "producer_pango", NULL );
312
313 // Obtain the producer
314 mlt_producer producer = &this->parent;
315
316 // Obtain the producer properties
317 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
318
319 // Get producer properties
320 char *fg = mlt_properties_get( producer_props, "fgcolour" );
321 char *bg = mlt_properties_get( producer_props, "bgcolour" );
322 int align = alignment_parse( mlt_properties_get( producer_props, "align" ) );
323 int pad = mlt_properties_get_int( producer_props, "pad" );
324 char *markup = mlt_properties_get( producer_props, "markup" );
325 char *text = mlt_properties_get( producer_props, "text" );
326 char *font = mlt_properties_get( producer_props, "font" );
327 char *encoding = mlt_properties_get( producer_props, "encoding" );
328 int weight = mlt_properties_get_int( producer_props, "weight" );
329 int size = mlt_properties_get_int( producer_props, "size" );
330 int property_changed = ( align != this->align );
331
332 // Check for file support
333 int position = mlt_properties_get_position( properties, "pango_position" );
334 mlt_properties contents = mlt_properties_get_data( producer_props, "contents", NULL );
335 mlt_geometry key_frames = mlt_properties_get_data( producer_props, "key_frames", NULL );
336 struct mlt_geometry_item_s item;
337 if ( contents != NULL )
338 {
339 char temp[ 20 ];
340 mlt_geometry_prev_key( key_frames, &item, position );
341 sprintf( temp, "%d", item.frame );
342 markup = mlt_properties_get( contents, temp );
343 }
344
345 // See if any properties changed
346 property_changed = property_changed || ( this->fgcolor == NULL || ( fg && strcmp( fg, this->fgcolor ) ) );
347 property_changed = property_changed || ( this->bgcolor == NULL || ( bg && strcmp( bg, this->bgcolor ) ) );
348 property_changed = property_changed || ( pad != this->pad );
349 property_changed = property_changed || ( markup && this->markup && strcmp( markup, this->markup ) );
350 property_changed = property_changed || ( text && this->text && strcmp( text, this->text ) );
351 property_changed = property_changed || ( font && this->font && strcmp( font, this->font ) );
352 property_changed = property_changed || ( weight != this->weight );
353
354 // Save the properties for next comparison
355 this->align = align;
356 this->pad = pad;
357 set_string( &this->fgcolor, fg, "0xffffffff" );
358 set_string( &this->bgcolor, bg, "0x00000000" );
359 set_string( &this->markup, markup, NULL );
360 set_string( &this->text, text, NULL );
361 set_string( &this->font, font, "Sans 48" );
362 this->weight = weight;
363
364 if ( property_changed )
365 {
366 rgba_color fgcolor = parse_color( this->fgcolor );
367 rgba_color bgcolor = parse_color( this->bgcolor );
368
369 mlt_pool_release( this->image );
370 mlt_pool_release( this->alpha );
371 this->image = NULL;
372 this->alpha = NULL;
373
374 // Convert from specified encoding to UTF-8
375 if ( encoding != NULL && !strncaseeq( encoding, "utf-8", 5 ) && !strncaseeq( encoding, "utf8", 4 ) )
376 {
377 if ( markup != NULL && iconv_utf8( producer_props, "markup", encoding ) != -1 )
378 {
379 markup = mlt_properties_get( producer_props, "markup" );
380 set_string( &this->markup, markup, NULL );
381 }
382 if ( text != NULL && iconv_utf8( producer_props, "text", encoding ) != -1 )
383 {
384 text = mlt_properties_get( producer_props, "text" );
385 set_string( &this->text, text, NULL );
386 }
387 }
388
389 // Render the title
390 pixbuf = pango_get_pixbuf( markup, text, font, fgcolor, bgcolor, pad, align, weight, size );
391
392 if ( pixbuf != NULL )
393 {
394 // Register this pixbuf for destruction and reuse
395 mlt_properties_set_data( producer_props, "pixbuf", pixbuf, 0, ( mlt_destructor )g_object_unref, NULL );
396
397 mlt_properties_set_int( producer_props, "real_width", gdk_pixbuf_get_width( pixbuf ) );
398 mlt_properties_set_int( producer_props, "real_height", gdk_pixbuf_get_height( pixbuf ) );
399
400 // Store the width/height of the pixbuf temporarily
401 this->width = gdk_pixbuf_get_width( pixbuf );
402 this->height = gdk_pixbuf_get_height( pixbuf );
403 }
404 }
405 else if ( width > 0 && ( this->image == NULL || width != this->width || height != this->height ) )
406 {
407 mlt_pool_release( this->image );
408 mlt_pool_release( this->alpha );
409 this->image = NULL;
410 this->alpha = NULL;
411
412 pixbuf = mlt_properties_get_data( producer_props, "pixbuf", NULL );
413 }
414
415 // If we have a pixbuf and a valid width
416 if ( pixbuf && width > 0 )
417 {
418 char *interps = mlt_properties_get( properties, "rescale.interp" );
419 int interp = GDK_INTERP_BILINEAR;
420
421 if ( strcmp( interps, "nearest" ) == 0 )
422 interp = GDK_INTERP_NEAREST;
423 else if ( strcmp( interps, "tiles" ) == 0 )
424 interp = GDK_INTERP_TILES;
425 else if ( strcmp( interps, "hyper" ) == 0 )
426 interp = GDK_INTERP_HYPER;
427
428 // fprintf( stderr, "SCALING PANGO from %dx%d to %dx%d was %dx%d\n", gdk_pixbuf_get_width( pixbuf ), gdk_pixbuf_get_height( pixbuf ), width, height, this->width, this->height );
429
430 // Note - the original pixbuf is already safe and ready for destruction
431 pixbuf = gdk_pixbuf_scale_simple( pixbuf, width, height, interp );
432
433 // Store width and height
434 this->width = width;
435 this->height = height;
436
437 // Allocate/define image
438 this->image = mlt_pool_alloc( width * ( height + 1 ) * 2 );
439 this->alpha = mlt_pool_alloc( this->width * this->height );
440
441 // Convert the image
442 mlt_convert_rgb24a_to_yuv422( gdk_pixbuf_get_pixels( pixbuf ),
443 this->width, this->height,
444 gdk_pixbuf_get_rowstride( pixbuf ),
445 this->image, this->alpha );
446
447 // Finished with pixbuf now
448 g_object_unref( pixbuf );
449 }
450
451 // Set width/height
452 mlt_properties_set_int( properties, "width", this->width );
453 mlt_properties_set_int( properties, "height", this->height );
454 mlt_properties_set_int( properties, "real_width", mlt_properties_get_int( producer_props, "real_width" ) );
455 mlt_properties_set_int( properties, "real_height", mlt_properties_get_int( producer_props, "real_height" ) );
456
457 // pass the image data without destructor
458 mlt_properties_set_data( properties, "image", this->image, this->width * ( this->height + 1 ) * 2, NULL, NULL );
459 mlt_properties_set_data( properties, "alpha", this->alpha, this->width * this->height, NULL, NULL );
460 }
461
462 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
463 {
464 // Obtain properties of frame
465 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
466
467 // We need to know the size of the image to clone it
468 int image_size = 0;
469 int alpha_size = 0;
470
471 // Alpha channel
472 uint8_t *alpha = NULL;
473
474 *width = mlt_properties_get_int( properties, "rescale_width" );
475 *height = mlt_properties_get_int( properties, "rescale_height" );
476
477 // Refresh the image
478 pthread_mutex_lock( &pango_mutex );
479 refresh_image( frame, *width, *height );
480
481 // Get the image
482 *buffer = mlt_properties_get_data( properties, "image", &image_size );
483 alpha = mlt_properties_get_data( properties, "alpha", &alpha_size );
484
485 // Get width and height
486 *width = mlt_properties_get_int( properties, "width" );
487 *height = mlt_properties_get_int( properties, "height" );
488
489 // Always clone here to allow 'animated' text
490 if ( *buffer != NULL )
491 {
492 // Clone the image and the alpha
493 uint8_t *image_copy = mlt_pool_alloc( image_size );
494 uint8_t *alpha_copy = mlt_pool_alloc( alpha_size );
495
496 memcpy( image_copy, *buffer, image_size );
497
498 // Copy or default the alpha
499 if ( alpha != NULL )
500 memcpy( alpha_copy, alpha, alpha_size );
501 else
502 memset( alpha_copy, 255, alpha_size );
503
504 // Now update properties so we free the copy after
505 mlt_properties_set_data( properties, "image", image_copy, image_size, mlt_pool_release, NULL );
506 mlt_properties_set_data( properties, "alpha", alpha_copy, alpha_size, mlt_pool_release, NULL );
507
508 // We're going to pass the copy on
509 *buffer = image_copy;
510 }
511 else
512 {
513 // TODO: Review all cases of invalid images
514 *buffer = mlt_pool_alloc( 50 * 50 * 2 );
515 mlt_properties_set_data( properties, "image", *buffer, image_size, mlt_pool_release, NULL );
516 *width = 50;
517 *height = 50;
518 }
519
520 pthread_mutex_unlock( &pango_mutex );
521
522 return 0;
523 }
524
525 static uint8_t *producer_get_alpha_mask( mlt_frame this )
526 {
527 // Obtain properties of frame
528 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
529
530 // Return the alpha mask
531 return mlt_properties_get_data( properties, "alpha", NULL );
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 // Generate a frame
539 *frame = mlt_frame_init( );
540
541 // Obtain properties of frame and producer
542 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
543
544 // Set the producer on the frame properties
545 mlt_properties_set_data( properties, "producer_pango", this, 0, NULL, NULL );
546
547 // Update timecode on the frame we're creating
548 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
549 mlt_properties_set_position( properties, "pango_position", mlt_producer_frame( producer ) );
550
551 // Refresh the pango image
552 pthread_mutex_lock( &pango_mutex );
553 refresh_image( *frame, 0, 0 );
554 pthread_mutex_unlock( &pango_mutex );
555
556 // Set producer-specific frame properties
557 mlt_properties_set_int( properties, "progressive", 1 );
558 mlt_properties_set_double( properties, "aspect_ratio", 1 );
559
560 // Set alpha call back
561 ( *frame )->get_alpha_mask = producer_get_alpha_mask;
562
563 // Stack the get image callback
564 mlt_frame_push_get_image( *frame, producer_get_image );
565
566 // Calculate the next timecode
567 mlt_producer_prepare_next( producer );
568
569 return 0;
570 }
571
572 static void producer_close( mlt_producer parent )
573 {
574 producer_pango this = parent->child;
575 mlt_pool_release( this->image );
576 mlt_pool_release( this->alpha );
577 free( this->fgcolor );
578 free( this->bgcolor );
579 free( this->markup );
580 free( this->text );
581 free( this->font );
582 parent->close = NULL;
583 mlt_producer_close( parent );
584 free( this );
585 }
586
587 static void pango_draw_background( GdkPixbuf *pixbuf, rgba_color bg )
588 {
589 int ww = gdk_pixbuf_get_width( pixbuf );
590 int hh = gdk_pixbuf_get_height( pixbuf );
591 uint8_t *p = gdk_pixbuf_get_pixels( pixbuf );
592 int i, j;
593
594 for ( j = 0; j < hh; j++ )
595 {
596 for ( i = 0; i < ww; i++ )
597 {
598 *p++ = bg.r;
599 *p++ = bg.g;
600 *p++ = bg.b;
601 *p++ = bg.a;
602 }
603 }
604 }
605
606 static GdkPixbuf *pango_get_pixbuf( const char *markup, const char *text, const char *font, rgba_color fg, rgba_color bg, int pad, int align, int weight, int size )
607 {
608 PangoContext *context = pango_ft2_font_map_create_context( fontmap );
609 PangoLayout *layout = pango_layout_new( context );
610 int w, h, x;
611 int i, j;
612 GdkPixbuf *pixbuf = NULL;
613 FT_Bitmap bitmap;
614 uint8_t *src = NULL;
615 uint8_t* dest = NULL;
616 uint8_t *d, *s, a;
617 int stride;
618 PangoFontDescription *desc = pango_font_description_from_string( font );
619
620 pango_ft2_font_map_set_resolution( fontmap, 72, 72 );
621 pango_layout_set_width( layout, -1 ); // set wrapping constraints
622 pango_font_description_set_weight( desc, ( PangoWeight ) weight );
623 if ( size != 0 )
624 pango_font_description_set_size( desc, PANGO_SCALE * size );
625 pango_layout_set_font_description( layout, desc );
626 // pango_layout_set_spacing( layout, space );
627 pango_layout_set_alignment( layout, ( PangoAlignment ) align );
628 if ( markup != NULL && strcmp( markup, "" ) != 0 )
629 pango_layout_set_markup( layout, markup, strlen( markup ) );
630 else if ( text != NULL && strcmp( text, "" ) != 0 )
631 pango_layout_set_text( layout, text, strlen( text ) );
632 else
633 pango_layout_set_text( layout, " ", 2 );
634 pango_layout_get_pixel_size( layout, &w, &h );
635
636 pixbuf = gdk_pixbuf_new( GDK_COLORSPACE_RGB, TRUE /* has alpha */, 8, w + 2 * pad, h + 2 * pad );
637 pango_draw_background( pixbuf, bg );
638
639 stride = gdk_pixbuf_get_rowstride( pixbuf );
640
641 bitmap.width = w;
642 bitmap.pitch = 32 * ( ( w + 31 ) / 31 );
643 bitmap.rows = h;
644 bitmap.buffer = mlt_pool_alloc( h * bitmap.pitch );
645 bitmap.num_grays = 256;
646 bitmap.pixel_mode = ft_pixel_mode_grays;
647
648 memset( bitmap.buffer, 0, h * bitmap.pitch );
649
650 pango_ft2_render_layout( &bitmap, layout, 0, 0 );
651
652 src = bitmap.buffer;
653 x = ( gdk_pixbuf_get_width( pixbuf ) - w - 2 * pad ) * align / 2 + pad;
654 dest = gdk_pixbuf_get_pixels( pixbuf ) + 4 * x + pad * stride;
655 j = h;
656
657 while( j -- )
658 {
659 d = dest;
660 s = src;
661 i = w;
662 while( i -- )
663 {
664 a = *s ++;
665 *d++ = ( a * fg.r + ( 255 - a ) * bg.r ) >> 8;
666 *d++ = ( a * fg.g + ( 255 - a ) * bg.g ) >> 8;
667 *d++ = ( a * fg.b + ( 255 - a ) * bg.b ) >> 8;
668 *d++ = ( a * fg.a + ( 255 - a ) * bg.a ) >> 8;
669 }
670 dest += stride;
671 src += bitmap.pitch;
672 }
673 mlt_pool_release( bitmap.buffer );
674 pango_font_description_free( desc );
675 g_object_unref( layout );
676 g_object_unref( context );
677
678 return pixbuf;
679 }