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