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