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.
This commit is contained in:
Sebastian Dröge 2008-05-05 12:33:05 +00:00
parent 76e3ffb61c
commit 9333eb4899
3 changed files with 83 additions and 10 deletions

View file

@ -1,3 +1,14 @@
2008-05-05 Sebastian Dröge <slomo@circular-chaos.org>
* 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 <slomo@circular-chaos.org>
Patch by: Young-Ho Cha <ganadist at chollian dot net>

View file

@ -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);

View file

@ -579,6 +579,42 @@ GST_START_TEST (test_subviewer2)
GST_END_TEST;
GST_START_TEST (test_sami)
{
SubParseInputChunk sami_input[] = {
{"<SAMI>\n"
"<HEAD>\n"
" <TITLE>Subtitle</TITLE>\n"
" <STYLE TYPE=\"text/css\">\n"
" <!--\n"
" P {margin-left:8pt; margin-right:8pt; margin-bottom:2pt; margin-top:2pt; text-align:center; font-size:12pt; font-weight:normal; color:black;}\n"
" .CC {Name:English; lang:en-AU; SAMIType:CC;}\n"
" #STDPrn {Name:Standard Print;}\n"
" #LargePrn {Name:Large Print; font-size:24pt;}\n"
" #SmallPrn {Name:Small Print; font-size:16pt;}\n"
" -->\n"
" </Style>\n"
"</HEAD>\n"
"<BODY>\n"
" <SYNC Start=1000>\n"
" <P Class=CC>\n"
" This is a comment.<br>\n"
" This is a second comment.\n",
1000 * GST_MSECOND, 2000 * GST_MSECOND,
"This is a comment.\nThis is a second comment."},
{" <SYNC Start=2000>\n"
" <P Class=CC>\n"
" This is a third comment.<br>\n"
" This is a fourth comment.\n" "</BODY>\n" "</SAMI>\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;
}