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