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