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 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.
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.
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.
21 #include "producer_pango.h"
22 #include <framework/mlt_frame.h>
25 #include <gdk-pixbuf/gdk-pixbuf.h>
26 #include <pango/pangoft2.h>
27 #include <freetype/freetype.h>
30 struct producer_pango_s
32 struct mlt_producer_s parent;
47 // special color type used by internal pango routines
53 // Forward declarations
54 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int index );
55 static void producer_close( mlt_producer parent );
56 static void pango_draw_background( GdkPixbuf *pixbuf, rgba_color bg );
57 static GdkPixbuf *pango_get_pixbuf( const char *markup, const char *text, const char *font,
58 rgba_color fg, rgba_color bg, int pad, int align, int weight, int size );
60 /** Return nonzero if the two strings are equal, ignoring case, up to
61 the first n characters.
63 int strncaseeq(const char *s1, const char *s2, size_t n)
67 if (tolower(*s1++) != tolower(*s2++))
73 /** Parse the alignment property.
76 static int alignment_parse( char* align )
78 int ret = pango_align_left;
81 else if ( isdigit( align[ 0 ] ) )
83 else if ( align[ 0 ] == 'c' || align[ 0 ] == 'm' )
84 ret = pango_align_center;
85 else if ( align[ 0 ] == 'r' )
86 ret = pango_align_right;
91 mlt_producer producer_pango_init( const char *filename )
93 producer_pango this = calloc( sizeof( struct producer_pango_s ), 1 );
94 if ( this != NULL && mlt_producer_init( &this->parent, this ) == 0 )
96 mlt_producer producer = &this->parent;
98 producer->get_frame = producer_get_frame;
99 producer->close = ( mlt_destructor )producer_close;
101 // This is required to initialise gdk-pixbuf
104 // Get the properties interface
105 mlt_properties properties = MLT_PRODUCER_PROPERTIES( &this->parent );
107 // Set the default properties
108 mlt_properties_set( properties, "fgcolour", "0xffffffff" );
109 mlt_properties_set( properties, "bgcolour", "0x00000000" );
110 mlt_properties_set_int( properties, "align", pango_align_left );
111 mlt_properties_set_int( properties, "pad", 0 );
112 mlt_properties_set( properties, "text", "" );
113 mlt_properties_set( properties, "font", "Sans 48" );
114 mlt_properties_set( properties, "encoding", "UTF-8" );
115 mlt_properties_set_int( properties, "weight", PANGO_WEIGHT_NORMAL );
117 if ( filename == NULL )
119 mlt_properties_set( properties, "markup", "" );
121 else if ( filename[ 0 ] == '+' || strstr( filename, "/+" ) )
123 char *copy = strdup( filename + 1 );
125 if ( strstr( markup, "/+" ) )
126 markup = strstr( markup, "/+" ) + 2;
127 ( *strrchr( markup, '.' ) ) = '\0';
128 while ( strchr( markup, '~' ) )
129 ( *strchr( markup, '~' ) ) = '\n';
130 mlt_properties_set( properties, "resource", ( char * )filename );
131 mlt_properties_set( properties, "markup", markup );
136 FILE *f = fopen( filename, "r" );
144 while ( fgets( line, 80, f ) )
146 size += strlen( line ) + 1;
149 markup = realloc( markup, size );
150 strcat( markup, line );
154 markup = strdup( line );
159 if ( markup[ strlen( markup ) - 1 ] == '\n' )
160 markup[ strlen( markup ) - 1 ] = '\0';
162 mlt_properties_set( properties, "resource", ( char * ) filename );
163 mlt_properties_set( properties, "markup", ( char * ) ( markup == NULL ? "" : markup ) );
168 mlt_properties_set( properties, "markup", "" );
178 static void set_string( char **string, char *value, char *fallback )
183 *string = strdup( value );
185 else if ( *string == NULL && fallback != NULL )
187 *string = strdup( fallback );
189 else if ( *string != NULL && fallback == NULL )
196 rgba_color parse_color( char *color )
198 rgba_color result = { 0xff, 0xff, 0xff, 0xff };
200 if ( !strncmp( color, "0x", 2 ) )
202 unsigned int temp = 0;
203 sscanf( color + 2, "%x", &temp );
204 result.r = ( temp >> 24 ) & 0xff;
205 result.g = ( temp >> 16 ) & 0xff;
206 result.b = ( temp >> 8 ) & 0xff;
207 result.a = ( temp ) & 0xff;
209 else if ( !strcmp( color, "red" ) )
215 else if ( !strcmp( color, "green" ) )
221 else if ( !strcmp( color, "blue" ) )
229 unsigned int temp = 0;
230 sscanf( color, "%d", &temp );
231 result.r = ( temp >> 24 ) & 0xff;
232 result.g = ( temp >> 16 ) & 0xff;
233 result.b = ( temp >> 8 ) & 0xff;
234 result.a = ( temp ) & 0xff;
240 /** Convert a string property to UTF-8
242 static int iconv_utf8( mlt_properties properties, char *prop_name, const char* encoding )
244 char *text = mlt_properties_get( properties, prop_name );
247 iconv_t cd = iconv_open( "UTF-8", encoding );
248 if ( cd != ( iconv_t )-1 )
250 char *inbuf_p = text;
251 size_t inbuf_n = strlen( text );
252 size_t outbuf_n = inbuf_n * 6;
253 char *outbuf = mlt_pool_alloc( outbuf_n );
254 char *outbuf_p = outbuf;
256 memset( outbuf, 0, outbuf_n );
258 if ( text != NULL && strcmp( text, "" ) && iconv( cd, &inbuf_p, &inbuf_n, &outbuf_p, &outbuf_n ) != -1 )
259 mlt_properties_set( properties, prop_name, outbuf );
261 mlt_properties_set( properties, prop_name, "" );
263 mlt_pool_release( outbuf );
270 static void refresh_image( mlt_frame frame, int width, int height )
273 GdkPixbuf *pixbuf = NULL;
275 // Obtain properties of frame
276 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
278 // Obtain the producer pango for this frame
279 producer_pango this = mlt_properties_get_data( properties, "producer_pango", NULL );
281 // Obtain the producer
282 mlt_producer producer = &this->parent;
284 // Obtain the producer properties
285 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
287 // Get producer properties
288 char *fg = mlt_properties_get( producer_props, "fgcolour" );
289 char *bg = mlt_properties_get( producer_props, "bgcolour" );
290 int align = alignment_parse( mlt_properties_get( producer_props, "align" ) );
291 int pad = mlt_properties_get_int( producer_props, "pad" );
292 char *markup = mlt_properties_get( producer_props, "markup" );
293 char *text = mlt_properties_get( producer_props, "text" );
294 char *font = mlt_properties_get( producer_props, "font" );
295 char *encoding = mlt_properties_get( producer_props, "encoding" );
296 int weight = mlt_properties_get_int( producer_props, "weight" );
297 int size = mlt_properties_get_int( producer_props, "size" );
299 // See if any properties changed
300 int property_changed = ( align != this->align );
301 property_changed = property_changed || ( this->fgcolor == NULL || ( fg && strcmp( fg, this->fgcolor ) ) );
302 property_changed = property_changed || ( this->bgcolor == NULL || ( bg && strcmp( bg, this->bgcolor ) ) );
303 property_changed = property_changed || ( pad != this->pad );
304 property_changed = property_changed || ( markup && this->markup && strcmp( markup, this->markup ) );
305 property_changed = property_changed || ( text && this->text && strcmp( text, this->text ) );
306 property_changed = property_changed || ( font && this->font && strcmp( font, this->font ) );
307 property_changed = property_changed || ( weight != this->weight );
309 // Save the properties for next comparison
312 set_string( &this->fgcolor, fg, "0xffffffff" );
313 set_string( &this->bgcolor, bg, "0x00000000" );
314 set_string( &this->markup, markup, NULL );
315 set_string( &this->text, text, NULL );
316 set_string( &this->font, font, "Sans 48" );
317 this->weight = weight;
319 if ( property_changed )
321 rgba_color fgcolor = parse_color( this->fgcolor );
322 rgba_color bgcolor = parse_color( this->bgcolor );
324 mlt_pool_release( this->image );
325 mlt_pool_release( this->alpha );
329 // Convert from specified encoding to UTF-8
330 if ( encoding != NULL && !strncaseeq( encoding, "utf-8", 5 ) && !strncaseeq( encoding, "utf8", 4 ) )
332 if ( markup != NULL && iconv_utf8( producer_props, "markup", encoding ) != -1 )
334 markup = mlt_properties_get( producer_props, "markup" );
335 set_string( &this->markup, markup, NULL );
337 if ( text != NULL && iconv_utf8( producer_props, "text", encoding ) != -1 )
339 text = mlt_properties_get( producer_props, "text" );
340 set_string( &this->text, text, NULL );
345 pixbuf = pango_get_pixbuf( markup, text, font, fgcolor, bgcolor, pad, align, weight, size );
347 if ( pixbuf != NULL )
349 // Register this pixbuf for destruction and reuse
350 mlt_properties_set_data( producer_props, "pixbuf", pixbuf, 0, ( mlt_destructor )g_object_unref, NULL );
352 mlt_properties_set_int( producer_props, "real_width", gdk_pixbuf_get_width( pixbuf ) );
353 mlt_properties_set_int( producer_props, "real_height", gdk_pixbuf_get_height( pixbuf ) );
355 // Store the width/height of the pixbuf temporarily
356 this->width = gdk_pixbuf_get_width( pixbuf );
357 this->height = gdk_pixbuf_get_height( pixbuf );
360 else if ( width > 0 && ( this->image == NULL || width != this->width || height != this->height ) )
362 mlt_pool_release( this->image );
363 mlt_pool_release( this->alpha );
367 pixbuf = mlt_properties_get_data( producer_props, "pixbuf", NULL );
370 // If we have a pixbuf and a valid width
371 if ( pixbuf && width > 0 )
373 char *interps = mlt_properties_get( properties, "rescale.interp" );
374 int interp = GDK_INTERP_BILINEAR;
376 if ( strcmp( interps, "nearest" ) == 0 )
377 interp = GDK_INTERP_NEAREST;
378 else if ( strcmp( interps, "tiles" ) == 0 )
379 interp = GDK_INTERP_TILES;
380 else if ( strcmp( interps, "hyper" ) == 0 )
381 interp = GDK_INTERP_HYPER;
383 // 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 );
385 // Note - the original pixbuf is already safe and ready for destruction
386 pixbuf = gdk_pixbuf_scale_simple( pixbuf, width, height, interp );
388 // Store width and height
390 this->height = height;
392 // Allocate/define image
393 this->image = mlt_pool_alloc( width * ( height + 1 ) * 2 );
394 this->alpha = mlt_pool_alloc( this->width * this->height );
397 mlt_convert_rgb24a_to_yuv422( gdk_pixbuf_get_pixels( pixbuf ),
398 this->width, this->height,
399 gdk_pixbuf_get_rowstride( pixbuf ),
400 this->image, this->alpha );
402 // Finished with pixbuf now
403 g_object_unref( pixbuf );
407 mlt_properties_set_int( properties, "width", this->width );
408 mlt_properties_set_int( properties, "height", this->height );
409 mlt_properties_set_int( properties, "real_width", mlt_properties_get_int( producer_props, "real_width" ) );
410 mlt_properties_set_int( properties, "real_height", mlt_properties_get_int( producer_props, "real_height" ) );
412 // pass the image data without destructor
413 mlt_properties_set_data( properties, "image", this->image, this->width * ( this->height + 1 ) * 2, NULL, NULL );
414 mlt_properties_set_data( properties, "alpha", this->alpha, this->width * this->height, NULL, NULL );
417 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
419 // Obtain properties of frame
420 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
422 *width = mlt_properties_get_int( properties, "rescale_width" );
423 *height = mlt_properties_get_int( properties, "rescale_height" );
426 refresh_image( frame, *width, *height );
429 //mlt_producer this = mlt_properties_get_data( properties, "producer_pango", NULL );
430 //*format = ( mlt_properties_get_int( MLT_PRODUCER_PROPERTIES( this ), "bpp" ) == 4 ) ? mlt_image_rgb24a : mlt_image_rgb24;
432 // May need to know the size of the image to clone it
436 uint8_t *image = mlt_properties_get_data( properties, "image", &size );
438 // Get width and height
439 *width = mlt_properties_get_int( properties, "width" );
440 *height = mlt_properties_get_int( properties, "height" );
442 // Clone if necessary
446 uint8_t *copy = mlt_pool_alloc( size );
447 if ( copy != NULL && image != NULL )
448 memcpy( copy, image, size );
450 // We're going to pass the copy on
453 // Now update properties so we free the copy after
454 mlt_properties_set_data( properties, "image", copy, size, ( mlt_destructor )mlt_pool_release, NULL );
463 static uint8_t *producer_get_alpha_mask( mlt_frame this )
465 // Obtain properties of frame
466 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
468 // Return the alpha mask
469 return mlt_properties_get_data( properties, "alpha", NULL );
472 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
474 producer_pango this = producer->child;
477 *frame = mlt_frame_init( );
479 // Obtain properties of frame and producer
480 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
482 // Set the producer on the frame properties
483 mlt_properties_set_data( properties, "producer_pango", this, 0, NULL, NULL );
485 // Refresh the pango image
486 refresh_image( *frame, 0, 0 );
488 // Set producer-specific frame properties
489 mlt_properties_set_int( properties, "progressive", 1 );
490 mlt_properties_set_double( properties, "aspect_ratio", 1 );
492 // Set alpha call back
493 ( *frame )->get_alpha_mask = producer_get_alpha_mask;
495 // Stack the get image callback
496 mlt_frame_push_get_image( *frame, producer_get_image );
498 // Update timecode on the frame we're creating
499 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
501 // Calculate the next timecode
502 mlt_producer_prepare_next( producer );
507 static void producer_close( mlt_producer parent )
509 producer_pango this = parent->child;
510 mlt_pool_release( this->image );
511 mlt_pool_release( this->alpha );
512 free( this->fgcolor );
513 free( this->bgcolor );
514 free( this->markup );
517 parent->close = NULL;
518 mlt_producer_close( parent );
522 static void pango_draw_background( GdkPixbuf *pixbuf, rgba_color bg )
524 int ww = gdk_pixbuf_get_width( pixbuf );
525 int hh = gdk_pixbuf_get_height( pixbuf );
526 uint8_t *p = gdk_pixbuf_get_pixels( pixbuf );
529 for ( j = 0; j < hh; j++ )
531 for ( i = 0; i < ww; i++ )
541 static PangoFT2FontMap *fontmap = NULL;
543 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 )
545 if ( fontmap == NULL )
546 fontmap = (PangoFT2FontMap*) pango_ft2_font_map_new();
548 PangoContext *context = pango_ft2_font_map_create_context( fontmap );
549 PangoLayout *layout = pango_layout_new( context );
552 GdkPixbuf *pixbuf = NULL;
555 uint8_t* dest = NULL;
558 PangoFontDescription *desc = pango_font_description_from_string( font );
560 pango_ft2_font_map_set_resolution( fontmap, 72, 72 );
561 pango_layout_set_width( layout, -1 ); // set wrapping constraints
562 pango_font_description_set_weight( desc, ( PangoWeight ) weight );
564 pango_font_description_set_size( desc, PANGO_SCALE * size );
565 pango_layout_set_font_description( layout, desc );
566 // pango_layout_set_spacing( layout, space );
567 pango_layout_set_alignment( layout, ( PangoAlignment ) align );
568 if ( markup != NULL && strcmp( markup, "" ) != 0 )
569 pango_layout_set_markup( layout, markup, strlen( markup ) );
570 else if ( text != NULL && strcmp( text, "" ) != 0 )
571 pango_layout_set_text( layout, text, strlen( text ) );
573 pango_layout_set_text( layout, " ", 2 );
574 pango_layout_get_pixel_size( layout, &w, &h );
576 pixbuf = gdk_pixbuf_new( GDK_COLORSPACE_RGB, TRUE /* has alpha */, 8, w + 2 * pad, h + 2 * pad );
577 pango_draw_background( pixbuf, bg );
579 stride = gdk_pixbuf_get_rowstride( pixbuf );
582 bitmap.pitch = 32 * ( ( w + 31 ) / 31 );
584 bitmap.buffer = mlt_pool_alloc( h * bitmap.pitch );
585 bitmap.num_grays = 256;
586 bitmap.pixel_mode = ft_pixel_mode_grays;
588 memset( bitmap.buffer, 0, h * bitmap.pitch );
590 pango_ft2_render_layout( &bitmap, layout, 0, 0 );
593 x = ( gdk_pixbuf_get_width( pixbuf ) - w - 2 * pad ) * align / 2 + pad;
594 dest = gdk_pixbuf_get_pixels( pixbuf ) + 4 * x + pad * stride;
605 *d++ = ( a * fg.r + ( 255 - a ) * bg.r ) >> 8;
606 *d++ = ( a * fg.g + ( 255 - a ) * bg.g ) >> 8;
607 *d++ = ( a * fg.b + ( 255 - a ) * bg.b ) >> 8;
608 *d++ = ( a * fg.a + ( 255 - a ) * bg.a ) >> 8;
613 mlt_pool_release( bitmap.buffer );
614 pango_font_description_free( desc );
615 g_object_unref( layout );
616 g_object_unref( context );