2 * producer_pango.c -- a pango-based titler
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Dan Dennedy <dan@dennedy.org>
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.
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.
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.
21 #include "producer_pango.h"
22 #include <framework/mlt_frame.h>
25 #include <gdk-pixbuf/gdk-pixbuf.h>
26 #include <pango/pangoft2.h>
27 #include <freetype/freetype.h>
29 struct producer_pango_s
31 struct mlt_producer_s parent
;
45 // special color type used by internal pango routines
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
);
58 mlt_producer
producer_pango_init( const char *markup
)
60 producer_pango
this = calloc( sizeof( struct producer_pango_s
), 1 );
61 if ( this != NULL
&& mlt_producer_init( &this->parent
, this ) == 0 )
63 mlt_producer producer
= &this->parent
;
65 producer
->get_frame
= producer_get_frame
;
66 producer
->close
= producer_close
;
68 // This is required to initialise gdk-pixbuf
71 // Get the properties interface
72 mlt_properties properties
= mlt_producer_properties( &this->parent
);
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
, "markup", ( char * ) ( markup
== NULL ?
"" : markup
) );
81 mlt_properties_set( properties
, "text", "" );
82 mlt_properties_set( properties
, "font", "Sans 48" );
83 mlt_properties_set_int( properties
, "x", 0 );
84 mlt_properties_set_int( properties
, "y", 0 );
85 mlt_properties_set_double( properties
, "mix", 1.0 );
87 mlt_properties_set( properties
, "resource", "pango" );
95 static int producer_get_image( mlt_frame
this, uint8_t **buffer
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
97 // Obtain properties of frame
98 mlt_properties properties
= mlt_frame_properties( this );
100 // May need to know the size of the image to clone it
104 uint8_t *image
= mlt_properties_get_data( properties
, "image", &size
);
106 // Get width and height
107 *width
= mlt_properties_get_int( properties
, "width" );
108 *height
= mlt_properties_get_int( properties
, "height" );
110 // Clone if necessary
114 uint8_t *copy
= malloc( size
);
115 memcpy( copy
, image
, size
);
117 // We're going to pass the copy on
120 // Now update properties so we free the copy after
121 mlt_properties_set_data( properties
, "image", copy
, size
, free
, NULL
);
130 static uint8_t *producer_get_alpha_mask( mlt_frame
this )
132 // Obtain properties of frame
133 mlt_properties properties
= mlt_frame_properties( this );
135 // Return the alpha mask
136 return mlt_properties_get_data( properties
, "alpha", NULL
);
139 static int producer_get_frame( mlt_producer producer
, mlt_frame_ptr frame
, int index
)
141 producer_pango
this = producer
->child
;
142 GdkPixbuf
*pixbuf
= NULL
;
145 *frame
= mlt_frame_init( );
147 // Obtain properties of frame and producer
148 mlt_properties properties
= mlt_frame_properties( *frame
);
149 mlt_properties producer_props
= mlt_producer_properties( producer
);
151 // Get producer properties
152 int fg
= mlt_properties_get_int( producer_props
, "fgcolor" );
153 int bg
= mlt_properties_get_int( producer_props
, "bgcolor" );
154 int align
= mlt_properties_get_int( producer_props
, "align" );
155 int pad
= mlt_properties_get_int( producer_props
, "pad" );
156 char *markup
= mlt_properties_get( producer_props
, "markup" );
157 char *text
= mlt_properties_get( producer_props
, "text" );
158 char *font
= mlt_properties_get( producer_props
, "font" );
160 // See if any properties changed
161 int property_changed
= ( fg
!= this->fgcolor
);
162 property_changed
= property_changed
|| ( bg
!= this->bgcolor
);
163 property_changed
= property_changed
|| ( align
!= this->align
);
164 property_changed
= property_changed
|| ( pad
!= this->pad
);
165 property_changed
= property_changed
|| ( markup
&& this->markup
&& strcmp( markup
, this->markup
) );
166 property_changed
= property_changed
|| ( text
&& this->text
&& strcmp( text
, this->text
) );
167 property_changed
= property_changed
|| ( font
&& this->font
&& strcmp( font
, this->font
) );
169 // Save the properties for next comparison
174 if ( markup
!= NULL
)
176 if ( this->markup
!= NULL
)
177 free( this->markup
);
178 this->markup
= strdup( markup
);
182 if ( this->text
!= NULL
)
184 this->text
= strdup( text
);
188 if ( this->font
!= NULL
)
190 this->font
= strdup( font
);
193 if ( property_changed
)
197 ( fg
& 0xff000000 ) >> 24,
198 ( fg
& 0x00ff0000 ) >> 16,
199 ( fg
& 0x0000ff00 ) >> 8,
204 ( bg
& 0xff000000 ) >> 24,
205 ( bg
& 0x00ff0000 ) >> 16,
206 ( bg
& 0x0000ff00 ) >> 8,
211 pixbuf
= pango_get_pixbuf( markup
, text
, font
, fgcolor
, bgcolor
, pad
, align
);
214 // If we have a pixbuf
217 // Scale to adjust for sample aspect ratio
218 if ( mlt_properties_get_int( properties
, "video_standard" ) == mlt_video_standard_pal
)
220 GdkPixbuf
*temp
= pixbuf
;
221 GdkPixbuf
*scaled
= gdk_pixbuf_scale_simple( pixbuf
,
222 (gint
) ( (float) gdk_pixbuf_get_width( pixbuf
) * 54.0/59.0),
223 gdk_pixbuf_get_height( pixbuf
), GDK_INTERP_HYPER
);
225 g_object_unref( temp
);
229 GdkPixbuf
*temp
= pixbuf
;
230 GdkPixbuf
*scaled
= gdk_pixbuf_scale_simple( pixbuf
,
231 (gint
) ( (float) gdk_pixbuf_get_width( pixbuf
) * 11.0/10.0 ),
232 gdk_pixbuf_get_height( pixbuf
), GDK_INTERP_HYPER
);
234 g_object_unref( temp
);
237 // Store width and height
238 this->width
= gdk_pixbuf_get_width( pixbuf
);
239 this->height
= gdk_pixbuf_get_height( pixbuf
);
241 // Allocate/define image and alpha
242 uint8_t *image
= malloc( this->width
* this->height
* 2 );
243 uint8_t *alpha
= NULL
;
245 // Extract YUV422 and alpha
246 if ( gdk_pixbuf_get_has_alpha( pixbuf
) )
248 // Allocate the alpha mask
249 alpha
= malloc( this->width
* this->height
);
252 mlt_convert_rgb24a_to_yuv422( gdk_pixbuf_get_pixels( pixbuf
),
253 this->width
, this->height
,
254 gdk_pixbuf_get_rowstride( pixbuf
),
259 // No alpha to extract
260 mlt_convert_rgb24_to_yuv422( gdk_pixbuf_get_pixels( pixbuf
),
261 this->width
, this->height
,
262 gdk_pixbuf_get_rowstride( pixbuf
),
266 // Finished with pixbuf now
267 g_object_unref( pixbuf
);
269 // if single picture, reference the image and alpha in the producer
270 if ( this->image
!= NULL
)
273 if ( this->alpha
!= NULL
)
279 if ( this->image
!= NULL
)
282 mlt_properties_set_int( properties
, "width", this->width
);
283 mlt_properties_set_int( properties
, "height", this->height
);
285 // Set the compositing properties
286 mlt_properties_set_int( properties
, "x", mlt_properties_get_int( producer_props
, "x" ) );
287 mlt_properties_set_int( properties
, "y", mlt_properties_get_int( producer_props
, "y" ) );
288 mlt_properties_set_double( properties
, "mix", mlt_properties_get_double( producer_props
, "mix" ) );
290 // if picture sequence pass the image and alpha data without destructor
291 mlt_properties_set_data( properties
, "image", this->image
, 0, NULL
, NULL
);
292 mlt_properties_set_data( properties
, "alpha", this->alpha
, 0, NULL
, NULL
);
294 // Set alpha mask call back
295 ( *frame
)->get_alpha_mask
= producer_get_alpha_mask
;
297 // Stack the get image callback
298 mlt_frame_push_get_image( *frame
, producer_get_image
);
301 // Update timecode on the frame we're creating
302 mlt_frame_set_timecode( *frame
, mlt_producer_position( producer
) );
304 // Calculate the next timecode
305 mlt_producer_prepare_next( producer
);
310 static void producer_close( mlt_producer parent
)
312 producer_pango
this = parent
->child
;
313 if ( this->image
!= NULL
)
315 if ( this->alpha
!= NULL
)
317 if ( this->markup
!= NULL
)
318 free( this->markup
);
319 if ( this->text
!= NULL
)
321 if ( this->font
!= NULL
)
323 parent
->close
= NULL
;
324 mlt_producer_close( parent
);
328 static void pango_draw_background( GdkPixbuf
*pixbuf
, rgba_color bg
)
330 int ww
= gdk_pixbuf_get_width( pixbuf
);
331 int hh
= gdk_pixbuf_get_height( pixbuf
);
332 uint8_t *p
= gdk_pixbuf_get_pixels( pixbuf
);
335 for ( j
= 0; j
< hh
; j
++ )
337 for ( i
= 0; i
< ww
; i
++ )
347 static GdkPixbuf
*pango_get_pixbuf( const char *markup
, const char *text
, const char *font
, rgba_color fg
, rgba_color bg
, int pad
, int align
)
349 PangoFT2FontMap
*fontmap
= (PangoFT2FontMap
*) pango_ft2_font_map_new();
350 PangoContext
*context
= pango_ft2_font_map_create_context( fontmap
);
351 PangoLayout
*layout
= pango_layout_new( context
);
354 GdkPixbuf
*pixbuf
= NULL
;
357 uint8_t* dest
= NULL
;
360 pango_ft2_font_map_set_resolution( fontmap
, 72, 72 );
361 pango_layout_set_width( layout
, -1 ); // set wrapping constraints
362 pango_layout_set_font_description( layout
, pango_font_description_from_string( font
) );
363 // pango_layout_set_spacing( layout, space );
364 pango_layout_set_alignment( layout
, ( PangoAlignment
) align
);
365 if ( markup
!= NULL
&& strcmp( markup
, "" ) != 0 )
366 pango_layout_set_markup( layout
, markup
, strlen( markup
) );
367 else if ( text
!= NULL
&& strcmp( text
, "" ) != 0 )
368 pango_layout_set_text( layout
, text
, strlen( text
) );
371 pango_layout_get_pixel_size( layout
, &w
, &h
);
373 pixbuf
= gdk_pixbuf_new( GDK_COLORSPACE_RGB
, TRUE
/* has alpha */, 8, w
+ 2 * pad
, h
+ 2 * pad
);
374 pango_draw_background( pixbuf
, bg
);
376 stride
= gdk_pixbuf_get_rowstride( pixbuf
);
379 bitmap
.pitch
= 32 * ( ( w
+ 31 ) / 31 );
381 bitmap
.buffer
= ( unsigned char * ) calloc( 1, h
* bitmap
.pitch
);
382 bitmap
.num_grays
= 256;
383 bitmap
.pixel_mode
= ft_pixel_mode_grays
;
385 pango_ft2_render_layout( &bitmap
, layout
, 0, 0 );
388 x
= ( gdk_pixbuf_get_width( pixbuf
) - w
- 2 * pad
) * align
/ 2 + pad
;
389 dest
= gdk_pixbuf_get_pixels( pixbuf
) + 4 * x
+ pad
* stride
;
390 for ( j
= 0; j
< h
; j
++ )
393 for ( i
= 0; i
< w
; i
++ )
395 float a
= ( float ) bitmap
.buffer
[ j
* bitmap
.pitch
+ i
] / 255.0;
396 *d
++ = ( int ) ( a
* fg
.r
+ ( 1 - a
) * bg
.r
);
397 *d
++ = ( int ) ( a
* fg
.g
+ ( 1 - a
) * bg
.g
);
398 *d
++ = ( int ) ( a
* fg
.b
+ ( 1 - a
) * bg
.b
);
399 *d
++ = ( int ) ( a
* fg
.a
+ ( 1 - a
) * bg
.a
);
403 free( bitmap
.buffer
);
405 g_object_unref( layout
);
406 g_object_unref( context
);
407 g_object_unref( fontmap
);