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