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