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