2 * producer_pango.c -- a pango-based titler
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Dan Dennedy <dan@dennedy.org>
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.
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.
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
21 #include <framework/mlt_producer.h>
22 #include <framework/mlt_frame.h>
23 #include <framework/mlt_geometry.h>
26 #include <gdk-pixbuf/gdk-pixbuf.h>
27 #include <pango/pangoft2.h>
28 #include <freetype/freetype.h>
33 typedef struct producer_pango_s
*producer_pango
;
42 static pthread_mutex_t pango_mutex
= PTHREAD_MUTEX_INITIALIZER
;
44 struct producer_pango_s
46 struct mlt_producer_s parent
;
61 // special color type used by internal pango routines
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
);
74 /** Return nonzero if the two strings are equal, ignoring case, up to
75 the first n characters.
77 int strncaseeq(const char *s1
, const char *s2
, size_t n
)
81 if (tolower(*s1
++) != tolower(*s2
++))
87 /** Parse the alignment property.
90 static int alignment_parse( char* align
)
92 int ret
= pango_align_left
;
95 else if ( isdigit( align
[ 0 ] ) )
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
;
105 static PangoFT2FontMap
*fontmap
= NULL
;
107 mlt_producer
producer_pango_init( const char *filename
)
109 producer_pango
this = calloc( sizeof( struct producer_pango_s
), 1 );
110 if ( this != NULL
&& mlt_producer_init( &this->parent
, this ) == 0 )
112 mlt_producer producer
= &this->parent
;
114 pthread_mutex_lock( &pango_mutex
);
115 if ( fontmap
== NULL
)
116 fontmap
= (PangoFT2FontMap
*) pango_ft2_font_map_new();
118 pthread_mutex_unlock( &pango_mutex
);
120 producer
->get_frame
= producer_get_frame
;
121 producer
->close
= ( mlt_destructor
)producer_close
;
123 // Get the properties interface
124 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( &this->parent
);
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
);
136 if ( filename
== NULL
)
138 mlt_properties_set( properties
, "markup", "" );
140 else if ( filename
[ 0 ] == '+' || strstr( filename
, "/+" ) )
142 char *copy
= strdup( filename
+ 1 );
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", ( char * )filename
);
150 mlt_properties_set( properties
, "markup", markup
);
153 else if ( strstr( filename
, ".mpl" ) )
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", ( char * )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
);
163 // Make sure we have at least one entry
164 if ( mlt_properties_get( contents
, "0" ) == NULL
)
165 mlt_properties_set( contents
, "0", "" );
167 for ( i
= 0; i
< mlt_properties_count( contents
); i
++ )
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
);
179 FILE *f
= fopen( filename
, "r" );
187 while ( fgets( line
, 80, f
) )
189 size
+= strlen( line
) + 1;
192 markup
= realloc( markup
, size
);
193 strcat( markup
, line
);
197 markup
= strdup( line
);
202 if ( markup
[ strlen( markup
) - 1 ] == '\n' )
203 markup
[ strlen( markup
) - 1 ] = '\0';
205 mlt_properties_set( properties
, "resource", ( char * ) filename
);
206 mlt_properties_set( properties
, "markup", ( char * ) ( markup
== NULL ?
"" : markup
) );
211 mlt_properties_set( properties
, "markup", "" );
221 static void set_string( char **string
, char *value
, char *fallback
)
226 *string
= strdup( value
);
228 else if ( *string
== NULL
&& fallback
!= NULL
)
230 *string
= strdup( fallback
);
232 else if ( *string
!= NULL
&& fallback
== NULL
)
239 rgba_color
parse_color( char *color
)
241 rgba_color result
= { 0xff, 0xff, 0xff, 0xff };
243 if ( !strncmp( color
, "0x", 2 ) )
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;
252 else if ( !strcmp( color
, "red" ) )
258 else if ( !strcmp( color
, "green" ) )
264 else if ( !strcmp( color
, "blue" ) )
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;
283 /** Convert a string property to UTF-8
285 static int iconv_utf8( mlt_properties properties
, char *prop_name
, const char* encoding
)
287 char *text
= mlt_properties_get( properties
, prop_name
);
290 iconv_t cd
= iconv_open( "UTF-8", encoding
);
291 if ( cd
!= ( iconv_t
)-1 )
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
;
299 memset( outbuf
, 0, outbuf_n
);
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
);
304 mlt_properties_set( properties
, prop_name
, "" );
306 mlt_pool_release( outbuf
);
313 static void refresh_image( mlt_frame frame
, int width
, int height
)
316 GdkPixbuf
*pixbuf
= mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame
), "pixbuf", NULL
);
318 // Obtain properties of frame
319 mlt_properties properties
= MLT_FRAME_PROPERTIES( frame
);
321 // Obtain the producer pango for this frame
322 producer_pango
this = mlt_properties_get_data( properties
, "producer_pango", NULL
);
324 // Obtain the producer
325 mlt_producer producer
= &this->parent
;
327 // Obtain the producer properties
328 mlt_properties producer_props
= MLT_PRODUCER_PROPERTIES( producer
);
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;
343 if ( pixbuf
== NULL
)
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
)
353 mlt_geometry_prev_key( key_frames
, &item
, position
);
354 sprintf( temp
, "%d", item
.frame
);
355 markup
= mlt_properties_get( contents
, temp
);
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
);
368 // Save the properties for next comparison
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
;
379 if ( pixbuf
== NULL
&& property_changed
)
381 rgba_color fgcolor
= parse_color( this->fgcolor
);
382 rgba_color bgcolor
= parse_color( this->bgcolor
);
384 mlt_pool_release( this->image
);
385 mlt_pool_release( this->alpha
);
389 // Convert from specified encoding to UTF-8
390 if ( encoding
!= NULL
&& !strncaseeq( encoding
, "utf-8", 5 ) && !strncaseeq( encoding
, "utf8", 4 ) )
392 if ( markup
!= NULL
&& iconv_utf8( producer_props
, "markup", encoding
) != -1 )
394 markup
= mlt_properties_get( producer_props
, "markup" );
395 set_string( &this->markup
, markup
, NULL
);
397 if ( text
!= NULL
&& iconv_utf8( producer_props
, "text", encoding
) != -1 )
399 text
= mlt_properties_get( producer_props
, "text" );
400 set_string( &this->text
, text
, NULL
);
405 pixbuf
= pango_get_pixbuf( markup
, text
, font
, fgcolor
, bgcolor
, pad
, align
, weight
, size
);
407 if ( pixbuf
!= NULL
)
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
);
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
) );
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
);
422 else if ( pixbuf
== NULL
&& ( width
> 0 && ( this->image
== NULL
|| width
!= this->width
|| height
!= this->height
) ) )
424 mlt_pool_release( this->image
);
425 mlt_pool_release( this->alpha
);
429 pixbuf
= mlt_properties_get_data( producer_props
, "pixbuf", NULL
);
432 // If we have a pixbuf and a valid width
433 if ( pixbuf
&& width
> 0 )
435 char *interps
= mlt_properties_get( properties
, "rescale.interp" );
436 int interp
= GDK_INTERP_BILINEAR
;
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
;
445 // fprintf(stderr,"%s: scaling from %dx%d to %dx%d\n", __FILE__, this->width, this->height, width, height);
447 // Note - the original pixbuf is already safe and ready for destruction
448 pixbuf
= gdk_pixbuf_scale_simple( pixbuf
, width
, height
, interp
);
450 // Store width and height
452 this->height
= height
;
454 // Allocate/define image
455 this->image
= mlt_pool_alloc( width
* ( height
+ 1 ) * 2 );
456 this->alpha
= mlt_pool_alloc( this->width
* this->height
);
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
);
464 // Finished with pixbuf now
465 g_object_unref( pixbuf
);
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" ) );
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
);
479 static int producer_get_image( mlt_frame frame
, uint8_t **buffer
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
481 // Obtain properties of frame
482 mlt_properties properties
= MLT_FRAME_PROPERTIES( frame
);
484 // We need to know the size of the image to clone it
489 uint8_t *alpha
= NULL
;
491 *width
= mlt_properties_get_int( properties
, "rescale_width" );
492 *height
= mlt_properties_get_int( properties
, "rescale_height" );
495 pthread_mutex_lock( &pango_mutex
);
496 refresh_image( frame
, *width
, *height
);
499 *buffer
= mlt_properties_get_data( properties
, "image", &image_size
);
500 alpha
= mlt_properties_get_data( properties
, "alpha", &alpha_size
);
502 // Get width and height
503 *width
= mlt_properties_get_int( properties
, "width" );
504 *height
= mlt_properties_get_int( properties
, "height" );
506 // Always clone here to allow 'animated' text
507 if ( *buffer
!= NULL
)
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
);
513 memcpy( image_copy
, *buffer
, image_size
);
515 // Copy or default the alpha
517 memcpy( alpha_copy
, alpha
, alpha_size
);
519 memset( alpha_copy
, 255, alpha_size
);
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
);
525 // We're going to pass the copy on
526 *buffer
= image_copy
;
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
);
537 pthread_mutex_unlock( &pango_mutex
);
542 static uint8_t *producer_get_alpha_mask( mlt_frame
this )
544 // Obtain properties of frame
545 mlt_properties properties
= MLT_FRAME_PROPERTIES( this );
547 // Return the alpha mask
548 return mlt_properties_get_data( properties
, "alpha", NULL
);
551 static int producer_get_frame( mlt_producer producer
, mlt_frame_ptr frame
, int index
)
553 producer_pango
this = producer
->child
;
556 *frame
= mlt_frame_init( MLT_PRODUCER_SERVICE( producer
) );
558 // Obtain properties of frame and producer
559 mlt_properties properties
= MLT_FRAME_PROPERTIES( *frame
);
561 // Set the producer on the frame properties
562 mlt_properties_set_data( properties
, "producer_pango", this, 0, NULL
, NULL
);
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
) );
568 // Refresh the pango image
569 pthread_mutex_lock( &pango_mutex
);
570 refresh_image( *frame
, 0, 0 );
571 pthread_mutex_unlock( &pango_mutex
);
573 // Set producer-specific frame properties
574 mlt_properties_set_int( properties
, "progressive", 1 );
575 mlt_properties_set_double( properties
, "aspect_ratio", 1 );
577 // Set alpha call back
578 ( *frame
)->get_alpha_mask
= producer_get_alpha_mask
;
580 // Stack the get image callback
581 mlt_frame_push_get_image( *frame
, producer_get_image
);
583 // Calculate the next timecode
584 mlt_producer_prepare_next( producer
);
589 static void producer_close( mlt_producer parent
)
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
);
599 parent
->close
= NULL
;
600 mlt_producer_close( parent
);
604 static void pango_draw_background( GdkPixbuf
*pixbuf
, rgba_color bg
)
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
);
611 for ( j
= 0; j
< hh
; j
++ )
613 for ( i
= 0; i
< ww
; i
++ )
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
)
625 PangoContext
*context
= pango_ft2_font_map_create_context( fontmap
);
626 PangoLayout
*layout
= pango_layout_new( context
);
629 GdkPixbuf
*pixbuf
= NULL
;
632 uint8_t* dest
= NULL
;
635 PangoFontDescription
*desc
= pango_font_description_from_string( font
);
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
);
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 )
647 pango_layout_set_markup( layout
, markup
, strlen( markup
) );
649 else if ( text
!= NULL
&& strcmp( text
, "" ) != 0 )
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
) );
660 // Pango doesn't like empty strings
661 pango_layout_set_text( layout
, " ", 2 );
663 pango_layout_get_pixel_size( layout
, &w
, &h
);
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
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
);
678 pixbuf
= gdk_pixbuf_new( GDK_COLORSPACE_RGB
, TRUE
/* has alpha */, 8, w
+ 2 * pad
, h
+ 2 * pad
);
679 pango_draw_background( pixbuf
, bg
);
681 stride
= gdk_pixbuf_get_rowstride( pixbuf
);
684 bitmap
.pitch
= 32 * ( ( w
+ 31 ) / 31 );
686 bitmap
.buffer
= mlt_pool_alloc( h
* bitmap
.pitch
);
687 bitmap
.num_grays
= 256;
688 bitmap
.pixel_mode
= ft_pixel_mode_grays
;
690 memset( bitmap
.buffer
, 0, h
* bitmap
.pitch
);
692 pango_ft2_render_layout( &bitmap
, layout
, 0, 0 );
695 x
= ( gdk_pixbuf_get_width( pixbuf
) - w
- 2 * pad
) * align
/ 2 + pad
;
696 dest
= gdk_pixbuf_get_pixels( pixbuf
) + 4 * x
+ pad
* stride
;
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;
715 mlt_pool_release( bitmap
.buffer
);
716 pango_font_description_free( desc
);
717 g_object_unref( layout
);
718 g_object_unref( context
);