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