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