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