1 /*
2  * producer_pango.c -- a pango-based titler
3  * Copyright (C) 2003-2020 Meltytech, LLC
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19 
20 #include <framework/mlt_producer.h>
21 #include <framework/mlt_frame.h>
22 #include <framework/mlt_geometry.h>
23 #include <framework/mlt_cache.h>
24 #include <framework/mlt_log.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <gdk-pixbuf/gdk-pixbuf.h>
28 #include <pango/pangoft2.h>
29 #include <ft2build.h>
30 #include FT_FREETYPE_H
31 #include <iconv.h>
32 #include <pthread.h>
33 #include <ctype.h>
34 
35 typedef struct producer_pango_s *producer_pango;
36 
37 typedef enum
38 {
39 	pango_align_left = 0,
40 	pango_align_center,
41 	pango_align_right
42 } pango_align;
43 
44 static pthread_mutex_t pango_mutex = PTHREAD_MUTEX_INITIALIZER;
45 
46 struct pango_cached_image_s
47 {
48 	uint8_t *image, *alpha;
49 	mlt_image_format format;
50 	int width, height;
51 };
52 
pango_cached_image_destroy(void * p)53 static void pango_cached_image_destroy( void* p )
54 {
55 	struct pango_cached_image_s* i = p;
56 
57 	if ( !i )
58 		return;
59 	if ( i->image )
60 		mlt_pool_release( i->image );
61 	if ( i->alpha )
62 		mlt_pool_release( i->alpha );
63 	mlt_pool_release( i );
64 };
65 
66 struct producer_pango_s
67 {
68 	struct mlt_producer_s parent;
69 	int   width;
70 	int   height;
71 	GdkPixbuf *pixbuf;
72 	char *fgcolor;
73 	char *bgcolor;
74 	char *olcolor;
75 	int   align;
76 	int   pad;
77 	int   outline;
78 	char *markup;
79 	char *text;
80 	char *font;
81 	char *family;
82 	int   size;
83 	int   style;
84 	int   weight;
85 	int   stretch;
86 	int   rotate;
87 	int   width_crop;
88 	int   width_fit;
89 	int   wrap_type;
90 	int   wrap_width;
91 	int   line_spacing;
92 	double aspect_ratio;
93 };
94 
clean_cached(producer_pango self)95 static void clean_cached( producer_pango self )
96 {
97 	mlt_service_cache_put( MLT_PRODUCER_SERVICE( &self->parent ), "pango.image", NULL, 0, NULL );
98 }
99 
100 // special color type used by internal pango routines
101 typedef struct
102 {
103 	uint8_t r, g, b, a;
104 } rgba_color;
105 
106 // Forward declarations
107 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int index );
108 static void producer_close( mlt_producer parent );
109 static void pango_draw_background( GdkPixbuf *pixbuf, rgba_color bg );
110 static GdkPixbuf *pango_get_pixbuf( const char *markup, const char *text, const char *font,
111 		rgba_color fg, rgba_color bg, rgba_color ol, int pad, int align, char* family,
112 		int style, int weight, int stretch, int size, int outline, int rotate,
113 		int width_crop, int width_fit, int wrap_type, int wrap_width,
114 		int line_spacing, double aspect_ratio );
115 static void fill_pixbuf( GdkPixbuf* pixbuf, FT_Bitmap* bitmap, int w, int h, int pad, int align, rgba_color fg, rgba_color bg );
116 static void fill_pixbuf_with_outline( GdkPixbuf* pixbuf, FT_Bitmap* bitmap, int w, int h, int pad, int align, rgba_color fg, rgba_color bg, rgba_color ol, int outline );
117 
118 /** Return nonzero if the two strings are equal, ignoring case, up to
119     the first n characters.
120 */
strncaseeq(const char * s1,const char * s2,size_t n)121 int strncaseeq(const char *s1, const char *s2, size_t n)
122 {
123 	for ( ; n > 0; n--)
124 	{
125 		if (tolower(*s1++) != tolower(*s2++))
126 			return 0;
127 	}
128 	return 1;
129 }
130 
131 /** Parse the alignment property.
132 */
133 
parse_alignment(char * align)134 static int parse_alignment( char* align )
135 {
136 	int ret = pango_align_left;
137 
138 	if ( align == NULL );
139 	else if ( isdigit( align[ 0 ] ) )
140 		ret = atoi( align );
141 	else if ( align[ 0 ] == 'c' || align[ 0 ] == 'm' )
142 		ret = pango_align_center;
143 	else if ( align[ 0 ] == 'r' )
144 		ret = pango_align_right;
145 
146 	return ret;
147 }
148 
149 /** Parse the style property.
150 */
151 
parse_style(char * style)152 static int parse_style( char* style )
153 {
154 	int ret = PANGO_STYLE_NORMAL;
155 	if( !strncmp(style, "italic", 6) )
156 		ret = PANGO_STYLE_ITALIC;
157 	if( !strncmp(style, "oblique", 7) )
158 		ret = PANGO_STYLE_OBLIQUE;
159 	return ret;
160 }
161 
162 static PangoFT2FontMap *fontmap = NULL;
163 
164 static void on_fontmap_reload( );
producer_pango_init(const char * filename)165 mlt_producer producer_pango_init( const char *filename )
166 {
167 	producer_pango self = calloc( 1, sizeof( struct producer_pango_s ) );
168 	if ( self != NULL && mlt_producer_init( &self->parent, self ) == 0 )
169 	{
170 		mlt_producer producer = &self->parent;
171 
172 		pthread_mutex_lock( &pango_mutex );
173 		if ( fontmap == NULL )
174 			fontmap = (PangoFT2FontMap*) pango_ft2_font_map_new();
175 		pthread_mutex_unlock( &pango_mutex );
176 
177 		producer->get_frame = producer_get_frame;
178 		producer->close = ( mlt_destructor )producer_close;
179 
180 		// Get the properties interface
181 		mlt_properties properties = MLT_PRODUCER_PROPERTIES( &self->parent );
182 
183 		mlt_events_register( properties, "fontmap-reload", NULL );
184 		mlt_events_listen( properties, producer, "fontmap-reload", (mlt_listener) on_fontmap_reload );
185 
186 		// Set the default properties
187 		mlt_properties_set_string( properties, "fgcolour", "0xffffffff" );
188 		mlt_properties_set_string( properties, "bgcolour", "0x00000000" );
189 		mlt_properties_set_string( properties, "olcolour", "0x00000000" );
190 		mlt_properties_set_int( properties, "align", pango_align_left );
191 		mlt_properties_set_int( properties, "pad", 0 );
192 		mlt_properties_set_int( properties, "outline", 0 );
193 		mlt_properties_set_string( properties, "text", "" );
194 		mlt_properties_set_string( properties, "font", NULL );
195 		mlt_properties_set_string( properties, "family", "Sans" );
196 		mlt_properties_set_int( properties, "size", 48 );
197 		mlt_properties_set_string( properties, "style", "normal" );
198 		mlt_properties_set_string( properties, "encoding", "UTF-8" );
199 		mlt_properties_set_int( properties, "weight", PANGO_WEIGHT_NORMAL );
200 		mlt_properties_set_int( properties, "stretch", PANGO_STRETCH_NORMAL + 1 );
201 		mlt_properties_set_int( properties, "rotate", 0 );
202 		mlt_properties_set_int( properties, "seekable", 1 );
203 
204 		if ( filename == NULL || ( filename && ( !strcmp( filename, "" )
205 			|| strstr( filename, "<producer>" )
206 			// workaround for old kdenlive countdown generator
207 			|| strstr( filename, "&lt;producer&gt;" ) ) ) )
208 		{
209 			mlt_properties_set_string( properties, "markup", "" );
210 		}
211 		else if ( filename[ 0 ] == '+' || strstr( filename, "/+" ) )
212 		{
213 			char *copy = strdup( filename + 1 );
214 			char *markup = copy;
215 			if ( strstr( markup, "/+" ) )
216 				markup = strstr( markup, "/+" ) + 2;
217 			if ( strrchr( markup, '.' ) )
218 				( *strrchr( markup, '.' ) ) = '\0';
219 			while ( strchr( markup, '~' ) )
220 				( *strchr( markup, '~' ) ) = '\n';
221 			mlt_properties_set_string( properties, "resource", filename );
222 			mlt_properties_set_string( properties, "markup", markup );
223 			free( copy );
224 		}
225 		else if ( strstr( filename, ".mpl" ) )
226 		{
227 			int i = 0;
228 			mlt_position out_point = 0;
229 			mlt_properties contents = mlt_properties_load( filename );
230 			mlt_geometry key_frames = mlt_geometry_init( );
231 			struct mlt_geometry_item_s item;
232 			mlt_properties_set_string( properties, "resource", filename );
233 			mlt_properties_set_data( properties, "contents", contents, 0, ( mlt_destructor )mlt_properties_close, NULL );
234 			mlt_properties_set_data( properties, "key_frames", key_frames, 0, ( mlt_destructor )mlt_geometry_close, NULL );
235 
236 			// Make sure we have at least one entry
237 			if ( mlt_properties_get( contents, "0" ) == NULL )
238 				mlt_properties_set_string( contents, "0", "" );
239 
240 			for ( i = 0; i < mlt_properties_count( contents ); i ++ )
241 			{
242 				char *name = mlt_properties_get_name( contents, i );
243 				char *value = mlt_properties_get_value( contents, i );
244 				while ( value != NULL && strchr( value, '~' ) )
245 					( *strchr( value, '~' ) ) = '\n';
246 				item.frame = atoi( name );
247 				mlt_geometry_insert( key_frames, &item );
248 				out_point = MAX( out_point, item.frame );
249 			}
250 			mlt_geometry_interpolate( key_frames );
251 			mlt_properties_set_position( properties, "length", out_point + 1 );
252 			mlt_properties_set_position( properties, "out", out_point );
253 		}
254 		else
255 		{
256 			mlt_properties_set_string( properties, "resource", filename );
257 			FILE *f = mlt_fopen( filename, "r" );
258 			if ( f != NULL )
259 			{
260 				char line[81];
261 				char *markup = NULL;
262 				size_t size = 0;
263 				line[80] = '\0';
264 
265 				while ( fgets( line, 80, f ) )
266 				{
267 					size += strlen( line ) + 1;
268 					if ( markup )
269 					{
270 						markup = realloc( markup, size );
271 						if ( markup )
272 							strcat( markup, line );
273 					}
274 					else
275 					{
276 						markup = strdup( line );
277 					}
278 				}
279 				fclose( f );
280 
281 				if ( markup && markup[ strlen( markup ) - 1 ] == '\n' )
282 					markup[ strlen( markup ) - 1 ] = '\0';
283 
284 				if ( markup )
285 					mlt_properties_set_string( properties, "markup", markup );
286 				else
287 					mlt_properties_set_string( properties, "markup", "" );
288 				free( markup );
289 			}
290 			else
291 			{
292 				producer->close = NULL;
293 				mlt_producer_close( producer );
294 				producer = NULL;
295 				free( self );
296 			}
297 		}
298 
299 		return producer;
300 	}
301 	free( self );
302 	return NULL;
303 }
304 
set_string(char ** string,const char * value,const char * fallback)305 static void set_string( char **string, const char *value, const char *fallback )
306 {
307 	if ( value != NULL )
308 	{
309 		free( *string );
310 		*string = strdup( value );
311 	}
312 	else if ( *string == NULL && fallback != NULL )
313 	{
314 		*string = strdup( fallback );
315 	}
316 	else if ( *string != NULL && fallback == NULL )
317 	{
318 		free( *string );
319 		*string = NULL;
320 	}
321 }
322 
parse_color(char * color,unsigned int color_int)323 rgba_color parse_color( char *color, unsigned int color_int )
324 {
325 	rgba_color result = { 0xff, 0xff, 0xff, 0xff };
326 
327 	if ( !strcmp( color, "red" ) )
328 	{
329 		result.r = 0xff;
330 		result.g = 0x00;
331 		result.b = 0x00;
332 	}
333 	else if ( !strcmp( color, "green" ) )
334 	{
335 		result.r = 0x00;
336 		result.g = 0xff;
337 		result.b = 0x00;
338 	}
339 	else if ( !strcmp( color, "blue" ) )
340 	{
341 		result.r = 0x00;
342 		result.g = 0x00;
343 		result.b = 0xff;
344 	}
345 	else if ( strcmp( color, "white" ) )
346 	{
347 		result.r = ( color_int >> 24 ) & 0xff;
348 		result.g = ( color_int >> 16 ) & 0xff;
349 		result.b = ( color_int >> 8 ) & 0xff;
350 		result.a = ( color_int ) & 0xff;
351 	}
352 
353 	return result;
354 }
355 
356 /** Convert a string property to UTF-8
357 */
iconv_utf8(mlt_properties properties,const char * prop_name,const char * encoding)358 static int iconv_utf8( mlt_properties properties, const char *prop_name, const char* encoding )
359 {
360 	char *text = mlt_properties_get( properties, prop_name );
361 	int result = -1;
362 
363 	iconv_t	cd = iconv_open( "UTF-8", encoding );
364 	if ( text && ( cd != ( iconv_t )-1 ) )
365 	{
366 		char *inbuf_p = text;
367 		size_t inbuf_n = strlen( text );
368 		size_t outbuf_n = inbuf_n * 6;
369 		char *outbuf = mlt_pool_alloc( outbuf_n );
370 		char *outbuf_p = outbuf;
371 
372 		memset( outbuf, 0, outbuf_n );
373 
374 		if ( text != NULL && strcmp( text, "" ) && iconv( cd, &inbuf_p, &inbuf_n, &outbuf_p, &outbuf_n ) != -1 )
375 			mlt_properties_set_string( properties, prop_name, outbuf );
376 		else
377 			mlt_properties_set_string( properties, prop_name, "" );
378 
379 		mlt_pool_release( outbuf );
380 		result = 0;
381 	}
382 	iconv_close( cd );
383 	return result;
384 }
385 
refresh_image(producer_pango self,mlt_frame frame,int width,int height)386 static void refresh_image( producer_pango self, mlt_frame frame, int width, int height )
387 {
388 	// Pixbuf
389 	GdkPixbuf *pixbuf = mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "pixbuf", NULL );
390 
391 	// Obtain properties of frame
392 	mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
393 
394 	// Obtain the producer
395 	mlt_producer producer = &self->parent;
396 
397 	// Obtain the producer properties
398 	mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
399 
400 	// Get producer properties
401 	char *fg = mlt_properties_get( producer_props, "fgcolour" );
402 	char *bg = mlt_properties_get( producer_props, "bgcolour" );
403 	char *ol = mlt_properties_get( producer_props, "olcolour" );
404 	int align = parse_alignment( mlt_properties_get( producer_props, "align" ) );
405 	int pad = mlt_properties_get_int( producer_props, "pad" );
406 	int outline = mlt_properties_get_int( producer_props, "outline" );
407 	char *markup = mlt_properties_get( producer_props, "markup" );
408 	char *text = mlt_properties_get( producer_props, "text" );
409 	char *font = mlt_properties_get( producer_props, "font" );
410 	char *family = mlt_properties_get( producer_props, "family" );
411 	int style = parse_style( mlt_properties_get( producer_props, "style" ) );
412 	char *encoding = mlt_properties_get( producer_props, "encoding" );
413 	int weight = mlt_properties_get_int( producer_props, "weight" );
414 	int stretch = mlt_properties_get_int( producer_props, "stretch" );
415 	int rotate = mlt_properties_get_int( producer_props, "rotate" );
416 	int size = mlt_properties_get_int( producer_props, "size" );
417 	int width_crop = mlt_properties_get_int( producer_props, "width_crop" );
418 	int width_fit = mlt_properties_get_int( producer_props, "width_fit" );
419 	int wrap_type = mlt_properties_get_int( producer_props, "wrap_type" );
420 	int wrap_width = mlt_properties_get_int( producer_props, "wrap_width" );
421 	int line_spacing = mlt_properties_get_int( properties, "line_spacing" );
422 	double aspect_ratio = mlt_properties_get_double( properties, "aspect_ratio" );
423 	int property_changed = 0;
424 
425 	if ( pixbuf == NULL )
426 	{
427 		// Check for file support
428 		mlt_properties contents = mlt_properties_get_data( producer_props, "contents", NULL );
429 		mlt_geometry key_frames = mlt_properties_get_data( producer_props, "key_frames", NULL );
430 		struct mlt_geometry_item_s item;
431 		if ( contents != NULL )
432 		{
433 			char temp[ 20 ];
434 			mlt_geometry_prev_key( key_frames, &item, mlt_frame_original_position( frame ) );
435 			sprintf( temp, "%d", item.frame );
436 			markup = mlt_properties_get( contents, temp );
437 		}
438 
439 		// See if any properties changed
440 		property_changed = ( align != self->align );
441 		property_changed = property_changed || ( self->fgcolor == NULL || ( fg && strcmp( fg, self->fgcolor ) ) );
442 		property_changed = property_changed || ( self->bgcolor == NULL || ( bg && strcmp( bg, self->bgcolor ) ) );
443 		property_changed = property_changed || ( self->olcolor == NULL || ( ol && strcmp( ol, self->olcolor ) ) );
444 		property_changed = property_changed || ( pad != self->pad );
445 		property_changed = property_changed || ( outline != self->outline );
446 		property_changed = property_changed || ( markup && self->markup && strcmp( markup, self->markup ) );
447 		property_changed = property_changed || ( text && self->text && strcmp( text, self->text ) );
448 		property_changed = property_changed || ( font && self->font && strcmp( font, self->font ) );
449 		property_changed = property_changed || ( family && self->family && strcmp( family, self->family ) );
450 		property_changed = property_changed || ( weight != self->weight );
451 		property_changed = property_changed || ( stretch != self->stretch );
452 		property_changed = property_changed || ( rotate != self->rotate );
453 		property_changed = property_changed || ( style != self->style );
454 		property_changed = property_changed || ( size != self->size );
455 		property_changed = property_changed || ( width_crop != self->width_crop );
456 		property_changed = property_changed || ( width_fit != self->width_fit );
457 		property_changed = property_changed || ( wrap_type != self->wrap_type );
458 		property_changed = property_changed || ( wrap_width != self->wrap_width );
459 		property_changed = property_changed || ( line_spacing != self->line_spacing );
460 		property_changed = property_changed || ( aspect_ratio != self->aspect_ratio );
461 
462 		// Save the properties for next comparison
463 		self->align = align;
464 		self->pad = pad;
465 		self->outline = outline;
466 		set_string( &self->fgcolor, fg, "0xffffffff" );
467 		set_string( &self->bgcolor, bg, "0x00000000" );
468 		set_string( &self->olcolor, ol, "0x00000000" );
469 		set_string( &self->markup, markup, NULL );
470 		set_string( &self->text, text, NULL );
471 		set_string( &self->font, font, NULL );
472 		set_string( &self->family, family, "Sans" );
473 		self->weight = weight;
474 		self->stretch = stretch;
475 		self->rotate = rotate;
476 		self->style = style;
477 		self->size = size;
478 		self->width_crop = width_crop;
479 		self->width_fit = width_fit;
480 		self->wrap_type = wrap_type;
481 		self->wrap_width = wrap_width;
482 		self->line_spacing = line_spacing;
483 		self->aspect_ratio = aspect_ratio;
484 	}
485 
486 	if ( pixbuf == NULL && property_changed )
487 	{
488 		rgba_color fgcolor = parse_color( self->fgcolor, mlt_properties_get_int( producer_props, "fgcolour" ) );
489 		rgba_color bgcolor = parse_color( self->bgcolor, mlt_properties_get_int( producer_props, "bgcolour" ) );
490 		rgba_color olcolor = parse_color( self->olcolor, mlt_properties_get_int( producer_props, "olcolour" ) );
491 
492 		if ( self->pixbuf )
493 			g_object_unref( self->pixbuf );
494 		self->pixbuf = NULL;
495 		clean_cached( self );
496 
497 		// Convert from specified encoding to UTF-8
498 		if ( encoding != NULL && !strncaseeq( encoding, "utf-8", 5 ) && !strncaseeq( encoding, "utf8", 4 ) )
499 		{
500 			if ( markup != NULL && iconv_utf8( producer_props, "markup", encoding ) != -1 )
501 			{
502 				markup = mlt_properties_get( producer_props, "markup" );
503 				set_string( &self->markup, markup, NULL );
504 			}
505 			if ( text != NULL && iconv_utf8( producer_props, "text", encoding ) != -1 )
506 			{
507 				text = mlt_properties_get( producer_props, "text" );
508 				set_string( &self->text, text, NULL );
509 			}
510 		}
511 
512 		// Render the title
513 		pixbuf = pango_get_pixbuf( markup, text, font, fgcolor, bgcolor, olcolor, pad, align, family,
514 			style, weight, stretch, size, outline, rotate,
515 			width_crop, width_fit, wrap_type, wrap_width,
516 			line_spacing, aspect_ratio );
517 
518 		if ( pixbuf != NULL )
519 		{
520 			// Register self pixbuf for destruction and reuse
521 			mlt_properties_set_data( producer_props, "pixbuf", pixbuf, 0, ( mlt_destructor )g_object_unref, NULL );
522 			g_object_ref( pixbuf );
523 			mlt_properties_set_data( MLT_FRAME_PROPERTIES( frame ), "pixbuf", pixbuf, 0, ( mlt_destructor )g_object_unref, NULL );
524 
525 			mlt_properties_set_int( producer_props, "meta.media.width", gdk_pixbuf_get_width( pixbuf ) );
526 			mlt_properties_set_int( producer_props, "meta.media.height", gdk_pixbuf_get_height( pixbuf ) );
527 
528 			// Store the width/height of the pixbuf temporarily
529 			self->width = gdk_pixbuf_get_width( pixbuf );
530 			self->height = gdk_pixbuf_get_height( pixbuf );
531 		}
532 	}
533 	else if ( pixbuf == NULL && width > 0 && ( self->pixbuf == NULL || width != self->width || height != self->height ) )
534 	{
535 		if ( self->pixbuf )
536 			g_object_unref( self->pixbuf );
537 		self->pixbuf = NULL;
538 		clean_cached( self );
539 		pixbuf = mlt_properties_get_data( producer_props, "pixbuf", NULL );
540 	}
541 
542 	// If we have a pixbuf and a valid width
543 	if ( pixbuf && width > 0 )
544 	{
545 		char *interps = mlt_properties_get( properties, "rescale.interp" );
546 		int interp = GDK_INTERP_BILINEAR;
547 
548 		if ( strcmp( interps, "nearest" ) == 0 )
549 			interp = GDK_INTERP_NEAREST;
550 		else if ( strcmp( interps, "tiles" ) == 0 )
551 			interp = GDK_INTERP_TILES;
552 		else if ( strcmp( interps, "hyper" ) == 0 || strcmp( interps, "bicubic" ) == 0 )
553 			interp = GDK_INTERP_HYPER;
554 
555 // fprintf(stderr,"%s: scaling from %dx%d to %dx%d\n", __FILE__, self->width, self->height, width, height);
556 
557 		// Note - the original pixbuf is already safe and ready for destruction
558 		self->pixbuf = gdk_pixbuf_scale_simple( pixbuf, width, height, interp );
559 		clean_cached( self );
560 
561 		// Store width and height
562 		self->width = width;
563 		self->height = height;
564 	}
565 
566 	// Set width/height
567 	mlt_properties_set_int( properties, "width", self->width );
568 	mlt_properties_set_int( properties, "height", self->height );
569 }
570 
producer_get_image(mlt_frame frame,uint8_t ** buffer,mlt_image_format * format,int * width,int * height,int writable)571 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
572 {
573 	int error = 0;
574 	producer_pango self = ( producer_pango ) mlt_frame_pop_service( frame );
575 
576 	// Obtain properties of frame
577 	mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
578 
579 	*width = mlt_properties_get_int( properties, "rescale_width" );
580 	*height = mlt_properties_get_int( properties, "rescale_height" );
581 
582 	mlt_service_lock( MLT_PRODUCER_SERVICE( &self->parent ) );
583 
584 	// Refresh the image
585 	pthread_mutex_lock( &pango_mutex );
586 	refresh_image( self, frame, *width, *height );
587 
588 	// Get width and height
589 	*width = self->width;
590 	*height = self->height;
591 
592 	// Always clone here to allow 'animated' text
593 	if ( self->pixbuf )
594 	{
595 		int size, bpp;
596 		uint8_t *buf;
597 		mlt_cache_item cached_item = mlt_service_cache_get( MLT_PRODUCER_SERVICE( &self->parent ), "pango.image" );
598 		struct pango_cached_image_s* cached = mlt_cache_item_data( cached_item, NULL );
599 
600 		// destroy cached data if request is differ
601 		if ( !cached || ( cached && (cached->format != *format || cached->width != *width || cached->height != *height )))
602 		{
603 			mlt_cache_item_close( cached_item );
604 			cached_item = NULL;
605 			cached = NULL;
606 			clean_cached( self );
607 		}
608 
609 		// create cached image
610 		if ( !cached )
611 		{
612 			int dst_stride, src_stride;
613 
614 			cached = mlt_pool_alloc( sizeof( struct pango_cached_image_s ));
615 			cached->width = self->width;
616 			cached->height = self->height;
617 			cached->format = gdk_pixbuf_get_has_alpha( self->pixbuf ) ? mlt_image_rgb24a : mlt_image_rgb24;
618 			cached->alpha = NULL;
619 			cached->image = NULL;
620 
621 			src_stride = gdk_pixbuf_get_rowstride( self->pixbuf );
622 			dst_stride = self->width * ( mlt_image_rgb24a == cached->format ? 4 : 3 );
623 
624 			size = mlt_image_format_size( cached->format, cached->width, cached->height, &bpp );
625 			buf = mlt_pool_alloc( size );
626 			uint8_t *buf_save = buf;
627 
628 			if ( src_stride != dst_stride )
629 			{
630 				int y = self->height;
631 				uint8_t *src = gdk_pixbuf_get_pixels( self->pixbuf );
632 				uint8_t *dst = buf;
633 				while ( y-- )
634 				{
635 					memcpy( dst, src, dst_stride );
636 					dst += dst_stride;
637 					src += src_stride;
638 				}
639 			}
640 			else
641 			{
642 				memcpy( buf, gdk_pixbuf_get_pixels( self->pixbuf ), src_stride * self->height );
643 			}
644 
645 			// convert image
646 			if(frame->convert_image && cached->format != *format)
647 			{
648 				frame->convert_image( frame, &buf, &cached->format, *format );
649 				*format = cached->format;
650 				if ( buf != buf_save ) mlt_pool_release( buf_save );
651 			}
652 
653 			size = mlt_image_format_size(cached->format, cached->width, cached->height, &bpp );
654 			cached->image = mlt_pool_alloc( size );
655 			memcpy( cached->image, buf, size );
656 
657 			if ( ( buf = mlt_frame_get_alpha( frame ) ) )
658 			{
659 				size = cached->width * cached->height;
660 				cached->alpha = mlt_pool_alloc( size );
661 				memcpy( cached->alpha, buf, size );
662 			}
663 		}
664 
665 		if ( cached )
666 		{
667 			// clone image surface
668 			size = mlt_image_format_size(cached->format, cached->width, cached->height, &bpp );
669 			buf = mlt_pool_alloc( size );
670 			memcpy( buf, cached->image, size );
671 
672 			// set image surface
673 			mlt_frame_set_image( frame, buf, size, mlt_pool_release );
674 			*buffer = buf;
675 
676 			// set alpha
677 			if ( cached->alpha )
678 			{
679 				size = cached->width * cached->height;
680 				buf = mlt_pool_alloc( size );
681 				memcpy( buf, cached->alpha, size );
682 				mlt_frame_set_alpha( frame, buf, size, mlt_pool_release );
683 			}
684 		}
685 
686 		if ( cached_item )
687 			mlt_cache_item_close( cached_item );
688 		else
689 			mlt_service_cache_put( MLT_PRODUCER_SERVICE( &self->parent ), "pango.image",
690 				cached, sizeof( struct pango_cached_image_s ), pango_cached_image_destroy );
691 	}
692 	else
693 	{
694 		error = 1;
695 	}
696 
697 	pthread_mutex_unlock( &pango_mutex );
698 	mlt_service_unlock( MLT_PRODUCER_SERVICE( &self->parent ) );
699 
700 	return error;
701 }
702 
producer_get_frame(mlt_producer producer,mlt_frame_ptr frame,int index)703 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
704 {
705 	producer_pango self = producer->child;
706 
707 	// Fetch the producers properties
708 	mlt_properties producer_properties = MLT_PRODUCER_PROPERTIES( producer );
709 
710 	// Generate a frame
711 	*frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
712 
713 	// Obtain properties of frame and producer
714 	mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
715 
716 	// Update timecode on the frame we're creating
717 	mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
718 
719 	// Set producer-specific frame properties
720 	mlt_properties_set_int( properties, "progressive", 1 );
721 	double force_ratio = mlt_properties_get_double( producer_properties, "force_aspect_ratio" );
722 	if ( force_ratio > 0.0 )
723 		mlt_properties_set_double( properties, "aspect_ratio", force_ratio );
724 	else
725 	{
726 		mlt_profile profile = mlt_service_profile( MLT_PRODUCER_SERVICE( producer ) );
727 		mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( profile ) );
728 	}
729 
730 	// Refresh the pango image
731 	pthread_mutex_lock( &pango_mutex );
732 	refresh_image( self, *frame, 0, 0 );
733 	pthread_mutex_unlock( &pango_mutex );
734 
735 	// Stack the get image callback
736 	mlt_frame_push_service( *frame, self );
737 	mlt_frame_push_get_image( *frame, producer_get_image );
738 
739 	// Calculate the next timecode
740 	mlt_producer_prepare_next( producer );
741 
742 	return 0;
743 }
744 
producer_close(mlt_producer parent)745 static void producer_close( mlt_producer parent )
746 {
747 	producer_pango self = parent->child;
748 	if ( self->pixbuf )
749 		g_object_unref( self->pixbuf );
750 	mlt_service_cache_purge( MLT_PRODUCER_SERVICE(parent) );
751 	free( self->fgcolor );
752 	free( self->bgcolor );
753 	free( self->olcolor );
754 	free( self->markup );
755 	free( self->text );
756 	free( self->font );
757 	free( self->family );
758 	parent->close = NULL;
759 	mlt_producer_close( parent );
760 	free( self );
761 }
762 
pango_draw_background(GdkPixbuf * pixbuf,rgba_color bg)763 static void pango_draw_background( GdkPixbuf *pixbuf, rgba_color bg )
764 {
765 	int ww = gdk_pixbuf_get_width( pixbuf );
766 	int hh = gdk_pixbuf_get_height( pixbuf );
767 	uint8_t *p = gdk_pixbuf_get_pixels( pixbuf );
768 	int i, j;
769 
770 	for ( j = 0; j < hh; j++ )
771 	{
772 		for ( i = 0; i < ww; i++ )
773 		{
774 			*p++ = bg.r;
775 			*p++ = bg.g;
776 			*p++ = bg.b;
777 			*p++ = bg.a;
778 		}
779 	}
780 }
781 
pango_get_pixbuf(const char * markup,const char * text,const char * font,rgba_color fg,rgba_color bg,rgba_color ol,int pad,int align,char * family,int style,int weight,int stretch,int size,int outline,int rotate,int width_crop,int width_fit,int wrap_type,int wrap_width,int line_spacing,double aspect_ratio)782 static GdkPixbuf *pango_get_pixbuf( const char *markup, const char *text, const char *font,
783 	rgba_color fg, rgba_color bg, rgba_color ol, int pad, int align, char* family,
784 	int style, int weight, int stretch, int size, int outline, int rotate,
785 	int width_crop, int width_fit, int wrap_type, int wrap_width,
786 	int line_spacing, double aspect_ratio )
787 {
788 	PangoContext *context = pango_ft2_font_map_create_context( fontmap );
789 	PangoLayout *layout = pango_layout_new( context );
790 	int w, h;
791 	int x = 0, y = 0;
792 	GdkPixbuf *pixbuf = NULL;
793 	FT_Bitmap bitmap;
794 	PangoFontDescription *desc = NULL;
795 
796 	if( font )
797 	{
798 		// Support for deprecated "font" property.
799 		desc = pango_font_description_from_string( font );
800 		pango_ft2_font_map_set_resolution( fontmap, 72, 72 );
801 	}
802 	else
803 	{
804 		desc = pango_font_description_from_string( family );
805 		pango_font_description_set_size( desc, PANGO_SCALE * size );
806 		pango_font_description_set_style( desc, (PangoStyle) style );
807 	}
808 
809 	pango_font_description_set_weight( desc, ( PangoWeight ) weight  );
810 
811 	if ( stretch )
812 		pango_font_description_set_stretch( desc, ( PangoStretch ) ( stretch - 1 ) );
813 
814 	// set line_spacing
815 	if ( line_spacing )
816 		pango_layout_set_spacing( layout,  PANGO_SCALE * line_spacing );
817 
818 	// set wrapping constraints
819 	if ( wrap_width <= 0 )
820 		pango_layout_set_width( layout, -1 );
821 	else
822 	{
823 		pango_layout_set_width( layout,  PANGO_SCALE * wrap_width );
824 		pango_layout_set_wrap( layout,  ( PangoWrapMode ) wrap_type );
825 	}
826 
827 	pango_layout_set_font_description( layout, desc );
828 	pango_layout_set_alignment( layout, ( PangoAlignment ) align  );
829 	if ( markup != NULL && strcmp( markup, "" ) != 0 )
830 	{
831 		pango_layout_set_markup( layout, markup, strlen( markup ) );
832 	}
833 	else if ( text != NULL && strcmp( text, "" ) != 0 )
834 	{
835 		pango_layout_set_text( layout, text, strlen( text ) );
836 	}
837 	else
838 	{
839 		// Pango doesn't like empty strings
840 		pango_layout_set_text( layout, "  ", 2 );
841 	}
842 
843 	if ( rotate )
844 	{
845 		double n_x, n_y;
846 		PangoRectangle rect;
847 		PangoMatrix m_layout = PANGO_MATRIX_INIT, m_offset = PANGO_MATRIX_INIT;
848 
849 		pango_matrix_rotate( &m_layout, rotate );
850 		pango_matrix_rotate( &m_offset, -rotate );
851 
852 		pango_context_set_base_gravity( context, PANGO_GRAVITY_AUTO );
853 		pango_context_set_matrix( context, &m_layout );
854 		pango_layout_context_changed( layout );
855 		pango_layout_get_extents( layout, NULL, &rect );
856 		pango_matrix_transform_rectangle( pango_context_get_matrix( context ), &rect);
857 
858 		n_x = -rect.x;
859 		n_y = -rect.y;
860 		pango_matrix_transform_point( &m_offset, &n_x, &n_y );
861 		rect.x = n_x;
862 		rect.y = n_y;
863 
864 		pango_extents_to_pixels( &rect, NULL );
865 
866 		w = rect.width;
867 		h = rect.height;
868 
869 		x = rect.x;
870 		y = rect.y;
871 	}
872 	else
873 		pango_layout_get_pixel_size( layout, &w, &h );
874 
875 	// respect aspect ratio
876 	if ( 0.0 < aspect_ratio && aspect_ratio != 1.0)
877 	{
878 		double n_x, n_y;
879 		PangoRectangle rect;
880 		PangoMatrix m_layout = PANGO_MATRIX_INIT, m_offset = PANGO_MATRIX_INIT;
881 #if 1
882 		pango_matrix_scale( &m_layout, 1.0 / aspect_ratio, 1.0 );
883 		pango_matrix_scale( &m_offset, 1.0 / aspect_ratio, 1.0 );
884 #else
885 		pango_matrix_scale( &m_layout, 1.0, aspect_ratio );
886 		pango_matrix_scale( &m_offset, 1.0, aspect_ratio );
887 #endif
888 		pango_context_set_base_gravity( context, PANGO_GRAVITY_AUTO );
889 		pango_context_set_matrix( context, &m_layout );
890 		pango_layout_context_changed( layout );
891 		pango_layout_get_extents( layout, NULL, &rect );
892 		pango_matrix_transform_rectangle( pango_context_get_matrix( context ), &rect);
893 
894 		n_x = -rect.x;
895 		n_y = -rect.y;
896 		pango_matrix_transform_point( &m_offset, &n_x, &n_y );
897 		rect.x = n_x;
898 		rect.y = n_y;
899 
900 		pango_extents_to_pixels( &rect, NULL );
901 
902 		w = rect.width;
903 		h = rect.height;
904 
905 		x = rect.x;
906 		y = rect.y;
907 	}
908 
909 	// limit width
910 	if ( width_crop && w > width_crop)
911 		w = width_crop;
912 	else if (width_fit && w > width_fit)
913 	{
914 		double n_x, n_y;
915 		PangoRectangle rect;
916 		PangoMatrix m_layout = PANGO_MATRIX_INIT, m_offset = PANGO_MATRIX_INIT;
917 
918 		pango_matrix_scale( &m_layout, width_fit / (double)w, 1.0 );
919 		pango_matrix_scale( &m_offset, width_fit / (double)w, 1.0 );
920 
921 		pango_context_set_base_gravity( context, PANGO_GRAVITY_AUTO );
922 		pango_context_set_matrix( context, &m_layout );
923 		pango_layout_context_changed( layout );
924 		pango_layout_get_extents( layout, NULL, &rect );
925 		pango_matrix_transform_rectangle( pango_context_get_matrix( context ), &rect);
926 
927 		n_x = -rect.x;
928 		n_y = -rect.y;
929 		pango_matrix_transform_point( &m_offset, &n_x, &n_y );
930 		rect.x = n_x;
931 		rect.y = n_y;
932 
933 		pango_extents_to_pixels( &rect, NULL );
934 
935 		w = rect.width;
936 		h = rect.height;
937 
938 		x = rect.x;
939 		y = rect.y;
940 	}
941 
942 	if ( pad == 0 )
943 		pad = 1;
944 
945 	pixbuf = gdk_pixbuf_new( GDK_COLORSPACE_RGB, TRUE /* has alpha */, 8, w + 2 * pad, h + 2 * pad );
946 	pango_draw_background( pixbuf, bg );
947 
948 	bitmap.width     = w;
949 	bitmap.pitch     = 32 * ( ( w + 31 ) / 31 );
950 	bitmap.rows      = h;
951 	bitmap.buffer    = mlt_pool_alloc( h * bitmap.pitch );
952 	bitmap.num_grays = 256;
953 	bitmap.pixel_mode = ft_pixel_mode_grays;
954 
955 	memset( bitmap.buffer, 0, h * bitmap.pitch );
956 
957 	pango_ft2_render_layout( &bitmap, layout, x, y );
958 
959 	if ( outline )
960 	{
961 		fill_pixbuf_with_outline( pixbuf, &bitmap, w, h, pad, align, fg, bg, ol, outline );
962 	}
963 	else
964 	{
965 		fill_pixbuf( pixbuf, &bitmap, w, h, pad, align, fg, bg );
966 	}
967 
968 	mlt_pool_release( bitmap.buffer );
969 	pango_font_description_free( desc );
970 	g_object_unref( layout );
971 	g_object_unref( context );
972 
973 	return pixbuf;
974 }
975 
fill_pixbuf(GdkPixbuf * pixbuf,FT_Bitmap * bitmap,int w,int h,int pad,int align,rgba_color fg,rgba_color bg)976 static void fill_pixbuf( GdkPixbuf* pixbuf, FT_Bitmap* bitmap, int w, int h, int pad, int align, rgba_color fg, rgba_color bg )
977 {
978 	int stride = gdk_pixbuf_get_rowstride( pixbuf );
979 	uint8_t* src = bitmap->buffer;
980 	int x = ( gdk_pixbuf_get_width( pixbuf ) - w - 2 * pad ) * align / 2 + pad;
981 	uint8_t* dest = gdk_pixbuf_get_pixels( pixbuf ) + 4 * x + pad * stride;
982 	int j = h;
983 	int i = 0;
984 	uint8_t *d, *s, a;
985 
986 	while( j -- )
987 	{
988 		d = dest;
989 		s = src;
990 		i = w;
991 		while( i -- )
992 		{
993 			a = *s ++;
994 			*d++ = ( a * fg.r + ( 255 - a ) * bg.r ) >> 8;
995 			*d++ = ( a * fg.g + ( 255 - a ) * bg.g ) >> 8;
996 			*d++ = ( a * fg.b + ( 255 - a ) * bg.b ) >> 8;
997 			*d++ = ( a * fg.a + ( 255 - a ) * bg.a ) >> 8;
998 		}
999 		dest += stride;
1000 		src += bitmap->pitch;
1001 	}
1002 }
1003 
fill_pixbuf_with_outline(GdkPixbuf * pixbuf,FT_Bitmap * bitmap,int w,int h,int pad,int align,rgba_color fg,rgba_color bg,rgba_color ol,int outline)1004 static void fill_pixbuf_with_outline( GdkPixbuf* pixbuf, FT_Bitmap* bitmap, int w, int h, int pad, int align, rgba_color fg, rgba_color bg, rgba_color ol, int outline )
1005 {
1006 	int stride = gdk_pixbuf_get_rowstride( pixbuf );
1007 	int x = ( gdk_pixbuf_get_width( pixbuf ) - w - 2 * pad ) * align / 2 + pad;
1008 	uint8_t* dest = gdk_pixbuf_get_pixels( pixbuf ) + 4 * x + pad * stride;
1009 	int j ,i;
1010 	uint8_t *d = NULL;
1011 	float a_ol = 0;
1012 	float a_fg = 0;
1013 
1014 	for ( j = 0; j < h; j++ )
1015 	{
1016 		d = dest;
1017 		for ( i = 0; i < w; i++ )
1018 		{
1019 #define geta(x, y) (float) bitmap->buffer[ (y) * bitmap->pitch + (x) ] / 255.0
1020 
1021 			a_ol = geta(i, j);
1022 			// One pixel fake circle
1023 			if ( i > 0 )
1024 				a_ol = MAX( a_ol, geta(i - 1, j) );
1025 			if ( i < w - 1 )
1026 				a_ol = MAX( a_ol, geta(i + 1, j) );
1027 			if ( j > 0 )
1028 				a_ol = MAX( a_ol, geta(i, j - 1) );
1029 			if ( j < h - 1 )
1030 				a_ol = MAX( a_ol, geta(i, j + 1) );
1031 			if ( outline >= 2 ) {
1032 				// Two pixels fake circle
1033 				if ( i > 1 ) {
1034 					a_ol = MAX( a_ol, geta(i - 2, j) );
1035 					if ( j > 0 )
1036 						a_ol = MAX( a_ol, geta(i - 2, j - 1) );
1037 					if ( j < h - 1 )
1038 						a_ol = MAX( a_ol, geta(i - 2, j + 1) );
1039 				}
1040 				if ( i > 0 ) {
1041 					if ( j > 0 )
1042 						a_ol = MAX( a_ol, geta(i - 1, j - 1) );
1043 					if ( j > 1 )
1044 						a_ol = MAX( a_ol, geta(i - 1, j - 2) );
1045 					if ( j < h - 1 )
1046 						a_ol = MAX( a_ol, geta(i - 1, j + 1) );
1047 					if ( j < h - 2 )
1048 						a_ol = MAX( a_ol, geta(i - 1, j + 2) );
1049 				}
1050 				if ( j > 1 )
1051 					a_ol = MAX( a_ol, geta(i, j - 2) );
1052 				if ( j < h - 2 )
1053 					a_ol = MAX( a_ol, geta(i, j + 2) );
1054 				if ( i < w - 1 ) {
1055 					if ( j > 0 )
1056 						a_ol = MAX( a_ol, geta(i + 1, j - 1) );
1057 					if ( j > 1 )
1058 						a_ol = MAX( a_ol, geta(i + 1, j - 2) );
1059 					if ( j < h - 1 )
1060 						a_ol = MAX( a_ol, geta(i + 1, j + 1) );
1061 					if ( j < h - 2 )
1062 						a_ol = MAX( a_ol, geta(i + 1, j + 2) );
1063 				}
1064 				if ( i < w - 2 ) {
1065 					a_ol = MAX( a_ol, geta(i + 2, j) );
1066 					if ( j > 0 )
1067 						a_ol = MAX( a_ol, geta(i + 2, j - 1) );
1068 					if ( j < h - 1 )
1069 						a_ol = MAX( a_ol, geta(i + 2, j + 1) );
1070 				}
1071 			}
1072 			if ( outline >= 3 ) {
1073 				// Three pixels fake circle
1074 				if ( i > 2 ) {
1075 					a_ol = MAX( a_ol, geta(i - 3, j) );
1076 					if ( j > 0 )
1077 						a_ol = MAX( a_ol, geta(i - 3, j - 1) );
1078 					if ( j < h - 1 )
1079 						a_ol = MAX( a_ol, geta(i - 3, j + 1) );
1080 				}
1081 				if ( i > 1 ) {
1082 					if ( j > 1 )
1083 						a_ol = MAX( a_ol, geta(i - 2, j - 2) );
1084 					if ( j < h - 2 )
1085 						a_ol = MAX( a_ol, geta(i - 2, j + 2) );
1086 				}
1087 				if ( i > 0 ) {
1088 					if ( j > 2 )
1089 						a_ol = MAX( a_ol, geta(i - 1, j - 3) );
1090 					if ( j < h - 3 )
1091 						a_ol = MAX( a_ol, geta(i - 1, j + 3) );
1092 				}
1093 				if ( j > 2 )
1094 					a_ol = MAX( a_ol, geta(i, j - 3) );
1095 				if ( j < h - 3 )
1096 					a_ol = MAX( a_ol, geta(i, j + 3) );
1097 				if ( i < w - 1 ) {
1098 					if ( j > 2 )
1099 						a_ol = MAX( a_ol, geta(i + 1, j - 3) );
1100 					if ( j < h - 3 )
1101 						a_ol = MAX( a_ol, geta(i + 1, j + 3) );
1102 				}
1103 				if ( i < w - 2 ) {
1104 					if ( j > 1 )
1105 						a_ol = MAX( a_ol, geta(i + 2, j - 2) );
1106 					if ( j < h - 2 )
1107 						a_ol = MAX( a_ol, geta(i + 2, j + 2) );
1108 				}
1109 				if ( i < w - 3 ) {
1110 					a_ol = MAX( a_ol, geta(i + 3, j) );
1111 					if ( j > 0 )
1112 						a_ol = MAX( a_ol, geta(i + 3, j - 1) );
1113 					if ( j < h - 1 )
1114 						a_ol = MAX( a_ol, geta(i + 3, j + 1) );
1115 				}
1116 			}
1117 
1118 			a_fg = ( float ) bitmap->buffer[ j * bitmap->pitch + i ] / 255.0;
1119 
1120 			*d++ = ( int ) ( a_fg * fg.r + ( 1 - a_fg ) * ( a_ol * ol.r + ( 1 - a_ol ) * bg.r ) );
1121 			*d++ = ( int ) ( a_fg * fg.g + ( 1 - a_fg ) * ( a_ol * ol.g + ( 1 - a_ol ) * bg.g ) );
1122 			*d++ = ( int ) ( a_fg * fg.b + ( 1 - a_fg ) * ( a_ol * ol.b + ( 1 - a_ol ) * bg.b ) );
1123 			*d++ = ( int ) ( a_fg * fg.a + ( 1 - a_fg ) * ( a_ol * ol.a + ( 1 - a_ol ) * bg.a ) );
1124 		}
1125 		dest += stride;
1126 	}
1127 }
1128 
on_fontmap_reload()1129 static void on_fontmap_reload()
1130 {
1131 	PangoFT2FontMap *new_fontmap = NULL, *old_fontmap = NULL;
1132 
1133 	FcInitReinitialize();
1134 
1135 	new_fontmap = (PangoFT2FontMap*) pango_ft2_font_map_new();
1136 
1137 	pthread_mutex_lock( &pango_mutex );
1138 	old_fontmap = fontmap;
1139 	fontmap = new_fontmap;
1140 	pthread_mutex_unlock( &pango_mutex );
1141 
1142 	if ( old_fontmap )
1143 		g_object_unref( old_fontmap );
1144 }
1145