ac2d4b379eb897c4a64fcaf35b2aed8b0faac0e4
[platform/upstream/gstreamer.git] / ext / pango / gsttextrender.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  * Copyright (C) <2003> David Schleef <ds@schleef.org>
4  * Copyright (C) <2009> Young-Ho Cha <ganadist@gmail.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library 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 GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22
23 /**
24  * SECTION:element-textrender
25  * @see_also: #GstTextOverlay
26  *
27  * This plugin renders text received on the text sink pad to a video
28  * buffer (retaining the alpha channel), so it can later be overlayed
29  * on top of video streams using other elements.
30  *
31  * The text can contain newline characters. (FIXME: What about text 
32  * wrapping? It does not make sense in this context)
33  *
34  * <refsect2>
35  * <title>Example launch lines</title>
36  * |[
37  * gst-launch -v filesrc location=subtitles.srt ! subparse ! textrender ! xvimagesink
38  * ]|
39  * </refsect2>
40  */
41
42 #ifdef HAVE_CONFIG_H
43 #include <config.h>
44 #endif
45
46 #include <gst/gst.h>
47 #include <gst/video/video.h>
48
49 #include "gsttextrender.h"
50 #include <string.h>
51
52 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
53 # define CAIRO_ARGB_A 3
54 # define CAIRO_ARGB_R 2
55 # define CAIRO_ARGB_G 1
56 # define CAIRO_ARGB_B 0
57 #else
58 # define CAIRO_ARGB_A 0
59 # define CAIRO_ARGB_R 1
60 # define CAIRO_ARGB_G 2
61 # define CAIRO_ARGB_B 3
62 #endif
63
64 GST_DEBUG_CATEGORY_EXTERN (pango_debug);
65 #define GST_CAT_DEFAULT pango_debug
66
67 #define MINIMUM_OUTLINE_OFFSET 1.0
68
69 #define DEFAULT_PROP_VALIGNMENT GST_TEXT_RENDER_VALIGN_BASELINE
70 #define DEFAULT_PROP_HALIGNMENT GST_TEXT_RENDER_HALIGN_CENTER
71 #define DEFAULT_PROP_LINE_ALIGNMENT GST_TEXT_RENDER_LINE_ALIGN_CENTER
72 #define DEFAULT_PROP_XPAD       25
73 #define DEFAULT_PROP_YPAD       25
74
75 #define DEFAULT_RENDER_WIDTH 720
76 #define DEFAULT_RENDER_HEIGHT 576
77
78 enum
79 {
80   PROP_0,
81   PROP_HALIGNMENT,
82   PROP_VALIGNMENT,
83   PROP_LINE_ALIGNMENT,
84   PROP_XPAD,
85   PROP_YPAD,
86   PROP_FONT_DESC
87 };
88
89
90 static GstStaticPadTemplate src_template_factory =
91     GST_STATIC_PAD_TEMPLATE ("src",
92     GST_PAD_SRC,
93     GST_PAD_ALWAYS,
94     GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("AYUV") ";" GST_VIDEO_CAPS_ARGB)
95     );
96
97 static GstStaticPadTemplate sink_template_factory =
98     GST_STATIC_PAD_TEMPLATE ("sink",
99     GST_PAD_SINK,
100     GST_PAD_ALWAYS,
101     GST_STATIC_CAPS ("text/x-pango-markup; text/plain")
102     );
103
104 #define GST_TYPE_TEXT_RENDER_VALIGN (gst_text_render_valign_get_type())
105 static GType
106 gst_text_render_valign_get_type (void)
107 {
108   static GType text_render_valign_type = 0;
109   static const GEnumValue text_render_valign[] = {
110     {GST_TEXT_RENDER_VALIGN_BASELINE, "baseline", "baseline"},
111     {GST_TEXT_RENDER_VALIGN_BOTTOM, "bottom", "bottom"},
112     {GST_TEXT_RENDER_VALIGN_TOP, "top", "top"},
113     {0, NULL, NULL},
114   };
115
116   if (!text_render_valign_type) {
117     text_render_valign_type =
118         g_enum_register_static ("GstTextRenderVAlign", text_render_valign);
119   }
120   return text_render_valign_type;
121 }
122
123 #define GST_TYPE_TEXT_RENDER_HALIGN (gst_text_render_halign_get_type())
124 static GType
125 gst_text_render_halign_get_type (void)
126 {
127   static GType text_render_halign_type = 0;
128   static const GEnumValue text_render_halign[] = {
129     {GST_TEXT_RENDER_HALIGN_LEFT, "left", "left"},
130     {GST_TEXT_RENDER_HALIGN_CENTER, "center", "center"},
131     {GST_TEXT_RENDER_HALIGN_RIGHT, "right", "right"},
132     {0, NULL, NULL},
133   };
134
135   if (!text_render_halign_type) {
136     text_render_halign_type =
137         g_enum_register_static ("GstTextRenderHAlign", text_render_halign);
138   }
139   return text_render_halign_type;
140 }
141
142 #define GST_TYPE_TEXT_RENDER_LINE_ALIGN (gst_text_render_line_align_get_type())
143 static GType
144 gst_text_render_line_align_get_type (void)
145 {
146   static GType text_render_line_align_type = 0;
147   static const GEnumValue text_render_line_align[] = {
148     {GST_TEXT_RENDER_LINE_ALIGN_LEFT, "left", "left"},
149     {GST_TEXT_RENDER_LINE_ALIGN_CENTER, "center", "center"},
150     {GST_TEXT_RENDER_LINE_ALIGN_RIGHT, "right", "right"},
151     {0, NULL, NULL}
152   };
153
154   if (!text_render_line_align_type) {
155     text_render_line_align_type =
156         g_enum_register_static ("GstTextRenderLineAlign",
157         text_render_line_align);
158   }
159   return text_render_line_align_type;
160 }
161
162 static void gst_text_render_adjust_values_with_fontdesc (GstTextRender *
163     render, PangoFontDescription * desc);
164
165 #define gst_text_render_parent_class parent_class
166 G_DEFINE_TYPE (GstTextRender, gst_text_render, GST_TYPE_ELEMENT);
167
168 static void gst_text_render_finalize (GObject * object);
169 static void gst_text_render_set_property (GObject * object,
170     guint prop_id, const GValue * value, GParamSpec * pspec);
171 static void gst_text_render_get_property (GObject * object,
172     guint prop_id, GValue * value, GParamSpec * pspec);
173
174 static void
175 gst_text_render_class_init (GstTextRenderClass * klass)
176 {
177   GObjectClass *gobject_class;
178   GstElementClass *gstelement_class;
179   PangoFontMap *fontmap;
180
181   gobject_class = (GObjectClass *) klass;
182   gstelement_class = (GstElementClass *) klass;
183
184   parent_class = g_type_class_peek_parent (klass);
185
186   gobject_class->finalize = gst_text_render_finalize;
187   gobject_class->set_property = gst_text_render_set_property;
188   gobject_class->get_property = gst_text_render_get_property;
189
190   gst_element_class_add_pad_template (gstelement_class,
191       gst_static_pad_template_get (&src_template_factory));
192   gst_element_class_add_pad_template (gstelement_class,
193       gst_static_pad_template_get (&sink_template_factory));
194
195   gst_element_class_set_details_simple (gstelement_class, "Text renderer",
196       "Filter/Editor/Video",
197       "Renders a text string to an image bitmap",
198       "David Schleef <ds@schleef.org>, "
199       "GStreamer maintainers <gstreamer-devel@lists.sourceforge.net>");
200
201   fontmap = pango_cairo_font_map_get_default ();
202   klass->pango_context =
203       pango_cairo_font_map_create_context (PANGO_CAIRO_FONT_MAP (fontmap));
204   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_FONT_DESC,
205       g_param_spec_string ("font-desc", "font description",
206           "Pango font description of font "
207           "to be used for rendering. "
208           "See documentation of "
209           "pango_font_description_from_string"
210           " for syntax.", "", G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
211   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_VALIGNMENT,
212       g_param_spec_enum ("valignment", "vertical alignment",
213           "Vertical alignment of the text", GST_TYPE_TEXT_RENDER_VALIGN,
214           DEFAULT_PROP_VALIGNMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
215   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_HALIGNMENT,
216       g_param_spec_enum ("halignment", "horizontal alignment",
217           "Horizontal alignment of the text", GST_TYPE_TEXT_RENDER_HALIGN,
218           DEFAULT_PROP_HALIGNMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
219   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_XPAD,
220       g_param_spec_int ("xpad", "horizontal paddding",
221           "Horizontal paddding when using left/right alignment", 0, G_MAXINT,
222           DEFAULT_PROP_XPAD, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
223   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_YPAD,
224       g_param_spec_int ("ypad", "vertical padding",
225           "Vertical padding when using top/bottom alignment", 0, G_MAXINT,
226           DEFAULT_PROP_YPAD, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
227   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_LINE_ALIGNMENT,
228       g_param_spec_enum ("line-alignment", "line alignment",
229           "Alignment of text lines relative to each other.",
230           GST_TYPE_TEXT_RENDER_LINE_ALIGN, DEFAULT_PROP_LINE_ALIGNMENT,
231           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
232 }
233
234 static void
235 gst_text_render_adjust_values_with_fontdesc (GstTextRender * render,
236     PangoFontDescription * desc)
237 {
238   gint font_size = pango_font_description_get_size (desc) / PANGO_SCALE;
239
240   render->shadow_offset = (double) (font_size) / 13.0;
241   render->outline_offset = (double) (font_size) / 15.0;
242   if (render->outline_offset < MINIMUM_OUTLINE_OFFSET)
243     render->outline_offset = MINIMUM_OUTLINE_OFFSET;
244 }
245
246 static void
247 gst_text_render_render_pangocairo (GstTextRender * render)
248 {
249   cairo_t *cr;
250   cairo_surface_t *surface;
251   cairo_t *cr_shadow;
252   cairo_surface_t *surface_shadow;
253   PangoRectangle ink_rect, logical_rect;
254   gint width, height;
255
256   pango_layout_get_pixel_extents (render->layout, &ink_rect, &logical_rect);
257
258   width = logical_rect.width + render->shadow_offset;
259   height = logical_rect.height + logical_rect.y + render->shadow_offset;
260
261   surface_shadow = cairo_image_surface_create (CAIRO_FORMAT_A8, width, height);
262   cr_shadow = cairo_create (surface_shadow);
263
264   /* clear shadow surface */
265   cairo_set_operator (cr_shadow, CAIRO_OPERATOR_CLEAR);
266   cairo_paint (cr_shadow);
267   cairo_set_operator (cr_shadow, CAIRO_OPERATOR_OVER);
268
269   /* draw shadow text */
270   cairo_save (cr_shadow);
271   cairo_set_source_rgba (cr_shadow, 0.0, 0.0, 0.0, 0.5);
272   cairo_translate (cr_shadow, render->shadow_offset, render->shadow_offset);
273   pango_cairo_show_layout (cr_shadow, render->layout);
274   cairo_restore (cr_shadow);
275
276   /* draw outline text */
277   cairo_save (cr_shadow);
278   cairo_set_source_rgb (cr_shadow, 0.0, 0.0, 0.0);
279   cairo_set_line_width (cr_shadow, render->outline_offset);
280   pango_cairo_layout_path (cr_shadow, render->layout);
281   cairo_stroke (cr_shadow);
282   cairo_restore (cr_shadow);
283
284   cairo_destroy (cr_shadow);
285
286   render->text_image = g_realloc (render->text_image, 4 * width * height);
287
288   surface = cairo_image_surface_create_for_data (render->text_image,
289       CAIRO_FORMAT_ARGB32, width, height, width * 4);
290   cr = cairo_create (surface);
291   cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
292   cairo_paint (cr);
293   cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
294
295   /* set default color */
296   cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
297
298   cairo_save (cr);
299   /* draw text */
300   pango_cairo_show_layout (cr, render->layout);
301   cairo_restore (cr);
302
303   /* composite shadow with offset */
304   cairo_set_operator (cr, CAIRO_OPERATOR_DEST_OVER);
305   cairo_set_source_surface (cr, surface_shadow, 0.0, 0.0);
306   cairo_paint (cr);
307
308   cairo_destroy (cr);
309   cairo_surface_destroy (surface_shadow);
310   cairo_surface_destroy (surface);
311   render->image_width = width;
312   render->image_height = height;
313 }
314
315 static void
316 gst_text_render_check_argb (GstTextRender * render)
317 {
318   GstCaps *peer_caps;
319   peer_caps = gst_pad_get_allowed_caps (render->srcpad);
320   if (G_LIKELY (peer_caps)) {
321     guint i = 0, n = 0;
322
323     n = gst_caps_get_size (peer_caps);
324     GST_DEBUG_OBJECT (render, "peer allowed caps (%u structure(s)) are %"
325         GST_PTR_FORMAT, n, peer_caps);
326
327     /* Check if AYUV or ARGB is first */
328     for (i = 0; i < n; i++) {
329       GstStructure *s = gst_caps_get_structure (peer_caps, i);
330       if (gst_structure_has_name (s, "video/x-raw-rgb") &&
331           gst_structure_has_field (s, "alpha_mask")) {
332         render->use_ARGB = TRUE;
333         break;
334       } else if (gst_structure_has_name (s, "video/x-raw-yuv")) {
335         guint fourcc;
336         if (gst_structure_get_fourcc (s, "format", &fourcc) &&
337             fourcc == GST_MAKE_FOURCC ('A', 'Y', 'U', 'V')) {
338           render->use_ARGB = FALSE;
339           break;
340         }
341       }
342     }
343     gst_caps_unref (peer_caps);
344   }
345 }
346
347 static gboolean
348 gst_text_render_src_setcaps (GstTextRender * render, GstCaps * caps)
349 {
350   GstStructure *structure;
351   gboolean ret;
352   gint width = 0, height = 0;
353
354   structure = gst_caps_get_structure (caps, 0);
355   gst_structure_get_int (structure, "width", &width);
356   gst_structure_get_int (structure, "height", &height);
357
358   GST_DEBUG_OBJECT (render, "Got caps %" GST_PTR_FORMAT, caps);
359
360   if (width >= render->image_width && height >= render->image_height) {
361     render->width = width;
362     render->height = height;
363   }
364
365   gst_text_render_check_argb (render);
366
367   ret = gst_pad_push_event (render->srcpad, gst_event_new_caps (caps));
368
369   return ret;
370 }
371
372 static void
373 gst_text_render_fixate_caps (GstPad * pad, GstCaps * caps)
374 {
375   GstTextRender *render = GST_TEXT_RENDER (gst_pad_get_parent (pad));
376   GstStructure *s = gst_caps_get_structure (caps, 0);
377
378   GST_DEBUG ("Fixating caps %" GST_PTR_FORMAT, caps);
379   gst_structure_fixate_field_nearest_int (s, "width", MAX (render->image_width,
380           DEFAULT_RENDER_WIDTH));
381   gst_structure_fixate_field_nearest_int (s, "height",
382       MAX (render->image_height + render->ypad, DEFAULT_RENDER_HEIGHT));
383   GST_DEBUG ("Fixated to    %" GST_PTR_FORMAT, caps);
384
385   gst_object_unref (render);
386 }
387
388 #define CAIRO_UNPREMULTIPLY(a,r,g,b) G_STMT_START { \
389   b = (a > 0) ? MIN ((b * 255 + a / 2) / a, 255) : 0; \
390   g = (a > 0) ? MIN ((g * 255 + a / 2) / a, 255) : 0; \
391   r = (a > 0) ? MIN ((r * 255 + a / 2) / a, 255) : 0; \
392 } G_STMT_END
393
394 static void
395 gst_text_renderer_image_to_ayuv (GstTextRender * render, guchar * pixbuf,
396     int xpos, int ypos, int stride)
397 {
398   int y;                        /* text bitmap coordinates */
399   guchar *p, *bitp;
400   guchar a, r, g, b;
401   int width, height;
402
403   width = render->image_width;
404   height = render->image_height;
405
406   for (y = 0; y < height && ypos + y < render->height; y++) {
407     int n;
408     p = pixbuf + (ypos + y) * stride + xpos * 4;
409     bitp = render->text_image + y * width * 4;
410     for (n = 0; n < width && n < render->width; n++) {
411       b = bitp[CAIRO_ARGB_B];
412       g = bitp[CAIRO_ARGB_G];
413       r = bitp[CAIRO_ARGB_R];
414       a = bitp[CAIRO_ARGB_A];
415       bitp += 4;
416
417       /* Cairo uses pre-multiplied ARGB, unpremultiply it */
418       CAIRO_UNPREMULTIPLY (a, r, g, b);
419
420       *p++ = a;
421       *p++ = CLAMP ((int) (((19595 * r) >> 16) + ((38470 * g) >> 16) +
422               ((7471 * b) >> 16)), 0, 255);
423       *p++ = CLAMP ((int) (-((11059 * r) >> 16) - ((21709 * g) >> 16) +
424               ((32768 * b) >> 16) + 128), 0, 255);
425       *p++ = CLAMP ((int) (((32768 * r) >> 16) - ((27439 * g) >> 16) -
426               ((5329 * b) >> 16) + 128), 0, 255);
427     }
428   }
429 }
430
431 static void
432 gst_text_renderer_image_to_argb (GstTextRender * render, guchar * pixbuf,
433     int xpos, int ypos, int stride)
434 {
435   int i, j;
436   guchar *p, *bitp;
437   int width, height;
438
439   width = render->image_width;
440   height = render->image_height;
441
442   for (i = 0; i < height && ypos + i < render->height; i++) {
443     p = pixbuf + (ypos + i) * stride + xpos * 4;
444     bitp = render->text_image + i * width * 4;
445     for (j = 0; j < width && j < render->width; j++) {
446       p[0] = bitp[CAIRO_ARGB_A];
447       p[1] = bitp[CAIRO_ARGB_R];
448       p[2] = bitp[CAIRO_ARGB_G];
449       p[3] = bitp[CAIRO_ARGB_B];
450
451       /* Cairo uses pre-multiplied ARGB, unpremultiply it */
452       CAIRO_UNPREMULTIPLY (p[0], p[1], p[2], p[3]);
453
454       bitp += 4;
455       p += 4;
456     }
457   }
458 }
459
460 static GstFlowReturn
461 gst_text_render_chain (GstPad * pad, GstBuffer * inbuf)
462 {
463   GstTextRender *render;
464   GstFlowReturn ret;
465   GstBuffer *outbuf;
466   GstCaps *caps = NULL, *padcaps;
467   guint8 *data;
468   gsize size;
469   gint n;
470   gint xpos, ypos;
471
472   render = GST_TEXT_RENDER (gst_pad_get_parent (pad));
473
474   data = gst_buffer_map (inbuf, &size, NULL, GST_MAP_READ);
475
476   /* somehow pango barfs over "\0" buffers... */
477   while (size > 0 &&
478       (data[size - 1] == '\r' ||
479           data[size - 1] == '\n' || data[size - 1] == '\0')) {
480     size--;
481   }
482
483   /* render text */
484   GST_DEBUG ("rendering '%*s'", size, data);
485   pango_layout_set_markup (render->layout, (gchar *) data, size);
486   gst_text_render_render_pangocairo (render);
487   gst_buffer_unmap (inbuf, data, size);
488
489   gst_text_render_check_argb (render);
490
491   padcaps = gst_pad_get_caps (render->srcpad, NULL);
492   caps = gst_pad_peer_get_caps (render->srcpad, padcaps);
493   gst_caps_unref (padcaps);
494
495   if (!caps || gst_caps_is_empty (caps)) {
496     GST_ELEMENT_ERROR (render, CORE, NEGOTIATION, (NULL), (NULL));
497     ret = GST_FLOW_ERROR;
498     goto done;
499   }
500
501   gst_caps_truncate (caps);
502   gst_pad_fixate_caps (render->srcpad, caps);
503
504   if (!gst_text_render_src_setcaps (render, caps)) {
505     GST_ELEMENT_ERROR (render, CORE, NEGOTIATION, (NULL), (NULL));
506     ret = GST_FLOW_ERROR;
507     goto done;
508   }
509
510   GST_DEBUG ("Allocating buffer WxH = %dx%d", render->width, render->height);
511   outbuf = gst_buffer_new_and_alloc (render->width * render->height * 4);
512
513   gst_buffer_copy_into (outbuf, inbuf, GST_BUFFER_COPY_TIMESTAMPS, 0, -1);
514   data = gst_buffer_map (outbuf, &size, NULL, GST_MAP_WRITE);
515
516   if (render->use_ARGB) {
517     memset (data, 0, render->width * render->height * 4);
518   } else {
519     for (n = 0; n < render->width * render->height; n++) {
520       data[n * 4] = data[n * 4 + 1] = 0;
521       data[n * 4 + 2] = data[n * 4 + 3] = 128;
522     }
523   }
524
525   switch (render->halign) {
526     case GST_TEXT_RENDER_HALIGN_LEFT:
527       xpos = render->xpad;
528       break;
529     case GST_TEXT_RENDER_HALIGN_CENTER:
530       xpos = (render->width - render->image_width) / 2;
531       break;
532     case GST_TEXT_RENDER_HALIGN_RIGHT:
533       xpos = render->width - render->image_width - render->xpad;
534       break;
535     default:
536       xpos = 0;
537   }
538
539   switch (render->valign) {
540     case GST_TEXT_RENDER_VALIGN_BOTTOM:
541       ypos = render->height - render->image_height - render->ypad;
542       break;
543     case GST_TEXT_RENDER_VALIGN_BASELINE:
544       ypos = render->height - (render->image_height + render->ypad);
545       break;
546     case GST_TEXT_RENDER_VALIGN_TOP:
547       ypos = render->ypad;
548       break;
549     default:
550       ypos = render->ypad;
551       break;
552   }
553
554   if (render->text_image) {
555     if (render->use_ARGB) {
556       gst_text_renderer_image_to_argb (render, data, xpos, ypos,
557           render->width * 4);
558     } else {
559       gst_text_renderer_image_to_ayuv (render, data, xpos, ypos,
560           render->width * 4);
561     }
562   }
563   gst_buffer_unmap (outbuf, data, size);
564
565   ret = gst_pad_push (render->srcpad, outbuf);
566
567 done:
568   if (caps)
569     gst_caps_unref (caps);
570   gst_buffer_unref (inbuf);
571   gst_object_unref (render);
572   return ret;
573 }
574
575 static void
576 gst_text_render_finalize (GObject * object)
577 {
578   GstTextRender *render = GST_TEXT_RENDER (object);
579
580   g_free (render->text_image);
581
582   if (render->layout)
583     g_object_unref (render->layout);
584
585   G_OBJECT_CLASS (parent_class)->finalize (object);
586 }
587
588 static void
589 gst_text_render_init (GstTextRender * render)
590 {
591   GstPadTemplate *template;
592
593   /* sink */
594   template = gst_static_pad_template_get (&sink_template_factory);
595   render->sinkpad = gst_pad_new_from_template (template, "sink");
596   gst_object_unref (template);
597   gst_pad_set_chain_function (render->sinkpad,
598       GST_DEBUG_FUNCPTR (gst_text_render_chain));
599   gst_element_add_pad (GST_ELEMENT (render), render->sinkpad);
600
601   /* source */
602   template = gst_static_pad_template_get (&src_template_factory);
603   render->srcpad = gst_pad_new_from_template (template, "src");
604   gst_object_unref (template);
605   gst_pad_set_fixatecaps_function (render->srcpad,
606       GST_DEBUG_FUNCPTR (gst_text_render_fixate_caps));
607
608   gst_element_add_pad (GST_ELEMENT (render), render->srcpad);
609
610   render->line_align = DEFAULT_PROP_LINE_ALIGNMENT;
611   render->layout =
612       pango_layout_new (GST_TEXT_RENDER_GET_CLASS (render)->pango_context);
613   pango_layout_set_alignment (render->layout,
614       (PangoAlignment) render->line_align);
615
616   render->halign = DEFAULT_PROP_HALIGNMENT;
617   render->valign = DEFAULT_PROP_VALIGNMENT;
618   render->xpad = DEFAULT_PROP_XPAD;
619   render->ypad = DEFAULT_PROP_YPAD;
620
621   render->width = DEFAULT_RENDER_WIDTH;
622   render->height = DEFAULT_RENDER_HEIGHT;
623
624   render->use_ARGB = FALSE;
625 }
626
627 static void
628 gst_text_render_set_property (GObject * object, guint prop_id,
629     const GValue * value, GParamSpec * pspec)
630 {
631   GstTextRender *render = GST_TEXT_RENDER (object);
632
633   switch (prop_id) {
634     case PROP_VALIGNMENT:
635       render->valign = g_value_get_enum (value);
636       break;
637     case PROP_HALIGNMENT:
638       render->halign = g_value_get_enum (value);
639       break;
640     case PROP_LINE_ALIGNMENT:
641       render->line_align = g_value_get_enum (value);
642       pango_layout_set_alignment (render->layout,
643           (PangoAlignment) render->line_align);
644       break;
645     case PROP_XPAD:
646       render->xpad = g_value_get_int (value);
647       break;
648     case PROP_YPAD:
649       render->ypad = g_value_get_int (value);
650       break;
651     case PROP_FONT_DESC:
652     {
653       PangoFontDescription *desc;
654
655       desc = pango_font_description_from_string (g_value_get_string (value));
656       if (desc) {
657         GST_LOG ("font description set: %s", g_value_get_string (value));
658         GST_OBJECT_LOCK (render);
659         pango_layout_set_font_description (render->layout, desc);
660         gst_text_render_adjust_values_with_fontdesc (render, desc);
661         pango_font_description_free (desc);
662         gst_text_render_render_pangocairo (render);
663         GST_OBJECT_UNLOCK (render);
664       } else {
665         GST_WARNING ("font description parse failed: %s",
666             g_value_get_string (value));
667       }
668       break;
669     }
670
671     default:
672       break;
673   }
674 }
675
676 static void
677 gst_text_render_get_property (GObject * object, guint prop_id,
678     GValue * value, GParamSpec * pspec)
679 {
680   GstTextRender *render = GST_TEXT_RENDER (object);
681
682   switch (prop_id) {
683     case PROP_VALIGNMENT:
684       g_value_set_enum (value, render->valign);
685       break;
686     case PROP_HALIGNMENT:
687       g_value_set_enum (value, render->halign);
688       break;
689     case PROP_LINE_ALIGNMENT:
690       g_value_set_enum (value, render->line_align);
691       break;
692     case PROP_XPAD:
693       g_value_set_int (value, render->xpad);
694       break;
695     case PROP_YPAD:
696       g_value_set_int (value, render->ypad);
697       break;
698     default:
699       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
700       break;
701   }
702 }