From 9333eb48998e4cfecce193d1c4046934cf6c6b50 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Sebastian=20Dr=C3=B6ge?= Date: Mon, 5 May 2008 12:33:05 +0000 Subject: [PATCH] gst/subparse/samiparse.c: Remove trailing, leading and double whitespaces. Original commit message from CVS: * gst/subparse/samiparse.c: (handle_start_sync), (end_sami_element), (characters_sami): Remove trailing, leading and double whitespaces. Correctly timestamp buffers and output the last buffer too. * tests/check/elements/subparse.c: (GST_START_TEST), (subparse_suite): Add a simple unit test for SAMI parsing. --- ChangeLog | 11 ++++++++++ gst/subparse/samiparse.c | 45 ++++++++++++++++++++++++++++++++--------- tests/check/elements/subparse.c | 37 +++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 10 deletions(-) diff --git a/ChangeLog b/ChangeLog index 26d8796..7a722d8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,16 @@ 2008-05-05 Sebastian Dröge + * gst/subparse/samiparse.c: (handle_start_sync), + (end_sami_element), (characters_sami): + Remove trailing, leading and double whitespaces. + Correctly timestamp buffers and output the last buffer too. + + * tests/check/elements/subparse.c: (GST_START_TEST), + (subparse_suite): + Add a simple unit test for SAMI parsing. + +2008-05-05 Sebastian Dröge + Patch by: Young-Ho Cha * gst/subparse/samiparse.c: (handle_start_sync), diff --git a/gst/subparse/samiparse.c b/gst/subparse/samiparse.c index 4563279..1cceeaa 100644 --- a/gst/subparse/samiparse.c +++ b/gst/subparse/samiparse.c @@ -131,18 +131,13 @@ handle_start_sync (GstSamiContext * sctx, const xmlChar ** atts) if (!value) continue; if (!xmlStrncmp ((const xmlChar *) "start", key, 5)) { - /* FIXME: this is not correct. According to[0] the start parameter - * specifies the time when the text should be shown and not the time - * when the text should be hidden again. - * This essentially means that we have to look at the next sync element - * before pushing one buffer and push the last buffer with -1 - * as duration after the body element is closed. - * [0] http://msdn.microsoft.com/en-us/library/ms971327.aspx - */ - sctx->time1 = sctx->time2; + /* Only set a new start time if we don't have text pending */ + if (sctx->resultbuf->len == 0) + sctx->time1 = sctx->time2; + sctx->time2 = atoi ((const char *) value) * GST_MSECOND; - sctx->has_result = TRUE; g_string_append (sctx->resultbuf, sctx->buf->str); + sctx->has_result = (sctx->resultbuf->len != 0) ? TRUE : FALSE; g_string_truncate (sctx->buf, 0); } } @@ -248,6 +243,19 @@ end_sami_element (void *ctx, const xmlChar * name) if (!xmlStrncmp ((const xmlChar *) "sync", name, 4)) { sctx->in_sync = FALSE; + } else if (!xmlStrncmp ((const xmlChar *) "body", name, 4)) { + /* We will usually have one buffer left when the body is closed + * as we need the next sync to actually send it */ + if (sctx->buf->len != 0) { + /* Only set a new start time if we don't have text pending */ + if (sctx->resultbuf->len == 0) + sctx->time1 = sctx->time2; + + sctx->time2 = GST_CLOCK_TIME_NONE; + g_string_append (sctx->resultbuf, sctx->buf->str); + sctx->has_result = (sctx->resultbuf->len != 0) ? TRUE : FALSE; + g_string_truncate (sctx->buf, 0); + } } else if (!xmlStrncmp ((const xmlChar *) "font", name, 4)) { sami_context_pop_state (sctx, SPAN_TAG); } else if (!xmlStrncmp ((const xmlChar *) "ruby", name, 4)) { @@ -262,12 +270,29 @@ characters_sami (void *ctx, const xmlChar * ch, int len) { GstSamiContext *sctx = (GstSamiContext *) ctx; gchar *escaped; + gchar *tmp; + gint i; /* Skip everything except content of the sync elements */ if (!sctx->in_sync) return; escaped = g_markup_escape_text ((const gchar *) ch, len); + g_strstrip (escaped); + + /* Remove double spaces forom the string as those are + * usually added by newlines and indention */ + tmp = escaped; + for (i = 0; i <= strlen (escaped); i++) { + escaped[i] = *tmp; + if (*tmp != ' ') { + tmp++; + continue; + } + while (*tmp == ' ') + tmp++; + } + if (has_tag (sctx->state, RT_TAG)) { g_string_append_c (sctx->rubybuf, ' '); g_string_append (sctx->rubybuf, escaped); diff --git a/tests/check/elements/subparse.c b/tests/check/elements/subparse.c index 4101df9..c0b5c45 100644 --- a/tests/check/elements/subparse.c +++ b/tests/check/elements/subparse.c @@ -579,6 +579,42 @@ GST_START_TEST (test_subviewer2) GST_END_TEST; +GST_START_TEST (test_sami) +{ + SubParseInputChunk sami_input[] = { + {"\n" + "\n" + " Subtitle\n" + " \n" + "\n" + "\n" + " \n" + "

\n" + " This is a comment.
\n" + " This is a second comment.\n", + 1000 * GST_MSECOND, 2000 * GST_MSECOND, + "This is a comment.\nThis is a second comment."}, + {" \n" + "

\n" + " This is a third comment.
\n" + " This is a fourth comment.\n" "\n" "\n", + 2000 * GST_MSECOND, GST_CLOCK_TIME_NONE, + "This is a third comment.\nThis is a fourth comment."} + }; + + do_test (sami_input, G_N_ELEMENTS (sami_input), "text/x-pango-markup"); +} + +GST_END_TEST; + /* TODO: * - add/modify tests so that lines aren't dogfed to the parsers in complete * lines or sets of complete lines, but rather in random chunks @@ -606,6 +642,7 @@ subparse_suite (void) tcase_add_test (tc_chain, test_mpl2); tcase_add_test (tc_chain, test_subviewer); tcase_add_test (tc_chain, test_subviewer2); + tcase_add_test (tc_chain, test_sami); return s; } -- 2.7.4