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