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